circuitscript 0.0.24 → 0.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/cjs/BaseVisitor.js +487 -0
  2. package/dist/cjs/SemanticTokenVisitor.js +218 -0
  3. package/dist/cjs/SymbolValidatorVisitor.js +233 -0
  4. package/dist/cjs/antlr/CircuitScriptLexer.js +209 -195
  5. package/dist/cjs/antlr/CircuitScriptParser.js +2310 -2087
  6. package/dist/cjs/antlr/CircuitScriptVisitor.js +4 -3
  7. package/dist/cjs/draw_symbols.js +67 -22
  8. package/dist/cjs/execute.js +51 -53
  9. package/dist/cjs/geometry.js +28 -8
  10. package/dist/cjs/helpers.js +175 -5
  11. package/dist/cjs/index.js +2 -0
  12. package/dist/cjs/layout.js +8 -0
  13. package/dist/cjs/lexer.js +19 -22
  14. package/dist/cjs/main.js +6 -11
  15. package/dist/cjs/objects/ClassComponent.js +3 -0
  16. package/dist/cjs/objects/ExecutionScope.js +1 -0
  17. package/dist/cjs/objects/types.js +7 -1
  18. package/dist/cjs/parser.js +29 -258
  19. package/dist/cjs/validate.js +81 -0
  20. package/dist/cjs/visitor.js +529 -820
  21. package/dist/esm/BaseVisitor.mjs +488 -0
  22. package/dist/esm/SemanticTokenVisitor.mjs +215 -0
  23. package/dist/esm/SymbolValidatorVisitor.mjs +222 -0
  24. package/dist/esm/antlr/CircuitScriptLexer.mjs +184 -194
  25. package/dist/esm/antlr/CircuitScriptParser.mjs +2279 -2084
  26. package/dist/esm/antlr/CircuitScriptVisitor.mjs +8 -3
  27. package/dist/esm/draw_symbols.mjs +67 -22
  28. package/dist/esm/execute.mjs +50 -52
  29. package/dist/esm/geometry.mjs +28 -8
  30. package/dist/esm/helpers.mjs +165 -6
  31. package/dist/esm/index.mjs +2 -0
  32. package/dist/esm/layout.mjs +8 -0
  33. package/dist/esm/lexer.mjs +10 -10
  34. package/dist/esm/main.mjs +7 -12
  35. package/dist/esm/objects/ClassComponent.mjs +3 -0
  36. package/dist/esm/objects/ExecutionScope.mjs +1 -0
  37. package/dist/esm/objects/types.mjs +6 -0
  38. package/dist/esm/parser.mjs +25 -230
  39. package/dist/esm/validate.mjs +74 -0
  40. package/dist/esm/visitor.mjs +343 -640
  41. package/dist/types/BaseVisitor.d.ts +69 -0
  42. package/dist/types/SemanticTokenVisitor.d.ts +36 -0
  43. package/dist/types/SymbolValidatorVisitor.d.ts +61 -0
  44. package/dist/types/antlr/CircuitScriptLexer.d.ts +8 -7
  45. package/dist/types/antlr/CircuitScriptParser.d.ts +513 -469
  46. package/dist/types/antlr/CircuitScriptVisitor.d.ts +69 -59
  47. package/dist/types/draw_symbols.d.ts +9 -0
  48. package/dist/types/execute.d.ts +5 -8
  49. package/dist/types/geometry.d.ts +4 -0
  50. package/dist/types/helpers.d.ts +32 -1
  51. package/dist/types/index.d.ts +2 -0
  52. package/dist/types/lexer.d.ts +2 -2
  53. package/dist/types/objects/ExecutionScope.d.ts +4 -1
  54. package/dist/types/objects/types.d.ts +5 -0
  55. package/dist/types/parser.d.ts +15 -28
  56. package/dist/types/validate.d.ts +2 -0
  57. package/dist/types/visitor.d.ts +40 -95
  58. package/fonts/Inter-Bold.ttf +0 -0
  59. package/fonts/Inter-Regular.ttf +0 -0
  60. package/fonts/OpenSans-Regular.ttf +0 -0
  61. package/fonts/Roboto-Regular.ttf +0 -0
  62. package/libs/lib.cst +183 -0
  63. package/package.json +11 -6
@@ -1,298 +1,69 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.CircuitscriptParserErrorListener = exports.SemanticTokensVisitor = exports.parseFileWithVisitor = void 0;
27
- const antlr4_1 = require("antlr4");
28
- const CircuitScriptParser_js_1 = __importStar(require("./antlr/CircuitScriptParser.js"));
3
+ exports.CircuitscriptParserErrorListener = exports.TempErrorStrategy = exports.parseFileWithVisitor = void 0;
4
+ const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
29
5
  const lexer_js_1 = require("./lexer.js");
30
6
  const utils_js_1 = require("./utils.js");
7
+ const antlr4ng_1 = require("antlr4ng");
31
8
  function parseFileWithVisitor(visitor, data) {
32
- const chars = new antlr4_1.CharStream(data);
9
+ const chars = antlr4ng_1.CharStream.fromString(data);
33
10
  const lexer = new lexer_js_1.MainLexer(chars);
34
11
  const lexerTimer = new utils_js_1.SimpleStopwatch();
35
- const tokens = new antlr4_1.CommonTokenStream(lexer);
12
+ const tokens = new antlr4ng_1.CommonTokenStream(lexer);
36
13
  tokens.fill();
37
14
  const lexerTimeTaken = lexerTimer.lap();
38
15
  const parserTimer = new utils_js_1.SimpleStopwatch();
39
- const parser = new CircuitScriptParser_js_1.default(tokens);
40
- parser.removeErrorListeners();
41
- const errorListener = new CircuitscriptParserErrorListener();
42
- parser.addErrorListener(errorListener);
16
+ const parser = new CircuitScriptParser_js_1.CircuitScriptParser(tokens);
43
17
  const tree = parser.script();
44
18
  let hasError = false;
45
19
  try {
46
20
  visitor.visit(tree);
47
21
  }
48
22
  catch (err) {
49
- err.print(data);
23
+ console.log(err);
50
24
  hasError = true;
51
25
  }
52
26
  const parserTimeTaken = parserTimer.lap();
53
- const semanticTokenVisitor = new SemanticTokensVisitor(lexer, data);
54
- semanticTokenVisitor.visit(tree);
55
- const semanticTokens = semanticTokenVisitor.semanticTokens;
56
- const parsedTokens = prepareTokens(tokens.tokens, lexer, data);
57
- const finalParsedTokens = [];
58
- parsedTokens.forEach(token => {
59
- const location = `${token.line}_${token.column}`;
60
- if (semanticTokens.has(location)) {
61
- finalParsedTokens.push(semanticTokens.get(location));
62
- }
63
- else {
64
- finalParsedTokens.push(token);
65
- }
66
- });
67
27
  return {
68
28
  tree, parser,
69
- hasParseError: errorListener.hasParseErrors(),
29
+ hasParseError: false,
70
30
  hasError,
71
31
  parserTimeTaken,
72
32
  lexerTimeTaken,
73
- tokens: finalParsedTokens,
74
33
  };
75
34
  }
76
35
  exports.parseFileWithVisitor = parseFileWithVisitor;
77
- class SemanticTokensVisitor extends antlr4_1.ParseTreeVisitor {
78
- constructor(lexer, script) {
79
- super();
80
- this.parsedTokens = [];
81
- this.semanticTokens = new Map();
82
- this.lexer = lexer;
83
- this.script = script;
84
- }
85
- visit(ctx) {
86
- const here = this;
87
- if (Array.isArray(ctx)) {
88
- return ctx.map(function (child) {
89
- try {
90
- here.checkContext(child);
91
- return child.accept(this);
92
- }
93
- catch (err) {
94
- this.handleError(child, err);
95
- }
96
- }, this);
97
- }
98
- else {
99
- try {
100
- this.checkContext(ctx);
101
- return ctx.accept(this);
102
- }
103
- catch (err) {
104
- this.handleError(ctx, err);
105
- }
106
- }
36
+ class TempErrorStrategy extends antlr4ng_1.DefaultErrorStrategy {
37
+ recover(recognizer, e) {
38
+ throw new Error('Method not implemented.');
107
39
  }
108
- handleError(ctx, err) {
109
- console.log('error!', err);
40
+ reportError(recognizer, e) {
41
+ throw new Error('Method not implemented.');
110
42
  }
111
- checkContext(ctx) {
112
- if (ctx instanceof CircuitScriptParser_js_1.Function_def_exprContext) {
113
- this.addSemanticToken(this.parseToken(ctx.ID(), ['declaration'], 'function'));
114
- }
115
- else if (ctx instanceof CircuitScriptParser_js_1.Create_component_exprContext
116
- || ctx instanceof CircuitScriptParser_js_1.Create_graphic_exprContext) {
117
- this.addSemanticToken(this.parseToken(ctx.Create(), ['defaultLibrary'], 'function'));
118
- }
119
- else if (ctx instanceof CircuitScriptParser_js_1.Atom_exprContext) {
120
- if (ctx.ID()) {
121
- if (ctx.trailer_expr_list().length > 0) {
122
- this.addSemanticToken(this.parseToken(ctx.ID(), ['declaration'], 'function'));
123
- }
124
- else {
125
- this.addSemanticToken(this.parseToken(ctx.ID(), ['declaration'], 'variable'));
126
- }
127
- }
128
- }
129
- else if (ctx instanceof CircuitScriptParser_js_1.Property_key_exprContext) {
130
- let useToken = null;
131
- if (ctx.ID()) {
132
- useToken = ctx.ID();
133
- }
134
- else if (ctx.INTEGER_VALUE()) {
135
- useToken = ctx.INTEGER_VALUE();
136
- }
137
- else if (ctx.STRING_VALUE()) {
138
- useToken = ctx.STRING_VALUE();
139
- }
140
- useToken && this.addSemanticToken(this.parseToken(useToken, ['declaration'], 'property'));
141
- }
142
- else if (ctx instanceof CircuitScriptParser_js_1.Sub_exprContext) {
143
- let useToken = null;
144
- if (ctx.ID()) {
145
- useToken = ctx.ID();
146
- }
147
- else if (ctx.Pin()) {
148
- useToken = ctx.Pin();
149
- }
150
- useToken && this.addSemanticToken(this.parseToken(useToken, ['defaultLibrary'], 'function'));
151
- }
152
- }
153
- addSemanticToken(parsedToken) {
154
- this.semanticTokens.set(parsedToken.line + "_" + parsedToken.column, parsedToken);
43
+ }
44
+ exports.TempErrorStrategy = TempErrorStrategy;
45
+ class CircuitscriptParserErrorListener {
46
+ constructor(onErrorHandler = null) {
47
+ this.syntaxErrorCounter = 0;
48
+ this.onErrorHandler = null;
49
+ this.onErrorHandler = onErrorHandler;
155
50
  }
156
- parseToken(node, modifiers, tokenType = null) {
157
- const token = node.symbol;
158
- let stringValue = "";
159
- let textPart = "";
160
- if (this.lexer.symbolicNames[token.type] !== null && this.lexer.symbolicNames[token.type] !== undefined) {
161
- stringValue = this.lexer.symbolicNames[token.type];
162
- if (stringValue !== "NEWLINE") {
163
- textPart = this.script.substring(token.start, token.stop + 1);
164
- }
165
- else {
166
- textPart = token.text.length - 1;
167
- }
168
- }
169
- else if (this.lexer.literalNames[token.type] !== null && this.lexer.literalNames[token.type] !== undefined) {
170
- stringValue = this.lexer.literalNames[token.type];
171
- textPart = this.script.substring(token.start, token.stop + 1);
51
+ syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e) {
52
+ if (this.onErrorHandler) {
53
+ this.onErrorHandler(line, charPositionInLine, msg, e);
172
54
  }
173
55
  else {
174
- stringValue = token._text;
175
- }
176
- return {
177
- line: token.line,
178
- column: token.column,
179
- length: token.stop - token.start + 1,
180
- tokenType: tokenType !== null ? tokenType : stringValue,
181
- tokenModifiers: modifiers,
182
- textValue: textPart,
183
- };
184
- }
185
- }
186
- exports.SemanticTokensVisitor = SemanticTokensVisitor;
187
- function prepareTokens(tokens, lexer, script) {
188
- const parsedTokens = [];
189
- tokens.forEach(item => {
190
- if (item.type !== -1) {
191
- let stringValue = "";
192
- let textPart = "";
193
- if (lexer.symbolicNames[item.type] !== null && lexer.symbolicNames[item.type] !== undefined) {
194
- stringValue = lexer.symbolicNames[item.type];
195
- if (stringValue !== "NEWLINE") {
196
- textPart = script.substring(item.start, item.stop + 1);
197
- }
198
- else {
199
- textPart = item.text.length - 1;
200
- }
201
- }
202
- else if (lexer.literalNames[item.type] !== null && lexer.literalNames[item.type] !== undefined) {
203
- stringValue = lexer.literalNames[item.type];
204
- textPart = script.substring(item.start, item.stop + 1);
205
- }
206
- else {
207
- stringValue = item._text;
208
- }
209
- if (textPart !== 0 && textPart !== '') {
210
- parsedTokens.push({
211
- line: item.line,
212
- column: item.column,
213
- length: item.stop - item.start + 1,
214
- tokenType: resolveTokenType(stringValue),
215
- tokenModifiers: resolveTokenModifiers(stringValue),
216
- textValue: textPart,
217
- });
218
- }
56
+ console.log("Syntax error at line", line, ':', charPositionInLine, ' - ', msg);
219
57
  }
220
- });
221
- return parsedTokens;
222
- }
223
- const languageKeywords = [
224
- 'break', 'branch', 'create', 'component',
225
- 'graphic', 'wire', 'pin', 'add', 'at', 'to',
226
- 'point', 'join', 'parallel', 'return', 'def', 'import',
227
- 'true', 'false', 'nc', 'frame',
228
- ];
229
- const operatorKeywords = [
230
- 'at', 'to', 'wire', 'add', 'frame', 'join', 'parallel', 'point'
231
- ];
232
- function resolveTokenType(tokenType) {
233
- if (operatorKeywords.indexOf(tokenType.toLowerCase()) !== -1) {
234
- return 'graphKeyword';
58
+ this.syntaxErrorCounter++;
235
59
  }
236
- else if (languageKeywords.indexOf(tokenType.toLowerCase()) !== -1) {
237
- return 'keyword';
60
+ reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs) {
238
61
  }
239
- else {
240
- switch (tokenType) {
241
- case 'INTEGER_VALUE':
242
- case 'NUMERIC_VALUE':
243
- case 'DECIMAL_VALUE':
244
- case 'PERCENTAGE_VALUE':
245
- return 'number';
246
- case 'STRING_VALUE':
247
- return 'string';
248
- case 'ID':
249
- return 'variable';
250
- case 'Define':
251
- return 'keyword';
252
- case 'COMMENT':
253
- return 'comment';
254
- }
255
- return null;
62
+ reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs) {
256
63
  }
257
- }
258
- function resolveTokenModifiers(tokenType) {
259
- return [];
260
- }
261
- function dumpTokens(tokens, lexer, scriptData) {
262
- tokens.forEach(item => {
263
- if (item.type !== -1) {
264
- let stringValue = "";
265
- let textPart = "";
266
- if (lexer.symbolicNames[item.type] !== null && lexer.symbolicNames[item.type] !== undefined) {
267
- stringValue = lexer.symbolicNames[item.type];
268
- if (stringValue !== "NEWLINE") {
269
- textPart = scriptData.substring(item.start, item.stop + 1);
270
- }
271
- else {
272
- textPart = item.text.length - 1;
273
- }
274
- }
275
- else if (lexer.literalNames[item.type] !== null && lexer.literalNames[item.type] !== undefined) {
276
- stringValue = lexer.literalNames[item.type];
277
- textPart = scriptData.substring(item.start, item.stop + 1);
278
- }
279
- else {
280
- stringValue = item._text;
281
- }
282
- console.log('line', item.line + ':' + item.column, `\t${stringValue} (${item.type})`.padEnd(30), textPart);
283
- }
284
- });
285
- }
286
- class CircuitscriptParserErrorListener extends antlr4_1.ErrorListener {
287
- constructor() {
288
- super(...arguments);
289
- this.syntaxErrorCounter = 0;
290
- }
291
- syntaxError(recognizer, offendingSymbol, line, column, msg, e) {
292
- console.log("Syntax error at line", line, ':', column, ' - ', msg);
293
- this.syntaxErrorCounter++;
64
+ reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs) {
294
65
  }
295
- hasParseErrors() {
66
+ hasSyntaxErrors() {
296
67
  return (this.syntaxErrorCounter > 0);
297
68
  }
298
69
  }
@@ -0,0 +1,81 @@
1
+ #! /usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.validate = void 0;
8
+ const commander_1 = require("commander");
9
+ const figlet_1 = __importDefault(require("figlet"));
10
+ const path_1 = __importDefault(require("path"));
11
+ const fs_1 = require("fs");
12
+ const sizing_js_1 = require("./sizing.js");
13
+ const helpers_js_1 = require("./helpers.js");
14
+ async function validate() {
15
+ const { filePath } = (0, helpers_js_1.getCurrentPath)();
16
+ const toolSrcPath = filePath;
17
+ const toolDirectory = path_1.default.dirname(toolSrcPath) + '/../../';
18
+ const fontsPath = toolDirectory + '/fonts';
19
+ const defaultLibsPath = toolDirectory + '/libs';
20
+ const packageJson = JSON.parse((0, fs_1.readFileSync)(toolDirectory + 'package.json').toString());
21
+ ;
22
+ const { version } = packageJson;
23
+ commander_1.program
24
+ .description('generate graphical output from circuitscript files')
25
+ .version(version)
26
+ .option('-i, --input text <input text>', 'Input text directly')
27
+ .option('-f, --input-file <path>', 'Input file')
28
+ .option('-o, --output <path>', 'Output path')
29
+ .option('-c, --current-directory <path>', 'Set current directory')
30
+ .option('-k, --kicad-netlist <filename>', 'Create KiCad netlist')
31
+ .option('-w, --watch', 'Watch for file changes')
32
+ .option('-n, --dump-nets', 'Dump out net information')
33
+ .option('-d, --dump-data', 'Dump data during parsing')
34
+ .option('-s, --stats', 'Show stats during generation');
35
+ commander_1.program.addHelpText('before', figlet_1.default.textSync('circuitscript', {
36
+ font: 'Small Slant'
37
+ }));
38
+ if (process.argv.length < 3) {
39
+ commander_1.program.help();
40
+ }
41
+ commander_1.program.parse();
42
+ const options = commander_1.program.opts();
43
+ const watchFileChanges = options.watch;
44
+ const outputPath = options.output ?? null;
45
+ const dumpNets = options.dumpNets;
46
+ const dumpData = options.dumpData;
47
+ const kicadNetlist = options.kicadNetlist;
48
+ let currentDirectory = options.currentDirectory ?? null;
49
+ if (watchFileChanges) {
50
+ console.log('watching for file changes...');
51
+ }
52
+ await (0, sizing_js_1.prepareSVGEnvironment)(fontsPath);
53
+ let inputFilePath = null;
54
+ let scriptData;
55
+ if (options.input) {
56
+ scriptData = options.input;
57
+ }
58
+ else {
59
+ inputFilePath = options.inputFile;
60
+ const tmpScriptData = (0, helpers_js_1.getScriptText)(inputFilePath);
61
+ if (tmpScriptData === null) {
62
+ throw "File does not exists";
63
+ }
64
+ scriptData = tmpScriptData;
65
+ if (currentDirectory === null) {
66
+ currentDirectory = path_1.default.dirname(inputFilePath);
67
+ }
68
+ }
69
+ const scriptOptions = {
70
+ currentDirectory,
71
+ defaultLibsPath,
72
+ dumpNets,
73
+ dumpData,
74
+ kicadNetlistPath: kicadNetlist,
75
+ showStats: options.stats,
76
+ };
77
+ const visitor = (0, helpers_js_1.validateScript)(scriptData, scriptOptions);
78
+ const semanticTokensVisitor = (0, helpers_js_1.getSemanticTokens)(scriptData, scriptOptions);
79
+ }
80
+ exports.validate = validate;
81
+ validate();