UNPKG

2.13 kBJavaScriptView Raw
1/*
2 Language: G-code (ISO 6983)
3 Contributors: Adam Joseph Cook <adam.joseph.cook@gmail.com>
4 Description: G-code syntax highlighter for Fanuc and other common CNC machine tool controls.
5 Website: https://www.sis.se/api/document/preview/911952/
6 */
7
8function gcode(hljs) {
9 const GCODE_IDENT_RE = '[A-Z_][A-Z0-9_.]*';
10 const GCODE_CLOSE_RE = '%';
11 const GCODE_KEYWORDS = {
12 $pattern: GCODE_IDENT_RE,
13 keyword: 'IF DO WHILE ENDWHILE CALL ENDIF SUB ENDSUB GOTO REPEAT ENDREPEAT ' +
14 'EQ LT GT NE GE LE OR XOR'
15 };
16 const GCODE_START = {
17 className: 'meta',
18 begin: '([O])([0-9]+)'
19 };
20 const NUMBER = hljs.inherit(hljs.C_NUMBER_MODE, {
21 begin: '([-+]?((\\.\\d+)|(\\d+)(\\.\\d*)?))|' + hljs.C_NUMBER_RE
22 });
23 const GCODE_CODE = [
24 hljs.C_LINE_COMMENT_MODE,
25 hljs.C_BLOCK_COMMENT_MODE,
26 hljs.COMMENT(/\(/, /\)/),
27 NUMBER,
28 hljs.inherit(hljs.APOS_STRING_MODE, {
29 illegal: null
30 }),
31 hljs.inherit(hljs.QUOTE_STRING_MODE, {
32 illegal: null
33 }),
34 {
35 className: 'name',
36 begin: '([G])([0-9]+\\.?[0-9]?)'
37 },
38 {
39 className: 'name',
40 begin: '([M])([0-9]+\\.?[0-9]?)'
41 },
42 {
43 className: 'attr',
44 begin: '(VC|VS|#)',
45 end: '(\\d+)'
46 },
47 {
48 className: 'attr',
49 begin: '(VZOFX|VZOFY|VZOFZ)'
50 },
51 {
52 className: 'built_in',
53 begin: '(ATAN|ABS|ACOS|ASIN|SIN|COS|EXP|FIX|FUP|ROUND|LN|TAN)(\\[)',
54 contains: [
55 NUMBER
56 ],
57 end: '\\]'
58 },
59 {
60 className: 'symbol',
61 variants: [
62 {
63 begin: 'N',
64 end: '\\d+',
65 illegal: '\\W'
66 }
67 ]
68 }
69 ];
70
71 return {
72 name: 'G-code (ISO 6983)',
73 aliases: ['nc'],
74 // Some implementations (CNC controls) of G-code are interoperable with uppercase and lowercase letters seamlessly.
75 // However, most prefer all uppercase and uppercase is customary.
76 case_insensitive: true,
77 keywords: GCODE_KEYWORDS,
78 contains: [
79 {
80 className: 'meta',
81 begin: GCODE_CLOSE_RE
82 },
83 GCODE_START
84 ].concat(GCODE_CODE)
85 };
86}
87
88export { gcode as default };