circuitscript 0.1.28 → 0.1.31
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 +155 -17
- package/dist/cjs/SemanticTokenVisitor.js +6 -0
- package/dist/cjs/antlr/CircuitScriptLexer.js +241 -236
- package/dist/cjs/antlr/CircuitScriptParser.js +568 -431
- package/dist/cjs/builtinMethods.js +6 -2
- package/dist/cjs/environment.js +21 -0
- package/dist/cjs/execute.js +90 -59
- package/dist/cjs/globals.js +4 -1
- package/dist/cjs/helpers.js +9 -4
- package/dist/cjs/objects/ExecutionScope.js +9 -0
- package/dist/cjs/objects/types.js +21 -2
- package/dist/cjs/parser.js +6 -2
- package/dist/cjs/validate/SymbolTable.js +7 -1
- package/dist/cjs/validate/SymbolValidatorVisitor.js +54 -7
- package/dist/cjs/visitor.js +15 -47
- package/dist/esm/BaseVisitor.js +157 -19
- package/dist/esm/SemanticTokenVisitor.js +6 -0
- package/dist/esm/antlr/CircuitScriptLexer.js +241 -236
- package/dist/esm/antlr/CircuitScriptParser.js +567 -429
- package/dist/esm/antlr/CircuitScriptVisitor.js +3 -1
- package/dist/esm/builtinMethods.js +7 -3
- package/dist/esm/environment.js +21 -0
- package/dist/esm/execute.js +91 -60
- package/dist/esm/globals.js +2 -0
- package/dist/esm/helpers.js +9 -4
- package/dist/esm/objects/ExecutionScope.js +9 -0
- package/dist/esm/objects/types.js +27 -1
- package/dist/esm/parser.js +6 -2
- package/dist/esm/validate/SymbolTable.js +5 -0
- package/dist/esm/validate/SymbolValidatorVisitor.js +53 -6
- package/dist/esm/visitor.js +16 -45
- package/dist/types/BaseVisitor.d.ts +12 -5
- package/dist/types/SemanticTokenVisitor.d.ts +2 -1
- package/dist/types/antlr/CircuitScriptLexer.d.ts +43 -42
- package/dist/types/antlr/CircuitScriptParser.d.ts +71 -45
- package/dist/types/antlr/CircuitScriptVisitor.d.ts +6 -2
- package/dist/types/environment.d.ts +7 -0
- package/dist/types/execute.d.ts +2 -2
- package/dist/types/globals.d.ts +2 -0
- package/dist/types/objects/ExecutionScope.d.ts +3 -1
- package/dist/types/objects/types.d.ts +22 -1
- package/dist/types/validate/SymbolTable.d.ts +1 -0
- package/dist/types/validate/SymbolValidatorVisitor.d.ts +6 -2
- package/dist/types/visitor.d.ts +3 -1
- package/package.json +4 -1
|
@@ -8,6 +8,7 @@ const big_js_1 = __importDefault(require("big.js"));
|
|
|
8
8
|
const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
|
|
9
9
|
const types_js_1 = require("./objects/types.js");
|
|
10
10
|
const utils_js_1 = require("./utils.js");
|
|
11
|
+
const globals_js_1 = require("./globals.js");
|
|
11
12
|
const builtInMethods = [
|
|
12
13
|
['enumerate', enumerate],
|
|
13
14
|
['toMils', toMils],
|
|
@@ -20,7 +21,7 @@ const builtInMethods = [
|
|
|
20
21
|
];
|
|
21
22
|
exports.buildInMethodNamesList = builtInMethods.map(item => item[0]);
|
|
22
23
|
function linkBuiltInMethods(context, visitor) {
|
|
23
|
-
context.createFunction('print', (params) => {
|
|
24
|
+
context.createFunction(globals_js_1.BaseNamespace, 'print', (params) => {
|
|
24
25
|
const args = getPositionParams(params);
|
|
25
26
|
const items = args.map(item => {
|
|
26
27
|
return toString((0, utils_js_1.unwrapValue)(item));
|
|
@@ -34,7 +35,7 @@ function linkBuiltInMethods(context, visitor) {
|
|
|
34
35
|
});
|
|
35
36
|
builtInMethods.forEach(([functionName, functionImpl]) => {
|
|
36
37
|
if (functionImpl !== null) {
|
|
37
|
-
context.createFunction(functionName, params => {
|
|
38
|
+
context.createFunction(globals_js_1.BaseNamespace, functionName, params => {
|
|
38
39
|
const args = getPositionParams(params);
|
|
39
40
|
const functionReturn = functionImpl(...args);
|
|
40
41
|
return [visitor, functionReturn];
|
|
@@ -155,6 +156,9 @@ function toString(obj) {
|
|
|
155
156
|
else if (obj instanceof types_js_1.CFunctionEntry) {
|
|
156
157
|
return obj.toString();
|
|
157
158
|
}
|
|
159
|
+
else if (obj instanceof types_js_1.ImportedModule) {
|
|
160
|
+
return `[module: ${obj.moduleName}]`;
|
|
161
|
+
}
|
|
158
162
|
else {
|
|
159
163
|
if (obj === undefined) {
|
|
160
164
|
return 'undefined';
|
package/dist/cjs/environment.js
CHANGED
|
@@ -7,12 +7,14 @@ exports.NodeScriptEnvironment = void 0;
|
|
|
7
7
|
const svg_js_1 = require("@svgdotjs/svg.js");
|
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const crypto_js_1 = __importDefault(require("crypto-js"));
|
|
10
11
|
const globals_js_1 = require("./globals.js");
|
|
11
12
|
const utils_js_1 = require("./utils.js");
|
|
12
13
|
class NodeScriptEnvironment {
|
|
13
14
|
constructor() {
|
|
14
15
|
this.useModuleDirectoryPath = null;
|
|
15
16
|
this.useDefaultLibsPath = null;
|
|
17
|
+
this.currentFile = '';
|
|
16
18
|
this.globalCreateSVGWindow = null;
|
|
17
19
|
this.cachedVersion = null;
|
|
18
20
|
this.supportedFonts = {
|
|
@@ -118,6 +120,22 @@ class NodeScriptEnvironment {
|
|
|
118
120
|
async readFile(path, options) {
|
|
119
121
|
return fs_1.default.promises.readFile(path, options);
|
|
120
122
|
}
|
|
123
|
+
getAbsolutePath(filePath) {
|
|
124
|
+
return path_1.default.resolve(filePath);
|
|
125
|
+
}
|
|
126
|
+
getDirPath(filePath) {
|
|
127
|
+
return path_1.default.dirname(path_1.default.resolve(filePath));
|
|
128
|
+
}
|
|
129
|
+
setCurrentFile(filePath) {
|
|
130
|
+
this.currentFile = this.getAbsolutePath(filePath);
|
|
131
|
+
return this.currentFile;
|
|
132
|
+
}
|
|
133
|
+
getCurrentFile() {
|
|
134
|
+
return this.currentFile;
|
|
135
|
+
}
|
|
136
|
+
getRelativeToCurrentFolder(filePath) {
|
|
137
|
+
return path_1.default.join(this.getDirPath(this.currentFile), filePath);
|
|
138
|
+
}
|
|
121
139
|
async exists(path) {
|
|
122
140
|
try {
|
|
123
141
|
fs_1.default.promises.access(path, fs_1.default.constants.F_OK);
|
|
@@ -127,6 +145,9 @@ class NodeScriptEnvironment {
|
|
|
127
145
|
return false;
|
|
128
146
|
}
|
|
129
147
|
}
|
|
148
|
+
hashStringSHA256(value) {
|
|
149
|
+
return crypto_js_1.default.SHA256(value).toString();
|
|
150
|
+
}
|
|
130
151
|
}
|
|
131
152
|
exports.NodeScriptEnvironment = NodeScriptEnvironment;
|
|
132
153
|
NodeScriptEnvironment._instance = null;
|
package/dist/cjs/execute.js
CHANGED
|
@@ -34,7 +34,7 @@ class ExecutionContext {
|
|
|
34
34
|
this.scope.scopeLevel = scopeLevel;
|
|
35
35
|
this.setupRoot();
|
|
36
36
|
this.silent = silent;
|
|
37
|
-
this.log(
|
|
37
|
+
this.log(`create new execution context, namespace: ${this.namespace}, name: ${this.name}, level: ${this.scope.scopeLevel}`);
|
|
38
38
|
this.parentContext = parent;
|
|
39
39
|
this.warnings = warnings;
|
|
40
40
|
}
|
|
@@ -175,8 +175,8 @@ class ExecutionContext {
|
|
|
175
175
|
paramsMap.set(param.paramName, param.paramValue);
|
|
176
176
|
});
|
|
177
177
|
if (component.typeProp === globals_js_1.ComponentTypes.net) {
|
|
178
|
-
const netName = paramsMap.get(globals_js_1.ParamKeys.net_name);
|
|
179
|
-
const netType = paramsMap.get(globals_js_1.ParamKeys.net_type);
|
|
178
|
+
const netName = paramsMap.get(globals_js_1.ParamKeys.net_name) ?? this.getUniqueNetName();
|
|
179
|
+
const netType = paramsMap.get(globals_js_1.ParamKeys.net_type) ?? types_js_1.NetTypes.Any;
|
|
180
180
|
let priority = 0;
|
|
181
181
|
if (paramsMap.has(globals_js_1.ParamKeys.priority)) {
|
|
182
182
|
priority = paramsMap.get(globals_js_1.ParamKeys.priority).toNumber();
|
|
@@ -586,10 +586,11 @@ class ExecutionContext {
|
|
|
586
586
|
getBreakContext() {
|
|
587
587
|
return this.scope.breakStack[this.scope.breakStack.length - 1];
|
|
588
588
|
}
|
|
589
|
-
createFunction(functionName, __runFunc, source, uniqueId) {
|
|
590
|
-
|
|
591
|
-
this.
|
|
592
|
-
this.
|
|
589
|
+
createFunction(namespace, functionName, __runFunc, source, uniqueId) {
|
|
590
|
+
const functionPath = `${namespace}${functionName}`;
|
|
591
|
+
this.scope.functions.set(functionPath, new types_js_1.CFunctionEntry(namespace, functionName, __runFunc, source, uniqueId));
|
|
592
|
+
this.__functionCache.set(functionPath, __runFunc);
|
|
593
|
+
this.log(`defined new function: ${functionPath}`);
|
|
593
594
|
}
|
|
594
595
|
hasFunction(functionName) {
|
|
595
596
|
return this.scope.functions.has(functionName);
|
|
@@ -598,32 +599,63 @@ class ExecutionContext {
|
|
|
598
599
|
return this.scope.functions.get(functionName);
|
|
599
600
|
}
|
|
600
601
|
resolveVariable(executionStack, idName, trailers = []) {
|
|
602
|
+
this.log('resolve variable name:', idName, 'trailers:', trailers);
|
|
601
603
|
const reversed = [...executionStack].reverse();
|
|
602
604
|
for (let i = 0; i < reversed.length; i++) {
|
|
603
605
|
const context = reversed[i];
|
|
604
|
-
|
|
606
|
+
const functionPath = `${context.namespace}${idName}`;
|
|
607
|
+
if (context.hasFunction(functionPath)) {
|
|
605
608
|
return new types_js_1.DeclaredReference({
|
|
606
609
|
found: true,
|
|
607
|
-
value: context.getFunction(
|
|
610
|
+
value: context.getFunction(functionPath),
|
|
608
611
|
type: globals_js_1.ReferenceTypes.function,
|
|
609
612
|
name: idName,
|
|
610
613
|
});
|
|
611
614
|
}
|
|
612
615
|
else {
|
|
616
|
+
const modules = Array.from(context.scope.modules.values());
|
|
617
|
+
for (let j = 0; j < modules.length; j++) {
|
|
618
|
+
const module = modules[j];
|
|
619
|
+
if (module.importHandlingFlag === types_js_1.ImportFunctionHandling.AllMergeIntoNamespace ||
|
|
620
|
+
(module.importHandlingFlag === types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace
|
|
621
|
+
&& module.specifiedImports.indexOf(idName) !== -1)) {
|
|
622
|
+
const moduleContext = module.context;
|
|
623
|
+
const functionPath = `${moduleContext.namespace}${idName}`;
|
|
624
|
+
if (module.context.hasFunction(functionPath)) {
|
|
625
|
+
return new types_js_1.DeclaredReference({
|
|
626
|
+
found: true,
|
|
627
|
+
rootValue: module,
|
|
628
|
+
value: module.context.getFunction(functionPath),
|
|
629
|
+
type: globals_js_1.ReferenceTypes.function,
|
|
630
|
+
name: idName,
|
|
631
|
+
trailerIndex: 1,
|
|
632
|
+
trailers: [idName],
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
let isModule = false;
|
|
638
|
+
if (context.scope.modules.has(idName)) {
|
|
639
|
+
const module = context.scope.modules.get(idName);
|
|
640
|
+
if (module.importHandlingFlag === types_js_1.ImportFunctionHandling.AllWithNamespace) {
|
|
641
|
+
isModule = true;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
613
644
|
let isVariable = context.scope.variables.has(idName);
|
|
614
645
|
let isComponentInstance = context.scope.instances.has(idName);
|
|
615
|
-
if (isVariable || isComponentInstance) {
|
|
616
|
-
const scopeList =
|
|
617
|
-
: context.scope.instances;
|
|
646
|
+
if (isModule || isVariable || isComponentInstance) {
|
|
647
|
+
const scopeList = isModule ? context.scope.modules :
|
|
648
|
+
(isVariable ? context.scope.variables : context.scope.instances);
|
|
618
649
|
const useValue = scopeList.get(idName);
|
|
619
650
|
if (!isComponentInstance && (useValue instanceof ClassComponent_js_1.ClassComponent)) {
|
|
620
651
|
isComponentInstance = true;
|
|
621
652
|
isVariable = false;
|
|
622
653
|
}
|
|
623
|
-
const
|
|
654
|
+
const referenceType = isModule ? globals_js_1.ReferenceTypes.module :
|
|
655
|
+
(isVariable ? globals_js_1.ReferenceTypes.variable : globals_js_1.ReferenceTypes.instance);
|
|
656
|
+
const tmpReference = this.resolveTrailers(referenceType, useValue, trailers);
|
|
624
657
|
return new types_js_1.DeclaredReference({
|
|
625
|
-
type:
|
|
626
|
-
: globals_js_1.ReferenceTypes.instance,
|
|
658
|
+
type: referenceType,
|
|
627
659
|
found: (tmpReference.value !== undefined),
|
|
628
660
|
rootValue: tmpReference.rootValue,
|
|
629
661
|
value: tmpReference.value,
|
|
@@ -644,65 +676,63 @@ class ExecutionContext {
|
|
|
644
676
|
if (trailers.length > 0) {
|
|
645
677
|
rootValue = useValue;
|
|
646
678
|
const trailersPath = trailers.join(".");
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
679
|
+
switch (type) {
|
|
680
|
+
case globals_js_1.ReferenceTypes.variable:
|
|
681
|
+
useValue = rootValue;
|
|
682
|
+
trailers.forEach(trailerPath => {
|
|
683
|
+
useValue = useValue[trailerPath];
|
|
684
|
+
});
|
|
685
|
+
break;
|
|
686
|
+
case globals_js_1.ReferenceTypes.instance: {
|
|
687
|
+
const tmpComponent = rootValue;
|
|
688
|
+
if (tmpComponent.typeProp === globals_js_1.ComponentTypes.net) {
|
|
689
|
+
const usedNet = this.scope.getNet(tmpComponent, new PinDefinition_js_1.PinId(1));
|
|
690
|
+
if (usedNet) {
|
|
691
|
+
const trailerValue = trailers.join(".");
|
|
692
|
+
useValue = usedNet.params.get(trailerValue) ?? null;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
else {
|
|
696
|
+
useValue = rootValue
|
|
697
|
+
.parameters.get(trailersPath);
|
|
660
698
|
}
|
|
699
|
+
break;
|
|
661
700
|
}
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
701
|
+
case globals_js_1.ReferenceTypes.module: {
|
|
702
|
+
const funcName = trailers[0];
|
|
703
|
+
const module = rootValue;
|
|
704
|
+
const functionPath = `${module.moduleNamespace}${funcName}`;
|
|
705
|
+
if (module.context.hasFunction(functionPath)) {
|
|
706
|
+
const foundFunc = module.context.getFunction(functionPath);
|
|
707
|
+
return new types_js_1.AnyReference({
|
|
708
|
+
found: true,
|
|
709
|
+
type: globals_js_1.ReferenceTypes.function,
|
|
710
|
+
rootValue,
|
|
711
|
+
trailers,
|
|
712
|
+
trailerIndex: trailers.length,
|
|
713
|
+
value: foundFunc,
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
break;
|
|
665
717
|
}
|
|
666
718
|
}
|
|
667
719
|
}
|
|
668
720
|
let found = false;
|
|
669
|
-
if (
|
|
721
|
+
if (useValue !== undefined) {
|
|
670
722
|
found = true;
|
|
671
723
|
}
|
|
672
724
|
return new types_js_1.AnyReference({
|
|
673
725
|
found,
|
|
674
|
-
type
|
|
726
|
+
type,
|
|
675
727
|
rootValue,
|
|
676
728
|
trailers,
|
|
677
729
|
trailerIndex: trailers.length,
|
|
678
730
|
value: useValue,
|
|
679
731
|
});
|
|
680
732
|
}
|
|
681
|
-
callFunction(
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
if (this.hasFunction(functionName)) {
|
|
685
|
-
const entry = this.getFunction(functionName);
|
|
686
|
-
__runFunc = entry.execute;
|
|
687
|
-
}
|
|
688
|
-
if (__runFunc === null) {
|
|
689
|
-
this.log(`searching for function ${functionName} in upper context`);
|
|
690
|
-
const tmpResolveResult = this.resolveVariable(executionStack, functionName);
|
|
691
|
-
if (tmpResolveResult.found) {
|
|
692
|
-
const entry = tmpResolveResult.value;
|
|
693
|
-
__runFunc = entry.execute;
|
|
694
|
-
}
|
|
695
|
-
else {
|
|
696
|
-
throw `Invalid function ${functionName}`;
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
this.log('save function to cache:', functionName);
|
|
700
|
-
this.__functionCache.set(functionName, __runFunc);
|
|
701
|
-
}
|
|
702
|
-
else {
|
|
703
|
-
this.log('found function in cache:', functionName);
|
|
704
|
-
__runFunc = this.__functionCache.get(functionName);
|
|
705
|
-
}
|
|
733
|
+
callFunction(functionReference, functionParams, executionStack, netNamespace) {
|
|
734
|
+
const functionEntry = functionReference.value;
|
|
735
|
+
const { name: functionName, execute: __runFunc } = functionEntry;
|
|
706
736
|
if (__runFunc !== null) {
|
|
707
737
|
let functionCallIndex = -1;
|
|
708
738
|
if (!this.scope.functionCounter.has(__runFunc)) {
|
|
@@ -720,6 +750,7 @@ class ExecutionContext {
|
|
|
720
750
|
return functionResult;
|
|
721
751
|
}
|
|
722
752
|
else {
|
|
753
|
+
this.log(`Invalid function: ${functionName}`);
|
|
723
754
|
throw `Invalid function '${functionName}'`;
|
|
724
755
|
}
|
|
725
756
|
}
|
|
@@ -730,7 +761,7 @@ class ExecutionContext {
|
|
|
730
761
|
const tmpNets = childScope.getNets();
|
|
731
762
|
const mergedInstances = [];
|
|
732
763
|
for (const [instanceName, component] of tmpInstances) {
|
|
733
|
-
const newInstanceName = `${namespace}.${instanceName}
|
|
764
|
+
const newInstanceName = namespace !== "" ? `${namespace}.${instanceName}` : instanceName;
|
|
734
765
|
component.instanceName = newInstanceName;
|
|
735
766
|
if (component === childScope.componentRoot) {
|
|
736
767
|
continue;
|
package/dist/cjs/globals.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.SymbolValidatorContext = exports.RenderFlags = exports.GlobalDocumentName = exports.ModuleContainsKeyword = exports.FrameType = exports.NetGraphicsParams = exports.BlockTypes = exports.ReferenceTypes = exports.ComponentTypes = exports.ColorScheme = exports.PortPaddingVertical = exports.PortPaddingHorizontal = exports.PortArrowSize = exports.junctionSize = exports.defaultFontSize = exports.defaultFontBold = exports.defaultFont = exports.displayUnits = exports.defaultFrameTitleTextSize = exports.CustomSymbolParamTextSize = exports.CustomSymbolRefDesSize = exports.CustomSymbolPinIdSize = exports.CustomSymbolPinTextSize = exports.defaultPageSpacingMM = exports.defaultPageMarginMM = exports.defaultPinIdTextSize = exports.defaultPinNameTextSize = exports.defaultWireLineWidth = exports.defaultSymbolLineWidth = exports.fontDisplayScale = exports.defaultZoomScale = exports.defaultGridSizeUnits = exports.portHeight = exports.portWidth = exports.PxToMM = exports.MMToPt = exports.MMToPx = exports.MilsToMM = exports.WireAutoDirection = exports.LengthUnit = exports.ValidPinSides = exports.SymbolPinSide = exports.LayoutDirection = exports.ParamKeys = exports.NoNetText = exports.GlobalNames = exports.BaseNamespace = exports.DoubleDelimiter1 = exports.Delimiter1 = exports.TOOL_VERSION = void 0;
|
|
4
|
+
exports.TrailerArrayIndex = void 0;
|
|
4
5
|
const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
|
|
5
6
|
exports.TOOL_VERSION = '0.1.5';
|
|
6
7
|
exports.Delimiter1 = '-';
|
|
7
8
|
exports.DoubleDelimiter1 = `${exports.Delimiter1}${exports.Delimiter1}`;
|
|
9
|
+
exports.BaseNamespace = `${exports.DoubleDelimiter1}.`;
|
|
8
10
|
var GlobalNames;
|
|
9
11
|
(function (GlobalNames) {
|
|
10
12
|
GlobalNames["__root"] = "--root";
|
|
@@ -96,6 +98,7 @@ var ReferenceTypes;
|
|
|
96
98
|
ReferenceTypes["variable"] = "variable";
|
|
97
99
|
ReferenceTypes["instance"] = "instance";
|
|
98
100
|
ReferenceTypes["pinType"] = "pinType";
|
|
101
|
+
ReferenceTypes["module"] = "module";
|
|
99
102
|
ReferenceTypes["unknown"] = "unknown";
|
|
100
103
|
})(ReferenceTypes || (exports.ReferenceTypes = ReferenceTypes = {}));
|
|
101
104
|
var BlockTypes;
|
package/dist/cjs/helpers.js
CHANGED
|
@@ -187,7 +187,7 @@ async function renderScript(scriptData, outputPath, options) {
|
|
|
187
187
|
}
|
|
188
188
|
exports.renderScript = renderScript;
|
|
189
189
|
async function renderScriptCustom(scriptData, outputPath, options, parseHandlers, postAnnotationCallbacks) {
|
|
190
|
-
const { dumpNets = false, dumpData = false, showStats = false, enableErc = false, enableBom = false, bomOutputPath = undefined, environment } = options;
|
|
190
|
+
const { dumpNets = false, dumpData = false, showStats = false, enableErc = false, enableBom = false, inputPath = '', bomOutputPath = undefined, environment } = options;
|
|
191
191
|
const errors = [];
|
|
192
192
|
const onErrorHandler = (message, context, error) => {
|
|
193
193
|
if (error && error instanceof utils_js_1.RuntimeExecutionError) {
|
|
@@ -214,11 +214,16 @@ async function renderScriptCustom(scriptData, outputPath, options, parseHandlers
|
|
|
214
214
|
errors.push(new utils_js_1.ParseError(message, context.start, context.stop));
|
|
215
215
|
}
|
|
216
216
|
};
|
|
217
|
-
|
|
217
|
+
environment.setCurrentFile(inputPath);
|
|
218
|
+
const visitor = new visitor_js_1.ParserVisitor(true, onErrorHandler, environment);
|
|
218
219
|
visitor.onImportFile = async (visitor, filePath, fileData) => {
|
|
219
|
-
const { hasError, hasParseError } = await (0, parser_js_1.parseFileWithVisitor)(visitor, fileData);
|
|
220
|
+
const { hasError, hasParseError, throwError } = await (0, parser_js_1.parseFileWithVisitor)(visitor, fileData);
|
|
220
221
|
if (hasError || hasParseError) {
|
|
221
|
-
|
|
222
|
+
let importErrorMsg = "";
|
|
223
|
+
if (throwError) {
|
|
224
|
+
importErrorMsg = ": " + throwError.message;
|
|
225
|
+
}
|
|
226
|
+
throw new utils_js_1.ParseError(`Error parsing imported file: ${filePath}${importErrorMsg}`, undefined, undefined, filePath);
|
|
222
227
|
}
|
|
223
228
|
return { hasError, hasParseError };
|
|
224
229
|
};
|
|
@@ -12,6 +12,7 @@ class ExecutionScope {
|
|
|
12
12
|
this.functionCounter = new Map();
|
|
13
13
|
this.variables = new Map();
|
|
14
14
|
this.symbols = new Map();
|
|
15
|
+
this.modules = new Map();
|
|
15
16
|
this.blockStack = new Map();
|
|
16
17
|
this.contextStack = [];
|
|
17
18
|
this.onPropertyHandler = [];
|
|
@@ -169,6 +170,14 @@ class ExecutionScope {
|
|
|
169
170
|
getInstances() {
|
|
170
171
|
return Array.from(this.instances.values());
|
|
171
172
|
}
|
|
173
|
+
copyTo(scope) {
|
|
174
|
+
this.functions.forEach((value, key) => {
|
|
175
|
+
scope.functions.set(key, value);
|
|
176
|
+
});
|
|
177
|
+
this.variables.forEach((value, key) => {
|
|
178
|
+
scope.variables.set(key, value);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
172
181
|
}
|
|
173
182
|
exports.ExecutionScope = ExecutionScope;
|
|
174
183
|
ExecutionScope.scopeId = 0;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NetTypes = exports.TypeProps = exports.Direction = exports.ParseSymbolType = exports.DeclaredReference = exports.UndeclaredReference = exports.AnyReference = exports.CFunctionEntry = void 0;
|
|
3
|
+
exports.ImportFunctionHandling = exports.ImportedModule = exports.NetTypes = exports.TypeProps = exports.Direction = exports.ParseSymbolType = exports.DeclaredReference = exports.UndeclaredReference = exports.AnyReference = exports.CFunctionEntry = void 0;
|
|
4
4
|
const globals_js_1 = require("../globals.js");
|
|
5
5
|
const utils_js_1 = require("../utils.js");
|
|
6
6
|
class CFunctionEntry {
|
|
7
|
-
constructor(name, execute, source, uniqueId) {
|
|
7
|
+
constructor(namespace, name, execute, source, uniqueId) {
|
|
8
8
|
this.name = name;
|
|
9
|
+
this.namespace = namespace;
|
|
10
|
+
this.originalNamespace = namespace;
|
|
9
11
|
this.execute = execute;
|
|
10
12
|
this.uniqueId = uniqueId;
|
|
11
13
|
this.source = source;
|
|
@@ -125,3 +127,20 @@ var NetTypes;
|
|
|
125
127
|
NetTypes["Any"] = "any";
|
|
126
128
|
NetTypes["Source"] = "source";
|
|
127
129
|
})(NetTypes || (exports.NetTypes = NetTypes = {}));
|
|
130
|
+
class ImportedModule {
|
|
131
|
+
constructor(moduleName, moduleNamespace, moduleFilePath, context, flag, specifiedImports) {
|
|
132
|
+
this.moduleName = moduleName;
|
|
133
|
+
this.moduleNamespace = moduleNamespace;
|
|
134
|
+
this.moduleFilePath = moduleFilePath;
|
|
135
|
+
this.context = context;
|
|
136
|
+
this.importHandlingFlag = flag;
|
|
137
|
+
this.specifiedImports = specifiedImports;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.ImportedModule = ImportedModule;
|
|
141
|
+
var ImportFunctionHandling;
|
|
142
|
+
(function (ImportFunctionHandling) {
|
|
143
|
+
ImportFunctionHandling["AllWithNamespace"] = "all-with-namespace";
|
|
144
|
+
ImportFunctionHandling["AllMergeIntoNamespace"] = "all-merge-into-namespace";
|
|
145
|
+
ImportFunctionHandling["SpecificMergeIntoNamespace"] = "specific-merge-into-namespace";
|
|
146
|
+
})(ImportFunctionHandling || (exports.ImportFunctionHandling = ImportFunctionHandling = {}));
|
package/dist/cjs/parser.js
CHANGED
|
@@ -22,6 +22,8 @@ async function parseFileWithVisitor(visitor, data) {
|
|
|
22
22
|
parser.addErrorListener(parserErrorListener);
|
|
23
23
|
const tree = parser.script();
|
|
24
24
|
let throwError;
|
|
25
|
+
let hasError = false;
|
|
26
|
+
let hasParseError = false;
|
|
25
27
|
try {
|
|
26
28
|
await visitor.visitAsync(tree);
|
|
27
29
|
}
|
|
@@ -34,13 +36,15 @@ async function parseFileWithVisitor(visitor, data) {
|
|
|
34
36
|
throwError = error;
|
|
35
37
|
}
|
|
36
38
|
}
|
|
39
|
+
hasError = true;
|
|
40
|
+
hasParseError = true;
|
|
37
41
|
}
|
|
38
42
|
const parserTimeTaken = parserTimer.lap();
|
|
39
43
|
return {
|
|
40
44
|
tree, parser,
|
|
41
45
|
tokens,
|
|
42
|
-
hasParseError
|
|
43
|
-
hasError
|
|
46
|
+
hasParseError,
|
|
47
|
+
hasError,
|
|
44
48
|
parserTimeTaken,
|
|
45
49
|
lexerTimeTaken,
|
|
46
50
|
throwError
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SymbolTable = void 0;
|
|
3
|
+
exports.cloneSymbol = exports.SymbolTable = void 0;
|
|
4
4
|
const types_js_1 = require("../objects/types.js");
|
|
5
5
|
class SymbolTable {
|
|
6
6
|
constructor() {
|
|
@@ -94,3 +94,9 @@ class SymbolTable {
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
exports.SymbolTable = SymbolTable;
|
|
97
|
+
function cloneSymbol(symbol) {
|
|
98
|
+
return {
|
|
99
|
+
...symbol
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
exports.cloneSymbol = cloneSymbol;
|
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SymbolValidatorVisitor = void 0;
|
|
4
|
+
const CircuitScriptParser_js_1 = require("../antlr/CircuitScriptParser.js");
|
|
4
5
|
const builtinMethods_js_1 = require("../builtinMethods.js");
|
|
5
6
|
const types_js_1 = require("../objects/types.js");
|
|
6
7
|
const SymbolTable_js_1 = require("./SymbolTable.js");
|
|
8
|
+
const SymbolTable_js_2 = require("./SymbolTable.js");
|
|
7
9
|
const BaseVisitor_js_1 = require("../BaseVisitor.js");
|
|
8
10
|
const globals_js_1 = require("../globals.js");
|
|
9
11
|
class SymbolValidatorVisitor extends BaseVisitor_js_1.BaseVisitor {
|
|
10
12
|
constructor() {
|
|
11
13
|
super(...arguments);
|
|
12
|
-
this.symbolTable = new
|
|
14
|
+
this.symbolTable = new SymbolTable_js_2.SymbolTable();
|
|
13
15
|
this.filePathStack = [];
|
|
14
|
-
this.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
this.visitImport_simple = async (ctx) => {
|
|
17
|
+
await this.importCommon(ctx, types_js_1.ImportFunctionHandling.AllWithNamespace);
|
|
18
|
+
};
|
|
19
|
+
this.visitImport_all_simple = async (ctx) => {
|
|
20
|
+
await this.importCommon(ctx, types_js_1.ImportFunctionHandling.AllMergeIntoNamespace);
|
|
21
|
+
};
|
|
22
|
+
this.visitImport_specific = async (ctx) => {
|
|
23
|
+
await this.importCommon(ctx, types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace);
|
|
20
24
|
};
|
|
21
25
|
this.visitAssignment_expr = (ctx) => {
|
|
22
26
|
const ctxDataExpr = ctx.data_expr();
|
|
@@ -165,6 +169,49 @@ class SymbolValidatorVisitor extends BaseVisitor_js_1.BaseVisitor {
|
|
|
165
169
|
setSymbols(symbolTable) {
|
|
166
170
|
this.symbolTable = symbolTable;
|
|
167
171
|
}
|
|
172
|
+
async importCommon(ctx, handling) {
|
|
173
|
+
const specifiedImports = [];
|
|
174
|
+
if (ctx instanceof CircuitScriptParser_js_1.Import_specificContext) {
|
|
175
|
+
const tmpImports = ctx._funcNames.map(item => {
|
|
176
|
+
return item.text;
|
|
177
|
+
});
|
|
178
|
+
specifiedImports.push(...tmpImports);
|
|
179
|
+
}
|
|
180
|
+
const id = ctx._moduleName.text;
|
|
181
|
+
const { pathExists, importedModule } = await this.handleImportFile(id, handling, true, ctx, specifiedImports);
|
|
182
|
+
if (!pathExists) {
|
|
183
|
+
this.symbolTable.addUndefined(this.getCurrentFile(), this.getExecutor(), id, ctx._moduleName);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
this.applyModuleImports(importedModule);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
applyModuleImports(module) {
|
|
190
|
+
const { importHandlingFlag: importHandling, specifiedImports } = module;
|
|
191
|
+
const addedSymbols = [];
|
|
192
|
+
const executor = this.getExecutor();
|
|
193
|
+
const symbolTable = this.symbolTable.getSymbols();
|
|
194
|
+
symbolTable.forEach((value, key) => {
|
|
195
|
+
if (value.type === types_js_1.ParseSymbolType.Function) {
|
|
196
|
+
const definedSymbol = value;
|
|
197
|
+
if (definedSymbol.fileName === module.moduleFilePath) {
|
|
198
|
+
const addSymbolToNamespace = importHandling === types_js_1.ImportFunctionHandling.AllMergeIntoNamespace
|
|
199
|
+
|| (importHandling === types_js_1.ImportFunctionHandling.SpecificMergeIntoNamespace
|
|
200
|
+
&& specifiedImports.indexOf(definedSymbol.id) !== -1);
|
|
201
|
+
if (addSymbolToNamespace) {
|
|
202
|
+
const funcPath = `${globals_js_1.BaseNamespace}${definedSymbol.id}`;
|
|
203
|
+
const tmpSymbol = (0, SymbolTable_js_1.cloneSymbol)(value);
|
|
204
|
+
tmpSymbol.context = executor;
|
|
205
|
+
addedSymbols.push([funcPath, tmpSymbol]);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
addedSymbols.forEach(item => {
|
|
211
|
+
const [key, value] = item;
|
|
212
|
+
symbolTable.set(key, value);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
168
215
|
getSymbols() {
|
|
169
216
|
return this.symbolTable;
|
|
170
217
|
}
|