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

[Feature]: Comment #69

Merged
merged 4 commits into from Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -6,6 +6,7 @@ Not Yet Released

Code change: <https://github.com/soasme/PeppaPEG/compare/v1.11.0..HEAD>.

* [Feature]: Support Comment for PEG Grammar. `#69 <https://github.com/soasme/PeppaPEG/pull/69>`_
* [Feature]: Support Unicode Character Categories for P4_Range. `#65 <https://github.com/soasme/PeppaPEG/pull/65>`_, `#67 <https://github.com/soasme/PeppaPEG/pull/67>`_

1.11.0 (8 Apr, 2021)
Expand Down
5 changes: 3 additions & 2 deletions ROADMAP.md
Expand Up @@ -11,7 +11,6 @@
- [ ] api: Sanitize `\u0000` to whitespace for the source input, this happens in creating the source/setting the source size.
- [ ] api: left recursion. https://github.com/orlandohill/peg-left-recursion
- [ ] api: register a function for matching source. This should help dealing with some inputs difficult to parse.
- [ ] api: support comment in peg grammar
- [ ] api: Support UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of source.
- [ ] peg: Numeric.
- [ ] peg: Complement.
Expand All @@ -23,6 +22,7 @@
- [ ] perf: pre-alloc tokens.
- [ ] perf: Cache literal len.
- [ ] perf: backrefs is not necessary if there is no BackReference in Sequence.
- [ ] perf: trace: add a tracer in P4_Source. When matching, annotate the tracer. An additional tool can aggregate data and output a DOT / compile to png.
- [ ] perf: tracer: https://pegjs.org/documentation
https://github.com/orlandohill/peg-left-recursion
- [ ] binding: python: example: cffi, misaka, parsimonious (api). <https://tomassetti.me/parsing-in-python/>.
Expand All @@ -41,13 +41,14 @@
- [ ] Pratt parser: https://en.wikipedia.org/wiki/Operator-precedence_parser
- [ ] build: static lib.
- [ ] build: wasm. `docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emcc peppapeg.c -Os -s WASM=1 -s SIDE_MODULE=1 -o /src/peppapeg.wasm`. https://gist.github.com/kripken/59c67556dc03bb6d57052fedef1e61ab https://github.com/mbasso/awesome-wasm
- [x] api: support comment in peg grammar. Added in v1.12.0.
- [x] peg: CharacterSet. can use range.
- [x] api: Support more spaced rules. Added in v1.11.0.
- [x] peg: extend range: `[0-9..2] / [a-z] / [\p{L}] / [\u{1}-\u{10ffff}]`. Added in v1.12.0.
- [x] peg: built-in rules: letters.
- [x] peg: built-in rules: unicode letters.
- [x] peg: built-in rules: digits.
- [x] peg: built-in rules: unicode digits.
- [x] api: Support more spaced rules. Added in v1.11.0.
- [x] api: `P4_AcquireSourceAst(source, &ast)`: set ast, reset source. It's useful when we need the parsed result but not care about source itself. Token tree should now owned by ast and shall then be free by the caller. Added in v1.11.0
- [x] docs: add explanations.
- [x] GetErrorString.
Expand Down
14 changes: 14 additions & 0 deletions docs/peg.rst
Expand Up @@ -246,6 +246,18 @@ Repeat **matches the sub-expression several times**.

hex = "\u{" ([0-9] / [a-z] / [A-Z]){1,6} "}";

Comment
-------

Comment are any characters followed by a # (included) in a line.

.. code-block::

# THIS IS A COMMENT.
rule = "hello"; # THIS IS ANOTHER COMMENT.

Comments are ignored.

Grammar Rule Flags
------------------

Expand Down Expand Up @@ -446,3 +458,5 @@ Cheatsheet
- repeat between m-n times
* - `foo{m}`
- repeat exact n times
* - `# IGNORE`
- comment
16 changes: 16 additions & 0 deletions peppapeg.c
Expand Up @@ -3585,6 +3585,22 @@ P4_Grammar* P4_CreatePegGrammar () {
P4_FLAG_LIFTED | P4_FLAG_SPACED))
goto finalize;

if (P4_Ok != P4_AddSequenceWithMembers(grammar, P4_PegRuleComment, 3,
P4_CreateLiteral("#", true),
P4_CreateZeroOrMore(
P4_CreateSequenceWithMembers(2,
P4_CreateNegative(P4_CreateLiteral("\n", true)),
P4_CreateRange(0x1, 0x10ffff, 1)
)
),
P4_CreateZeroOrOnce(P4_CreateLiteral("\n", true))
))
goto finalize;

if (P4_Ok != P4_SetGrammarRuleFlag(grammar, P4_PegRuleComment,
P4_FLAG_LIFTED | P4_FLAG_SPACED))
goto finalize;

return grammar;

finalize:
Expand Down
1 change: 1 addition & 0 deletions peppapeg.h
Expand Up @@ -252,6 +252,7 @@ typedef enum {
P4_PegRuleDot = 28,
P4_PegRuleWhitespace = 29,
P4_PegRuleRangeCategory = 30,
P4_PegRuleComment = 31,
} P4_PegRuleID;

/*
Expand Down
3 changes: 3 additions & 0 deletions tests/test_peg.c
Expand Up @@ -525,6 +525,9 @@ void test_eval_grammar(void) {
ASSERT_EVAL_GRAMMAR("R1 = .;", "R1", "好", P4_Ok, "[{\"slice\":[0,3],\"type\":\"R1\"}]");
ASSERT_EVAL_GRAMMAR("R1 = \"a\"* !.;", "R1", "aaab", P4_MatchError, "[]");
ASSERT_EVAL_GRAMMAR("R1 = \"a\"*;", "R1", "aaab", P4_Ok, "[{\"slice\":[0,3],\"type\":\"R1\"}]");
ASSERT_EVAL_GRAMMAR("R1 = \"1\"# R1 = \"3\";\n;", "R1", "1", P4_Ok, "[{\"slice\":[0,1],\"type\":\"R1\"}]");
ASSERT_EVAL_GRAMMAR("# R1 = \"2\";\nR1 = \"1\";", "R1", "1", P4_Ok, "[{\"slice\":[0,1],\"type\":\"R1\"}]");
ASSERT_EVAL_GRAMMAR("R1 = \"1\";##", "R1", "1", P4_Ok, "[{\"slice\":[0,1],\"type\":\"R1\"}]");

ASSERT_EVAL_GRAMMAR(
"R1 = R2; "
Expand Down