Author: fperrad
Date: Fri May 4 00:14:59 2007
New Revision: 18420
Modified:
trunk/languages/lua/src/dumplex.tg
trunk/languages/lua/src/lua51.pir
trunk/languages/lua/src/lua51_testlex.pg
trunk/languages/lua/t/test_lex.t
trunk/languages/lua/test_lex.pir
Log:
[Lua]
- lexico: add tests and pod
Modified: trunk/languages/lua/src/dumplex.tg
==============================================================================
--- trunk/languages/lua/src/dumplex.tg (original)
+++ trunk/languages/lua/src/dumplex.tg Fri May 4 00:14:59 2007
@@ -7,7 +7,7 @@
=head1 DESCRIPTION
-Just print node.
+Just emits code that prints terminal nodes.
Used by F<languages/lua/test_lex.pir>.
@@ -15,6 +15,15 @@
grammar Lua::DumpLex is TGE::Grammar;
+
+=head2 Functions
+
+=over 4
+
+=item C<transform past (ROOT)>
+
+=cut
+
transform past (ROOT) :language('PIR') {
.local pmc past
new past, 'PAST::Block'
@@ -37,6 +46,11 @@
.return (past)
}
+
+=item C<transform past (Lua::TestLex::token)>
+
+=cut
+
transform past (Lua::TestLex::token) :language('PIR') {
.local pmc past, arg
new past, 'PAST::Op'
@@ -77,6 +91,11 @@
.return (past)
}
+
+=item C<transform past (Lua::TestLex::keyword)>
+
+=cut
+
transform past (Lua::TestLex::keyword) :language('PIR') {
.local pmc past
new past, 'PAST::Val'
@@ -86,6 +105,11 @@
.return (past)
}
+
+=item C<transform past (Lua::TestLex::punctuator)>
+
+=cut
+
transform past (Lua::TestLex::punctuator) :language('PIR') {
.local pmc past
new past, 'PAST::Val'
@@ -95,6 +119,11 @@
.return (past)
}
+
+=item C<transform past (Lua::TestLex::Name)>
+
+=cut
+
transform past (Lua::TestLex::Name) :language('PIR') {
.local pmc past
new past, 'PAST::Val'
@@ -104,6 +133,11 @@
.return (past)
}
+
+=item C<transform past (Lua::TestLex::String)>
+
+=cut
+
transform past (Lua::TestLex::String) :language('PIR') {
.local pmc past
new past, 'PAST::Val'
@@ -112,18 +146,26 @@
$P0 = node['quoted_literal']
$S0 = $P0
$S0 = concat "String:\t", $S0
- goto L2
+ goto L9
L1:
$I0 = defined node['long_string']
unless $I0 goto L2
$P0 = node['long_string']
$S0 = $P0
$S0 = concat "LongString:\t", $S0
+ goto L9
L2:
+ internal_error('unknown string')
+L9:
past.'init'('node'=>node, 'vtype'=>'.String', 'name'=>$S0, 'ctype'=>'s~')
.return (past)
}
+
+=item C<transform past (Lua::TestLex::Number)>
+
+=cut
+
transform past (Lua::TestLex::Number) :language('PIR') {
.local pmc past
new past, 'PAST::Val'
@@ -133,6 +175,8 @@
.return (past)
}
+=back
+
=head1 AUTHOR
Francois Perrad
Modified: trunk/languages/lua/src/lua51.pir
==============================================================================
--- trunk/languages/lua/src/lua51.pir (original)
+++ trunk/languages/lua/src/lua51.pir Fri May 4 00:14:59 2007
@@ -87,45 +87,21 @@
.end
-=item C<syntax_error (message)>
+=item C<syntax_error (match, [message])>
=cut
.sub 'syntax_error'
- .param pmc self
- .param string message
-
- $P0 = get_hll_global ['PGE::Util'], 'line_number'
- if null $P0 goto NO_LINE_NR_METHOD
- $I0 = self.$P0()
-
- # line_number() starts counting at line 0, so increment:
- inc $I0
-
- printerr "Syntax error (line "
- printerr $I0
- printerr "): "
- printerr message
- printerr "\n\n"
-
- # increment parse errors
- .local pmc errs
- errs = get_root_global 'errors'
- inc errs
-
- .return()
-
- NO_LINE_NR_METHOD:
- printerr "can't find PGE::Util::line_number"
- exit 1
+ .param pmc mob
+ .param string message :optional
+ unless null message goto L1
+ message = 'syntax error'
+L1:
+ lexerror(mob, message)
.end
-=item C<lexerror (message)>
-
-=cut
-
-.sub 'lexerror'
+.sub 'lexerror' :anon
.param pmc mob
.param string message
.local int lineno
@@ -141,10 +117,12 @@
$S0 .= $S1
$S0 .= ': '
$S0 .= message
- $S0 .= " near '"
$S1 = mob.'text'()
+ if $S1 == '' goto L1
+ $S0 .= " near '"
$S0 .= $S1
$S0 .= "'"
+L1:
.local pmc ex
ex = new .Exception
ex['_message'] = $S0
@@ -290,7 +268,7 @@
.end
-.sub 'read_numeral'
+.sub 'read_numeral' :anon
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
@@ -360,6 +338,7 @@
literal = ''
LOOP:
if pos < lastpos goto L1
+ mpos = pos
lexerror(mob, "unfinished string")
L1:
$S0 = substr target, pos, 1
@@ -370,6 +349,7 @@
L2:
$I0 = index "\n\r", $S0
if $I0 < 0 goto L3
+ mpos = pos
lexerror(mob, "unfinished string")
L3:
if $S0 != "\\" goto CONCAT
@@ -405,6 +385,7 @@
dec pos
L7:
if $I0 < 256 goto L8
+ mpos = pos
lexerror(mob, "escape sequence too large")
L8:
$S0 = chr $I0
@@ -439,6 +420,7 @@
(pos, sep) = _skip_sep(target, pos, '[')
if sep >= 0 goto L1
if sep == -1 goto END
+ mpos = pos
lexerror(mob, "invalid long string delimiter")
L1:
inc pos
@@ -459,6 +441,8 @@
inc pos
$S0 = substr target, pos, 1
if $S0 != '[' goto L5
+ inc pos
+ mpos = pos
lexerror(mob, "nesting of [[...]] is deprecated")
L5:
dec pos
@@ -532,6 +516,8 @@
inc pos
$S0 = substr target, pos, 1
if $S0 != '[' goto L5
+ inc pos
+ mpos = pos
lexerror(mob, "nesting of [[...]] is deprecated")
L5:
dec pos
Modified: trunk/languages/lua/src/lua51_testlex.pg
==============================================================================
--- trunk/languages/lua/src/lua51_testlex.pg (original)
+++ trunk/languages/lua/src/lua51_testlex.pg Fri May 4 00:14:59 2007
@@ -12,6 +12,15 @@
So, it just defines some top rules, the rules that we want to test are
imported from the whole Lua grammar.
+ TOP ::= {token}
+ token ::= Name | keyword | String | Number | punctuator
+ keyword ::= 'and' | 'break' | 'do' | 'elseif' | 'else' | 'end' | 'false'
+ | 'for' | 'function' | 'if' | 'in' | 'local' | 'nil' | 'not'
+ | 'or' | 'repeat' | 'return' | 'then' | 'true' | 'until' | 'while'
+ punctuator ::= '+' | '-' | '#' | '*' | '/' | '%' | '^' | '...' | '..' | '.'
+ | '<=' | '<' | '>=' | '>' | '==' | '~=' | '='
+ | ',' | ';' | ':' | '(' | ')' | '{' | '}' | '[' | ']'
+
Used by F<languages/lua/test_lex.pir>.
=cut
Modified: trunk/languages/lua/t/test_lex.t
==============================================================================
--- trunk/languages/lua/t/test_lex.t (original)
+++ trunk/languages/lua/t/test_lex.t Fri May 4 00:14:59 2007
@@ -25,7 +25,7 @@
use FindBin;
use lib "$FindBin::Bin";
-use Parrot::Test tests => 8;
+use Parrot::Test tests => 21;
use Test::More;
language_output_is( 'Lua_lex', <<'CODE', <<'OUT', 'hello' );
@@ -37,6 +37,19 @@
punct: )
OUT
+language_output_is( 'Lua_lex', <<'CODE', <<'OUT', 'name' );
+i j i10 _ij _
+aSomewhatLongName _INPUT
+CODE
+Name: i
+Name: j
+Name: i10
+Name: _ij
+Name: _
+Name: aSomewhatLongName
+Name: _INPUT
+OUT
+
language_output_is( 'Lua_lex', <<'CODE', <<'OUT', 'keyword' );
and
break
@@ -104,6 +117,18 @@
123"
OUT
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'unfinished string' );
+print "unfinished string
+CODE
+/^\w+\.\w+:1: unfinished string near '"?unfinished string'/
+OUT
+
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'string with escape
sequence too large' );
+print "escape\333sequence"
+CODE
+/^\w+\.\w+:1: escape sequence too large near '"?escape/
+OUT
+
language_output_is( 'Lua_lex', <<'CODE', <<'OUT', 'long string' );
[[alo
123"]]
@@ -117,6 +142,26 @@
123"
OUT
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'invalid long string
delimiter' );
+print [===+ string ]===]
+CODE
+/^\w+\.\w+:1: invalid long string delimiter near '\[==='/
+OUT
+
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'unfinished string' );
+print [[unfinished long string
+
+CODE
+/^\w+\.\w+:1: unfinished long string/
+OUT
+
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'nested long string' );
+print [[ long string [[ nested ]] ]]
+
+CODE
+/^\w+\.\w+:1: nesting of \[\[\.\.\.\]\] is deprecated near '\[\[ long string
\[\['/
+OUT
+
language_output_is( 'Lua_lex', <<'CODE', <<'OUT', 'number' );
3
3.0
@@ -137,6 +182,24 @@
Number: 0x56
OUT
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'malformed number' );
+0x1Bh
+CODE
+/^\w+\.\w+:1: malformed number near '0x1Bh'/
+OUT
+
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'malformed number' );
+1.2.34
+CODE
+/^\w+\.\w+:1: malformed number near '1.2.34'/
+OUT
+
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'malformed number' );
+.2A
+CODE
+/^\w+\.\w+:1: malformed number near '.2A'/
+OUT
+
language_output_is( 'Lua_lex', <<'CODE', <<'OUT', 'comment' );
-- comment
1 -- comment
@@ -148,16 +211,50 @@
language_output_is( 'Lua_lex', <<'CODE', <<'OUT', 'long comment' );
--[[
-long comment
+ long comment
]]
1
--[===[
-long comment
+ long comment
]===]
CODE
Number: 1
OUT
+language_output_is( 'Lua_lex', <<'CODE', <<'OUT', 'not long comment' );
+--[[
+ no action (comment)
+--]]
+1
+---[[
+ active
+--]]
+CODE
+Number: 1
+Name: active
+OUT
+
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'unfinished long comment'
);
+ --[[unfinished long comment
+
+CODE
+/^\w+\.\w+:1: unfinished long comment/
+OUT
+
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'nested long comment' );
+--[[ long comment [[ nested ]] ]]
+
+CODE
+/^\w+\.\w+:1: nesting of \[\[\.\.\.\]\] is deprecated near '\[\[ long comment
\[\['/
+OUT
+
+language_output_like( 'Lua_lex', <<'CODE', <<'OUT', 'syntax error' );
+!!
+CODE
+/^\w+\.\w+:1: syntax error/
+OUT
+
+
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
Modified: trunk/languages/lua/test_lex.pir
==============================================================================
--- trunk/languages/lua/test_lex.pir (original)
+++ trunk/languages/lua/test_lex.pir Fri May 4 00:14:59 2007
@@ -45,20 +45,18 @@
$P0.'commandline_banner'($S0)
$P0.'commandline_prompt'('> ')
- # import PGE::Util::die into Lua::TestLex
- $P0 = get_hll_global ['PGE::Util'], 'die'
- set_hll_global ['Lua::TestLex'], 'die', $P0
-
# import Lua::Grammar::* into Lua::TestLex
- $P0 = get_hll_global ['Lua::Grammar'], 'name'
- set_hll_global ['Lua::TestLex'], 'Name', $P0
$P0 = get_hll_global ['Lua::Grammar'], 'string'
set_hll_global ['Lua::TestLex'], 'String', $P0
- $P0 = get_hll_global ['Lua::Grammar'], 'number'
- set_hll_global ['Lua::TestLex'], 'Number', $P0
$P0 = get_hll_global ['Lua::Grammar'], 'ws'
set_hll_global ['Lua::TestLex'], 'ws', $P0
+ $P0 = get_hll_global ['Lua::Grammar'], 'syntax_error'
+ set_hll_global ['Lua::TestLex'], 'die', $P0
+ $P0 = get_hll_global ['Lua::Grammar'], 'name'
+ set_hll_global ['Lua::TestLex'], 'Name', $P0
+ $P0 = get_hll_global ['Lua::Grammar'], 'number'
+ set_hll_global ['Lua::TestLex'], 'Number', $P0
$P0 = get_hll_global ['Lua::Grammar'], 'quoted_literal'
set_hll_global ['Lua::TestLex'], 'quoted_literal', $P0
$P0 = get_hll_global ['Lua::Grammar'], 'long_string'