Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
winitzki committed Jul 22, 2024
1 parent 6abdf9e commit bbbf60d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
39 changes: 23 additions & 16 deletions nano-dhall/src/main/scala/io/chymyst/nanodhall/SymbolicGraph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package io.chymyst.nanodhall
import sourcecode.{File, Line, Name}

object SymbolicGraph {
final class RuleDef(val name: String, rule: () => GrammarRule) {
sealed trait RuleDef
final case class RuleLiteral(literalMatch: LiteralMatch) extends RuleDef
final class RuleSymbol(val name: String, rule: () => GrammarRule) extends RuleDef {
override def equals(obj: Any): Boolean = obj match {
case r: RuleDef => name == r.name
case r: RuleSymbol => name == r.name
case _ => false
}
def grammarRule: GrammarRule = rule()
def grammarRule: GrammarRule = rule()
}

sealed trait GrammarRule
Expand All @@ -23,21 +25,26 @@ object SymbolicGraph {
final case class And(rule1: GrammarRule, rule2: GrammarRule) extends GrammarRule
final case class Or(rule1: GrammarRule, rule2: GrammarRule) extends GrammarRule

implicit class SymbolicGraphOps(r: => RuleDef) {
def ~(next: => RuleDef)(implicit file: File, line: Line, varName: Name): RuleDef =
new RuleDef(name = varName.value, rule = {() =>
println(s"DEBUG: evaluating And(${r.name}, ${next.name}")
And(r.grammarRule,
next.grammarRule)
})
implicit class SymbolicGraphOps(r: => RuleSymbol) {
def ~(next: => RuleSymbol)(implicit file: File, line: Line, varName: Name): RuleDef = {
new RuleSymbol(
name = varName.value,
rule = { () =>
println(s"DEBUG: evaluating And(${r.name}, ${next.name}")
And(r.grammarRule, next.grammarRule)
},
)
def ~(next: RuleLiteral)(implicit file: File, line: Line, varName: Name): RuleDef =
}

def |(next: => RuleDef)(implicit file: File, line: Line, varName: Name): RuleDef =
new RuleDef(name = varName.value, rule =
{ () =>
println(s"DEBUG: evaluating Or(${r.name}, ${next.name}")
Or(r.grammarRule,
next.grammarRule)
})
new RuleDef(
name = varName.value,
rule = { () =>
println(s"DEBUG: evaluating Or(${r.name}, ${next.name}")
Or(r.grammarRule, next.grammarRule)
},
)
}

def lit(s: String)(implicit file: File, line: Line, varName: Name): RuleDef = new RuleDef(name = varName.value, rule = () => LiteralMatch(s))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ class SymbolicGraphTest extends FunSuite {
def c: RuleDef = lit("y") ~ a | b

expect(a.name == "a")
expect(a.grammarRule match {
expect(a.grammarRule match {
case LiteralMatch("x") => true
})

expect(b.name == "b")
expect(b.grammarRule match {
case And(LiteralMatch("y"), GrammarSymbol("a", _)) => true
})

expect(c.name == "c")
expect(c.grammarRule match {
case Or(And(LiteralMatch("y"), GrammarSymbol("a", _)), GrammarSymbol("b", _)) => true
})
}

test("circular dependencies do not create an infinite loop 1") {
Expand Down Expand Up @@ -54,10 +59,15 @@ class SymbolicGraphTest extends FunSuite {
expect(b.name == "b")
expect(c.name == "c")

// expect(a == new RuleDef(name = "a", rule = () => And(GrammarSymbol("b", () => b), GrammarSymbol("c", () => c))))
// expect(
// b == new RuleDef(name = "b", rule = () => Or(And(And(LiteralMatch("x"), GrammarSymbol("a", () => a)), GrammarSymbol("b", () => b)), LiteralMatch("y")))
// )
// expect(c == new RuleDef(name = "c", rule = () => And(LiteralMatch("z"), GrammarSymbol("a", () => a))))
expect(a.grammarRule match {
case And(GrammarSymbol("b", bx), GrammarSymbol("c", cx)) => true
})
expect(b.grammarRule match {
case Or(And(And(LiteralMatch("x"), GrammarSymbol("a", _)), GrammarSymbol("b", _)), LiteralMatch("y")) => true
})
expect(c.grammarRule match {
case And(LiteralMatch("z"), GrammarSymbol("a", _)) => true
})

}
}

0 comments on commit bbbf60d

Please sign in to comment.