From 9d288158c11ecfd26e2a9f3be6fceac4148b0f20 Mon Sep 17 00:00:00 2001 From: barthy Date: Wed, 24 Apr 2024 09:15:24 +0200 Subject: [PATCH] enh(gcode): review fixes and on:begin filter --- src/languages/gcode.js | 71 +++++++++++++++++++++++---- test/markup/gcode/default.expect.txt | 6 +-- test/markup/gcode/extended.expect.txt | 22 ++++----- test/markup/gcode/extended.txt | 2 +- 4 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/languages/gcode.js b/src/languages/gcode.js index 45db86f772..a26b2a7849 100644 --- a/src/languages/gcode.js +++ b/src/languages/gcode.js @@ -10,7 +10,7 @@ export default function(hljs) { const regex = hljs.regex; const GCODE_KEYWORDS = { $pattern: /[A-Z]+|%/, - keywords: [ + keyword: [ // conditions 'THEN', 'ELSE', @@ -62,9 +62,32 @@ export default function(hljs) { }; - const LETTER_BOUNDARY_RE = /(?= '0' && charBeforeMatch <= '9') { + return; + } + + response.ignoreMatch(); + } + const NUMBER_RE = /[+-]?((\.\d+)|(\d+)(\.\d*)?)/; + const GENERAL_MISC_FUNCTION_RE = /[GM]\s*\d+(\.\d+)?/; + const TOOLS_RE = /T\s*\d+/; + const SUBROUTINE_RE = /O\s*\d+/; + const SUBROUTINE_NAMED_RE = /O<.+>/; + const AXES_RE = /[ABCUVWXYZ]\s*/; + const PARAMETERS_RE = /[FHIJKPQRS]\s*/; + const GCODE_CODE = [ // comments hljs.COMMENT(/\(/, /\)/), @@ -79,9 +102,17 @@ export default function(hljs) { variants: [ // G General functions: G0, G5.1, G5.2, … // M Misc functions: M0, M55.6, M199, … - { match: regex.concat(LETTER_BOUNDARY_RE, /[GM]\s*\d+(\.\d+)?/) }, + { match: regex.concat(LETTER_BOUNDARY_RE, GENERAL_MISC_FUNCTION_RE) }, + { + begin: GENERAL_MISC_FUNCTION_RE, + 'on:begin': LETTER_BOUNDARY_CALLBACK + }, // T Tools - { match: regex.concat(LETTER_BOUNDARY_RE, /T\s*\d+/) } + { match: regex.concat(LETTER_BOUNDARY_RE, TOOLS_RE), }, + { + begin: TOOLS_RE, + 'on:begin': LETTER_BOUNDARY_CALLBACK + } ] }, @@ -89,9 +120,17 @@ export default function(hljs) { scope: 'symbol', variants: [ // O Subroutine ID: O100, O110, … - { match: regex.concat(LETTER_BOUNDARY_RE, /O\s*\d+/) }, + { match: regex.concat(LETTER_BOUNDARY_RE, SUBROUTINE_RE) }, + { + begin: SUBROUTINE_RE, + 'on:begin': LETTER_BOUNDARY_CALLBACK + }, // O Subroutine name: O, … - { match: regex.concat(LETTER_BOUNDARY_RE, /O<.+>/) }, + { match: regex.concat(LETTER_BOUNDARY_RE, SUBROUTINE_NAMED_RE) }, + { + begin: SUBROUTINE_NAMED_RE, + 'on:begin': LETTER_BOUNDARY_CALLBACK + }, // Checksum at end of line: *71, *199, … { match: /\*\s*\d+\s*$/ } ] @@ -108,13 +147,25 @@ export default function(hljs) { }, { - scope: 'property', // Physical axes - match: regex.concat(LETTER_BOUNDARY_RE, /[ABCUVWXYZ]\s*/, NUMBER_RE) + scope: 'property', // Physical axes, + variants: [ + { match: regex.concat(LETTER_BOUNDARY_RE, AXES_RE, NUMBER_RE) }, + { + begin: regex.concat(AXES_RE, NUMBER_RE), + 'on:begin': LETTER_BOUNDARY_CALLBACK + }, + ] }, { scope: 'params', // Different types of parameters - match: regex.concat(LETTER_BOUNDARY_RE, /[FHIJKPQRS]\s*/, NUMBER_RE) + variants: [ + { match: regex.concat(LETTER_BOUNDARY_RE, PARAMETERS_RE, NUMBER_RE) }, + { + begin: regex.concat(PARAMETERS_RE, NUMBER_RE), + 'on:begin': LETTER_BOUNDARY_CALLBACK + }, + ] }, ]; @@ -124,6 +175,8 @@ export default function(hljs) { // Some implementations (CNC controls) of G-code are interoperable with uppercase and lowercase letters seamlessly. // However, most prefer all uppercase and uppercase is customary. case_insensitive: true, + // TODO: post v12 with the use of look-behind this can be enabled + disableAutodetect: true, keywords: GCODE_KEYWORDS, contains: GCODE_CODE }; diff --git a/test/markup/gcode/default.expect.txt b/test/markup/gcode/default.expect.txt index 81cc7431be..e0db3161f4 100644 --- a/test/markup/gcode/default.expect.txt +++ b/test/markup/gcode/default.expect.txt @@ -15,17 +15,17 @@ N14 G0 X-.6 Y1.590 N15 G0 Z.1 N16 M5 G49 G28 G91 Z0 -N17 CALL O9456 +N17 CALL O9456 N18 #500=0.004 N19 #503=[#500+#501] N20 VC45=0.0006 VS4=0.0007 N21 G90 G10 L20 P3 X5.Y4. Z6.567 N22 G0 X5000 -N23 IF [#1 LT 0.370] GOTO 49 +N23 IF [#1 LT 0.370] GOTO 49 N24 X-0.678 Y+.990 N25 G84.3 X-0.1 N26 #4=#5*COS[45] N27 #4=#5*SIN[45] N28 VZOFZ=652.9658 -% +% diff --git a/test/markup/gcode/extended.expect.txt b/test/markup/gcode/extended.expect.txt index a5d82a283a..954d2f3a2a 100644 --- a/test/markup/gcode/extended.expect.txt +++ b/test/markup/gcode/extended.expect.txt @@ -1,4 +1,4 @@ -%; something important +%; something important ; another comment @@ -12,7 +12,7 @@ M500; comment after code M500 (comment after code) -O<boring> SUB +O<boring> SUB #20 = [#1 * SIN[#1]] #21 = [-#2 * COS[#2]] @@ -25,7 +25,7 @@ ; WITH SANE SPACING -N1 O107 IF [#10 GT 5] +N1 O107 IF [#10 GT 5] N2 G0 A0 B0 C0 U0 V0 W0 X0 Y0 Z0 *71 N3 G100 A100 B0 N4 G5.2 A10.2 B0 @@ -35,12 +35,12 @@ N8 T0 N9 M100 F1 H2 I3 J4 K5 P6 Q7 R8 S9 N10 M587 S"MYROUTER" P"ABCxyz;" "123" -N10 O107 ENDIF +N10 O107 ENDIF ; WITH EXCESSIVE SPACING -O 108 IF [#10 GT 5] +O 108 IF [#10 GT 5] N 102 G 0 A 0 B 0 C 0 U 0 V 0 W 0 X 0 Y 0 Z 0 *71 N 103 G 100 A 100 B 0 N 104 G 5.2 A 10.2 B 0 @@ -50,12 +50,12 @@ N 108 T 0 N 109 M 100 F 1 H 2 I 3 J 4 K 5 P 6 Q 7 R 8 S 9 N 110 M 587 S "MYROUTER" P "ABCxyz;" "123" -O 108 ENDIF +O 108 ENDIF ; WITHOUT SPACING -O109IF[#10GT5] +O109IF[#10GT5] N202G0A0B0C0U0V0W0X0Y0Z0*71 N203G100A100B0 N204G5.2A10.2B0 @@ -65,11 +65,11 @@ N208T0 N209M100F1H2I3J4K5P6Q7R8S9 N210M587S"MYROUTER"P"ABCxyz;""123" -0O109ENDIF +O109ENDIF -O<boring> ENDSUB +O<boring> ENDSUB -O<boring> CALL [1] [2] [3] [4] [5] +O<boring> CALL [1] [2] [3] [4] [5] ; Words as seen in Klipper Firmware GCode @@ -81,4 +81,4 @@ SET_SKEW XY=100.0000, SKEW_PROFILE SAVE="this name has spaces" SAVE_CONFIG -% +% diff --git a/test/markup/gcode/extended.txt b/test/markup/gcode/extended.txt index fcbaaf54a5..dbcf52ed7b 100644 --- a/test/markup/gcode/extended.txt +++ b/test/markup/gcode/extended.txt @@ -65,7 +65,7 @@ N207G100A+.2B0 N208T0 N209M100F1H2I3J4K5P6Q7R8S9 N210M587S"MYROUTER"P"ABCxyz;""123" -0O109ENDIF +O109ENDIF O ENDSUB