Gathering detailed insights and metrics for pegjs-util
Gathering detailed insights and metrics for pegjs-util
Gathering detailed insights and metrics for pegjs-util
Gathering detailed insights and metrics for pegjs-util
npm install pegjs-util
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
32 Stars
133 Commits
5 Forks
3 Watching
1 Branches
2 Contributors
Updated on 04 May 2024
JavaScript (83.76%)
Makefile (11.86%)
PEG.js (4.38%)
Cumulative downloads
Total Downloads
Last day
-49.6%
919
Compared to previous day
Last week
12.8%
8,758
Compared to previous week
Last month
15.8%
39,489
Compared to previous month
Last year
-27.7%
863,108
Compared to previous year
1
This is a small utility class for the excellent
Peggy (formerly PEG.js)
parser generator which wraps around Peggy's central parse
function
and provides three distinct convenience features: Parser Tree Token
Unrolling, Abstract Syntax Tree Node Generation and Cooked Error
Reporting.
1$ npm install peggy pegjs-util
{
var unroll = options.util.makeUnroll(location, options)
var ast = options.util.makeAST (location, options)
}
start
= _ seq:id_seq _ {
return ast("Sample").add(seq)
}
id_seq
= id:id ids:(_ "," _ id)* {
return ast("IdentifierSequence").add(unroll(id, ids, 3))
}
id
= id:$([a-zA-Z_][a-zA-Z0-9_]*) {
return ast("Identifier").set("name", id)
}
_ "blank"
= (co / ws)*
co "comment"
= "//" (![\r\n] .)*
/ "/*" (!"*/" .)* "*/"
ws "whitespaces"
= [ \t\r\n]+
1var fs = require("fs") 2var ASTY = require("asty") 3var PEG = require("peggy") 4var PEGUtil = require("pegjs-util") 5 6var asty = new ASTY() 7var parser = PEG.generate(fs.readFileSync("sample.pegjs", "utf8")) 8var result = PEGUtil.parse(parser, fs.readFileSync(process.argv[2], "utf8"), { 9 startRule: "start", 10 makeAST: function (line, column, offset, args) { 11 return asty.create.apply(asty, args).pos(line, column, offset) 12 } 13}) 14if (result.error !== null) 15 console.log("ERROR: Parsing Failure:\n" + 16 PEGUtil.errorMessage(result.error, true).replace(/^/mg, "ERROR: ")) 17else 18 console.log(result.ast.dump().replace(/\n$/, ""))
1$ cat sample-input-ok.txt 2/* some ok input */ 3foo, bar, quux 4 5$ node sample.js sample-input-ok.txt 6Sample [1/1] 7 IdentifierSequence [2/1] 8 Identifier (name: "foo") [2/1] 9 Identifier (name: "bar") [2/6] 10 Identifier (name: "quux") [2/11] 11 12$ cat sample-input-bad.txt 13/* some bad input */ 14foo, bar, quux baz 15 16$ node sample.js sample-input-bad.txt 17ERROR: Parsing Failure: 18ERROR: line 2 (column 16): */\nfoo, bar, quux baz\n 19ERROR: -----------------------------------------^ 20ERROR: Expected "," or end of input but "b" found.
PEGUtil is a small utility class for the excellent
Peggy (formerly PEG.js)
parser generator. It wraps around Peggy's central parse
function and
provides three distinct convenience features:
In many Peggy gammar rule actions you have to concatenate a first token and a repeated sequence of tokens, where from the sequence of tokens only relevant ones should be picked:
id_seq
= id:id ids:(_ "," _ id)* {
return unroll(id, ids, 3)
}
Here the id_seq
rule returns an array of ids, consisting of the first
token id
and then all 4th tokens from each element of the ids
repetition.
The unroll
function has the following signature:
unroll(first: Token, list: Token[], take: Number): Token[]
unroll(first: Token, list: Token[], take: Number[]): Token[]
It accepts first
to be also null
(and then skips this) and take
can be either just a single position (counting from 0) or a list of
positions.
To make the unroll
function available to your rule actions code,
place the following at the top of your grammar definition:
1{ 2 var unroll = options.util.makeUnroll(location, options) 3}
The options.util
above points to the PEGUtil API and is made available
automatically by using PEGUtil.parse
instead of Peggy's standard
parser method parse
.
Usually the result of Peggy grammar rule actions should be the generation of an Abstract Syntax Tree (AST) node. For this libraries like e.g. ASTy can be used.
id_seq
= id:id ids:(_ "," _ id)* {
return ast("IdentifierSequence").add(unroll(id, ids, 3))
}
Here the result is an AST node of type IdentifierSequence
which contains no attributes but all identifiers as child nodes.
To make the ast
function available to your rule actions code,
place the following at the top of your grammar definition:
1{ 2 var ast = options.util.makeAST(location, options) 3}
Additionally, before performing the parsing step, your
application has to tell PEGUtil how to map this call
onto the underlying AST implementation. For ASTy you
can use a makeAST
function like:
1function (line, column, offset, args) { 2 return ASTY.apply(null, args).pos(line, column, offset) 3}
The args
argument is an array containing all arguments
you supply to the generated ast()
function. For
ASTy this would be
at least the type of the AST node.
The options.util
above again points to the PEGUtil API and is made available
automatically by using PEGUtil.parse
instead of Peggy's standard
parser method parse
.
Instead of calling the regular Peggy parser.parse(source[, startRule])
you now should call PEGUtil.parse(parser, source[, startRule])
. The result then is always an object consisting of either
an ast
field (in case of success) or an error
field (in case of an
error).
In case of an error, the error
field provides cooked error information
which allow you to print out reasonable human-friendly error messages
(especially because of the detailed location
field):
1result = { 2 error: { 3 line: Number, /* line number */ 4 column: Number, /* column number */ 5 message: String, /* parsing error message */ 6 found: String, /* found token during parsing */ 7 expected: String, /* expected token during parsing */ 8 location: { 9 prolog: String, /* text before the error token */ 10 token: String, /* error token */ 11 epilog: String /* text after the error token */ 12 } 13 } 14}
For convenience reasons you can render a standard human-friendly
error message out of this information with
PEGUtil.errorMessage(result.error)
.
Copyright (c) 2014-2024 Dr. Ralf S. Engelschall (http://engelschall.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More