]>
luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/gcode.js
1 /*! `gcode` grammar compiled for Highlight.js 11.11.1 */
3 var hljsGrammar
= (function () {
7 Language: G-code (ISO 6983)
8 Contributors: Adam Joseph Cook <adam.joseph.cook@gmail.com>
9 Description: G-code syntax highlighter for Fanuc and other common CNC machine tool controls.
10 Website: https://www.sis.se/api/document/preview/911952/
14 function gcode(hljs
) {
15 const regex
= hljs
.regex
;
16 const GCODE_KEYWORDS
= {
48 // start/end of program
70 // TODO: post v12 lets use look-behind, until then \b and a callback filter will be used
71 // const LETTER_BOUNDARY_RE = /(?<![A-Z])/;
72 const LETTER_BOUNDARY_RE
= /\b/;
74 function LETTER_BOUNDARY_CALLBACK(matchdata
, response
) {
75 if (matchdata
.index
=== 0) {
79 const charBeforeMatch
= matchdata
.input
[matchdata
.index
- 1];
80 if (charBeforeMatch
>= '0' && charBeforeMatch
<= '9') {
84 if (charBeforeMatch
=== '_') {
88 response
.ignoreMatch();
91 const NUMBER_RE
= /[+-]?((\.\d+)|(\d+)(\.\d*)?)/;
93 const GENERAL_MISC_FUNCTION_RE
= /[GM]\s*\d+(\.\d+)?/;
94 const TOOLS_RE
= /T\s*\d+/;
95 const SUBROUTINE_RE
= /O\s*\d+/;
96 const SUBROUTINE_NAMED_RE
= /O<.+>/;
97 const AXES_RE
= /[ABCUVWXYZ]\s*/;
98 const PARAMETERS_RE
= /[FHIJKPQRS]\s*/;
102 hljs
.COMMENT(/\(/, /\)/),
103 hljs
.COMMENT(/;/, /$/),
104 hljs
.APOS_STRING_MODE
,
105 hljs
.QUOTE_STRING_MODE
,
110 scope: 'title.function',
112 // G General functions: G0, G5.1, G5.2, …
113 // M Misc functions: M0, M55.6, M199, …
114 { match: regex
.concat(LETTER_BOUNDARY_RE
, GENERAL_MISC_FUNCTION_RE
) },
116 begin: GENERAL_MISC_FUNCTION_RE
,
117 'on:begin': LETTER_BOUNDARY_CALLBACK
120 { match: regex
.concat(LETTER_BOUNDARY_RE
, TOOLS_RE
), },
123 'on:begin': LETTER_BOUNDARY_CALLBACK
131 // O Subroutine ID: O100, O110, …
132 { match: regex
.concat(LETTER_BOUNDARY_RE
, SUBROUTINE_RE
) },
134 begin: SUBROUTINE_RE
,
135 'on:begin': LETTER_BOUNDARY_CALLBACK
137 // O Subroutine name: O<some>, …
138 { match: regex
.concat(LETTER_BOUNDARY_RE
, SUBROUTINE_NAMED_RE
) },
140 begin: SUBROUTINE_NAMED_RE
,
141 'on:begin': LETTER_BOUNDARY_CALLBACK
143 // Checksum at end of line: *71, *199, …
144 { match: /\*\s*\d+\s*$/ }
149 scope: 'operator', // N Line number: N1, N2, N1020, …
159 scope: 'property', // Physical axes,
161 { match: regex
.concat(LETTER_BOUNDARY_RE
, AXES_RE
, NUMBER_RE
) },
163 begin: regex
.concat(AXES_RE
, NUMBER_RE
),
164 'on:begin': LETTER_BOUNDARY_CALLBACK
170 scope: 'params', // Different types of parameters
172 { match: regex
.concat(LETTER_BOUNDARY_RE
, PARAMETERS_RE
, NUMBER_RE
) },
174 begin: regex
.concat(PARAMETERS_RE
, NUMBER_RE
),
175 'on:begin': LETTER_BOUNDARY_CALLBACK
182 name: 'G-code (ISO 6983)',
184 // Some implementations (CNC controls) of G-code are interoperable with uppercase and lowercase letters seamlessly.
185 // However, most prefer all uppercase and uppercase is customary.
186 case_insensitive: true,
187 // TODO: post v12 with the use of look-behind this can be enabled
188 disableAutodetect: true,
189 keywords: GCODE_KEYWORDS
,
198 hljs
.registerLanguage('gcode', hljsGrammar
);