Skip to content

Commit

Permalink
enh(gcode): review fixes and on:begin filter
Browse files Browse the repository at this point in the history
  • Loading branch information
barthy-koeln committed Apr 24, 2024
1 parent 3bb7e65 commit 1daed93
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
71 changes: 62 additions & 9 deletions src/languages/gcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function(hljs) {
const regex = hljs.regex;
const GCODE_KEYWORDS = {
$pattern: /[A-Z]+|%/,
keywords: [
keyword: [
// conditions
'THEN',
'ELSE',
Expand Down Expand Up @@ -62,9 +62,32 @@ export default function(hljs) {
};


const LETTER_BOUNDARY_RE = /(?<![A-Z])/;
// TODO: post v12 lets use look-behind, until then \b and a callback filter will be used
// const LETTER_BOUNDARY_RE = /(?<![A-Z])/;
const LETTER_BOUNDARY_RE = /\b/;

function LETTER_BOUNDARY_CALLBACK(matchdata, response) {
if (matchdata.index === 0) {
return;
}

const charBeforeMatch = matchdata.input[matchdata.index - 1];
if (charBeforeMatch >= '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(/\(/, /\)/),
Expand All @@ -79,19 +102,35 @@ 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
}
]
},

{
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<some>, …
{ 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*$/ }
]
Expand All @@ -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
},
]
},
];

Expand All @@ -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
};
Expand Down
6 changes: 3 additions & 3 deletions test/markup/gcode/default.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
<span class="hljs-operator">N14</span> <span class="hljs-title function_">G0</span> <span class="hljs-property">X-.6</span> <span class="hljs-property">Y1.590</span>
<span class="hljs-operator">N15</span> <span class="hljs-title function_">G0</span> <span class="hljs-property">Z.1</span>
<span class="hljs-operator">N16</span> <span class="hljs-title function_">M5</span> <span class="hljs-title function_">G49</span> <span class="hljs-title function_">G28</span> <span class="hljs-title function_">G91</span> <span class="hljs-property">Z0</span>
<span class="hljs-operator">N17</span> <span class="hljs-keywords">CALL</span> <span class="hljs-symbol">O9456</span>
<span class="hljs-operator">N17</span> <span class="hljs-keyword">CALL</span> <span class="hljs-symbol">O9456</span>
<span class="hljs-operator">N18</span> <span class="hljs-variable">#500</span>=<span class="hljs-number">0.004</span>
<span class="hljs-operator">N19</span> <span class="hljs-variable">#503</span>=[<span class="hljs-variable">#500</span>+<span class="hljs-variable">#501</span>]
<span class="hljs-operator">N20</span> VC45=<span class="hljs-number">0.0006</span>
VS4=<span class="hljs-number">0.0007</span>
<span class="hljs-operator">N21</span> <span class="hljs-title function_">G90</span> <span class="hljs-title function_">G10</span> L20 <span class="hljs-params">P3</span> <span class="hljs-property">X5.</span><span class="hljs-property">Y4.</span> <span class="hljs-property">Z6.567</span>
<span class="hljs-operator">N22</span> <span class="hljs-title function_">G0</span> <span class="hljs-property">X5000</span>
<span class="hljs-operator">N23</span> <span class="hljs-keywords">IF</span> [<span class="hljs-variable">#1</span> <span class="hljs-keywords">LT</span> <span class="hljs-number">0.370</span>] <span class="hljs-keywords">GOTO</span> <span class="hljs-number">49</span>
<span class="hljs-operator">N23</span> <span class="hljs-keyword">IF</span> [<span class="hljs-variable">#1</span> <span class="hljs-keyword">LT</span> <span class="hljs-number">0.370</span>] <span class="hljs-keyword">GOTO</span> <span class="hljs-number">49</span>
<span class="hljs-operator">N24</span> <span class="hljs-property">X-0.678</span> <span class="hljs-property">Y+.990</span>
<span class="hljs-operator">N25</span> <span class="hljs-title function_">G84.3</span> <span class="hljs-property">X-0.1</span>
<span class="hljs-operator">N26</span> <span class="hljs-variable">#4</span>=<span class="hljs-variable">#5</span>*<span class="hljs-built_in">COS</span>[<span class="hljs-number">45</span>]
<span class="hljs-operator">N27</span> <span class="hljs-variable">#4</span>=<span class="hljs-variable">#5</span>*<span class="hljs-built_in">SIN</span>[<span class="hljs-number">45</span>]
<span class="hljs-operator">N28</span> VZOFZ=<span class="hljs-number">652.9658</span>
<span class="hljs-keywords">%</span>
<span class="hljs-keyword">%</span>
22 changes: 11 additions & 11 deletions test/markup/gcode/extended.expect.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<span class="hljs-keywords">%</span><span class="hljs-comment">; something important</span>
<span class="hljs-keyword">%</span><span class="hljs-comment">; something important</span>

<span class="hljs-comment">; another comment</span>

Expand All @@ -12,7 +12,7 @@
<span class="hljs-title function_">M500</span><span class="hljs-comment">; comment after code</span>
<span class="hljs-title function_">M500</span> <span class="hljs-comment">(comment after code)</span>

<span class="hljs-symbol">O&lt;boring&gt;</span> <span class="hljs-keywords">SUB</span>
<span class="hljs-symbol">O&lt;boring&gt;</span> <span class="hljs-keyword">SUB</span>

<span class="hljs-variable">#20</span> = [<span class="hljs-variable">#1</span> * <span class="hljs-built_in">SIN</span>[<span class="hljs-variable">#1</span>]]
<span class="hljs-variable">#21</span> = [<span class="hljs-variable">-#2</span> * <span class="hljs-built_in">COS</span>[<span class="hljs-variable">#2</span>]]
Expand All @@ -25,7 +25,7 @@

<span class="hljs-comment">; WITH SANE SPACING</span>

<span class="hljs-operator">N1</span> <span class="hljs-symbol">O107</span> <span class="hljs-keywords">IF</span> [<span class="hljs-variable">#10</span> <span class="hljs-keywords">GT</span> <span class="hljs-number">5</span>]
<span class="hljs-operator">N1</span> <span class="hljs-symbol">O107</span> <span class="hljs-keyword">IF</span> [<span class="hljs-variable">#10</span> <span class="hljs-keyword">GT</span> <span class="hljs-number">5</span>]
<span class="hljs-operator">N2</span> <span class="hljs-title function_">G0</span> <span class="hljs-property">A0</span> <span class="hljs-property">B0</span> <span class="hljs-property">C0</span> <span class="hljs-property">U0</span> <span class="hljs-property">V0</span> <span class="hljs-property">W0</span> <span class="hljs-property">X0</span> <span class="hljs-property">Y0</span> <span class="hljs-property">Z0</span> <span class="hljs-symbol">*71</span>
<span class="hljs-operator">N3</span> <span class="hljs-title function_">G100</span> <span class="hljs-property">A100</span> <span class="hljs-property">B0</span>
<span class="hljs-operator">N4</span> <span class="hljs-title function_">G5.2</span> <span class="hljs-property">A10.2</span> <span class="hljs-property">B0</span>
Expand All @@ -35,12 +35,12 @@
<span class="hljs-operator">N8</span> <span class="hljs-title function_">T0</span>
<span class="hljs-operator">N9</span> <span class="hljs-title function_">M100</span> <span class="hljs-params">F1</span> <span class="hljs-params">H2</span> <span class="hljs-params">I3</span> <span class="hljs-params">J4</span> <span class="hljs-params">K5</span> <span class="hljs-params">P6</span> <span class="hljs-params">Q7</span> <span class="hljs-params">R8</span> <span class="hljs-params">S9</span>
<span class="hljs-operator">N10</span> <span class="hljs-title function_">M587</span> S<span class="hljs-string">&quot;MYROUTER&quot;</span> P<span class="hljs-string">&quot;ABCxyz;&quot;</span> <span class="hljs-string">&quot;123&quot;</span>
<span class="hljs-operator">N10</span> <span class="hljs-symbol">O107</span> <span class="hljs-keywords">ENDIF</span>
<span class="hljs-operator">N10</span> <span class="hljs-symbol">O107</span> <span class="hljs-keyword">ENDIF</span>


<span class="hljs-comment">; WITH EXCESSIVE SPACING</span>

<span class="hljs-symbol">O 108</span> <span class="hljs-keywords">IF</span> [<span class="hljs-variable">#10</span> <span class="hljs-keywords">GT</span> <span class="hljs-number">5</span>]
<span class="hljs-symbol">O 108</span> <span class="hljs-keyword">IF</span> [<span class="hljs-variable">#10</span> <span class="hljs-keyword">GT</span> <span class="hljs-number">5</span>]
<span class="hljs-operator">N 102</span> <span class="hljs-title function_">G 0</span> <span class="hljs-property">A 0</span> <span class="hljs-property">B 0</span> <span class="hljs-property">C 0</span> <span class="hljs-property">U 0</span> <span class="hljs-property">V 0</span> <span class="hljs-property">W 0</span> <span class="hljs-property">X 0</span> <span class="hljs-property">Y 0</span> <span class="hljs-property">Z 0</span> <span class="hljs-symbol">*71</span>
<span class="hljs-operator">N 103</span> <span class="hljs-title function_">G 100</span> <span class="hljs-property">A 100</span> <span class="hljs-property">B 0</span>
<span class="hljs-operator">N 104</span> <span class="hljs-title function_">G 5.2</span> <span class="hljs-property">A 10.2</span> <span class="hljs-property">B 0</span>
Expand All @@ -50,12 +50,12 @@
<span class="hljs-operator">N 108</span> <span class="hljs-title function_">T 0</span>
<span class="hljs-operator">N 109</span> <span class="hljs-title function_">M 100</span> <span class="hljs-params">F 1</span> <span class="hljs-params">H 2</span> <span class="hljs-params">I 3</span> <span class="hljs-params">J 4</span> <span class="hljs-params">K 5</span> <span class="hljs-params">P 6</span> <span class="hljs-params">Q 7</span> <span class="hljs-params">R 8</span> <span class="hljs-params">S 9</span>
<span class="hljs-operator">N 110</span> <span class="hljs-title function_">M 587</span> S <span class="hljs-string">&quot;MYROUTER&quot;</span> P <span class="hljs-string">&quot;ABCxyz;&quot;</span> <span class="hljs-string">&quot;123&quot;</span>
<span class="hljs-symbol">O 108</span> <span class="hljs-keywords">ENDIF</span>
<span class="hljs-symbol">O 108</span> <span class="hljs-keyword">ENDIF</span>


<span class="hljs-comment">; WITHOUT SPACING</span>

<span class="hljs-symbol">O109</span><span class="hljs-keywords">IF</span>[<span class="hljs-variable">#10</span><span class="hljs-keywords">GT</span>5]
<span class="hljs-symbol">O109</span><span class="hljs-keyword">IF</span>[<span class="hljs-variable">#10</span><span class="hljs-keyword">GT</span>5]
<span class="hljs-operator">N202</span><span class="hljs-title function_">G0</span><span class="hljs-property">A0</span><span class="hljs-property">B0</span><span class="hljs-property">C0</span><span class="hljs-property">U0</span><span class="hljs-property">V0</span><span class="hljs-property">W0</span><span class="hljs-property">X0</span><span class="hljs-property">Y0</span><span class="hljs-property">Z0</span><span class="hljs-symbol">*71</span>
<span class="hljs-operator">N203</span><span class="hljs-title function_">G100</span><span class="hljs-property">A100</span><span class="hljs-property">B0</span>
<span class="hljs-operator">N204</span><span class="hljs-title function_">G5.2</span><span class="hljs-property">A10.2</span><span class="hljs-property">B0</span>
Expand All @@ -65,11 +65,11 @@
<span class="hljs-operator">N208</span><span class="hljs-title function_">T0</span>
<span class="hljs-operator">N209</span><span class="hljs-title function_">M100</span><span class="hljs-params">F1</span><span class="hljs-params">H2</span><span class="hljs-params">I3</span><span class="hljs-params">J4</span><span class="hljs-params">K5</span><span class="hljs-params">P6</span><span class="hljs-params">Q7</span><span class="hljs-params">R8</span><span class="hljs-params">S9</span>
<span class="hljs-operator">N210</span><span class="hljs-title function_">M587</span>S<span class="hljs-string">&quot;MYROUTER&quot;</span>P<span class="hljs-string">&quot;ABCxyz;&quot;</span><span class="hljs-string">&quot;123&quot;</span>
<span class="hljs-number">0</span><span class="hljs-symbol">O109</span><span class="hljs-keywords">ENDIF</span>
<span class="hljs-symbol">O109</span><span class="hljs-keyword">ENDIF</span>

<span class="hljs-symbol">O&lt;boring&gt;</span> <span class="hljs-keywords">ENDSUB</span>
<span class="hljs-symbol">O&lt;boring&gt;</span> <span class="hljs-keyword">ENDSUB</span>

<span class="hljs-symbol">O&lt;boring&gt;</span> <span class="hljs-keywords">CALL</span> [<span class="hljs-number">1</span>] [<span class="hljs-number">2</span>] [<span class="hljs-number">3</span>] [<span class="hljs-number">4</span>] [<span class="hljs-number">5</span>]
<span class="hljs-symbol">O&lt;boring&gt;</span> <span class="hljs-keyword">CALL</span> [<span class="hljs-number">1</span>] [<span class="hljs-number">2</span>] [<span class="hljs-number">3</span>] [<span class="hljs-number">4</span>] [<span class="hljs-number">5</span>]

<span class="hljs-comment">; Words as seen in Klipper Firmware GCode</span>

Expand All @@ -81,4 +81,4 @@ SET_SKEW XY=<span class="hljs-number">100.0000</span>,<span class="hljs-number">
SKEW_PROFILE SAVE=<span class="hljs-string">&quot;this name has spaces&quot;</span>
SAVE_CONFIG

<span class="hljs-keywords">%</span>
<span class="hljs-keyword">%</span>
2 changes: 1 addition & 1 deletion test/markup/gcode/extended.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ N207G100A+.2B0
N208T0
N209M100F1H2I3J4K5P6Q7R8S9
N210M587S"MYROUTER"P"ABCxyz;""123"
0O109ENDIF
O109ENDIF

O<boring> ENDSUB

Expand Down

0 comments on commit 1daed93

Please sign in to comment.