Skip to content

Line numbers for better error handling (WIP) #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 49 additions & 21 deletions js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ class PhraseLexer extends Lexer {
} else if (this.checkEscapeChar('}')) {
result += '}';
this.advance();
this.advance();
this.advance();line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is now merged, but I don't understand this code. What's the line at the end? Just a typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is what it is 😓
I am sorry, let me just change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fdb I have made the correction in #43

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

} else {
result += this.currentChar;
this.advance();
Expand Down Expand Up @@ -524,8 +524,9 @@ class Node {
}

class Parser {
constructor(lexer) {
constructor(lexer, lineno) {
this.lexer = lexer;
this.lineno = lineno;
this.currentToken = this.lexer.nextToken();
}

Expand Down Expand Up @@ -643,6 +644,10 @@ class PhraseParser extends Parser {
node = new Node(NODE_STRING, { value: token.value });
} else if (token.type === KEY) {
this.consume(KEY);
if(this.currentToken.type === KEY){
throw new Error(`Invalid syntax: Whitespace not allowed in identifier at position ${this.lexer.pos}`);
}

node = new Node(NODE_KEY, { key: token.value });
node = this._name(node);
node = this._parameters(node);
Expand Down Expand Up @@ -728,11 +733,16 @@ class PhraseParser extends Parser {
}

parse() {
const node = this.phrase();
if (this.currentToken.type !== EOF) {
this.error(EOF);
try{
const node = this.phrase();
if (this.currentToken.type !== EOF) {
this.error(EOF);
}
return node;
}
catch(e){
throw new Error(`Line ${this.lineno}: ${e.message}`);
}
return node;
}
}

Expand All @@ -744,6 +754,10 @@ class DefParser extends Parser {
_key() {
let key = this.currentToken.value;
this.consume(KEY);
if(this.currentToken.type === KEY){
throw new Error(`Invalid syntax: Whitespace not allowed in identifier at position ${this.lexer.pos}`);
}

return key;
}

Expand Down Expand Up @@ -771,16 +785,21 @@ class DefParser extends Parser {
}

parse() {
const key = this._key();
const parameters = this._parameters();
this.consume(COLON);
if (this.currentToken.type !== EOF) {
this.error(EOF);
}
if (parameters.length === 0) {
return {key};
} else {
return {key, parameters};
try{
const key = this._key();
const parameters = this._parameters();
this.consume(COLON);
if (this.currentToken.type !== EOF) {
this.error(EOF);
}
if (parameters.length === 0) {
return {key};
} else {
return {key, parameters};
}
}
catch(e){
throw new Error(`Line ${this.lineno}: ${e.message}`);
}
}
}
Expand Down Expand Up @@ -1027,13 +1046,18 @@ class Interpreter {
}

interpret() {
return this.visit(this.phrase.tree);
try{
return this.visit(this.phrase.tree);
}
catch(e){
throw new Error(`Line ${this.phrase.lineno}: ${e.message}`)
}
}
}

function parsePhrase(phrase) {
function parsePhrase(phrase, lineno) {
const lexer = new PhraseLexer(phrase);
const parser = new PhraseParser(lexer);
const parser = new PhraseParser(lexer, lineno);
const tree = parser.parse();
return tree;
}
Expand Down Expand Up @@ -1162,11 +1186,14 @@ async function parsePhraseBook(s, loadSketch) {
throw new Error(`Line ${ i + 1 }: line without a key.`);
}
currentPhrase.values.push(line.substring(2));
currentPhrase.lines.push(i+1);
} else if (trimmedLine.endsWith(':')) {
// Keys end with ":"
let parser = new DefParser(new DefLexer(trimmedLine));
let parser = new DefParser(new DefLexer(trimmedLine), (i+1));
currentPhrase = parser.parse();
currentPhrase.values = [];
currentPhrase.lines = [];
currentPhrase.lineno = i+1;
phrases.push(currentPhrase);
} else {
throw new Error(`Line ${ i + 1 }: do not know what to do with line "${line}".`);
Expand All @@ -1192,7 +1219,8 @@ async function parsePhraseBook(s, loadSketch) {
}
const phraseBook = {};
for (let phrase of phrases) {
phraseBook[phrase.key] = phrase.values.map(text => ({text, tree: parsePhrase(text)}));
phraseBook[phrase.key] = phrase.values.map((text,index) => ({text, tree: parsePhrase(text,phrase.lines[index]), lineno:phrase.lines[index]}));
phraseBook[phrase.key].lineno = phrase.lineno;
if (phrase.parameters) {
phraseBook[phrase.key].parameters = phrase.parameters;
}
Expand Down