Skip to content

Commit

Permalink
Use jison and not hand rolled parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbabcock committed Feb 5, 2015
1 parent 833015e commit 4c7ece2
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 1,188 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
coverage/
lib/jomini.js
14 changes: 8 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var jomini = require('./');
var JS = require('json3');
var header = new jomini.Header({header: 'EU4txt'});
var parser = new jomini.Parser();
process.stdin.pipe(header).pipe(parser);
parser.on('finish', function() {
process.stdout.write(JS.stringify(parser.obj), 'utf8', function() {});
});
var concat = require('concat-stream');
var concatStream = concat(function(buf) {
var str = buf.toString('utf8');
var obj = jomini.parse(str);
process.stdout.write(JS.stringify(obj));
});

process.stdin.pipe(concatStream)
17 changes: 12 additions & 5 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ var jscs = require('gulp-jscs');
var mocha = require('gulp-mocha');
var jshint = require('gulp-jshint');
var istanbul = require('gulp-istanbul');
var jison = require('gulp-jison');

gulp.task('test', function(cb) {
gulp.src('lib/*.js')
gulp.task('test', ['jison'], function(cb) {
gulp.src(['lib/*.js', '!lib/jomini.js'])
.pipe(istanbul())
.pipe(istanbul.hookRequire())
.on('finish', function() {
Expand All @@ -20,15 +21,21 @@ gulp.task('test', function(cb) {
});
});

gulp.task('jison', function() {
return gulp.src('./src/*.jison')
.pipe(jison({ moduleType: 'commonjs' }))
.pipe(gulp.dest('./src/'));
});

gulp.task('lint', function() {
return gulp.src(['lib/**/*.js', 'test/**/*.js'])
return gulp.src(['lib/**/*.js', 'test/**/*.js', '!lib/jomini.js'])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});

gulp.task('main', ['test', 'lint'], function() {
return gulp.src(['lib/*js']).pipe(jscs({
gulp.task('main', ['jison', 'test', 'lint'], function() {
return gulp.src(['lib/*js', '!lib/jomini.js']).pipe(jscs({
preset: 'google'
}));
});
Expand Down
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
exports = module.exports;
exports.Parser = require('./lib/parser');
exports.Header = require('./lib/header');
exports.parse = require('./lib/parse');
exports.parse = require('./lib/jomini').parser.parse;
exports.toArray = require('./lib/toArray');
exports.toDate = require('./lib/toDate');
exports.toBool = require('./lib/toBool');
39 changes: 0 additions & 39 deletions lib/header.js

This file was deleted.

86 changes: 86 additions & 0 deletions lib/jomini.jison
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
%{
var toDate = require('./toDate');
var setProp = require('./setProp');
%}

/* lexical grammar */
%lex
%%

\s+ /* skip whitespace */
"yes"\b return 'BOOL'
"no"\b return 'BOOL'
[0-9]+"."[0-9]+"."[0-9]+\b return 'DATE'
'"'[0-9]+"."[0-9]+"."[0-9]+'"' yytext = yytext.substr(1,yyleng-2); return 'QDATE'
"-"?[0-9]+("."[0-9]+)?\b return 'NUMBER'
"{" return '{'
"}" return '}'
"=" return '='
\"[^\"]*\" yytext = yytext.substr(1,yyleng-2); return 'QIDENTIFIER'
[a-zA-Z0-9_]+ return 'IDENTIFIER'
"#"[^\r\n]*((\r\n)|<<EOF>>) /* skip comments */
. return 'INVALID'
<<EOF>> return 'EOF'


/lex

%start expressions

%% /* language grammar */

expressions
: PMemberList EOF
{ return obj; }
| IDENTIFIER PMemberList EOF
{ return obj; }
;

PMemberList
: PMember
{if(key) {nest.push(obj); obj = {}; setProp(obj, key, value);}}
| PMemberList PMember
{if (key) {setProp(obj, key, value);}}
;

PMember
: IDENTIFIER '=' PValue
{key = $1; value = $3;}
| DATE '=' PValue
{key = $1; value = $3;}
| IDENTIFIER '=' '{' '}'
{key = $1; value = {};}
| '{' '}'
{key = undefined;}
;

PList
: PValue
{nest.push(obj); obj = [$1];}
| PList PValue
{obj.push($2);}
;

PValue
: NUMBER
{$$ = +yytext;}
| BOOL
{$$ = yytext === 'yes';}
| QIDENTIFIER
{$$ = yytext;}
| IDENTIFIER
{$$ = yytext;}
| DATE
{$$ = toDate(yytext);}
| QDATE
{$$ = toDate(yytext);}
| '{' PMemberList '}'
{$$ = obj; obj = nest.pop();}
| '{' PList '}'
{$$ = obj; obj = nest.pop();}
;

%%
nest = [];
obj = {};
10 changes: 0 additions & 10 deletions lib/parse.js

This file was deleted.

Loading

0 comments on commit 4c7ece2

Please sign in to comment.