Author: pmichaud
Date: Wed Oct 26 20:54:52 2005
New Revision: 9586
Added:
trunk/runtime/parrot/library/PGE/Util.pir
Modified:
trunk/MANIFEST
trunk/compilers/pge/PGE/OPTable.pir
trunk/compilers/pge/library.pge
trunk/compilers/pge/mklib.pir
Log:
Added PGE::Util, with a compile_rules subroutine for quickly
compiling grammars and sets of rules.
Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST (original)
+++ trunk/MANIFEST Wed Oct 26 20:54:52 2005
@@ -1642,6 +1642,7 @@ runtime/parrot/library/PGE/Dumper.pir
runtime/parrot/library/PGE/Glob.pir [library]
runtime/parrot/library/PGE/Hs.pir [library]
runtime/parrot/library/PGE/Text.pir [library]
+runtime/parrot/library/PGE/Util.pir [library]
runtime/parrot/library/SDL.imc [library]
runtime/parrot/library/SDL/App.imc [library]
runtime/parrot/library/SDL/Button.imc [library]
Modified: trunk/compilers/pge/PGE/OPTable.pir
==============================================================================
--- trunk/compilers/pge/PGE/OPTable.pir (original)
+++ trunk/compilers/pge/PGE/OPTable.pir Wed Oct 26 20:54:52 2005
@@ -279,10 +279,20 @@ representing the result of the parse.
key = $P0."lkey"(target, wspos)
tok = $P0[key]
bsr tok_match
- unless oper goto term_error
- $P0 = tok["syncat"]
- if $P0 == PGE_OPTABLE_PREFIX goto oper_shift # (S1)
- if $P0 == PGE_OPTABLE_CIRCUMFIX goto oper_shift # (S2, P2)
+ if oper goto expect_term_2
+ $I0 = elements tokstack
+ if $I0 < 1 goto term_error
+ top = tokstack[-1]
+ $S0 = top["opts"]
+ $I0 = index $S0, "nullterm"
+ if $I0 < 0 goto term_error
+ oper = newfrom(mob, wspos, "PGE::Match")
+ push termstack, oper
+ goto expect_oper
+ expect_term_2:
+ tokcat = tok["syncat"]
+ if tokcat == PGE_OPTABLE_PREFIX goto oper_shift # (S1)
+ if tokcat == PGE_OPTABLE_CIRCUMFIX goto oper_shift # (S2, P2)
push termstack, oper
pos = oper.to()
@@ -334,7 +344,6 @@ representing the result of the parse.
push tokstack, tok
push operstack, oper
pos = oper.to()
- tokcat = tok["syncat"]
if tokcat >= PGE_OPTABLE_PREFIX goto expect_term
if tokcat == PGE_OPTABLE_POSTFIX goto expect_oper
if topcat == PGE_OPTABLE_TERNARY goto expect_term
Modified: trunk/compilers/pge/library.pge
==============================================================================
--- trunk/compilers/pge/library.pge (original)
+++ trunk/compilers/pge/library.pge Wed Oct 26 20:54:52 2005
@@ -2,7 +2,5 @@ grammar PGE::Rule;
rule ident { [ _ | <?alpha> ] \w* }
-# comments
-
rule name { <ident> [ \:\: <ident> ]* }
Modified: trunk/compilers/pge/mklib.pir
==============================================================================
--- trunk/compilers/pge/mklib.pir (original)
+++ trunk/compilers/pge/mklib.pir Wed Oct 26 20:54:52 2005
@@ -32,43 +32,14 @@ file.
load()
load = find_global "PGE::Rule", "__onload"
load()
- p6rule = find_global "PGE", "p6rule"
- $S0 = ':w ( (grammar) ([\w|\:]+) ; | (rule) (\w+) \{(<-[}]>+)\} | (\#)\N*
)*'
- (gparse, code) = p6rule($S0)
- grammar = "PGE::Rule"
+ load_bytecode "PGE/Util.pir"
$P0 = open "library.pge", "<"
$S0 = read $P0, 65535
close $P0
- $P0 = gparse($S0)
- $P1 = $P0[0]
- $I0 = elements $P1
- $I1 = 0
- loop_1:
- if $I1 >= $I0 goto end
- stmt = $P1[$I1]
- inc $I1
- $P0 = stmt[0]
- keyword = $P0
- if keyword == "#" goto loop_1
- if keyword == "grammar" goto grammar_stmt
- $P0 = stmt[1]
- name = $P0
- $P0 = stmt[2]
- code = $P0
- print "\n\n# "
- print grammar
- print "::"
- print name
- print "\n"
- ($P0, code) = p6rule(code, grammar, name)
+ $P1 = find_global "PGE", "compile_rules"
+ code = $P1($S0)
print code
- goto loop_1
- grammar_stmt:
- $P0 = stmt[1]
- grammar = $P0
- goto loop_1
- end:
.end
.include "compilers/pge/PGE/TokenHash.pir"
Added: trunk/runtime/parrot/library/PGE/Util.pir
==============================================================================
--- (empty file)
+++ trunk/runtime/parrot/library/PGE/Util.pir Wed Oct 26 20:54:52 2005
@@ -0,0 +1,66 @@
+.namespace [ "PGE" ]
+
+=head1 TITLE
+
+PGE::Util - utility PGE subroutines
+
+=head1 DESCRIPTION
+
+The subroutines in this module are provided to make working with
+PGE a little easier.
+
+=item C<compile_rules(STR src)>
+
+Compiles and loads a sequence of rule statements from the string
+given by C<src>. This string has statements of the form
+
+ grammar <name> ;
+ rule <ident> { <pattern> } ;
+
+Each of the patterns in rule statements are then compiled and
+loaded under the name and identifier. If no C<grammar> statement
+is supplied, rules are loaded into the C<PGE::Rule> namespace.
+
+=cut
+
+.sub "compile_rules"
+ .param string str
+ .local pmc p6rule
+ .local pmc parser
+ .local pmc match, slist, stmt
+ .local pmc namespace
+ .local pmc code
+
+ namespace = new String
+ namespace = "PGE::Rule"
+ code = new String
+ p6rule = find_global "PGE", "p6rule"
+ parser = p6rule(":w ( (grammar) (\\w+[\\:\\:\\w+]*) ;? | (rule) (\\w+)
\\{(<-[}]>*)\\} ;? | (\\#)\\N*\\n )*")
+
+ match = parser(str)
+ unless match goto end
+ slist = match[0]
+ loop:
+ unless slist goto end
+ stmt = shift slist
+ $P1 = stmt[0]
+ if $P1 == "#" goto loop
+ if $P1 == "rule" goto compile_rule
+ namespace = stmt[1]
+ goto loop
+ compile_rule:
+ $P1 = stmt[1]
+ $P2 = stmt[2]
+ ($P3, $P4, $P5) = p6rule($P2, namespace, $P1)
+ code .= "# <"
+ code .= namespace
+ code .= "::"
+ code .= $P1
+ code .= ">\n\n"
+ code .= $P4
+ code .= "\n"
+ goto loop
+ end:
+ .return (code)
+.end
+