Permalink
Browse files

added jison grammar file

  • Loading branch information...
1 parent f909f4e commit 83100cd0c1cbb93f7c73e8c4b0bb1d2db1cfa0a0 @Ejhfast Ejhfast committed Feb 8, 2012
Showing with 105 additions and 0 deletions.
  1. +105 −0 typedjs_parser.jison
View
@@ -0,0 +1,105 @@
+/* description: Parses end executes mathematical expressions. */
+
+/* lexical grammar */
+%lex
+%%
+
+\s+ /* skip whitespace */
+"String" return 'string'
+"Number" return 'number'
+"Char" return 'char'
+"Boolean" return 'boolean'
+([a-z]|[A-Z]|\_|\.)+ return 'VAR'
+"//+" return 'BEGIN'
+"," return ','
+"(" return '('
+")" return ')'
+"[" return '['
+"]" return ']'
+"{" return '{'
+"}" return '}'
+"::" return '::'
+"->" return '->'
+":" return ':'
+"|" return '|'
+<<EOF>> return 'EOF'
+. return 'INVALID'
+
+/lex
+
+/* operator associations and precedence */
+
+%left ','
+%left ':'
+%left '|'
+
+
+%start expressions
+
+%% /* language grammar */
+
+expressions
+ : BEGIN VAR "::" arg_list EOF
+ {return '{"func":"' + $2 + '","args":[' +$4 + ']}';}
+ ;
+
+arg_list
+ : e
+ {$$ = $1;}
+ | arg_list "->" e
+ {$$ = $1 +"," + $3;}
+ ;
+
+primitive
+ : string
+ {$$ = '"string"';}
+ | char
+ {$$ = '"char"';}
+ | number
+ {$$ = '"number"';}
+ | boolean
+ {$$ = '"boolean"';}
+ ;
+
+tuple
+ : '(' e ')'
+ {$$ = "["+$2+"]";}
+ ;
+
+object
+ : '{' e '}'
+ {$$ = "{"+$2+"}";}
+ ;
+
+list
+ : '[' primitive ']'
+ {$$ = '{"array":' + $2 + '}';}
+ | '[' object ']'
+ {$$ = '{"array":' + $2 + '}';}
+ | '[' tuple ']'
+ {$$ = '{"array":' + $2 + '}';}
+ | '[' list ']'
+ {$$ = '{"array":' + $2 + '}';}
+ | '[' or ']'
+ {$$ = '{"array":{"or":[' + $2 + ']}}'; }
+ ;
+
+or
+ : e '|' e
+ {$$ = $1 + ',' + $3;}
+ ;
+
+e
+ : e ',' e
+ {$$ = $1+','+$3;}
+ | or
+ {$$ = '{"or":[' + $1 + ']}';}
+ | object
+ | tuple
+ | list
+ | primitive
+ | e ':' e
+ {$$ = $1 +':'+ $3;}
+ | VAR
+ {$$ = '"' + String(yytext) + '"';}
+ ;

0 comments on commit 83100cd

Please sign in to comment.