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
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareTokens = exports.SemanticTokensVisitor = void 0;
4
+ const CircuitScriptParser_1 = require("./antlr/CircuitScriptParser");
5
+ const BaseVisitor_1 = require("./BaseVisitor");
6
+ class SemanticTokensVisitor extends BaseVisitor_1.BaseVisitor {
7
+ constructor(silent = false, onErrorHandler = null, currentDirectory, defaultsLibsPath, lexer, script) {
8
+ super(silent, onErrorHandler, currentDirectory, defaultsLibsPath);
9
+ this.parsedTokens = [];
10
+ this.semanticTokens = new Map();
11
+ this.visitFunction_args_expr = (ctx) => {
12
+ const IDs = ctx.ID();
13
+ IDs.map(id => {
14
+ this.addSemanticToken(id, ['declaration'], 'parameter');
15
+ });
16
+ };
17
+ this.visitFunction_call_expr = (ctx) => {
18
+ this.addSemanticToken(ctx.ID(), [], 'function');
19
+ };
20
+ this.visitFunction_def_expr = (ctx) => {
21
+ const functionName = ctx.ID().getText();
22
+ this.addSemanticToken(ctx.ID(), ['declaration'], 'function');
23
+ const ctxFunctionArgsExpr = ctx.function_args_expr();
24
+ if (ctxFunctionArgsExpr) {
25
+ this.visit(ctxFunctionArgsExpr);
26
+ }
27
+ const executionContextName = functionName + '_validate';
28
+ const newExecutor = this.enterNewChildContext(this.executionStack, this.getExecutor(), executionContextName, { netNamespace: "" }, [], []);
29
+ this.runExpressions(newExecutor, ctx.function_expr());
30
+ this.executionStack.pop();
31
+ };
32
+ this.visitCreate_component_expr = (ctx) => {
33
+ this.addSemanticToken(ctx.Create(), ['defaultLibrary'], 'function');
34
+ ctx.property_expr().forEach(property_expr => {
35
+ this.visit(property_expr);
36
+ });
37
+ };
38
+ this.visitCreate_graphic_expr = (ctx) => {
39
+ this.addSemanticToken(ctx.Create(), ['defaultLibrary'], 'function');
40
+ ctx.sub_expr().forEach(sub_expr => {
41
+ this.visit(sub_expr);
42
+ });
43
+ };
44
+ this.visitProperty_key_expr = (ctx) => {
45
+ let useValue = null;
46
+ const ctxId = ctx.ID();
47
+ const ctxIntegerValue = ctx.INTEGER_VALUE();
48
+ const ctxStringValue = ctx.STRING_VALUE();
49
+ if (ctxId) {
50
+ useValue = ctxId;
51
+ }
52
+ else if (ctxIntegerValue) {
53
+ useValue = ctxIntegerValue;
54
+ }
55
+ else if (ctxStringValue) {
56
+ useValue = ctxStringValue;
57
+ }
58
+ if (useValue) {
59
+ this.addSemanticToken(useValue, [], 'property');
60
+ }
61
+ };
62
+ this.visitSub_expr = (ctx) => {
63
+ let useValue = null;
64
+ const ctxId = ctx.ID();
65
+ const ctxPin = ctx.Pin();
66
+ if (ctxId) {
67
+ useValue = ctxId;
68
+ }
69
+ else if (ctxPin) {
70
+ useValue = ctxPin;
71
+ }
72
+ if (useValue) {
73
+ this.addSemanticToken(useValue, [], 'property');
74
+ }
75
+ };
76
+ this.visitValueAtomExpr = (ctx) => {
77
+ const ctxValueExpr = ctx.value_expr();
78
+ const ctxAtomExpr = ctx.atom_expr();
79
+ if (ctxValueExpr) {
80
+ this.visit(ctxValueExpr);
81
+ }
82
+ else if (ctxAtomExpr) {
83
+ this.visit(ctxAtomExpr);
84
+ }
85
+ };
86
+ this.visitAssignment_expr = (ctx) => {
87
+ this.visit(ctx.atom_expr());
88
+ this.visit(ctx.data_expr());
89
+ };
90
+ this.visitAtom_expr = (ctx) => {
91
+ if (ctx.parent instanceof CircuitScriptParser_1.Assignment_exprContext && ctx.ID(0)) {
92
+ this.addSemanticToken(ctx.ID(0), [], 'variable');
93
+ }
94
+ };
95
+ this.visitImport_expr = (ctx) => {
96
+ this.addSemanticToken(ctx.ID(), [], 'namespace');
97
+ };
98
+ this.lexer = lexer;
99
+ this.script = script;
100
+ }
101
+ addSemanticToken(node, modifiers, tokenType = null) {
102
+ const parsedToken = this.parseToken(node, modifiers, tokenType);
103
+ this.semanticTokens.set(parsedToken.line + "_" + parsedToken.column, parsedToken);
104
+ }
105
+ parseToken(node, modifiers, tokenType = null) {
106
+ const token = node.symbol;
107
+ let stringValue = "";
108
+ let textPart = "";
109
+ if (this.lexer.symbolicNames[token.type] !== null && this.lexer.symbolicNames[token.type] !== undefined) {
110
+ stringValue = this.lexer.symbolicNames[token.type];
111
+ if (stringValue !== "NEWLINE") {
112
+ textPart = this.script.substring(token.start, token.stop + 1);
113
+ }
114
+ else {
115
+ textPart = token.text.length - 1;
116
+ }
117
+ }
118
+ else if (this.lexer.literalNames[token.type] !== null && this.lexer.literalNames[token.type] !== undefined) {
119
+ stringValue = this.lexer.literalNames[token.type];
120
+ textPart = this.script.substring(token.start, token.stop + 1);
121
+ }
122
+ else {
123
+ stringValue = token._text;
124
+ }
125
+ return {
126
+ line: token.line,
127
+ column: token.column,
128
+ length: token.stop - token.start + 1,
129
+ tokenType: tokenType !== null ? tokenType : stringValue,
130
+ tokenModifiers: modifiers,
131
+ textValue: textPart,
132
+ };
133
+ }
134
+ dumpTokens() {
135
+ for (const [id, value] of this.semanticTokens) {
136
+ console.log(id, value);
137
+ }
138
+ }
139
+ getTokens() {
140
+ return this.semanticTokens;
141
+ }
142
+ }
143
+ exports.SemanticTokensVisitor = SemanticTokensVisitor;
144
+ function prepareTokens(tokens, lexer, script) {
145
+ const parsedTokens = [];
146
+ tokens.forEach(item => {
147
+ if (item.type !== -1) {
148
+ let stringValue = "";
149
+ let textPart = "";
150
+ if (lexer.symbolicNames[item.type] !== null && lexer.symbolicNames[item.type] !== undefined) {
151
+ stringValue = lexer.symbolicNames[item.type];
152
+ if (stringValue !== "NEWLINE") {
153
+ textPart = script.substring(item.start, item.stop + 1);
154
+ }
155
+ else {
156
+ textPart = item.text.length - 1;
157
+ }
158
+ }
159
+ else if (lexer.literalNames[item.type] !== null && lexer.literalNames[item.type] !== undefined) {
160
+ stringValue = lexer.literalNames[item.type];
161
+ textPart = script.substring(item.start, item.stop + 1);
162
+ }
163
+ else {
164
+ stringValue = item._text;
165
+ }
166
+ if (textPart !== 0 && textPart !== '') {
167
+ parsedTokens.push({
168
+ line: item.line,
169
+ column: item.column,
170
+ length: item.stop - item.start + 1,
171
+ tokenType: resolveTokenType(stringValue),
172
+ tokenModifiers: resolveTokenModifiers(stringValue),
173
+ textValue: textPart,
174
+ });
175
+ }
176
+ }
177
+ });
178
+ return parsedTokens;
179
+ }
180
+ exports.prepareTokens = prepareTokens;
181
+ const languageKeywords = [
182
+ 'break', 'branch', 'create', 'component',
183
+ 'graphic', 'wire', 'pin', 'add', 'at', 'to',
184
+ 'point', 'join', 'parallel', 'return', 'def', 'import',
185
+ 'true', 'false', 'nc', 'frame',
186
+ ];
187
+ const operatorKeywords = [
188
+ 'at', 'to', 'wire', 'add', 'frame', 'join', 'parallel', 'point'
189
+ ];
190
+ function resolveTokenType(tokenType) {
191
+ if (operatorKeywords.indexOf(tokenType.toLowerCase()) !== -1) {
192
+ return 'graphKeyword';
193
+ }
194
+ else if (languageKeywords.indexOf(tokenType.toLowerCase()) !== -1) {
195
+ return 'keyword';
196
+ }
197
+ else {
198
+ switch (tokenType) {
199
+ case 'INTEGER_VALUE':
200
+ case 'NUMERIC_VALUE':
201
+ case 'DECIMAL_VALUE':
202
+ case 'PERCENTAGE_VALUE':
203
+ return 'number';
204
+ case 'STRING_VALUE':
205
+ return 'string';
206
+ case 'ID':
207
+ return 'variable';
208
+ case 'Define':
209
+ return 'keyword';
210
+ case 'COMMENT':
211
+ return 'comment';
212
+ }
213
+ return null;
214
+ }
215
+ }
216
+ function resolveTokenModifiers(tokenType) {
217
+ return [];
218
+ }
@@ -0,0 +1,233 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SymbolTable = exports.SymbolValidatorResolveVisitor = exports.SymbolValidatorVisitor = void 0;
4
+ const BaseVisitor_js_1 = require("./BaseVisitor.js");
5
+ const types_js_1 = require("./objects/types.js");
6
+ class SymbolValidatorVisitor extends BaseVisitor_js_1.BaseVisitor {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.symbolTable = new SymbolTable();
10
+ this.visitImport_expr = (ctx) => {
11
+ const ID = ctx.ID().toString();
12
+ const { pathExists } = this.handleImportFile(ID, false);
13
+ if (!pathExists) {
14
+ this.symbolTable.addUndefined(this.getExecutor(), ID, ctx.ID());
15
+ }
16
+ };
17
+ this.visitAssignment_expr = (ctx) => {
18
+ const atomStr = ctx.atom_expr().getText();
19
+ const ctxDataExpr = ctx.data_expr();
20
+ this.visit(ctxDataExpr);
21
+ const value = this.getResult(ctxDataExpr);
22
+ this.addSymbolVariable(atomStr, value);
23
+ return null;
24
+ };
25
+ this.visitAtom_expr = (ctx) => {
26
+ const tmpSymbol = this.handleAtomSymbol(ctx.ID(0));
27
+ this.setResult(ctx, tmpSymbol);
28
+ };
29
+ this.visitFunction_call_expr = (ctx) => {
30
+ this.handleAtomSymbol(ctx.ID());
31
+ if (ctx.trailer_expr().length > 0) {
32
+ ctx.trailer_expr().forEach(item => {
33
+ if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
34
+ const params = item.parameters();
35
+ if (params) {
36
+ this.visit(params);
37
+ }
38
+ }
39
+ });
40
+ }
41
+ };
42
+ this.visitValueAtomExpr = (ctx) => {
43
+ let value = null;
44
+ const ctxValueExpr = ctx.value_expr();
45
+ const cxtAtomExpr = ctx.atom_expr();
46
+ if (ctxValueExpr) {
47
+ this.visit(ctxValueExpr);
48
+ value = this.getResult(ctxValueExpr);
49
+ }
50
+ else if (cxtAtomExpr) {
51
+ this.visit(cxtAtomExpr);
52
+ value = this.getResult(cxtAtomExpr);
53
+ }
54
+ this.setResult(ctx, value);
55
+ };
56
+ this.visitUnaryOperatorExpr = (ctx) => {
57
+ this.visit(ctx.data_expr());
58
+ };
59
+ this.visitMultiplyExpr = (ctx) => {
60
+ this.visit(ctx.data_expr(0));
61
+ this.visit(ctx.data_expr(1));
62
+ };
63
+ this.visitAdditionExpr = (ctx) => {
64
+ this.visit(ctx.data_expr(0));
65
+ this.visit(ctx.data_expr(1));
66
+ };
67
+ this.visitBinaryOperatorExpr = (ctx) => {
68
+ this.visit(ctx.data_expr(0));
69
+ this.visit(ctx.data_expr(1));
70
+ };
71
+ this.visitDataExpr = (ctx) => {
72
+ return;
73
+ };
74
+ this.visitFunction_def_expr = (ctx) => {
75
+ const functionName = ctx.ID().getText();
76
+ let funcDefinedParameters = [];
77
+ const ctxFunctionArgsExpr = ctx.function_args_expr();
78
+ if (ctxFunctionArgsExpr) {
79
+ this.visit(ctxFunctionArgsExpr);
80
+ funcDefinedParameters = this.getResult(ctxFunctionArgsExpr);
81
+ }
82
+ this.addSymbolFunction(functionName, funcDefinedParameters);
83
+ const executionContextName = functionName + '_validate';
84
+ const passedInParamsNull = funcDefinedParameters.map((param, index) => {
85
+ return ['position', index, null];
86
+ });
87
+ const newExecutor = this.enterNewChildContext(this.executionStack, this.getExecutor(), executionContextName, { netNamespace: "" }, funcDefinedParameters, passedInParamsNull);
88
+ funcDefinedParameters.forEach(param => {
89
+ this.addSymbolVariable(param[0], null, newExecutor);
90
+ });
91
+ this.runExpressions(newExecutor, ctx.function_expr());
92
+ this.executionStack.pop();
93
+ };
94
+ }
95
+ addSymbolVariable(name, value, executor = null) {
96
+ const useExecutor = executor === null ? this.getExecutor() : executor;
97
+ this.symbolTable.addVariable(useExecutor, name, value);
98
+ this.log2('add symbol variable: ' + name);
99
+ }
100
+ addSymbolFunction(functionName, funcDefinedParameters) {
101
+ if (!this.symbolTable.exists(this.getExecutor(), functionName)) {
102
+ this.symbolTable.addFunction(this.getExecutor(), functionName, funcDefinedParameters);
103
+ this.log2('add symbol function: ' + functionName);
104
+ }
105
+ }
106
+ handleAtomSymbol(atom) {
107
+ const atomId = atom.getText();
108
+ const executor = this.getExecutor();
109
+ let tmpSymbol;
110
+ if (this.symbolTable.exists(executor, atomId)) {
111
+ tmpSymbol = this.symbolTable.get(executor, atomId);
112
+ }
113
+ else {
114
+ const foundContext = this.symbolTable.searchParentContext(executor, atomId);
115
+ if (foundContext === null) {
116
+ tmpSymbol = this.symbolTable.addUndefined(executor, atomId, atom);
117
+ this.log2('symbol not found: ' + atomId);
118
+ }
119
+ else {
120
+ tmpSymbol = this.symbolTable.get(foundContext, atomId);
121
+ }
122
+ }
123
+ return tmpSymbol;
124
+ }
125
+ setSymbols(symbolTable) {
126
+ this.symbolTable = symbolTable;
127
+ }
128
+ getSymbols() {
129
+ return this.symbolTable;
130
+ }
131
+ dumpSymbols() {
132
+ this.symbolTable.dumpSymbols();
133
+ }
134
+ }
135
+ exports.SymbolValidatorVisitor = SymbolValidatorVisitor;
136
+ class SymbolValidatorResolveVisitor extends SymbolValidatorVisitor {
137
+ addSymbolVariable(name, value) {
138
+ }
139
+ addSymbolFunction(functionName, funcDefinedParameters) {
140
+ if (this.symbolTable.exists(this.getExecutor(), functionName)) {
141
+ this.symbolTable.addFunction(this.getExecutor(), functionName, funcDefinedParameters);
142
+ }
143
+ }
144
+ }
145
+ exports.SymbolValidatorResolveVisitor = SymbolValidatorResolveVisitor;
146
+ class SymbolTable {
147
+ constructor() {
148
+ this.symbols = new Map();
149
+ this.executonContextsNamespaces = [];
150
+ }
151
+ getSymbols() {
152
+ return this.symbols;
153
+ }
154
+ addFunction(executionContext, id, funcDefinedParameters) {
155
+ return this.add(executionContext, id, types_js_1.ParseSymbolType.Function, {
156
+ funcDefinedParameters
157
+ });
158
+ }
159
+ addVariable(executionContext, id, variableValue) {
160
+ return this.add(executionContext, id, types_js_1.ParseSymbolType.Variable, {
161
+ variableValue
162
+ });
163
+ }
164
+ addUndefined(executionContext, id, node) {
165
+ return this.add(executionContext, id, types_js_1.ParseSymbolType.Undefined, {
166
+ node
167
+ });
168
+ }
169
+ add(executionContext, id, type, extra) {
170
+ if (this.executonContextsNamespaces.indexOf(executionContext.namespace) === -1) {
171
+ this.executonContextsNamespaces.push(executionContext.namespace);
172
+ }
173
+ const item = {
174
+ id,
175
+ type,
176
+ context: executionContext,
177
+ extra
178
+ };
179
+ this.symbols.set(this.idName(executionContext, id), item);
180
+ return item;
181
+ }
182
+ idName(executionContext, id) {
183
+ return executionContext.namespace + id;
184
+ }
185
+ dumpSymbols() {
186
+ for (const [key, value] of this.symbols) {
187
+ console.log(value.type.padEnd(10, " "), key);
188
+ }
189
+ }
190
+ exists(executionContext, id) {
191
+ const name = this.idName(executionContext, id);
192
+ return this.symbols.has(name);
193
+ }
194
+ existsAny(executionContext, id) {
195
+ if (this.exists(executionContext, id)) {
196
+ return true;
197
+ }
198
+ else {
199
+ return this.searchParentContext(executionContext, id) !== null;
200
+ }
201
+ }
202
+ get(executionContext, id) {
203
+ const name = this.idName(executionContext, id);
204
+ return this.symbols.get(name);
205
+ }
206
+ getParentContexts(executionContext, contextsNamespace) {
207
+ if (executionContext.parentContext !== null) {
208
+ contextsNamespace.push(executionContext.parentContext.namespace);
209
+ this.getParentContexts(executionContext.parentContext, contextsNamespace);
210
+ }
211
+ return contextsNamespace;
212
+ }
213
+ searchParentContext(executionContext, id) {
214
+ const contextNames = this.getParentContexts(executionContext, []);
215
+ for (const [key,] of this.symbols) {
216
+ if (key.endsWith(`.${id}`)) {
217
+ const { context } = this.symbols.get(key);
218
+ if (contextNames.indexOf(context.namespace) !== -1) {
219
+ return context;
220
+ }
221
+ }
222
+ }
223
+ return null;
224
+ }
225
+ clearUndefined() {
226
+ for (const [key, value] of this.symbols) {
227
+ if (value.type === types_js_1.ParseSymbolType.Undefined) {
228
+ this.symbols.delete(key);
229
+ }
230
+ }
231
+ }
232
+ }
233
+ exports.SymbolTable = SymbolTable;