Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use PEG for parsing; better support for literal strings in patterns
- Loading branch information
Showing
7 changed files
with
117 additions
and
57 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
start | ||
= ws head:segment tail:segmentTail* { tail.unshift(head); return tail; } | ||
|
||
segmentTail | ||
= ws ',' ws seg:segment { return seg; } | ||
|
||
segment | ||
= str:string { return {string: str}; } | ||
/ v:identifier size:size ? specs:specifierList ? | ||
{ return {name: v, size: size, specifiers: specs}; } | ||
/ v:number size:size ? specs:specifierList ? | ||
{ return {value: v, size: size, specifiers: specs}; } | ||
|
||
string | ||
= '"' '"' { return ""; } | ||
/ '"' chars:chars '"' { return chars; } | ||
|
||
/* From JSON example | ||
https://github.com/dmajda/pegjs/blob/master/examples/json.pegjs */ | ||
|
||
chars | ||
= chars:char+ { return chars.join(""); } | ||
|
||
char | ||
= [^"\\\0-\x1F\x7f] | ||
/ '\\"' { return '"'; } | ||
/ "\\\\" { return "\\"; } | ||
/ "\\/" { return "/"; } | ||
/ "\\b" { return "\b"; } | ||
/ "\\f" { return "\f"; } | ||
/ "\\n" { return "\n"; } | ||
/ "\\r" { return "\r"; } | ||
/ "\\t" { return "\t"; } | ||
/ "\\u" h1:hexDigit h2:hexDigit h3:hexDigit h4:hexDigit { | ||
return String.fromCharCode(parseInt("0x" + h1 + h2 + h3 + h4)); | ||
} | ||
|
||
hexDigit | ||
= [0-9a-fA-F] | ||
|
||
identifier | ||
= (head:[_a-zA-Z] tail:[_a-zA-Z0-9]*) { return head + tail.join(''); } | ||
|
||
number | ||
= '0' { return 0; } | ||
/ head:[1-9] tail:[0-9]* { return parseInt(head + tail.join('')); } | ||
|
||
size | ||
= ':' num:number { return num; } | ||
/ ':' id:identifier { return id; } | ||
|
||
specifierList | ||
= '/' head:specifier tail:specifierTail* { tail.unshift(head); return tail; } | ||
|
||
specifierTail | ||
= '-' spec:specifier { return spec; } | ||
|
||
specifier | ||
= 'little' / 'big' / 'signed' / 'unsigned' | ||
/ 'integer' / 'binary' / 'float' | ||
/ unit | ||
|
||
unit | ||
= 'unit:' num:number { return 'unit:' + num; } | ||
|
||
ws = [ \t\n]* |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,32 @@ | ||
// Parse patterns in string form into the form we use for interpreting | ||
// (and later, for compiling). | ||
|
||
var ast = require('./pattern'); | ||
var peg = require('pegjs'), | ||
ast = require('./pattern'), | ||
path = require('path'); | ||
|
||
function compose() { | ||
var funcs = [].slice.call(arguments); | ||
return function(elem) { | ||
var result = elem; | ||
for (var i in funcs) { | ||
result = funcs[i](result); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
function map(array0, func) { | ||
var array = array0.slice(); | ||
for (var i in array.slice()) { | ||
array[i] = func(array[i]); | ||
} | ||
return array; | ||
} | ||
var grammar = require('fs').readFileSync( | ||
path.join(path.dirname(module.filename), 'grammar.pegjs')).toString(); | ||
var parser = peg.buildParser(grammar); | ||
|
||
function parse_pattern(string) { | ||
return map( | ||
string.split(','), | ||
compose( | ||
function(s) { return s.replace(/\s/g, ''); }, | ||
parse_segment)); | ||
var segments = parser.parse(string); | ||
for (var i=0, len = segments.length; i < len; i++) { | ||
var s = segments[i]; | ||
if (s.string != undefined) { | ||
segments[i] = ast.string(s.string); | ||
} | ||
else if (s.value != undefined) { | ||
segments[i] = ast.value(s.value, s.size, s.specifiers); | ||
} | ||
else if (s.name != undefined) { | ||
segments[i] = ast.variable(s.name, s.size, s.specifiers); | ||
} | ||
else { | ||
throw "Unknown segment " + s; | ||
} | ||
} | ||
return segments; | ||
} | ||
|
||
module.exports.parse = parse_pattern; | ||
|
||
// From | ||
// http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric | ||
function isNumber(n) { | ||
return !isNaN(parseFloat(n)) && isFinite(n); | ||
} | ||
|
||
var PARTS = /^([_a-zA-Z0-9\.]*)(?:\:([a-zA-Z_0-9]+))?(?:\/([a-z0-9:-]*))?$/; | ||
var STRING = /^"(([^"]|(\"))*)"$/; | ||
|
||
function parse_segment(string) { | ||
var parts = STRING.exec(string); | ||
if (parts) { | ||
return ast.string(parts[1]); | ||
} | ||
parts = PARTS.exec(string); | ||
var nameOrValue = parts[1]; | ||
var size = parts[2]; | ||
var specifiers = (parts[3] || '').split('-'); | ||
if (size !== undefined && isNumber(size)) { | ||
size = parseInt(size); | ||
} | ||
return ((isNumber(nameOrValue)) ? | ||
ast.value : | ||
ast.variable) (nameOrValue, size, specifiers); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
"engines": { | ||
"node": ">0.4" | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"pegjs": "~0.7" | ||
}, | ||
"devDependencies": {} | ||
} |
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