diff --git a/README.md b/README.md index 10449b8..8f72417 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ The PEG syntax is well described on page 2 in the [document](http://www.brynosau * `$name` (Backreference operator) * `%whitespace` (Automatic whitespace skipping) * `%word` (Word expression) - * `$name(` ... `)` (Create capture scope) + * `$name(` ... `)` (Capture scope operator) * `$name<` ... `>` (Named capture operator) * `$name` (Backreference operator) @@ -261,8 +261,28 @@ peg::parser parser(R"( %word <- [a-z]+ )"); -parser.parse("hello world") // OK -parser.parse("helloworld") // NG +parser.parse("hello world"); // OK +parser.parse("helloworld"); // NG +``` + +Capture/Backreference +--------------------- + +```cpp +peg::parser parser(R"( + ROOT <- CONTENT + CONTENT <- (ELEMENT / TEXT)* + ELEMENT <- $(STAG CONTENT ETAG) + STAG <- '<' $tag< TAG_NAME > '>' + ETAG <- '' + TAG_NAME <- 'b' / 'u' + TEXT <- TEXT_DATA + TEXT_DATA <- ![<] . +)"); + +parser.parse("This is a test text."); // OK +parser.parse("This is a test text."); // NG +parser.parse("This is a test text."); // NG ``` AST generation @@ -326,7 +346,7 @@ The following are available operators: | dot | Any character | | tok | Token boundary | | ign | Ignore semantic value | -| ncs | New capture scope | +| csc | Capture scope | | cap | Capture | | bkr | Back reference | diff --git a/peglib.h b/peglib.h index 4170a83..c94f1cc 100644 --- a/peglib.h +++ b/peglib.h @@ -1032,10 +1032,10 @@ public: void accept(Visitor& v) override; }; -class NewCaptureScope : public Ope +class CaptureScope : public Ope { public: - NewCaptureScope(const std::shared_ptr& ope) + CaptureScope(const std::shared_ptr& ope) : ope_(ope) {} size_t parse(const char* s, size_t n, SemanticValues& sv, Context& c, any& dt) const override { @@ -1219,7 +1219,7 @@ struct Ope::Visitor virtual void visit(CharacterClass& /*ope*/) {} virtual void visit(Character& /*ope*/) {} virtual void visit(AnyCharacter& /*ope*/) {} - virtual void visit(NewCaptureScope& /*ope*/) {} + virtual void visit(CaptureScope& /*ope*/) {} virtual void visit(Capture& /*ope*/) {} virtual void visit(TokenBoundary& /*ope*/) {} virtual void visit(Ignore& /*ope*/) {} @@ -1249,7 +1249,7 @@ struct AssignIDToDefinition : public Ope::Visitor void visit(Option& ope) override { ope.ope_->accept(*this); } void visit(AndPredicate& ope) override { ope.ope_->accept(*this); } void visit(NotPredicate& ope) override { ope.ope_->accept(*this); } - void visit(NewCaptureScope& ope) override { ope.ope_->accept(*this); } + void visit(CaptureScope& ope) override { ope.ope_->accept(*this); } void visit(Capture& ope) override { ope.ope_->accept(*this); } void visit(TokenBoundary& ope) override { ope.ope_->accept(*this); } void visit(Ignore& ope) override { ope.ope_->accept(*this); } @@ -1279,7 +1279,7 @@ struct IsToken : public Ope::Visitor void visit(ZeroOrMore& ope) override { ope.ope_->accept(*this); } void visit(OneOrMore& ope) override { ope.ope_->accept(*this); } void visit(Option& ope) override { ope.ope_->accept(*this); } - void visit(NewCaptureScope& ope) override { ope.ope_->accept(*this); } + void visit(CaptureScope& ope) override { ope.ope_->accept(*this); } void visit(Capture& ope) override { ope.ope_->accept(*this); } void visit(TokenBoundary& /*ope*/) override { has_token_boundary = true; } void visit(Ignore& ope) override { ope.ope_->accept(*this); } @@ -1677,7 +1677,7 @@ inline void LiteralString::accept(Visitor& v) { v.visit(*this); } inline void CharacterClass::accept(Visitor& v) { v.visit(*this); } inline void Character::accept(Visitor& v) { v.visit(*this); } inline void AnyCharacter::accept(Visitor& v) { v.visit(*this); } -inline void NewCaptureScope::accept(Visitor& v) { v.visit(*this); } +inline void CaptureScope::accept(Visitor& v) { v.visit(*this); } inline void Capture::accept(Visitor& v) { v.visit(*this); } inline void TokenBoundary::accept(Visitor& v) { v.visit(*this); } inline void Ignore::accept(Visitor& v) { v.visit(*this); } @@ -1747,8 +1747,8 @@ inline std::shared_ptr dot() { return std::make_shared(); } -inline std::shared_ptr ncs(const std::shared_ptr& ope) { - return std::make_shared(ope); +inline std::shared_ptr csc(const std::shared_ptr& ope) { + return std::make_shared(ope); } inline std::shared_ptr cap(const std::shared_ptr& ope, Capture::MatchAction ma) { @@ -1877,7 +1877,7 @@ private: void visit(AnyCharacter& /*ope*/) override { done_ = true; } - void visit(NewCaptureScope& ope) override { + void visit(CaptureScope& ope) override { ope.ope_->accept(*this); } void visit(Capture& ope) override { @@ -2102,8 +2102,8 @@ private: case 2: { // TokenBoundary return tok(sv[1].get>()); } - case 3: { // NewCaptureScope - return ncs(sv[1].get>()); + case 3: { // CaptureScope + return csc(sv[1].get>()); } case 4: { // Capture const auto& name = sv[0].get(); diff --git a/test/test.cc b/test/test.cc index b557efa..f5eac12 100644 --- a/test/test.cc +++ b/test/test.cc @@ -789,6 +789,7 @@ TEST_CASE("Nested capture test", "[backreference]") REQUIRE(parser.parse("This is a test text.")); REQUIRE(!parser.parse("This is a test text.")); REQUIRE(!parser.parse("This is a test text.")); + REQUIRE(!parser.parse("This is a test text.")); } TEST_CASE("Backreference with Prioritized Choice test", "[backreference]")