/* * xtc - The eXTensible Compiler * Copyright (C) 2004-2005 Robert Grimm * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** * Java grammar. * * @author Robert Grimm * @version $Revision: 1.5 $ */ module xtc.lang.Java; import xtc.lang.JavaIdentifier(xtc.lang.JavaSymbol, xtc.util.Spacing); instantiate xtc.util.Symbol(xtc.util.Spacing); import xtc.lang.JavaSymbol(xtc.util.Symbol); import xtc.util.Spacing; option withLocation, constant, parser(xtc.lang.JavaPackratParser); // ---------------------------------------------------------- Compilation Unit public transient void CompilationUnit = Spacing ( "package":Word QualifiedIdentifier ";":Symbol )? ImportDeclaration* Declaration* '\u001a'? EndOfFile ; transient void ImportDeclaration = "import":Word QualifiedIdentifier ( ".":Symbol "*":Symbol )? ";":Symbol ; // ---------------------------------------------------------- Declarations void Modifier = "public":Word / "protected":Word / "private":Word / "static":Word / "abstract":Word / "final":Word / "native":Word / "synchronized":Word / "transient":Word / "volatile":Word / "strictfp":Word ; transient void FormalParameter = ("final":Word)? Type Identifier Dimensions ; void FormalParameters = "(":Symbol FormalParameter ( ",":Symbol FormalParameter )* ")":Symbol / "(":Symbol ")":Symbol ; transient void Declarator = Identifier Dimensions ( "=":Symbol VariableInitializer )? ; void Declarators = Declarator ( ",":Symbol Declarator )* ; transient void ClassBody = "{":Symbol Declaration* "}":Symbol ; transient void Declaration = // Field declaration. Modifier* Type Declarators ";":Symbol // Method declaration. / Modifier* ResultType Identifier FormalParameters Dimensions OptionalThrowsClause ( Block / ";":Symbol ) // Constructor declaration. / Modifier* Identifier FormalParameters OptionalThrowsClause Block // Class declaration. / Modifier* "class":Word Identifier ( "extends":Word Type )? ( "implements":Word Type ( ",":Symbol Type )* )? ClassBody // Interface declaration. / Modifier* "interface":Word Identifier ( "extends":Word Type ( ",":Symbol Type )* )? ClassBody // Initialization block. / ( "static":Word / /* Empty */ ) Block ; void OptionalThrowsClause = "throws":Word QualifiedIdentifier ( ",":Symbol QualifiedIdentifier )* / /* Empty */ ; // ---------------------------------------------------------- Statements void Block = "{":Symbol BlockStatement* "}":Symbol ; transient void BlockStatement = Declaration / Statement ; transient void Statement = Block / "if":Word ParExpression Statement ( "else":Word Statement )? / "for":Word "(":Symbol ForInit ";":Symbol Expression? ";":Symbol ExpressionList? ")":Symbol Statement / "while":Word ParExpression Statement / "do":Word Statement "while":Word ParExpression ";":Symbol / "try":Word Block CatchClause* "finally":Word Block / "try":Word Block CatchClause+ / "switch":Word ParExpression "{":Symbol SwitchGroup* "}":Symbol / "synchronized":Word ParExpression Block / "return":Word Expression? ";":Symbol / "throw":Word Expression ";":Symbol / "break":Word Identifier? ";":Symbol / "continue":Word Identifier? ";":Symbol / Identifier ":":Symbol Statement / Expression ";":Symbol / ";":Symbol ; transient void ParExpression = "(":Symbol Expression ")":Symbol ; void ForInit = ("final":Word)? Type Declarators / ExpressionList / /* Empty */ ; void CatchClause = "catch":Word "(":Symbol FormalParameter ")":Symbol Block ; void SwitchGroup = "case":Word Expression ":":Symbol BlockStatement* / "default":Word ":":Symbol BlockStatement* ; // ---------------------------------------------------------- Expressions transient void ExpressionList = Expression ( ",":Symbol Expression )* ; void Expression = ConditionalExpression AssignmentOperator Expression / ConditionalExpression ; void AssignmentOperator = "=":Symbol / "+=":Symbol / "-=":Symbol / "*=":Symbol / "/=":Symbol / "&=":Symbol / "|=":Symbol / "^=":Symbol / "%=":Symbol / "<<=":Symbol / ">>=":Symbol / ">>>=":Symbol ; void ConditionalExpression = ConditionalOrExpression "?":Symbol Expression ":":Symbol ConditionalExpression / ConditionalOrExpression ; void ConditionalOrExpression = ConditionalOrExpression "||":Symbol ConditionalAndExpression / ConditionalAndExpression ; transient void ConditionalAndExpression = ConditionalAndExpression "&&":Symbol InclusiveOrExpression / InclusiveOrExpression ; transient void InclusiveOrExpression = InclusiveOrExpression "|":Symbol ExclusiveOrExpression / ExclusiveOrExpression ; transient void ExclusiveOrExpression = ExclusiveOrExpression "^":Symbol AndExpression / AndExpression ; transient void AndExpression = AndExpression "&":Symbol EqualityExpression / EqualityExpression ; transient void EqualityExpression = EqualityExpression "==":Symbol InstanceOfExpression / EqualityExpression "!=":Symbol InstanceOfExpression / InstanceOfExpression ; transient void InstanceOfExpression = RelationalExpression "instanceof":Word Type / RelationalExpression ; void RelationalExpression = RelationalExpression "<":Symbol ShiftExpression / RelationalExpression ">":Symbol ShiftExpression / RelationalExpression "<=":Symbol ShiftExpression / RelationalExpression ">=":Symbol ShiftExpression / ShiftExpression ; transient void ShiftExpression = ShiftExpression "<<":Symbol AdditiveExpression / ShiftExpression ">>":Symbol AdditiveExpression / ShiftExpression ">>>":Symbol AdditiveExpression / AdditiveExpression ; transient void AdditiveExpression = AdditiveExpression "+":Symbol MultiplicativeExpression / AdditiveExpression "-":Symbol MultiplicativeExpression / MultiplicativeExpression ; transient void MultiplicativeExpression = MultiplicativeExpression "*":Symbol UnaryExpression / MultiplicativeExpression "/":Symbol UnaryExpression / MultiplicativeExpression "%":Symbol UnaryExpression / UnaryExpression ; transient void UnaryExpression = "+":Symbol UnaryExpression / "-":Symbol UnaryExpression / "++":Symbol UnaryExpression / "--":Symbol UnaryExpression / UnaryExpressionNotPlusMinus ; transient void UnaryExpressionNotPlusMinus = "~":Symbol UnaryExpression / "!":Symbol UnaryExpression / "(":Symbol BasicType Dimensions ")":Symbol UnaryExpression / "(":Symbol Type ")":Symbol UnaryExpressionNotPlusMinus / PostfixExpression ; void PostfixExpression = PrimaryExpression PostfixExpressionTail* ; void PostfixExpressionTail = "[":Symbol Expression? "]":Symbol / Arguments / ".":Symbol PrimaryExpression / ".":Symbol "class":Word / "++":Symbol / "--":Symbol ; transient void PrimaryExpression = Literal / Identifier / "(":Symbol Expression ")":Symbol / "this":Word / "super":Word / "new":Word QualifiedIdentifier Arguments ClassBody? / "new":Word TypeName DimensionExpression+ Dimensions / "new":Word TypeName Dimensions ArrayInitializer / "void":Word ".":Symbol "class":Word ; transient void DimensionExpression = "[":Symbol Expression "]":Symbol ; transient void Arguments = "(":Symbol ExpressionList ")":Symbol / "(":Symbol ")":Symbol ; transient void ArrayInitializer = "{":Symbol ( VariableInitializer ",":Symbol )* "}":Symbol / "{":Symbol VariableInitializer ( ",":Symbol VariableInitializer )* "}":Symbol ; void VariableInitializer = ArrayInitializer / Expression ; // ---------------------------------------------------------- Types void ResultType = "void":Word / Type ; void Type = TypeName Dimensions ; void TypeName = BasicType / QualifiedIdentifier ; void BasicType = "byte":Word / "short":Word / "char":Word / "int":Word / "long":Word / "float":Word / "double":Word / "boolean":Word ; void Dimensions = ( "[":Symbol "]":Symbol )* ; // ---------------------------------------------------------- Literals String Literal = FloatingPointLiteral Spacing / IntegerLiteral Spacing / CharacterLiteral Spacing / StringLiteral Spacing / BooleanLiteral / NullLiteral ; // ----- Integer literals String IntegerLiteral = HexNumeral IntegerTypeSuffix? / OctalNumeral IntegerTypeSuffix? / DecimalNumeral IntegerTypeSuffix? ; transient String DecimalNumeral = '0' / NonZeroDigit Digit* ; transient String NonZeroDigit = [1-9] ; transient String Digit = [0-9] ; transient String HexNumeral = '0' [xX] HexDigit+ ; transient String HexDigit = [0-9a-fA-F] ; transient String OctalNumeral = '0' OctalDigit+ ; transient String OctalDigit = [0-7] ; transient String IntegerTypeSuffix = [lL] ; // ----- Floating point literals String FloatingPointLiteral = Digit+ '.' Digit* Exponent? FloatTypeSuffix? / '.' Digit+ Exponent? FloatTypeSuffix? / Digit+ Exponent FloatTypeSuffix? / Digit+ Exponent? FloatTypeSuffix ; transient String Exponent = [eE] [+\-]? Digit+ ; transient String FloatTypeSuffix = [fFdD] ; // ----- Character and string literals String CharacterLiteral = ['] ( EscapeSequence / !['\\] _ ) ['] ; String StringLiteral = ["] ( EscapeSequence / !["\\] _ )* ["] ; transient String EscapeSequence = '\\' [btnfr"'\\] / UnicodeEscape / OctalEscape ; transient String OctalEscape = '\\' [0-3] OctalDigit OctalDigit / '\\' OctalDigit OctalDigit / '\\' OctalDigit ; transient String UnicodeEscape = '\\' 'u' HexDigit HexDigit HexDigit HexDigit ; // ----- Boolean literals String BooleanLiteral = "true":Word / "false":Word ; // ----- Null literals String NullLiteral = "null":Word ; // ============================================================================