Skip to content

Commit

Permalink
Add G-code language (#1572)
Browse files Browse the repository at this point in the history
This PR adds the [G-code language](https://reprap.org/wiki/G-code) to Prism
and resolves #1571.

### Features

- All fields from A-Z ([case sensitive](https://reprap.org/wiki/G-code#Case_sensitivity)) and [checksums](https://reprap.org/wiki/G-code#.2A:_Checksum) (`*`).
- [Comments](https://reprap.org/wiki/G-code#Comments) of both the semicolon and parenthesis style.
- [Quoted strings](https://reprap.org/wiki/G-code#Quoted_strings).
- The colon punctuation of [lists (see the example)](https://reprap.org/wiki/G-code#M584:_Set_drive_mapping).
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Dec 1, 2018
1 parent 225dd3f commit 2288c25
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@
"title": "Fortran",
"owner": "Golmote"
},
"gcode": {
"title": "G-code",
"owner": "RunDevelopment"
},
"gedcom": {
"title": "GEDCOM",
"owner": "Golmote"
Expand Down
15 changes: 15 additions & 0 deletions components/prism-gcode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Prism.languages.gcode = {
'comment': /;.*|\B\(.*?\)\B/,
'string': {
pattern: /"(?:""|[^"])*"/,
greedy: true
},
'keyword': /\b[GM]\d+(?:\.\d+)?\b/,
'property': /\b[A-Z]/,
'checksum': {
pattern: /\*\d+/,
alias: 'punctuation'
},
// T0:0:0
'punctuation': /:/
};
1 change: 1 addition & 0 deletions components/prism-gcode.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions examples/prism-gcode.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<h2>Comments</h2>
<pre><code>; comment
(some more comments)
G28 (even in here) X0
</code></pre>

<h2>Quoted strings</h2>
<pre><code>"foo""bar"</code></pre>

<h2>Full example</h2>
<pre><code>M190 S60 ; Heat bed to 60°C
G21 ; Set units to millimeters
G28 ; Move to Origin (Homing)
G29 ; Auto Bed Leveling
G28 X0 Y0 ; Home X and Y to min endstops
M107 ; Fan off
M109 S200 ; Heat hotend to 200°C
G92 E0 ; Set current extruder position as zero
G1 F200 E15 ; Extrude 15mm filament with 200mm/min
G92 E0 ; Set current extruder position as zero
G1 F500
</code></pre>
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (!Prism.plugins.toolbar) {
}

// The languages map is built automatically with gulp
var Languages = /*languages_placeholder[*/{"html":"HTML","xml":"XML","svg":"SVG","mathml":"MathML","css":"CSS","clike":"C-like","javascript":"JavaScript","abap":"ABAP","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","arff":"ARFF","asciidoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","shell":"Shell","basic":"BASIC","csharp":"C#","cpp":"C++","cil":"CIL","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","erb":"ERB","fsharp":"F#","gedcom":"GEDCOM","glsl":"GLSL","gml":"GameMaker Language","graphql":"GraphQL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","json":"JSON","jsonp":"JSONP","latex":"LaTeX","livescript":"LiveScript","lolcode":"LOLCODE","markup-templating":"Markup templating","matlab":"MATLAB","mel":"MEL","n4js":"N4JS","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","objectpascal":"Object Pascal","php":"PHP","php-extras":"PHP Extras","plsql":"PL/SQL","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","q":"Q (kdb+ database)","jsx":"React JSX","tsx":"React TSX","renpy":"Ren'py","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","soy":"Soy (Closure Template)","tap":"TAP","tt2":"Template Toolkit 2","typescript":"TypeScript","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","visual-basic":"Visual Basic","wasm":"WebAssembly","wiki":"Wiki markup","xeoracube":"XeoraCube","xojo":"Xojo (REALbasic)","xquery":"XQuery","yaml":"YAML"}/*]*/;
var Languages = /*languages_placeholder[*/{"html":"HTML","xml":"XML","svg":"SVG","mathml":"MathML","css":"CSS","clike":"C-like","javascript":"JavaScript","abap":"ABAP","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","arff":"ARFF","asciidoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","shell":"Shell","basic":"BASIC","csharp":"C#","cpp":"C++","cil":"CIL","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","erb":"ERB","fsharp":"F#","gcode":"G-code","gedcom":"GEDCOM","glsl":"GLSL","gml":"GameMaker Language","graphql":"GraphQL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","json":"JSON","jsonp":"JSONP","latex":"LaTeX","livescript":"LiveScript","lolcode":"LOLCODE","markup-templating":"Markup templating","matlab":"MATLAB","mel":"MEL","n4js":"N4JS","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","objectpascal":"Object Pascal","php":"PHP","php-extras":"PHP Extras","plsql":"PL/SQL","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","q":"Q (kdb+ database)","jsx":"React JSX","tsx":"React TSX","renpy":"Ren'py","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","soy":"Soy (Closure Template)","tap":"TAP","tt2":"Template Toolkit 2","typescript":"TypeScript","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","visual-basic":"Visual Basic","wasm":"WebAssembly","wiki":"Wiki markup","xeoracube":"XeoraCube","xojo":"Xojo (REALbasic)","xquery":"XQuery","yaml":"YAML"}/*]*/;
Prism.plugins.toolbar.registerButton('show-language', function(env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName)) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/languages/gcode/checksum_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
G28*22

----------------------------------------------------

[
["keyword", "G28"],
["checksum", "*22"]
]

----------------------------------------------------

Checks for checksums.
20 changes: 20 additions & 0 deletions tests/languages/gcode/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; foo
(Home some axes)
G28 (here come the axes to be homed) X

----------------------------------------------------

[
["comment", "; foo"],

["comment", "(Home some axes)"],

["keyword", "G28"],
["comment", "(here come the axes to be homed)"],
["property", "X"]

]

----------------------------------------------------

Checks for comments.
23 changes: 23 additions & 0 deletions tests/languages/gcode/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
G00
G200
G84.1

M00
M123
M52.4

----------------------------------------------------

[
["keyword", "G00"],
["keyword", "G200"],
["keyword", "G84.1"],

["keyword", "M00"],
["keyword", "M123"],
["keyword", "M52.4"]
]

----------------------------------------------------

Checks for G- and M-codes.
17 changes: 17 additions & 0 deletions tests/languages/gcode/property_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
X123
Y0.2
Z-3.1415
E420:420

----------------------------------------------------

[
["property", "X"], "123\n",
["property", "Y"], "0.2\n",
["property", "Z"], "-3.1415\n",
["property", "E"], "420", ["punctuation", ":"], "420"
]

----------------------------------------------------

Checks for all other codes except G- and M-codes.
17 changes: 17 additions & 0 deletions tests/languages/gcode/string_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
M587 S"MYROUTER" P"ABCxyz;"" 123"

----------------------------------------------------

[
["keyword", "M587"],

["property", "S"],
["string", "\"MYROUTER\""],

["property", "P"],
["string", "\"ABCxyz;\"\" 123\""]
]

----------------------------------------------------

Checks for strings.

0 comments on commit 2288c25

Please sign in to comment.