Skip to content

Commit

Permalink
implement &
Browse files Browse the repository at this point in the history
  • Loading branch information
jrudolph committed Aug 31, 2021
1 parent a101ccd commit 0e7482a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions parboiled-core/src/main/scala-3/org/parboiled2/ParserMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ class OpTreeContext(parser: Expr[Parser])(using Quotes) {
}
}

case class AndPredicate(op: OpTree) extends DefaultNonTerminalOpTree {
def ruleTraceNonTerminalKey = '{ RuleTrace.AndPredicate }
def renderInner(start: Expr[Int], wrapped: Boolean): Expr[Boolean] =
'{
val mark = $parser.__saveState
val matched = ${ op.render(wrapped) }
$parser.__restoreState(mark)
matched
}
}

private case class Action(body: Term, ts: List[TypeTree]) extends DefaultNonTerminalOpTree {
def ruleTraceNonTerminalKey = '{ RuleTrace.Action }

Expand Down Expand Up @@ -750,6 +761,8 @@ class OpTreeContext(parser: Expr[Parser])(using Quotes) {
) =>
Sequence(rec(base), Action(body, ts))

case Apply(Select(_, "&"), List(arg)) => AndPredicate(rec(arg))

case Apply(
Select(Apply(TypeApply(Select(_, "rule2WithSeparatedBy"), _), List(base)), "separatedBy"),
List(sep)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ object CombinatorSpec extends TestParserSpec {
"a" must beMismatched
"b" must beMatched
"" must beMatched
}
}*/

"`&` modifier" - new TestParser0 {
def targetRule = rule(&("a"))
"a" must beMatched
cursor ==> 0
"b" must beMismatched
"" must beMismatched
}*/
}

"`1.times(Rule0)` modifier" - new TestParser0 {
def targetRule = rule(1.times("a"))
Expand Down

0 comments on commit 0e7482a

Please sign in to comment.