circuitscript 0.0.21 → 0.0.24

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 (163) hide show
  1. package/dist/cjs/antlr/CircuitScriptLexer.js +288 -0
  2. package/dist/cjs/antlr/CircuitScriptParser.js +4905 -0
  3. package/dist/cjs/antlr/CircuitScriptVisitor.js +6 -0
  4. package/{src/draw_symbols.ts → dist/cjs/draw_symbols.js} +303 -614
  5. package/dist/cjs/execute.js +780 -0
  6. package/{src/export.ts → dist/cjs/export.js} +34 -56
  7. package/dist/cjs/fonts.js +4 -0
  8. package/dist/cjs/geometry.js +430 -0
  9. package/dist/cjs/globals.js +60 -0
  10. package/dist/cjs/helpers.js +99 -0
  11. package/dist/cjs/index.js +29 -0
  12. package/{src/layout.ts → dist/cjs/layout.js} +413 -1002
  13. package/dist/cjs/lexer.js +114 -0
  14. package/dist/cjs/logger.js +17 -0
  15. package/dist/cjs/main.js +87 -0
  16. package/dist/cjs/objects/ClassComponent.js +142 -0
  17. package/dist/cjs/objects/ExecutionScope.js +134 -0
  18. package/dist/cjs/objects/Frame.js +22 -0
  19. package/{src/objects/Net.ts → dist/cjs/objects/Net.js} +9 -24
  20. package/dist/cjs/objects/ParamDefinition.js +42 -0
  21. package/dist/cjs/objects/PinDefinition.js +31 -0
  22. package/dist/cjs/objects/PinTypes.js +11 -0
  23. package/dist/cjs/objects/Wire.js +9 -0
  24. package/dist/cjs/objects/types.js +9 -0
  25. package/dist/cjs/parser.js +299 -0
  26. package/dist/cjs/regenerate-tests.js +23 -0
  27. package/dist/cjs/render.js +155 -0
  28. package/{src/server.ts → dist/cjs/server.js} +15 -21
  29. package/dist/cjs/sizing.js +105 -0
  30. package/{src/utils.ts → dist/cjs/utils.js} +25 -35
  31. package/{src/visitor.ts → dist/cjs/visitor.js} +392 -948
  32. package/{build/src/antlr/CircuitScriptLexer.js → dist/esm/antlr/CircuitScriptLexer.mjs} +90 -91
  33. package/{build/src/antlr/CircuitScriptParser.js → dist/esm/antlr/CircuitScriptParser.mjs} +138 -136
  34. package/{build/src/draw_symbols.js → dist/esm/draw_symbols.mjs} +11 -11
  35. package/{build/src/execute.js → dist/esm/execute.mjs} +9 -8
  36. package/{build/src/export.js → dist/esm/export.mjs} +2 -2
  37. package/{build/src/geometry.js → dist/esm/geometry.mjs} +3 -7
  38. package/{build/src/helpers.js → dist/esm/helpers.mjs} +27 -7
  39. package/dist/esm/index.mjs +13 -0
  40. package/{build/src/layout.js → dist/esm/layout.mjs} +11 -11
  41. package/{build/src/lexer.js → dist/esm/lexer.mjs} +2 -2
  42. package/{build/src/main.js → dist/esm/main.mjs} +5 -5
  43. package/{build/src/objects/ClassComponent.js → dist/esm/objects/ClassComponent.mjs} +3 -3
  44. package/{build/src/objects/PinDefinition.js → dist/esm/objects/PinDefinition.mjs} +1 -1
  45. package/dist/esm/parser.mjs +269 -0
  46. package/{build/src/regenerate-tests.js → dist/esm/regenerate-tests.mjs} +1 -1
  47. package/{build/src/render.js → dist/esm/render.mjs} +7 -24
  48. package/{build/src/sizing.js → dist/esm/sizing.mjs} +22 -8
  49. package/{build/src/visitor.js → dist/esm/visitor.mjs} +10 -29
  50. package/dist/types/antlr/CircuitScriptLexer.d.ts +71 -0
  51. package/dist/types/antlr/CircuitScriptParser.d.ts +675 -0
  52. package/dist/types/antlr/CircuitScriptVisitor.d.ts +115 -0
  53. package/dist/types/draw_symbols.d.ts +162 -0
  54. package/dist/types/execute.d.ts +85 -0
  55. package/dist/types/export.d.ts +2 -0
  56. package/dist/types/fonts.d.ts +1 -0
  57. package/dist/types/geometry.d.ts +84 -0
  58. package/dist/types/globals.d.ts +50 -0
  59. package/dist/types/helpers.d.ts +9 -0
  60. package/dist/types/index.d.ts +13 -0
  61. package/dist/types/layout.d.ts +147 -0
  62. package/dist/types/lexer.d.ts +19 -0
  63. package/dist/types/logger.d.ts +6 -0
  64. package/dist/types/main.d.ts +2 -0
  65. package/dist/types/objects/ClassComponent.d.ts +40 -0
  66. package/dist/types/objects/ExecutionScope.d.ts +64 -0
  67. package/dist/types/objects/Frame.d.ts +15 -0
  68. package/dist/types/objects/Net.d.ts +10 -0
  69. package/dist/types/objects/ParamDefinition.d.ts +20 -0
  70. package/dist/types/objects/PinDefinition.d.ts +24 -0
  71. package/dist/types/objects/PinTypes.d.ts +7 -0
  72. package/dist/types/objects/Wire.d.ts +11 -0
  73. package/dist/types/objects/types.d.ts +49 -0
  74. package/dist/types/parser.d.ts +38 -0
  75. package/dist/types/regenerate-tests.d.ts +1 -0
  76. package/dist/types/render.d.ts +10 -0
  77. package/dist/types/server.d.ts +1 -0
  78. package/dist/types/sizing.d.ts +15 -0
  79. package/dist/types/utils.d.ts +19 -0
  80. package/dist/types/visitor.d.ts +135 -0
  81. package/package.json +30 -12
  82. package/.editorconfig +0 -15
  83. package/.eslintignore +0 -1
  84. package/.eslintrc.json +0 -27
  85. package/.gitlab-ci.yml +0 -81
  86. package/.prettierignore +0 -8
  87. package/.prettierrc +0 -16
  88. package/__tests__/expectedResults.ts +0 -657
  89. package/__tests__/helpers.ts +0 -82
  90. package/__tests__/parseScripts.ts +0 -593
  91. package/__tests__/renderData/script1.cst +0 -58
  92. package/__tests__/renderData/script1.cst.svg +0 -1
  93. package/__tests__/renderData/script2.cst +0 -16
  94. package/__tests__/renderData/script2.cst.svg +0 -1
  95. package/__tests__/renderData/script3.cst +0 -30
  96. package/__tests__/renderData/script3.cst.svg +0 -1
  97. package/__tests__/renderData/script4.cst +0 -54
  98. package/__tests__/renderData/script4.cst.svg +0 -1
  99. package/__tests__/renderData/script5.cst +0 -23
  100. package/__tests__/renderData/script5.cst.svg +0 -1
  101. package/__tests__/renderData/script6.cst +0 -28
  102. package/__tests__/renderData/script6.cst.svg +0 -1
  103. package/__tests__/renderData/script7.cst +0 -26
  104. package/__tests__/renderData/script7.cst.svg +0 -1
  105. package/__tests__/renderData/script8.cst +0 -37
  106. package/__tests__/renderData/script8.cst.svg +0 -1
  107. package/__tests__/testCLI.ts +0 -68
  108. package/__tests__/testMathOps.ts +0 -36
  109. package/__tests__/testMergeWires.ts +0 -141
  110. package/__tests__/testParse.ts +0 -263
  111. package/__tests__/testRender.ts +0 -38
  112. package/build/src/parser.js +0 -69
  113. package/documentation.md +0 -238
  114. package/examples/example_arduino_uno.cst +0 -1146
  115. package/examples/example_garden_pump.cst +0 -567
  116. package/examples/lib.cst +0 -185
  117. package/fonts/Inter-Bold.ttf +0 -0
  118. package/fonts/Inter-Regular.ttf +0 -0
  119. package/fonts/OpenSans-Regular.ttf +0 -0
  120. package/fonts/Roboto-Regular.ttf +0 -0
  121. package/jest.config.js +0 -23
  122. package/libs/lib.cst +0 -185
  123. package/refresh.html +0 -42
  124. package/server.cjs +0 -50
  125. package/src/antlr/CircuitScript.g4 +0 -209
  126. package/src/antlr/CircuitScriptLexer.ts +0 -317
  127. package/src/antlr/CircuitScriptParser.ts +0 -4979
  128. package/src/antlr/CircuitScriptVisitor.ts +0 -420
  129. package/src/execute.ts +0 -1227
  130. package/src/fonts.ts +0 -1
  131. package/src/geometry.ts +0 -638
  132. package/src/globals.ts +0 -67
  133. package/src/helpers.ts +0 -114
  134. package/src/lexer.ts +0 -151
  135. package/src/logger.ts +0 -17
  136. package/src/main.ts +0 -105
  137. package/src/objects/ClassComponent.ts +0 -223
  138. package/src/objects/ExecutionScope.ts +0 -201
  139. package/src/objects/Frame.ts +0 -20
  140. package/src/objects/ParamDefinition.ts +0 -49
  141. package/src/objects/PinDefinition.ts +0 -49
  142. package/src/objects/PinTypes.ts +0 -7
  143. package/src/objects/Wire.ts +0 -19
  144. package/src/objects/types.ts +0 -66
  145. package/src/parser.ts +0 -106
  146. package/src/regenerate-tests.ts +0 -25
  147. package/src/render.ts +0 -260
  148. package/src/sizing.ts +0 -96
  149. package/tsconfig.json +0 -26
  150. package/tsconfig.release.json +0 -8
  151. /package/{build/src/antlr/CircuitScriptVisitor.js → dist/esm/antlr/CircuitScriptVisitor.mjs} +0 -0
  152. /package/{build/src/fonts.js → dist/esm/fonts.mjs} +0 -0
  153. /package/{build/src/globals.js → dist/esm/globals.mjs} +0 -0
  154. /package/{build/src/logger.js → dist/esm/logger.mjs} +0 -0
  155. /package/{build/src/objects/ExecutionScope.js → dist/esm/objects/ExecutionScope.mjs} +0 -0
  156. /package/{build/src/objects/Frame.js → dist/esm/objects/Frame.mjs} +0 -0
  157. /package/{build/src/objects/Net.js → dist/esm/objects/Net.mjs} +0 -0
  158. /package/{build/src/objects/ParamDefinition.js → dist/esm/objects/ParamDefinition.mjs} +0 -0
  159. /package/{build/src/objects/PinTypes.js → dist/esm/objects/PinTypes.mjs} +0 -0
  160. /package/{build/src/objects/Wire.js → dist/esm/objects/Wire.mjs} +0 -0
  161. /package/{build/src/objects/types.js → dist/esm/objects/types.mjs} +0 -0
  162. /package/{build/src/server.js → dist/esm/server.mjs} +0 -0
  163. /package/{build/src/utils.js → dist/esm/utils.mjs} +0 -0
@@ -1,6 +1,5 @@
1
- import { SymbolPinSide, defaultFont } from "./globals.js";
2
- import { Geometry, GeometryProp, HorizontalAlign, Label, VerticalAlign } from "./geometry.js";
3
- const defaultSymbolLineColor = '#333';
1
+ import { SymbolPinSide, defaultFont } from "./globals.mjs";
2
+ import { Geometry, GeometryProp, HorizontalAlign, Label, VerticalAlign } from "./geometry.mjs";
4
3
  const defaultSymbolLineWidth = 2;
5
4
  export class SymbolGraphic {
6
5
  drawPortsName = true;
@@ -56,13 +55,11 @@ export class SymbolGraphic {
56
55
  const [x, y] = pin.start;
57
56
  const useX = Math.round(x * 10000) / 10000;
58
57
  const useY = Math.round(y * 10000 / 10000);
59
- if (pin) {
60
- return {
61
- x: useX,
62
- y: useY,
63
- angle: pin.angle,
64
- };
65
- }
58
+ return {
59
+ x: useX,
60
+ y: useY,
61
+ angle: pin.angle,
62
+ };
66
63
  }
67
64
  drawBounds(group) {
68
65
  const bbox = this.drawing.getBoundingBox();
@@ -191,6 +188,7 @@ export class SymbolGraphic {
191
188
  if (this.labelTexts.has(labelId)) {
192
189
  return this.labelTexts.get(labelId);
193
190
  }
191
+ return undefined;
194
192
  }
195
193
  }
196
194
  export function SymbolFactory(name) {
@@ -198,6 +196,7 @@ export function SymbolFactory(name) {
198
196
  case 'point':
199
197
  return new SymbolPointHidden();
200
198
  }
199
+ return null;
201
200
  }
202
201
  export class SymbolPointHidden extends SymbolGraphic {
203
202
  generateDrawing() {
@@ -723,7 +722,7 @@ export class SymbolDrawing {
723
722
  return item[0] === pinId;
724
723
  });
725
724
  if (pin) {
726
- const [pinId, feature, angle] = pin;
725
+ const [, feature, angle] = pin;
727
726
  const withAngle = Geometry.rotateDegs(feature, this.angle, this.mainOrigin);
728
727
  const coords = Geometry.getCoords(withAngle);
729
728
  return {
@@ -732,6 +731,7 @@ export class SymbolDrawing {
732
731
  angle,
733
732
  };
734
733
  }
734
+ return null;
735
735
  }
736
736
  }
737
737
  export class SymbolDrawingCommands extends SymbolDrawing {
@@ -1,10 +1,10 @@
1
- import { BlockTypes, ComponentTypes, GlobalNames, NoNetText, ParamKeys, ReferenceTypes } from './globals.js';
2
- import { ClassComponent } from './objects/ClassComponent.js';
3
- import { ActiveObject, ExecutionScope, FrameAction, SequenceAction } from './objects/ExecutionScope.js';
4
- import { Net } from './objects/Net.js';
5
- import { PortSide } from './objects/PinDefinition.js';
6
- import { Wire } from './objects/Wire.js';
7
- import { Frame } from './objects/Frame.js';
1
+ import { BlockTypes, ComponentTypes, GlobalNames, NoNetText, ParamKeys, ReferenceTypes } from './globals.mjs';
2
+ import { ClassComponent } from './objects/ClassComponent.mjs';
3
+ import { ActiveObject, ExecutionScope, FrameAction, SequenceAction } from './objects/ExecutionScope.mjs';
4
+ import { Net } from './objects/Net.mjs';
5
+ import { PortSide } from './objects/PinDefinition.mjs';
6
+ import { Wire } from './objects/Wire.mjs';
7
+ import { Frame } from './objects/Frame.mjs';
8
8
  export class ExecutionContext {
9
9
  name;
10
10
  namespace;
@@ -118,7 +118,7 @@ export class ExecutionContext {
118
118
  }
119
119
  mergeNets(net1, net2) {
120
120
  if (net1 === net2) {
121
- return;
121
+ return net1;
122
122
  }
123
123
  let tmpNet;
124
124
  if (net2.priority > net1.priority) {
@@ -519,6 +519,7 @@ export class ExecutionContext {
519
519
  const currentComponent = this.scope.currentComponent;
520
520
  const currentPin = this.scope.currentPin;
521
521
  const currentWireId = this.scope.currentWireId;
522
+ const gndCopyIdOffset = 0;
522
523
  const tmpInstances = childScope.instances;
523
524
  const tmpNets = childScope.getNets();
524
525
  for (const [instanceName, component] of tmpInstances) {
@@ -1,5 +1,5 @@
1
- import { ComponentTypes, NoNetText } from "./globals.js";
2
- import { NumericValue } from "./objects/ParamDefinition.js";
1
+ import { ComponentTypes, NoNetText } from "./globals.mjs";
2
+ import { NumericValue } from "./objects/ParamDefinition.mjs";
3
3
  export function generateKiCADNetList(netlist) {
4
4
  const componentsList = [];
5
5
  const nets = {};
@@ -1,7 +1,7 @@
1
1
  import Flatten from '@flatten-js/core';
2
- import { measureTextSize2 } from './sizing.js';
3
- import { defaultFont } from './globals.js';
4
- import { NumericValue } from './objects/ParamDefinition.js';
2
+ import { measureTextSize2 } from './sizing.mjs';
3
+ import { defaultFont } from './globals.mjs';
4
+ import { NumericValue } from './objects/ParamDefinition.mjs';
5
5
  export class Label extends Flatten.Polygon {
6
6
  id;
7
7
  text;
@@ -418,10 +418,6 @@ export var VerticalAlign;
418
418
  VerticalAlign["Middle"] = "middle";
419
419
  VerticalAlign["Bottom"] = "bottom";
420
420
  })(VerticalAlign || (VerticalAlign = {}));
421
- function getArcPoint(centerX, centerY, radius, angleDegrees) {
422
- const angleRads = angleDegrees * Math.PI / 180;
423
- return getArcPointRadians(centerX, centerY, radius, angleRads);
424
- }
425
421
  function getArcPointRadians(centerX, centerY, radius, angleRads) {
426
422
  const dx = Math.cos(angleRads);
427
423
  const dy = Math.sin(angleRads);
@@ -1,11 +1,17 @@
1
1
  import { writeFileSync } from "fs";
2
- import { generateKiCADNetList } from "./export.js";
3
- import { LayoutEngine } from "./layout.js";
4
- import { SequenceAction } from "./objects/ExecutionScope.js";
5
- import { parseFileWithVisitor } from "./parser.js";
6
- import { generateSVG2 } from "./render.js";
7
- import { SimpleStopwatch } from "./utils.js";
8
- import { MainVisitor } from "./visitor.js";
2
+ import { generateKiCADNetList } from "./export.mjs";
3
+ import { LayoutEngine } from "./layout.mjs";
4
+ import { SequenceAction } from "./objects/ExecutionScope.mjs";
5
+ import { parseFileWithVisitor } from "./parser.mjs";
6
+ import { generateSVG2 } from "./render.mjs";
7
+ import { SimpleStopwatch } from "./utils.mjs";
8
+ import { MainVisitor } from "./visitor.mjs";
9
+ import { createContext } from "this-file";
10
+ export var JSModuleType;
11
+ (function (JSModuleType) {
12
+ JSModuleType["CommonJs"] = "cjs";
13
+ JSModuleType["ESM"] = "mjs";
14
+ })(JSModuleType || (JSModuleType = {}));
9
15
  export function renderScript(scriptData, outputPath, options) {
10
16
  const { currentDirectory = null, defaultLibsPath, dumpNets = false, dumpData = false, kicadNetlistPath = null, showStats = false } = options;
11
17
  const visitor = new MainVisitor(true);
@@ -71,3 +77,17 @@ export function renderScript(scriptData, outputPath, options) {
71
77
  }
72
78
  return svgOutput;
73
79
  }
80
+ export function detectJSModuleType() {
81
+ if (typeof __filename === 'undefined' &&
82
+ typeof __dirname === 'undefined') {
83
+ return JSModuleType.ESM;
84
+ }
85
+ else {
86
+ return JSModuleType.CommonJs;
87
+ }
88
+ }
89
+ const context = createContext();
90
+ export function getCurrentPath() {
91
+ const filename = context.filename;
92
+ return { filePath: filename };
93
+ }
@@ -0,0 +1,13 @@
1
+ export * from './draw_symbols.mjs';
2
+ export * from './execute.mjs';
3
+ export * from './export.mjs';
4
+ export * from './geometry.mjs';
5
+ export * from './globals.mjs';
6
+ export * from './helpers.mjs';
7
+ export * from './layout.mjs';
8
+ export * from './lexer.mjs';
9
+ export * from './logger.mjs';
10
+ export * from './parser.mjs';
11
+ export * from './render.mjs';
12
+ export * from './utils.mjs';
13
+ export * from './visitor.mjs';
@@ -1,12 +1,12 @@
1
- import graphlib from '@dagrejs/graphlib';
2
- import { SymbolCustom, SymbolDrawing, SymbolFactory, SymbolPlaceholder, SymbolText } from "./draw_symbols.js";
3
- import { FrameAction, SequenceAction } from "./objects/ExecutionScope.js";
4
- import { GlobalNames, ParamKeys } from './globals.js';
5
- import { NumericValue } from './objects/ParamDefinition.js';
6
- import { Geometry } from './geometry.js';
7
- import { Logger } from './logger.js';
8
- import { Frame, FrameParamKeys, FramePlotDirection } from './objects/Frame.js';
9
- import { getBoundsSize, printBounds, resizeBounds, resizeToNearestGrid, toNearestGrid } from './utils.js';
1
+ import { Graph, alg } from '@dagrejs/graphlib';
2
+ import { SymbolCustom, SymbolDrawing, SymbolFactory, SymbolPlaceholder, SymbolText } from "./draw_symbols.mjs";
3
+ import { FrameAction, SequenceAction } from "./objects/ExecutionScope.mjs";
4
+ import { GlobalNames, ParamKeys } from './globals.mjs';
5
+ import { NumericValue } from './objects/ParamDefinition.mjs';
6
+ import { Geometry } from './geometry.mjs';
7
+ import { Logger } from './logger.mjs';
8
+ import { Frame, FrameParamKeys, FramePlotDirection } from './objects/Frame.mjs';
9
+ import { getBoundsSize, printBounds, resizeBounds, resizeToNearestGrid, toNearestGrid } from './utils.mjs';
10
10
  export class LayoutEngine {
11
11
  logger;
12
12
  placeSubgraphVersion = 2;
@@ -350,7 +350,7 @@ export class LayoutEngine {
350
350
  generateLayoutGraph(sequence, nets) {
351
351
  let previousNode = null;
352
352
  let previousPin = null;
353
- const graph = new graphlib.Graph({
353
+ const graph = new Graph({
354
354
  directed: false,
355
355
  compound: true,
356
356
  });
@@ -498,7 +498,7 @@ export class LayoutEngine {
498
498
  graph.setEdge(node1, node2, edgeValue);
499
499
  }
500
500
  sizeSubGraphs(graph) {
501
- const subGraphs = graphlib.alg.components(graph);
501
+ const subGraphs = alg.components(graph);
502
502
  const subGraphsStarts = [];
503
503
  this.print('===== placing subgraphs =====');
504
504
  this.print('number of subgraphs: ', subGraphs.length);
@@ -1,6 +1,6 @@
1
1
  import { CommonToken } from "antlr4";
2
- import CircuitScriptParser from "./antlr/CircuitScriptParser.js";
3
- import CircuitScriptLexer from "./antlr/CircuitScriptLexer.js";
2
+ import CircuitScriptParser from "./antlr/CircuitScriptParser.mjs";
3
+ import CircuitScriptLexer from "./antlr/CircuitScriptLexer.mjs";
4
4
  export class MainLexer extends CircuitScriptLexer {
5
5
  tokens;
6
6
  indents;
@@ -2,12 +2,12 @@
2
2
  import { program } from 'commander';
3
3
  import figlet from 'figlet';
4
4
  import path from 'path';
5
- import { fileURLToPath } from 'url';
6
5
  import { readFileSync, watch } from 'fs';
7
- import { prepareSizing } from './sizing.js';
8
- import { renderScript } from './helpers.js';
6
+ import { prepareSVGEnvironment } from './sizing.mjs';
7
+ import { getCurrentPath, renderScript } from './helpers.mjs';
9
8
  export default async function main() {
10
- const toolSrcPath = fileURLToPath(import.meta.url);
9
+ const { filePath } = getCurrentPath();
10
+ const toolSrcPath = filePath;
11
11
  const toolDirectory = path.dirname(toolSrcPath) + '/../../';
12
12
  const fontsPath = toolDirectory + '/fonts';
13
13
  const defaultLibsPath = toolDirectory + '/libs';
@@ -43,7 +43,7 @@ export default async function main() {
43
43
  if (watchFileChanges) {
44
44
  console.log('watching for file changes...');
45
45
  }
46
- await prepareSizing(fontsPath);
46
+ await prepareSVGEnvironment(fontsPath);
47
47
  let inputFilePath = null;
48
48
  let scriptData;
49
49
  if (options.input) {
@@ -1,6 +1,6 @@
1
- import { SymbolDrawingCommands } from '../draw_symbols.js';
2
- import { PinDefinition, PinIdType } from './PinDefinition.js';
3
- import { PinTypes } from './PinTypes.js';
1
+ import { SymbolDrawingCommands } from '../draw_symbols.mjs';
2
+ import { PinDefinition, PinIdType } from './PinDefinition.mjs';
3
+ import { PinTypes } from './PinTypes.mjs';
4
4
  export class ClassComponent {
5
5
  instanceName;
6
6
  numPins;
@@ -1,4 +1,4 @@
1
- import { PinTypes } from './PinTypes.js';
1
+ import { PinTypes } from './PinTypes.mjs';
2
2
  export class PinDefinition {
3
3
  id;
4
4
  idType;
@@ -0,0 +1,269 @@
1
+ import { CharStream, CommonTokenStream, ErrorListener, ParseTreeVisitor } from 'antlr4';
2
+ import CircuitScriptParser, { Atom_exprContext, Create_component_exprContext, Create_graphic_exprContext, Function_def_exprContext, Property_key_exprContext, Sub_exprContext } from './antlr/CircuitScriptParser.mjs';
3
+ import { MainLexer } from './lexer.mjs';
4
+ import { SimpleStopwatch } from './utils.mjs';
5
+ export function parseFileWithVisitor(visitor, data) {
6
+ const chars = new CharStream(data);
7
+ const lexer = new MainLexer(chars);
8
+ const lexerTimer = new SimpleStopwatch();
9
+ const tokens = new CommonTokenStream(lexer);
10
+ tokens.fill();
11
+ const lexerTimeTaken = lexerTimer.lap();
12
+ const parserTimer = new SimpleStopwatch();
13
+ const parser = new CircuitScriptParser(tokens);
14
+ parser.removeErrorListeners();
15
+ const errorListener = new CircuitscriptParserErrorListener();
16
+ parser.addErrorListener(errorListener);
17
+ const tree = parser.script();
18
+ let hasError = false;
19
+ try {
20
+ visitor.visit(tree);
21
+ }
22
+ catch (err) {
23
+ err.print(data);
24
+ hasError = true;
25
+ }
26
+ const parserTimeTaken = parserTimer.lap();
27
+ const semanticTokenVisitor = new SemanticTokensVisitor(lexer, data);
28
+ semanticTokenVisitor.visit(tree);
29
+ const semanticTokens = semanticTokenVisitor.semanticTokens;
30
+ const parsedTokens = prepareTokens(tokens.tokens, lexer, data);
31
+ const finalParsedTokens = [];
32
+ parsedTokens.forEach(token => {
33
+ const location = `${token.line}_${token.column}`;
34
+ if (semanticTokens.has(location)) {
35
+ finalParsedTokens.push(semanticTokens.get(location));
36
+ }
37
+ else {
38
+ finalParsedTokens.push(token);
39
+ }
40
+ });
41
+ return {
42
+ tree, parser,
43
+ hasParseError: errorListener.hasParseErrors(),
44
+ hasError,
45
+ parserTimeTaken,
46
+ lexerTimeTaken,
47
+ tokens: finalParsedTokens,
48
+ };
49
+ }
50
+ export class SemanticTokensVisitor extends ParseTreeVisitor {
51
+ parsedTokens = [];
52
+ lexer;
53
+ script;
54
+ semanticTokens = new Map();
55
+ constructor(lexer, script) {
56
+ super();
57
+ this.lexer = lexer;
58
+ this.script = script;
59
+ }
60
+ visit(ctx) {
61
+ const here = this;
62
+ if (Array.isArray(ctx)) {
63
+ return ctx.map(function (child) {
64
+ try {
65
+ here.checkContext(child);
66
+ return child.accept(this);
67
+ }
68
+ catch (err) {
69
+ this.handleError(child, err);
70
+ }
71
+ }, this);
72
+ }
73
+ else {
74
+ try {
75
+ this.checkContext(ctx);
76
+ return ctx.accept(this);
77
+ }
78
+ catch (err) {
79
+ this.handleError(ctx, err);
80
+ }
81
+ }
82
+ }
83
+ handleError(ctx, err) {
84
+ console.log('error!', err);
85
+ }
86
+ checkContext(ctx) {
87
+ if (ctx instanceof Function_def_exprContext) {
88
+ this.addSemanticToken(this.parseToken(ctx.ID(), ['declaration'], 'function'));
89
+ }
90
+ else if (ctx instanceof Create_component_exprContext
91
+ || ctx instanceof Create_graphic_exprContext) {
92
+ this.addSemanticToken(this.parseToken(ctx.Create(), ['defaultLibrary'], 'function'));
93
+ }
94
+ else if (ctx instanceof Atom_exprContext) {
95
+ if (ctx.ID()) {
96
+ if (ctx.trailer_expr_list().length > 0) {
97
+ this.addSemanticToken(this.parseToken(ctx.ID(), ['declaration'], 'function'));
98
+ }
99
+ else {
100
+ this.addSemanticToken(this.parseToken(ctx.ID(), ['declaration'], 'variable'));
101
+ }
102
+ }
103
+ }
104
+ else if (ctx instanceof Property_key_exprContext) {
105
+ let useToken = null;
106
+ if (ctx.ID()) {
107
+ useToken = ctx.ID();
108
+ }
109
+ else if (ctx.INTEGER_VALUE()) {
110
+ useToken = ctx.INTEGER_VALUE();
111
+ }
112
+ else if (ctx.STRING_VALUE()) {
113
+ useToken = ctx.STRING_VALUE();
114
+ }
115
+ useToken && this.addSemanticToken(this.parseToken(useToken, ['declaration'], 'property'));
116
+ }
117
+ else if (ctx instanceof Sub_exprContext) {
118
+ let useToken = null;
119
+ if (ctx.ID()) {
120
+ useToken = ctx.ID();
121
+ }
122
+ else if (ctx.Pin()) {
123
+ useToken = ctx.Pin();
124
+ }
125
+ useToken && this.addSemanticToken(this.parseToken(useToken, ['defaultLibrary'], 'function'));
126
+ }
127
+ }
128
+ addSemanticToken(parsedToken) {
129
+ this.semanticTokens.set(parsedToken.line + "_" + parsedToken.column, parsedToken);
130
+ }
131
+ parseToken(node, modifiers, tokenType = null) {
132
+ const token = node.symbol;
133
+ let stringValue = "";
134
+ let textPart = "";
135
+ if (this.lexer.symbolicNames[token.type] !== null && this.lexer.symbolicNames[token.type] !== undefined) {
136
+ stringValue = this.lexer.symbolicNames[token.type];
137
+ if (stringValue !== "NEWLINE") {
138
+ textPart = this.script.substring(token.start, token.stop + 1);
139
+ }
140
+ else {
141
+ textPart = token.text.length - 1;
142
+ }
143
+ }
144
+ else if (this.lexer.literalNames[token.type] !== null && this.lexer.literalNames[token.type] !== undefined) {
145
+ stringValue = this.lexer.literalNames[token.type];
146
+ textPart = this.script.substring(token.start, token.stop + 1);
147
+ }
148
+ else {
149
+ stringValue = token._text;
150
+ }
151
+ return {
152
+ line: token.line,
153
+ column: token.column,
154
+ length: token.stop - token.start + 1,
155
+ tokenType: tokenType !== null ? tokenType : stringValue,
156
+ tokenModifiers: modifiers,
157
+ textValue: textPart,
158
+ };
159
+ }
160
+ }
161
+ function prepareTokens(tokens, lexer, script) {
162
+ const parsedTokens = [];
163
+ tokens.forEach(item => {
164
+ if (item.type !== -1) {
165
+ let stringValue = "";
166
+ let textPart = "";
167
+ if (lexer.symbolicNames[item.type] !== null && lexer.symbolicNames[item.type] !== undefined) {
168
+ stringValue = lexer.symbolicNames[item.type];
169
+ if (stringValue !== "NEWLINE") {
170
+ textPart = script.substring(item.start, item.stop + 1);
171
+ }
172
+ else {
173
+ textPart = item.text.length - 1;
174
+ }
175
+ }
176
+ else if (lexer.literalNames[item.type] !== null && lexer.literalNames[item.type] !== undefined) {
177
+ stringValue = lexer.literalNames[item.type];
178
+ textPart = script.substring(item.start, item.stop + 1);
179
+ }
180
+ else {
181
+ stringValue = item._text;
182
+ }
183
+ if (textPart !== 0 && textPart !== '') {
184
+ parsedTokens.push({
185
+ line: item.line,
186
+ column: item.column,
187
+ length: item.stop - item.start + 1,
188
+ tokenType: resolveTokenType(stringValue),
189
+ tokenModifiers: resolveTokenModifiers(stringValue),
190
+ textValue: textPart,
191
+ });
192
+ }
193
+ }
194
+ });
195
+ return parsedTokens;
196
+ }
197
+ const languageKeywords = [
198
+ 'break', 'branch', 'create', 'component',
199
+ 'graphic', 'wire', 'pin', 'add', 'at', 'to',
200
+ 'point', 'join', 'parallel', 'return', 'def', 'import',
201
+ 'true', 'false', 'nc', 'frame',
202
+ ];
203
+ const operatorKeywords = [
204
+ 'at', 'to', 'wire', 'add', 'frame', 'join', 'parallel', 'point'
205
+ ];
206
+ function resolveTokenType(tokenType) {
207
+ if (operatorKeywords.indexOf(tokenType.toLowerCase()) !== -1) {
208
+ return 'graphKeyword';
209
+ }
210
+ else if (languageKeywords.indexOf(tokenType.toLowerCase()) !== -1) {
211
+ return 'keyword';
212
+ }
213
+ else {
214
+ switch (tokenType) {
215
+ case 'INTEGER_VALUE':
216
+ case 'NUMERIC_VALUE':
217
+ case 'DECIMAL_VALUE':
218
+ case 'PERCENTAGE_VALUE':
219
+ return 'number';
220
+ case 'STRING_VALUE':
221
+ return 'string';
222
+ case 'ID':
223
+ return 'variable';
224
+ case 'Define':
225
+ return 'keyword';
226
+ case 'COMMENT':
227
+ return 'comment';
228
+ }
229
+ return null;
230
+ }
231
+ }
232
+ function resolveTokenModifiers(tokenType) {
233
+ return [];
234
+ }
235
+ function dumpTokens(tokens, lexer, scriptData) {
236
+ tokens.forEach(item => {
237
+ if (item.type !== -1) {
238
+ let stringValue = "";
239
+ let textPart = "";
240
+ if (lexer.symbolicNames[item.type] !== null && lexer.symbolicNames[item.type] !== undefined) {
241
+ stringValue = lexer.symbolicNames[item.type];
242
+ if (stringValue !== "NEWLINE") {
243
+ textPart = scriptData.substring(item.start, item.stop + 1);
244
+ }
245
+ else {
246
+ textPart = item.text.length - 1;
247
+ }
248
+ }
249
+ else if (lexer.literalNames[item.type] !== null && lexer.literalNames[item.type] !== undefined) {
250
+ stringValue = lexer.literalNames[item.type];
251
+ textPart = scriptData.substring(item.start, item.stop + 1);
252
+ }
253
+ else {
254
+ stringValue = item._text;
255
+ }
256
+ console.log('line', item.line + ':' + item.column, `\t${stringValue} (${item.type})`.padEnd(30), textPart);
257
+ }
258
+ });
259
+ }
260
+ export class CircuitscriptParserErrorListener extends ErrorListener {
261
+ syntaxErrorCounter = 0;
262
+ syntaxError(recognizer, offendingSymbol, line, column, msg, e) {
263
+ console.log("Syntax error at line", line, ':', column, ' - ', msg);
264
+ this.syntaxErrorCounter++;
265
+ }
266
+ hasParseErrors() {
267
+ return (this.syntaxErrorCounter > 0);
268
+ }
269
+ }
@@ -1,5 +1,5 @@
1
1
  import fs from 'fs';
2
- import { renderScript } from './helpers.js';
2
+ import { renderScript } from './helpers.mjs';
3
3
  const mainDir = './__tests__/renderData/';
4
4
  const cstFiles = [];
5
5
  const files = fs.readdirSync(mainDir);
@@ -1,12 +1,11 @@
1
- import { createSVGWindow } from 'svgdom';
2
1
  import { SVG, registerWindow } from '@svgdotjs/svg.js';
3
- import { RenderFrameType, getBounds } from "./layout.js";
4
- import { applyFontsToSVG } from './sizing.js';
5
- import { ParamKeys, bodyColor, junctionColor, junctionSize, wireColor } from './globals.js';
6
- import { NumericValue } from './objects/ParamDefinition.js';
7
- import { getBoundsSize } from './utils.js';
2
+ import { RenderFrameType, getBounds } from "./layout.mjs";
3
+ import { applyFontsToSVG, getCreateSVGWindow } from './sizing.mjs';
4
+ import { ParamKeys, bodyColor, junctionColor, junctionSize, wireColor } from './globals.mjs';
5
+ import { NumericValue } from './objects/ParamDefinition.mjs';
6
+ import { getBoundsSize } from './utils.mjs';
8
7
  export function generateSVG2(graph) {
9
- const window = createSVGWindow();
8
+ const window = getCreateSVGWindow()();
10
9
  const document = window.document;
11
10
  registerWindow(window, document);
12
11
  const canvas = SVG(document.documentElement);
@@ -36,7 +35,7 @@ function generateSVGChild(canvas, components, wires, junctions, mergedWires, fra
36
35
  symbolGroup.translate(x, y);
37
36
  const { symbol = null } = item;
38
37
  if (symbol !== null && symbol) {
39
- let extra = {};
38
+ const extra = {};
40
39
  if (item.component.parameters.has('__is_net')) {
41
40
  extra.net_name = item.component.parameters.get(ParamKeys.net_name);
42
41
  }
@@ -150,19 +149,3 @@ function drawGrid(group, canvasSize) {
150
149
  color: '#aaa'
151
150
  });
152
151
  }
153
- function calculateBoundingBox(components) {
154
- let maxX = Number.NEGATIVE_INFINITY;
155
- let minX = Number.POSITIVE_INFINITY;
156
- let maxY = Number.NEGATIVE_INFINITY;
157
- let minY = Number.POSITIVE_INFINITY;
158
- components.forEach(item => {
159
- minX = Math.min(item.x, minX);
160
- maxX = Math.max(item.x + item.width, maxX);
161
- minY = Math.min(item.y, minY);
162
- maxY = Math.max(item.y + item.height, maxY);
163
- });
164
- return {
165
- width: (maxX - minX),
166
- height: (maxY - minY)
167
- };
168
- }
@@ -1,13 +1,27 @@
1
1
  import { SVG, registerWindow } from '@svgdotjs/svg.js';
2
- import { config, createSVGWindow } from 'svgdom';
3
- import { HorizontalAlign, VerticalAlign } from './geometry.js';
4
- import { defaultFont } from './globals.js';
2
+ import { HorizontalAlign, VerticalAlign } from './geometry.mjs';
3
+ import { defaultFont } from './globals.mjs';
4
+ import { JSModuleType, detectJSModuleType } from './helpers.mjs';
5
5
  let MainCanvas = null;
6
6
  const supportedFonts = {};
7
- export async function prepareSizing(fontsPath) {
8
- await config.setFontDir(fontsPath)
9
- .setFontFamilyMappings(supportedFonts)
10
- .preloadFonts();
7
+ let globalCreateSVGWindow;
8
+ export async function prepareSVGEnvironment(fontsPath) {
9
+ const moduleType = detectJSModuleType();
10
+ if (moduleType === JSModuleType.CommonJs) {
11
+ const { config, createSVGWindow } = await import('svgdom');
12
+ globalCreateSVGWindow = createSVGWindow;
13
+ if (fontsPath !== null) {
14
+ await config.setFontDir(fontsPath)
15
+ .setFontFamilyMappings(supportedFonts)
16
+ .preloadFonts();
17
+ }
18
+ }
19
+ }
20
+ export function getCreateSVGWindow() {
21
+ if (globalCreateSVGWindow === undefined) {
22
+ throw "SVG environment is not set up yet";
23
+ }
24
+ return globalCreateSVGWindow;
11
25
  }
12
26
  export function applyFontsToSVG(canvas) {
13
27
  }
@@ -18,7 +32,7 @@ const measureTextSizeCache = {};
18
32
  const measureTextSizeCacheHits = {};
19
33
  export function measureTextSize2(text, fontFamily, fontSize, fontWeight = 'regular', anchor = HorizontalAlign.Left, vanchor = VerticalAlign.Bottom) {
20
34
  if (MainCanvas === null) {
21
- const window = createSVGWindow();
35
+ const window = getCreateSVGWindow()();
22
36
  const { document } = window;
23
37
  registerWindow(window, document);
24
38
  MainCanvas = SVG(document.documentElement);