Comparing version 0.3.0 to 0.3.1
44
moo.js
@@ -335,18 +335,2 @@ (function(root, factory) { | ||
// throw, if no rule with {error: true} | ||
if (!group) { | ||
// seek to end | ||
this.index = buffer.length | ||
var start = Math.max(0, index - this.col + 1) | ||
var eol = text.indexOf('\n') | ||
if (eol === -1) eol = text.length | ||
var line = buffer.slice(start, index + eol) | ||
var message = "" | ||
message += "invalid syntax at line " + this.line + " col " + this.col + ":\n\n" | ||
message += " " + line + "\n" | ||
message += " " + Array(this.col).join(" ") + "^" | ||
throw new Error(message) | ||
} | ||
} else { | ||
@@ -373,3 +357,3 @@ text = match[0] | ||
var lineBreaks = 0 | ||
if (group.lineBreaks) { | ||
if (!group || group.lineBreaks) { | ||
var matchNL = /\n/g | ||
@@ -386,3 +370,3 @@ var nl = 1 | ||
var token = { | ||
type: group.tokenType, | ||
type: group && group.tokenType, | ||
value: value, | ||
@@ -397,6 +381,2 @@ toString: tokenToString, | ||
if (group.pop) this.popState() | ||
else if (group.push) this.pushState(group.push) | ||
else if (group.next) this.setState(group.next) | ||
this.index += size | ||
@@ -409,2 +389,10 @@ this.line += lineBreaks | ||
} | ||
// throw, if no rule with {error: true} | ||
if (!group) { | ||
throw new Error(this.formatError(token, "invalid syntax")) | ||
} | ||
if (group.pop) this.popState() | ||
else if (group.push) this.pushState(group.push) | ||
else if (group.next) this.setState(group.next) | ||
return token | ||
@@ -428,2 +416,14 @@ } | ||
Lexer.prototype.formatError = function(token, message) { | ||
var value = token.value | ||
var index = token.offset | ||
var eol = token.lineBreaks ? value.indexOf('\n') : value.length | ||
var start = Math.max(0, index - token.col + 1) | ||
var firstLine = this.buffer.substring(start, index + eol) | ||
message += " at line " + token.line + " col " + token.col + ":\n\n" | ||
message += " " + firstLine + "\n" | ||
message += " " + Array(token.col).join(" ") + "^" | ||
return message | ||
} | ||
Lexer.prototype.reset = function(data, info) { | ||
@@ -430,0 +430,0 @@ this.buffer = data || '' |
{ | ||
"name": "moo", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Optimised tokenizer/lexer generator! 🐄 Uses /y for performance. Moo!", | ||
@@ -5,0 +5,0 @@ "main": "moo.js", |
@@ -66,15 +66,5 @@ ![](cow.png) | ||
You can also feed it chunks of input at a time. | ||
When you reach the end of Moo's internal buffer, next() will return `undefined`. You can always `reset()` it and feed it more data when that happens. | ||
```j | ||
lexer.reset() | ||
lexer.feed('while') | ||
lexer.feed(' 10 cows\n') | ||
lexer.next() // -> { type: 'keyword', value: 'while' } | ||
// ... | ||
``` | ||
If you've reached the end of Moo's internal buffer, next() will return `undefined`. You can always feed() it more if that happens. | ||
On Regular Expressions | ||
@@ -155,8 +145,8 @@ ---------------------- | ||
```js | ||
let state = lexer.save() // -> { line: 10 } | ||
lexer.feed('some line\n') | ||
lexer.reset('some line\n') | ||
let info = lexer.save() // -> { line: 10 } | ||
lexer.next() // -> { line: 10 } | ||
lexer.next() // -> { line: 11 } | ||
// ... | ||
lexer.reset('a different line\n', state) | ||
lexer.reset('a different line\n', info) | ||
lexer.next() // -> { line: 10 } | ||
@@ -269,3 +259,18 @@ ``` | ||
If you want to throw an error from your parser, you might find `formatError` helpful. Call it with the offending token: | ||
```js | ||
throw new Error(lexer.formatError(token, "invalid syntax")) | ||
``` | ||
And it returns a string with a pretty error message. | ||
``` | ||
Error: invalid syntax at line 2 col 15: | ||
totally valid `syntax` | ||
^ | ||
``` | ||
Iteration | ||
@@ -272,0 +277,0 @@ --------- |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22117
385
298