Anglr Syntax Rules for Java Programming Language
Introduction
This pragraph introduces Anglr syntax rules for Java programming language (early working copy). They are copied from this website and customized for Anglr.
Syntax
[ CompilationInfo ClassName='JavaDeclarations' NameSpace='Java.Declarations' Access='public' Hover='true' ] %declarations javaDecls %{ %terminal { TypeIdentifier Identifier Literal UnqualifiedMethodIdentifier VariableAccess } %terminal { abstract assert boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new non-sealed open opens package permits private protected provides public record requires return sealed short static strictfp super switch synchronized this throw throws to transient transitive try uses var void volatile while with yield } %terminal { not-op '!' not-eq-op '!=' mod-op '%' mod-eq-op '%=' log-and-op '&&' and-op '&' and-eq-op '&=' left-br '(' right-br ')' mul-op '*' mul-eq-op '*=' add-op '+' inc-op '++' add-eq-op '+=' coma ',' sub-op '-' dec-op '--' seb-eq-op '-=' lambda-op '->' punctuation '.' elipses '...' div-op '/' div-eq-op '/=' colon ':' dcolon '::' semicolon ';' less-op '<' lsh-op '<<' lsh-eq-op '<<=' less-eq-op '<=' neq-op '<>' assign-op '=' equ-op '==' greater-op '>' greater-eq-op '>=' rsh-op '>>' rsh-eq-op '>>=' rrsh-op '>>>' rrsh-eq-op '>>>=' if-op '?' at-sign '@' left-sq-br '[' right-sq-br ']' excl-op '^' excl-eq-op '^=' left-cur-br '{' or-op '|' or-eq-op '|=' log-or-op '||' right-cur-br '}' conv-op '~' } %regex { decimal-digit 0-9 lower-case-char a-z upper-case-char A-Z alpha-char {lower-case-char}{upper-case-char} alpha-num-char {alpha-char}{decimal-digit} hyphen \- underscore _ identifier [{alpha-char}{underscore}][{alpha-num-char}{underscore}{hyphen}]* } %} [ Declarations Id='javaDecls' Hover='true' ] [ CompilationInfo ClassName='CommentScanner' NameSpace='Java.Scanner' Access='public' Hover='true' ] %scanner commentScanner %{ [\*]+\/ pop [\n\r] skip [^\*]+ skip [\*]+ skip %} [ Declarations Id='javaDecls' Hover='true' ] [ CompilationInfo ClassName='JavaScanner' NameSpace='Java.Scanner' Access='public' Hover='true' ] %scanner javaScanner %{ \/\* push commentScanner {identifier} event identifier %} [ UseScanner ScannerId='commentScanner' InitialScanner='javaScanner' Hover='true' ] [ CompilationInfo ClassName='JavaLexer' NameSpace='Java.Lexer' Access='public' Hover='true' ] %lexer javaLexer %{ %} [ Lexer Id='javaLexer' Hover='true' ] [ Declarations Id='javaDecls' Hover='true' ] [ CompilationInfo ClassName='JavaParser' NameSpace='Java.Parser' Access='public' Hover='true' ] %parser javaParser %{ PrimitiveType : Annotation * NumericType | Annotation * boolean ; NumericType : IntegralType | FloatingPointType ; IntegralType : byte | short | int | long | char ; FloatingPointType : float | double ; ReferenceType : ClassOrInterfaceType | TypeVariable | ArrayType ; ClassOrInterfaceType : ClassType | InterfaceType ; ClassType : Annotation * TypeIdentifier TypeArguments ? | PackageName '.' Annotation * TypeIdentifier TypeArguments ? | ClassOrInterfaceType '.' Annotation * TypeIdentifier TypeArguments ? ; InterfaceType : ClassType ; TypeVariable : Annotation * TypeIdentifier ; ArrayType : PrimitiveType Dims | ClassOrInterfaceType Dims | TypeVariable Dims ; Dims : (Annotation * '[' ']') + ; TypeParameter : TypeParameterModifier * TypeIdentifier TypeBound ? ; TypeParameterModifier : Annotation ; TypeBound : extends TypeVariable | extends ClassOrInterfaceType AdditionalBound * ; AdditionalBound : '&' InterfaceType ; TypeArguments : '<' TypeArgumentList '>' ; TypeArgumentList : TypeArgument + [ ',' ] ; TypeArgument : ReferenceType | Wildcard ; Wildcard : Annotation * '?' WildcardBounds ? ; WildcardBounds : extends ReferenceType | super ReferenceType ; ModuleName : Identifier + [ '.' ] ; PackageName : Identifier + [ '.' ] ; TypeName : ( PackageOrTypeName '.' ) ? TypeIdentifier ; ExpressionName : AmbiguousName ; MethodName : UnqualifiedMethodIdentifier ; PackageOrTypeName : Identifier + [ '.' ] ; AmbiguousName : Identifier + [ '.' ] ; [ Start ] CompilationUnit : OrdinaryCompilationUnit | ModularCompilationUnit ; OrdinaryCompilationUnit : PackageDeclaration ? ImportDeclaration * TopLevelClassOrInterfaceDeclaration * ; ModularCompilationUnit : ImportDeclaration * ModuleDeclaration ; PackageDeclaration : PackageModifier * package Identifier + [ '.' ] ';' ; PackageModifier : Annotation ; ImportDeclaration : SingleTypeImportDeclaration | TypeImportOnDemandDeclaration | SingleStaticImportDeclaration | StaticImportOnDemandDeclaration ; SingleTypeImportDeclaration : import TypeName ';' ; TypeImportOnDemandDeclaration : import PackageOrTypeName '.' '*' ';' ; SingleStaticImportDeclaration : import static TypeName '.' Identifier ';' ; StaticImportOnDemandDeclaration : import static TypeName '.' '*' ';' ; TopLevelClassOrInterfaceDeclaration : ClassDeclaration | InterfaceDeclaration | ';' ; ModuleDeclaration : Annotation * open ? module Identifier + [ '.' ] '{' ModuleDirective * '}' ; ModuleDirective : requires RequiresModifier * ModuleName ';' | exports PackageName (to ModuleName + [ ',' ] ) ? ';' | opens PackageName (to ModuleName + [ ',' ] ) ? ';' | uses TypeName ';' | provides TypeName with TypeName + [ ',' ] ';' ; RequiresModifier : transitive | static ; ClassDeclaration : NormalClassDeclaration | EnumDeclaration | RecordDeclaration ; NormalClassDeclaration : ClassModifier * class TypeIdentifier TypeParameters ? ClassExtends ? ClassImplements ? ClassPermits ? ClassBody ; ClassModifier : Annotation | public | protected | private | abstract | static | final | sealed | non-sealed | strictfp ; TypeParameters : '<' TypeParameterList '>' ; TypeParameterList : TypeParameter + [ ',' ] ; ClassExtends : extends ClassType ; ClassImplements : implements InterfaceTypeList ; InterfaceTypeList : InterfaceType + [ ',' ] ; ClassPermits : permits TypeName + [ ',' ] ; ClassBody : '{' ClassBodyDeclaration * '}' ; ClassBodyDeclaration : ClassMemberDeclaration | InstanceInitializer | StaticInitializer | ConstructorDeclaration ; ClassMemberDeclaration : FieldDeclaration | MethodDeclaration | ClassDeclaration | InterfaceDeclaration | ';' ; FieldDeclaration : FieldModifier * UnannType VariableDeclaratorList ';' ; FieldModifier : Annotation | public | protected | private | static | final | transient | volatile ; VariableDeclaratorList : VariableDeclarator + [ ',' ] ; VariableDeclarator : VariableDeclaratorId ( '=' VariableInitializer ) ? ; VariableDeclaratorId : Identifier Dims ? ; VariableInitializer : Expression | ArrayInitializer ; UnannType : UnannPrimitiveType | UnannReferenceType ; UnannPrimitiveType : NumericType | boolean ; UnannReferenceType : UnannClassOrInterfaceType | UnannTypeVariable | UnannArrayType ; UnannClassOrInterfaceType : UnannClassType | UnannInterfaceType ; UnannClassType : TypeIdentifier TypeArguments ? | PackageName '.' Annotation * TypeIdentifier TypeArguments ? | UnannClassOrInterfaceType '.' Annotation * TypeIdentifier TypeArguments ? ; UnannInterfaceType : UnannClassType ; UnannTypeVariable : TypeIdentifier ; UnannArrayType : UnannPrimitiveType Dims | UnannClassOrInterfaceType Dims | UnannTypeVariable Dims ; MethodDeclaration : MethodModifier * MethodHeader MethodBody ; MethodModifier : Annotation | public | protected | private | abstract | static | final | synchronized | native | strictfp ; MethodHeader : ( TypeParameters Annotation * ) ? Result MethodDeclarator Throws ? ; Result : UnannType | void ; MethodDeclarator : Identifier '(' ( ReceiverParameter ',' ) ? FormalParameterList ? ')' Dims ? ; ReceiverParameter : Annotation * UnannType ( Identifier '.' ) ? this ; FormalParameterList : FormalParameter + [ ',' ] ; FormalParameter : VariableModifier * UnannType VariableDeclaratorId | VariableArityParameter ; VariableArityParameter : VariableModifier * UnannType Annotation * '...' Identifier ; VariableModifier : Annotation | final ; Throws : throws ExceptionTypeList ; ExceptionTypeList : ExceptionType + [ ',' ] ; ExceptionType : ClassType | TypeVariable ; MethodBody : Block | ';' ; InstanceInitializer : Block ; StaticInitializer : static Block ; ConstructorDeclaration : ConstructorModifier * ConstructorDeclarator Throws ? ConstructorBody ; ConstructorModifier : Annotation | public | protected | private ; ConstructorDeclarator : TypeParameters ? SimpleTypeName '(' ( ReceiverParameter ',' ) ? FormalParameterList ? ')' ; SimpleTypeName : TypeIdentifier ; ConstructorBody : '{' ExplicitConstructorInvocation ? BlockStatements ? '}' ; ExplicitConstructorInvocation : TypeArguments ? this '(' ArgumentList ? ')' ';' | TypeArguments ? super '(' ArgumentList ? ')' ';' | ExpressionName '.' TypeArguments ? super '(' ArgumentList ? ')' ';' | Primary '.' TypeArguments ? super '(' ArgumentList ? ')' ';' ; EnumDeclaration : ClassModifier * enum TypeIdentifier ClassImplements ? EnumBody ; EnumBody : '{' EnumConstantList ? ',' ? EnumBodyDeclarations ? '}' ; EnumConstantList : EnumConstant + [ ',' ] ; EnumConstant : EnumConstantModifier * Identifier ( '(' ArgumentList ? ')' ) ? ClassBody ? ; EnumConstantModifier : Annotation ; EnumBodyDeclarations : ';' ClassBodyDeclaration * ; RecordDeclaration : ClassModifier * record TypeIdentifier TypeParameters ? RecordHeader ClassImplements ? RecordBody ; RecordHeader : '(' RecordComponentList ? ')' ; RecordComponentList : RecordComponent + [ ',' ] ; RecordComponent : RecordComponentModifier * UnannType Identifier | VariableArityRecordComponent ; VariableArityRecordComponent : RecordComponentModifier * UnannType Annotation * '...' Identifier ; RecordComponentModifier : Annotation ; RecordBody : '{' RecordBodyDeclaration * '}' ; RecordBodyDeclaration : ClassBodyDeclaration | CompactConstructorDeclaration ; CompactConstructorDeclaration : ConstructorModifier * SimpleTypeName ConstructorBody ; InterfaceDeclaration : NormalInterfaceDeclaration | AnnotationInterfaceDeclaration ; NormalInterfaceDeclaration : InterfaceModifier * interface TypeIdentifier TypeParameters ? InterfaceExtends ? InterfacePermits ? InterfaceBody ; InterfaceModifier : Annotation | public | protected | private | abstract | static | sealed | non-sealed | strictfp ; InterfaceExtends : extends InterfaceTypeList ; InterfacePermits : permits TypeName + [ ',' ] ; InterfaceBody : '{' InterfaceMemberDeclaration * '}' ; InterfaceMemberDeclaration : ConstantDeclaration | InterfaceMethodDeclaration | ClassDeclaration | InterfaceDeclaration | ';' ; ConstantDeclaration : ConstantModifier * UnannType VariableDeclaratorList ';' ; ConstantModifier : Annotation | public | static | final ; InterfaceMethodDeclaration : InterfaceMethodModifier * MethodHeader MethodBody ; InterfaceMethodModifier : Annotation | public | private | abstract | default | static | strictfp ; AnnotationInterfaceDeclaration : InterfaceModifier * '@' interface TypeIdentifier AnnotationInterfaceBody ; AnnotationInterfaceBody : '{' AnnotationInterfaceMemberDeclaration * '}' ; AnnotationInterfaceMemberDeclaration : AnnotationInterfaceElementDeclaration | ConstantDeclaration | ClassDeclaration | InterfaceDeclaration | ';' ; AnnotationInterfaceElementDeclaration : AnnotationInterfaceElementModifier * UnannType Identifier '(' ')' Dims ? DefaultValue ? ';' ; AnnotationInterfaceElementModifier : Annotation | public | abstract ; DefaultValue : default ElementValue ; Annotation : NormalAnnotation | MarkerAnnotation | SingleElementAnnotation ; NormalAnnotation : '@' TypeName '(' ElementValuePairList ? ')' ; ElementValuePairList : ElementValuePair + [ ',' ] ; ElementValuePair : Identifier '=' ElementValue ; ElementValue : ConditionalExpression | ElementValueArrayInitializer | Annotation ; ElementValueArrayInitializer : '{' ElementValueList ? ',' ? '}' ; ElementValueList : ElementValue + [ ',' ] ; MarkerAnnotation : '@' TypeName ; SingleElementAnnotation : '@' TypeName '(' ElementValue ')' ; ArrayInitializer : '{' VariableInitializerList ? ',' ? '}' ; VariableInitializerList : VariableInitializer + [ ',' ] ; Block : '{' BlockStatements ? '}' ; BlockStatements : BlockStatement + ; BlockStatement : LocalClassOrInterfaceDeclaration | LocalVariableDeclarationStatement | Statement ; LocalClassOrInterfaceDeclaration : ClassDeclaration | NormalInterfaceDeclaration ; LocalVariableDeclarationStatement : LocalVariableDeclaration ';' ; LocalVariableDeclaration : VariableModifier * LocalVariableType VariableDeclaratorList ; LocalVariableType : UnannType | var ; Statement : StatementWithoutTrailingSubstatement | LabeledStatement | IfThenStatement | IfThenElseStatement | WhileStatement | ForStatement ; StatementNoShortIf : StatementWithoutTrailingSubstatement | LabeledStatementNoShortIf | IfThenElseStatementNoShortIf | WhileStatementNoShortIf | ForStatementNoShortIf ; StatementWithoutTrailingSubstatement : Block | EmptyStatement | ExpressionStatement | AssertStatement | SwitchStatement | DoStatement | BreakStatement | ContinueStatement | ReturnStatement | SynchronizedStatement | ThrowStatement | TryStatement | YieldStatement ; EmptyStatement : ';' ; LabeledStatement : Identifier ':' Statement ; LabeledStatementNoShortIf : Identifier ':' StatementNoShortIf ; ExpressionStatement : StatementExpression ';' ; StatementExpression : Assignment | PreIncrementExpression | PreDecrementExpression | PostIncrementExpression | PostDecrementExpression | MethodInvocation | ClassInstanceCreationExpression ; IfThenStatement : if '(' Expression ')' Statement ; IfThenElseStatement : if '(' Expression ')' StatementNoShortIf else Statement ; IfThenElseStatementNoShortIf : if '(' Expression ')' StatementNoShortIf else StatementNoShortIf ; AssertStatement : assert Expression ';' | assert Expression ':' Expression ';' ; SwitchStatement : switch '(' Expression ')' SwitchBlock ; SwitchBlock : '{' SwitchRule + '}' | '{' SwitchBlockStatementGroup * ( SwitchLabel ':' ) * '}' ; SwitchRule : SwitchLabel '->' Expression ';' | SwitchLabel '->' Block | SwitchLabel '->' ThrowStatement ; SwitchBlockStatementGroup : ( SwitchLabel ':' ) + BlockStatements ; SwitchLabel : case CaseConstant + [ ',' ] | default ; CaseConstant : ConditionalExpression ; WhileStatement : while '(' Expression ')' Statement ; WhileStatementNoShortIf : while '(' Expression ')' StatementNoShortIf ; DoStatement : do Statement while '(' Expression ')' ';' ; ForStatement : BasicForStatement | EnhancedForStatement ; ForStatementNoShortIf : BasicForStatementNoShortIf | EnhancedForStatementNoShortIf ; BasicForStatement : for '(' ForInit ? ';' Expression ? ';' ForUpdate ? ')' Statement ; BasicForStatementNoShortIf : for '(' ForInit ? ';' Expression ? ';' ForUpdate ? ')' StatementNoShortIf ; ForInit : StatementExpressionList | LocalVariableDeclaration ; ForUpdate : StatementExpressionList ; StatementExpressionList : StatementExpression + [ ',' ] ; EnhancedForStatement : for '(' LocalVariableDeclaration ':' Expression ')' Statement ; EnhancedForStatementNoShortIf : for '(' LocalVariableDeclaration ':' Expression ')' StatementNoShortIf ; BreakStatement : break Identifier ? ';' ; YieldStatement : yield Expression ';' ; ContinueStatement : continue Identifier ? ';' ; ReturnStatement : return Expression ? ';' ; ThrowStatement : throw Expression ';' ; SynchronizedStatement : synchronized '(' Expression ')' Block ; TryStatement : try Block Catches | try Block Catches ? Finally | TryWithResourcesStatement ; Catches : CatchClause + ; CatchClause : catch '(' CatchFormalParameter ')' Block ; CatchFormalParameter : VariableModifier * CatchType VariableDeclaratorId ; CatchType : UnannClassType ( '|' ClassType ) * ; Finally : finally Block ; TryWithResourcesStatement : try ResourceSpecification Block Catches ? Finally ? ; ResourceSpecification : '(' ResourceList ';' ? ')' ; ResourceList : Resource + [ ';' ] ; Resource : LocalVariableDeclaration | VariableAccess ; Pattern : TypePattern ; TypePattern : LocalVariableDeclaration ; Primary : PrimaryNoNewArray | ArrayCreationExpression ; PrimaryNoNewArray : Literal | ClassLiteral | this | TypeName '.' this | '(' Expression ')' | ClassInstanceCreationExpression | FieldAccess | ArrayAccess | MethodInvocation | MethodReference ; ClassLiteral : TypeName ( '[' ']' ) * '.' class | NumericType ( '[' ']' ) * '.' class | boolean ( '[' ']' ) * '.' class | void '.' class ; ClassInstanceCreationExpression : UnqualifiedClassInstanceCreationExpression | ExpressionName '.' UnqualifiedClassInstanceCreationExpression | Primary '.' UnqualifiedClassInstanceCreationExpression ; UnqualifiedClassInstanceCreationExpression : new TypeArguments ? ClassOrInterfaceTypeToInstantiate '(' ArgumentList ? ')' ClassBody ? ; ClassOrInterfaceTypeToInstantiate : ( Annotation * Identifier ) + [ '.' ] TypeArgumentsOrDiamond ? ; TypeArgumentsOrDiamond : TypeArguments | '<>' ; FieldAccess : Primary '.' Identifier | super '.' Identifier | TypeName '.' super '.' Identifier ; ArrayAccess : ExpressionName '[' Expression ']' | PrimaryNoNewArray '[' Expression ']' ; MethodInvocation : MethodName '(' ArgumentList ? ')' | TypeName '.' TypeArguments ? Identifier '(' ArgumentList ? ')' | ExpressionName '.' TypeArguments ? Identifier '(' ArgumentList ? ')' | Primary '.' TypeArguments ? Identifier '(' ArgumentList ? ')' | super '.' TypeArguments ? Identifier '(' ArgumentList ? ')' | TypeName '.' super '.' TypeArguments ? Identifier '(' ArgumentList ? ')' ; ArgumentList : Expression + [ ',' ] ; MethodReference : ExpressionName '::' TypeArguments ? Identifier | Primary '::' TypeArguments ? Identifier | ReferenceType '::' TypeArguments ? Identifier | super '::' TypeArguments ? Identifier | TypeName '.' super '::' TypeArguments ? Identifier | ClassType '::' TypeArguments ? new | ArrayType '::' new ; ArrayCreationExpression : new PrimitiveType DimExprs Dims ? | new ClassOrInterfaceType DimExprs Dims ? | new PrimitiveType Dims ArrayInitializer | new ClassOrInterfaceType Dims ArrayInitializer ; DimExprs : DimExpr + ; DimExpr : Annotation * '[' Expression ']' ; Expression : LambdaExpression | AssignmentExpression ; LambdaExpression : LambdaParameters '->' LambdaBody ; LambdaParameters : '(' LambdaParameterList ? ')' | Identifier ; LambdaParameterList : LambdaParameter + [ ',' ] | Identifier + [ ',' ] ; LambdaParameter : VariableModifier * LambdaParameterType VariableDeclaratorId | VariableArityParameter ; LambdaParameterType : UnannType | var ; LambdaBody : Expression | Block ; AssignmentExpression : ConditionalExpression | Assignment ; Assignment : LeftHandSide AssignmentOperator Expression ; LeftHandSide : ExpressionName | FieldAccess | ArrayAccess ; AssignmentOperator : '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '>>>=' | '&=' | '^=' | '|=' ; ConditionalExpression : ConditionalOrExpression | ConditionalOrExpression '?' Expression ':' ConditionalExpression | ConditionalOrExpression '?' Expression ':' LambdaExpression ; ConditionalOrExpression : ConditionalAndExpression | ConditionalOrExpression '||' ConditionalAndExpression ; ConditionalAndExpression : InclusiveOrExpression | ConditionalAndExpression '&&' InclusiveOrExpression ; InclusiveOrExpression : ExclusiveOrExpression | InclusiveOrExpression '|' ExclusiveOrExpression ; ExclusiveOrExpression : AndExpression | ExclusiveOrExpression '^' AndExpression ; AndExpression : EqualityExpression | AndExpression '&' EqualityExpression ; EqualityExpression : RelationalExpression | EqualityExpression '==' RelationalExpression | EqualityExpression '!=' RelationalExpression ; RelationalExpression : ShiftExpression | RelationalExpression '<' ShiftExpression | RelationalExpression '>' ShiftExpression | RelationalExpression '<=' ShiftExpression | RelationalExpression '>=' ShiftExpression | InstanceofExpression ; InstanceofExpression : RelationalExpression instanceof ReferenceType | RelationalExpression instanceof Pattern ; ShiftExpression : AdditiveExpression | ShiftExpression '<<' AdditiveExpression | ShiftExpression '>>' AdditiveExpression | ShiftExpression '>>>' AdditiveExpression ; AdditiveExpression : MultiplicativeExpression | AdditiveExpression '+' MultiplicativeExpression | AdditiveExpression '-' MultiplicativeExpression ; MultiplicativeExpression : UnaryExpression | MultiplicativeExpression '*' UnaryExpression | MultiplicativeExpression '/' UnaryExpression | MultiplicativeExpression '%' UnaryExpression ; UnaryExpression : PreIncrementExpression | PreDecrementExpression | '+' UnaryExpression | '-' UnaryExpression | UnaryExpressionNotPlusMinus ; PreIncrementExpression : '++' UnaryExpression ; PreDecrementExpression : '--' UnaryExpression ; UnaryExpressionNotPlusMinus : PostfixExpression | '~' UnaryExpression | '!' UnaryExpression | CastExpression | SwitchExpression ; PostfixExpression : Primary | ExpressionName | PostIncrementExpression | PostDecrementExpression ; PostIncrementExpression : PostfixExpression '++' ; PostDecrementExpression : PostfixExpression '--' ; CastExpression : '(' PrimitiveType ')' UnaryExpression | '(' ReferenceType AdditionalBound * ')' UnaryExpressionNotPlusMinus | '(' ReferenceType AdditionalBound * ')' LambdaExpression ; SwitchExpression : switch '(' Expression ')' SwitchBlock ; %}