3 // using CommonAST = antlr.CommonAST;
\r
5 using System.Collections;
\r
6 using Castle.Rook.AST;
\r
10 language = "CSharp";
\r
11 namespace = "Castle.Rook.Parse";
\r
14 class RookLangParser extends Parser;
\r
18 k = 2; // two token lookahead
\r
19 defaultErrorHandler = true; // Don't generate parser error handlers
\r
25 CLASS_DEF = "class";
\r
26 MIXIN_DEF = "mixin";
\r
27 NAMESPACE = "namespace";
\r
28 INTERFACE = "interface";
\r
29 INIT = "initialize";
\r
45 AccessLevel currentAccessLevel = AccessLevel.Public;
\r
47 public override void reportError(RecognitionException ex)
\r
53 compilation_unit returns[CompilationUnitNode unit]
\r
55 unit = new CompilationUnitNode();
\r
58 (options { greedy=true;}: EOS!)*
\r
59 (declaration[unit])*
\r
63 declaration[AbstractDeclarationContainer container]
\r
65 mixin_declaration[container.MixinTypes]
\r
67 class_declaration[container.ClassesTypes]
\r
69 namespace_member_declaration[container.Namespaces]
\r
72 namespace_member_declaration[IList namespaces]
\r
74 namespace_declaration[namespaces]
\r
77 namespace_declaration![IList namespaces]
\r
79 NamespaceNode ns = null;
\r
80 Identifier qi = null;
\r
83 NAMESPACE qi=qualified_identifier
\r
85 ns = new NamespaceNode( qi );
\r
93 namespace_body[NamespaceNode ns]
\r
95 (declaration[ns])* END!
\r
98 class_declaration![IList types]
\r
100 ClassDefinitionStatement classNode = null;
\r
101 Identifier id = null;
\r
104 /* TODO:visibility public/private/etc */
\r
105 CLASS_DEF! id=identifier
\r
107 classNode = new ClassDefinitionStatement(id.Name);
\r
108 types.Add(classNode);
\r
110 (baseTypes[classNode])?
\r
111 class_body[classNode]
\r
114 mixin_declaration![IList mixins]
\r
116 MixinDefinitionStatement mixinNode = null;
\r
117 Identifier id = null;
\r
120 MIXIN_DEF! id=identifier
\r
122 mixinNode = new MixinDefinitionStatement(id.Name);
\r
123 mixins.Add(mixinNode);
\r
125 mixin_body[mixinNode]
\r
128 mixin_body[MixinDefinitionStatement mixinNode]
\r
133 class_body[ClassDefinitionStatement classNode]
\r
135 // Default access level for the method body
\r
136 currentAccessLevel = AccessLevel.Public;
\r
139 (access_level class_level_supported_statements[classNode.Statements])* END!
\r
143 baseTypes![TypeDefinitionStatement type]
\r
145 Identifier qi = null;
\r
148 LESSTHAN! qi=qualified_identifier
\r
150 type.BaseTypes.Add( qi );
\r
152 (COMMA! qi=qualified_identifier
\r
154 type.BaseTypes.Add( qi );
\r
161 "public" { currentAccessLevel = AccessLevel.Public; }
\r
163 "private" { currentAccessLevel = AccessLevel.Private; }
\r
165 "protected" { currentAccessLevel = AccessLevel.Protected; }
\r
167 "internal" { currentAccessLevel = AccessLevel.Internal; }
\r
169 /* nothing - inherits the access level defined previously */
\r
173 identifier! returns [Identifier ident]
\r
179 { ident = new Identifier(id.getText()); }
\r
183 qualified_identifier returns [Identifier ident]
\r
186 StringBuilder sb = new StringBuilder();
\r
187 Identifier id = null;
\r
192 sb.Append(id.Name);
\r
194 ( options { greedy=true; } : DOT! id=identifier
\r
197 sb.Append(id.Name);
\r
201 ident = new Identifier( sb.ToString() );
\r
206 method_name returns [String[] parts]
\r
208 parts = new String[2];
\r
209 Identifier id = null;
\r
215 parts[0] = id.Name;
\r
223 (DOT! id=identifier
\r
225 parts[1] = id.Name;
\r
231 type_name returns [Identifier qi]
\r
236 (COLON qi=qualified_identifier)?
\r
243 block_statement returns [BlockStatement block]
\r
245 block = new BlockStatement();
\r
249 statement_list[block.Statements]
\r
253 method_def_stmt returns [MethodDefinitionStatement method]
\r
255 String[] nameParts;
\r
256 Identifier retType = null;
\r
260 DEF! nameParts=method_name
\r
262 method = new MethodDefinitionStatement(currentAccessLevel, nameParts);
\r
264 formal_param_list[method]
\r
267 method.ReturnType = retType;
\r
269 method_body[method]
\r
274 formal_param_list[MethodDefinitionStatement method]
\r
276 LPAREN! ( method_param[method] (COMMA! method_param[method])* )? RPAREN!
\r
280 method_param[MethodDefinitionStatement method]
\r
282 Identifier id = null;
\r
283 Identifier qi = null;
\r
286 (REF|OUT)? id=identifier qi=type_name
\r
288 method.Parameters.Add( new MethodParameterNode(id.Name, qi) );
\r
293 method_body[MethodDefinitionStatement method]
\r
295 statement_list[method.Statements]
\r
300 statement returns [Statement stmt]
\r
305 stmt=method_def_stmt
\r
307 stmt=expression_statement
\r
309 stmt=block_statement
\r
311 stmt=simple_field_decl_stmt
\r
315 statement_list[IList statements]
\r
317 Statement stmt = null;
\r
320 (stmt=statement { statements.Add( stmt ); } )*
\r
324 class_level_supported_statements[IList statements]
\r
326 Statement stmt = null;
\r
330 stmt=field_decl_stmt
\r
332 stmt=method_def_stmt
\r
335 statements.Add( stmt );
\r
340 var_reference returns [IdentifierReferenceExpression exp]
\r
342 Identifier qi = null; exp = null;
\r
345 id:STATIC_IDENTIFIER { exp = new StaticFieldReferenceExpression(id.getText()); }
\r
347 id2:INSTANCE_IDENTIFIER { exp = new InstanceFieldReferenceExpression(id2.getText()); }
\r
349 qi=qualified_identifier { exp = new IdentifierReferenceExpression(qi); }
\r
353 simple_field_decl_stmt returns [FieldDeclarationStatement stmt]
\r
355 stmt = null; IdentifierReferenceExpression var;
\r
356 Expression initializer = null; Identifier tn = null;
\r
359 var=var_reference tn=type_name (ASSIGN initializer=expression)?
\r
361 stmt = new FieldDeclarationStatement(currentAccessLevel, var, tn, initializer);
\r
366 field_decl_stmt returns [FieldDeclarationStatement stmt]
\r
368 stmt = null; IdentifierReferenceExpression var;
\r
369 Expression initializer = null; Identifier tn = null;
\r
372 var=var_reference tn=type_name (ASSIGN initializer=expression)?
\r
374 stmt = new FieldDeclarationStatement(currentAccessLevel, var, tn, initializer);
\r
380 assign_stmt returns [AssignmentStatement stmt]
\r
382 stmt = null; IdentifierReferenceExpression target; Expression value;
\r
385 target=var_reference ASSIGN value=expression
\r
387 stmt = new AssignmentStatement(currentAccessLevel, target, value);
\r
393 expression_statement returns [ExpressionStatement stmt]
\r
396 Expression exp = null;
\r
400 (assign_exp) => exp=assign_exp
\r
405 stmt = new ExpressionStatement(exp);
\r
414 assign_exp returns [AssignmentExpression ue]
\r
416 Expression target, value = null; ue = null;
\r
419 target=unary_exp ASSIGN value=expression
\r
421 ue = new AssignmentExpression(target, value);
\r
426 unary_exp returns [Expression exp]
\r
433 expression returns [Expression exp]
\r
439 literal_exp returns [LiteralExpression le]
\r
444 le = new LiteralIntegerExpression( t.getText() );
\r
449 symbol_exp returns [SymbolExpression se]
\r
454 se = new SymbolExpression( t.getText() );
\r
460 method_invoke_exp[Expression target] returns [MethodInvokeExpression mie]
\r
462 mie = new MethodInvokeExpression(target);
\r
466 LPAREN ( exp=expression { mie.Arguments.Add( exp ); }
\r
467 (COMMA! exp=expression { mie.Arguments.Add( exp ); } )* )? RPAREN
\r
468 ( options {greedy=true;} : SEMI)?
\r
472 primary_start returns [Expression exp]
\r
483 SELF { exp = SelfReferenceExpression.Instance; }
\r
485 BASE { exp = BaseReferenceExpression.Instance; }
\r
489 primary_exp returns [Expression exp]
\r
491 Expression ps = null; exp = null;
\r
496 ( options {greedy=true;}:
\r
497 exp=postfix_exp[ps]
\r
499 exp=method_invoke_exp[ps]
\r
501 exp=member_access[ps]
\r
505 if (exp == null) exp = ps;
\r
510 member_access[Expression target] returns [MemberAccessExpression mae]
\r
518 mae = new MemberAccessExpression(target, id);
\r
524 postfix_exp[Expression target] returns [PostFixExpression pfe]
\r
529 INC { pfe = new PostFixExpression( target, 1 ); }
\r
531 DEC { pfe = new PostFixExpression( target, 2 ); }
\r
538 class RookLexer extends Lexer;
\r
542 k=4; // four characters of lookahead
\r
543 charVocabulary='\u0003'..'\u7FFF'; // to avoid hanging eof on comments (eof = -1)
\r
545 testLiterals=false;
\r
563 // ***** A.1.1 LINE TERMINATORS *****
\r
566 : ( // carriage return character followed by possible line feed character
\r
567 { LA(2)=='\u000A' }? '\u000D' '\u000A'
\r
568 | '\u000D' // line feed character
\r
569 | '\u000A' // line feed character
\r
570 | '\u2028' // line separator character
\r
571 | '\u2029' // paragraph separator character
\r
578 : ('\u000D' | '\u000A' | '\u2028' | '\u2029')
\r
583 : ~( '\u000D' | '\u000A' | '\u2028' | '\u2029')
\r
586 // ***** A.1.2 WHITESPACE *****
\r
589 | '\u0009' // horizontal tab character
\r
590 | '\u000B' // vertical tab character
\r
591 | '\u000C' // form feed character
\r
594 { _ttype = Token.SKIP; }
\r
598 // ***** A.1.3 COMMENTS *****
\r
599 SINGLE_LINE_COMMENT
\r
602 (NEW_LINE)? // may be eof
\r
603 {_ttype = Token.SKIP;}
\r
607 // ***** A.1.6 IDENTIFIERS *****
\r
612 testLiterals=true;
\r
613 paraphrase = "an identifier";
\r
616 IDENTIFIER_START_CHARACTER (IDENTIFIER_PART_CHARACTER)*
\r
623 testLiterals=false;
\r
624 paraphrase = "a symbol";
\r
632 IDENTIFIER_START_CHARACTER
\r
633 : ('a'..'z'|'A'..'Z'|'_'|'$')
\r
637 IDENTIFIER_PART_CHARACTER
\r
638 : ('a'..'z'|'A'..'Z'|'_'|'0'..'9')
\r
644 testLiterals=false;
\r
645 paraphrase = "an static variable name";
\r
648 "@@" IDENTIFIER_START_CHARACTER (IDENTIFIER_PART_CHARACTER)*
\r
651 INSTANCE_IDENTIFIER
\r
654 testLiterals=false;
\r
655 paraphrase = "an instance variable name";
\r
658 "@" IDENTIFIER_START_CHARACTER (IDENTIFIER_PART_CHARACTER)*
\r
666 $setType(INTEGER_LITERAL);
\r