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