Author: pmichaud
Date: Tue Nov 1 08:25:31 2005
New Revision: 9685
Modified:
trunk/compilers/pge/PGE/Exp.pir
trunk/compilers/pge/PGE/OPTable.pir
trunk/compilers/pge/PGE/P6Rule.pir
trunk/compilers/pge/PGE/Rule.pir
Log:
First version of parameterized subrules.
* Added lookahead (<before ...>)
Modified: trunk/compilers/pge/PGE/Exp.pir
==============================================================================
--- trunk/compilers/pge/PGE/Exp.pir (original)
+++ trunk/compilers/pge/PGE/Exp.pir Tue Nov 1 08:25:31 2005
@@ -871,7 +871,7 @@ register.
.param string label
.param string next
.local pmc emit
- .local string subname
+ .local string subname, subargs
.local string cname, captsave, captback
.local int iscapture
emit = find_global "PGE::Exp", "emit"
@@ -879,6 +879,16 @@ register.
iscapture = self["iscapture"]
cname = self["cname"]
emit(code, "\n %s: # subrule %s ##", label, subname)
+ subargs = ""
+ $I0 = exists self["arg"]
+ if $I0 == 0 goto nosubargs
+ $P0 = find_global "Data::Escape", "String"
+ $S0 = self["arg"]
+ $S0 = $P0($S0, '"')
+ subargs = ', "'
+ subargs .= $S0
+ subargs .= '"'
+ nosubargs:
captsave = ""
captback = ""
if iscapture == 0 goto subrule
@@ -898,7 +908,7 @@ register.
emit(code, " ($P1,$P9,$P9,$P0) = newfrom(captscope, pos, \"%s\")", $S0)
emit(code, " $P0 = pos")
emit(code, " $P0 = find_global \"%s\", \"%s\"", $S0, $S1)
- emit(code, " $P0 = $P0($P1)")
+ emit(code, " $P0 = $P0($P1%s)", subargs)
goto subrule_3
subrule_simple_name:
emit(code, " $P0 = getattribute captscope, \"PGE::Match\\x0$:pos\"")
@@ -910,7 +920,7 @@ register.
emit(code, " %s_s1:", label)
emit(code, " $P0 = find_global \"%s\"", subname)
emit(code, " %s_s2:", label)
- emit(code, " $P0 = $P0(captscope)")
+ emit(code, " $P0 = $P0(captscope%s)", subargs)
subrule_3:
emit(code, " unless $P0 goto fail")
emit(code, captsave, cname)
Modified: trunk/compilers/pge/PGE/OPTable.pir
==============================================================================
--- trunk/compilers/pge/PGE/OPTable.pir (original)
+++ trunk/compilers/pge/PGE/OPTable.pir Tue Nov 1 08:25:31 2005
@@ -196,6 +196,7 @@ value for C<match> is "PGE::Match".
if syncat == "prefix:" goto prefix
if syncat == "postcircumfix:" goto postcircumfix
if syncat == "ternary:" goto ternary
+ if syncat == "close:" goto close
term:
tok["syncat"] = PGE_OPTABLE_TERM
goto expect_term
@@ -220,6 +221,10 @@ value for C<match> is "PGE::Match".
tok["syncat"] = PGE_OPTABLE_TERNARY
tok["arity"] = 3
goto expect_op
+ close:
+ tok["syncat"] = PGE_OPTABLE_CLOSE
+ tok["arity"] = 0
+ goto expect_op
expect_term:
termtable[tok1] = tok
if nows goto end
Modified: trunk/compilers/pge/PGE/P6Rule.pir
==============================================================================
--- trunk/compilers/pge/PGE/P6Rule.pir (original)
+++ trunk/compilers/pge/PGE/P6Rule.pir Tue Nov 1 08:25:31 2005
@@ -39,7 +39,6 @@
optable.addtok("circumfix:[ ]", "term:", "nows", "PGE::Exp::Group")
optable.addtok("circumfix:( )", "term:", "nows", "PGE::Exp::Group")
-
$P0 = find_global "PGE::P6Rule", "parse_subrule"
optable.addtok("term:<", "term:", "nows", $P0)
optable.addtok("term:<?", "term:", "nows", $P0)
@@ -63,8 +62,9 @@
$P0 = find_global "PGE::P6Rule", "parse_modifier"
optable.addtok("prefix::", "<infix:|", "nows", $P0)
-.end
+ optable.addtok("close:>", "<prefix::", "nows")
+.end
.sub "parse_ws_lit"
@@ -257,7 +257,19 @@
$I1 = pos - $I0
subname = substr target, $I0, $I1
$S0 = substr target, pos, 1
- if $S0 != '>' goto end
+ if $S0 != " " goto subrule_end
+ inc pos
+ mpos = pos
+ $P0 = find_global "PGE::Rule", "p6rule"
+ $P1 = $P0(mob)
+ unless $P1 goto end
+ $S0 = $P1
+ mob["arg"] = $S0
+ pos = $P1.to()
+ mpos = -1
+ $S0 = substr target, pos, 1
+ subrule_end:
+ if $S0 != ">" goto end
inc pos
mpos = pos
mob["subname"] = subname
Modified: trunk/compilers/pge/PGE/Rule.pir
==============================================================================
--- trunk/compilers/pge/PGE/Rule.pir (original)
+++ trunk/compilers/pge/PGE/Rule.pir Tue Nov 1 08:25:31 2005
@@ -19,6 +19,8 @@ a number of built-in rules.
.local pmc p6rule
base = getclass "PGE::Match"
$P0 = subclass base, "PGE::Rule"
+ $P0 = new Hash
+ store_global "PGE::Rule", "%:cache", $P0
.return ()
.end
@@ -417,6 +419,39 @@ Match whitespace between tokens.
.end
+=item C<before(PMC mob, STR pattern)>
+
+Perform lookahead -- i.e., check if we're at a position where
+C<pattern> matches. Always returns a zero-width Match object.
+
+=cut
+
+.sub "before"
+ .param pmc mob
+ .param string pattern :optional
+ .param int has_pattern :opt_flag
+ .local pmc cache, rule
+
+ cache = find_global "PGE::Rule", "%:cache"
+ $I0 = exists cache[pattern]
+ if $I0 == 0 goto new_pattern
+ rule = cache[pattern]
+ goto match
+ new_pattern:
+ $P0 = find_global "PGE", "p6rule"
+ rule = $P0(pattern)
+ cache[pattern] = rule
+ match:
+ mob = rule(mob)
+ unless mob goto end
+ $P0 = getattribute mob, "PGE::Match\x0$:from"
+ $P1 = getattribute mob, "PGE::Match\x0$:pos"
+ assign $P1, $P0
+ end:
+ .return (mob)
+.end
+
+
=head1 AUTHOR
Patrick Michaud ([EMAIL PROTECTED]) is the author and maintainer.