circuitscript 0.1.19 → 0.1.22

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 (50) hide show
  1. package/dist/cjs/BaseVisitor.js +22 -0
  2. package/dist/cjs/ComponentAnnotater.js +88 -0
  3. package/dist/cjs/RefdesAnnotationVisitor.js +197 -0
  4. package/dist/cjs/antlr/CircuitScriptLexer.js +202 -197
  5. package/dist/cjs/antlr/CircuitScriptParser.js +964 -831
  6. package/dist/cjs/environment.js +15 -1
  7. package/dist/cjs/execute.js +45 -2
  8. package/dist/cjs/graph.js +23 -2
  9. package/dist/cjs/helpers.js +21 -4
  10. package/dist/cjs/layout.js +3 -0
  11. package/dist/cjs/lexer.js +21 -9
  12. package/dist/cjs/main.js +13 -0
  13. package/dist/cjs/objects/ClassComponent.js +4 -1
  14. package/dist/cjs/objects/ExecutionScope.js +1 -0
  15. package/dist/cjs/parser.js +1 -0
  16. package/dist/cjs/visitor.js +119 -71
  17. package/dist/esm/BaseVisitor.js +22 -0
  18. package/dist/esm/ComponentAnnotater.js +84 -0
  19. package/dist/esm/RefdesAnnotationVisitor.js +196 -0
  20. package/dist/esm/antlr/CircuitScriptLexer.js +202 -197
  21. package/dist/esm/antlr/CircuitScriptParser.js +960 -829
  22. package/dist/esm/antlr/CircuitScriptVisitor.js +2 -0
  23. package/dist/esm/environment.js +15 -1
  24. package/dist/esm/execute.js +45 -2
  25. package/dist/esm/graph.js +23 -2
  26. package/dist/esm/helpers.js +21 -4
  27. package/dist/esm/layout.js +4 -0
  28. package/dist/esm/lexer.js +21 -9
  29. package/dist/esm/main.js +13 -0
  30. package/dist/esm/objects/ClassComponent.js +4 -1
  31. package/dist/esm/objects/ExecutionScope.js +1 -0
  32. package/dist/esm/parser.js +1 -0
  33. package/dist/esm/visitor.js +117 -69
  34. package/dist/types/BaseVisitor.d.ts +3 -0
  35. package/dist/types/ComponentAnnotater.d.ts +16 -0
  36. package/dist/types/RefdesAnnotationVisitor.d.ts +35 -0
  37. package/dist/types/antlr/CircuitScriptLexer.d.ts +15 -14
  38. package/dist/types/antlr/CircuitScriptParser.d.ts +80 -60
  39. package/dist/types/antlr/CircuitScriptVisitor.d.ts +4 -0
  40. package/dist/types/environment.d.ts +1 -0
  41. package/dist/types/execute.d.ts +8 -1
  42. package/dist/types/helpers.d.ts +6 -3
  43. package/dist/types/layout.d.ts +2 -0
  44. package/dist/types/lexer.d.ts +1 -1
  45. package/dist/types/objects/ClassComponent.d.ts +9 -0
  46. package/dist/types/objects/ExecutionScope.d.ts +2 -1
  47. package/dist/types/objects/types.d.ts +1 -0
  48. package/dist/types/parser.d.ts +2 -1
  49. package/dist/types/visitor.d.ts +8 -1
  50. package/package.json +1 -1
@@ -25,6 +25,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
25
25
  this.acceptedDirections = [types_js_1.Direction.Up, types_js_1.Direction.Down,
26
26
  types_js_1.Direction.Right, types_js_1.Direction.Left];
27
27
  this.resultData = new Map;
28
+ this.componentCtxLinks = new Map;
28
29
  this.pinTypesList = [
29
30
  PinTypes_js_1.PinTypes.Any,
30
31
  PinTypes_js_1.PinTypes.Input,
@@ -569,6 +570,27 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
569
570
  getResult(ctx) {
570
571
  return this.resultData.get(ctx);
571
572
  }
573
+ linkComponentToCtx(ctx, instance, creationFlag = true) {
574
+ const scope = this.getScope();
575
+ const indexedStack = [];
576
+ if (scope.breakStack.length > 0) {
577
+ const executor = this.getExecutor();
578
+ scope.breakStack.forEach(stackCtx => {
579
+ const entry = executor.indexedStack.get(stackCtx);
580
+ const { index } = entry;
581
+ indexedStack.push([stackCtx, index]);
582
+ });
583
+ }
584
+ instance.ctxReferences.push({
585
+ ctx,
586
+ indexedStack,
587
+ creationFlag
588
+ });
589
+ this.componentCtxLinks.set(ctx, instance);
590
+ }
591
+ getComponentCtxLinks() {
592
+ return this.componentCtxLinks;
593
+ }
572
594
  visitResult(ctx) {
573
595
  this.visit(ctx);
574
596
  return this.getResult(ctx);
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ComponentAnnotater = void 0;
4
+ const visitor_js_1 = require("./visitor.js");
5
+ class ComponentAnnotater {
6
+ constructor() {
7
+ this.counter = {};
8
+ this.indexedContextPrefix = new Map();
9
+ this.existingRefDes = [];
10
+ for (const key in visitor_js_1.ComponentRefDesPrefixes) {
11
+ this.counter[key] = 1;
12
+ }
13
+ }
14
+ getAnnotation(instance) {
15
+ const type = instance.typeProp ?? 'conn';
16
+ if (this.counter[type] === undefined && type.length <= 2) {
17
+ for (const [, value] of Object.entries(visitor_js_1.ComponentRefDesPrefixes)) {
18
+ if (value === type) {
19
+ throw "Refdes prefix is already in use!";
20
+ }
21
+ }
22
+ if (visitor_js_1.ComponentRefDesPrefixes[type] === undefined) {
23
+ visitor_js_1.ComponentRefDesPrefixes[type] = type;
24
+ this.counter[type] = 1;
25
+ }
26
+ }
27
+ if (visitor_js_1.ComponentRefDesPrefixes[type] === undefined) {
28
+ return null;
29
+ }
30
+ let prefix = '';
31
+ let resultRefdes = '';
32
+ const { ctxReferences } = instance;
33
+ if (ctxReferences.length > 0) {
34
+ const firstReference = ctxReferences[0];
35
+ const { ctx: useCtx, indexedStack, creationFlag } = firstReference;
36
+ if (indexedStack.length > 0 && creationFlag) {
37
+ const indexes = indexedStack.map(item => {
38
+ return item[1] + 1;
39
+ });
40
+ if (!this.indexedContextPrefix.has(useCtx)) {
41
+ if (instance.placeHolderRefDes) {
42
+ prefix = instance.placeHolderRefDes.replaceAll('_', '');
43
+ }
44
+ else {
45
+ const { index: nextIndex, proposedName } = this.getNextRefdesCounter(visitor_js_1.ComponentRefDesPrefixes[type], this.counter[type]);
46
+ this.counter[type] = nextIndex;
47
+ prefix = proposedName;
48
+ }
49
+ this.existingRefDes.push(prefix);
50
+ this.indexedContextPrefix.set(useCtx, prefix);
51
+ }
52
+ const prefixParts = [this.indexedContextPrefix.get(useCtx), ...indexes];
53
+ resultRefdes = prefixParts.join('_');
54
+ if (this.existingRefDes.indexOf(resultRefdes) !== -1) {
55
+ console.log('Warning: Refdes already used:', resultRefdes);
56
+ }
57
+ }
58
+ else {
59
+ const refdesCounter = this.getNextRefdesCounter(visitor_js_1.ComponentRefDesPrefixes[type], this.counter[type]);
60
+ this.counter[type] = refdesCounter.index;
61
+ resultRefdes = refdesCounter.proposedName;
62
+ }
63
+ this.existingRefDes.push(resultRefdes);
64
+ return resultRefdes;
65
+ }
66
+ }
67
+ getNextRefdesCounter(prefix, startingIndex) {
68
+ let attempts = 100;
69
+ let proposedName = "";
70
+ let index = startingIndex;
71
+ while (attempts >= 0) {
72
+ proposedName = prefix + index;
73
+ index++;
74
+ if (this.existingRefDes.indexOf(proposedName) === -1) {
75
+ break;
76
+ }
77
+ attempts--;
78
+ }
79
+ if (attempts === 0) {
80
+ throw "Annotation failed";
81
+ }
82
+ return { index, proposedName };
83
+ }
84
+ trackRefDes(name) {
85
+ this.existingRefDes.push(name);
86
+ }
87
+ }
88
+ exports.ComponentAnnotater = ComponentAnnotater;
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RefdesAnnotationVisitor = void 0;
4
+ const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
5
+ const BaseVisitor_js_1 = require("./BaseVisitor.js");
6
+ class RefdesAnnotationVisitor extends BaseVisitor_js_1.BaseVisitor {
7
+ constructor(silent, sourceText, tokenStream, componentCtxLinks) {
8
+ super(true);
9
+ this.modifications = new Map();
10
+ this.resultText = '';
11
+ this.debug = false;
12
+ this.visitScript = async (ctx) => {
13
+ const result = this.runExpressions(this.getExecutor(), ctx.expression());
14
+ this.setResult(ctx, result);
15
+ this.getExecutor().closeOpenPathBlocks();
16
+ this.resultText = this.generateModifiedText();
17
+ };
18
+ this.visitAdd_component_expr = (ctx) => {
19
+ this.addRefdesAnnotationComment(ctx);
20
+ };
21
+ this.visitAt_component_expr = (ctx) => {
22
+ this.addRefdesAnnotationComment(ctx);
23
+ };
24
+ this.visitComponent_select_expr = (ctx) => {
25
+ this.addRefdesAnnotationComment(ctx);
26
+ };
27
+ this.visitAt_block_header = (ctx) => {
28
+ this.addRefdesAnnotationComment(ctx);
29
+ };
30
+ this.visitAt_block = (ctx) => {
31
+ this.visit(ctx.at_block_header());
32
+ ctx.at_block_expressions().forEach(expression => {
33
+ this.visit(expression);
34
+ });
35
+ };
36
+ this.visitTo_component_expr = (ctx) => {
37
+ const allRefdes = [];
38
+ ctx.component_select_expr().forEach(itemCtx => {
39
+ const tmpRefdes = this.generateRefdesAnnotationComment(itemCtx);
40
+ if (tmpRefdes !== null) {
41
+ allRefdes.push(tmpRefdes);
42
+ }
43
+ });
44
+ if (allRefdes.length > 0) {
45
+ const originalText = this.getOriginalText(ctx);
46
+ const annotation = ' #= ' + allRefdes.join(',');
47
+ this.modifications.set(ctx, originalText + annotation);
48
+ }
49
+ };
50
+ this.visitFunction_def_expr = (ctx) => {
51
+ this.runExpressions(this.getExecutor(), ctx.function_expr());
52
+ };
53
+ this.visitFunction_return_expr = (ctx) => {
54
+ };
55
+ this.visitFrame_expr = (ctx) => {
56
+ this.visit(ctx.expressions_block());
57
+ };
58
+ this.visitFunction_call_expr = (ctx) => {
59
+ };
60
+ this.visitParameters = (ctx) => {
61
+ };
62
+ this.addedRefdesAnnotations = [];
63
+ this.sourceText = sourceText;
64
+ this.tokenStream = tokenStream;
65
+ this.componentCtxLinks = componentCtxLinks;
66
+ }
67
+ getOriginalText(ctx) {
68
+ if (!ctx.start || !ctx.stop) {
69
+ return '';
70
+ }
71
+ const startIndex = ctx.start.start;
72
+ const stopIndex = ctx.stop.stop;
73
+ return this.sourceText.substring(startIndex, stopIndex + 1);
74
+ }
75
+ generateRefdesAnnotationComment(ctx) {
76
+ if (this.componentCtxLinks.has(ctx)) {
77
+ const instance = this.componentCtxLinks.get(ctx);
78
+ const alreadyHaveRefdesAnnotation = instance.assignedRefDes !== null ?
79
+ (this.addedRefdesAnnotations.indexOf(instance.assignedRefDes) !== -1) : false;
80
+ if (!instance.hasParam('refdes')
81
+ && instance.placeHolderRefDes === null
82
+ && instance.assignedRefDes
83
+ && !alreadyHaveRefdesAnnotation) {
84
+ let useRefDes = instance.assignedRefDes;
85
+ let isPlaceholderRefdes = false;
86
+ const { ctxReferences } = instance;
87
+ if (ctxReferences.length > 0) {
88
+ const firstReference = ctxReferences[0];
89
+ const { indexedStack: loopStack = [] } = firstReference;
90
+ if (loopStack.length > 0) {
91
+ isPlaceholderRefdes = true;
92
+ const parts = instance.assignedRefDes.split('_');
93
+ useRefDes = parts[0] + '_';
94
+ }
95
+ if (this.addedRefdesAnnotations.indexOf(instance.assignedRefDes) === -1
96
+ && !isPlaceholderRefdes) {
97
+ this.addedRefdesAnnotations.push(instance.assignedRefDes);
98
+ }
99
+ return useRefDes;
100
+ }
101
+ }
102
+ }
103
+ return null;
104
+ }
105
+ addRefdesAnnotationComment(ctx) {
106
+ const refdes = this.generateRefdesAnnotationComment(ctx);
107
+ if (refdes !== null) {
108
+ const originalText = this.getOriginalText(ctx);
109
+ const annotation = ' #= ' + refdes;
110
+ this.modifications.set(ctx, originalText + annotation);
111
+ }
112
+ }
113
+ getOutput() {
114
+ return this.resultText;
115
+ }
116
+ generateModifiedText() {
117
+ const output = [];
118
+ const allTokens = this.tokenStream.getTokens();
119
+ const processedTokens = new Set();
120
+ let lastSourcePos = 0;
121
+ const contextTokenRanges = this.buildContextTokenRanges();
122
+ for (let i = 0; i < allTokens.length; i++) {
123
+ const token = allTokens[i];
124
+ if (processedTokens.has(i)) {
125
+ continue;
126
+ }
127
+ const tokenText = this.sourceText.substring(token.start, token.stop + 1);
128
+ this.log(i, `token: [${tokenText}], length: ${tokenText.length}, text: [${token.text}]`);
129
+ if (token.type === CircuitScriptParser_js_1.CircuitScriptParser.DEDENT
130
+ || token.type === CircuitScriptParser_js_1.CircuitScriptParser.EOF
131
+ || (token.type === CircuitScriptParser_js_1.CircuitScriptParser.NEWLINE && token.__skip)) {
132
+ this.log('--skip dedent/EOF token');
133
+ continue;
134
+ }
135
+ const ctx = this.findContextForToken(token, contextTokenRanges);
136
+ if (ctx) {
137
+ const isFirstTokenInContext = token.tokenIndex === ctx.start?.tokenIndex;
138
+ if (isFirstTokenInContext) {
139
+ if (token.start > lastSourcePos) {
140
+ output.push(this.sourceText.substring(lastSourcePos, token.start));
141
+ }
142
+ if (this.modifications.has(ctx)) {
143
+ output.push(this.modifications.get(ctx));
144
+ this.markTokensAsProcessed(ctx, processedTokens);
145
+ if (ctx.stop) {
146
+ lastSourcePos = ctx.stop.stop + 1;
147
+ }
148
+ continue;
149
+ }
150
+ }
151
+ }
152
+ if (token.start > lastSourcePos) {
153
+ output.push(this.sourceText.substring(lastSourcePos, token.start));
154
+ }
155
+ if (tokenText.length > 0) {
156
+ output.push(tokenText);
157
+ }
158
+ processedTokens.add(i);
159
+ lastSourcePos = token.stop + 1;
160
+ }
161
+ if (lastSourcePos < this.sourceText.length) {
162
+ output.push(this.sourceText.substring(lastSourcePos));
163
+ }
164
+ return output.join('');
165
+ }
166
+ buildContextTokenRanges() {
167
+ const ranges = new Map();
168
+ for (const ctx of this.modifications.keys()) {
169
+ if (ctx.start && ctx.stop) {
170
+ ranges.set(ctx, [ctx.start.tokenIndex, ctx.stop.tokenIndex]);
171
+ }
172
+ }
173
+ return ranges;
174
+ }
175
+ findContextForToken(token, contextRanges) {
176
+ for (const [ctx, [start, end]] of contextRanges) {
177
+ if (token.tokenIndex >= start && token.tokenIndex <= end) {
178
+ return ctx;
179
+ }
180
+ }
181
+ return null;
182
+ }
183
+ markTokensAsProcessed(ctx, processedTokens) {
184
+ if (!ctx.start || !ctx.stop) {
185
+ return;
186
+ }
187
+ for (let i = ctx.start.tokenIndex; i <= ctx.stop.tokenIndex; i++) {
188
+ processedTokens.add(i);
189
+ }
190
+ }
191
+ log(...message) {
192
+ if (this.debug) {
193
+ console.log(...message);
194
+ }
195
+ }
196
+ }
197
+ exports.RefdesAnnotationVisitor = RefdesAnnotationVisitor;