pegjs-util
Advanced tools
Comparing version 0.9.6 to 0.9.7
{ | ||
"name": "pegjs-util", | ||
"version": "0.9.6", | ||
"version": "0.9.7", | ||
"description": "Utility Class for PEG.js", | ||
@@ -5,0 +5,0 @@ "main": "PEGUtil.js", |
{ | ||
"name": "pegjs-util", | ||
"version": "0.9.6", | ||
"version": "0.9.7", | ||
"description": "Utility Class for PEG.js", | ||
@@ -5,0 +5,0 @@ "keywords": [ "pegjs", "parser", "AST", "unroll" ], |
@@ -46,11 +46,7 @@ /* | ||
/* helper function for generating a function to generate an AST node */ | ||
PEGUtil.makeAST = function makeAST (line, column, offset) { | ||
if (makeAST._cb === null) | ||
throw new Error("makeAST: no callback set: you have to provide it via PEGUtil.makeAST.cb() first"); | ||
PEGUtil.makeAST = function makeAST (line, column, offset, options) { | ||
return function () { | ||
return makeAST._cb.call(PEGUtil, line(), column(), offset(), arguments); | ||
return options.util.__makeAST.call(null, line(), column(), offset(), arguments); | ||
}; | ||
}; | ||
PEGUtil.makeAST.cb = function (cb) { this._cb = cb; }; | ||
PEGUtil.makeAST._cb = null; | ||
@@ -113,3 +109,3 @@ /* helper function for generating a function to unroll the parse stack */ | ||
/* provide top-level parsing functionality */ | ||
PEGUtil.parse = function (parser, txt, rule) { | ||
PEGUtil.parse = function (parser, txt, options) { | ||
if (typeof parser !== "object") | ||
@@ -121,8 +117,26 @@ throw new Error("invalid parser object (not an object)"); | ||
throw new Error("invalid input text (not a string)"); | ||
if (typeof options !== "undefined" && typeof options !== "object") | ||
throw new Error("invalid options (not an object)"); | ||
if (typeof options === "undefined") | ||
options = {}; | ||
var result = { ast: null, error: null }; | ||
try { | ||
var options = { util: PEGUtil }; | ||
if (typeof rule === "string") | ||
options.startRule = rule; | ||
result.ast = parser.parse(txt, options); | ||
var makeAST; | ||
if (typeof options.makeAST === "function") | ||
makeAST = options.makeAST; | ||
else { | ||
makeAST = function (line, column, offset, args) { | ||
return { line: line, column: column, offset: offset, args: args }; | ||
}; | ||
} | ||
var opts = { | ||
util: { | ||
makeUnroll: PEGUtil.makeUnroll, | ||
makeAST: PEGUtil.makeAST, | ||
__makeAST: makeAST | ||
} | ||
}; | ||
if (typeof options.startRule === "string") | ||
opts.startRule = options.startRule; | ||
result.ast = parser.parse(txt, opts); | ||
result.error = null; | ||
@@ -129,0 +143,0 @@ } |
@@ -42,3 +42,3 @@ | ||
var unroll = options.util.makeUnroll(line, column, offset, SyntaxError) | ||
var ast = options.util.makeAST(line, column, offset) | ||
var ast = options.util.makeAST(line, column, offset, options) | ||
} | ||
@@ -80,7 +80,9 @@ | ||
PEGUtil.makeAST.cb(function (line, column, offset, args) { | ||
return ASTY.apply(null, args).pos(line, column, offset) | ||
var parser = PEG.buildParser(fs.readFileSync("sample.pegjs", "utf8")) | ||
var result = PEGUtil.parse(parser, fs.readFileSync(process.argv[2], "utf8"), { | ||
startRule: "start", | ||
makeAST: function (line, column, offset, args) { | ||
return ASTY.apply(null, args).pos(line, column, offset) | ||
} | ||
}) | ||
var parser = PEG.buildParser(fs.readFileSync("sample.pegjs", "utf8")) | ||
var result = PEGUtil.parse(parser, fs.readFileSync(process.argv[2], "utf8"), "start") | ||
if (result.error !== null) | ||
@@ -187,3 +189,3 @@ console.log("ERROR: Parsing Failure:\n" + | ||
{ | ||
var ast = options.util.makeAST(line, column, offset) | ||
var ast = options.util.makeAST(line, column, offset, options) | ||
} | ||
@@ -195,8 +197,8 @@ ``` | ||
onto the underlying AST implementation. For [ASTy](http://github.com/rse/asty) you | ||
can use: | ||
can use a `makeAST` function like: | ||
```js | ||
PEGUtil.makeAST.cb(function (line, column, offset, args) { | ||
function (line, column, offset, args) { | ||
return ASTY.apply(null, args).pos(line, column, offset) | ||
}) | ||
} | ||
``` | ||
@@ -203,0 +205,0 @@ |
@@ -6,7 +6,9 @@ var fs = require("fs") | ||
PEGUtil.makeAST.cb(function (line, column, offset, args) { | ||
return ASTY.apply(null, args).pos(line, column, offset) | ||
var parser = PEG.buildParser(fs.readFileSync("sample.pegjs", "utf8")) | ||
var result = PEGUtil.parse(parser, fs.readFileSync(process.argv[2], "utf8"), { | ||
startRule: "start", | ||
makeAST: function (line, column, offset, args) { | ||
return ASTY.apply(null, args).pos(line, column, offset) | ||
} | ||
}) | ||
var parser = PEG.buildParser(fs.readFileSync("sample.pegjs", "utf8")) | ||
var result = PEGUtil.parse(parser, fs.readFileSync(process.argv[2], "utf8"), "start") | ||
if (result.error !== null) | ||
@@ -13,0 +15,0 @@ console.log("ERROR: Parsing Failure:\n" + |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26464
431
268