Permalink
Browse files

Treat quantifiers as partially declarative whenever at all possible

  • Loading branch information...
1 parent d07a93d commit b8a5ff0589317ffb296c2e9ae3a31e4f17a1fc76 @sorear committed Dec 15, 2011
Showing with 64 additions and 92 deletions.
  1. +0 −1 docs/TODO.S05
  2. +6 −12 lib/CodeGen.cs
  3. +42 −69 lib/Cursor.cs
  4. +4 −10 lib/Serialize.cs
  5. +12 −0 src/niecza
View
@@ -50,7 +50,6 @@ All line numbers are relative to c4882a67. Also, deliberate discrepencies.)
(2380) No <commit> or <cut>
(2460) $foo ~~ rule { } does not automatically dual-anchor (DISCUSS)
(2501) No <prior> (probable fossil) DISCUSS
-(2638) ** 1..5 is not considered declarative
(2738) No success value; failed matches return Match
(2788) $/.ast doesn't correctly return undefined
(2789) No special case for $(), @(), %()
View
@@ -3541,12 +3541,9 @@ public class DowncallReceiver : CallReceiver {
return new LADParam(JScalar.S(tree[1]));
} else if (key == "Method") {
return new LADMethod(JScalar.S(tree[1]));
- } else if (key == "Opt") {
- return new LADOpt(BuildLadJ(tree[1]));
- } else if (key == "Star") {
- return new LADStar(BuildLadJ(tree[1]));
- } else if (key == "Plus") {
- return new LADPlus(BuildLadJ(tree[1]));
+ } else if (key == "Quant") {
+ return new LADQuant(JScalar.I(tree[1]), BuildLadJ(tree[2]),
+ tree.Length > 3 ? BuildLadJ(tree[3]) : null);
} else if (key == "Sequence" || key == "Any") {
object[] za = (object[])tree[1];
LAD[] z = new LAD[za.Length];
@@ -3583,12 +3580,9 @@ public class DowncallReceiver : CallReceiver {
return new LADParam((string)tree[1]);
} else if (key == "Method") {
return new LADMethod((string)tree[1]);
- } else if (key == "Opt") {
- return new LADOpt(BuildLad((object[])tree[1]));
- } else if (key == "Star") {
- return new LADStar(BuildLad((object[])tree[1]));
- } else if (key == "Plus") {
- return new LADPlus(BuildLad((object[])tree[1]));
+ } else if (key == "Quant") {
+ return new LADQuant((int)tree[1], BuildLad((object[])tree[2]),
+ tree.Length > 3 ? BuildLad((object[])tree[3]) : null);
} else if (key == "Sequence" || key == "Any") {
object[] za = (object[])tree[1];
LAD[] z = new LAD[za.Length];
View
@@ -1232,93 +1232,66 @@ public class LADDot : LAD {
}
}
-public class LADStar : LAD {
- public LAD child;
- public LADStar(LAD child) { this.child = child; }
- private LADStar() {}
-
- public override LAD Reify(NFA pad) { return new LADStar(child.Reify(pad)); }
- public override void ToNFA(NFA pad, int from, int to) {
- int knot = pad.AddNode();
- pad.AddEdge(from, knot, null);
- pad.AddEdge(knot, to, null);
- child.ToNFA(pad, knot, knot);
- }
-
- public override void Dump(int indent) {
- Console.WriteLine(new string(' ', indent) + "star:");
- child.Dump(indent + 4);
- }
- public override void Freeze(FreezeBuffer fb) {
- fb.Byte((byte) SerializationCode.LADStar);
- fb.ObjRef(child);
- }
- internal static object Thaw(ThawBuffer tb) {
- var n = new LADStar();
- tb.Register(n);
- n.child = (LAD) tb.ObjRef();
- return n;
- }
-}
-
-public class LADOpt : LAD {
- public LAD child;
- public override LAD Reify(NFA pad) { return new LADOpt(child.Reify(pad)); }
- public LADOpt(LAD child) { this.child = child; }
- private LADOpt() {}
-
- public override void ToNFA(NFA pad, int from, int to) {
- pad.AddEdge(from, to, null);
- child.ToNFA(pad, from, to);
+public class LADQuant : LAD {
+ public int type; // 1=allow 0 2=allow multiple 4=z1 optional
+ public LAD z0, z1;
+ public LADQuant(int type, LAD z0, LAD z1) {
+ this.type = type; this.z0 = z0; this.z1 = z1;
}
-
- public override void Dump(int indent) {
- Console.WriteLine(new string(' ', indent) + "opt:");
- child.Dump(indent + 4);
- }
- public override void Freeze(FreezeBuffer fb) {
- fb.Byte((byte) SerializationCode.LADOpt);
- fb.ObjRef(child);
- }
- internal static object Thaw(ThawBuffer tb) {
- var n = new LADOpt();
- tb.Register(n);
- n.child = (LAD) tb.ObjRef();
- return n;
- }
-}
-
-public class LADPlus : LAD {
- public LAD child;
- public override LAD Reify(NFA pad) { return new LADPlus(child.Reify(pad)); }
- public LADPlus(LAD child) { this.child = child; }
- private LADPlus() { }
+ private LADQuant() {}
public override void QueryLiteral(NFA pad, out int len, out bool cont) {
- child.QueryLiteral(pad, out len, out cont);
+ if ((type & 1) == 0) {
+ z0.QueryLiteral(pad, out len, out cont);
+ } else {
+ len = 0;
+ }
+ cont = false;
}
+ public override LAD Reify(NFA pad) {
+ return new LADQuant(type, z0.Reify(pad), z1 != null ? z1.Reify(pad) : null);
+ }
public override void ToNFA(NFA pad, int from, int to) {
int knot1 = pad.AddNode();
int knot2 = pad.AddNode();
+ int knot3 = pad.AddNode();
pad.AddEdge(from, knot1, null);
+
+ z0.ToNFA(pad, knot1, knot2);
+ if (z1 != null)
+ z1.ToNFA(pad, knot2, knot3);
+ else
+ pad.AddEdge(knot2, knot3, null);
+
pad.AddEdge(knot2, to, null);
- pad.AddEdge(knot2, knot1, null);
- child.ToNFA(pad, knot1, knot2);
+
+ if ((type & 1) != 0)
+ pad.AddEdge(from, to, null);
+ if ((type & 4) != 0)
+ pad.AddEdge(knot3, to, null);
+ if ((type & 2) != 0)
+ pad.AddEdge(knot3, knot1, null);
}
public override void Dump(int indent) {
- Console.WriteLine(new string(' ', indent) + "plus:");
- child.Dump(indent + 4);
+ Console.WriteLine(new string(' ', indent) + "quant({0}):", type);
+ z0.Dump(indent + 4);
+ if (z1 != null)
+ z1.Dump(indent + 4);
}
public override void Freeze(FreezeBuffer fb) {
- fb.Byte((byte) SerializationCode.LADPlus);
- fb.ObjRef(child);
+ fb.Byte((byte) SerializationCode.LADQuant);
+ fb.Byte((byte) (type | (z1 != null ? 8 : 0)));
+ fb.ObjRef(z0);
+ if (z1 != null) fb.ObjRef(z1);
}
internal static object Thaw(ThawBuffer tb) {
- var n = new LADPlus();
+ var n = new LADQuant();
tb.Register(n);
- n.child = (LAD) tb.ObjRef();
+ n.type = tb.Byte();
+ n.z0 = (LAD) tb.ObjRef();
+ if ((n.type & 8) != 0) n.z1 = (LAD) tb.ObjRef();
return n;
}
}
View
@@ -64,7 +64,7 @@ struct ObjRef {
internal static HashAlgorithm NewHash() { return new SHA256Managed(); }
static readonly string signature = "Niecza-Serialized-Module";
- static readonly int version = 6;
+ static readonly int version = 7;
// Routines for use by serialization code
public bool CheckWriteObject(SerUnit into, object o,
@@ -258,9 +258,7 @@ enum SerializationCode : byte {
LADStrNoCase,
LADMethod,
LADParam,
- LADOpt, // LAD
- LADPlus,
- LADStar,
+ LADQuant, // byte,LAD,LAD
LADSequence, // LAD[]
LADAny,
LADCC, // CC
@@ -760,12 +758,8 @@ class ThawBuffer {
return LADMethod.Thaw(this);
case SerializationCode.LADParam:
return LADParam.Thaw(this);
- case SerializationCode.LADOpt:
- return LADOpt.Thaw(this);
- case SerializationCode.LADPlus:
- return LADPlus.Thaw(this);
- case SerializationCode.LADStar:
- return LADStar.Thaw(this);
+ case SerializationCode.LADQuant:
+ return LADQuant.Thaw(this);
case SerializationCode.LADSequence:
return LADSequence.Thaw(this);
case SerializationCode.LADAny:
View
@@ -28,6 +28,18 @@ use STD;
augment class NieczaActions {
}
+augment class RxOp::Quantifier {
+ method lad() {
+ return [ 'Imp' ] if $!minimal || $!closure;
+
+ my $mode = 0;
+ $mode += 1 if $!min <= 0;
+ $mode += 2 if ($!max // Inf) > 1;
+ $mode += 4 if $!opsep;
+
+ [ 'Quant', $mode, map *.lad, @.zyg ];
+ }
+}
CgOp._register_ops: < who sc_root sc_indir temporize _addmethod _invalidate
>;

0 comments on commit b8a5ff0

Please sign in to comment.