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

Add subscript operator to Expression to access sub-expressions by name #60

Merged
merged 6 commits into from
Feb 5, 2021

Conversation

jteuber
Copy link
Contributor

@jteuber jteuber commented Feb 3, 2021

Hi there!
It might be just me, but accessing certain sub-expressions of a rule by name seems very handy for me, especially with very complicated rules. So I added this. It seems to work quite well. Especially the std::optional makes parsing expressions with optional expressions much simpler.

@TheLartians
Copy link
Owner

Hey, thanks for the PR, I definitely see the advantages of the rule based query, and good call with the optional return! Could you also add a test case, so the feature remains stable in the future?

@jteuber
Copy link
Contributor Author

jteuber commented Feb 4, 2021

Sure, there you go! :)

@codecov
Copy link

codecov bot commented Feb 4, 2021

Codecov Report

Merging #60 (e1b5022) into master (dcec3a0) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #60      +/-   ##
==========================================
+ Coverage   97.67%   97.69%   +0.02%     
==========================================
  Files           9        9              
  Lines         646      652       +6     
==========================================
+ Hits          631      637       +6     
  Misses         15       15              
Impacted Files Coverage Δ
include/peg_parser/interpreter.h 95.71% <100.00%> (+0.40%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dcec3a0...e1b5022. Read the comment docs.

Comment on lines 289 to 307
ParserGenerator<std::string> program;
program.setSeparator(program["Whitespace"] << "[\t ]");

program["Word"] << "[a-z]+";
program["Yell"] << "[A-Z]+";
program["Number"] << "[0-9]+";
program["IntSubscript"] << "Word Yell? Number" >> [](auto e) { return e[1].string(); };
program["StringSubscriptOptional"] << "Number Yell? Word" >> [](auto e) {
if (auto expr = e["Yell"]) return expr->string();
return std::string();
};
program["Start"] << "IntSubscript | StringSubscriptOptional";

program.setStart(program["Start"]);

REQUIRE(program.run("ab 0") == "0");
REQUIRE(program.run("ab CD 1") == "CD");
REQUIRE(program.run("2 ef") == "");
REQUIRE(program.run("2 GH ij") == "GH");
Copy link
Owner

@TheLartians TheLartians Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for adding the test case! I do find it to be a hard to follow, as it's doing a lot of things at once. What do you think about simplifying it to a single rule that checks the new [string] operator?

For example, something like

  ParserGenerator<bool> program;
  program["Word"] << "[a-z]+";
  program["Yell"] << "[A-Z]+";
  program["Start"] << "Word | Yell" >> [](auto e){ return bool(e["Yell"]);  }
  REQUIRE(!program.run("hello"));
  REQUIRE(program.run("HELLO"));

should be enough.

@jteuber
Copy link
Contributor Author

jteuber commented Feb 5, 2021 via email

Copy link
Owner

@TheLartians TheLartians left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the PR and updates!

@TheLartians TheLartians merged commit 99ea226 into TheLartians:master Feb 5, 2021
@TheLartians
Copy link
Owner

The feature is released in v2.2.0! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants