aaditmshah/lexer

"Parse error on line NaN: Unexpected..."

Closed this issue · 2 comments

Looks like the lexer can't communicate the location info to Jison. Many times, when there's a syntax error, I get that message.

My lexer looks like this:

let col = 1
let row = 1

const lexer = new Lexer(function (char) {
  throw new Error(`Unexpected character '${char}' at line ${row}, column ${col}`)
})

lexer.addRule(/(?:\r?\n)+/, function (lexeme) {
  col = 1
  row += lexeme.length

  return 'NEWLINE'
})

// ...

I tried setting yyloc like in the documentation (https://zaa.ch/jison/docs/#lexical-analysis) but it doesn't seem to work.

Hey! I was facing the same error.

I noticed jison uses a property called yylineno in that message. If you set that property inside your rule, the message returns the right line number. At least this was how I was able to workaround this issue.

Closing this issue because it's about Jison.