Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pest #20

Merged
merged 3 commits into from
Aug 29, 2024
Merged

Pest #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
*.cjs text eol=lf
*.css text eol=lf
*.csv text eol=lf
*.html text eol=lf
*.ico binary
*.jpg binary
*.js text eol=lf
*.json text eol=lf
*.map text eol=lf
*.mjs text eol=lf
*.mk text eol=lf
*.pdf binary
*.peg.js text eol=lf linguist-language=PEG.js
*.peggy text eol=lf linguist-language=PEG.js
*.pegjs text eol=lf linguist-language=PEG.js
*.png binary
*.rs text eol=lf
*.ts text eol=lf
*.tsv text eol=lf
*.tsx text eol=lf
examples/*.abnf eol=crlf

Makefile text eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
coverage/
examples/*.js
examples/*.peggy
examples/*.pest
node_modules
t.abnf
26 changes: 16 additions & 10 deletions bin/abnf_gen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import * as abnf from "../lib/abnf.js";
import { Command } from "commander";
import { Command, Option } from "commander";
import { fileURLToPath } from "url";
import fs from "fs";
import path from "path";
Expand All @@ -11,8 +11,8 @@ const __dirname = path.dirname(__filename);

const COMBINED = "XXXXXCOMBINEDXXXXX";

function gen_peggy(rules, opts) {
const out = rules.toPeggy(opts);
function genFormat(rules, opts) {
const out = rules.toFormat(opts);
if (opts.output === "-") {
console.log(out);
} else {
Expand All @@ -22,16 +22,21 @@ function gen_peggy(rules, opts) {

const program = new Command();
program
.argument("[abnfFile...]", "ABNF files to turn into peggy grammars.")
.description("Create a Peggy grammar from an ABNF file")
.argument("[abnfFile...]", "ABNF files to turn into grammars.")
.description("Create a grammar from an ABNF file")
.addOption(
new Option("-f, --format <format>", "Output format")
.choices(["peggy", "pest"])
.default("peggy")
)
.option(
"-s, --startRule <ruleName>",
"Start rule for peggy grammar. Defaults to first rule in ABNF grammar."
"Start rule for generated grammar. Defaults to first rule in ABNF grammar."
)
.option("--stubs", "Generate stubs for rules that do not exist, rather than failing.")
.option(
"-o, --output <file>",
"Output peggy grammar file name. Derived from input file name if not specified.",
"Output grammar file name. Derived from input file name if not specified.",
"stdin.peggy"
)
.option(
Expand Down Expand Up @@ -62,7 +67,7 @@ program
if (cmd.getOptionValueSource("output") === "default") {
const p = path.parse(f);
delete p.base;
p.ext = ".peggy";
p.ext = `.${opts.format}`;
cmd.setOptionValueWithSource("output", path.format(p), "first");
}
text = fs.readFileSync(f, "utf8");
Expand All @@ -78,8 +83,9 @@ program
}

try {
const rules = abnf.parseString(input, COMBINED);
gen_peggy(rules, opts);
const utf16 = opts.format === "peggy";
const rules = abnf.parseString(input, COMBINED, utf16);
genFormat(rules, opts);
} catch (er) {
if (typeof er.format === "function") {
const line = er.location.start.line;
Expand Down
2 changes: 1 addition & 1 deletion bin/abnf_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ program
let text = null;
try {
const rules = await abnf.parseString(abnfSource, f);
text = rules.toPeggy(opts);
text = rules.toFormat(opts);

const abnfOpts = {
grammarSource: f,
Expand Down
12 changes: 10 additions & 2 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
abnf = $(wildcard *.abnf)
peggy = $(patsubst %.abnf,%.peggy,$(abnf))
pest = $(patsubst %.abnf,%.pest,$(abnf))
js = $(patsubst %.peggy,%.js,$(peggy))

%.peggy: %.abnf
../bin/abnf_gen.js $< -uo $@

%.pest: %.abnf
../bin/abnf_gen.js -f pest $< -uo $@
pest_debugger -g $@ < /dev/null

.PRECIOUS: %.peggy
%.js: %.peggy
npx peggy $< -o $@

.PHONY: all
all: $(js)
all: $(js) $(pest)

.PHONY: clean
clean:
$(RM) $(js) $(peggy)
$(RM) $(js) $(peggy) $(pest)

json.peggy: json.abnf
../bin/abnf_gen.js $< -cuo $@

json.pest: json.abnf
../bin/abnf_gen.js $< -f pest -cuo $@
11 changes: 6 additions & 5 deletions lib/abnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import * as ast from "./ast.js";
import * as fs from "fs";
import { parse } from "./abnfp.js";

export function parseString(str, grammarSource = "unknown") {
export function parseString(str, grammarSource = "unknown", utf16 = true) {
const text = str;
try {
return parse(text, {
grammarSource,
utf16,
});
} catch (er) {
er.grammarSource = grammarSource;
Expand All @@ -18,7 +19,7 @@ export function parseString(str, grammarSource = "unknown") {
}
}

export function parseStream(input, grammarSource = "stdin") {
export function parseStream(input, grammarSource = "stdin", utf16 = true) {
return new Promise((resolve, reject) => {
const bufs = [];

Expand All @@ -41,14 +42,14 @@ export function parseStream(input, grammarSource = "stdin") {
break;
}
try {
resolve(parseString(s, grammarSource));
resolve(parseString(s, grammarSource, utf16));
} catch (er) {
reject(er);
}
});
});
}

export function parseFile(input) {
return parseString(fs.readFileSync(input, "utf8"), input);
export function parseFile(input, utf16 = true) {
return parseString(fs.readFileSync(input, "utf8"), input, utf16);
}
6 changes: 3 additions & 3 deletions lib/abnfp.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ function peg$parse(input, options) {
);
};
var peg$f14 = function(min, max) {
return ast.Range.create(2, min, max, location());
return ast.Range.create(2, min, max, location(), options.utf16);
};
var peg$f15 = function(n) {
return new ast.CaseSensitiveString(String.fromCodePoint(n), 2, location())
Expand All @@ -334,7 +334,7 @@ function peg$parse(input, options) {
);
};
var peg$f17 = function(min, max) {
return ast.Range.create(10, min, max, location());
return ast.Range.create(10, min, max, location(), options.utf16);
};
var peg$f18 = function(n) {
return new ast.CaseSensitiveString(String.fromCodePoint(n), 10, location())
Expand All @@ -347,7 +347,7 @@ function peg$parse(input, options) {
);
};
var peg$f20 = function(min, max) {
return ast.Range.create(16, min, max, location());
return ast.Range.create(16, min, max, location(), options.utf16);
};
var peg$f21 = function(n) {
return new ast.CaseSensitiveString(String.fromCodePoint(n), 10, location())
Expand Down
6 changes: 3 additions & 3 deletions lib/abnfp.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ bin_val
);
}
/ "b"i min:binnum "-" max:binnum {
return ast.Range.create(2, min, max, location());
return ast.Range.create(2, min, max, location(), options.utf16);
}
/ "b"i n:binnum {
return new ast.CaseSensitiveString(String.fromCodePoint(n), 2, location())
Expand All @@ -129,7 +129,7 @@ dec_val
);
}
/ "d"i min:decnum "-" max:decnum {
return ast.Range.create(10, min, max, location());
return ast.Range.create(10, min, max, location(), options.utf16);
}
/ "d"i n:decnum {
return new ast.CaseSensitiveString(String.fromCodePoint(n), 10, location())
Expand All @@ -144,7 +144,7 @@ hex_val
);
}
/ "x"i min:hexnum "-" max:hexnum {
return ast.Range.create(16, min, max, location());
return ast.Range.create(16, min, max, location(), options.utf16);
}
/ "x"i n:hexnum {
return new ast.CaseSensitiveString(String.fromCodePoint(n), 10, location())
Expand Down
Loading