Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
zaach committed Jan 27, 2013
0 parents commit 8132b7d
Show file tree
Hide file tree
Showing 7 changed files with 1,476 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# jison-lex
A lexical analyzer generator used by [jison](http://jison.org).

## install
npm install jison-lex -g

## usage
```
Usage: jison-lex [file] [options]
file file containing a lexical grammar
Options:
-o FILE, --outfile FILE Filename and base module name of the generated parser
-t TYPE, --module-type TYPE The type of module to generate (commonjs, js)
--version print version and exit
```

## license
MIT
87 changes: 87 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env node

var version = require('./package.json').version;

var path = require('path');
var fs = require('fs');
var lexParser = require('lex-parser');
var RegExpLexer = require('./regexp-lexer.js');


var opts = require("nomnom")
.script('jison-lex')
.option('file', {
flag: true,
position: 0,
help: 'file containing a lexical grammar'
})
.option('outfile', {
abbr: 'o',
metavar: 'FILE',
help: 'Filename and base module name of the generated parser'
})
.option('module-type', {
abbr: 't',
default: 'commonjs',
metavar: 'TYPE',
help: 'The type of module to generate (commonjs, js)'
})
.option('version', {
flag: true,
help: 'print version and exit',
callback: function() {
return version;
}
})
.parse();

exports.main = function () {
if (opts.file) {
var raw = fs.readFileSync(path.normalize(opts.file), 'utf8'),
name = path.basename((opts.outfile||opts.file)).replace(/\..*$/g,'');

fs.writeFileSync(opts.outfile||(name + '.js'), processGrammar(raw, name));
} else {
readin(function (raw) {
console.log(processGrammar(raw));
});
}
};

function processGrammar (file, name) {
var grammar;
try {
grammar = lexParser.parse(file);
} catch (e) {
try {
grammar = JSON.parse(file);
} catch (e2) {
throw e;
}
}

var settings = grammar.options || {};
if (!settings.moduleType) settings.moduleType = opts.moduleType;
if (!settings.moduleName && name) settings.moduleName = name.replace(/-\w/g, function (match){ return match.charAt(1).toUpperCase(); });

grammar.options = settings;

var lexer = new RegExpLexer(grammar);
return lexer.generate(settings);
}

function readin (cb) {
var stdin = process.openStdin(),
data = '';

stdin.setEncoding('utf8');
stdin.addListener('data', function (chunk) {
data += chunk;
});
stdin.addListener('end', function () {
cb(data);
});
}

if (require.main === module)
exports.main();
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"author": "Zach Carter <[email protected]> (http://zaa.ch)",
"name": "jison-lex",
"description": "lexical analyzer generator used by jison",
"version": "0.0.1",
"keywords": [
"jison",
"parser",
"generator",
"lexer",
"flex",
"tokenizer"
],
"repository": {
"type": "git",
"url": "git://github.com/zaach/jison-lex.git"
},
"bugs": {
"email": "[email protected]",
"url": "http://github.com/zaach/jison-lex/issues"
},
"main": "regexp-lexer",
"bin": "cli.js",
"engines": {
"node": ">=0.4"
},
"dependencies": {
"lex-parser": "0.0.1",
"nomnom": "1.5.2"
},
"devDependencies": {
"test": "0.4.4"
},
"scripts": {
"test": "node tests/all-tests.js"
},
"directories": {
"lib": "lib",
"tests": "tests"
},
"homepage": "http://jison.org"
}
Loading

0 comments on commit 8132b7d

Please sign in to comment.