|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import {GCodeTreeNode} from './gcodeTree'; |
1 | 3 |
|
2 | 4 | export class GCodeParser { |
3 | 5 |
|
| 6 | + private blocks: Array<GCodeTreeNode>; |
| 7 | + |
4 | 8 | constructor( readonly text: string) { |
5 | | - |
| 9 | + |
| 10 | + this.blocks = this.getBlocks(text); |
| 11 | + |
| 12 | + } |
| 13 | + |
| 14 | + getTree(): Array<GCodeTreeNode> { |
| 15 | + return this.blocks; |
6 | 16 | } |
7 | 17 |
|
| 18 | + // Split Text into Blocks by newline or ; |
| 19 | + private getBlocks(text: string): Array<any> { |
8 | 20 |
|
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(); |
14 | 27 |
|
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 | + } |
16 | 171 | } |
17 | 172 |
|
| 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 | + |
18 | 187 |
|
19 | 188 |
|
20 | 189 |
|
|
0 commit comments