-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcode_mode.js
More file actions
87 lines (72 loc) · 2.58 KB
/
gcode_mode.js
File metadata and controls
87 lines (72 loc) · 2.58 KB
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
// CodeMirror.defineMode("gcode", function() {
// var TOKEN_NAMES = {
// 'G': 'tag',
// 'M': 'string',
// ';': 'meta'
// };
// return {
// token: function(stream) {
// var tw_pos = stream.string.search(/[\t ]+?$/);
// if (!stream.sol() || tw_pos === 0) {
// stream.skipToEnd();
// return ("error " + (
// TOKEN_NAMES[stream.string.charAt(0)] || '')).replace(/ $/, '');
// }
// var token_name = TOKEN_NAMES[stream.peek()] || stream.skipToEnd();
// if (tw_pos === -1) {
// stream.skipToEnd();
// } else {
// stream.pos = tw_pos;
// }
// return token_name;
// }
// };
// });
// CodeMirror.defineMIME("text/x-gcode", "gcode");
CodeMirror.defineMode("gcode", function() {
var TOKEN_NAMES = {
'G': 'tag',
'M': 'string',
'X': 'coordinate-name',
'Y': 'coordinate-name',
'Z': 'coordinate-name',
'A': 'coordinate-name',
'B': 'coordinate-name',
'C': 'coordinate-name',
'F': 'coordinate-name',
'E': 'coordinate-name',
';': 'meta'
};
return {
token: function(stream) {
// 寻找下一个空格或制表符的位置
var tw_pos = stream.string.search(/[\t ]/);
if (!stream.sol() || tw_pos === 0) {
stream.skipToEnd();
return ("error " + (
TOKEN_NAMES[stream.string.charAt(0)] || '')).replace(/ $/, '');
}
var token_name = TOKEN_NAMES[stream.peek()] || stream.skipToEnd();
if (tw_pos === -1) {
stream.skipToEnd(); // 没有找到空格,标记到行末尾
} else {
stream.pos = stream.start + tw_pos; // 标记到空格之前的位置
}
if (token_name === 'meta') {
return token_name;
} else if (token_name === 'tag' || token_name === 'string') {
return token_name + ' ' + stream.current().trim();
} else if (token_name === 'error') {
return token_name;
} else if (token_name === 'comment') {
stream.skipToEnd();
return token_name;
} else if (token_name === 'coordinate-name') {
stream.eatWhile(/[^ \t]/);
return token_name;
}
return token_name;
}
};
});
CodeMirror.defineMIME("text/x-gcode", "gcode");