circuitscript 0.0.22 → 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 (164) 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/{build/src → dist/types}/antlr/CircuitScriptLexer.d.ts +23 -23
  51. package/{build/src → dist/types}/antlr/CircuitScriptParser.d.ts +24 -23
  52. package/{build/src → dist/types}/draw_symbols.d.ts +2 -2
  53. package/{build/src → dist/types}/execute.d.ts +2 -2
  54. package/{build/src → dist/types}/geometry.d.ts +1 -1
  55. package/dist/types/helpers.d.ts +9 -0
  56. package/dist/types/index.d.ts +13 -0
  57. package/{build/src → dist/types}/layout.d.ts +10 -10
  58. package/{build/src → dist/types}/objects/ClassComponent.d.ts +2 -2
  59. package/{build/src → dist/types}/objects/PinDefinition.d.ts +1 -1
  60. package/dist/types/parser.d.ts +38 -0
  61. package/{build/src → dist/types}/render.d.ts +1 -1
  62. package/{build/src → dist/types}/sizing.d.ts +3 -1
  63. package/{build/src → dist/types}/visitor.d.ts +5 -3
  64. package/package.json +30 -12
  65. package/.editorconfig +0 -15
  66. package/.eslintignore +0 -1
  67. package/.eslintrc.json +0 -27
  68. package/.gitlab-ci.yml +0 -81
  69. package/.prettierignore +0 -8
  70. package/.prettierrc +0 -16
  71. package/__tests__/expectedResults.ts +0 -657
  72. package/__tests__/helpers.ts +0 -82
  73. package/__tests__/parseScripts.ts +0 -593
  74. package/__tests__/renderData/script1.cst +0 -58
  75. package/__tests__/renderData/script1.cst.svg +0 -1
  76. package/__tests__/renderData/script2.cst +0 -16
  77. package/__tests__/renderData/script2.cst.svg +0 -1
  78. package/__tests__/renderData/script3.cst +0 -30
  79. package/__tests__/renderData/script3.cst.svg +0 -1
  80. package/__tests__/renderData/script4.cst +0 -54
  81. package/__tests__/renderData/script4.cst.svg +0 -1
  82. package/__tests__/renderData/script5.cst +0 -23
  83. package/__tests__/renderData/script5.cst.svg +0 -1
  84. package/__tests__/renderData/script6.cst +0 -28
  85. package/__tests__/renderData/script6.cst.svg +0 -1
  86. package/__tests__/renderData/script7.cst +0 -26
  87. package/__tests__/renderData/script7.cst.svg +0 -1
  88. package/__tests__/renderData/script8.cst +0 -37
  89. package/__tests__/renderData/script8.cst.svg +0 -1
  90. package/__tests__/testCLI.ts +0 -68
  91. package/__tests__/testMathOps.ts +0 -36
  92. package/__tests__/testMergeWires.ts +0 -141
  93. package/__tests__/testParse.ts +0 -263
  94. package/__tests__/testRender.ts +0 -38
  95. package/build/src/helpers.d.ts +0 -1
  96. package/build/src/parser.js +0 -69
  97. package/documentation.md +0 -238
  98. package/examples/example_arduino_uno.cst +0 -1146
  99. package/examples/example_garden_pump.cst +0 -567
  100. package/examples/lib.cst +0 -185
  101. package/fonts/Inter-Bold.ttf +0 -0
  102. package/fonts/Inter-Regular.ttf +0 -0
  103. package/fonts/OpenSans-Regular.ttf +0 -0
  104. package/fonts/Roboto-Regular.ttf +0 -0
  105. package/jest.config.js +0 -23
  106. package/libs/lib.cst +0 -185
  107. package/refresh.html +0 -42
  108. package/server.cjs +0 -50
  109. package/src/antlr/CircuitScript.g4 +0 -209
  110. package/src/antlr/CircuitScriptLexer.ts +0 -317
  111. package/src/antlr/CircuitScriptParser.ts +0 -4979
  112. package/src/antlr/CircuitScriptVisitor.ts +0 -420
  113. package/src/execute.ts +0 -1227
  114. package/src/fonts.ts +0 -1
  115. package/src/geometry.ts +0 -638
  116. package/src/globals.ts +0 -67
  117. package/src/helpers.ts +0 -114
  118. package/src/lexer.ts +0 -151
  119. package/src/logger.ts +0 -17
  120. package/src/main.ts +0 -105
  121. package/src/objects/ClassComponent.ts +0 -223
  122. package/src/objects/ExecutionScope.ts +0 -201
  123. package/src/objects/Frame.ts +0 -20
  124. package/src/objects/ParamDefinition.ts +0 -49
  125. package/src/objects/PinDefinition.ts +0 -49
  126. package/src/objects/PinTypes.ts +0 -7
  127. package/src/objects/Wire.ts +0 -19
  128. package/src/objects/types.ts +0 -66
  129. package/src/parser.ts +0 -106
  130. package/src/regenerate-tests.ts +0 -25
  131. package/src/render.ts +0 -260
  132. package/src/sizing.ts +0 -96
  133. package/tsconfig.json +0 -27
  134. package/tsconfig.release.json +0 -8
  135. /package/{build/src/antlr/CircuitScriptVisitor.js → dist/esm/antlr/CircuitScriptVisitor.mjs} +0 -0
  136. /package/{build/src/fonts.js → dist/esm/fonts.mjs} +0 -0
  137. /package/{build/src/globals.js → dist/esm/globals.mjs} +0 -0
  138. /package/{build/src/logger.js → dist/esm/logger.mjs} +0 -0
  139. /package/{build/src/objects/ExecutionScope.js → dist/esm/objects/ExecutionScope.mjs} +0 -0
  140. /package/{build/src/objects/Frame.js → dist/esm/objects/Frame.mjs} +0 -0
  141. /package/{build/src/objects/Net.js → dist/esm/objects/Net.mjs} +0 -0
  142. /package/{build/src/objects/ParamDefinition.js → dist/esm/objects/ParamDefinition.mjs} +0 -0
  143. /package/{build/src/objects/PinTypes.js → dist/esm/objects/PinTypes.mjs} +0 -0
  144. /package/{build/src/objects/Wire.js → dist/esm/objects/Wire.mjs} +0 -0
  145. /package/{build/src/objects/types.js → dist/esm/objects/types.mjs} +0 -0
  146. /package/{build/src/server.js → dist/esm/server.mjs} +0 -0
  147. /package/{build/src/utils.js → dist/esm/utils.mjs} +0 -0
  148. /package/{build/src → dist/types}/antlr/CircuitScriptVisitor.d.ts +0 -0
  149. /package/{build/src → dist/types}/export.d.ts +0 -0
  150. /package/{build/src → dist/types}/fonts.d.ts +0 -0
  151. /package/{build/src → dist/types}/globals.d.ts +0 -0
  152. /package/{build/src → dist/types}/lexer.d.ts +0 -0
  153. /package/{build/src → dist/types}/logger.d.ts +0 -0
  154. /package/{build/src → dist/types}/main.d.ts +0 -0
  155. /package/{build/src → dist/types}/objects/ExecutionScope.d.ts +0 -0
  156. /package/{build/src → dist/types}/objects/Frame.d.ts +0 -0
  157. /package/{build/src → dist/types}/objects/Net.d.ts +0 -0
  158. /package/{build/src → dist/types}/objects/ParamDefinition.d.ts +0 -0
  159. /package/{build/src → dist/types}/objects/PinTypes.d.ts +0 -0
  160. /package/{build/src → dist/types}/objects/Wire.d.ts +0 -0
  161. /package/{build/src → dist/types}/objects/types.d.ts +0 -0
  162. /package/{build/src → dist/types}/regenerate-tests.d.ts +0 -0
  163. /package/{build/src → dist/types}/server.d.ts +0 -0
  164. /package/{build/src → dist/types}/utils.d.ts +0 -0
@@ -1,201 +0,0 @@
1
- import { ClassComponent } from './ClassComponent.js';
2
- import { Net } from './Net.js';
3
- import { CFunction, ComponentPinNet, ComponentPinNetPair, ValueType } from './types.js';
4
- import { LayoutDirection } from '../globals.js';
5
- import { Wire, WireSegment } from './Wire.js';
6
- import { Frame } from './Frame.js';
7
-
8
- export class ExecutionScope {
9
- scopeId: number;
10
- private nets: ComponentPinNetPair[] = [];
11
-
12
- instances: Map<string, ClassComponent> = new Map();
13
-
14
- functions: Map<string, CFunction> = new Map();
15
-
16
- variables: Map<string, ValueType | ClassComponent> = new Map();
17
-
18
- blockStack: Map<number, any> = new Map();
19
-
20
- wires: Wire[] = [];
21
- frames: Frame[] = [];
22
-
23
- indentLevel = 0;
24
- netCounter = 1;
25
- unnamedCounter = 1;
26
-
27
- currentComponent: ClassComponent | null = null;
28
- currentPin: number | null = null;
29
-
30
- currentWireId = -1;
31
- currentFrameId = -1;
32
-
33
- netGnd: Net | null = null;
34
-
35
- // This is the main gnd net of the circuit scope
36
- componentGnd: ClassComponent | null = null;
37
-
38
- // This is the first component in the circuit scope
39
- componentRoot: ClassComponent | null = null;
40
-
41
- // Tracks the counter for copies of each component instance
42
- // Copies are the same component electrically, but different symbols.
43
- copyIDs: Map<string, number> = new Map();
44
-
45
- // Determines how components are added/joined on the graph graphically.
46
- // This is very important in "building" up the graph
47
- // according to the user's order.
48
- sequence: any[] = [];
49
-
50
- constructor(scopeId: number) {
51
- this.scopeId = scopeId;
52
- }
53
-
54
- static scopeId = 0;
55
-
56
- static create(): ExecutionScope {
57
- const scope = new ExecutionScope(ExecutionScope.scopeId);
58
- ExecutionScope.scopeId += 1;
59
- return scope;
60
- }
61
-
62
- private findNet(component: ClassComponent, pin: number): ComponentPinNetPair {
63
- return this.nets.find(([tmpComponent, tmpPin]) => {
64
- // Use manual equality, much faster than using lodash
65
- return tmpComponent.isEqual(component) && tmpPin === pin;
66
- });
67
- }
68
-
69
- getNetWithName(name: string): Net {
70
- const found = this.nets.find(([,, net]) => {
71
- return net.name === name;
72
- });
73
-
74
- return found ? found[2]: null;
75
- }
76
-
77
- hasNet(component: ClassComponent, pin: number): boolean {
78
- return this.findNet(component, pin) !== undefined;
79
- }
80
-
81
- getNet(component: ClassComponent, pin: number): Net {
82
- const result = this.findNet(component, pin);
83
- if (result) {
84
- return result[2]; // net
85
- }
86
-
87
- return null;
88
- }
89
-
90
- setNet(component: ClassComponent, pin: number, net: Net): void {
91
- const pair = this.findNet(component, pin);
92
- const result = this.nets.indexOf(pair);
93
-
94
- if (result === -1) {
95
- this.nets.push([component, pin, net]);
96
- } else {
97
- this.nets[result][2] = net;
98
- }
99
-
100
- component.pinNets.set(pin, net);
101
- }
102
-
103
- removeNet(component: ClassComponent, pin: number): void {
104
- const pair = this.findNet(component, pin);
105
- const result = this.nets.indexOf(pair);
106
-
107
- if (result !== -1) {
108
- this.nets.splice(result, 1);
109
- }
110
-
111
- component.pinNets.delete(pin);
112
- }
113
-
114
- getNets(): ComponentPinNetPair[] {
115
- return this.nets;
116
- }
117
-
118
- dumpNets(): ComponentPinNet[] {
119
- const sortedNet = [...this.nets].sort((a, b) => {
120
- const netA = a[2];
121
- const netB = b[2];
122
-
123
- const netAId = netA.namespace + netA.name;
124
- const netBId = netB.namespace + b[2].name;
125
-
126
- if (netAId > netBId) {
127
- return 1;
128
- } else if (netAId < netBId) {
129
- return -1;
130
- } else {
131
- return 0;
132
- }
133
- });
134
-
135
- return sortedNet.map(([component, pin, net]) => {
136
- return [net.namespace + net.name, component.instanceName, pin];
137
- });
138
- }
139
-
140
- printNets(): void {
141
- this.dumpNets().forEach(item => {
142
- const [netName, instanceName, pin] = item;
143
- console.log(netName.padEnd(10), '=>', instanceName, pin);
144
- });
145
- }
146
-
147
- setActive(type: ActiveObject, item: any): void {
148
- // Reset the state and ensure that only one of the
149
- // active object is selected.
150
-
151
- this.clearActive();
152
-
153
- if (type === ActiveObject.Wire) {
154
- this.currentWireId = item;
155
- } else if (type === ActiveObject.Frame) {
156
- this.currentFrameId = item;
157
- }
158
- }
159
-
160
- clearActive(): void {
161
- // Clears any current selected wire or frame
162
- this.currentWireId = -1;
163
- this.currentFrameId = -1;
164
- }
165
- }
166
-
167
- export enum SequenceAction {
168
- // Link current insertion point to component pin
169
- To = 'to',
170
-
171
- // Move insertion point at component pin
172
- At = 'at',
173
-
174
- // Link current insertion point with wire of given segments
175
- Wire = 'wire',
176
-
177
- // Jump to wire with target ID. Pin 0 of wire is the start of the
178
- // wire, pin 1 is the other end of the wire.
179
- WireJump = 'wire-jump',
180
-
181
- // Creates a new frame group
182
- Frame = 'frame',
183
- }
184
-
185
- export enum FrameAction {
186
- Enter = 'enter',
187
- Exit = 'exit',
188
- }
189
-
190
- export enum ActiveObject {
191
- Frame = 'frame',
192
- Wire = 'wire',
193
- }
194
-
195
-
196
- export type SequenceItem =
197
- [SequenceAction.To | SequenceAction.At, ClassComponent, number, LayoutDirection?, string?]
198
- | [SequenceAction.Wire, wireId: number, WireSegment[]]
199
- | [SequenceAction.WireJump, wireId: number, pinId: number]
200
- | [SequenceAction.Frame, Frame, "enter" | "exit"]
201
- ;
@@ -1,20 +0,0 @@
1
- export class Frame {
2
- parameters: Map<string, any> = new Map();
3
- frameId: number;
4
-
5
- constructor(frameId: number) {
6
- this.frameId = frameId;
7
- }
8
- }
9
-
10
- export enum FrameParamKeys {
11
- Title = 'title',
12
- Direction = 'direction',
13
- Padding = 'padding',
14
- Border = 'border',
15
- }
16
-
17
- export enum FramePlotDirection {
18
- Row = 'row',
19
- Column = 'column',
20
- }
@@ -1,49 +0,0 @@
1
- export class ParamDefinition {
2
- paramName: string;
3
- paramValue: string | number | any;
4
-
5
- constructor(paramName: string, paramValue: any) {
6
- this.paramName = paramName;
7
- this.paramValue = paramValue;
8
- }
9
- }
10
-
11
- export class NumericValue {
12
- value: string | number;
13
-
14
- constructor(value: string | number) {
15
- this.value = value;
16
- }
17
-
18
- toString(): string {
19
- return 'numeric:' + this.value;
20
- }
21
-
22
- toDisplayString(): string {
23
- if (typeof this.value === 'number'){
24
- return this.value.toString();
25
-
26
- } else if (typeof this.value === 'string'){
27
- return this.value;
28
- }
29
- }
30
- }
31
- export class PercentageValue {
32
- value: string | number;
33
-
34
- constructor(value: string | number) {
35
- this.value = value;
36
- }
37
-
38
- toString(): string {
39
- return this.value.toString();
40
- }
41
- }
42
-
43
- export class PinBlankValue {
44
- blank: number;
45
-
46
- constructor(value: number) {
47
- this.blank = value;
48
- }
49
- }
@@ -1,49 +0,0 @@
1
- import { PinTypes } from './PinTypes.js';
2
-
3
- export type PinId = number | string;
4
-
5
- export class PinDefinition {
6
- id: PinId;
7
-
8
- idType: PinIdType;
9
-
10
- pinType: PinTypes;
11
-
12
- name: string;
13
- altNames: string[];
14
-
15
- side: string = PortSide.EAST;
16
-
17
- position = -1;
18
-
19
- constructor(
20
- id: PinId,
21
- idType: PinIdType,
22
- name: string,
23
- pinType = PinTypes.Any,
24
- altNames = [],
25
- ) {
26
- this.id = id;
27
- this.idType = idType;
28
-
29
- this.pinType = pinType;
30
-
31
- this.name = name;
32
- this.altNames = altNames;
33
- }
34
- }
35
-
36
- export enum PinIdType {
37
- Int = 'int',
38
- Str = 'str',
39
- }
40
-
41
- export enum PortSide {
42
- WEST = 'WEST',
43
- EAST = 'EAST',
44
- SOUTH = 'SOUTH',
45
- NORTH = 'NORTH'
46
- }
47
-
48
-
49
- export class PinReference {}
@@ -1,7 +0,0 @@
1
- export enum PinTypes {
2
- Any = 'any',
3
- Input = 'input',
4
- Output = 'output',
5
- IO = 'io',
6
- Power = 'power',
7
- }
@@ -1,19 +0,0 @@
1
- import { ClassComponent } from "./ClassComponent.js";
2
-
3
- export class Wire {
4
-
5
- // Should have some net info too
6
-
7
- path: WireSegment[]
8
-
9
- constructor(path: WireSegment[]) {
10
- this.path = path;
11
- }
12
- }
13
-
14
- export type WireSegment = {
15
- direction: 'up' | 'down' | 'left' | 'right' | 'auto' | 'auto_';
16
- value: number,
17
- valueXY?: [x: number, y: number],
18
- until?: [instance: ClassComponent, pin: number],
19
- }
@@ -1,66 +0,0 @@
1
- import { ExecutionContext } from '../execute.js';
2
- import { ClassComponent } from './ClassComponent.js';
3
- import { Net } from './Net.js';
4
- import { NumericValue, PercentageValue, PinBlankValue } from './ParamDefinition.js';
5
-
6
- export type CFunction = (args: CallableParameter[],
7
- options?: CFunctionOptions) => CFunctionResult;
8
-
9
- export type CFunctionOptions = {
10
- netNamespace?: string
11
- }
12
-
13
- export type CFunctionResult = [
14
- executionContext: ExecutionContext,
15
- result: ValueType | ClassComponent | null
16
- ];
17
-
18
- // export type NetMap = Map<ComponentPin, Net>;
19
-
20
- export type ComponentPinNet = [
21
- netName: string, instanceName: string, pin: number];
22
-
23
- // Need to improve name...
24
- export type ComponentPinNetPair = [
25
- component: ClassComponent,
26
- pin: number,
27
- net: Net
28
- ];
29
-
30
- export type ComponentPin = [
31
- component: ClassComponent,
32
- pinId: number|string
33
- ];
34
-
35
- export type ComplexType = ValueType
36
- | ClassComponent
37
- | UndeclaredReference
38
- | null;
39
-
40
- export type ValueType = boolean | number | string |
41
- NumericValue | PercentageValue | PinBlankValue;
42
-
43
- export type CallableParameter =
44
- ['keyword', key: string, value: ValueType] |
45
- ['position', key: number, value: ValueType];
46
-
47
- export type FunctionDefinedParameter = [name: string, defaultValue: ValueType]
48
- | [name: string];
49
-
50
-
51
- export class UndeclaredReference {
52
- reference: ReferenceType;
53
-
54
- constructor(reference: ReferenceType) {
55
- this.reference = reference;
56
- }
57
- }
58
-
59
- export type ReferenceType =
60
- {
61
- found: boolean,
62
- name?: string,
63
- trailers?: string[],
64
- type?: string,
65
- value?: any
66
- };
package/src/parser.ts DELETED
@@ -1,106 +0,0 @@
1
- import { CharStream, CommonTokenStream, ErrorListener, Token } from 'antlr4';
2
-
3
- import CircuitScriptLexer from './antlr/CircuitScriptLexer.js';
4
- import CircuitScriptParser from './antlr/CircuitScriptParser.js';
5
-
6
- import { MainVisitor } from './visitor.js';
7
- import { MainLexer } from './lexer.js';
8
- import { SimpleStopwatch } from './utils.js';
9
-
10
- export function parseFileWithVisitor(visitor: MainVisitor, data: string) {
11
- const chars = new CharStream(data);
12
- const lexer = new MainLexer(chars);
13
-
14
- const lexerTimer = new SimpleStopwatch();
15
-
16
- const tokens = new CommonTokenStream(lexer);
17
- tokens.fill();
18
-
19
- const lexerTimeTaken = lexerTimer.lap();
20
-
21
- const parserTimer = new SimpleStopwatch();
22
- const parser = new CircuitScriptParser(tokens);
23
- // Clear any existing error listeners and use the custom one only
24
- parser.removeErrorListeners();
25
-
26
- const errorListener = new CircuitscriptParserErrorListener();
27
- parser.addErrorListener(errorListener);
28
-
29
- const tree = parser.script();
30
-
31
- let hasError = false;
32
-
33
- try {
34
- visitor.visit(tree);
35
- } catch (err){
36
- // Error should be internally handled in visitor
37
- err.print(data);
38
- hasError = true;
39
- }
40
-
41
- const parserTimeTaken = parserTimer.lap();
42
-
43
- // dumpTokens(tokens.tokens, lexer, data);
44
-
45
- return {
46
- tree, parser,
47
- hasParseError: errorListener.hasParseErrors(),
48
- hasError,
49
- parserTimeTaken,
50
- lexerTimeTaken,
51
- };
52
- }
53
-
54
- function dumpTokens(tokens:Token[], lexer: CircuitScriptLexer, scriptData: string): void {
55
- tokens.forEach(item => {
56
- if (item.type !== -1) {
57
- let stringValue = "";
58
- let textPart = "";
59
-
60
- if (lexer.symbolicNames[item.type] !== null && lexer.symbolicNames[item.type] !== undefined) {
61
- stringValue = lexer.symbolicNames[item.type];
62
- if (stringValue !== "NEWLINE") {
63
- textPart = scriptData.substring(item.start, item.stop + 1);
64
- } else {
65
- textPart = item.text.length-1;
66
- }
67
- } else if (lexer.literalNames[item.type] !== null && lexer.literalNames[item.type] !== undefined) {
68
- stringValue = lexer.literalNames[item.type];
69
- textPart = scriptData.substring(item.start, item.stop + 1);
70
- } else {
71
- stringValue = item._text;
72
- }
73
-
74
- console.log('line', item.line + ':' + item.column, `\t${stringValue} (${item.type})`.padEnd(30), textPart);
75
- }
76
- });
77
- }
78
-
79
- export class CircuitscriptParserErrorListener extends ErrorListener {
80
-
81
- syntaxErrorCounter = 0;
82
-
83
- syntaxError(recognizer: Recognizer<TSymbol>,
84
- offendingSymbol: TSymbol,
85
- line: number,
86
- column: number,
87
- msg: string,
88
- e: RecognitionException | undefined) {
89
- console.log("Syntax error at line", line, ':', column, ' - ', msg);
90
-
91
- this.syntaxErrorCounter++;
92
- }
93
-
94
- // reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs) {
95
- // }
96
-
97
- // reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs) {
98
- // }
99
-
100
- // reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs) {
101
- // }
102
-
103
- hasParseErrors(): boolean {
104
- return (this.syntaxErrorCounter > 0);
105
- }
106
- }
@@ -1,25 +0,0 @@
1
- import fs from 'fs';
2
- import { renderScript } from './helpers.js';
3
-
4
- const mainDir = './__tests__/renderData/';
5
-
6
- const cstFiles = [];
7
-
8
- const files = fs.readdirSync(mainDir);
9
- files.forEach(file => {
10
- if (file.endsWith('.cst')){
11
- cstFiles.push(file);
12
- }
13
- });
14
-
15
- const useCurrentDir = './examples/';
16
-
17
- cstFiles.forEach(file => {
18
- const inputPath = mainDir + file;
19
- const scriptData = fs.readFileSync(inputPath, { encoding: 'utf-8' });
20
-
21
- const outputPath = inputPath + '.svg';
22
- renderScript(scriptData, outputPath, { currentDirectory: useCurrentDir });
23
-
24
- console.log('generated ', outputPath);
25
- });