circuitscript 0.1.5 → 0.1.7

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 (96) hide show
  1. package/dist/cjs/BaseVisitor.js +127 -73
  2. package/dist/cjs/SemanticTokenVisitor.js +19 -13
  3. package/dist/cjs/antlr/CircuitScriptParser.js +711 -671
  4. package/dist/cjs/builtinMethods.js +29 -25
  5. package/dist/cjs/environment.js +118 -0
  6. package/dist/cjs/execute.js +53 -12
  7. package/dist/cjs/geometry.js +1 -0
  8. package/dist/cjs/globals.js +11 -6
  9. package/dist/cjs/helpers.js +135 -127
  10. package/dist/cjs/index.js +5 -0
  11. package/dist/cjs/layout.js +37 -12
  12. package/dist/cjs/main.js +31 -19
  13. package/dist/cjs/objects/ExecutionScope.js +33 -0
  14. package/dist/cjs/objects/ParamDefinition.js +15 -15
  15. package/dist/cjs/parser.js +27 -21
  16. package/dist/cjs/regenerate-tests.js +9 -6
  17. package/dist/cjs/render.js +3 -1
  18. package/dist/cjs/sizing.js +5 -58
  19. package/dist/cjs/utils.js +85 -30
  20. package/dist/cjs/validate/SymbolTable.js +96 -0
  21. package/dist/cjs/validate/SymbolValidatorResolveVisitor.js +14 -0
  22. package/dist/cjs/validate/SymbolValidatorVisitor.js +170 -0
  23. package/dist/cjs/validate.js +52 -44
  24. package/dist/cjs/visitor.js +140 -24
  25. package/dist/esm/{BaseVisitor.mjs → BaseVisitor.js} +98 -45
  26. package/dist/esm/{SemanticTokenVisitor.mjs → SemanticTokenVisitor.js} +17 -11
  27. package/dist/esm/antlr/{CircuitScriptParser.mjs → CircuitScriptParser.js} +711 -671
  28. package/dist/esm/{builtinMethods.mjs → builtinMethods.js} +20 -16
  29. package/dist/esm/{draw_symbols.mjs → draw_symbols.js} +7 -7
  30. package/dist/esm/environment.js +110 -0
  31. package/dist/esm/{execute.mjs → execute.js} +66 -25
  32. package/dist/esm/{export.mjs → export.js} +2 -2
  33. package/dist/esm/{geometry.mjs → geometry.js} +6 -5
  34. package/dist/esm/{globals.mjs → globals.js} +6 -1
  35. package/dist/esm/helpers.js +377 -0
  36. package/dist/esm/index.js +20 -0
  37. package/dist/esm/{layout.mjs → layout.js} +42 -20
  38. package/dist/esm/{lexer.mjs → lexer.js} +2 -2
  39. package/dist/esm/{main.mjs → main.js} +33 -21
  40. package/dist/esm/objects/{ClassComponent.mjs → ClassComponent.js} +5 -4
  41. package/dist/esm/objects/{ExecutionScope.mjs → ExecutionScope.js} +33 -0
  42. package/dist/esm/objects/{Frame.mjs → Frame.js} +1 -1
  43. package/dist/esm/objects/{ParamDefinition.mjs → ParamDefinition.js} +1 -1
  44. package/dist/esm/objects/{PinDefinition.mjs → PinDefinition.js} +1 -1
  45. package/dist/esm/parser.js +71 -0
  46. package/dist/esm/{regenerate-tests.mjs → regenerate-tests.js} +10 -7
  47. package/dist/esm/{render.mjs → render.js} +11 -9
  48. package/dist/esm/{sizing.mjs → sizing.js} +6 -34
  49. package/dist/esm/{utils.mjs → utils.js} +61 -17
  50. package/dist/esm/validate/SymbolTable.js +90 -0
  51. package/dist/esm/validate/SymbolValidatorResolveVisitor.js +10 -0
  52. package/dist/esm/validate/SymbolValidatorVisitor.js +163 -0
  53. package/dist/esm/validate.js +86 -0
  54. package/dist/esm/{visitor.mjs → visitor.js} +151 -35
  55. package/dist/fonts/Arial.ttf +0 -0
  56. package/dist/fonts/Inter-Bold.ttf +0 -0
  57. package/dist/fonts/Inter-Regular.ttf +0 -0
  58. package/dist/fonts/OpenSans-Regular.ttf +0 -0
  59. package/dist/fonts/Roboto-Regular.ttf +0 -0
  60. package/dist/libs/lib.cst +423 -0
  61. package/dist/types/BaseVisitor.d.ts +34 -21
  62. package/dist/types/SemanticTokenVisitor.d.ts +6 -5
  63. package/dist/types/antlr/CircuitScriptParser.d.ts +4 -2
  64. package/dist/types/builtinMethods.d.ts +3 -2
  65. package/dist/types/environment.d.ts +31 -0
  66. package/dist/types/globals.d.ts +4 -1
  67. package/dist/types/helpers.d.ts +12 -14
  68. package/dist/types/index.d.ts +5 -0
  69. package/dist/types/objects/ClassComponent.d.ts +1 -0
  70. package/dist/types/objects/ExecutionScope.d.ts +11 -0
  71. package/dist/types/objects/types.d.ts +6 -1
  72. package/dist/types/parser.d.ts +7 -11
  73. package/dist/types/sizing.d.ts +0 -3
  74. package/dist/types/utils.d.ts +30 -6
  75. package/dist/types/validate/SymbolTable.d.ts +40 -0
  76. package/dist/types/validate/SymbolValidatorResolveVisitor.d.ts +7 -0
  77. package/dist/types/validate/SymbolValidatorVisitor.d.ts +32 -0
  78. package/dist/types/validate.d.ts +1 -1
  79. package/package.json +14 -13
  80. package/dist/cjs/SymbolValidatorVisitor.js +0 -233
  81. package/dist/esm/SymbolValidatorVisitor.mjs +0 -222
  82. package/dist/esm/helpers.mjs +0 -364
  83. package/dist/esm/index.mjs +0 -15
  84. package/dist/esm/parser.mjs +0 -64
  85. package/dist/esm/validate.mjs +0 -74
  86. package/dist/types/SymbolValidatorVisitor.d.ts +0 -61
  87. package/dist/types/layout.d.ts +0 -148
  88. /package/dist/esm/antlr/{CircuitScriptLexer.mjs → CircuitScriptLexer.js} +0 -0
  89. /package/dist/esm/antlr/{CircuitScriptVisitor.mjs → CircuitScriptVisitor.js} +0 -0
  90. /package/dist/esm/{fonts.mjs → fonts.js} +0 -0
  91. /package/dist/esm/{logger.mjs → logger.js} +0 -0
  92. /package/dist/esm/objects/{Net.mjs → Net.js} +0 -0
  93. /package/dist/esm/objects/{PinTypes.mjs → PinTypes.js} +0 -0
  94. /package/dist/esm/objects/{Wire.mjs → Wire.js} +0 -0
  95. /package/dist/esm/objects/{types.mjs → types.js} +0 -0
  96. /package/dist/esm/{server.mjs → server.js} +0 -0
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NumberOperator = exports.WrappedNumber = exports.PercentageValue = exports.numeric = exports.NumericValue = exports.ParamDefinition = void 0;
4
- const utils_1 = require("../utils");
4
+ const utils_js_1 = require("../utils.js");
5
5
  const big_js_1 = require("big.js");
6
6
  class ParamDefinition {
7
7
  constructor(paramName, paramValue) {
@@ -17,7 +17,7 @@ class NumericValue {
17
17
  const matches = value.match(/^([\d]+(?:.[\d]+)?)([\w]*)$/);
18
18
  if (matches) {
19
19
  this.valuePart = new big_js_1.Big(matches[1]);
20
- this.prefixPart = (0, utils_1.getNumberExponential)(matches[2]);
20
+ this.prefixPart = (0, utils_js_1.getNumberExponential)(matches[2]);
21
21
  }
22
22
  else {
23
23
  throw new Error("Invalid numeric value: " + value);
@@ -27,7 +27,7 @@ class NumericValue {
27
27
  this.valuePart = new big_js_1.Big(value);
28
28
  this.prefixPart = prefix;
29
29
  this.value = this.valuePart.toString()
30
- + (0, utils_1.getNumberExponentialText)(prefix);
30
+ + (0, utils_js_1.getNumberExponentialText)(prefix);
31
31
  }
32
32
  }
33
33
  toString() {
@@ -39,7 +39,7 @@ class NumericValue {
39
39
  }
40
40
  else {
41
41
  return this.valuePart.toString()
42
- + (0, utils_1.getNumberExponentialText)(this.prefixPart);
42
+ + (0, utils_js_1.getNumberExponentialText)(this.prefixPart);
43
43
  }
44
44
  }
45
45
  toNumber() {
@@ -52,7 +52,7 @@ class NumericValue {
52
52
  if (typeof value === 'number') {
53
53
  value = numeric(value);
54
54
  }
55
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().div(value.toBigNumber()));
55
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().div(value.toBigNumber()));
56
56
  }
57
57
  half() {
58
58
  return this.div(2);
@@ -61,28 +61,28 @@ class NumericValue {
61
61
  if (typeof value === 'number') {
62
62
  value = numeric(value);
63
63
  }
64
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().mul(value.toBigNumber()));
64
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().mul(value.toBigNumber()));
65
65
  }
66
66
  add(value) {
67
67
  if (typeof value === 'number') {
68
68
  value = numeric(value);
69
69
  }
70
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().add(value.toBigNumber()));
70
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().add(value.toBigNumber()));
71
71
  }
72
72
  sub(value) {
73
73
  if (typeof value === 'number') {
74
74
  value = numeric(value);
75
75
  }
76
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().sub(value.toBigNumber()));
76
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().sub(value.toBigNumber()));
77
77
  }
78
78
  mod(value) {
79
79
  if (typeof value === 'number') {
80
80
  value = numeric(value);
81
81
  }
82
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().mod(value.toBigNumber()));
82
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().mod(value.toBigNumber()));
83
83
  }
84
84
  neg() {
85
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().neg());
85
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().neg());
86
86
  }
87
87
  eq(value) {
88
88
  return this.toBigNumber().eq(value.toBigNumber());
@@ -129,27 +129,27 @@ class NumberOperator {
129
129
  multiply(value1, value2) {
130
130
  const big1 = new big_js_1.Big(value1.toNumber());
131
131
  const big2 = new big_js_1.Big(value2.toNumber());
132
- return (0, utils_1.resolveToNumericValue)(big1.mul(big2));
132
+ return (0, utils_js_1.resolveToNumericValue)(big1.mul(big2));
133
133
  }
134
134
  divide(value1, value2) {
135
135
  const big1 = new big_js_1.Big(value1.toNumber());
136
136
  const big2 = new big_js_1.Big(value2.toNumber());
137
- return (0, utils_1.resolveToNumericValue)(big1.div(big2));
137
+ return (0, utils_js_1.resolveToNumericValue)(big1.div(big2));
138
138
  }
139
139
  addition(value1, value2) {
140
140
  const big1 = new big_js_1.Big(value1.toNumber());
141
141
  const big2 = new big_js_1.Big(value2.toNumber());
142
- return (0, utils_1.resolveToNumericValue)(big1.add(big2));
142
+ return (0, utils_js_1.resolveToNumericValue)(big1.add(big2));
143
143
  }
144
144
  subtraction(value1, value2) {
145
145
  const big1 = new big_js_1.Big(value1.toNumber());
146
146
  const big2 = new big_js_1.Big(value2.toNumber());
147
- return (0, utils_1.resolveToNumericValue)(big1.sub(big2));
147
+ return (0, utils_js_1.resolveToNumericValue)(big1.sub(big2));
148
148
  }
149
149
  modulus(value1, value2) {
150
150
  const big1 = new big_js_1.Big(value1.toNumber());
151
151
  const big2 = new big_js_1.Big(value2.toNumber());
152
- return (0, utils_1.resolveToNumericValue)(big1.mod(big2));
152
+ return (0, utils_js_1.resolveToNumericValue)(big1.mod(big2));
153
153
  }
154
154
  }
155
155
  exports.NumberOperator = NumberOperator;
@@ -1,59 +1,65 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CircuitscriptParserErrorListener = exports.TempErrorStrategy = exports.parseFileWithVisitor = void 0;
3
+ exports.CircuitscriptParserErrorListener = exports.parseFileWithVisitor = void 0;
4
4
  const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
5
5
  const lexer_js_1 = require("./lexer.js");
6
6
  const utils_js_1 = require("./utils.js");
7
7
  const antlr4ng_1 = require("antlr4ng");
8
- function parseFileWithVisitor(visitor, data) {
8
+ async function parseFileWithVisitor(visitor, data) {
9
+ const lexerErrorListener = new CircuitscriptParserErrorListener(visitor.onErrorHandler);
10
+ const parserErrorListener = new CircuitscriptParserErrorListener(visitor.onErrorHandler);
9
11
  const chars = antlr4ng_1.CharStream.fromString(data);
10
12
  const lexer = new lexer_js_1.MainLexer(chars);
13
+ lexer.removeErrorListeners();
14
+ lexer.addErrorListener(lexerErrorListener);
11
15
  const lexerTimer = new utils_js_1.SimpleStopwatch();
12
16
  const tokens = new antlr4ng_1.CommonTokenStream(lexer);
13
17
  tokens.fill();
14
18
  const lexerTimeTaken = lexerTimer.lap();
15
19
  const parserTimer = new utils_js_1.SimpleStopwatch();
16
20
  const parser = new CircuitScriptParser_js_1.CircuitScriptParser(tokens);
21
+ parser.removeErrorListeners();
22
+ parser.addErrorListener(parserErrorListener);
17
23
  const tree = parser.script();
18
- let hasError = false;
19
24
  try {
20
- visitor.visit(tree);
25
+ await visitor.visitAsync(tree);
21
26
  }
22
- catch (err) {
23
- console.log(err);
24
- hasError = true;
27
+ catch (error) {
28
+ if (visitor.onErrorHandler) {
29
+ if (error instanceof utils_js_1.RuntimeExecutionError) {
30
+ visitor.onErrorHandler(error.message, null, error);
31
+ }
32
+ else {
33
+ throw error;
34
+ }
35
+ }
25
36
  }
26
37
  const parserTimeTaken = parserTimer.lap();
27
38
  return {
28
39
  tree, parser,
29
40
  hasParseError: false,
30
- hasError,
41
+ hasError: false,
31
42
  parserTimeTaken,
32
43
  lexerTimeTaken,
33
44
  };
34
45
  }
35
46
  exports.parseFileWithVisitor = parseFileWithVisitor;
36
- class TempErrorStrategy extends antlr4ng_1.DefaultErrorStrategy {
37
- recover(recognizer, e) {
38
- throw new Error('Method not implemented.');
39
- }
40
- reportError(recognizer, e) {
41
- throw new Error('Method not implemented.');
42
- }
43
- }
44
- exports.TempErrorStrategy = TempErrorStrategy;
45
47
  class CircuitscriptParserErrorListener {
46
48
  constructor(onErrorHandler = null) {
47
49
  this.syntaxErrorCounter = 0;
48
50
  this.onErrorHandler = null;
49
51
  this.onErrorHandler = onErrorHandler;
50
52
  }
51
- syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e) {
52
- if (this.onErrorHandler) {
53
- this.onErrorHandler(line, charPositionInLine, msg, e);
53
+ syntaxError(recognizer, offendingSymbol, line, column, msg, e) {
54
+ if (this.onErrorHandler && e) {
55
+ this.onErrorHandler(msg, e.ctx, e);
54
56
  }
55
57
  else {
56
- console.log("Syntax error at line", line, ':', charPositionInLine, ' - ', msg);
58
+ if (offendingSymbol && msg.match("extraneous input 'import' expecting")) {
59
+ msg = "Invalid import statement";
60
+ throw new utils_js_1.ParseSyntaxError("Invalid import statement", offendingSymbol);
61
+ }
62
+ console.log("Syntax error at line", line, ':', column, ' - ', msg);
57
63
  }
58
64
  this.syntaxErrorCounter++;
59
65
  }
@@ -5,12 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const fs_1 = __importDefault(require("fs"));
7
7
  const helpers_js_1 = require("./helpers.js");
8
- const sizing_js_1 = require("./sizing.js");
8
+ const environment_js_1 = require("./environment.js");
9
9
  const mainDir = './__tests__/renderData/';
10
- const fontsPath = (0, helpers_js_1.getFontsPath)();
11
- const defaultLibsPath = (0, helpers_js_1.getDefaultLibsPath)();
10
+ const env = new environment_js_1.NodeScriptEnvironment();
11
+ environment_js_1.NodeScriptEnvironment.setInstance(env);
12
12
  async function regenerateTests(extra = "") {
13
- await (0, sizing_js_1.prepareSVGEnvironment)(fontsPath);
13
+ env.prepareSVGEnvironment();
14
14
  const cstFiles = [];
15
15
  const files = fs_1.default.readdirSync(mainDir);
16
16
  files.forEach(file => {
@@ -22,9 +22,12 @@ async function regenerateTests(extra = "") {
22
22
  const inputPath = mainDir + file;
23
23
  const scriptData = fs_1.default.readFileSync(inputPath, { encoding: 'utf-8' });
24
24
  const outputPath = mainDir + 'svgs/' + file + extra + '.svg';
25
+ env.setModuleDirectory(mainDir);
25
26
  (0, helpers_js_1.renderScript)(scriptData, outputPath, {
26
- currentDirectory: mainDir,
27
- defaultLibsPath,
27
+ dumpNets: false,
28
+ dumpData: false,
29
+ showStats: false,
30
+ environment: env,
28
31
  });
29
32
  console.log('generated ', outputPath);
30
33
  });
@@ -11,11 +11,13 @@ const globals_js_1 = require("./globals.js");
11
11
  const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
12
12
  const utils_js_1 = require("./utils.js");
13
13
  const helpers_js_1 = require("./helpers.js");
14
+ const environment_js_1 = require("./environment.js");
14
15
  const svg_to_pdfkit_1 = __importDefault(require("svg-to-pdfkit"));
15
16
  const Frame_js_1 = require("./objects/Frame.js");
16
17
  const draw_symbols_js_1 = require("./draw_symbols.js");
17
18
  function createSvgCanvas() {
18
- const window = (0, sizing_js_1.getCreateSVGWindow)()();
19
+ const env = environment_js_1.NodeScriptEnvironment.getInstance();
20
+ const window = env.createSVGWindow();
19
21
  const document = window.document;
20
22
  (0, svg_js_1.registerWindow)(window, document);
21
23
  const canvas = (0, svg_js_1.SVG)(document.documentElement);
@@ -1,71 +1,18 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.measureTextSize2 = exports.applyFontsToSVG = exports.getCreateSVGWindow = exports.prepareSVGEnvironment = void 0;
27
- const svg_js_1 = require("@svgdotjs/svg.js");
3
+ exports.measureTextSize2 = exports.applyFontsToSVG = void 0;
28
4
  const big_js_1 = require("big.js");
29
5
  const geometry_js_1 = require("./geometry.js");
30
6
  const globals_js_1 = require("./globals.js");
31
- const helpers_js_1 = require("./helpers.js");
32
- let MainCanvas = null;
33
- const supportedFonts = {
34
- 'Arial': 'Arial.ttf',
35
- };
36
- let globalCreateSVGWindow;
37
- async function prepareSVGEnvironment(fontsPath) {
38
- const moduleType = (0, helpers_js_1.detectJSModuleType)();
39
- if (moduleType === helpers_js_1.JSModuleType.CommonJs) {
40
- const { config, createSVGWindow } = await Promise.resolve().then(() => __importStar(require('svgdom')));
41
- globalCreateSVGWindow = createSVGWindow;
42
- if (fontsPath !== null) {
43
- await config.setFontDir(fontsPath)
44
- .setFontFamilyMappings(supportedFonts)
45
- .preloadFonts();
46
- }
47
- }
48
- }
49
- exports.prepareSVGEnvironment = prepareSVGEnvironment;
50
- function getCreateSVGWindow() {
51
- if (globalCreateSVGWindow === undefined) {
52
- throw "SVG environment is not set up yet";
53
- }
54
- return globalCreateSVGWindow;
55
- }
56
- exports.getCreateSVGWindow = getCreateSVGWindow;
7
+ const environment_js_1 = require("./environment.js");
57
8
  function applyFontsToSVG(canvas) {
58
9
  }
59
10
  exports.applyFontsToSVG = applyFontsToSVG;
60
11
  const measureTextSizeCache = {};
61
12
  const measureTextSizeCacheHits = {};
62
13
  function measureTextSize2(text, fontFamily, fontSize, fontWeight = 'regular', anchor = geometry_js_1.HorizontalAlign.Left, vanchor = geometry_js_1.VerticalAlign.Bottom) {
63
- if (MainCanvas === null) {
64
- const window = getCreateSVGWindow()();
65
- const { document } = window;
66
- (0, svg_js_1.registerWindow)(window, document);
67
- MainCanvas = (0, svg_js_1.SVG)(document.documentElement);
68
- }
14
+ const environment = environment_js_1.NodeScriptEnvironment.getInstance();
15
+ const mainCanvas = environment.getCanvasWindow();
69
16
  const key = `${text}-${fontFamily}-${fontSize}-${fontWeight}-${anchor}-${vanchor}`;
70
17
  if (measureTextSizeCache[key] === undefined) {
71
18
  let dominantBaseline = geometry_js_1.VerticalAlignProp.Hanging;
@@ -93,7 +40,7 @@ function measureTextSize2(text, fontFamily, fontSize, fontWeight = 'regular', an
93
40
  break;
94
41
  }
95
42
  fontFamily = globals_js_1.defaultFont;
96
- const tmpTextElement = MainCanvas.text(text).font({
43
+ const tmpTextElement = mainCanvas.text(text).font({
97
44
  family: fontFamily,
98
45
  size: fontSize,
99
46
  anchor: useAnchor,
package/dist/cjs/utils.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getBlockTypeString = exports.generateDebugSequenceAction = exports.sequenceActionString = exports.areasOverlap = exports.isPointWithinArea = exports.resolveToNumericValue = exports.getNumberExponentialText = exports.getNumberExponential = exports.combineMaps = exports.throwWithContext = exports.roundValue = exports.getPortType = exports.getBoundsSize = exports.toNearestGrid = exports.resizeToNearestGrid = exports.printBounds = exports.resizeBounds = exports.SimpleStopwatch = void 0;
3
+ exports.RenderError = exports.RuntimeExecutionError = exports.ParseError = exports.ParseSyntaxError = exports.BaseError = exports.getBlockTypeString = exports.generateDebugSequenceAction = exports.sequenceActionString = exports.areasOverlap = exports.isPointWithinArea = exports.resolveToNumericValue = exports.getNumberExponentialText = exports.getNumberExponential = exports.combineMaps = exports.throwWithTokenRange = exports.throwWithToken = exports.throwWithContext = exports.roundValue = exports.getPortType = exports.getBoundsSize = exports.toNearestGrid = exports.resizeToNearestGrid = exports.printBounds = exports.resizeBounds = exports.SimpleStopwatch = void 0;
4
4
  const big_js_1 = require("big.js");
5
- const ClassComponent_1 = require("./objects/ClassComponent");
6
- const ParamDefinition_1 = require("./objects/ParamDefinition");
7
- const ExecutionScope_1 = require("./objects/ExecutionScope");
8
- const globals_1 = require("./globals");
5
+ const ClassComponent_js_1 = require("./objects/ClassComponent.js");
6
+ const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
7
+ const ExecutionScope_js_1 = require("./objects/ExecutionScope.js");
8
+ const globals_js_1 = require("./globals.js");
9
9
  class SimpleStopwatch {
10
10
  constructor() {
11
11
  this.startTime = new Date();
@@ -81,22 +81,21 @@ function roundValue(value) {
81
81
  return resolveToNumericValue(new big_js_1.Big(value.toBigNumber().toFixed(7)));
82
82
  }
83
83
  exports.roundValue = roundValue;
84
- function throwWithContext(context, message) {
85
- const startLine = context.start?.line;
86
- const startColumn = context.start?.column;
87
- const startString = startLine + ":" + startColumn;
88
- const stopLine = context.stop?.line;
89
- const stopColumn = context.stop?.column;
90
- let stopString = "";
91
- if (startLine === stopLine) {
92
- stopString = stopColumn?.toString();
84
+ function throwWithContext(context, messageOrError) {
85
+ if (messageOrError instanceof BaseError) {
86
+ throw messageOrError;
93
87
  }
94
- else {
95
- stopString = stopLine + ":" + stopString;
96
- }
97
- throw `Parse exception at [${startString} - ${stopString}] : ${message}`;
88
+ throwWithTokenRange(messageOrError, context.start, context.stop);
98
89
  }
99
90
  exports.throwWithContext = throwWithContext;
91
+ function throwWithToken(message, token) {
92
+ throw new ParseError(message, token);
93
+ }
94
+ exports.throwWithToken = throwWithToken;
95
+ function throwWithTokenRange(message, startToken, endToken) {
96
+ throw new ParseError(message, startToken, endToken);
97
+ }
98
+ exports.throwWithTokenRange = throwWithTokenRange;
100
99
  function combineMaps(map1, map2) {
101
100
  const newMap = new Map(map1);
102
101
  map2.forEach((value, key) => {
@@ -156,7 +155,7 @@ function getNumberExponentialText(value) {
156
155
  exports.getNumberExponentialText = getNumberExponentialText;
157
156
  function resolveToNumericValue(value) {
158
157
  if (value.toNumber() === 0) {
159
- return new ParamDefinition_1.NumericValue(0);
158
+ return new ParamDefinition_js_1.NumericValue(0);
160
159
  }
161
160
  const isNeg = value.lt(0);
162
161
  const positiveValue = isNeg ? value.neg() : value;
@@ -166,7 +165,7 @@ function resolveToNumericValue(value) {
166
165
  const tmpValue1 = positiveValue.div(Math.pow(10, prefixPart * 3));
167
166
  useValue = isNeg ? tmpValue1.neg() : tmpValue1;
168
167
  }
169
- return new ParamDefinition_1.NumericValue(useValue, prefixPart * 3);
168
+ return new ParamDefinition_js_1.NumericValue(useValue, prefixPart * 3);
170
169
  }
171
170
  exports.resolveToNumericValue = resolveToNumericValue;
172
171
  function isPointWithinArea(point, bounds) {
@@ -190,7 +189,7 @@ exports.areasOverlap = areasOverlap;
190
189
  function sequenceActionString(sequenceAction) {
191
190
  const tmp = [...sequenceAction];
192
191
  const action = tmp[0];
193
- if (action === ExecutionScope_1.SequenceAction.Wire) {
192
+ if (action === ExecutionScope_js_1.SequenceAction.Wire) {
194
193
  tmp[2] = tmp[2].map(item2 => {
195
194
  const lengthValue = item2.value;
196
195
  const useValue = [item2.direction];
@@ -201,12 +200,12 @@ function sequenceActionString(sequenceAction) {
201
200
  return useValue.join(",");
202
201
  }).join(" ");
203
202
  }
204
- else if (action === ExecutionScope_1.SequenceAction.Frame) {
203
+ else if (action === ExecutionScope_js_1.SequenceAction.Frame) {
205
204
  tmp[1] = sequenceAction[1].frameId;
206
205
  }
207
- else if (action !== ExecutionScope_1.SequenceAction.WireJump) {
206
+ else if (action !== ExecutionScope_js_1.SequenceAction.WireJump) {
208
207
  const [, component] = sequenceAction;
209
- if (component instanceof ClassComponent_1.ClassComponent) {
208
+ if (component instanceof ClassComponent_js_1.ClassComponent) {
210
209
  tmp[1] = sequenceAction[1].instanceName;
211
210
  }
212
211
  }
@@ -218,12 +217,12 @@ function generateDebugSequenceAction(sequence) {
218
217
  return sequence.map(item => {
219
218
  const returnResult = [...item];
220
219
  const [action,] = item;
221
- if (action === ExecutionScope_1.SequenceAction.Assign) {
220
+ if (action === ExecutionScope_js_1.SequenceAction.Assign) {
222
221
  const [, name, component] = item;
223
222
  variableMapping.set(name, component);
224
223
  }
225
224
  else {
226
- if (action === ExecutionScope_1.SequenceAction.At || action === ExecutionScope_1.SequenceAction.To) {
225
+ if (action === ExecutionScope_js_1.SequenceAction.At || action === ExecutionScope_js_1.SequenceAction.To) {
227
226
  const [, component,] = item;
228
227
  const foundIndex = Array.from(variableMapping.values()).findIndex(item2 => {
229
228
  if (component._copyFrom !== null) {
@@ -244,19 +243,75 @@ exports.generateDebugSequenceAction = generateDebugSequenceAction;
244
243
  function getBlockTypeString(type) {
245
244
  let returnValue = 'branch';
246
245
  switch (type) {
247
- case globals_1.BlockTypes.Branch:
246
+ case globals_js_1.BlockTypes.Branch:
248
247
  returnValue = 'branch';
249
248
  break;
250
- case globals_1.BlockTypes.Join:
249
+ case globals_js_1.BlockTypes.Join:
251
250
  returnValue = 'join';
252
251
  break;
253
- case globals_1.BlockTypes.Parallel:
252
+ case globals_js_1.BlockTypes.Parallel:
254
253
  returnValue = 'parallel';
255
254
  break;
256
- case globals_1.BlockTypes.Point:
255
+ case globals_js_1.BlockTypes.Point:
257
256
  returnValue = 'point';
258
257
  break;
259
258
  }
260
259
  return returnValue;
261
260
  }
262
261
  exports.getBlockTypeString = getBlockTypeString;
262
+ class BaseError extends Error {
263
+ constructor(message, startToken, endToken, filePath) {
264
+ super(message);
265
+ this.name = 'BaseError';
266
+ this.message = message;
267
+ this.startToken = startToken;
268
+ this.endToken = endToken;
269
+ this.filePath = filePath;
270
+ }
271
+ toString() {
272
+ const parts = [this.name];
273
+ if (this.startToken) {
274
+ const { line, column } = this.startToken;
275
+ if (this.endToken && (this.endToken.line !== this.startToken.line || this.endToken.column !== this.startToken.column)) {
276
+ const endLine = this.endToken.line;
277
+ const endColumn = this.endToken.column + (this.endToken.stop - this.endToken.start);
278
+ parts.push(` at ${line}:${column}-${endLine}:${endColumn}`);
279
+ }
280
+ else {
281
+ parts.push(` at ${line}:${column}`);
282
+ }
283
+ }
284
+ parts.push(`: ${this.message}`);
285
+ return parts.join('');
286
+ }
287
+ }
288
+ exports.BaseError = BaseError;
289
+ class ParseSyntaxError extends BaseError {
290
+ constructor() {
291
+ super(...arguments);
292
+ this.name = 'ParseSyntaxError';
293
+ }
294
+ }
295
+ exports.ParseSyntaxError = ParseSyntaxError;
296
+ class ParseError extends ParseSyntaxError {
297
+ constructor() {
298
+ super(...arguments);
299
+ this.name = 'ParseError';
300
+ }
301
+ }
302
+ exports.ParseError = ParseError;
303
+ class RuntimeExecutionError extends BaseError {
304
+ constructor() {
305
+ super(...arguments);
306
+ this.name = 'RuntimeExecutionError';
307
+ }
308
+ }
309
+ exports.RuntimeExecutionError = RuntimeExecutionError;
310
+ class RenderError extends Error {
311
+ constructor(message, stage) {
312
+ super(message);
313
+ this.name = 'RenderError';
314
+ this.stage = stage;
315
+ }
316
+ }
317
+ exports.RenderError = RenderError;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SymbolTable = void 0;
4
+ const types_js_1 = require("../objects/types.js");
5
+ class SymbolTable {
6
+ constructor() {
7
+ this.symbols = new Map();
8
+ this.executonContextsNamespaces = [];
9
+ }
10
+ getSymbols() {
11
+ return this.symbols;
12
+ }
13
+ addFunction(token, fileName, executionContext, id, funcDefinedParameters) {
14
+ return this.add(fileName, executionContext, id, types_js_1.ParseSymbolType.Function, {
15
+ funcDefinedParameters
16
+ }, token);
17
+ }
18
+ addVariable(token, fileName, executionContext, id, variableValue) {
19
+ return this.add(fileName, executionContext, id, types_js_1.ParseSymbolType.Variable, {
20
+ variableValue
21
+ }, token);
22
+ }
23
+ addUndefined(filePath, executionContext, id, token) {
24
+ return this.add(filePath, executionContext, id, types_js_1.ParseSymbolType.Undefined, {}, token);
25
+ }
26
+ add(fileName, executionContext, id, type, extra, token) {
27
+ if (this.executonContextsNamespaces.indexOf(executionContext.namespace) === -1) {
28
+ this.executonContextsNamespaces.push(executionContext.namespace);
29
+ }
30
+ const item = {
31
+ id,
32
+ type,
33
+ context: executionContext,
34
+ fileName,
35
+ extra,
36
+ token,
37
+ instances: [],
38
+ };
39
+ if (token !== null) {
40
+ item.instances.push(token);
41
+ }
42
+ this.symbols.set(this.idName(executionContext, id), item);
43
+ return item;
44
+ }
45
+ idName(executionContext, id) {
46
+ return executionContext.namespace + id;
47
+ }
48
+ dumpSymbols() {
49
+ for (const [key, value] of this.symbols) {
50
+ console.log(value.type.padEnd(10, " "), key);
51
+ }
52
+ }
53
+ exists(executionContext, id) {
54
+ const name = this.idName(executionContext, id);
55
+ return this.symbols.has(name);
56
+ }
57
+ existsAny(executionContext, id) {
58
+ if (this.exists(executionContext, id)) {
59
+ return true;
60
+ }
61
+ else {
62
+ return this.searchParentContext(executionContext, id) !== null;
63
+ }
64
+ }
65
+ get(executionContext, id) {
66
+ const name = this.idName(executionContext, id);
67
+ return this.symbols.get(name);
68
+ }
69
+ getParentContexts(executionContext, contextsNamespace) {
70
+ if (executionContext.parentContext !== null) {
71
+ contextsNamespace.push(executionContext.parentContext.namespace);
72
+ this.getParentContexts(executionContext.parentContext, contextsNamespace);
73
+ }
74
+ return contextsNamespace;
75
+ }
76
+ searchParentContext(executionContext, id) {
77
+ const contextNames = this.getParentContexts(executionContext, []);
78
+ for (const [key,] of this.symbols) {
79
+ if (key.endsWith(`.${id}`)) {
80
+ const { context } = this.symbols.get(key);
81
+ if (contextNames.indexOf(context.namespace) !== -1) {
82
+ return context;
83
+ }
84
+ }
85
+ }
86
+ return null;
87
+ }
88
+ clearUndefined() {
89
+ for (const [key, value] of this.symbols) {
90
+ if (value.type === types_js_1.ParseSymbolType.Undefined) {
91
+ this.symbols.delete(key);
92
+ }
93
+ }
94
+ }
95
+ }
96
+ exports.SymbolTable = SymbolTable;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SymbolValidatorResolveVisitor = void 0;
4
+ const SymbolValidatorVisitor_js_1 = require("./SymbolValidatorVisitor.js");
5
+ class SymbolValidatorResolveVisitor extends SymbolValidatorVisitor_js_1.SymbolValidatorVisitor {
6
+ addSymbolVariable(token, name, value) {
7
+ }
8
+ addSymbolFunction(token, functionName, funcDefinedParameters) {
9
+ if (this.symbolTable.exists(this.getExecutor(), functionName)) {
10
+ this.symbolTable.addFunction(token, this.getCurrentFile(), this.getExecutor(), functionName, funcDefinedParameters);
11
+ }
12
+ }
13
+ }
14
+ exports.SymbolValidatorResolveVisitor = SymbolValidatorResolveVisitor;