2 s = ws! CompilationUnit ws!
5 CompilationUnit:: Package? Import* (InterfaceDecl|ClassDecl)* /ws
7 Package = Annotations?! "package" PackageName ";" /ws
11 Import = "import" TypeName ";" /ws
12 | "import" (TypeName ".*") ";" /ws
13 | "import" "static" TypeName ";" /ws
14 | "import" "static" (TypeName ".*") ";" /ws
16 TypeName = Identifier +/ "."
17 PackageName = Identifier +/ "."
19 Modifiers = Modifiers:: ("public" | "protected" | "private") (ws! "abstract")?
20 | Modifiers:: "abstract"
22 ClassDecl = Class:: Modifiers "class" TypeDecl ClassBody /ws
23 InterfaceDecl = Interface:: Modifiers "interface" TypeDecl ClassBody /ws
26 | GenericTypeDecl:: Identifier "<" (TypeArg +/ comma) ">" /ws
29 | Extends:: Identifier "extends" Type /ws
30 | Super:: Identifier "super" Type /ws
32 | ExistExtends:: "?" "extends" Type
33 | Intersect:: Identifier "&" Type
35 ClassBody = "{" (BodyDecl +/ ws) "}" /ws
38 BodyDecl = FieldDecl | MethodDecl | ClassDecl | InterfaceDecl
40 FieldDecl = Field:: Modifiers Type Identifier ";" /ws
41 MethodDecl = Method:: MethodHeader (";" | MethodBody) /ws
43 MethodHeader = MethodHeader:: Modifiers Type Identifier Args /ws
44 MethodBody = "{" "}" /ws
46 Args = "(" (Arg+/comma) ")" /ws
48 Arg = Arg:: Type Identifier /ws
50 Type = BareType | GenericType | ArrayType
51 BareType = Type:: TypeName | "boolean" | "int" | "double" | "float" | "char" | "short" | "long" | "void"
52 GenericType = GenericType:: TypeName "<" (Type+/comma) ">" /ws
53 ArrayType = ArrayOf:: (BareType | GenericType) "[]" /ws
58 JavaLetter = [a-zA-Z_$]
59 Identifier = JavaLetter++
64 BooleanLiteral = "true" | "false"
67 // http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#29542
70 //UnicodeEscape = "\\u" [0-9] [0-9] [0-9] [0-9] // this is valid even inside strings/comments
73 //// Unicode escapes (including \\u garbage)
75 //WhiteSpace = [\r\n\t ]
76 //Comment = "/*" (~[]* &~ (~[]*! "*/" ~[]*!)) "*/"
77 // | "//" [^\r\n]* [\r\n]!
79 //Token = Identifier | Keyword | Literal | Separator | Operator
84 // | FloatingPointLiteral
91 //FloatLiteral = (DecimalFloatLiteral > HexFloatLiteral) [dDfF]?
92 //DecimalFloatLiteral = [0-9]++ "." [0-9]++ [ep] [+-]? [0-9]++
93 //HexFloatLiteral = ("0x"|"0X") [0-9a-fA-F]++ "." [0-9a-fA-F]++ [ep] [+-]? [0-9a-fA-F]++
95 //CharLiteral = "'" ([^\\\'] | "\\" ~[]) "'"
96 //StringLiteral = "\"" ([^\\\"] | "\\" ~[])* "\""
98 //IntegerLiteral = (DecimalLiteral | OctalLiteral | HexLiteral) [lL]?
99 //DecimalLiteral = [0-9]++ &~ OctalLiteral
100 //OctalLiteral = "0" [0-9]++ &~ "0"
101 //HexLiteral = ("0x"|"0X") [0-9a-fA-F]++
105 "abstract" | "continue" | "for" | "new" | "switch"
106 | "assert" | "default" | "if" | "package" | "synchronized"
107 | "boolean" | "do" | "goto" | "private" | "this"
108 | "break" | "double" | "implements" | "protected" | "throw"
109 | "byte" | "else" | "import" | "public" | "throws"
110 | "case" | "enum" | "instanceof" | "return" | "transient"
111 | "catch" | "extends" | "int" | "short" | "try"
112 | "char" | "final" | "interface" | "static" | "void"
113 | "class" | "finally" | "long" | "strictfp" | "volatile"
114 | "const" | "float" | "native" | "super" | "while"
116 // We don't obey this:
118 // "The longest possible translation is used at each step, even if
119 // "the result does not ultimately make a correct program while
120 // "another lexical translation would. Thus the input characters
121 // "a--b are tokenized (-3.5) as a, --, b, which is not part of any
122 // "grammatically correct program, even though the tokenization a,
123 // "-, -, b could be part of a grammatically correct program.