Author: fperrad
Date: Thu May  3 00:31:34 2007
New Revision: 18409

Modified:
   trunk/languages/lua/src/ASTGrammar.tg
   trunk/languages/lua/src/dumplex.tg
   trunk/languages/lua/src/lua51.pg
   trunk/languages/lua/src/lua51.pir
   trunk/languages/lua/src/lua51_testlex.pg
   trunk/languages/lua/test_lex.pir

Log:
[Lua]
- add internal_error, lexerror
- parse number in PIR (instead of PGE)

Modified: trunk/languages/lua/src/ASTGrammar.tg
==============================================================================
--- trunk/languages/lua/src/ASTGrammar.tg       (original)
+++ trunk/languages/lua/src/ASTGrammar.tg       Thu May  3 00:31:34 2007
@@ -85,8 +85,7 @@
     .return (past)
 
   no_stat:
-    printerr "no last statement\n"
-    exit 1
+    internal_error("no last statement")
 }
 
 transform past (Lua::Grammar::if_stat) :language('PIR') {
@@ -265,8 +264,7 @@
     if null $P0 goto more_expr
     .return tree.'get'('past', $P0, 'Lua::Grammar::expression_list')
   more_expr:
-    printerr "Not implemented\n"
-    exit 1
+    internal_error("Not implemented")
 
 }
 
@@ -366,9 +364,9 @@
   iter_end:
     .return (past)
   error:
-    printerr "null access in expr list\n"
-    exit 1
+    internal_error("null access in expr list")
 }
+
 transform past (Lua::Grammar::expression) :language('PIR') {
     $P0 = node['expr']
     .return tree.'get'('past', $P0, 'Lua::Grammar::expr')
@@ -471,7 +469,7 @@
     result.'init'('node'=>node, 'vtype'=>'.String', 'name'=>$S0)
     .return (result)
   error:
-    printerr "Unknown string\n"
+    internal_error("Unknown string")
 }
 
 =head1 AUTHORS

Modified: trunk/languages/lua/src/dumplex.tg
==============================================================================
--- trunk/languages/lua/src/dumplex.tg  (original)
+++ trunk/languages/lua/src/dumplex.tg  Thu May  3 00:31:34 2007
@@ -71,6 +71,7 @@
     arg = tree.get('past', $P1, 'Lua::TestLex::punctuator')
     goto L9
 L5:
+    internal_error('unknown token')
 L9:
     past.'push'(arg)
     .return (past)

Modified: trunk/languages/lua/src/lua51.pg
==============================================================================
--- trunk/languages/lua/src/lua51.pg    (original)
+++ trunk/languages/lua/src/lua51.pg    Thu May  3 00:31:34 2007
@@ -285,13 +285,6 @@
 }
 
 
-token number {
-  |  0 <[Xx]> <xdigit>+
-  | [ <digit>+ [ \. <digit>* ]? | \. <digit>+ ]
-    [ <[Ee]> <[+\-]>? <digit>+ ]?
-}
-
-
 token ws {
     [ \s+
     | <'--'> <long_comment>

Modified: trunk/languages/lua/src/lua51.pir
==============================================================================
--- trunk/languages/lua/src/lua51.pir   (original)
+++ trunk/languages/lua/src/lua51.pir   Thu May  3 00:31:34 2007
@@ -121,31 +121,74 @@
 .end
 
 
-=item C<name>
+=item C<lexerror (message)>
+
+=cut
+
+.sub 'lexerror'
+    .param pmc mob
+    .param string message
+    .local int lineno
+#    .local pmc infile
+#    infile = get_hll_global ['TGE::Compiler'], '$!infile'
+#    $S0 = infile
+#    $S0 .= ':'
+    $S0 = '_._:'
+    $P0 = get_hll_global ['PGE::Util'], 'line_number'
+    lineno = mob.$P0()
+    inc lineno
+    $S1 = lineno
+    $S0 .= $S1
+    $S0 .= ': '
+    $S0 .= message
+    $S0 .= " near '"
+    $S1 = mob.'text'()
+    $S0 .= $S1
+    $S0 .= "'"
+    .local pmc ex
+    ex = new .Exception
+    ex['_message'] = $S0
+    throw ex
+.end
 
-ident but not keywords
+
+=item C<name>
 
 =cut
 
+.include 'cclass.pasm'
+
 .sub 'name'
     .param pmc mob
-    .param pmc params :slurpy
+    .param pmc adverbs         :slurpy :named
+    .local string target
+    .local pmc mfrom, mpos
+    .local int pos, lastpos
 
-    $P0 = get_hll_global ['PGE::Match'], 'ident'
-    mob = $P0(mob)
+    $P0 = get_hll_global ["PGE::Match"], 'newfrom'
+    (mob, target, mfrom, mpos) = $P0(mob)
 
-    unless mob goto L1
+    pos = mfrom
+    lastpos = length target
+    $S0 = substr target, pos, 1
+    if $S0 == '_' goto L1
+    $I0 = is_cclass .CCLASS_ALPHABETIC, target, pos
+    if $I0 == 0 goto L2
+L1:
+    $I0 = pos
+    pos = find_not_cclass .CCLASS_WORD, target, pos, lastpos
+    $I1 = pos - $I0
+    $S0 = substr target, $I0, $I1
     .local pmc kw
     kw = get_hll_global 'keyword'
-    unless null kw goto L2
+    unless null kw goto L3
     kw = _const_keyword()
     set_hll_global 'keyword', kw
-L2:
-    $S0 = mob.'text'()
+L3:
     $I0 = exists kw[$S0]
-    unless $I0 goto L1
-    mob.'next'()
-L1:
+    if $I0 goto L2
+    mpos = pos
+L2:
     .return (mob)
 .end
 
@@ -177,6 +220,126 @@
 .end
 
 
+=item C<number>
+
+=cut
+
+.sub 'number'
+    .param pmc mob
+    .param pmc params :slurpy
+
+    mob = read_numeral(mob)
+    unless mob goto L1
+
+    .local string target
+    .local int pos, lastpos
+
+    target = mob.'text'()
+    lastpos = length target
+L_alt1:     #   0 [Xx] <xdigit>+
+    pos = 0
+    $S0 = substr target, pos, 1
+    unless $S0 == '0' goto L_alt2
+    inc pos
+    $S0 = substr target, pos, 1
+    $I0 = index 'Xx', $S0
+    if $I0 < 0 goto L_alt2
+    inc pos
+    $I0 = is_cclass .CCLASS_HEXADECIMAL, target, pos
+    if $I0 == 0 goto L_alt2
+    pos = find_not_cclass .CCLASS_HEXADECIMAL, target, pos, lastpos
+    goto L_end
+L_alt2:     #   <digit>+ [\.]?
+    pos = 0
+    $I0 = is_cclass .CCLASS_NUMERIC, target, pos
+    if $I0 == 0 goto L_alt3
+    pos = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
+    $S0 = substr target, pos, 1
+    unless $S0 == '.' goto L_opt2
+    inc pos
+    goto L_opt1
+L_alt3:     #   \. <digit>
+    pos = 0
+    $S0 = substr target, pos, 1
+    unless $S0 == '.' goto L_end
+    inc pos
+    $I0 = is_cclass .CCLASS_NUMERIC, target, pos
+    if $I0 == 0 goto L_end
+L_opt1:     #   <digit>*
+    pos = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
+L_opt2:     #   [Ee] [+\-]? <digit>+
+    .local int pos2
+    pos2 = pos
+    $S0 = substr target, pos2, 1
+    $I0 = index 'Ee', $S0
+    if $I0 < 0 goto L_end
+    inc pos2
+    $S0 = substr target, pos2, 1
+    $I0 = index '+-', $S0
+    if $I0 < 0 goto L_opt3
+    inc pos2
+L_opt3:
+    $I0 = is_cclass .CCLASS_NUMERIC, target, pos2
+    if $I0 == 0 goto L_end
+    pos = find_not_cclass .CCLASS_NUMERIC, target, pos2, lastpos
+L_end:
+    if pos == lastpos goto L1
+    lexerror(mob, 'malformed number')
+L1:
+    .return (mob)
+.end
+
+
+.sub 'read_numeral'
+    .param pmc mob
+    .param pmc adverbs         :slurpy :named
+    .local string target
+    .local pmc mfrom, mpos
+    .local int pos, lastpos
+
+    $P0 = get_hll_global ["PGE::Match"], 'newfrom'
+    (mob, target, mfrom, mpos) = $P0(mob)
+
+    pos = mfrom
+    lastpos = length target
+    $S0 = substr target, pos, 1
+    unless $S0 == '.' goto L1
+    inc pos
+L1:
+    $I0 = is_cclass .CCLASS_NUMERIC, target, pos
+    if $I0 == 0 goto L2
+    inc pos
+L3:
+    $I0 = is_cclass .CCLASS_NUMERIC, target, pos
+    if $I0 == 0 goto L4
+    pos = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
+    $S0 = substr target, pos, 1
+    unless $S0 == '.' goto L5
+    inc pos
+    goto L3
+L4:
+    $S0 = substr target, pos, 1
+    unless $S0 == '.' goto L5
+    inc pos
+    goto L3
+L5:
+    $S0 = substr target, pos, 1
+    $I0 = index 'Ee', $S0
+    if $I0 < 0 goto L6
+    inc pos
+    $S0 = substr target, pos, 1
+    $I0 = index '+-', $S0
+    if $I0 < 0 goto L6
+    inc pos
+L6:
+    $I0 = .CCLASS_NUMERIC | .CCLASS_WORD
+    pos = find_not_cclass $I0, target, pos, lastpos
+    mpos = pos
+L2:
+    .return (mob)
+.end
+
+
 =item C<quoted_literal>
 
 =cut
@@ -197,7 +360,7 @@
     literal = ''
 LOOP:
     if pos < lastpos goto L1
-    error(mob, "unfinished string")
+    lexerror(mob, "unfinished string")
 L1:
     $S0 = substr target, pos, 1
     if $S0 != delim goto L2
@@ -207,7 +370,7 @@
 L2:
     $I0 = index "\n\r", $S0
     if $I0 < 0 goto L3
-    error(mob, "unfinished string")
+    lexerror(mob, "unfinished string")
 L3:
     if $S0 != "\\" goto CONCAT
     inc pos
@@ -242,7 +405,7 @@
     dec pos
 L7:
     if $I0 < 256 goto L8
-    error(mob, "escape sequence too large")
+    lexerror(mob, "escape sequence too large")
 L8:
     $S0 = chr $I0
 
@@ -276,7 +439,7 @@
     (pos, sep) = _skip_sep(target, pos, '[')
     if sep >= 0 goto L1
     if sep == -1 goto END
-    error(mob, "invalid long string delimiter")
+    lexerror(mob, "invalid long string delimiter")
 L1:
     inc pos
     $S0 = substr target, pos, 1
@@ -289,14 +452,14 @@
     literal = ''
 LOOP:
     if pos < lastpos goto L3
-    error(mob, "unfinished long string")
+    lexerror(mob, "unfinished long string")
 L3:
     $S0 = substr target, pos, 1
     if $S0 != '[' goto L4
     inc pos
     $S0 = substr target, pos, 1
     if $S0 != '[' goto L5
-    error(mob, "nesting of [[...]] is deprecated")
+    lexerror(mob, "nesting of [[...]] is deprecated")
 L5:
     dec pos
     goto CONCAT
@@ -362,14 +525,14 @@
 #    literal = ''
 LOOP:
     if pos < lastpos goto L3
-    error(mob, "unfinished long comment")
+    lexerror(mob, "unfinished long comment")
 L3:
     $S0 = substr target, pos, 1
     if $S0 != '[' goto L4
     inc pos
     $S0 = substr target, pos, 1
     if $S0 != '[' goto L5
-    error(mob, "nesting of [[...]] is deprecated")
+    lexerror(mob, "nesting of [[...]] is deprecated")
 L5:
     dec pos
     goto CONCAT
@@ -422,6 +585,22 @@
 .end
 
 
+.namespace [ 'Lua::PAST::Grammar' ]
+
+=item C<internal_error>
+
+used in F<languages/lua/src/ASTGrammar.tg>
+
+=cut
+
+.sub internal_error
+    .param string msg
+    $S0 = "ERROR_INTERNAL (PAST): " . msg
+    $S0 .= "\n"
+    printerr $S0
+    exit 1
+.end
+
 .include 'languages/lua/src/ASTGrammar.pir'
 .include 'languages/lua/src/lua51_grammar_gen.pir'
 

Modified: trunk/languages/lua/src/lua51_testlex.pg
==============================================================================
--- trunk/languages/lua/src/lua51_testlex.pg    (original)
+++ trunk/languages/lua/src/lua51_testlex.pg    Thu May  3 00:31:34 2007
@@ -3,7 +3,7 @@
 
 =head1 NAME
 
-src/lua51_testlex.tg - a grammar for testing lexico
+src/lua51_testlex.pg - a grammar for testing lexico
 
 =head1 DESCRIPTION
 

Modified: trunk/languages/lua/test_lex.pir
==============================================================================
--- trunk/languages/lua/test_lex.pir    (original)
+++ trunk/languages/lua/test_lex.pir    Thu May  3 00:31:34 2007
@@ -15,7 +15,21 @@
 
 =head1 DESCRIPTION
 
-C<test_lex> is a dumper for Lua 5.1 lexicography.
+This compiler extends C<HLLCompiler>
+(see F<runtime/parrot/library/Parrot/HLLCompiler.pir>)
+
+This compiler defines the following stages:
+
+=over 4
+
+=item * parse F<languages/lua/src/lua51_testlex.pg>
+
+=item * past  F<languages/lua/src/dumplex.tg>
+
+=back
+
+and imports many definitions from the full Lua compiler
+(F<languages/lua/src/lua51.pir>).
 
 =cut
 
@@ -51,10 +65,15 @@
     set_hll_global ['Lua::TestLex'], 'long_string', $P0
     $P0 = get_hll_global ['Lua::Grammar'], 'long_comment'
     set_hll_global ['Lua::TestLex'], 'long_comment', $P0
+
+    # import Lua::PAST::Grammar::internal_error into Lua::DumpLex
+    $P0 = get_hll_global ['Lua::PAST::Grammar'], 'internal_error'
+    set_hll_global ['Lua::DumpLex'], 'internal_error', $P0
 .end
 
 .sub 'main' :main
     .param pmc args
+    load_bytecode 'PGE/Dumper.pbc'
     $P0 = compreg 'LuaTestLex'
     $P0.'command_line'(args)
 .end

Reply via email to