Skip to content

Commit

Permalink
feat: gcode improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
barthy-koeln committed Apr 14, 2024
1 parent 90425a3 commit e416841
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 115 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Core Grammars:
- enh(markdown) add entity support [David Schach][] [TaraLei][]
- enh(css) add `justify-items` and `justify-self` attributes [Vasily Polovnyov][]
- enh(css) add `accent-color`, `appearance`, `color-scheme`, `rotate`, `scale` and `translate` attributes [Carl Räfting][]
- enh(gcode) rewrote language for modern gcode support [Barthélémy Bonhomme][]

New Grammars:

Expand Down Expand Up @@ -88,6 +89,7 @@ Themes:
[Rúnar Bjarnason]: https://github.com/runarorama
[Carl Räfting]: https://github.com/carlrafting
[BackupMiles]: https://github.com/BackupMiles
[Barthélémy Bonhomme]: https://github.com/barthy-koeln



Expand Down
34 changes: 14 additions & 20 deletions src/languages/gcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,57 +71,51 @@ export default function(hljs) {
hljs.QUOTE_STRING_MODE,
hljs.C_NUMBER_MODE,

// {
// scope: 'number',
// match: NUMBER
// },

// gcodes
{
scope: 'title.function',
relevance: 10,
variants: [
// G General functions: G0, G5.1, G5.2, …
// M Misc functions: M0, M55.6, M199, …
{ match: /\b[GM]\s*\d+(\.\d+)?/ },
{ match: /(?<![A-Z])[GM]\s*\d+(\.\d+)?/ },
// T Tools
{ match: /\bT\s*\d+/ },
{ match: /(?<![A-Z])T\s*\d+/ },
]
},

{
scope: 'symbol',
relevance: 10,
variants: [
// F Line number: N1, N2, N1020, …
// O Subroutine ID: O100, O110, …
{ match: /^\s*[NO]\s*\d+/ },
{ match: /(?<![A-Z])O\s*\d+/ },
// O Subroutine name: O<some>, …
{ match: /(?<![A-Z])O<.+>/ },
// Checksum at end of line: *71, *199, …
{ match: /\*\s*\d+\s*$/ }
]
},

{
scope: 'name',
begin: '<',
end: '>'
scope: 'operator', // N Line number: N1, N2, N1020, …
match: /^N\s*\d+/
},


{
scope: 'variable',
// Physical axes
relevance: 0,
match: /-?#\s*\d+/,
},

{
scope: 'property',
// Physical axes
match: new RegExp(`[ABCUVWXYZ]\\s*${NUMBER.source}`),
scope: 'property', // Physical axes
match: new RegExp(`(?<![A-Z])[ABCUVWXYZ]\\s*${NUMBER.source}`),
},

{
scope: 'params',
// Different types of parameters
match: new RegExp(`[FHIJKPQRS]\\s*${NUMBER.source}`),
scope: 'params', // Different types of parameters
match: new RegExp(`(?<![A-Z])[FHIJKPQRS]\\s*${NUMBER.source}`),
},
];

Expand Down
57 changes: 28 additions & 29 deletions test/markup/gcode/default.expect.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
<span class="hljs-symbol">O003</span> <span class="hljs-comment">(DIAMOND SQUARE)</span>
<span class="hljs-symbol">N2</span> <span class="hljs-title function_">G54</span> <span class="hljs-title function_">G90</span> <span class="hljs-title function_">G49</span> <span class="hljs-title function_">G80</span>
<span class="hljs-symbol">N3</span> <span class="hljs-title function_">M6</span> <span class="hljs-title function_">T1</span> <span class="hljs-comment">(1.ENDMILL)</span>
<span class="hljs-symbol">N4</span> <span class="hljs-title function_">M3</span> <span class="hljs-variable">S1800</span>
<span class="hljs-symbol">N5</span> <span class="hljs-title function_">G0</span> <span class="hljs-variable language_">X-.6</span> <span class="hljs-variable language_">Y2.050</span>
<span class="hljs-symbol">N6</span> <span class="hljs-title function_">G43</span> <span class="hljs-variable">H1</span> <span class="hljs-variable language_">Z.1</span>
<span class="hljs-symbol">N7</span> <span class="hljs-title function_">G1</span> <span class="hljs-variable language_">Z-.3</span> <span class="hljs-variable">F50.</span>
<span class="hljs-symbol">N8</span> <span class="hljs-title function_">G41</span> D1 <span class="hljs-variable language_">Y1.45</span>
<span class="hljs-symbol">N9</span> <span class="hljs-title function_">G1</span> <span class="hljs-variable language_">X0</span> <span class="hljs-variable">F20.</span>
<span class="hljs-symbol">N10</span> <span class="hljs-title function_">G2</span> <span class="hljs-variable">J-1.45</span>
<span class="hljs-operator">N2</span> <span class="hljs-title function_">G54</span> <span class="hljs-title function_">G90</span> <span class="hljs-title function_">G49</span> <span class="hljs-title function_">G80</span>
<span class="hljs-operator">N3</span> <span class="hljs-title function_">M6</span> <span class="hljs-title function_">T1</span> <span class="hljs-comment">(1.ENDMILL)</span>
<span class="hljs-operator">N4</span> <span class="hljs-title function_">M3</span> <span class="hljs-params">S1800</span>
<span class="hljs-operator">N5</span> <span class="hljs-title function_">G0</span> <span class="hljs-property">X-.6</span> <span class="hljs-property">Y2.050</span>
<span class="hljs-operator">N6</span> <span class="hljs-title function_">G43</span> <span class="hljs-params">H1</span> <span class="hljs-property">Z.1</span>
<span class="hljs-operator">N7</span> <span class="hljs-title function_">G1</span> <span class="hljs-property">Z-.3</span> <span class="hljs-params">F50.</span>
<span class="hljs-operator">N8</span> <span class="hljs-title function_">G41</span> D1 <span class="hljs-property">Y1.45</span>
<span class="hljs-operator">N9</span> <span class="hljs-title function_">G1</span> <span class="hljs-property">X0</span> <span class="hljs-params">F20.</span>
<span class="hljs-operator">N10</span> <span class="hljs-title function_">G2</span> <span class="hljs-params">J-1.45</span>
<span class="hljs-comment">(CUTTER COMP CANCEL)</span>
<span class="hljs-symbol">N11</span> <span class="hljs-title function_">G1</span> <span class="hljs-variable language_">Z-.2</span> <span class="hljs-variable">F50.</span>
<span class="hljs-symbol">N12</span> <span class="hljs-variable language_">Y-.990</span>
<span class="hljs-symbol">N13</span> <span class="hljs-title function_">G40</span>
<span class="hljs-symbol">N14</span> <span class="hljs-title function_">G0</span> <span class="hljs-variable language_">X-.6</span> <span class="hljs-variable language_">Y1.590</span>
<span class="hljs-symbol">N15</span> <span class="hljs-title function_">G0</span> <span class="hljs-variable language_">Z.1</span>
<span class="hljs-symbol">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-variable language_">Z0</span>
<span class="hljs-symbol">N17</span> CALL O9456
<span class="hljs-symbol">N18</span> #500=0.004
<span class="hljs-symbol">N19</span> #503=[#500+#501]
<span class="hljs-symbol">N20</span> V<span class="hljs-variable language_">C45</span>=0.0006
V<span class="hljs-variable">S4</span>=0.0007
<span class="hljs-symbol">N21</span> <span class="hljs-title function_">G90</span> <span class="hljs-title function_">G10</span> L20 <span class="hljs-variable">P3</span> <span class="hljs-variable language_">X5.</span><span class="hljs-variable language_">Y4.</span> <span class="hljs-variable language_">Z6.567</span>
<span class="hljs-symbol">N22</span> <span class="hljs-title function_">G0</span> <span class="hljs-variable language_">X5000</span>
<span class="hljs-symbol">N23</span> <span class="hljs-meta">IF</span> [#1 <span class="hljs-meta">LT</span> 0.370] <span class="hljs-meta">GOTO</span> 49
<span class="hljs-symbol">N24</span> <span class="hljs-variable language_">X-0.678</span> <span class="hljs-variable language_">Y+.990</span>
<span class="hljs-symbol">N25</span> <span class="hljs-title function_">G84.3</span> <span class="hljs-variable language_">X-0.1</span>
<span class="hljs-symbol">N26</span> #4=#5*<span class="hljs-built_in">COS</span>[45]
<span class="hljs-symbol">N27</span> #4=#5*<span class="hljs-built_in">SIN</span>[45]
<span class="hljs-symbol">N28</span> VZOFZ=652.9658
<span class="hljs-symbol">N29</span> SAVE_CONFIG
<span class="hljs-operator">N11</span> <span class="hljs-title function_">G1</span> <span class="hljs-property">Z-.2</span> <span class="hljs-params">F50.</span>
<span class="hljs-operator">N12</span> <span class="hljs-property">Y-.990</span>
<span class="hljs-operator">N13</span> <span class="hljs-title function_">G40</span>
<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-meta">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-meta">IF</span> [<span class="hljs-variable">#1</span> <span class="hljs-meta">LT</span> <span class="hljs-number">0.370</span>] <span class="hljs-meta">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-meta">%</span>
Loading

0 comments on commit e416841

Please sign in to comment.