circuitscript 0.0.22 → 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.
- package/dist/cjs/BaseVisitor.js +487 -0
- package/dist/cjs/SemanticTokenVisitor.js +218 -0
- package/dist/cjs/SymbolValidatorVisitor.js +233 -0
- package/dist/cjs/antlr/CircuitScriptLexer.js +302 -0
- package/dist/cjs/antlr/CircuitScriptParser.js +5128 -0
- package/dist/cjs/antlr/CircuitScriptVisitor.js +7 -0
- package/dist/cjs/draw_symbols.js +819 -0
- package/dist/cjs/execute.js +778 -0
- package/{src/export.ts → dist/cjs/export.js} +34 -56
- package/dist/cjs/fonts.js +4 -0
- package/dist/cjs/geometry.js +450 -0
- package/dist/cjs/globals.js +60 -0
- package/dist/cjs/helpers.js +269 -0
- package/dist/cjs/index.js +31 -0
- package/{src/layout.ts → dist/cjs/layout.js} +421 -1002
- package/dist/cjs/lexer.js +111 -0
- package/dist/cjs/logger.js +17 -0
- package/dist/cjs/main.js +82 -0
- package/dist/cjs/objects/ClassComponent.js +145 -0
- package/dist/cjs/objects/ExecutionScope.js +135 -0
- package/dist/cjs/objects/Frame.js +22 -0
- package/{src/objects/Net.ts → dist/cjs/objects/Net.js} +9 -24
- package/dist/cjs/objects/ParamDefinition.js +42 -0
- package/dist/cjs/objects/PinDefinition.js +31 -0
- package/dist/cjs/objects/PinTypes.js +11 -0
- package/dist/cjs/objects/Wire.js +9 -0
- package/dist/cjs/objects/types.js +15 -0
- package/dist/cjs/parser.js +70 -0
- package/dist/cjs/regenerate-tests.js +23 -0
- package/dist/cjs/render.js +155 -0
- package/{src/server.ts → dist/cjs/server.js} +15 -21
- package/dist/cjs/sizing.js +105 -0
- package/{src/utils.ts → dist/cjs/utils.js} +25 -35
- package/dist/cjs/validate.js +81 -0
- package/dist/cjs/visitor.js +844 -0
- package/dist/esm/BaseVisitor.mjs +488 -0
- package/dist/esm/SemanticTokenVisitor.mjs +215 -0
- package/dist/esm/SymbolValidatorVisitor.mjs +222 -0
- package/dist/esm/antlr/CircuitScriptLexer.mjs +276 -0
- package/dist/esm/antlr/CircuitScriptParser.mjs +5038 -0
- package/{build/src/antlr/CircuitScriptVisitor.js → dist/esm/antlr/CircuitScriptVisitor.mjs} +8 -3
- package/{build/src/draw_symbols.js → dist/esm/draw_symbols.mjs} +78 -33
- package/{build/src/execute.js → dist/esm/execute.mjs} +59 -60
- package/{build/src/export.js → dist/esm/export.mjs} +2 -2
- package/{build/src/geometry.js → dist/esm/geometry.mjs} +31 -15
- package/dist/esm/helpers.mjs +252 -0
- package/dist/esm/index.mjs +15 -0
- package/{build/src/layout.js → dist/esm/layout.mjs} +19 -11
- package/{build/src/lexer.js → dist/esm/lexer.mjs} +10 -10
- package/{build/src/main.js → dist/esm/main.mjs} +9 -14
- package/{build/src/objects/ClassComponent.js → dist/esm/objects/ClassComponent.mjs} +6 -3
- package/{build/src/objects/ExecutionScope.js → dist/esm/objects/ExecutionScope.mjs} +1 -0
- package/{build/src/objects/PinDefinition.js → dist/esm/objects/PinDefinition.mjs} +1 -1
- package/dist/esm/objects/types.mjs +12 -0
- package/dist/esm/parser.mjs +64 -0
- package/{build/src/regenerate-tests.js → dist/esm/regenerate-tests.mjs} +1 -1
- package/{build/src/render.js → dist/esm/render.mjs} +7 -24
- package/{build/src/sizing.js → dist/esm/sizing.mjs} +22 -8
- package/{src/main.ts → dist/esm/validate.mjs} +31 -62
- package/dist/esm/visitor.mjs +838 -0
- package/dist/types/BaseVisitor.d.ts +69 -0
- package/dist/types/SemanticTokenVisitor.d.ts +36 -0
- package/dist/types/SymbolValidatorVisitor.d.ts +61 -0
- package/{build/src → dist/types}/antlr/CircuitScriptLexer.d.ts +28 -27
- package/dist/types/antlr/CircuitScriptParser.d.ts +719 -0
- package/{build/src → dist/types}/antlr/CircuitScriptVisitor.d.ts +69 -59
- package/{build/src → dist/types}/draw_symbols.d.ts +11 -2
- package/{build/src → dist/types}/execute.d.ts +6 -9
- package/{build/src → dist/types}/geometry.d.ts +5 -1
- package/dist/types/helpers.d.ts +40 -0
- package/dist/types/index.d.ts +15 -0
- package/{build/src → dist/types}/layout.d.ts +10 -10
- package/{build/src → dist/types}/lexer.d.ts +2 -2
- package/{build/src → dist/types}/objects/ClassComponent.d.ts +2 -2
- package/{build/src → dist/types}/objects/ExecutionScope.d.ts +4 -1
- package/{build/src → dist/types}/objects/PinDefinition.d.ts +1 -1
- package/{build/src → dist/types}/objects/types.d.ts +5 -0
- package/dist/types/parser.d.ts +25 -0
- package/{build/src → dist/types}/render.d.ts +1 -1
- package/{build/src → dist/types}/sizing.d.ts +3 -1
- package/dist/types/validate.d.ts +2 -0
- package/dist/types/visitor.d.ts +80 -0
- package/libs/lib.cst +0 -2
- package/package.json +38 -15
- package/.editorconfig +0 -15
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -27
- package/.gitlab-ci.yml +0 -81
- package/.prettierignore +0 -8
- package/.prettierrc +0 -16
- package/__tests__/expectedResults.ts +0 -657
- package/__tests__/helpers.ts +0 -82
- package/__tests__/parseScripts.ts +0 -593
- package/__tests__/renderData/script1.cst +0 -58
- package/__tests__/renderData/script1.cst.svg +0 -1
- package/__tests__/renderData/script2.cst +0 -16
- package/__tests__/renderData/script2.cst.svg +0 -1
- package/__tests__/renderData/script3.cst +0 -30
- package/__tests__/renderData/script3.cst.svg +0 -1
- package/__tests__/renderData/script4.cst +0 -54
- package/__tests__/renderData/script4.cst.svg +0 -1
- package/__tests__/renderData/script5.cst +0 -23
- package/__tests__/renderData/script5.cst.svg +0 -1
- package/__tests__/renderData/script6.cst +0 -28
- package/__tests__/renderData/script6.cst.svg +0 -1
- package/__tests__/renderData/script7.cst +0 -26
- package/__tests__/renderData/script7.cst.svg +0 -1
- package/__tests__/renderData/script8.cst +0 -37
- package/__tests__/renderData/script8.cst.svg +0 -1
- package/__tests__/testCLI.ts +0 -68
- package/__tests__/testMathOps.ts +0 -36
- package/__tests__/testMergeWires.ts +0 -141
- package/__tests__/testParse.ts +0 -263
- package/__tests__/testRender.ts +0 -38
- package/build/src/antlr/CircuitScriptLexer.js +0 -287
- package/build/src/antlr/CircuitScriptParser.d.ts +0 -674
- package/build/src/antlr/CircuitScriptParser.js +0 -4841
- package/build/src/helpers.d.ts +0 -1
- package/build/src/helpers.js +0 -73
- package/build/src/objects/types.js +0 -6
- package/build/src/parser.js +0 -69
- package/build/src/visitor.d.ts +0 -133
- package/build/src/visitor.js +0 -1154
- package/documentation.md +0 -238
- package/examples/example_arduino_uno.cst +0 -1146
- package/examples/example_garden_pump.cst +0 -567
- package/examples/lib.cst +0 -185
- package/jest.config.js +0 -23
- package/refresh.html +0 -42
- package/server.cjs +0 -50
- package/src/antlr/CircuitScript.g4 +0 -209
- package/src/antlr/CircuitScriptLexer.ts +0 -317
- package/src/antlr/CircuitScriptParser.ts +0 -4979
- package/src/antlr/CircuitScriptVisitor.ts +0 -420
- package/src/draw_symbols.ts +0 -1085
- package/src/execute.ts +0 -1227
- package/src/fonts.ts +0 -1
- package/src/geometry.ts +0 -638
- package/src/globals.ts +0 -67
- package/src/helpers.ts +0 -114
- package/src/lexer.ts +0 -151
- package/src/logger.ts +0 -17
- package/src/objects/ClassComponent.ts +0 -223
- package/src/objects/ExecutionScope.ts +0 -201
- package/src/objects/Frame.ts +0 -20
- package/src/objects/ParamDefinition.ts +0 -49
- package/src/objects/PinDefinition.ts +0 -49
- package/src/objects/PinTypes.ts +0 -7
- package/src/objects/Wire.ts +0 -19
- package/src/objects/types.ts +0 -66
- package/src/parser.ts +0 -106
- package/src/regenerate-tests.ts +0 -25
- package/src/render.ts +0 -260
- package/src/sizing.ts +0 -96
- package/src/visitor.ts +0 -1691
- package/tsconfig.json +0 -27
- package/tsconfig.release.json +0 -8
- /package/{build/src/fonts.js → dist/esm/fonts.mjs} +0 -0
- /package/{build/src/globals.js → dist/esm/globals.mjs} +0 -0
- /package/{build/src/logger.js → dist/esm/logger.mjs} +0 -0
- /package/{build/src/objects/Frame.js → dist/esm/objects/Frame.mjs} +0 -0
- /package/{build/src/objects/Net.js → dist/esm/objects/Net.mjs} +0 -0
- /package/{build/src/objects/ParamDefinition.js → dist/esm/objects/ParamDefinition.mjs} +0 -0
- /package/{build/src/objects/PinTypes.js → dist/esm/objects/PinTypes.mjs} +0 -0
- /package/{build/src/objects/Wire.js → dist/esm/objects/Wire.mjs} +0 -0
- /package/{build/src/server.js → dist/esm/server.mjs} +0 -0
- /package/{build/src/utils.js → dist/esm/utils.mjs} +0 -0
- /package/{build/src → dist/types}/export.d.ts +0 -0
- /package/{build/src → dist/types}/fonts.d.ts +0 -0
- /package/{build/src → dist/types}/globals.d.ts +0 -0
- /package/{build/src → dist/types}/logger.d.ts +0 -0
- /package/{build/src → dist/types}/main.d.ts +0 -0
- /package/{build/src → dist/types}/objects/Frame.d.ts +0 -0
- /package/{build/src → dist/types}/objects/Net.d.ts +0 -0
- /package/{build/src → dist/types}/objects/ParamDefinition.d.ts +0 -0
- /package/{build/src → dist/types}/objects/PinTypes.d.ts +0 -0
- /package/{build/src → dist/types}/objects/Wire.d.ts +0 -0
- /package/{build/src → dist/types}/regenerate-tests.d.ts +0 -0
- /package/{build/src → dist/types}/server.d.ts +0 -0
- /package/{build/src → dist/types}/utils.d.ts +0 -0
|
@@ -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;
|