Skip to content

Commit 0cfc26b

Browse files
committed
change compiler to use ranges
1 parent d4506d4 commit 0cfc26b

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/compiler.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class SyntaxScriptCompiler {
6868
* Compiles one .syx file from the path given.
6969
* @param {string} file Path to a file to compile.
7070
* @author efekos
71-
* @version 1.0.2
71+
* @version 1.0.3
7272
* @since 0.0.1-alpha
7373
*/
7474
public compileSyx(file: string) {
@@ -106,7 +106,7 @@ export class SyntaxScriptCompiler {
106106
const compileStmt = stmt as CompileStatement;
107107

108108
compileStmt.formats.forEach(frmt => {
109-
if (operatorStmtExport.outputGenerators[frmt] !== undefined) throw new CompilerError({ character: compileStmt.pos, line: compileStmt.line }, `Duplicate file format at compile statement \'${frmt}\'`);
109+
if (operatorStmtExport.outputGenerators[frmt] !== undefined) throw new CompilerError(compileStmt.range, `Duplicate file format at compile statement \'${frmt}\'`);
110110

111111
operatorStmtExport.outputGenerators[frmt] = (src) => {
112112
let out = '';
@@ -117,7 +117,7 @@ export class SyntaxScriptCompiler {
117117
const varExpr = e as VariableExpression;
118118
const v = src.match(new RegExp(regexes[varExpr.value].source, 'g'))[varExpr.index];
119119

120-
if (v === undefined) throw new CompilerError({ character: compileStmt.pos, line: compileStmt.line }, 'Unknown statement/expression.');
120+
if (v === undefined) throw new CompilerError(compileStmt.range, 'Unknown statement/expression.');
121121
out += v;
122122
} else if (e.type === NodeType.WhitespaceIdentifier) out += ' ';
123123
});
@@ -131,11 +131,11 @@ export class SyntaxScriptCompiler {
131131
const importStmt = stmt as ImportsStatement;
132132

133133
importStmt.formats.forEach(frmt => {
134-
if (operatorStmtExport.imports[frmt] !== undefined) throw new CompilerError({ character: importStmt.pos, line: importStmt.line }, `Duplicate file format at imports statement \'${frmt}\'`);
134+
if (operatorStmtExport.imports[frmt] !== undefined) throw new CompilerError(importStmt.range, `Duplicate file format at imports statement \'${frmt}\'`);
135135
operatorStmtExport.imports[frmt] = importStmt.module;
136136
});
137137

138-
} else throw new CompilerError({line:stmt.line,character:stmt.pos}, `Unexpected \'${stmt.type}\' statement insdie operator statement.`);
138+
} else throw new CompilerError(stmt.range, `Unexpected \'${stmt.type}\' statement insdie operator statement.`);
139139
});
140140

141141
out.push(operatorStmtExport);
@@ -147,15 +147,15 @@ export class SyntaxScriptCompiler {
147147

148148
if (statement.type === NodeType.Compile) {
149149
const compileStatement = statement as CompileStatement;
150-
if (compileStatement.body[0].type !== NodeType.String) throw new CompilerError({ character: compileStatement.pos, line: compileStatement.line }, 'Expected a string after compile statement parens');
150+
if (compileStatement.body[0].type !== NodeType.String) throw new CompilerError(compileStatement.range, 'Expected a string after compile statement parens');
151151
compileStatement.formats.forEach(each => {
152-
if (statementExport.formatNames[each] !== undefined) throw new CompilerError({ character: compileStatement.pos, line: compileStatement.line }, `Encountered multiple compile statements for target language '${each}'`);
152+
if (statementExport.formatNames[each] !== undefined) throw new CompilerError(compileStatement.range, `Encountered multiple compile statements for target language '${each}'`);
153153
statementExport.formatNames[each] = compileStatement.body[0].value;
154154
});
155155
} else if (statement.type === NodeType.Imports) {
156156
const importsStatement = statement as ImportsStatement;
157157
importsStatement.formats.forEach(each => {
158-
if (statementExport.imports[each] !== undefined) throw new CompilerError({ character: importsStatement.pos, line: importsStatement.line }, `Encountered multiple import statements for target language '${each}'`);
158+
if (statementExport.imports[each] !== undefined) throw new CompilerError(importsStatement.range, `Encountered multiple import statements for target language '${each}'`);
159159
statementExport.imports[each] = importsStatement.module;
160160
});
161161
}
@@ -168,7 +168,7 @@ export class SyntaxScriptCompiler {
168168
const stmt = exported as KeywordStatement;
169169

170170
out.push({ type: ExportType.Keyword, word: stmt.word });
171-
} else throw new CompilerError({ character: statement.pos, line: statement.line }, `Unexpected \'${statement.type}\' statement after export statement.`);
171+
} else throw new CompilerError(statement.range, `Unexpected \'${statement.type}\' statement after export statement.`);
172172

173173
});
174174

@@ -197,7 +197,7 @@ export class SyntaxScriptCompiler {
197197
* @param {string} file Path to the .sys file to compile.
198198
* @author efekos
199199
* @since 0.0.1-alpha
200-
* @version 1.0.1
200+
* @version 1.0.2
201201
*/
202202
public compileSys(file: string) {
203203
const ast = sysparser.parseTokens(tokenizeSys(readFileSync(file).toString()), this.watchMode);
@@ -209,10 +209,10 @@ export class SyntaxScriptCompiler {
209209
const importStmt = stmt as ImportStatement;
210210

211211
const pathToImport = join(dirname(file), importStmt.path.endsWith('.syx') ? importStmt.path : importStmt.path + '.syx');
212-
if (!existsSync(pathToImport)) throw new CompilerError({ character: importStmt.pos, line: importStmt.line }, `File \'${pathToImport}\' imported from \'${file}\' does not exist.`);
212+
if (!existsSync(pathToImport)) throw new CompilerError(importStmt.range, `File \'${pathToImport}\' imported from \'${file}\' does not exist.`);
213213
this.exportData[pathToImport].forEach(exported => {
214214
if (exported.type === ExportType.Operator)
215-
if (imported.filter(r => r.type === ExportType.Operator).some(i => exported.regexMatcher === (i as Operator).regexMatcher)) throw new CompilerError({ character: importStmt.pos, line: importStmt.line }, `There are more than one operators with the same syntax imported to \'${file}\'.`);
215+
if (imported.filter(r => r.type === ExportType.Operator).some(i => exported.regexMatcher === (i as Operator).regexMatcher)) throw new CompilerError(importStmt.range, `There are more than one operators with the same syntax imported to \'${file}\'.`);
216216
imported.push(exported);
217217
});
218218
}
@@ -235,11 +235,11 @@ export class SyntaxScriptCompiler {
235235
imported.forEach(i => {
236236

237237
if (i.type === ExportType.Operator) {
238-
if (i.outputGenerators[this.mainFileFormat] === undefined) throw new CompilerError({ character: 1, line: 1 }, `Can't compile operator to target language (${this.mainFileFormat}).`);
238+
if (i.outputGenerators[this.mainFileFormat] === undefined) throw new CompilerError({end:{character:0,line:0},start:{character:0,line:0}}, `Can't compile operator to target language (${this.mainFileFormat}).`);
239239
fileContent = fileContent.replace(new RegExp(i.regexMatcher.source, 'g'), i.outputGenerators[this.mainFileFormat]);
240240
if (i.imports[this.mainFileFormat] !== undefined && !imports.includes(i.imports[this.mainFileFormat])) imports.push(i.imports[this.mainFileFormat]);
241241
} else if (i.type === ExportType.Function) {
242-
if (i.formatNames[this.mainFileFormat] === undefined) throw new CompilerError({ character: 1, line: 1 }, `Can't compile function to target language (${this.mainFileFormat}).`);
242+
if (i.formatNames[this.mainFileFormat] === undefined) throw new CompilerError({end:{character:0,line:0},start:{character:0,line:0}}, `Can't compile function to target language (${this.mainFileFormat}).`);
243243
fileContent = fileContent.replace(new RegExp(i.name + '\\(' + i.args.map(m => m.source).join(',') + '\\)', 'g'), (m) => m.replace(i.name, i.formatNames[this.mainFileFormat]));
244244
}
245245

0 commit comments

Comments
 (0)