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.
- package/dist/cjs/BaseVisitor.js +127 -73
- package/dist/cjs/SemanticTokenVisitor.js +19 -13
- package/dist/cjs/antlr/CircuitScriptParser.js +711 -671
- package/dist/cjs/builtinMethods.js +29 -25
- package/dist/cjs/environment.js +118 -0
- package/dist/cjs/execute.js +53 -12
- package/dist/cjs/geometry.js +1 -0
- package/dist/cjs/globals.js +11 -6
- package/dist/cjs/helpers.js +135 -127
- package/dist/cjs/index.js +5 -0
- package/dist/cjs/layout.js +37 -12
- package/dist/cjs/main.js +31 -19
- package/dist/cjs/objects/ExecutionScope.js +33 -0
- package/dist/cjs/objects/ParamDefinition.js +15 -15
- package/dist/cjs/parser.js +27 -21
- package/dist/cjs/regenerate-tests.js +9 -6
- package/dist/cjs/render.js +3 -1
- package/dist/cjs/sizing.js +5 -58
- package/dist/cjs/utils.js +85 -30
- package/dist/cjs/validate/SymbolTable.js +96 -0
- package/dist/cjs/validate/SymbolValidatorResolveVisitor.js +14 -0
- package/dist/cjs/validate/SymbolValidatorVisitor.js +170 -0
- package/dist/cjs/validate.js +52 -44
- package/dist/cjs/visitor.js +140 -24
- package/dist/esm/{BaseVisitor.mjs → BaseVisitor.js} +98 -45
- package/dist/esm/{SemanticTokenVisitor.mjs → SemanticTokenVisitor.js} +17 -11
- package/dist/esm/antlr/{CircuitScriptParser.mjs → CircuitScriptParser.js} +711 -671
- package/dist/esm/{builtinMethods.mjs → builtinMethods.js} +20 -16
- package/dist/esm/{draw_symbols.mjs → draw_symbols.js} +7 -7
- package/dist/esm/environment.js +110 -0
- package/dist/esm/{execute.mjs → execute.js} +66 -25
- package/dist/esm/{export.mjs → export.js} +2 -2
- package/dist/esm/{geometry.mjs → geometry.js} +6 -5
- package/dist/esm/{globals.mjs → globals.js} +6 -1
- package/dist/esm/helpers.js +377 -0
- package/dist/esm/index.js +20 -0
- package/dist/esm/{layout.mjs → layout.js} +42 -20
- package/dist/esm/{lexer.mjs → lexer.js} +2 -2
- package/dist/esm/{main.mjs → main.js} +33 -21
- package/dist/esm/objects/{ClassComponent.mjs → ClassComponent.js} +5 -4
- package/dist/esm/objects/{ExecutionScope.mjs → ExecutionScope.js} +33 -0
- package/dist/esm/objects/{Frame.mjs → Frame.js} +1 -1
- package/dist/esm/objects/{ParamDefinition.mjs → ParamDefinition.js} +1 -1
- package/dist/esm/objects/{PinDefinition.mjs → PinDefinition.js} +1 -1
- package/dist/esm/parser.js +71 -0
- package/dist/esm/{regenerate-tests.mjs → regenerate-tests.js} +10 -7
- package/dist/esm/{render.mjs → render.js} +11 -9
- package/dist/esm/{sizing.mjs → sizing.js} +6 -34
- package/dist/esm/{utils.mjs → utils.js} +61 -17
- package/dist/esm/validate/SymbolTable.js +90 -0
- package/dist/esm/validate/SymbolValidatorResolveVisitor.js +10 -0
- package/dist/esm/validate/SymbolValidatorVisitor.js +163 -0
- package/dist/esm/validate.js +86 -0
- package/dist/esm/{visitor.mjs → visitor.js} +151 -35
- package/dist/fonts/Arial.ttf +0 -0
- package/dist/fonts/Inter-Bold.ttf +0 -0
- package/dist/fonts/Inter-Regular.ttf +0 -0
- package/dist/fonts/OpenSans-Regular.ttf +0 -0
- package/dist/fonts/Roboto-Regular.ttf +0 -0
- package/dist/libs/lib.cst +423 -0
- package/dist/types/BaseVisitor.d.ts +34 -21
- package/dist/types/SemanticTokenVisitor.d.ts +6 -5
- package/dist/types/antlr/CircuitScriptParser.d.ts +4 -2
- package/dist/types/builtinMethods.d.ts +3 -2
- package/dist/types/environment.d.ts +31 -0
- package/dist/types/globals.d.ts +4 -1
- package/dist/types/helpers.d.ts +12 -14
- package/dist/types/index.d.ts +5 -0
- package/dist/types/objects/ClassComponent.d.ts +1 -0
- package/dist/types/objects/ExecutionScope.d.ts +11 -0
- package/dist/types/objects/types.d.ts +6 -1
- package/dist/types/parser.d.ts +7 -11
- package/dist/types/sizing.d.ts +0 -3
- package/dist/types/utils.d.ts +30 -6
- package/dist/types/validate/SymbolTable.d.ts +40 -0
- package/dist/types/validate/SymbolValidatorResolveVisitor.d.ts +7 -0
- package/dist/types/validate/SymbolValidatorVisitor.d.ts +32 -0
- package/dist/types/validate.d.ts +1 -1
- package/package.json +14 -13
- package/dist/cjs/SymbolValidatorVisitor.js +0 -233
- package/dist/esm/SymbolValidatorVisitor.mjs +0 -222
- package/dist/esm/helpers.mjs +0 -364
- package/dist/esm/index.mjs +0 -15
- package/dist/esm/parser.mjs +0 -64
- package/dist/esm/validate.mjs +0 -74
- package/dist/types/SymbolValidatorVisitor.d.ts +0 -61
- package/dist/types/layout.d.ts +0 -148
- /package/dist/esm/antlr/{CircuitScriptLexer.mjs → CircuitScriptLexer.js} +0 -0
- /package/dist/esm/antlr/{CircuitScriptVisitor.mjs → CircuitScriptVisitor.js} +0 -0
- /package/dist/esm/{fonts.mjs → fonts.js} +0 -0
- /package/dist/esm/{logger.mjs → logger.js} +0 -0
- /package/dist/esm/objects/{Net.mjs → Net.js} +0 -0
- /package/dist/esm/objects/{PinTypes.mjs → PinTypes.js} +0 -0
- /package/dist/esm/objects/{Wire.mjs → Wire.js} +0 -0
- /package/dist/esm/objects/{types.mjs → types.js} +0 -0
- /package/dist/esm/{server.mjs → server.js} +0 -0
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import { BaseVisitor } from "./BaseVisitor.mjs";
|
|
2
|
-
import { ParseSymbolType } from "./objects/types.mjs";
|
|
3
|
-
export class SymbolValidatorVisitor extends BaseVisitor {
|
|
4
|
-
symbolTable = new SymbolTable();
|
|
5
|
-
addSymbolVariable(name, value, executor = null) {
|
|
6
|
-
const useExecutor = executor === null ? this.getExecutor() : executor;
|
|
7
|
-
this.symbolTable.addVariable(useExecutor, name, value);
|
|
8
|
-
this.log2('add symbol variable: ' + name);
|
|
9
|
-
}
|
|
10
|
-
addSymbolFunction(functionName, funcDefinedParameters) {
|
|
11
|
-
if (!this.symbolTable.exists(this.getExecutor(), functionName)) {
|
|
12
|
-
this.symbolTable.addFunction(this.getExecutor(), functionName, funcDefinedParameters);
|
|
13
|
-
this.log2('add symbol function: ' + functionName);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
handleAtomSymbol(atom) {
|
|
17
|
-
const atomId = atom.getText();
|
|
18
|
-
const executor = this.getExecutor();
|
|
19
|
-
let tmpSymbol;
|
|
20
|
-
if (this.symbolTable.exists(executor, atomId)) {
|
|
21
|
-
tmpSymbol = this.symbolTable.get(executor, atomId);
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
const foundContext = this.symbolTable.searchParentContext(executor, atomId);
|
|
25
|
-
if (foundContext === null) {
|
|
26
|
-
tmpSymbol = this.symbolTable.addUndefined(executor, atomId, atom);
|
|
27
|
-
this.log2('symbol not found: ' + atomId);
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
tmpSymbol = this.symbolTable.get(foundContext, atomId);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return tmpSymbol;
|
|
34
|
-
}
|
|
35
|
-
setSymbols(symbolTable) {
|
|
36
|
-
this.symbolTable = symbolTable;
|
|
37
|
-
}
|
|
38
|
-
visitImport_expr = (ctx) => {
|
|
39
|
-
const ID = ctx.ID().toString();
|
|
40
|
-
const { pathExists } = this.handleImportFile(ID, false, ctx);
|
|
41
|
-
if (!pathExists) {
|
|
42
|
-
this.symbolTable.addUndefined(this.getExecutor(), ID, ctx.ID());
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
visitAssignment_expr = (ctx) => {
|
|
46
|
-
const atomStr = ctx.atom_expr().getText();
|
|
47
|
-
const ctxDataExpr = ctx.data_expr();
|
|
48
|
-
this.visit(ctxDataExpr);
|
|
49
|
-
const value = this.getResult(ctxDataExpr);
|
|
50
|
-
this.addSymbolVariable(atomStr, value);
|
|
51
|
-
return null;
|
|
52
|
-
};
|
|
53
|
-
visitAtom_expr = (ctx) => {
|
|
54
|
-
const tmpSymbol = this.handleAtomSymbol(ctx.ID(0));
|
|
55
|
-
this.setResult(ctx, tmpSymbol);
|
|
56
|
-
};
|
|
57
|
-
visitFunction_call_expr = (ctx) => {
|
|
58
|
-
this.handleAtomSymbol(ctx.ID());
|
|
59
|
-
if (ctx.trailer_expr().length > 0) {
|
|
60
|
-
ctx.trailer_expr().forEach(item => {
|
|
61
|
-
if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
|
|
62
|
-
const params = item.parameters();
|
|
63
|
-
if (params) {
|
|
64
|
-
this.visit(params);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
visitValueAtomExpr = (ctx) => {
|
|
71
|
-
let value = null;
|
|
72
|
-
const ctxValueExpr = ctx.value_expr();
|
|
73
|
-
const cxtAtomExpr = ctx.atom_expr();
|
|
74
|
-
if (ctxValueExpr) {
|
|
75
|
-
this.visit(ctxValueExpr);
|
|
76
|
-
value = this.getResult(ctxValueExpr);
|
|
77
|
-
}
|
|
78
|
-
else if (cxtAtomExpr) {
|
|
79
|
-
this.visit(cxtAtomExpr);
|
|
80
|
-
value = this.getResult(cxtAtomExpr);
|
|
81
|
-
}
|
|
82
|
-
this.setResult(ctx, value);
|
|
83
|
-
};
|
|
84
|
-
visitUnaryOperatorExpr = (ctx) => {
|
|
85
|
-
this.visit(ctx.data_expr());
|
|
86
|
-
};
|
|
87
|
-
visitMultiplyExpr = (ctx) => {
|
|
88
|
-
this.visit(ctx.data_expr(0));
|
|
89
|
-
this.visit(ctx.data_expr(1));
|
|
90
|
-
};
|
|
91
|
-
visitAdditionExpr = (ctx) => {
|
|
92
|
-
this.visit(ctx.data_expr(0));
|
|
93
|
-
this.visit(ctx.data_expr(1));
|
|
94
|
-
};
|
|
95
|
-
visitBinaryOperatorExpr = (ctx) => {
|
|
96
|
-
this.visit(ctx.data_expr(0));
|
|
97
|
-
this.visit(ctx.data_expr(1));
|
|
98
|
-
};
|
|
99
|
-
visitDataExpr = (ctx) => {
|
|
100
|
-
return;
|
|
101
|
-
};
|
|
102
|
-
visitFunction_def_expr = (ctx) => {
|
|
103
|
-
const functionName = ctx.ID().getText();
|
|
104
|
-
let funcDefinedParameters = [];
|
|
105
|
-
const ctxFunctionArgsExpr = ctx.function_args_expr();
|
|
106
|
-
if (ctxFunctionArgsExpr) {
|
|
107
|
-
this.visit(ctxFunctionArgsExpr);
|
|
108
|
-
funcDefinedParameters = this.getResult(ctxFunctionArgsExpr);
|
|
109
|
-
}
|
|
110
|
-
this.addSymbolFunction(functionName, funcDefinedParameters);
|
|
111
|
-
const executionContextName = functionName + '_validate';
|
|
112
|
-
const passedInParamsNull = funcDefinedParameters.map((param, index) => {
|
|
113
|
-
return ['position', index, null];
|
|
114
|
-
});
|
|
115
|
-
const newExecutor = this.enterNewChildContext(this.executionStack, this.getExecutor(), executionContextName, { netNamespace: "" }, funcDefinedParameters, passedInParamsNull);
|
|
116
|
-
funcDefinedParameters.forEach(param => {
|
|
117
|
-
this.addSymbolVariable(param[0], null, newExecutor);
|
|
118
|
-
});
|
|
119
|
-
this.runExpressions(newExecutor, ctx.function_expr());
|
|
120
|
-
this.executionStack.pop();
|
|
121
|
-
};
|
|
122
|
-
getSymbols() {
|
|
123
|
-
return this.symbolTable;
|
|
124
|
-
}
|
|
125
|
-
dumpSymbols() {
|
|
126
|
-
this.symbolTable.dumpSymbols();
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
export class SymbolValidatorResolveVisitor extends SymbolValidatorVisitor {
|
|
130
|
-
addSymbolVariable(name, value) {
|
|
131
|
-
}
|
|
132
|
-
addSymbolFunction(functionName, funcDefinedParameters) {
|
|
133
|
-
if (this.symbolTable.exists(this.getExecutor(), functionName)) {
|
|
134
|
-
this.symbolTable.addFunction(this.getExecutor(), functionName, funcDefinedParameters);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
export class SymbolTable {
|
|
139
|
-
symbols = new Map();
|
|
140
|
-
executonContextsNamespaces = [];
|
|
141
|
-
getSymbols() {
|
|
142
|
-
return this.symbols;
|
|
143
|
-
}
|
|
144
|
-
addFunction(executionContext, id, funcDefinedParameters) {
|
|
145
|
-
return this.add(executionContext, id, ParseSymbolType.Function, {
|
|
146
|
-
funcDefinedParameters
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
addVariable(executionContext, id, variableValue) {
|
|
150
|
-
return this.add(executionContext, id, ParseSymbolType.Variable, {
|
|
151
|
-
variableValue
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
addUndefined(executionContext, id, node) {
|
|
155
|
-
return this.add(executionContext, id, ParseSymbolType.Undefined, {
|
|
156
|
-
node
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
add(executionContext, id, type, extra) {
|
|
160
|
-
if (this.executonContextsNamespaces.indexOf(executionContext.namespace) === -1) {
|
|
161
|
-
this.executonContextsNamespaces.push(executionContext.namespace);
|
|
162
|
-
}
|
|
163
|
-
const item = {
|
|
164
|
-
id,
|
|
165
|
-
type,
|
|
166
|
-
context: executionContext,
|
|
167
|
-
extra
|
|
168
|
-
};
|
|
169
|
-
this.symbols.set(this.idName(executionContext, id), item);
|
|
170
|
-
return item;
|
|
171
|
-
}
|
|
172
|
-
idName(executionContext, id) {
|
|
173
|
-
return executionContext.namespace + id;
|
|
174
|
-
}
|
|
175
|
-
dumpSymbols() {
|
|
176
|
-
for (const [key, value] of this.symbols) {
|
|
177
|
-
console.log(value.type.padEnd(10, " "), key);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
exists(executionContext, id) {
|
|
181
|
-
const name = this.idName(executionContext, id);
|
|
182
|
-
return this.symbols.has(name);
|
|
183
|
-
}
|
|
184
|
-
existsAny(executionContext, id) {
|
|
185
|
-
if (this.exists(executionContext, id)) {
|
|
186
|
-
return true;
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
return this.searchParentContext(executionContext, id) !== null;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
get(executionContext, id) {
|
|
193
|
-
const name = this.idName(executionContext, id);
|
|
194
|
-
return this.symbols.get(name);
|
|
195
|
-
}
|
|
196
|
-
getParentContexts(executionContext, contextsNamespace) {
|
|
197
|
-
if (executionContext.parentContext !== null) {
|
|
198
|
-
contextsNamespace.push(executionContext.parentContext.namespace);
|
|
199
|
-
this.getParentContexts(executionContext.parentContext, contextsNamespace);
|
|
200
|
-
}
|
|
201
|
-
return contextsNamespace;
|
|
202
|
-
}
|
|
203
|
-
searchParentContext(executionContext, id) {
|
|
204
|
-
const contextNames = this.getParentContexts(executionContext, []);
|
|
205
|
-
for (const [key,] of this.symbols) {
|
|
206
|
-
if (key.endsWith(`.${id}`)) {
|
|
207
|
-
const { context } = this.symbols.get(key);
|
|
208
|
-
if (contextNames.indexOf(context.namespace) !== -1) {
|
|
209
|
-
return context;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
clearUndefined() {
|
|
216
|
-
for (const [key, value] of this.symbols) {
|
|
217
|
-
if (value.type === ParseSymbolType.Undefined) {
|
|
218
|
-
this.symbols.delete(key);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
package/dist/esm/helpers.mjs
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, createWriteStream, existsSync, mkdirSync } from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import PDFDocument from "pdfkit";
|
|
4
|
-
import { generateKiCADNetList, printTree } from "./export.mjs";
|
|
5
|
-
import { LayoutEngine } from "./layout.mjs";
|
|
6
|
-
import { parseFileWithVisitor } from "./parser.mjs";
|
|
7
|
-
import { generatePdfOutput, generateSvgOutput, renderSheetsToSVG } from "./render.mjs";
|
|
8
|
-
import { generateDebugSequenceAction, resolveToNumericValue, sequenceActionString, SimpleStopwatch } from "./utils.mjs";
|
|
9
|
-
import { ParserVisitor, VisitorExecutionException } from "./visitor.mjs";
|
|
10
|
-
import { createContext } from "this-file";
|
|
11
|
-
import { SymbolValidatorResolveVisitor, SymbolValidatorVisitor } from "./SymbolValidatorVisitor.mjs";
|
|
12
|
-
import { BaseErrorListener, CharStream, CommonTokenStream, DefaultErrorStrategy } from "antlr4ng";
|
|
13
|
-
import { MainLexer } from "./lexer.mjs";
|
|
14
|
-
import { CircuitScriptParser } from "./antlr/CircuitScriptParser.mjs";
|
|
15
|
-
import { prepareTokens, SemanticTokensVisitor } from "./SemanticTokenVisitor.mjs";
|
|
16
|
-
import { defaultPageMarginMM, defaultZoomScale, LengthUnit, MilsToMM, PxToMM } from "./globals.mjs";
|
|
17
|
-
import { FrameParamKeys } from "./objects/Frame.mjs";
|
|
18
|
-
import Big from "big.js";
|
|
19
|
-
import { Logger } from "./logger.mjs";
|
|
20
|
-
export var JSModuleType;
|
|
21
|
-
(function (JSModuleType) {
|
|
22
|
-
JSModuleType["CommonJs"] = "cjs";
|
|
23
|
-
JSModuleType["ESM"] = "mjs";
|
|
24
|
-
})(JSModuleType || (JSModuleType = {}));
|
|
25
|
-
export function prepareFile(textData) {
|
|
26
|
-
const chars = CharStream.fromString(textData);
|
|
27
|
-
const lexer = new MainLexer(chars);
|
|
28
|
-
const lexerTimer = new SimpleStopwatch();
|
|
29
|
-
const tokens = new CommonTokenStream(lexer);
|
|
30
|
-
tokens.fill();
|
|
31
|
-
const lexerTimeTaken = lexerTimer.lap();
|
|
32
|
-
const parser = new CircuitScriptParser(tokens);
|
|
33
|
-
return {
|
|
34
|
-
parser,
|
|
35
|
-
lexer,
|
|
36
|
-
lexerTimeTaken,
|
|
37
|
-
tokens
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
export function getScriptText(filePath) {
|
|
41
|
-
try {
|
|
42
|
-
return readFileSync(filePath, { encoding: 'utf-8' });
|
|
43
|
-
}
|
|
44
|
-
catch (err) {
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
export function getSemanticTokens(scriptData, options) {
|
|
49
|
-
const { parser, lexer, tokens } = prepareFile(scriptData);
|
|
50
|
-
const tree = parser.script();
|
|
51
|
-
const { currentDirectory = null, defaultLibsPath, } = options;
|
|
52
|
-
const visitor = new SemanticTokensVisitor(true, null, currentDirectory, defaultLibsPath, lexer, scriptData);
|
|
53
|
-
parser.removeErrorListeners();
|
|
54
|
-
visitor.onImportFile = (visitor, textData) => {
|
|
55
|
-
let hasError = false;
|
|
56
|
-
let hasParseError = false;
|
|
57
|
-
if (textData !== null) {
|
|
58
|
-
const { parser } = prepareFile(textData);
|
|
59
|
-
const tree = parser.script();
|
|
60
|
-
try {
|
|
61
|
-
visitor.visit(tree);
|
|
62
|
-
}
|
|
63
|
-
catch (err) {
|
|
64
|
-
console.log('Error while parsing: ', err);
|
|
65
|
-
hasParseError = true;
|
|
66
|
-
hasError = true;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
console.log('File does not exist');
|
|
71
|
-
hasError = true;
|
|
72
|
-
}
|
|
73
|
-
return {
|
|
74
|
-
hasError, hasParseError
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
visitor.visit(tree);
|
|
78
|
-
const semanticTokens = visitor.getTokens();
|
|
79
|
-
const parsedTokens = prepareTokens(tokens.getTokens(), lexer, scriptData);
|
|
80
|
-
const finalParsedTokens = [];
|
|
81
|
-
parsedTokens.forEach(token => {
|
|
82
|
-
const location = `${token.line}_${token.column}`;
|
|
83
|
-
if (semanticTokens.has(location)) {
|
|
84
|
-
finalParsedTokens.push(semanticTokens.get(location));
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
finalParsedTokens.push(token);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
return {
|
|
91
|
-
visitor,
|
|
92
|
-
parsedTokens: finalParsedTokens
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
class TokenErrorListener extends BaseErrorListener {
|
|
96
|
-
syntaxError(recognizer, offendingSymbol, line, column, msg, e) {
|
|
97
|
-
console.log(msg);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
export class ParseErrorStrategy extends DefaultErrorStrategy {
|
|
101
|
-
reportUnwantedToken(recognizer) {
|
|
102
|
-
if (this.inErrorRecoveryMode(recognizer)) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
this.beginErrorCondition(recognizer);
|
|
106
|
-
const t = recognizer.getCurrentToken();
|
|
107
|
-
const tokenName = this.getTokenErrorDisplay(t);
|
|
108
|
-
const msg = "extraneous input " + tokenName;
|
|
109
|
-
recognizer.notifyErrorListeners(msg, t, null);
|
|
110
|
-
this.endErrorCondition(recognizer);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
export function validateScript(scriptData, options) {
|
|
114
|
-
const { parser } = prepareFile(scriptData);
|
|
115
|
-
parser.removeErrorListeners();
|
|
116
|
-
parser.errorHandler = new ParseErrorStrategy();
|
|
117
|
-
parser.addErrorListener(new TokenErrorListener());
|
|
118
|
-
const tree = parser.script();
|
|
119
|
-
const { currentDirectory = null, defaultLibsPath, } = options;
|
|
120
|
-
const visitor = new SymbolValidatorVisitor(true, null, currentDirectory, defaultLibsPath);
|
|
121
|
-
visitor.onImportFile = (visitor, textData) => {
|
|
122
|
-
let hasError = false;
|
|
123
|
-
let hasParseError = false;
|
|
124
|
-
if (textData !== null) {
|
|
125
|
-
const { parser } = prepareFile(textData);
|
|
126
|
-
const tree = parser.script();
|
|
127
|
-
try {
|
|
128
|
-
visitor.visit(tree);
|
|
129
|
-
}
|
|
130
|
-
catch (err) {
|
|
131
|
-
console.log('got an error while parsing tree: ', err);
|
|
132
|
-
hasParseError = true;
|
|
133
|
-
hasError = true;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
console.log('file does not exist!');
|
|
138
|
-
hasError = true;
|
|
139
|
-
}
|
|
140
|
-
return {
|
|
141
|
-
hasError, hasParseError
|
|
142
|
-
};
|
|
143
|
-
};
|
|
144
|
-
visitor.visit(tree);
|
|
145
|
-
const symbolTable = visitor.getSymbols();
|
|
146
|
-
symbolTable.clearUndefined();
|
|
147
|
-
const visitorResolver = new SymbolValidatorResolveVisitor(true, null, currentDirectory, defaultLibsPath);
|
|
148
|
-
visitorResolver.setSymbols(visitor.getSymbols());
|
|
149
|
-
visitorResolver.onImportFile = visitor.onImportFile;
|
|
150
|
-
visitorResolver.visit(tree);
|
|
151
|
-
return visitorResolver;
|
|
152
|
-
}
|
|
153
|
-
export function renderScript(scriptData, outputPath, options) {
|
|
154
|
-
const { currentDirectory = null, defaultLibsPath, dumpNets = false, dumpData = false, showStats = false } = options;
|
|
155
|
-
const onErrorHandler = (line, column, message, error) => {
|
|
156
|
-
if (error instanceof VisitorExecutionException) {
|
|
157
|
-
console.log('Error', line, column, message, error.errorMessage);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
const visitor = new ParserVisitor(true, onErrorHandler, currentDirectory, defaultLibsPath);
|
|
161
|
-
visitor.onImportFile = (visitor, fileData) => {
|
|
162
|
-
const { hasError, hasParseError } = parseFileWithVisitor(visitor, fileData);
|
|
163
|
-
return { hasError, hasParseError };
|
|
164
|
-
};
|
|
165
|
-
visitor.log('reading file');
|
|
166
|
-
visitor.log('done reading file');
|
|
167
|
-
const { tree, parser, hasParseError, hasError, parserTimeTaken, lexerTimeTaken } = parseFileWithVisitor(visitor, scriptData);
|
|
168
|
-
showStats && console.log('Lexing took:', lexerTimeTaken);
|
|
169
|
-
showStats && console.log('Parsing took:', parserTimeTaken);
|
|
170
|
-
if (dumpNets) {
|
|
171
|
-
const nets = visitor.dumpNets();
|
|
172
|
-
nets.forEach(item => console.log(item.join(" | ")));
|
|
173
|
-
}
|
|
174
|
-
const dumpDirectory = currentDirectory + '/dump/';
|
|
175
|
-
if (dumpData) {
|
|
176
|
-
if (!existsSync(dumpDirectory)) {
|
|
177
|
-
mkdirSync(dumpDirectory);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
dumpData && writeFileSync(dumpDirectory + 'tree.lisp', tree.toStringTree(null, parser));
|
|
181
|
-
dumpData && writeFileSync(dumpDirectory + 'raw-parser.txt', visitor.logger.dump());
|
|
182
|
-
if (hasError || hasParseError) {
|
|
183
|
-
console.log('Error while parsing');
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
const { frameComponent } = visitor.applySheetFrameComponent();
|
|
187
|
-
try {
|
|
188
|
-
visitor.annotateComponents();
|
|
189
|
-
}
|
|
190
|
-
catch (err) {
|
|
191
|
-
console.log('Error during annotation: ', err);
|
|
192
|
-
}
|
|
193
|
-
const { sequence, nets } = visitor.getGraph();
|
|
194
|
-
const tmpSequence = generateDebugSequenceAction(sequence).map(item => sequenceActionString(item));
|
|
195
|
-
dumpData && writeFileSync(dumpDirectory + 'raw-sequence.txt', tmpSequence.join('\n'));
|
|
196
|
-
let svgOutput = "";
|
|
197
|
-
try {
|
|
198
|
-
let fileExtension = null;
|
|
199
|
-
let outputDefaultZoom = defaultZoomScale;
|
|
200
|
-
if (outputPath) {
|
|
201
|
-
fileExtension = path.extname(outputPath).substring(1);
|
|
202
|
-
if (fileExtension === "pdf") {
|
|
203
|
-
outputDefaultZoom = 1;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (fileExtension === 'net') {
|
|
207
|
-
const { tree: kicadNetList, missingFootprints } = generateKiCADNetList(visitor.getNetList());
|
|
208
|
-
missingFootprints.forEach(entry => {
|
|
209
|
-
console.log(`${entry.refdes} (${entry.instanceName}) does not have footprint`);
|
|
210
|
-
});
|
|
211
|
-
writeFileSync(outputPath, printTree(kicadNetList));
|
|
212
|
-
console.log('Generated file', outputPath);
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
const layoutEngine = new LayoutEngine();
|
|
216
|
-
const layoutTimer = new SimpleStopwatch();
|
|
217
|
-
const sheetFrames = layoutEngine.runLayout(sequence, nets);
|
|
218
|
-
layoutEngine.printWarnings();
|
|
219
|
-
showStats && console.log('Layout took:', layoutTimer.lap());
|
|
220
|
-
dumpData && writeFileSync(dumpDirectory + 'raw-layout.txt', layoutEngine.logger.dump());
|
|
221
|
-
const generateSvgTimer = new SimpleStopwatch();
|
|
222
|
-
const renderLogger = new Logger();
|
|
223
|
-
const svgCanvas = renderSheetsToSVG(sheetFrames, renderLogger);
|
|
224
|
-
showStats && console.log('Render took:', generateSvgTimer.lap());
|
|
225
|
-
dumpData && writeFileSync(dumpDirectory + 'raw-render.txt', renderLogger.dump());
|
|
226
|
-
svgOutput = generateSvgOutput(svgCanvas, outputDefaultZoom);
|
|
227
|
-
if (outputPath) {
|
|
228
|
-
if (fileExtension === 'svg') {
|
|
229
|
-
writeFileSync(outputPath, svgOutput);
|
|
230
|
-
}
|
|
231
|
-
else if (fileExtension === 'pdf') {
|
|
232
|
-
let sheetSize = "A4";
|
|
233
|
-
let sheetSizeDefined = false;
|
|
234
|
-
if (frameComponent) {
|
|
235
|
-
sheetSize = frameComponent.getParam(FrameParamKeys.PaperSize);
|
|
236
|
-
sheetSizeDefined = true;
|
|
237
|
-
}
|
|
238
|
-
const doc = new PDFDocument({
|
|
239
|
-
layout: 'landscape',
|
|
240
|
-
size: sheetSize
|
|
241
|
-
});
|
|
242
|
-
const outputStream = createWriteStream(outputPath);
|
|
243
|
-
generatePdfOutput(doc, svgCanvas, sheetSize, sheetSizeDefined, outputDefaultZoom);
|
|
244
|
-
doc.pipe(outputStream);
|
|
245
|
-
doc.end();
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
throw "Invalid output format";
|
|
249
|
-
}
|
|
250
|
-
console.log('Generated file', outputPath);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
catch (err) {
|
|
254
|
-
console.log('Error during render: ', err);
|
|
255
|
-
}
|
|
256
|
-
return svgOutput;
|
|
257
|
-
}
|
|
258
|
-
export function detectJSModuleType() {
|
|
259
|
-
if (typeof __filename === 'undefined' &&
|
|
260
|
-
typeof __dirname === 'undefined') {
|
|
261
|
-
return JSModuleType.ESM;
|
|
262
|
-
}
|
|
263
|
-
else {
|
|
264
|
-
return JSModuleType.CommonJs;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
const context = createContext();
|
|
268
|
-
export function getCurrentPath() {
|
|
269
|
-
const filename = context.filename;
|
|
270
|
-
return { filePath: filename };
|
|
271
|
-
}
|
|
272
|
-
function getToolsPath() {
|
|
273
|
-
const { filePath } = getCurrentPath();
|
|
274
|
-
return path.normalize(path.dirname(filePath) + '/../../');
|
|
275
|
-
}
|
|
276
|
-
export function getFontsPath() {
|
|
277
|
-
const toolsPath = getToolsPath();
|
|
278
|
-
return path.normalize(toolsPath + "fonts");
|
|
279
|
-
}
|
|
280
|
-
export function getDefaultLibsPath() {
|
|
281
|
-
const toolsPath = getToolsPath();
|
|
282
|
-
return path.normalize(toolsPath + "libs");
|
|
283
|
-
}
|
|
284
|
-
export function getPackageVersion() {
|
|
285
|
-
const packageJson = JSON.parse(readFileSync(getToolsPath() + 'package.json').toString());
|
|
286
|
-
const { version } = packageJson;
|
|
287
|
-
return version;
|
|
288
|
-
}
|
|
289
|
-
export class UnitDimension {
|
|
290
|
-
type;
|
|
291
|
-
value;
|
|
292
|
-
constructor(value, type = LengthUnit.mils) {
|
|
293
|
-
this.value = value;
|
|
294
|
-
this.type = type;
|
|
295
|
-
}
|
|
296
|
-
getMM() {
|
|
297
|
-
switch (this.type) {
|
|
298
|
-
case LengthUnit.mm:
|
|
299
|
-
return this.value;
|
|
300
|
-
case LengthUnit.mils:
|
|
301
|
-
return this.value * MilsToMM;
|
|
302
|
-
case LengthUnit.px:
|
|
303
|
-
return this.value * PxToMM;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
static mm(value) {
|
|
307
|
-
return new UnitDimension(value, LengthUnit.mm);
|
|
308
|
-
}
|
|
309
|
-
static mils(value) {
|
|
310
|
-
return new UnitDimension(value, LengthUnit.mils);
|
|
311
|
-
}
|
|
312
|
-
static px(value) {
|
|
313
|
-
return new UnitDimension(value, LengthUnit.px);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
export function milsToMM(value) {
|
|
317
|
-
if (typeof value === 'number') {
|
|
318
|
-
value = resolveToNumericValue(new Big(value));
|
|
319
|
-
}
|
|
320
|
-
return resolveToNumericValue(value.toBigNumber().mul(new Big(MilsToMM)).round(6));
|
|
321
|
-
}
|
|
322
|
-
export function pxToMM(value) {
|
|
323
|
-
return value * PxToMM;
|
|
324
|
-
}
|
|
325
|
-
const PaperSizes = {
|
|
326
|
-
'A0': [1189, 841],
|
|
327
|
-
'A1': [841, 594],
|
|
328
|
-
'A2': [594, 420],
|
|
329
|
-
'A3': [420, 297],
|
|
330
|
-
'A4': [297, 210],
|
|
331
|
-
'A5': [210, 148],
|
|
332
|
-
'A6': [148, 105],
|
|
333
|
-
};
|
|
334
|
-
export const PaperGridReferences = {
|
|
335
|
-
'A0': [16, 24],
|
|
336
|
-
'A1': [12, 16],
|
|
337
|
-
'A2': [8, 12],
|
|
338
|
-
'A3': [6, 8],
|
|
339
|
-
'A4': [4, 6],
|
|
340
|
-
};
|
|
341
|
-
export function isSupportedPaperSize(type) {
|
|
342
|
-
if (PaperSizes[type]) {
|
|
343
|
-
return true;
|
|
344
|
-
}
|
|
345
|
-
return false;
|
|
346
|
-
}
|
|
347
|
-
export function getPaperSize(type, margin = defaultPageMarginMM) {
|
|
348
|
-
if (PaperSizes[type]) {
|
|
349
|
-
const [width, height] = PaperSizes[type];
|
|
350
|
-
const useWidth = width - margin * 2;
|
|
351
|
-
const useHeight = height - margin * 2;
|
|
352
|
-
return {
|
|
353
|
-
width: Math.floor(useWidth * (1 / MilsToMM)),
|
|
354
|
-
height: Math.floor(useHeight * (1 / MilsToMM)),
|
|
355
|
-
widthMM: useWidth,
|
|
356
|
-
heightMM: useHeight,
|
|
357
|
-
originalWidthMM: width,
|
|
358
|
-
originalHeightMM: height,
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
else {
|
|
362
|
-
return getPaperSize('A4');
|
|
363
|
-
}
|
|
364
|
-
}
|
package/dist/esm/index.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
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';
|
|
14
|
-
export * from './sizing.mjs';
|
|
15
|
-
export * from './objects/types.mjs';
|
package/dist/esm/parser.mjs
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { CircuitScriptParser } from './antlr/CircuitScriptParser.mjs';
|
|
2
|
-
import { MainLexer } from './lexer.mjs';
|
|
3
|
-
import { SimpleStopwatch } from './utils.mjs';
|
|
4
|
-
import { CharStream, CommonTokenStream, DefaultErrorStrategy } from 'antlr4ng';
|
|
5
|
-
export function parseFileWithVisitor(visitor, data) {
|
|
6
|
-
const chars = CharStream.fromString(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
|
-
const tree = parser.script();
|
|
15
|
-
let hasError = false;
|
|
16
|
-
try {
|
|
17
|
-
visitor.visit(tree);
|
|
18
|
-
}
|
|
19
|
-
catch (err) {
|
|
20
|
-
console.log(err);
|
|
21
|
-
hasError = true;
|
|
22
|
-
}
|
|
23
|
-
const parserTimeTaken = parserTimer.lap();
|
|
24
|
-
return {
|
|
25
|
-
tree, parser,
|
|
26
|
-
hasParseError: false,
|
|
27
|
-
hasError,
|
|
28
|
-
parserTimeTaken,
|
|
29
|
-
lexerTimeTaken,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
export class TempErrorStrategy extends DefaultErrorStrategy {
|
|
33
|
-
recover(recognizer, e) {
|
|
34
|
-
throw new Error('Method not implemented.');
|
|
35
|
-
}
|
|
36
|
-
reportError(recognizer, e) {
|
|
37
|
-
throw new Error('Method not implemented.');
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
export class CircuitscriptParserErrorListener {
|
|
41
|
-
syntaxErrorCounter = 0;
|
|
42
|
-
onErrorHandler = null;
|
|
43
|
-
constructor(onErrorHandler = null) {
|
|
44
|
-
this.onErrorHandler = onErrorHandler;
|
|
45
|
-
}
|
|
46
|
-
syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e) {
|
|
47
|
-
if (this.onErrorHandler) {
|
|
48
|
-
this.onErrorHandler(line, charPositionInLine, msg, e);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
console.log("Syntax error at line", line, ':', charPositionInLine, ' - ', msg);
|
|
52
|
-
}
|
|
53
|
-
this.syntaxErrorCounter++;
|
|
54
|
-
}
|
|
55
|
-
reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs) {
|
|
56
|
-
}
|
|
57
|
-
reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs) {
|
|
58
|
-
}
|
|
59
|
-
reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs) {
|
|
60
|
-
}
|
|
61
|
-
hasSyntaxErrors() {
|
|
62
|
-
return (this.syntaxErrorCounter > 0);
|
|
63
|
-
}
|
|
64
|
-
}
|