]> luflow.net public git repositories - flow-web.git/blob - static/highlight/es/languages/gcode.js
Initial commit.
[flow-web.git] / static / highlight / es / languages / gcode.js
1 /*! `gcode` grammar compiled for Highlight.js 11.11.1 */
2 var hljsGrammar = (function () {
3 'use strict';
4
5 /*
6 Language: G-code (ISO 6983)
7 Contributors: Adam Joseph Cook <adam.joseph.cook@gmail.com>
8 Description: G-code syntax highlighter for Fanuc and other common CNC machine tool controls.
9 Website: https://www.sis.se/api/document/preview/911952/
10 Category: hardware
11 */
12
13 function gcode(hljs) {
14 const regex = hljs.regex;
15 const GCODE_KEYWORDS = {
16 $pattern: /[A-Z]+|%/,
17 keyword: [
18 // conditions
19 'THEN',
20 'ELSE',
21 'ENDIF',
22 'IF',
23
24 // controls
25 'GOTO',
26 'DO',
27 'WHILE',
28 'WH',
29 'END',
30 'CALL',
31
32 // scoping
33 'SUB',
34 'ENDSUB',
35
36 // comparisons
37 'EQ',
38 'NE',
39 'LT',
40 'GT',
41 'LE',
42 'GE',
43 'AND',
44 'OR',
45 'XOR',
46
47 // start/end of program
48 '%'
49 ],
50 built_in: [
51 'ATAN',
52 'ABS',
53 'ACOS',
54 'ASIN',
55 'COS',
56 'EXP',
57 'FIX',
58 'FUP',
59 'ROUND',
60 'LN',
61 'SIN',
62 'SQRT',
63 'TAN',
64 'EXISTS'
65 ]
66 };
67
68
69 // TODO: post v12 lets use look-behind, until then \b and a callback filter will be used
70 // const LETTER_BOUNDARY_RE = /(?<![A-Z])/;
71 const LETTER_BOUNDARY_RE = /\b/;
72
73 function LETTER_BOUNDARY_CALLBACK(matchdata, response) {
74 if (matchdata.index === 0) {
75 return;
76 }
77
78 const charBeforeMatch = matchdata.input[matchdata.index - 1];
79 if (charBeforeMatch >= '0' && charBeforeMatch <= '9') {
80 return;
81 }
82
83 if (charBeforeMatch === '_') {
84 return;
85 }
86
87 response.ignoreMatch();
88 }
89
90 const NUMBER_RE = /[+-]?((\.\d+)|(\d+)(\.\d*)?)/;
91
92 const GENERAL_MISC_FUNCTION_RE = /[GM]\s*\d+(\.\d+)?/;
93 const TOOLS_RE = /T\s*\d+/;
94 const SUBROUTINE_RE = /O\s*\d+/;
95 const SUBROUTINE_NAMED_RE = /O<.+>/;
96 const AXES_RE = /[ABCUVWXYZ]\s*/;
97 const PARAMETERS_RE = /[FHIJKPQRS]\s*/;
98
99 const GCODE_CODE = [
100 // comments
101 hljs.COMMENT(/\(/, /\)/),
102 hljs.COMMENT(/;/, /$/),
103 hljs.APOS_STRING_MODE,
104 hljs.QUOTE_STRING_MODE,
105 hljs.C_NUMBER_MODE,
106
107 // gcodes
108 {
109 scope: 'title.function',
110 variants: [
111 // G General functions: G0, G5.1, G5.2, …
112 // M Misc functions: M0, M55.6, M199, …
113 { match: regex.concat(LETTER_BOUNDARY_RE, GENERAL_MISC_FUNCTION_RE) },
114 {
115 begin: GENERAL_MISC_FUNCTION_RE,
116 'on:begin': LETTER_BOUNDARY_CALLBACK
117 },
118 // T Tools
119 { match: regex.concat(LETTER_BOUNDARY_RE, TOOLS_RE), },
120 {
121 begin: TOOLS_RE,
122 'on:begin': LETTER_BOUNDARY_CALLBACK
123 }
124 ]
125 },
126
127 {
128 scope: 'symbol',
129 variants: [
130 // O Subroutine ID: O100, O110, …
131 { match: regex.concat(LETTER_BOUNDARY_RE, SUBROUTINE_RE) },
132 {
133 begin: SUBROUTINE_RE,
134 'on:begin': LETTER_BOUNDARY_CALLBACK
135 },
136 // O Subroutine name: O<some>, …
137 { match: regex.concat(LETTER_BOUNDARY_RE, SUBROUTINE_NAMED_RE) },
138 {
139 begin: SUBROUTINE_NAMED_RE,
140 'on:begin': LETTER_BOUNDARY_CALLBACK
141 },
142 // Checksum at end of line: *71, *199, …
143 { match: /\*\s*\d+\s*$/ }
144 ]
145 },
146
147 {
148 scope: 'operator', // N Line number: N1, N2, N1020, …
149 match: /^N\s*\d+/
150 },
151
152 {
153 scope: 'variable',
154 match: /-?#\s*\d+/
155 },
156
157 {
158 scope: 'property', // Physical axes,
159 variants: [
160 { match: regex.concat(LETTER_BOUNDARY_RE, AXES_RE, NUMBER_RE) },
161 {
162 begin: regex.concat(AXES_RE, NUMBER_RE),
163 'on:begin': LETTER_BOUNDARY_CALLBACK
164 },
165 ]
166 },
167
168 {
169 scope: 'params', // Different types of parameters
170 variants: [
171 { match: regex.concat(LETTER_BOUNDARY_RE, PARAMETERS_RE, NUMBER_RE) },
172 {
173 begin: regex.concat(PARAMETERS_RE, NUMBER_RE),
174 'on:begin': LETTER_BOUNDARY_CALLBACK
175 },
176 ]
177 },
178 ];
179
180 return {
181 name: 'G-code (ISO 6983)',
182 aliases: [ 'nc' ],
183 // Some implementations (CNC controls) of G-code are interoperable with uppercase and lowercase letters seamlessly.
184 // However, most prefer all uppercase and uppercase is customary.
185 case_insensitive: true,
186 // TODO: post v12 with the use of look-behind this can be enabled
187 disableAutodetect: true,
188 keywords: GCODE_KEYWORDS,
189 contains: GCODE_CODE
190 };
191 }
192
193 return gcode;
194
195 })();
196 ;
197 export default hljsGrammar;