Skip to content

Commit

Permalink
Added error code fragment output in case of some issues of the code t…
Browse files Browse the repository at this point in the history
…o rapify
  • Loading branch information
winseros committed Mar 24, 2017
1 parent 4a64211 commit e4a8c80
Show file tree
Hide file tree
Showing 37 changed files with 349 additions and 198 deletions.
4 changes: 2 additions & 2 deletions src/optimizer/test/treeOptimizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('optimizer/treeOptimizer', () => {
it('should try to parse string nodes and catch ParserErrors', () => {
const value = new StringNode('str1');

spyOn(ExpressionParser.prototype, 'parseExpression').and.callFake(() => { throw new ParserError('msg', 0, 0); });
spyOn(ExpressionParser.prototype, 'parseExpression').and.callFake(() => { throw new ParserError('msg', 0, 0, 0); });
spyOn(ExpressionResolver.prototype, 'resolve').and.callThrough();

const optimizer = new TreeOptimizer();
Expand All @@ -184,7 +184,7 @@ describe('optimizer/treeOptimizer', () => {
it('should try to parse string nodes and catch NodeErrors', () => {
const value = new StringNode('str1');

spyOn(ExpressionParser.prototype, 'parseExpression').and.callFake(() => { throw new NodeError('msg', 0, 0); });
spyOn(ExpressionParser.prototype, 'parseExpression').and.callFake(() => { throw new NodeError('msg', 0, 0, 0); });
spyOn(ExpressionResolver.prototype, 'resolve').and.callThrough();

const optimizer = new TreeOptimizer();
Expand Down
4 changes: 4 additions & 0 deletions src/parser/charIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ export class CharIterator implements Iterator<string> {
get column(): number {
return this._column;
}

get index(): number {
return this._bufferIndex - 1;
}
}
4 changes: 4 additions & 0 deletions src/parser/charIteratorCheckpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class CharIteratorCheckpoint implements Checkpoint<string> {
return this._params.iterator.column;
}

get index(): number {
return this._params.iterator.index;
}

get depleted(): boolean {
return this._params.iterator.depleted;
}
Expand Down
2 changes: 2 additions & 0 deletions src/parser/iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface Iterator<T> {

column: number;

index: number;

createCheckpoint(): Checkpoint<T>;
}

Expand Down
9 changes: 6 additions & 3 deletions src/parser/nodeError.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import '../error';

export class NodeError extends Error {
constructor(message: string, line: number, column: number) {
constructor(message: string, line: number, column: number, index: number) {
super(message);
Error.captureStackTrace(this, NodeError);

this.line = line;
this.comumn = column;
this.index = index;
this.name = 'NodeError';
}

line: number;
readonly line: number;

comumn: number;
readonly comumn: number;

readonly index: number;
}
8 changes: 4 additions & 4 deletions src/parser/nodes/readers/expressionReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export class ExpressionReader {
if (halt) { break; }

if (iterator.current.tokenType === tokenTypes.newline) {
throw new NodeError(`; expected at the end of the line`, iterator.line, iterator.column);
throw new NodeError(`; expected at the end of the line`, iterator.line, iterator.column, iterator.index);
} else if (iterator.current.tokenType !== tokenTypes.mathOp) {
throw new NodeError(`Math operator expected but was "${iterator.current.tokenValue}" of type "${iterator.current.tokenType}"`, iterator.line, iterator.column);
throw new NodeError(`Math operator expected but was "${iterator.current.tokenValue}" of type "${iterator.current.tokenType}"`, iterator.line, iterator.column, iterator.index);
}

const operator = iterator.current;
Expand Down Expand Up @@ -110,7 +110,7 @@ export class ExpressionReader {
break;
}
default: {
throw new NodeError(`Unexpected token "${token.tokenValue}" of type "${token.tokenType}"`, this._reader.iterator.line, this._reader.iterator.column);
throw new NodeError(`Unexpected token "${token.tokenValue}" of type "${token.tokenType}"`, this._reader.iterator.line, this._reader.iterator.column, this._reader.iterator.index);
}
}

Expand All @@ -125,7 +125,7 @@ export class ExpressionReader {
const result = token.tokenValue === mathOperators.minus ? new MathNegNode(node) : node;
return result;
} else {
throw new NodeError(`Unexpected math operator "${token.tokenValue}" of type "${token.tokenType}"`, this._reader.iterator.line, this._reader.iterator.column);
throw new NodeError(`Unexpected math operator "${token.tokenValue}" of type "${token.tokenType}"`, this._reader.iterator.line, this._reader.iterator.column, this._reader.iterator.index);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/parser/nodes/readers/readerUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export class ReaderUtility {
const eof = this.moveToNextTokenOrEof();
if (eof) {
this._resetDefaults();
throw new NodeError(`${errorDescription} expected but got EOF`, this._iterator.line, this._iterator.column);
throw new NodeError(`${errorDescription} expected but got EOF`, this._iterator.line, this._iterator.column, this._iterator.index);
}

const current = this._iterator.current;
if (expectedTokenTypes.indexOf(current.tokenType) < 0) {
this._resetDefaults();

const value = this._getTokenValue(current);
throw new NodeError(`${errorDescription} expected but got "${value}"`, this._iterator.line, this._iterator.column);
throw new NodeError(`${errorDescription} expected but got "${value}"`, this._iterator.line, this._iterator.column, this._iterator.index);
}

this._resetDefaults();
Expand Down Expand Up @@ -76,7 +76,7 @@ export class ReaderUtility {
const eof = this.moveToNextTokenOrEof();
if (eof) {
this._resetDefaults();
throw new NodeError(`Any token expected but got EOF`, this._iterator.line, this._iterator.column);
throw new NodeError(`Any token expected but got EOF`, this._iterator.line, this._iterator.column, this._iterator.index);
}
}
}
2 changes: 1 addition & 1 deletion src/parser/nodes/readers/readers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export class Readers {

const iterator = reader.iterator;
const message = `Unexpected token: \"${iterator.current.tokenValue}\", type: ${iterator.current.tokenType}`;
throw new NodeError(message, iterator.line, iterator.column);
throw new NodeError(message, iterator.line, iterator.column, iterator.index);
}
}

0 comments on commit e4a8c80

Please sign in to comment.