Skip to content

Commit

Permalink
added 'length' property for lexingError.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahar Soel committed Jul 16, 2015
1 parent 7777d69 commit 7c93c81
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/scan/lexer_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module chevrotain {
export interface ILexingError {
line:number
column:number
length:number
message:string
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})
}
}

Expand Down
7 changes: 6 additions & 1 deletion test/scan/lexer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)])
})

Expand All @@ -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)])
})

Expand Down

0 comments on commit 7c93c81

Please sign in to comment.