Comparing version 1.6.0 to 1.7.0
@@ -5,3 +5,3 @@ { | ||
"description": "An elegant armor-plated JavaScript lexer modelled after flex. Easily extensible to tailor to your need for perfection.", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"keywords": ["lex", "lexer", "lexical", "analysis", "scan", "scanner", "scanning", "token", "tokenize", "tokenizer", "tokenization", "flex", "jison"], | ||
@@ -8,0 +8,0 @@ "main": "lib/lexer.js", |
@@ -18,3 +18,5 @@ if (typeof module === "object" && typeof module.exports === "object") module.exports = Lexer; | ||
this.addRule = function (pattern, action, start) { | ||
if (!pattern.global) { | ||
var global = pattern.global; | ||
if (!global) { | ||
var flags = "g"; | ||
@@ -30,2 +32,3 @@ if (pattern.multiline) flags += "m"; | ||
pattern: pattern, | ||
global: global, | ||
action: action, | ||
@@ -104,2 +107,3 @@ start: start | ||
var matches = []; | ||
var index = 0; | ||
@@ -124,3 +128,3 @@ var state = this.state; | ||
matches.push({ | ||
var length = matches.push({ | ||
result: result, | ||
@@ -131,3 +135,5 @@ action: rule.action, | ||
while (j--) { | ||
if (rule.global) index = length; | ||
while (j-- > index) { | ||
var k = j + 1; | ||
@@ -134,0 +140,0 @@ |
{ | ||
"name": "lex", | ||
"description": "An elegant armor-plated JavaScript lexer modelled after flex. Easily extensible to tailor to your need for perfection.", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"keywords": ["lex", "lexer", "lexical", "analysis", "scan", "scanner", "scanning", "token", "tokenize", "tokenizer", "tokenization", "flex", "jison"], | ||
@@ -6,0 +6,0 @@ "author": "Aadit M Shah (http://aaditmshah.github.com/) <aaditmshah@myopera.com>", |
@@ -129,1 +129,23 @@ # Lexer # | ||
``` | ||
## Global Patterns ## | ||
Sometimes you may wish to match a pattern which need not necessarily generate the longest possible string. Since the scanner sorts the matched strings according to their length there's no way to do so. Hence in v1.7.0 I introduced global patterns. Strings matching these patterns are never sorted. This allows you to match a shorter strings before longer ones: | ||
```javascript | ||
var lexer = new Lexer; | ||
lexer.addRule(/^ */gm, function (lexeme) { | ||
console.log(lexeme.length); | ||
}); | ||
lexer.addRule(/[0-9]+/, function (lexeme) { | ||
console.log(lexeme); | ||
}); | ||
lexer.setInput("37"); | ||
lexer.lex(); | ||
``` | ||
The above program first logs the number of spaces at the beginning of the line (`0`) and then the number `37` although the length of the string `"37"` is greater than the empty string `""`. This is because it's the first rule and the `global` flag for its pattern is set. |
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
11505
132
151