Last active
August 29, 2015 14:05
-
-
Save laughinghan/11b56580b74985b3a8d0 to your computer and use it in GitHub Desktop.
scan that shit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var patterns = []; | |
(function (args) { | |
for (var i = 0; i < args.length; i++) { | |
if (!args[i]) continue; | |
var parts = args[i].source.split('::='); | |
var name = parts[0].trim(); | |
var pattern = RegExp('^(?:' + parts[1].trim() + ')'); | |
patterns.push([name, pattern]); | |
} | |
})([ | |
/ left ::= [{(\[] /, | |
/ right ::= [})\]] /, | |
/ newline ::= \r\n|\r|\n /, | |
/ whitespace ::= \s+ /, | |
/ hash ::= #[^\r\n]* /, | |
/ line ::= \/\/[^\r\n]* /, | |
/ block ::= \/\*.*?\*\/ /, | |
/ identifier ::= [a-zA-Z][a-zA-z0-9]* /, | |
/ single ::= '(\\['\\/bfnrtu']|[^'\\'])*' /, | |
/ double ::= "(\\["\\/bfnrtu"]|[^"\\"])*" /, | |
/ error ::= '|"[^\r\n]* /, | |
/ integer ::= [0-9]+ /, | |
/ junk ::= [^\r \n {(\[ \])} # ' " \s]* /, | |
]); | |
return function (s) { | |
for (var i = 0; i < patterns.length; i++) { | |
var name = patterns[i][0]; | |
var re = patterns[i][1]; | |
var match = s.match(re); | |
if (match) { | |
var lexeme = match[0]; | |
return [name, lexeme]; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment