Created
May 1, 2013 20:15
-
-
Save mlconnor/5498010 to your computer and use it in GitHub Desktop.
Jison Gist for processing Java Generics like strings
This file contains 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 Parser = require("jison").Parser; | |
var grammar = { | |
"lex": { | |
"rules": [ | |
["\\s+", "/* skip whitespace */"], | |
[",", "return ','"], | |
["<", "return '<'"], | |
[">", "return '>'"], | |
["array", "return 'ARRAY'"], | |
["undefined|boolean|object|number|string", "return 'SIMPLETYPE'"], | |
["$", "return 'EOF'"] | |
] | |
}, | |
"bnf": { | |
// apparently you have to use the expressions tag for the return | |
"expressions" : [["typeList EOF","return $1;"]], | |
"typeList" : [[ "typeList , type", "$$ = ($1 instanceof Array) ? ($1).concat($3) : [$1,$3];"], | |
[ "type","$$ = $1;" ]], | |
"type" : [[ "genericArray", "$$ = $1;"], | |
[ "ARRAY", "$$ = $1;"], | |
[ "SIMPLETYPE", "$$ = $1;" ]], | |
"genericArray" : [[ "ARRAY < typeList >", "$$ = { \"arrayTypes\" : $3 };" ]] | |
} | |
}; | |
var parser = new Parser(grammar); | |
// generate source, ready to be written to disk | |
var parserSource = parser.generate(); | |
// you can also use the parser directly from memory | |
console.log('======================'); | |
parser.parse("number"); | |
console.log('======================'); | |
parser.parse("string,number"); | |
console.log('======================'); | |
parser.parse("string,number,array,boolean"); | |
var result = parser.parse("string,array<number,string,array<string,object>>,number,array<array>"); | |
console.log(result); | |
// throws lexical error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment