diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-03-30 14:54:14 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-03-30 14:54:14 +0000 |
| commit | 461ae787be5a4fce406c07498cc176c99c9615a2 (patch) | |
| tree | 74b9adbff2a7080bc66ef0a5805b32444e16b706 /lib/style-reader.js | |
| parent | b2d02fec9a161cfd657f240806b25bdaca315eea (diff) | |
Move tokeniser into lop
Diffstat (limited to 'lib/style-reader.js')
| -rw-r--r-- | lib/style-reader.js | 46 |
1 files changed, 8 insertions, 38 deletions
diff --git a/lib/style-reader.js b/lib/style-reader.js index 1da03ba..7ed4132 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -1,5 +1,5 @@ var lop = require("lop"); -var StringSource = lop.StringSource; +var RegexTokeniser = lop.RegexTokeniser; var styles = require("./styles"); @@ -65,41 +65,11 @@ function describeFailure(parseResult) { } function tokenise(string) { - var source = new StringSource(string, "raw string"); - var index = 0; - var tokens = []; - - while (index < string.length) { - var nextToken = readNextToken(string, index, source); - if (!nextToken) { - throw new Error("Could not read token from: " + string.substring(index)); - } - index += nextToken.value.length; - tokens.push(nextToken); - } - tokens.push(new lop.Token("end", null, source.range(string.length, string.length))); - return tokens; -} - -function readNextToken(string, startIndex, source) { - for (var i = 0; i < tokenRegexes.length; i++) { - var regex = tokenRegexes[i].regex; - regex.lastIndex = startIndex; - var result = regex.exec(string); - if (result && result.index === startIndex) { - var value = result[0]; - return new lop.Token( - tokenRegexes[i].name, - value, - source.range(startIndex, startIndex + value.length) - ); - } - } + var tokeniser = new RegexTokeniser([ + {name: "identifier", regex: /([a-z][a-z0-9\-]*)/}, + {name: "dot", regex: /\./}, + {name: "colon", regex: /:/}, + {name: "whitespace", regex: /\s+/} + ]); + return tokeniser.tokenise(string); } - -var tokenRegexes = [ - {name: "identifier", regex: /([a-z][a-z0-9\-]*)/g}, - {name: "dot", regex: /\./g}, - {name: "colon", regex: /:/g}, - {name: "whitespace", regex: /\s+/g} -]; |
