ovidiu 02/03/01 18:01:17
Modified: src/scratchpad/schecoon/src/org/apache/cocoon/flow flow.g
Log:
Updated. Added support for most Java-like statements, arrays and dictionaries.
Revision Changes Path
1.2 +105 -20
xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/flow/flow.g
Index: flow.g
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/flow/flow.g,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- flow.g 28 Feb 2002 01:51:16 -0000 1.1
+++ flow.g 2 Mar 2002 02:01:17 -0000 1.2
@@ -9,19 +9,56 @@
class FlowParser extends Parser;
options {
- k = 2;
+ k = 1;
+}
+
+tokens {
+ DOT;
}
exprs
- : SEMI exprs
- | expr
+ : (expr)? SEMI exprs
;
expr
- : assignment
- | function
+ : elem // Elementary expression
+ | function // Function declaration
+ | varDecl // Variable declaration
+ | "if" LPAREN elem RPAREN expr
+ (
+ // CONFLICT: the old "dangling-else" problem...
+ // ANTLR generates proper code matching
+ // as soon as possible. Hush warning.
+ options {
+ warnWhenFollowAmbig = false;
+ }
+ : "else" elem
+ )?
+ | "return" (elem|function)?
+ | "try" elem (handler)+
+ | "throw" elem
+
+ // The following expressions should be rewritten using macros,
+ // when they become available
+ | "while" LPAREN elem RPAREN expr
+ | "switch" LPAREN elem RPAREN casesGroup
+ | "for" LPAREN (varDecl|elemList) SEMI elemList SEMI elemList RPAREN expr
+ | "foreach" LPAREN IDENT "in" elem RPAREN expr
+ | "do" elem "while" LPAREN elem RPAREN
+ ;
+
+elem
+ : assignment // Simple expressions
+ | block // A sequence of expressions
;
+elemList
+ : elem (COMMA elem)*
+ ;
+
+// A function is either named or unnamed. A named function is //
+// equivalent to a variable declaration whose value is an unnamed
+// function.
function
: "function" (IDENT)? parameters block
;
@@ -30,12 +67,48 @@
: LPAREN (IDENT)* RPAREN
;
+// A sequence of expressions. The value of the block is the value of
+// the last expression evaluated inside the block.
block
: LCURLY exprs RCURLY
;
+// Variable declaration
+varDecl
+ : "var" assignment (COMMA assignment)*
+ ;
+
+casesGroup
+ : ( // CONFLICT: to which case group do the statements bind?
+ // ANTLR generates proper code: it groups the
+ // many "case"/"default" labels together then
+ // follows them with the statements
+ options {
+ warnWhenFollowAmbig = false;
+ }
+ :
+ aCase
+ )+
+ caseSList
+ {#casesGroup = #([CASE_GROUP, "CASE_GROUP"], #casesGroup);}
+ ;
+
+aCase
+ : ("case" elem | "default") COLON
+ ;
+
+caseSList
+ : exprs
+ ;
+
+// Handler for a try expression
+handler
+ : "catch" LPAREN IDENT IDENT RPAREN block
+ | "finally" block
+ ;
+
assignment
- : conditional
+ : logicalOr
( ( ASSIGN
| PLUS_ASSIGN
| MINUS_ASSIGN
@@ -50,11 +123,6 @@
)?
;
-conditional
- : logicalOr
- (QUESTION assignment COLON conditional)?
- ;
-
logicalOr
: logicalAnd (LOR logicalAnd)*
;
@@ -88,7 +156,6 @@
)
additive
)*
- | "instanceof" typespec
)
;
@@ -116,27 +183,46 @@
postfix
: primary
+ (
+ // Array indexing
+ LBRACK assignment RBRACK
+
+ // Method invocation
+ | DOT LPAREN elemList RPAREN
+
+ // Function invocation
+ | LPAREN elemList RPAREN
+ )*
+ ;
+
+primary
+ : IDENT
| constant
+ | arrayOrDict
| "true"
| "false"
| "null"
| LPAREN assignment RPAREN
;
-primary
- : IDENT
- ;
-
constant
: NUMBER
| CHAR_LITERAL
| STRING_LITERAL
;
-typespec
- : IDENT
+// An array or a dictionary:
+// [a, b, c] -> array
+// [a: 1, b: 2, c: 3] -> dictionary
+// Use a syntactic predicate to disambiguate on ':'
+arrayOrDict
+ : (LBRACK elem COLON)=>
+ LBRACK elem COLON elem (COMMA elem COLON elem)* RBRACK
+ | LBRACK elemList RBRACK
;
+
+
class FlowLexer extends Lexer;
options {
@@ -188,14 +274,13 @@
PANYS : ('a'..'z'|'A'..'Z'|'@'|'#'|'$'|'_'|'?');
protected
-ANY : (PANYS|PLUS|MINUS|COLON|SEMI|DIV|'0'..'9'|'.'|'"'|'\''|'='|'*'|'!');
+ANY : (PANYS|COLON|'0'..'9'|'!');
IDENT : PANYS (ANY)* PANYS;
//
// The following are stolen from java.g in the ANTLR distribution.
//
-
WS : ( ' '
| '\t'
| '\f'
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]