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
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions include/peg_parser/interpreter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <iterator>
#include <optional>
#include <type_traits>

#include "parser.h"
Expand Down Expand Up @@ -58,6 +59,14 @@ namespace peg_parser {
Expression operator[](size_t idx) const {
return interpreter.interpret(syntaxTree->inner[idx]);
}
std::optional<Expression> operator[](std::string_view name) const {
auto it = std::find_if(syntaxTree->inner.begin(), syntaxTree->inner.end(),
[name](auto st) { return st->rule->name == name; });
if (it != syntaxTree->inner.end()) {
return interpreter.interpret(*it);
}
return {};
}
iterator begin() const { return iterator(*this, 0); }
iterator end() const { return iterator(*this, size()); }

Expand Down
22 changes: 22 additions & 0 deletions test/source/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,25 @@ TEST_CASE("Documentation Example") {
REQUIRE(g.run("1 - 2*3/2 + 4") == Approx(2));
REQUIRE(g.run("1 + 2 * (3+4)/ 2 - 3") == Approx(5));
}

TEST_CASE("Subscript Operators") {
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.

}