Skip to content

Commit fc4b7d9

Browse files
committed
Working on Layout in Trees
1 parent 3a5e51f commit fc4b7d9

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

src/main/scala/org/meerkat/sppf/SPPFVisitor.scala

+19-11
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,19 @@ class SemanticActionExecutor(amb: (Set[Any], Int, Int) => Any,
108108

109109
def visit(node: SPPFNode): Any = node match {
110110

111-
case t: TerminalNode => if (t.leftExtent == t.rightExtent) ()
112-
else tn(t.leftExtent, t.rightExtent)
111+
case t: TerminalNode => if (t.leftExtent == t.rightExtent) ()
112+
else tn(t.leftExtent, t.rightExtent)
113113

114-
case t: LayoutTerminalNode => if (t.leftExtent == t.rightExtent) ()
115-
else tn(t.leftExtent, t.rightExtent)
116-
117-
case n: NonterminalNode => if (n isAmbiguous) ambiguity(n)
118-
else nonterminal(n.first, n.leftExtent, n.rightExtent)
114+
case l: LayoutTerminalNode => if (l.leftExtent == l.rightExtent) ()
115+
else tn(l.leftExtent, l.rightExtent)
116+
117+
case n: NonterminalNode => if (n isAmbiguous) ambiguity(n)
118+
else nonterminal(n.first, n.leftExtent, n.rightExtent)
119119

120-
case i: IntermediateNode => if (i isAmbiguous) ambiguity(i)
121-
else intermediate(i.first, visit(i.first.leftChild), visit(i.first.rightChild))
120+
case i: IntermediateNode => if (i isAmbiguous) ambiguity(i)
121+
else intermediate(i.first, visit(i.first.leftChild), visit(i.first.rightChild))
122122

123-
case p: PackedNode => throw new RuntimeException("Should not traverse a packed node!")
123+
case p: PackedNode => throw new RuntimeException("Should not traverse a packed node!")
124124
}
125125

126126
}
@@ -185,9 +185,17 @@ object TreeBuilder {
185185

186186
def t(input: Input)(l: Int, r: Int): Tree = org.meerkat.tree.TerminalNode(input.substring(l, r))
187187

188+
def l(input: Input)(l:Int, r: Int): Tree = LayoutNode(t(input)(l, r))
189+
188190
def int(input: Input)(t: Rule, v: Any) = v
189191

190-
def nt(input: Input)(t: Rule, v: Any, l: Int, r: Int) = RuleNode(Rule(t.head, flatten(t.body)), flatten(v).asInstanceOf[Seq[Tree]])
192+
def nt(input: Input)(t: Rule, v: Any, l: Int, r: Int) = {
193+
val node = RuleNode(Rule(t.head, flatten(t.body)), flatten(v).asInstanceOf[Seq[Tree]])
194+
t.head match {
195+
case Layout(_) => LayoutNode(node)
196+
case _ => node
197+
}
198+
}
191199

192200
def build(node: NonPackedNode, memoized: Boolean = true)(implicit input: Input): Tree = {
193201
val executer = if (memoized) new SemanticActionExecutor(amb(input), t(input), int(input), nt(input)) with Memoization

src/main/scala/org/meerkat/tree/Rule.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ case class Sequence(ss: Symbol*) extends NonterminalSymbol {
9191
}
9292

9393
object Sequence {
94-
def unapply(s: Sequence): Option[Seq[Symbol]] = Some(s.ss filter { case Layout(_) => true; case _ => false } )
94+
def unapply(s: Sequence): Option[Seq[Symbol]] = Some(s.ss filter { case Layout(_) => false; case _ => true } )
9595
}
9696

9797
case class Group(s: Symbol) extends NonterminalSymbol {

src/main/scala/org/meerkat/tree/Tree.scala

+11-6
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ object Tree {
4141
def isEpsilon(t: Tree): Boolean = t == epsilon
4242
}
4343

44-
class RuleNode(val r: Rule, val ts: Seq[Tree]) extends Tree
44+
trait RuleNode extends Tree {
45+
def r: Rule
46+
def ts: Seq[Tree]
47+
}
48+
49+
case class RuleNodeImpl(val r: Rule, val ts: Seq[Tree]) extends RuleNode
4550

46-
//object RuleNodeL {
47-
// def unapply(r: RuleNode)
48-
//}
51+
object RuleNodeL {
52+
def unapply(n: RuleNode): Option[(Rule, Seq[Tree])] = Some((n.r, n.ts))
53+
}
4954

5055
object RuleNode {
51-
def apply(r: Rule, ts: Seq[Tree])= new RuleNode(r, ts)
52-
def unapply(n: RuleNode): Option[(Rule, Seq[Tree])] = Some((n.r, n.ts)) // TODO: ???
56+
def apply(r: Rule, ts: Seq[Tree])= new RuleNodeImpl(r, ts)
57+
def unapply(n: RuleNode): Option[(Rule, Seq[Tree])] = Some((n.r, n.ts filter { case l: LayoutNode => false; case _ => true }))
5358
}
5459

5560
case class AmbNode(ts: Set[Tree]) extends Tree

0 commit comments

Comments
 (0)