Skip to content

Commit 270a7b0

Browse files
authored
Merge pull request #216 from pdfCN/fix/java_parser
Add Support for Lambda, Comments, Generics, and For-Each to Java Grammar
2 parents 66bb0b7 + acef3fa commit 270a7b0

1 file changed

Lines changed: 42 additions & 8 deletions

File tree

syncode/parsers/grammars/java.lark

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
start: compilation_unit
1+
start: compilation_unit
22

33
%import common.CNAME
44
%import common.DIGIT
55
%import common.WS
66

77
%ignore WS
88

9+
LINE_COMMENT: /\/\/[^\n\r]*/
10+
BLOCK_COMMENT: /\/\*[\s\S]*?\*\//
11+
12+
type_parameters: "<" type_parameter ("," type_parameter)* ">"
13+
type_parameter: CNAME type_bound?
14+
type_bound: "extends" type ("&" type)*
15+
16+
%ignore LINE_COMMENT
17+
%ignore BLOCK_COMMENT
18+
919
compilation_unit: package_declaration? import_declarations? type_declarations?
1020

1121
package_declaration: "package" name ";"
@@ -30,7 +40,15 @@ interface_type: class_or_interface_type
3040

3141
class_type: class_or_interface_type
3242

33-
class_or_interface_type: name
43+
class_or_interface_type: name type_arguments?
44+
45+
type_arguments: "<" type_argument ("," type_argument)* ">" | diamond_operator
46+
47+
diamond_operator: "<" ">"
48+
49+
type_argument: type | wildcard
50+
51+
wildcard: "?" ( "extends" type | "super" type )?
3452

3553
class_body: "{" class_body_declarations "}" | "{" "}"
3654

@@ -48,7 +66,7 @@ variable_initializer: expression | array_initializer
4866

4967
method_declaration: method_header method_body
5068

51-
method_header: modifiers? type method_declarator throws?
69+
method_header: modifiers? type_parameters? type method_declarator throws?
5270

5371
method_declarator: CNAME "(" formal_parameter_list? ")"
5472

@@ -128,7 +146,11 @@ while_statement: "while" "(" expression ")" statement
128146

129147
do_statement: "do" statement "while" "(" expression ")" ";"
130148

131-
for_statement: "for" "(" for_init? ";" expression? ";" for_update? ")" statement
149+
for_statement: "for" "(" for_each ")" statement| "for" "(" for_traditional ")" statement
150+
151+
for_each: type CNAME ":" expression
152+
153+
for_traditional: for_init? ";" expression? ";" for_update?
132154

133155
for_init: statement_expression_list | local_variable_declaration
134156

@@ -166,7 +188,7 @@ floating_point_literal: DIGIT+ "." DIGIT+
166188

167189
boolean_literal: "true" | "false"
168190

169-
character_literal: "/'[^\n\r']?'/"
191+
character_literal: "'" /[^\\'\n\r]/ "'"
170192

171193
string_literal: /".*?"/
172194

@@ -184,6 +206,8 @@ dim_expr: "[" expression "]"
184206

185207
dims: "[" "]"+
186208

209+
210+
187211
field_access: primary "." CNAME | "super" "." CNAME
188212

189213
method_invocation: name "(" argument_list? ")" | primary "." CNAME "(" argument_list? ")" | "super" "." CNAME "(" argument_list? ")"
@@ -204,7 +228,10 @@ pre_decrement_expression: "--" unary_expression
204228

205229
unary_expression_not_plus_minus: postfix_expression | "~" unary_expression | "!" unary_expression | cast_expression
206230

207-
cast_expression: "(" primitive_type dims? ")" unary_expression | "(" primitive_type ")" unary_expression | "(" expression ")" unary_expression_not_plus_minus | "(" name dims? ")" unary_expression_not_plus_minus
231+
cast_expression: "(" primitive_type dims? ")" unary_expression
232+
| "(" primitive_type ")" unary_expression
233+
| "(" expression ")" unary_expression_not_plus_minus
234+
| "(" name dims? ")" unary_expression_not_plus_minus
208235

209236
multiplicative_expression: unary_expression | multiplicative_expression "*" unary_expression | multiplicative_expression "/" unary_expression | multiplicative_expression "%" unary_expression
210237

@@ -236,7 +263,13 @@ left_hand_side: name | field_access | array_access
236263

237264
assignment_operator: "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | ">>>=" | "&=" | "^=" | "|="
238265

239-
expression: assignment_expression
266+
expression: assignment_expression | lambda_expression
267+
268+
lambda_expression: lambda_parameters "->" lambda_body
269+
270+
lambda_parameters: CNAME | "(" ")" | "(" CNAME ("," CNAME)* ")"
271+
272+
lambda_body: expression | block
240273

241274
constant_expression: expression
242275

@@ -254,4 +287,5 @@ floating_point_type: "float" | "double"
254287

255288
reference_type: class_or_interface_type | array_type
256289

257-
array_type: primitive_type dims | name dims | array_type dims
290+
array_type: primitive_type dims | name dims | array_type dims
291+

0 commit comments

Comments
 (0)