fstirlitz / luaparse

A Lua parser written in JavaScript
https://fstirlitz.github.io/luaparse/
MIT License
459 stars 91 forks source link

Locations of long string literals #36

Closed paulcuth closed 7 years ago

paulcuth commented 8 years ago

Hi,

Thanks for the great library, I've depended on this a few times and it has always worked superbly well.

However, I've come across an issue in the locations with long string literals. It seems that when the input expression spans many lines, the resulting column value for that expression is negative. For example:

const parser = require('luaparse');
const lua = 'local a = [[hello\nworld]]';
const ast = parser.parse(lua, { locations: true });

console.log(JSON.stringify(ast));

Results in:

{"type":"Chunk","body":[{"type":"LocalStatement","variables":[{"type":"Identifier","name":"a","loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}}}],"init":[{"type":"StringLiteral","value":"hello\nworld","raw":"[[hello\nworld]]","loc":{"start":{"line":2,"column":-8},"end":{"line":2,"column":7}}}],"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":7}}}],"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":7}},"comments":[]}

Note the "column":-8 in the middle.

I'll look into this sometime this week and open a PR if I can. However, if a possible cause pops into your head or you can give me any pointers in the meantime, that'd be great.

Cheers.

fstirlitz commented 7 years ago

Fixed in 4b56017a702789db0ea71011758667a511bc0d34.