]> luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/gcode.js
Initial commit.
[flow-web.git] / static / highlight / languages / gcode.js
1 /*! `gcode` grammar compiled for Highlight.js 11.11.1 */
2 (function(){
3 var hljsGrammar = (function () {
4 'use strict';
5
6 /*
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/
11 Category: hardware
12 */
13
14 function gcode(hljs) {
15 const regex = hljs.regex;
16 const GCODE_KEYWORDS = {
17 $pattern: /[A-Z]+|%/,
18 keyword: [
19 // conditions
20 'THEN',
21 'ELSE',
22 'ENDIF',
23 'IF',
24
25 // controls
26 'GOTO',
27 'DO',
28 'WHILE',
29 'WH',
30 'END',
31 'CALL',
32
33 // scoping
34 'SUB',
35 'ENDSUB',
36
37 // comparisons
38 'EQ',
39 'NE',
40 'LT',
41 'GT',
42 'LE',
43 'GE',
44 'AND',
45 'OR',
46 'XOR',
47
48 // start/end of program
49 '%'
50 ],
51 built_in: [
52 'ATAN',
53 'ABS',
54 'ACOS',
55 'ASIN',
56 'COS',
57 'EXP',
58 'FIX',
59 'FUP',
60 'ROUND',
61 'LN',
62 'SIN',
63 'SQRT',
64 'TAN',
65 'EXISTS'
66 ]
67 };
68
69
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/;
73
74 function LETTER_BOUNDARY_CALLBACK(matchdata, response) {
75 if (matchdata.index === 0) {
76 return;
77 }
78
79 const charBeforeMatch = matchdata.input[matchdata.index - 1];
80 if (charBeforeMatch >= '0' && charBeforeMatch <= '9') {
81 return;
82 }
83
84 if (charBeforeMatch === '_') {
85 return;
86 }
87
88 response.ignoreMatch();
89 }
90
91 const NUMBER_RE = /[+-]?((\.\d+)|(\d+)(\.\d*)?)/;
92
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*/;
99
100 const GCODE_CODE = [
101 // comments
102 hljs.COMMENT(/\(/, /\)/),
103 hljs.COMMENT(/;/, /$/),
104 hljs.APOS_STRING_MODE,
105 hljs.QUOTE_STRING_MODE,
106 hljs.C_NUMBER_MODE,
107
108 // gcodes
109 {
110 scope: 'title.function',
111 variants: [
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) },
115 {
116 begin: GENERAL_MISC_FUNCTION_RE,
117 'on:begin': LETTER_BOUNDARY_CALLBACK
118 },
119 // T Tools
120 { match: regex.concat(LETTER_BOUNDARY_RE, TOOLS_RE), },
121 {
122 begin: TOOLS_RE,
123 'on:begin': LETTER_BOUNDARY_CALLBACK
124 }
125 ]
126 },
127
128 {
129 scope: 'symbol',
130 variants: [
131 // O Subroutine ID: O100, O110, …
132 { match: regex.concat(LETTER_BOUNDARY_RE, SUBROUTINE_RE) },
133 {
134 begin: SUBROUTINE_RE,
135 'on:begin': LETTER_BOUNDARY_CALLBACK
136 },
137 // O Subroutine name: O<some>, …
138 { match: regex.concat(LETTER_BOUNDARY_RE, SUBROUTINE_NAMED_RE) },
139 {
140 begin: SUBROUTINE_NAMED_RE,
141 'on:begin': LETTER_BOUNDARY_CALLBACK
142 },
143 // Checksum at end of line: *71, *199, …
144 { match: /\*\s*\d+\s*$/ }
145 ]
146 },
147
148 {
149 scope: 'operator', // N Line number: N1, N2, N1020, …
150 match: /^N\s*\d+/
151 },
152
153 {
154 scope: 'variable',
155 match: /-?#\s*\d+/
156 },
157
158 {
159 scope: 'property', // Physical axes,
160 variants: [
161 { match: regex.concat(LETTER_BOUNDARY_RE, AXES_RE, NUMBER_RE) },
162 {
163 begin: regex.concat(AXES_RE, NUMBER_RE),
164 'on:begin': LETTER_BOUNDARY_CALLBACK
165 },
166 ]
167 },
168
169 {
170 scope: 'params', // Different types of parameters
171 variants: [
172 { match: regex.concat(LETTER_BOUNDARY_RE, PARAMETERS_RE, NUMBER_RE) },
173 {
174 begin: regex.concat(PARAMETERS_RE, NUMBER_RE),
175 'on:begin': LETTER_BOUNDARY_CALLBACK
176 },
177 ]
178 },
179 ];
180
181 return {
182 name: 'G-code (ISO 6983)',
183 aliases: [ 'nc' ],
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,
190 contains: GCODE_CODE
191 };
192 }
193
194 return gcode;
195
196 })();
197
198 hljs.registerLanguage('gcode', hljsGrammar);
199 })();