Home | History | Annotate | Download | only in Antlr3.Runtime.Test
      1 /*
      2  * [The "BSD licence"]
      3  * Copyright (c) 2011 Terence Parr
      4  * All rights reserved.
      5  *
      6  * Conversion to C#:
      7  * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 grammar TestExpressionFeatures;
     34 
     35 options {
     36     language=CSharp3;
     37     TokenLabelType=CommonToken;
     38     output=AST;
     39     ASTLabelType=CommonTree;
     40 }
     41 
     42 tokens {
     43 	LPAREN = '(';
     44 	RPAREN = ')';
     45 	DOT = '.';
     46 	LBRACK = '[';
     47 	RBRACK = ']';
     48 	INC = '++';
     49 	DEC = '--';
     50 	PLUS = '+';
     51 	MINUS = '-';
     52 	TILDE = '~';
     53 	NOT = '!';
     54 	//LSHIFT = '<<';
     55 	//RSHIFT = '>>';
     56 	//RUSHIFT = '>>>';
     57 	EQUALS = '==';
     58 	NOTEQ = '!=';
     59 	MOD = '%';
     60 	CARET = '^';
     61 	GT = '>';
     62 	LT = '<';
     63 	GE = '>=';
     64 	LE = '<=';
     65 	BITAND = '&';
     66 	BITOR = '|';
     67 	AND = '&&';
     68 	OR = '||';
     69 
     70 	KW_THIS = 'this';
     71 	KW_SUPER = 'super';
     72 	KW_CLASS = 'class';
     73 	KW_NEW = 'new';
     74 	KW_INSTANCEOF = 'instanceof';
     75 	KW_INT = 'int';
     76 }
     77 
     78 @lexer::namespace{Antlr3.Runtime.Test}
     79 @parser::namespace{Antlr3.Runtime.Test}
     80 
     81 @parser::header{using Console = System.Console;}
     82 
     83 /*
     84  * Parser Rules
     85  */
     86 
     87 public
     88 compileUnit
     89     :   e EOF
     90     ;
     91 
     92 e   :   '('! e ')'!
     93     |   'this'
     94     |   'super'
     95     |   INT
     96     |   ID
     97     |   type '.'^ 'class'
     98     |   e '.'^ ID
     99     |   e '.'^ 'this'
    100     |   e '.'^ 'super' '('^ expressionList? ')'!
    101     |   e '.'^ 'new'^ ID '('! expressionList? ')'!
    102     |	'new'^ type ( '(' expressionList? ')'! | (options {k=1;}:'[' e ']'!)+)
    103     |   e '['^ e ']'!
    104     |   '('^ type ')'! e
    105     |   e ('++'^ | '--'^)
    106     |   e '('^ expressionList? ')'!
    107     |   ('+'^|'-'^|'++'^|'--'^) e
    108     |   ('~'^|'!'^) e
    109     |   e ('*'^|'/'^|'%'^) e
    110     |   e ('+'^|'-'^) e
    111     |   e ('<' '<' | '>' '>' '>' | '>' '>') e
    112     |   e ('<='^ | '>='^ | '>'^ | '<'^) e
    113     |   e 'instanceof'^ e
    114     |   e ('=='^ | '!='^) e
    115     |   e '&'^ e
    116     |   e '^'<assoc=right>^ e
    117     |   e '|'^ e
    118     |   e '&&'^ e
    119     |   e '||'^ e
    120 //    |   e '?' e ':' e
    121     |   e ('='<assoc=right>^
    122           |'+='<assoc=right>^
    123           |'-='<assoc=right>^
    124           |'*='<assoc=right>^
    125           |'/='<assoc=right>^
    126           |'&='<assoc=right>^
    127           |'|='<assoc=right>^
    128           |'^='<assoc=right>^
    129           |'>>='<assoc=right>^
    130           |'>>>='<assoc=right>^
    131           |'<<='<assoc=right>^
    132           |'<<<='<assoc=right>^
    133           |'%='<assoc=right>^) e
    134     ;
    135 
    136 expressionList
    137 	:	e (','! e)*
    138 	;
    139 
    140 type: ID
    141 	| ID '['^ ']'!
    142 	| 'int'
    143 	| 'int' '['^ ']'!
    144 	;
    145 
    146 /*
    147  * Lexer Rules
    148  */
    149 
    150 WS
    151     :   (' ' | '\t' | '\n' | '\r') {Skip();}
    152     ;
    153 
    154 ID
    155 	:	('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '0'..'9' | '_')*
    156 	;
    157 
    158 INT
    159 	:	'0'..'9'+
    160 	;
    161