Author: kjs
Date: Sun Sep 30 20:58:37 2007
New Revision: 21701
Modified:
trunk/languages/PIR/lib/pir.pg
Log:
languages/PIR:
* refactoring and cleaning up the grammar of lang/pir
Modified: trunk/languages/PIR/lib/pir.pg
==============================================================================
--- trunk/languages/PIR/lib/pir.pg (original)
+++ trunk/languages/PIR/lib/pir.pg Sun Sep 30 20:58:37 2007
@@ -39,16 +39,18 @@
rule sub_def {
<'.sub'>
- [ <sub_id> | <syntax_error: sub identifier (id or string constant)
expected> ]
+ <sub_id>
<sub_pragmas>?
- [ <?nl> | <syntax_error: newline expected> ]
+ <?NL>
<param_decl>*
<labeled_pir_instr>*
[ <'.end'> | <syntax_error: '.end' expected to close method/sub body> ]
}
rule sub_id {
- <id> | <string_constant>
+ | <id>
+ | <string_constant>
+ | <syntax_error: subroutine identifier expected>
}
=head3 sub_pragmas
@@ -92,10 +94,7 @@
rule outer_pragma {
<':outer'>
[ <'('> | <syntax_error: '(' expected after ':outer' flag> ]
- [ <string_constant>
- | <id>
- | <syntax_error: identifier for outer sub expected>
- ]
+ <sub_id>
[ <')'> | <syntax_error: ')' expected to close ':outer' flag> ]
}
@@ -118,7 +117,7 @@
| <syntax_error: parameter type or register expected>
]
[ <get_flags> | <':unique_reg'> ]*
- [ <?nl> | <syntax_error: newline expected after parameter declaration> ]
+ <?NL>
}
@@ -133,18 +132,19 @@
[ <label> <instr>?
| <instr>
]
- [ <?nl> | <syntax_error: newline expected after instruction> ]
+ <?NL>
}
rule labeled_pasm_instr {
[ <label> <pasm_instr>?
| <pasm_instr>
]
- [ <?nl> | <syntax_error: newline expected after instruction> ]
+ <?NL>
}
rule instr {
- <pir_instr> | <pasm_instr>
+ | <pir_instr>
+ | <pasm_instr>
}
token pir_instr {
@@ -159,7 +159,7 @@
| <return_stat>
| <sub_invocation>
| <macro_invocation>
- | <jump_stat>
+ | <goto_stat>
| <source_info>
}
@@ -227,9 +227,9 @@
}
rule const_def_tail {
- | <'int'> <const_assign> [ <int_constant> | <syntax_error: integer
constant expected> ]
- | <'num'> <const_assign> [ <float_constant> | <syntax_error: floating
point constant expected> ]
- | [ <'pmc'> | <macro_id> | <id> ] <const_assign> [ <string_constant> |
<syntax_error: string constant expected> ]
+ | <'int'> <const_assign> [ <int_constant> | <syntax_error: integer
constant expected> ]
+ | <'num'> <const_assign> [ <float_constant> | <syntax_error: floating
point constant expected> ]
+ | <'pmc'> <const_assign> [ <string_constant> | <syntax_error: string
constant expected> ]
| <'string'> <const_assign> [ <string_constant> | <syntax_error: string
constant expected> ]
| <syntax_error: type expected for constant>
}
@@ -240,10 +240,16 @@
}
rule conditional_stat {
- [ <'if'> | <'unless'> ]
+ [ <'if'>
+ | <'unless'>
+ ]
<conditional_expr>
- [ <'goto'> | <syntax_error: 'goto' expected> ]
- [ <id> | <pasm_instr> | <syntax_error: label identifier expected> ]
+ <goto_stat>
+}
+
+token label_identifier {
+ | <id>
+ | <pasm_instr>
}
rule conditional_expr {
@@ -251,8 +257,8 @@
| <simple_expr> [ <relational_operator> <simple_expr> ]?
}
-rule jump_stat {
- <'goto'> [ <id> | <syntax_error: label expected after 'goto'> ]
+rule goto_stat {
+ <'goto'> [ <label_identifier> | <syntax_error: label expected after
'goto'> ]
}
@@ -280,40 +286,63 @@
}
token simple_expr {
- | <float_constant>
- | <int_constant>
- | <string_constant>
+ | <constant>
| <target>
}
rule keylist {
<'['>
- <key> [ [ <','> | <';'> ] <key> ]*
+ <key> [ <separator> <key> ]*
[ <']'> | <syntax_error: ']' expected> ]
}
-rule key {
- | <simple_expr> [ <'..'> [ <simple_expr> ]? ]?
+token separator {
+ | <','>
+ | <';'>
+}
+
+rule key {
+ | <simple_expr> <'..'> <simple_expr>
+ | <simple_expr> <'..'>
+ | <simple_expr>
| <'..'> <simple_expr>
}
+#rule assignment_stat {
+# | <target> <'='> <short_sub_call>
+# | <target> <'='> <target> <keylist>
+# | <target> <'='> <expression>
+# | <target> <'='> <pasm_instruction> \N*
+# #| <target> <'='> <pasm_op_1> <simple_expr>
+# #| <target> <'='> <pasm_op_2> <simple_expr> \, <simple_expr>
+# | <target> <'='> <'new'> <string_constant>
+# | <target> <'='> <'new'> <keylist>
+# | <target> <'='> <'find_type'> [ <string_constant> | <string_reg> | <id> ]
+# | <target> <'='> <heredoc>
+# | <target> <assign_operator> <simple_expr>
+# | <target> <keylist> <'='> <simple_expr>
+# | <result_var_list>
+# [ <'='> | <syntax_error: '=' expected> ]
+# <short_sub_call>
+#}
+
+
rule assignment_stat {
- | <target> <'='> <short_sub_call>
- | <target> <'='> <target> <keylist>
- | <target> <'='> <expression>
- | <target> <'='> <pasm_instruction> \N*
- #| <target> <'='> <pasm_op_1> <simple_expr>
- #| <target> <'='> <pasm_op_2> <simple_expr> \, <simple_expr>
- | <target> <'='> <'new'> [ <int_constant> | <string_constant> | <macro_id>
]
- | <target> <'='> <'new'> <keylist>
- | <target> <'='> <'find_type'> [ <string_constant> | <string_reg> | <id> ]
- | <target> <'='> <heredoc>
+ | <target> <'='> <rhs>
| <target> <assign_operator> <simple_expr>
| <target> <keylist> <'='> <simple_expr>
- | <result_var_list>
- [ <'='> | <syntax_error: '=' expected> ]
- <short_sub_call>
+ | <result_var_list> <'='> <short_sub_call>
+}
+
+rule rhs {
+ | <short_sub_call>
+ | <target> <keylist>
+ | <expression>
+ | <pasm_instruction> \N*
+ | <heredoc>
+ | <'new'> <string_constant>
+ | <'new'> <keylist>
}
@@ -363,7 +392,10 @@
<'.pcc_begin'>
[ <?nl> | <syntax_error: newline after '.pcc_begin' expected> ]
<arguments>
- [ [ <'.pcc_call'> | <'.nci_call'> ]
+ [
+ [ <'.pcc_call'>
+ | <'.nci_call'>
+ ]
| [ <invocant> <'.meth_call'> ]
]
[ <target> | <syntax_error: id or register expected that holds the sub
object> ]
@@ -390,13 +422,13 @@
#
rule short_sub_call {
[
- [ <target>\.]? # optional invocant
- [ <target> | <string_constant> ] # method or sub name/id
+ [ <target>\.]? # optional invocant
+ [ <target> | <string_constant> ] # method or sub name/id
|
- <target> <method>
+ <target> <method>
]
<parenthesized_args> # sub args
- <process_heredocs> # process the list of heredoc
labels, if any
+ <process_heredocs> # process the list of heredoc labels, if
any
<clear_heredocs>
}
@@ -411,10 +443,12 @@
rule result_var_list {
<'('>
- <result_var>
- [
- <','> [ <result_var> | <syntax_error: target to store result expected
after ','> ]
- ]*
+ [
+ <result_var>
+ [ <','>
+ [ <result_var> | <syntax_error: target to store result expected after
','> ]
+ ]*
+ ]?
[ <')'> | <syntax_error: ')' expected to close result target list> ]
}
@@ -494,7 +528,7 @@
}
rule long_return_stat {
- <'.pcc_begin_return'> <?nl1>
+ <'.pcc_begin_return'> <?NL>
[ <'.return'> <return_value> ]*
[ <'.pcc_end_return'> | <syntax_error: '.pcc_end_return' expected> ]
}
@@ -504,7 +538,7 @@
}
rule long_yield_stat {
- <'.pcc_begin_yield'> <?nl1>
+ <'.pcc_begin_yield'> <?NL>
[ <'.yield'> <return_value> ]*
[ <'.pcc_end_yield'> | <syntax_error: '.pcc_end_yield' expected> ]
}
@@ -513,12 +547,7 @@
rule return_value {
[ <simple_expr> | <syntax_error: simple expression expected> ]
<set_flags>?
- <?nl1>
-}
-
-rule yield_directive {
- <'.yield'>
- <return_value>
+ <?NL>
}
rule short_yield_stat {
@@ -530,16 +559,20 @@
}
rule open_namespace {
- <'.namespace'> [ <id> | <syntax_error: namespace identifier expected> ]
+ <'.namespace'> <scope_id>
}
rule close_namespace {
- <'.endnamespace'> [ <id> | <syntax_error: namespace identifier expected> ]
+ <'.endnamespace'> <scope_id>
+}
+
+rule scope_id {
+ <id> | <syntax_error: namespace identifier expected>
}
rule emit {
<'.emit'>
- [ <?nl> | <syntax_error: newline expected> ]
+ <?NL>
<labeled_pasm_instr>*
[ <'.eom'> | <syntax_error: '.eom' expected> ]
}
@@ -548,7 +581,7 @@
<'.macro'>
[ <id> | <syntax_error: macro identifier expected> ]
<macro_parameters>?
- [ <?nl> | <syntax_error: newline expected after macro parameter list> ]
+ <?NL>
<macro_body>
}
@@ -580,9 +613,16 @@
}
rule pasm_constant {
- <'.constant'> <id> [ <creg> | <float_constant> | <int_constant> |
<string_constant> ]
+ <'.constant'> <id> [ <creg> | <constant> ]
}
+token constant {
+ | <float_constant>
+ | <int_constant>
+ | <string_constant>
+}
+
+
# only a PASM register is allowed for pasm_constant
#
token creg {
@@ -699,7 +739,10 @@
}
token charset_specifier {
- <'ascii:'> | <'binary:'> | <'unicode:'> | <'iso-8859-1:'>
+ | <'ascii:'>
+ | <'binary:'>
+ | <'unicode:'>
+ | <'iso-8859-1:'>
}
regex stringdouble { <PGE::Text::bracketed: "> }
@@ -718,7 +761,8 @@
}
token normal_target {
- <id> | <reg>
+ | <id>
+ | <reg>
}
=head2 Macro_target
@@ -728,7 +772,9 @@
=cut
token macro_target {
- <id> | <reg> | <macro_id>
+ | <id>
+ | <reg>
+ | <macro_id>
}
token id {
@@ -740,9 +786,13 @@
}
token reg {
- <int_reg> | <num_reg> | <pmc_reg> | <string_reg>
+ | <int_reg>
+ | <num_reg>
+ | <pmc_reg>
+ | <string_reg>
}
+
token int_reg {
| I\d<[\d]>? # match I0 to I99
| \$I\d+ # match $I0 to infinity
@@ -783,7 +833,7 @@
[ \n <?ws> ]+
}
-token nl1 {
+token NL {
<nl> | <syntax_error: newline expected>
}
@@ -826,6 +876,7 @@
token pir_directive {
| <'.arg'>
| <'.const'>
+ | <'.constant'>
| <'.emit'>
| <'.end'>
| <'.endnamespace'>