Permalink
Browse files

Bugfixes to "isarray" handling for groups

Corrected __get_pmc_keyed in Match
Updated demo.pir to support subrules


git-svn-id: https://svn.parrot.org/parrot/trunk@8010 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information...
1 parent 8cda175 commit 4c67c81123b899fa00ed2164a8e07cbe597f742f @pmichaud pmichaud committed May 8, 2005
Showing with 47 additions and 41 deletions.
  1. +12 −9 compilers/pge/PGE/Exp.pir
  2. +17 −29 compilers/pge/PGE/Match.pir
  3. +3 −2 compilers/pge/README
  4. +15 −1 compilers/pge/demo.pir
@@ -830,7 +830,7 @@ register.
.param pmc next
.param pmc pad
.local pmc exp1
- .local int isarray
+ .local int isarray, psave
.local pmc creps
.local string cname
@@ -846,9 +846,9 @@ register.
unless $I0 goto isarray_0 # skip if no capture
$P0 = self["cname"]
cname = $P0
- cname = concat "%", cname
$I0 = isa $P0, "Integer" # Integer = subpattern cap
if $I0 goto creps_2
+ cname = concat "%", cname
creps_2:
$I0 = exists creps[cname] # have seen capture name?
unless $I0 goto creps_3 #
@@ -859,20 +859,23 @@ register.
creps[cname] = self # mark us for future ref
isarray_0:
- isarray = pad["isarray"] # set group's isarray
- $I0 = self["isarray"] # and pass along to
- isarray |= $I0 # nested objects
- self["isarray"] = isarray
- $I0 = self["cscope"]
- unless $I0 goto isarray_1
+ psave = pad["isarray"] # get current isarray status
+ isarray = self["isarray"] # combine with group's
+ isarray |= psave #
+ self["isarray"] = isarray # and pass to nested
+ pad["isarray"] = isarray
+ $I0 = self["cscope"] # new scope resets
+ unless $I0 goto isarray_1 # array status
+ pad["isarray"] = 0
isarray = 0 # each capt obj is single
delete pad["creps"] # new lexical name scope
isarray_1:
$I0 = defined self["exp1"]
unless $I0 goto end
exp1 = self["exp1"]
- pad["isarray"] = isarray
exp1.analyze(next, pad)
+ pad["isarray"] = psave
+
fc:
$I0 = self["min"] # set up firstchars
if $I0 > 0 goto fc_2
@@ -160,47 +160,35 @@ Returns the portion of the target string matched by this object.
.return ("")
.end
-=item C<__get_pmc_keyed_int(INT key)>
+=item C<__get_pmc_keyed(PMC key)>
-Returns the subpattern capture associated with key. Note that
-this will return either a single Match object or an array of
-match objects depending on the rule.
+Returns the subpattern or subrule capture associated with C<key>.
+If the first character of C<key> is a digit then return the
+subpattern, otherwise return the subrule. Note that this will
+return either a single Match object or an array of match objects
+depending on the rule.
=cut
-.sub "__get_pmc_keyed_int" method
- .param int key
+.sub "__get_pmc_keyed" method
+ .param pmc key
.local pmc capt
+ $S0 = key
+ $I0 = is_digit $S0, 0
+ unless $I0 goto keyed_1
capt = getattribute self, "PGE::Match\x0@:capt"
- $P0 = capt[key]
- $P1 = getprop "isarray", $P0
- unless $P1 goto end
- $P1 = $P1[-1]
- end:
- .return ($P1)
-.end
-
-=item C<__get_pmc_keyed_str(STR key)>
-
-Returns the subrule capture associated with C<key>. Note that
-this can return either a single Match object or an array of
-Match objects depending on the rule.
-
-=cut
-
-.sub "__get_pmc_keyed_str" method
- .param int key
- .local pmc capt
+ goto keyed_2
+ keyed_1:
capt = getattribute self, "PGE::Match\x0%:capt"
+ keyed_2:
$P0 = capt[key]
$P1 = getprop "isarray", $P0
- unless $P1 goto end
- $P1 = $P1[-1]
+ if $P1 goto end
+ $P0 = $P0[-1]
end:
- .return ($P1)
+ .return ($P0)
.end
-
=item C<dump()>
Produces a data dump of the match object and all of its subcaptures.
@@ -29,7 +29,8 @@ The distribution comes with a small F<demo.pir> program that gives an
example of using PGE. To run the demo, simply do
C<parrot demo.pir>. The demo understands the following commands:
- rule <pattern> - a perl 6 rule to be matched
+ rule pattern - compile a Perl 6 rule from "pattern"
+ save name - save the current rule as "name"
text - a text string to match against previously entered rule
pir - display the PIR cod generated for current rule
exp - display the expression tree for the current rule
@@ -95,7 +96,7 @@ PGE doesn't (yet) properly handle nested repetitions of zero-length
patterns in groups -- that's coming next.
This is just the first-cut framework for building the
-remainder of the engine, so many items (assertions, lookaround,
+remainder of the engine, so many items (lookaround,
conjunctions, closures, character classes, and hypotheticals)
just aren't implemented yet. They're on their way!
@@ -15,9 +15,12 @@
find_global p6rule_compile, "PGE", "p6rule"
istrace = 0
null rulesub
+
+ (rulesub, pir, exp) = p6rule_compile("\w+")
+ store_global "ident", rulesub
read_loop:
- print "\ninput \"rule <pattern>\", \"glob <pattern>\", \"pir\",\n"
+ print "\ninput \"rule <pattern>\", \"save <name>\", \"pir\", \"exp\", \n"
print "target string, \"trace\", or \"next\"\n"
getstdin stdin
readline x, stdin
@@ -31,6 +34,7 @@
chopn x, 1
if $S0 == "next" goto match_next
if $S0 == "rule" goto make_p6rule
+ if $S0 == "save" goto save_rule
if $S0 == "pir" goto print_pir
if $S0 == "exp" goto print_exp
if $S0 == "trace" goto toggle_trace
@@ -57,6 +61,16 @@
pattern = substr x, 5
(rulesub, pir, exp) = p6rule_compile(pattern)
goto read_loop
+
+ save_rule:
+ isnull rulesub, match_nopattern
+ x = substr x, 5
+ store_global x, rulesub
+ print "Saved as "
+ print x
+ print "\n"
+ goto read_loop
+
print_pir:
isnull rulesub, match_nopattern

0 comments on commit 4c67c81

Please sign in to comment.