Skip to content

Commit 94b2660

Browse files
committed
Parser work for Tree
Signed-off-by: Mike Centola <[email protected]>
1 parent 5b703df commit 94b2660

File tree

11 files changed

+2320
-49
lines changed

11 files changed

+2320
-49
lines changed

package.json

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"bugs": {
3131
"url": "https://github.com/appliedengdesign/vscode-gcode-syntax/issues"
3232
},
33-
"main": "./out/extension",
33+
"main": "./out/extension.js",
3434
"activationEvents": [
3535
"onLanguage:gcode"
3636
],
@@ -125,6 +125,12 @@
125125
"Loads specific snippets for CNC Lathe G-Code"
126126
],
127127
"scope": "window"
128+
},
129+
"gcode.treeAutoRefresh": {
130+
"type": "boolean",
131+
"default": false,
132+
"description": "Enable G-Code Tree Auto Refresh",
133+
"scope": "window"
128134
}
129135
}
130136
},
@@ -145,8 +151,26 @@
145151
"when": "gcodeTreeEnabled"
146152
}
147153
]
148-
}
149-
154+
},
155+
"menus": {
156+
"view/title": [
157+
{
158+
"command": "gcodeTree.refreshEntry",
159+
"when": "view == gcodeTree",
160+
"group": "navigation"
161+
}
162+
]
163+
},
164+
"commands": [
165+
{
166+
"command": "gcodeTree.refreshEntry",
167+
"title": "Refresh",
168+
"icon": {
169+
"light": "resources/icons/light/refresh.svg",
170+
"dark": "resources/icons/dark/refresh.svg"
171+
}
172+
}
173+
]
150174
},
151175
"scripts": {
152176
"vscode:prepublish": "npm run compile",

resources/icons/dark/cwcutting.svg

Lines changed: 1 addition & 0 deletions
Loading

resources/icons/dark/gcode.svg

Lines changed: 1019 additions & 0 deletions
Loading

resources/icons/dark/refresh.svg

Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

resources/icons/light/gcode.svg

Lines changed: 1019 additions & 0 deletions
Loading

resources/icons/light/refresh.svg

Lines changed: 1 addition & 0 deletions
Loading

src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class Config {
1313
this.config = workspace.getConfiguration("gcode");
1414
}
1515

16+
getParam(param: string): any {
17+
18+
return this.config.get(param);
19+
}
20+
1621
}
1722

1823
export const config = new Config();

src/extension.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
2+
import { config } from './config';
33
import * as util from './util';
44
import * as manifest from './manifest.json';
55

@@ -17,10 +17,12 @@ export function activate(context: vscode.ExtensionContext) {
1717
conout.show(true);
1818
conout.appendLine(name + " v" + version + " activated.");
1919

20-
// Enable Tree View
20+
// G-Code Tree View
2121
const gcodeTree = new GCodeTreeProvider(context);
2222
vscode.window.registerTreeDataProvider('gcodeTree', gcodeTree);
2323

24+
vscode.commands.registerCommand('gcodeTree.refreshEntry', () => gcodeTree.refresh());
25+
2426
conout.appendLine("G-Code Tree View Enabled");
2527

2628
/*

src/providers/gcodeParser.ts

Lines changed: 176 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,189 @@
1+
import * as vscode from 'vscode';
2+
import {GCodeTreeNode} from './gcodeTree';
13

24
export class GCodeParser {
35

6+
private blocks: Array<GCodeTreeNode>;
7+
48
constructor( readonly text: string) {
5-
9+
10+
this.blocks = this.getBlocks(text);
11+
12+
}
13+
14+
getTree(): Array<GCodeTreeNode> {
15+
return this.blocks;
616
}
717

18+
// Split Text into Blocks by newline or ;
19+
private getBlocks(text: string): Array<any> {
820

9-
// Comments
10-
private stripComments(line: string): string {
11-
const re1 = new RegExp(/\s*\([^\)]*\)/g); // Remove anything inside the parentheses
12-
const re2 = new RegExp(/\s*;.*/g); // Remove anything after a semi-colon to the end of the line, including preceding spaces
13-
const re3 = new RegExp(/\s+/g);
21+
const nodes:Array<any> = [];
22+
const lines = text.match(/.*(?:\r\n|\r|\n)/g) || [];
23+
24+
for (let i = 0; i < lines.length; ++i) {
25+
26+
const line = lines[i].trim();
1427

15-
return (line.replace(re1, '').replace(re2, '').replace(re3, ''));
28+
if (line.length === 0) {
29+
continue;
30+
}
31+
32+
const result = this.parseLine(line);
33+
if (result.length !== 0) {
34+
nodes.push(result);
35+
}
36+
}
37+
38+
return nodes;
39+
40+
}
41+
42+
// Parse Line for Blocks
43+
private parseLine(line: string): Array<GCodeTreeNode> {
44+
45+
let blocks: Array<GCodeTreeNode> = [];
46+
let node: GCodeTreeNode;
47+
48+
// Regexp to Pull key words
49+
const re = /((GOTO)|(IF)|(EQ)|(NE)|(LT)|(GT)|(LE)|(GE)|(DO)|(WHILE)|(END)|(AND)|(OR)|(XOR)|(SIN)|(COS)|(TAN)|(ASIN)|(ACOS)|(ATAN)|(FIX)|(FUP)|(LN)|(ROUND)|(SQRT)|(FIX)|(FUP)|(ROUND)|(ABS))|((?:\$\$)|(?:\$[a-zA-Z0-9#]*))|([a-zA-Z][0-9\+\-\.]+)|(\*[0-9]+)|([#][0-9]+)|([#][\[].+[\]])/igm;
50+
51+
// Strip Comments
52+
line = this.stripComments(line);
53+
54+
// Get Words
55+
const words = line.match(re) || [];
56+
57+
for (let i = 0; i < words.length; ++i) {
58+
const word = words[i];
59+
const letter = word[0].toUpperCase();
60+
const argument = word.slice(1);
61+
62+
// G-Code
63+
if (letter === 'G') {
64+
65+
switch(argument) {
66+
67+
// Rapid Motion
68+
case '00':
69+
case '0' :
70+
node = new GCodeTreeNode(
71+
'Rapid',
72+
vscode.TreeItemCollapsibleState.None,
73+
);
74+
node.tooltip = '[G00] Rapid Motion';
75+
node.setIcon('rapid');
76+
77+
blocks.push(node);
78+
break;
79+
80+
// Linear / Cutting
81+
case '01':
82+
case '1' :
83+
node = new GCodeTreeNode(
84+
'Cutting',
85+
vscode.TreeItemCollapsibleState.None,
86+
);
87+
node.tooltip = '[G01] Linear]';
88+
node.setIcon('cutting');
89+
90+
blocks.push(node);
91+
break;
92+
93+
// CW Motion
94+
case '02':
95+
case '2' :
96+
node = new GCodeTreeNode(
97+
'CW Cutting',
98+
vscode.TreeItemCollapsibleState.None,
99+
);
100+
node.tooltip = '[G02] CW Interpolation';
101+
node.setIcon('cwcutting');
102+
103+
blocks.push(node);
104+
break;
105+
106+
// CCW Motion
107+
case '03':
108+
case '3' :
109+
node = new GCodeTreeNode(
110+
'CCW Cutting',
111+
vscode.TreeItemCollapsibleState.None,
112+
);
113+
node.tooltip = '[G03] CCW Interpolation';
114+
node.setIcon('ccwcutting');
115+
116+
blocks.push(node);
117+
break;
118+
119+
default: {
120+
break;
121+
}
122+
}
123+
124+
} else if (letter === 'M') {
125+
126+
switch(argument) {
127+
128+
// Tool Change
129+
case '06':
130+
case '6' :
131+
node = new GCodeTreeNode(
132+
'Tool Change',
133+
vscode.TreeItemCollapsibleState.None,
134+
);
135+
node.tooltip = 'Tool Change';
136+
node.setIcon('toolchange');
137+
138+
blocks.push(node);
139+
break;
140+
141+
142+
// Coolant On
143+
case '08':
144+
case '8' :
145+
node = new GCodeTreeNode(
146+
'Coolant On',
147+
vscode.TreeItemCollapsibleState.None,
148+
);
149+
node.tooltip = 'Coolant Turned On';
150+
node.setIcon('coolant');
151+
152+
blocks.push(node);
153+
break;
154+
155+
// Coolant Off
156+
case '09':
157+
case '9' :
158+
node = new GCodeTreeNode(
159+
'Coolant Off',
160+
vscode.TreeItemCollapsibleState.None,
161+
);
162+
node.tooltip = 'Coolant Turned Off';
163+
node.setIcon('coolant');
164+
165+
blocks.push(node);
166+
167+
default:
168+
break;
169+
}
170+
}
16171
}
17172

173+
return blocks;
174+
175+
176+
}
177+
178+
// Comments
179+
private stripComments(line: string): string {
180+
const re1 = new RegExp(/\s*\([^\)]*\)/g); // Remove anything inside the parentheses
181+
const re2 = new RegExp(/\s*;.*/g); // Remove anything after a semi-colon to the end of the line, including preceding spaces
182+
const re3 = new RegExp(/\s+/g);
183+
184+
return (line.replace(re1, '').replace(re2, '').replace(re3, ''));
185+
}
186+
18187

19188

20189

0 commit comments

Comments
 (0)