Skip to content

Commit 18f2bc5

Browse files
committed
reorder rubyish grammar. comments and whitespace now last
1 parent a36ba3e commit 18f2bc5

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

examples/rubyish/rubyish.nqp

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,8 @@ grammar Rubyish::Grammar is HLL::Grammar {
6565
|| <.panic('Syntax error')>
6666
}
6767

68-
token continuation { \\ \n }
6968
rule separator { ';' | \n <!after continuation> }
70-
token template-chunk { [<tmpl-unesc>|<tmpl-hdr>] ~ [<tmpl-esc>|$] <template-nibble>* }
71-
proto token template-nibble {*}
72-
token template-nibble:sym<interp> { <interp> }
73-
token template-nibble:sym<stray-tag> { [<.tmpl-unesc>|<.tmpl-hdr>] <.panic("Stray tag, e.g. '%>' or '<?rbi?>'")> }
74-
token template-nibble:sym<literal> { [<!before [<tmpl-esc>|'#{'|$]> .]+ }
75-
76-
token tmpl-hdr {'<?rbi?>' \h* \n? {$*IN_TEMPLATE := 1} }
77-
token tmpl-esc {\h* '<%'
78-
[<?{$*IN_TEMPLATE}> || <.panic('Template directive precedes "<?rbi?>"')>]
79-
}
80-
token tmpl-unesc { '%>' \h* \n?
81-
[<?{$*IN_TEMPLATE}> || <.panic('Template directive precedes "<?rbi?>"')>]
82-
}
69+
token continuation { \\ \n }
8370

8471
rule stmtlist {
8572
[ <stmt=.stmtish>? ] *%% [<.separator>|<stmt=.template-chunk>]
@@ -282,12 +269,6 @@ grammar Rubyish::Grammar is HLL::Grammar {
282269
] <!ww>
283270
}
284271

285-
proto token comment {*}
286-
token comment:sym<line> { '#' [<?{!$*IN_TEMPLATE}> \N* || [<!before <tmpl-unesc>>\N]*] }
287-
token comment:sym<podish> {[^^'=begin'\n] [ .*? [^^'=end'[\n|$]] || <.panic('missing ^^=end at eof')>] }
288-
token ws { <!ww> [\h | <.continuation> | <.comment> | <?{$*IN_PARENS}> \n]* }
289-
token hs { <!ww> [\h | <.continuation> ]* }
290-
291272
INIT {
292273
# Operator precedence levels
293274
# see http://www.tutorialspoint.com/ruby/ruby_operators.htm
@@ -440,6 +421,29 @@ grammar Rubyish::Grammar is HLL::Grammar {
440421
]
441422
}
442423

424+
# Comments and whitespace
425+
proto token comment {*}
426+
token comment:sym<line> { '#' [<?{!$*IN_TEMPLATE}> \N* || [<!before <tmpl-unesc>>\N]*] }
427+
token comment:sym<podish> {[^^'=begin'\n] [ .*? [^^'=end'[\n|$]] || <.panic('missing ^^=end at eof')>] }
428+
token ws { <!ww> [\h | <.continuation> | <.comment> | <?{$*IN_PARENS}> \n]* }
429+
token hs { <!ww> [\h | <.continuation> ]* }
430+
431+
# Templates
432+
token template-chunk { [<tmpl-unesc>|<tmpl-hdr>] ~ [<tmpl-esc>|$] <template-nibble>* }
433+
proto token template-nibble {*}
434+
token template-nibble:sym<interp> { <interp> }
435+
token template-nibble:sym<stray-tag> { [<.tmpl-unesc>|<.tmpl-hdr>] <.panic("Stray tag, e.g. '%>' or '<?rbi?>'")> }
436+
token template-nibble:sym<literal> { [<!before [<tmpl-esc>|'#{'|$]> .]+ }
437+
438+
token tmpl-hdr {'<?rbi?>' \h* \n? {$*IN_TEMPLATE := 1} }
439+
token tmpl-esc {\h* '<%'
440+
[<?{$*IN_TEMPLATE}> || <.panic('Template directive precedes "<?rbi?>"')>]
441+
}
442+
token tmpl-unesc { '%>' \h* \n?
443+
[<?{$*IN_TEMPLATE}> || <.panic('Template directive precedes "<?rbi?>"')>]
444+
}
445+
446+
# Builtin functions
443447
method builtin-init() {
444448
nqp::hash(
445449
'abort', 'die',
@@ -470,22 +474,6 @@ class Rubyish::Actions is HLL::Actions {
470474
make $stmts;
471475
}
472476

473-
method template-chunk($/) {
474-
my $text := QAST::Stmts.new( :node($/) );
475-
$text.push( QAST::Op.new( :op<print>, $_.ast ) )
476-
for $<template-nibble>;
477-
478-
make $text;
479-
}
480-
481-
method template-nibble:sym<interp>($/) {
482-
make $<interp>.ast
483-
}
484-
485-
method template-nibble:sym<literal>($/) {
486-
make QAST::SVal.new( :value(~$/) );
487-
}
488-
489477
method stmtish($/) {
490478
make $<modifier>
491479
?? QAST::Op.new( $<EXPR>.ast, $<stmt>.ast,
@@ -634,7 +622,7 @@ class Rubyish::Actions is HLL::Actions {
634622
my $sigil := ~$<sigil> // '';
635623
my $name := $sigil ~ $<ident>;
636624

637-
my $ns := '';
625+
my $ns;
638626
if $<pkg> {
639627
$ns := ~$<pkg>;
640628
}
@@ -991,6 +979,22 @@ class Rubyish::Actions is HLL::Actions {
991979

992980
method term:sym<lambda>($/) { make $<closure>.ast }
993981

982+
method template-chunk($/) {
983+
my $text := QAST::Stmts.new( :node($/) );
984+
$text.push( QAST::Op.new( :op<print>, $_.ast ) )
985+
for $<template-nibble>;
986+
987+
make $text;
988+
}
989+
990+
method template-nibble:sym<interp>($/) {
991+
make $<interp>.ast
992+
}
993+
994+
method template-nibble:sym<literal>($/) {
995+
make QAST::SVal.new( :value(~$/) );
996+
}
997+
994998
}
995999

9961000
class Rubyish::Compiler is HLL::Compiler {

0 commit comments

Comments
 (0)