Skip to content

Commit

Permalink
Convert to jison.
Browse files Browse the repository at this point in the history
  • Loading branch information
pashields committed Mar 11, 2014
1 parent 2c1cdd5 commit 9344615
Show file tree
Hide file tree
Showing 5 changed files with 672 additions and 1,199 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "zerkel",
"version": "1.1.0",
"version": "1.2.0",
"description": "A compiler for the zerkel language",
"dependencies": {
"coffee-script": "1.6.3",
"coffee-script": "1.7.1",
"browserify": "2.22.0"
},
"devDependencies": {
"chai": "1.2.0",
"mocha": "1.4.0",
"pegjs": "0.7.0"
"jison": "0.4.13"
},
"scripts": {
"test": "node_modules/.bin/mocha -R dot"
Expand Down
67 changes: 67 additions & 0 deletions src/zerkel-parser.jison
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* lexical grammar */
%lex
%%
\s+ {/* skip whitespace */}
"and"|"AND" {return 'AND';}
"or"|"OR" {return 'OR';}
"not"|"NOT" {return 'NOT';}
"=" {return '=';}
"<>" {return '<>';}
">"|"<"|"<="|">=" {return 'LTGT';}
"contains"|"CONTAINS" {return 'CONTAINS';}
"like"|"LIKE" {return 'LIKE';}
[0-9]+ {return 'INTEGER';}
\"[^\"]*\" {return 'STRING';}
[A-Za-z]+ {return 'VAR';}
"(" {return 'OPEN';}
")" {return 'CLOSE';}
<<EOF>> {return 'EOF';}

/lex

/* operator associations and precedence */

%left '=' '<>'
%left 'LTGT'
%left 'AND' 'OR'
%left 'NOT'
%left 'CONTAINS' 'LIKE'

%start expressions

%% /* language grammar */

expressions
: e EOF
{return $1;}
;
e
: 'NOT' e
{$$ = "!" + $2;}
| 'OPEN' e 'CLOSE'
{$$ = "(" + $2 + ")";}
| e 'AND' e
{$$ = $1 + " && " + $3;}
| e 'OR' e
{$$ = $1 + " || " + $3;}
| value '=' value
{$$ = $1 + "==" + $3;}
| value '<>' value
{$$ = $1 + "!=" + $3;}
| value 'LTGT' value
{$$ = $1 + $2 + $3}
| value 'CONTAINS' value
{$$ = "(" + $1 + "&&" + $1 + ".indexOf(" + $3 + ")" + " >= 0)"; }
| value 'LIKE' value
{$$ = "_helpers['match'](" + $1 + "," + $3 + ")";}
| value
{$$ = $1;}
;
value
: INTEGER
{$$ = Number(yytext);}
| STRING
{$$ = yytext;}
| VAR
{$$ = "_env['" + yytext + "']";}
;
Loading

0 comments on commit 9344615

Please sign in to comment.