circuitscript 0.1.29 → 0.1.32

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 (71) hide show
  1. package/dist/cjs/BaseVisitor.js +185 -22
  2. package/dist/cjs/RefdesAnnotationVisitor.js +27 -10
  3. package/dist/cjs/antlr/CircuitScriptLexer.js +241 -236
  4. package/dist/cjs/antlr/CircuitScriptParser.js +1197 -901
  5. package/dist/cjs/builtinMethods.js +6 -2
  6. package/dist/cjs/draw_symbols.js +38 -34
  7. package/dist/cjs/environment.js +28 -4
  8. package/dist/cjs/execute.js +195 -125
  9. package/dist/cjs/globals.js +6 -1
  10. package/dist/cjs/graph.js +14 -12
  11. package/dist/cjs/helpers.js +90 -17
  12. package/dist/cjs/layout.js +50 -25
  13. package/dist/cjs/main.js +16 -14
  14. package/dist/cjs/objects/ClassComponent.js +199 -30
  15. package/dist/cjs/objects/ExecutionScope.js +9 -0
  16. package/dist/cjs/objects/types.js +25 -2
  17. package/dist/cjs/parser.js +6 -2
  18. package/dist/cjs/regenerate-tests.js +3 -3
  19. package/dist/cjs/render.js +5 -3
  20. package/dist/cjs/rules-check/no-connect-on-connected-pin.js +9 -8
  21. package/dist/cjs/rules-check/rules.js +7 -2
  22. package/dist/cjs/rules-check/unconnected-pins.js +10 -8
  23. package/dist/cjs/utils.js +2 -1
  24. package/dist/cjs/validate/SymbolTable.js +7 -1
  25. package/dist/cjs/validate/SymbolValidatorVisitor.js +54 -17
  26. package/dist/cjs/visitor.js +299 -238
  27. package/dist/esm/BaseVisitor.js +187 -24
  28. package/dist/esm/RefdesAnnotationVisitor.js +27 -10
  29. package/dist/esm/antlr/CircuitScriptLexer.js +241 -236
  30. package/dist/esm/antlr/CircuitScriptParser.js +1196 -899
  31. package/dist/esm/antlr/CircuitScriptVisitor.js +4 -1
  32. package/dist/esm/builtinMethods.js +7 -3
  33. package/dist/esm/draw_symbols.js +38 -34
  34. package/dist/esm/environment.js +25 -1
  35. package/dist/esm/execute.js +197 -127
  36. package/dist/esm/globals.js +4 -0
  37. package/dist/esm/graph.js +14 -12
  38. package/dist/esm/helpers.js +91 -18
  39. package/dist/esm/layout.js +51 -26
  40. package/dist/esm/main.js +16 -14
  41. package/dist/esm/objects/ClassComponent.js +201 -30
  42. package/dist/esm/objects/ExecutionScope.js +9 -0
  43. package/dist/esm/objects/types.js +33 -1
  44. package/dist/esm/parser.js +6 -2
  45. package/dist/esm/regenerate-tests.js +3 -3
  46. package/dist/esm/render.js +5 -3
  47. package/dist/esm/rules-check/no-connect-on-connected-pin.js +9 -8
  48. package/dist/esm/rules-check/rules.js +7 -2
  49. package/dist/esm/rules-check/unconnected-pins.js +10 -8
  50. package/dist/esm/utils.js +2 -1
  51. package/dist/esm/validate/SymbolTable.js +5 -0
  52. package/dist/esm/validate/SymbolValidatorVisitor.js +53 -16
  53. package/dist/esm/visitor.js +201 -137
  54. package/dist/types/BaseVisitor.d.ts +27 -10
  55. package/dist/types/RefdesAnnotationVisitor.d.ts +2 -0
  56. package/dist/types/antlr/CircuitScriptLexer.d.ts +43 -42
  57. package/dist/types/antlr/CircuitScriptParser.d.ts +102 -58
  58. package/dist/types/antlr/CircuitScriptVisitor.d.ts +8 -2
  59. package/dist/types/environment.d.ts +8 -1
  60. package/dist/types/execute.d.ts +6 -3
  61. package/dist/types/globals.d.ts +4 -0
  62. package/dist/types/graph.d.ts +2 -2
  63. package/dist/types/helpers.d.ts +2 -1
  64. package/dist/types/layout.d.ts +5 -4
  65. package/dist/types/objects/ClassComponent.d.ts +34 -9
  66. package/dist/types/objects/ExecutionScope.d.ts +3 -1
  67. package/dist/types/objects/types.d.ts +40 -3
  68. package/dist/types/validate/SymbolTable.d.ts +1 -0
  69. package/dist/types/validate/SymbolValidatorVisitor.d.ts +6 -6
  70. package/dist/types/visitor.d.ts +10 -2
  71. package/package.json +4 -1
@@ -2,11 +2,15 @@ import { ReferenceTypes } from '../globals.js';
2
2
  import { RuntimeExecutionError } from '../utils.js';
3
3
  export class CFunctionEntry {
4
4
  name;
5
+ namespace;
6
+ originalNamespace;
5
7
  execute;
6
8
  uniqueId;
7
9
  source;
8
- constructor(name, execute, source, uniqueId) {
10
+ constructor(namespace, name, execute, source, uniqueId) {
9
11
  this.name = name;
12
+ this.namespace = namespace;
13
+ this.originalNamespace = namespace;
10
14
  this.execute = execute;
11
15
  this.uniqueId = uniqueId;
12
16
  this.source = source;
@@ -124,3 +128,31 @@ export var NetTypes;
124
128
  NetTypes["Any"] = "any";
125
129
  NetTypes["Source"] = "source";
126
130
  })(NetTypes || (NetTypes = {}));
131
+ export class ImportedModule {
132
+ moduleName;
133
+ context;
134
+ importHandlingFlag;
135
+ specifiedImports;
136
+ moduleNamespace;
137
+ moduleFilePath;
138
+ enableRefdesAnnotation = false;
139
+ enableRefdesAnnotationFile = false;
140
+ tree;
141
+ tokens;
142
+ constructor(moduleName, moduleNamespace, moduleFilePath, tree, tokens, context, flag, specifiedImports) {
143
+ this.moduleName = moduleName;
144
+ this.moduleNamespace = moduleNamespace;
145
+ this.moduleFilePath = moduleFilePath;
146
+ this.tree = tree;
147
+ this.tokens = tokens;
148
+ this.context = context;
149
+ this.importHandlingFlag = flag;
150
+ this.specifiedImports = specifiedImports;
151
+ }
152
+ }
153
+ export var ImportFunctionHandling;
154
+ (function (ImportFunctionHandling) {
155
+ ImportFunctionHandling["AllWithNamespace"] = "all-with-namespace";
156
+ ImportFunctionHandling["AllMergeIntoNamespace"] = "all-merge-into-namespace";
157
+ ImportFunctionHandling["SpecificMergeIntoNamespace"] = "specific-merge-into-namespace";
158
+ })(ImportFunctionHandling || (ImportFunctionHandling = {}));
@@ -19,6 +19,8 @@ export async function parseFileWithVisitor(visitor, data) {
19
19
  parser.addErrorListener(parserErrorListener);
20
20
  const tree = parser.script();
21
21
  let throwError;
22
+ let hasError = false;
23
+ let hasParseError = false;
22
24
  try {
23
25
  await visitor.visitAsync(tree);
24
26
  }
@@ -31,13 +33,15 @@ export async function parseFileWithVisitor(visitor, data) {
31
33
  throwError = error;
32
34
  }
33
35
  }
36
+ hasError = true;
37
+ hasParseError = true;
34
38
  }
35
39
  const parserTimeTaken = parserTimer.lap();
36
40
  return {
37
41
  tree, parser,
38
42
  tokens,
39
- hasParseError: false,
40
- hasError: false,
43
+ hasParseError,
44
+ hasError,
41
45
  parserTimeTaken,
42
46
  lexerTimeTaken,
43
47
  throwError
@@ -1,7 +1,7 @@
1
1
  import fs from 'fs';
2
2
  import { renderScript } from './helpers.js';
3
3
  import { NodeScriptEnvironment } from "./environment.js";
4
- const mainDir = './__tests__/renderData/';
4
+ const mainDir = './__tests__/testData/renderData/';
5
5
  const env = new NodeScriptEnvironment();
6
6
  NodeScriptEnvironment.setInstance(env);
7
7
  async function regenerateTests(extra = "") {
@@ -9,7 +9,7 @@ async function regenerateTests(extra = "") {
9
9
  const cstFiles = [];
10
10
  const files = fs.readdirSync(mainDir);
11
11
  files.forEach(file => {
12
- if (file.endsWith('.cst')) {
12
+ if (file.endsWith('.cst') && file.startsWith('script')) {
13
13
  cstFiles.push(file);
14
14
  }
15
15
  });
@@ -19,7 +19,7 @@ async function regenerateTests(extra = "") {
19
19
  const scriptData = fs.readFileSync(inputPath, { encoding: 'utf-8' });
20
20
  const outputPath = mainDir + 'svgs/' + file + extra + '.svg';
21
21
  env.setModuleDirectory(mainDir);
22
- env.setDefaultLibsPath(mainDir + '../../libs/');
22
+ env.setDefaultLibsPath(mainDir + '../../../libs/');
23
23
  await renderScript(scriptData, outputPath, {
24
24
  dumpNets: false,
25
25
  dumpData: false,
@@ -36,10 +36,11 @@ export function renderSheetsToSVG(sheetFrames, logger) {
36
36
  const frameComponent = sheet.frame.frame.parameters
37
37
  .get(FrameParamKeys.SheetType);
38
38
  if (frameComponent) {
39
- if (frameComponent.displayProp === null) {
39
+ const frameComponentUnit = frameComponent.getUnit();
40
+ if (frameComponentUnit.displayProp === null) {
40
41
  throw 'Invalid graphic object for sheet frame';
41
42
  }
42
- const frameRects = ExtractDrawingRects(frameComponent.displayProp) ?? [];
43
+ const frameRects = ExtractDrawingRects(frameComponentUnit.displayProp) ?? [];
43
44
  let originalWidthMM = numeric(0);
44
45
  let originalHeightMM = numeric(0);
45
46
  let widthMM = numeric(0);
@@ -325,7 +326,8 @@ function drawSheetFrameBorder(frameGroup, frame) {
325
326
  const frameParams = frame.frame.parameters;
326
327
  if (frameParams.has(FrameParamKeys.SheetType)) {
327
328
  const frameComponent = frameParams.get(FrameParamKeys.SheetType);
328
- const { displayProp = null } = frameComponent ?? {};
329
+ const frameComponentUnit = frameComponent.getUnit();
330
+ const { displayProp = null } = frameComponentUnit ?? {};
329
331
  if (displayProp) {
330
332
  const sheetFrameGroup = frameGroup.group();
331
333
  const symbol = new SymbolPlaceholder(displayProp);
@@ -8,25 +8,26 @@ export function RuleCheck_NoConnectOnConnectedPin(graph, nets) {
8
8
  const makeComponentPinHash = (instanceName, pin) => {
9
9
  return instanceName + '-' + pin.getHashValue();
10
10
  };
11
- nets.forEach(item => {
12
- const [component, pin, net] = item;
11
+ for (const [component, pin, net] of nets) {
13
12
  if (!netComponentPins.has(net)) {
14
13
  netComponentPins.set(net, []);
15
14
  }
16
15
  const items = netComponentPins.get(net);
16
+ const unit = component.getUnitForPin(pin);
17
17
  items.push([
18
- component.instanceName,
18
+ unit.instanceName,
19
19
  pin
20
20
  ]);
21
21
  netComponentPins.set(net, items);
22
- pinMapping.set(makeComponentPinHash(component.instanceName, pin), net);
23
- });
24
- allNodes.forEach(node => {
22
+ pinMapping.set(makeComponentPinHash(unit.instanceName, pin), net);
23
+ }
24
+ ;
25
+ for (const node of allNodes) {
25
26
  const nodeInfo = graph.node(node);
26
27
  if (nodeInfo[0] === RenderItemType.Component) {
27
28
  const { component } = nodeInfo[1];
28
29
  if (component.hasParam('no_connect')) {
29
- const instanceName = component.instanceName;
30
+ const instanceName = component.getUnit().instanceName;
30
31
  const edges = graph.nodeEdges(node);
31
32
  const otherNodes = [];
32
33
  edges.forEach(edge => {
@@ -72,6 +73,6 @@ export function RuleCheck_NoConnectOnConnectedPin(graph, nets) {
72
73
  });
73
74
  }
74
75
  }
75
- });
76
+ }
76
77
  return items;
77
78
  }
@@ -22,7 +22,7 @@ export function EvaluateERCRules(visitor, graph, nets) {
22
22
  reportItems.push({
23
23
  type,
24
24
  start: token,
25
- message: `Unconnected pin ${item.pin} for component`
25
+ message: `Unconnected pin: ${instance.assignedRefDes} pin ${item.pin}`
26
26
  });
27
27
  }
28
28
  }
@@ -44,12 +44,17 @@ export function EvaluateERCRules(visitor, graph, nets) {
44
44
  case ERC_Rules.NoConnectOnConnectedPin:
45
45
  {
46
46
  const instance = item.instance;
47
+ const { instance: targetComponent, pin: targetPin } = item.target;
48
+ let extra = '';
49
+ if (targetComponent && targetComponent.assignedRefDes) {
50
+ extra = `: ${targetComponent.assignedRefDes} pin ${targetPin}`;
51
+ }
47
52
  const token = getComponentFirstCtxToken(instance);
48
53
  if (token) {
49
54
  reportItems.push({
50
55
  type,
51
56
  start: token,
52
- message: `No connect on connected pin`
57
+ message: `No connect on connected pin${extra}`
53
58
  });
54
59
  }
55
60
  }
@@ -3,13 +3,15 @@ import { ERC_Rules } from "./rules.js";
3
3
  export function RuleCheck_UnconnectedPinsWires(graph) {
4
4
  const items = [];
5
5
  const allNodes = graph.nodes();
6
- allNodes.forEach(node => {
6
+ for (const node of allNodes) {
7
7
  const nodeInfo = graph.node(node);
8
8
  if (nodeInfo[0] === RenderItemType.Component) {
9
- const { component } = nodeInfo[1];
9
+ const renderComponent = nodeInfo[1];
10
+ const { component, unitId } = renderComponent;
10
11
  const edges = graph.nodeEdges(node);
11
- const instanceName = component.instanceName;
12
- const connectedPins = [];
12
+ const componentUnit = component.getUnit(unitId);
13
+ const instanceName = componentUnit.instanceName;
14
+ const connectedUnitPins = [];
13
15
  edges.forEach(edge => {
14
16
  const edgeInfo = graph.edge(edge.v, edge.w);
15
17
  let pin;
@@ -19,12 +21,12 @@ export function RuleCheck_UnconnectedPinsWires(graph) {
19
21
  else if (edge.w === instanceName) {
20
22
  pin = edgeInfo[3];
21
23
  }
22
- connectedPins.push(pin.getHashValue());
24
+ connectedUnitPins.push(pin.getHashValue());
23
25
  });
24
- const pinIds = Array.from(component.pins.keys());
26
+ const pinIds = Array.from(componentUnit.pins.keys());
25
27
  pinIds.forEach(pinId => {
26
28
  const hashValue = pinId.getHashValue();
27
- if (connectedPins.indexOf(hashValue) === -1) {
29
+ if (connectedUnitPins.indexOf(hashValue) === -1) {
28
30
  items.push({
29
31
  type: ERC_Rules.UnconnectedPin,
30
32
  instance: component,
@@ -43,6 +45,6 @@ export function RuleCheck_UnconnectedPinsWires(graph) {
43
45
  });
44
46
  }
45
47
  }
46
- });
48
+ }
47
49
  return items;
48
50
  }
package/dist/esm/utils.js CHANGED
@@ -58,7 +58,8 @@ export function getBoundsSize(bounds) {
58
58
  };
59
59
  }
60
60
  export function getPortType(component) {
61
- const drawingCommands = component.displayProp;
61
+ const targetUnit = component.getUnit();
62
+ const drawingCommands = targetUnit.displayProp;
62
63
  let foundPinType = null;
63
64
  const commands = drawingCommands.getCommands();
64
65
  commands.some(item => {
@@ -88,3 +88,8 @@ export class SymbolTable {
88
88
  }
89
89
  }
90
90
  }
91
+ export function cloneSymbol(symbol) {
92
+ return {
93
+ ...symbol
94
+ };
95
+ }
@@ -1,20 +1,12 @@
1
+ import { Import_specificContext } from "../antlr/CircuitScriptParser.js";
1
2
  import { buildInMethodNamesList } from "../builtinMethods.js";
2
- import { ParseSymbolType } from "../objects/types.js";
3
+ import { ImportFunctionHandling, ParseSymbolType } from "../objects/types.js";
4
+ import { cloneSymbol } from "./SymbolTable.js";
3
5
  import { SymbolTable } from "./SymbolTable.js";
4
6
  import { BaseVisitor } from "../BaseVisitor.js";
5
- import { SymbolValidatorContext } from "../globals.js";
7
+ import { BaseNamespace, SymbolValidatorContext } from "../globals.js";
6
8
  export class SymbolValidatorVisitor extends BaseVisitor {
7
9
  symbolTable = new SymbolTable();
8
- filePathStack = [];
9
- enterFile(filePath) {
10
- this.filePathStack.push(filePath);
11
- }
12
- exitFile() {
13
- this.filePathStack.pop();
14
- }
15
- getCurrentFile() {
16
- return this.filePathStack[this.filePathStack.length - 1];
17
- }
18
10
  addSymbolVariable(token, name, value, executor = null) {
19
11
  const useExecutor = executor === null ? this.getExecutor() : executor;
20
12
  this.symbolTable.addVariable(token, this.getCurrentFile(), useExecutor, name, value);
@@ -62,13 +54,58 @@ export class SymbolValidatorVisitor extends BaseVisitor {
62
54
  setSymbols(symbolTable) {
63
55
  this.symbolTable = symbolTable;
64
56
  }
65
- visitImport_expr = (ctx) => {
66
- const ID = ctx.ID().toString();
67
- const { pathExists } = this.handleImportFile(ID, false, ctx);
57
+ async importCommon(ctx, handling) {
58
+ const specifiedImports = [];
59
+ if (ctx instanceof Import_specificContext) {
60
+ const tmpImports = ctx._funcNames.map(item => {
61
+ return item.text;
62
+ });
63
+ specifiedImports.push(...tmpImports);
64
+ }
65
+ const id = ctx._moduleName.text;
66
+ const { pathExists, importedModule } = await this.handleImportFile(id, handling, true, ctx, specifiedImports);
68
67
  if (!pathExists) {
69
- this.symbolTable.addUndefined(this.getCurrentFile(), this.getExecutor(), ID, ctx.ID().getSymbol());
68
+ this.symbolTable.addUndefined(this.getCurrentFile(), this.getExecutor(), id, ctx._moduleName);
70
69
  }
70
+ else {
71
+ this.applyModuleImports(importedModule);
72
+ }
73
+ }
74
+ visitImport_simple = async (ctx) => {
75
+ await this.importCommon(ctx, ImportFunctionHandling.AllWithNamespace);
76
+ };
77
+ visitImport_all_simple = async (ctx) => {
78
+ await this.importCommon(ctx, ImportFunctionHandling.AllMergeIntoNamespace);
71
79
  };
80
+ visitImport_specific = async (ctx) => {
81
+ await this.importCommon(ctx, ImportFunctionHandling.SpecificMergeIntoNamespace);
82
+ };
83
+ applyModuleImports(module) {
84
+ const { importHandlingFlag: importHandling, specifiedImports } = module;
85
+ const addedSymbols = [];
86
+ const executor = this.getExecutor();
87
+ const symbolTable = this.symbolTable.getSymbols();
88
+ symbolTable.forEach((value, key) => {
89
+ if (value.type === ParseSymbolType.Function) {
90
+ const definedSymbol = value;
91
+ if (definedSymbol.fileName === module.moduleFilePath) {
92
+ const addSymbolToNamespace = importHandling === ImportFunctionHandling.AllMergeIntoNamespace
93
+ || (importHandling === ImportFunctionHandling.SpecificMergeIntoNamespace
94
+ && specifiedImports.indexOf(definedSymbol.id) !== -1);
95
+ if (addSymbolToNamespace) {
96
+ const funcPath = `${BaseNamespace}${definedSymbol.id}`;
97
+ const tmpSymbol = cloneSymbol(value);
98
+ tmpSymbol.context = executor;
99
+ addedSymbols.push([funcPath, tmpSymbol]);
100
+ }
101
+ }
102
+ }
103
+ });
104
+ addedSymbols.forEach(item => {
105
+ const [key, value] = item;
106
+ symbolTable.set(key, value);
107
+ });
108
+ }
72
109
  visitAssignment_expr = (ctx) => {
73
110
  const ctxDataExpr = ctx.data_expr();
74
111
  this.visit(ctxDataExpr);