Skip to content

Commit

Permalink
Add location reporting to compiler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
grind086 committed Sep 20, 2017
1 parent f05c7be commit 9f5ff27
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loot-ml",
"version": "0.0.7",
"version": "0.0.8",
"description": "A language for creating loot tables",
"main": "index.js",
"author": "Rob Grindeland",
Expand Down
18 changes: 16 additions & 2 deletions src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Compiler {
this.compileSelector(obj as SelectorObject);
break;
default:
throw new Error(`Invalid parser object type ${obj.type}`);
this.compileError(`Invalid parser object type ${obj.type}`, obj.location);
}
}

Expand Down Expand Up @@ -163,7 +163,7 @@ class Compiler {
weights
},
(err: string) => {
throw new Error(err);
throw this.compileError(`${sel.identifier}: ${err}`, selector.location);
}
);

Expand Down Expand Up @@ -206,6 +206,20 @@ class Compiler {
public buildRepeater(fn: string, count: string): string {
return `()=>((i,n,o)=>{for(;i<n;i++)o.push((${fn})());return o})(0,${count},[])`;
}

/**
* Throws a compile error
* @param message Error message
* @param location Error location
*/
public compileError(message: string, location: { line: number; column: number } | null) {
if (location) {
const { line, column } = location;
throw new Error(`${message} (at ${line}:${column})`);
} else {
throw new Error(message);
}
}
}

export default Compiler;
9 changes: 5 additions & 4 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Parser {
}

if (!this._aliasObjects.hasOwnProperty(token.value)) {
this._aliasObjects[token.value] = new AliasObject(token.value, alias!);
this._aliasObjects[token.value] = new AliasObject(token.value, alias!, token.location);
}

return this._aliasObjects[token.value];
Expand All @@ -229,22 +229,23 @@ class Parser {

const list = this.parseIdentifierList();

return new SelectorObject(selector!, args, list);
return new SelectorObject(selector!, args, list, token.location);
}

/**
* Parses an item
*/
public parseItem(): ItemObject {
this.expectToken(TOKEN_TYPE.IDENTIFIER, true, 'item');
const token = this.expectToken(TOKEN_TYPE.IDENTIFIER, true, 'item');

this.expectToken(TOKEN_TYPE.LEFT_BRACKET, true);

const type = this.parseString();
const amount = this.matchToken(TOKEN_TYPE.COMMA, true) ? this.parseAmount() : 1;

this.expectToken(TOKEN_TYPE.RIGHT_BRACKET, true);

return new ItemObject(type, amount);
return new ItemObject(type, amount, token.location);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/parserObjects/AliasObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class AliasObject implements ParserObject {

constructor(
public name: string,
public identifier: ParserObject
public identifier: ParserObject,
public location: { line: number, column: number }
) {}
}

Expand Down
3 changes: 2 additions & 1 deletion src/parserObjects/ItemObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class ItemObject implements ParserObject {

constructor(
public name: string,
public amount: number | number[]
public amount: number | number[],
public location: { line: number, column: number }
) {}
}

Expand Down
3 changes: 2 additions & 1 deletion src/parserObjects/SelectorObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class SelectorObject implements ParserObject {
constructor(
public selector: Selector,
public args: Argument[],
public list: WeightedIdentifier[]
public list: WeightedIdentifier[],
public location: { line: number, column: number }
) {}
}

Expand Down
6 changes: 3 additions & 3 deletions src/selectors/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const RepeatSelector: Selector = {

compile({ list, args, compiledList }, err) {
if (args.length < 1) {
err(`repeat: Expected 1 argument but got ${args.length}`);
err(`Expected 1 argument but got ${args.length}`);
}

if ('string' === typeof args[0]) {
err(`repeat: First argument must be number or range`);
err(`First argument must be number or range`);
}

if (list.length !== 1) {
err('repeat: Must have exactly one element in the list');
err('Must have exactly one element in the list');
}

const amount: number | number[] = args[0] as any;
Expand Down
4 changes: 2 additions & 2 deletions src/selectors/someOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const SomeOfSelector: Selector = {

compile({ list, args, compiledList, isWeighted, totalWeights, weights }, err) {
if (args.length < 1) {
err(`oneOf: Expected 1 argument but got ${args.length}`);
err(`Expected 1 argument but got ${args.length}`);
}

if ('string' === typeof args[0]) {
err(`oneOf: First argument must be number or range`);
err(`First argument must be number or range`);
}

const amount: number | number[] = args[0] as any;
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface ItemResult {
export interface ParserObject {
type: PARSER_OBJECT_TYPE;
compiled: string;
location: { line: number, column: number };
}

/**
Expand Down

0 comments on commit 9f5ff27

Please sign in to comment.