From 7c93c816c58e3dc6b25ebf158b5297d4f0bf55b0 Mon Sep 17 00:00:00 2001 From: Shahar Soel Date: Thu, 16 Jul 2015 23:40:43 +0300 Subject: [PATCH] added 'length' property for lexingError. --- src/scan/lexer_public.ts | 6 ++++-- test/scan/lexer_spec.ts | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/scan/lexer_public.ts b/src/scan/lexer_public.ts index ab63ad621..e2bf644ac 100644 --- a/src/scan/lexer_public.ts +++ b/src/scan/lexer_public.ts @@ -16,6 +16,7 @@ module chevrotain { export interface ILexingError { line:number column:number + length:number message:string } @@ -107,7 +108,7 @@ module chevrotain { * @returns {{tokens: {Token}[], errors: string[]}} */ public tokenize(text:string):ILexingResult { - var match, i, j, matchAlt, longerAltIdx, matchedImage, imageLength, group, tokClass, newToken, + var match, i, j, matchAlt, longerAltIdx, matchedImage, imageLength, group, tokClass, newToken, errLength, canMatchedContainLineTerminator, fixForEndingInLT, c, droppedChar, lastLTIdx, errorMessage, lastCharIsLT var orgInput = text var offset = 0 @@ -219,10 +220,11 @@ module chevrotain { } } + errLength = offset - errorStartOffset // at this point we either re-synced or reached the end of the input text errorMessage = `unexpected character: ->${orgInput.charAt(errorStartOffset)}<- at offset: ${errorStartOffset},` + ` skipped ${offset - errorStartOffset} characters.` - errors.push({line: errorLine, column: errorColumn, message: errorMessage}) + errors.push({line: errorLine, column: errorColumn, length: errLength, message: errorMessage}) } } diff --git a/test/scan/lexer_spec.ts b/test/scan/lexer_spec.ts index 2f85dc7e3..14753880b 100644 --- a/test/scan/lexer_spec.ts +++ b/test/scan/lexer_spec.ts @@ -310,6 +310,7 @@ module chevrotain.lexer.spec { expect(_.contains(lexResult.errors[0].message, "@")).to.equal(true) expect(lexResult.errors[0].line).to.equal(1) expect(lexResult.errors[0].column).to.equal(18) + expect(lexResult.errors[0].length).to.equal(6) expect(lexResult.tokens).to.deep.equal([new If("if", 0, 1, 1), new LParen("(", 3, 1, 4), new Integer("666", 4, 1, 5), new RParen(")", 7, 1, 8), new Return("return", 9, 1, 10), new Integer("1", 16, 1, 17), new Else("else", 25, 2, 2), new Return("return", 30, 2, 7), new Integer("2", 37, 2, 14) @@ -328,6 +329,7 @@ module chevrotain.lexer.spec { expect(_.contains(lexResult.errors[0].message, "&")).to.equal(true) expect(lexResult.errors[0].line).to.equal(1) expect(lexResult.errors[0].column).to.equal(3) + expect(lexResult.errors[0].length).to.equal(28) expect(lexResult.tokens).to.deep.equal([new If("if", 0, 1, 1)]) }) @@ -340,14 +342,17 @@ module chevrotain.lexer.spec { expect(_.contains(lexResult.errors[0].message, "\r")).to.equal(true) expect(lexResult.errors[0].line).to.equal(1) expect(lexResult.errors[0].column).to.equal(3) + expect(lexResult.errors[0].length).to.equal(2) expect(_.contains(lexResult.errors[1].message, "\r")).to.equal(true) expect(lexResult.errors[1].line).to.equal(2) expect(lexResult.errors[1].column).to.equal(5) + expect(lexResult.errors[1].length).to.equal(1) - expect(_.contains(lexResult.errors[1].message, "\r")).to.equal(true) + expect(_.contains(lexResult.errors[2].message, "\r")).to.equal(true) expect(lexResult.errors[2].line).to.equal(3) expect(lexResult.errors[2].column).to.equal(3) + expect(lexResult.errors[2].length).to.equal(1) expect(lexResult.tokens).to.deep.equal([new If("if", 0, 1, 1), new Else("else", 4, 2, 1), new If("if", 9, 3, 1)]) })