Skip to content

Instantly share code, notes, and snippets.

@paularmstrong
Created July 25, 2013 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paularmstrong/6084711 to your computer and use it in GitHub Desktop.
Save paularmstrong/6084711 to your computer and use it in GitHub Desktop.
diff --git a/lib/lexer.js b/lib/lexer.js
index 155a137..21e7159 100644
--- a/lib/lexer.js
+++ b/lib/lexer.js
@@ -1,36 +1,12 @@
var _ = require('lodash');
-var TYPES = exports.types = {
- WHITESPACE: 0,
- STRING: 1,
- FILTER: 3,
- FUNCTION: 4,
- PARENOPEN: 6,
- COMMA: 7,
- VAR: 8,
- NUMBER: 9,
- OPERATOR: 10,
- BRACKETOPEN: 11,
- BRACKETCLOSE: 12,
- DOTKEY: 13,
- ARRAYOPEN: 14,
- ARRAYCLOSE: 15,
- CURLYOPEN: 16,
- CURLYCLOSE: 17,
- COLON: 18,
- COMPARATOR: 19,
- LOGIC: 20,
- UNKNOWN: 100
- },
- rules = [
- {
- type: TYPES.WHITESPACE,
+var rules = {
+ WHITESPACE: {
regex: [
/^\s+/
]
},
- {
- type: TYPES.STRING,
+ STRING: {
regex: [
/^""/,
/^".*?[^\\]"/,
@@ -38,96 +14,86 @@ var TYPES = exports.types = {
/^'.*?[^\\]'/
]
},
- {
- type: TYPES.FILTER,
+ FILTER: {
regex: [
/^\|\s*(\w+)\(/
],
idx: 1
},
- {
- type: TYPES.FUNCTION,
+ FUNCTION: {
regex: [
/^\s*(\w+)\(/
],
idx: 1
},
- {
- type: TYPES.PARENOPEN,
+ PARENOPEN: {
regex: [
/^\(/
]
},
- {
- type: TYPES.PARENCLOSE,
+ PARENCLOSE: {
regex: [
/^\)/
]
},
- {
- type: TYPES.COMMA,
+ COMMA: {
regex: [
/^,/
]
},
- {
- type: TYPES.VAR,
+ VAR: {
regex: [
/^[a-zA-Z]\w*((\.\w*)+)?/,
/^[a-zA-Z]\w*/
]
},
- {
- type: TYPES.BRACKETOPEN,
+ BRACKETOPEN: {
+ regex: [
+ /^\[/
+ ]
+ },
+ ARRAYOPEN: {
regex: [
/^\[/
]
},
- {
- type: TYPES.BRACKETCLOSE,
+ BRACKETCLOSE: {
regex: [
/^\]/
]
},
- {
- type: TYPES.CURLYOPEN,
+ CURLYOPEN: {
regex: [
/^\{/
]
},
- {
- type: TYPES.COLON,
+ COLON: {
regex: [
/^\:/
]
},
- {
- type: TYPES.CURLYCLOSE,
+ CURLYCLOSE: {
regex: [
/^\}/
]
},
- {
- type: TYPES.DOTKEY,
+ DOTKEY: {
regex: [
/^\.(\w+)/,
],
idx: 1
},
- {
- type: TYPES.NUMBER,
+ NUMBER: {
regex: [
/^[+\-]?\d+(\.\d+)?/
]
},
- {
- type: TYPES.OPERATOR,
+ OPERATOR: {
regex: [
/^(\+|\-|\/|\*|%)/
]
},
- {
- type: TYPES.LOGIC,
+ LOGIC: {
regex: [
/^(&&|\|\||and|or)\s+/
],
@@ -137,8 +103,7 @@ var TYPES = exports.types = {
'or': '||'
}
},
- {
- type: TYPES.COMPARATOR,
+ COMPARATOR: {
regex: [
/^(===|==|\!==|\!=|<=|<|>=|>|in|gte|gt|lte|lt)\s+/
],
@@ -150,8 +115,7 @@ var TYPES = exports.types = {
'lt': '<'
}
},
- {
- type: TYPES.NOT,
+ NOT: {
regex: [
/^(not|\!)\s*/
],
@@ -160,12 +124,14 @@ var TYPES = exports.types = {
'not': '!'
}
}
- ];
+ },
+ keys = _.keys(rules),
+ TYPES = exports.types = _.zipObject(keys, keys);
function reader(str) {
var matched;
- _.some(rules, function (rule) {
+ _.some(rules, function (rule, type) {
return _.some(rule.regex, function (regex) {
var match = str.match(regex),
normalized;
@@ -179,7 +145,7 @@ function reader(str) {
matched = {
match: normalized,
- type: rule.type,
+ type: type,
length: match[0].length
};
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment