Initial Commit
[packages] / xemacs-packages / jde / lisp / java.bnf
1 # BNF grammar for Java
2 #
3 # Copyright (C) 2000, 2001 Paul F. Kinnucan, Jr.
4 #
5 # Author: Paul F. Kinnucan, Jr. <paulk@mathworks.com>
6 #         Augmented by David Ponce <david@dponce.com>
7 # $Id: java.bnf,v 1.17 2001/01/17 18:22:15 paulk Exp $
8 #
9 # java.bnf is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
12 # any later version.
13 #
14 # This software is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with GNU Emacs; see the file COPYING.  If not, write to the
21 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 # Boston, MA 02111-1307, USA.
23
24 # TABLE: jde-java-grammar.el:jde-parse-bovine-java-grammar
25 # MODE: jde-mode
26
27 # --------
28 # Settings
29 # --------
30 %outputfile    jde-java-grammar.el
31 %parsetable    jde-parse-bovine-java-grammar
32 %keywordtable  jde-parse-bovine-java-keywords
33 %languagemode  jde-mode
34 %setupfunction jde-parse-semantic-default-setup
35
36 %(progn
37    ;; Java is case sensitive
38    (setq semantic-case-fold nil)
39    ;; imenu & speedbar setup
40    (jde-imenu-setup)
41    ;; initial parsing of the current buffer
42    (semantic-bovinate-toplevel)
43    (jde-parse-update-after-parse)
44    )%
45
46 %token ABSTRACT     "abstract"
47 %token BOOLEAN      "boolean"
48 %token BREAK        "break"
49 %token BYTE         "byte"
50 %token CASE         "case"
51 %token CATCH        "catch"
52 %token CHAR         "char"
53 %token CLASS        "class"
54 %token CONST        "const"
55 %token CONTINUE     "continue"
56 %token DEFAULT      "default"
57 %token DO           "do"
58 %token DOUBLE       "double"
59 %token ELSE         "else"
60 %token EXTENDS      "extends"
61 %token FINAL        "final"
62 %token FINALLY      "finally"
63 %token FLOAT        "float"
64 %token FOR          "for"
65 %token GOTO         "goto"
66 %token IF           "if"
67 %token IMPLEMENTS   "implements"
68 %token IMPORT       "import"
69 %token INSTANCEOF   "instanceof"
70 %token INT          "int"
71 %token INTERFACE    "interface"
72 %token LONG         "long"
73 %token NATIVE       "native"
74 %token NEW          "new"
75 %token PACKAGE      "package"
76 %token PRIVATE      "private"
77 %token PROTECTED    "protected"
78 %token PUBLIC       "public"
79 %token RETURN       "return"
80 %token SHORT        "short"
81 %token STATIC       "static"
82 %token STRICTFP     "strictfp"
83 %token SUPER        "super"
84 %token SWITCH       "switch"
85 %token SYNCHRONIZED "synchronized"
86 %token THIS         "this"
87 %token THROW        "throw"
88 %token THROWS       "throws"
89 %token TRANSIENT    "transient"
90 %token TRY          "try"
91 %token VOID         "void"
92 %token VOLATILE     "volatile"
93 %token WHILE        "while"
94   
95 # --------
96 # Grammar
97 # --------
98 bovine-toplevel : package_declaration
99                 | import_declaration
100                 | type_declaration
101                 ;
102   
103 number : symbol "[0-9]" punctuation "\\." symbol "[0-9Ee]" punctuation "[-+]" symbol "[0-9fFdD]"
104        | symbol "[0-9]" punctuation "\\." symbol "[0-9EefFdD]"
105        | symbol "[0-9fFdD]"
106        ;
107   
108 literal : number
109         | qualified_name
110         | string
111         ;
112
113 type : reference_type
114        (,$1)
115      | primitive_type
116        (,$1)
117      ;
118   
119 primitive_type : BOOLEAN | BYTE | SHORT | INT | LONG | CHAR | FLOAT | DOUBLE
120                #                 ($1)
121                ;
122
123 reference_type : array_type
124                  (,$1)
125                | qualified_name
126                  (,$1)
127                ;
128   
129 array_type : primitive_type dims
130              ((concat (car $1) (car $2)))
131            | qualified_name dims
132              ((concat (car $1) (car $2)))
133            ;
134
135 qualified_name : symbol punctuation "\\." qualified_name
136                  ((concat $1 $2 (car $3)))
137                | symbol
138                  ($1)
139                ;
140
141 package_declaration : PACKAGE qualified_name punctuation ";"
142                       (,$2 package nil)
143                     ;
144   
145 import_declaration : IMPORT qualified_name punctuation ";"
146                      (,$2 include nil)
147                    | IMPORT qualified_name punctuation "\\." punctuation "*" punctuation ";"
148                      ((concat (car $2) $3 $4) include nil)
149                    ;
150   
151 type_declaration : punctuation ";"
152                  | class_declaration
153                  | interface_declaration
154                  ;
155
156 modifiers_opt : modifiers
157                 (,$1)
158               | EMPTY
159               #(nil)
160               ;
161   
162 modifiers : modifier modifiers
163             (,(cons (car $1) ,$2))
164           | modifier
165             (,$1)
166           ;
167   
168 modifier : PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT
169          | FINAL | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE | STRICTFP
170          ;
171
172 class_declaration : modifiers_opt CLASS qualified_name class_parents class_body
173                     (,$3 type "class" $5 $4 $1 nil) # semantic 1.2
174                   #                            ^^^ empty comment spot
175                   ;
176
177 #
178 # `semantic-token-type-parent' returns
179 #
180 #  nil | ( ["extends_this" | nil] ["implements_this1" ... "implements_thisN"] )
181 #  
182 class_parents: super interfaces
183                (,(append $1 $2))
184              | interfaces super
185                (,(append $2 $1))
186              | super
187                (,$1)
188              | interfaces
189                (,(cons nil $1))
190              | EMPTY
191              ;
192   
193 super : EXTENDS qualified_name
194         (,$2)
195       ;
196   
197 interfaces : IMPLEMENTS qualified_name_list
198              (,$2)
199            ;
200   
201 qualified_name_list : qualified_name punctuation "," qualified_name_list
202                       (,(cons (car $1) ,$3))
203                     | qualified_name
204                       (,$1)
205                     ;
206   
207 class_body : semantic-list # ::= {class_body_declarations}
208              (EXPANDFULL $1 class_body_declarations)
209            ;
210   
211 # class_body_declarations : open-paren "{"
212 #                           ( )
213 #                         | close-paren "}"
214 #                           ( )
215 #                         | class_declaration
216 class_body_declarations : class_declaration
217                           (,$1)
218                         | interface_declaration
219                           (,$1)
220                         | field_declaration
221                           (,$1)
222                         | method_declaration
223                           (,$1)
224                         #| static_initializer
225                         | constructor_declaration
226                           (,$1)
227                         #| block
228                         ;
229   
230 field_declaration : modifiers_opt type variable_declarators punctuation ";"
231                     (,$3 variable ,$2 nil nil $1 nil) # semantic 1.2
232                   #                              ^^^ empty comment spot
233                   ;
234   
235 variable_declarators : variable_declarator variable_declarators_opt
236                        (,$1)
237                      ;
238
239 variable_declarators_opt: punctuation "," variable_declarators
240                         | EMPTY
241                         ;
242
243 variable_declarator : variable_declarator_id variable_assign_opt
244                       (,$1)
245                     ;
246
247 variable_assign_opt: punctuation "=" variable_initializer
248                    | EMPTY
249                    ;
250
251 variable_declarator_id : symbol dims
252                          ((concat $1 (car $2)))
253                        | symbol
254                          ($1)
255                        ;
256
257 variable_initializer : array_initializer
258                      | expression
259                      ;
260
261 method_declaration : method_header method_body
262                      (,$1)
263                    ;
264   
265 method_header : modifiers_opt method_type symbol formal_parameter_list_opt throws_opt
266                 ($3 function ,$2 $4 $1 $5 nil) # semantic 1.2
267               #                           ^^^ empty comment spot
268               ;
269
270 method_type: VOID
271              ($1)
272            | type
273              (,$1)
274            ;
275   
276 formal_parameter_list_opt : semantic-list # ::= (formal_parameter_list)
277                             (EXPAND $1 formal_parameter_list)
278                           ;
279   
280 formal_parameter_list : open-paren "(" close-paren ")"
281                         (nil)
282                       | open-paren "(" formal_parameter formal_parameter_list_next
283                         (,(cons ,$2 ,$3))
284                       ;
285
286 formal_parameter_list_next: close-paren ")"
287                             (nil)
288                           | punctuation "," formal_parameter formal_parameter_list_next
289                             (,(cons ,$2 ,$3))
290                           ;
291   
292 formal_parameter-modifier : FINAL
293                           | EMPTY
294                           ;
295
296 formal_parameter : formal_parameter-modifier type variable_declarator_id
297                    ((car $3) variable (car $2) nil nil $1 nil) # semantic 1.2
298                  #                                        ^^^ empty comment spot
299                  ;
300   
301 throws_opt : throws
302              (,$1)
303            | EMPTY
304            ;
305   
306 throws : THROWS qualified_name_list
307          (,$2)
308        ;
309   
310 method_body : punctuation ";"
311               (nil)
312             | block
313               (nil)
314             ;
315
316 #static_initializer : STATIC block
317 #                   ;
318
319 constructor_declaration : modifiers_opt symbol formal_parameter_list_opt throws_opt constructor_body
320                           ($2 function nil $3 $1 $4 nil) # semantic 1.2
321                         #                           ^^^ empty comment spot
322                         ;
323   
324 constructor_body : block
325                    (nil)
326                  ;
327
328 interface_declaration : modifiers_opt INTERFACE symbol interface_parents interface_body
329                         ($3 type "interface" $5 $4 $1 nil) # semantic 1.2
330                       #                               ^^^ empty comment spot
331                       ;
332
333 #
334 # `semantic-token-type-parent' returns:
335 #
336 #  nil | ( "extends_this1" ... "extends_thisN" )
337 #
338 interface_parents : EXTENDS qualified_name_list
339                     (,$2)
340                   | EMPTY
341                   ;
342   
343 interface_body : semantic-list # ::= { interface_body_declarations }
344                  (EXPANDFULL $1 interface_body_declarations)
345                ;
346   
347 # interface_body_declarations : open-paren "{"
348 #                               ( )
349 #                             | close-paren "}"
350 #                               ( )
351 #                             | class_declaration
352 interface_body_declarations : class_declaration
353                               (,$1)
354                             | interface_declaration
355                               (,$1)
356                             | method_header punctuation ";"
357                               (,$1)
358                             | field_declaration
359                               (,$1)
360                             ;
361   
362 array_initializer : semantic-list "\\`{" # ::= {expression, expression, ...}
363                   ;
364   
365 block : semantic-list "\\`{" # ::= {statements}
366       ;
367   
368 primary : array_creation_expression
369         | primary_no_new_array primary_dim_opt
370         ;
371
372 primary_dim_opt : semantic-list "\\`\\["
373                 | EMPTY
374                 ;
375   
376 primary_no_new_array : qualified_name semantic-list "\\`(" # method_invocation
377                      | class_instance_creation_expression
378                      | semantic-list "\\`(" # (expression)
379                      | array_type punctuation "\\." CLASS
380                      | literal
381                      ;
382
383 class_instance_creation_expression : NEW qualified_name semantic-list "\\`(" semantic-list "\\`{" # ::= { class_body_declarations }
384                                    | NEW qualified_name semantic-list "\\`("
385                                    ;
386   
387 array_creation_expression : NEW qualified_name dims array_initializer
388                           | NEW qualified_name dims #dim_exprs dims_opt
389                           ;
390   
391 dims_opt : dims
392            (,$1)
393          | EMPTY
394            (nil)
395          ;
396
397 dims: semantic-list "\\`\\[" dims_opt
398       ((concat "[]" (car ,$2)))
399     ;
400   
401 field_access : primary punctuation "\\." symbol
402              | qualified_name
403              ;
404   
405 postfix_expression : primary postfix_operator_opt
406                    ;
407
408 postfix_operator_opt: punctuation "[-+]" punctuation "[-+]"
409                     | EMPTY
410                     ;
411
412 unary_expression : punctuation "[-+^!]" unary_expression
413                  | punctuation "[-+]" punctuation "[-+]" unary_expression
414                  | semantic-list "\\`(" unary_expression # cast
415                  | postfix_expression
416                  ;
417
418 operator: punctuation "[-+*/%=<>^~&|!?:.]" # added DOT as field/method access operator
419         | INSTANCEOF 
420         ;
421
422 operators: operator operators
423          | operator
424          ;
425
426 operators_expression_opt: operators expression
427                         | EMPTY
428                         ;
429
430 expression: unary_expression operators_expression_opt
431           ;
432
433 # $Log: java.bnf,v $
434 # Revision 1.17  2001/01/17 18:22:15  paulk
435 # Changed the definition of formal_parameter_list to improve performance (less
436 # backtracking) when parsing parameters in method and constructor
437 # declarations. Thanks to David Ponce.
438 #
439 # Revision 1.16  2000/10/25 04:31:37  paulk
440 # Modified to reflect new location of generated files.
441 #
442 # Revision 1.15  2000/10/25 03:29:05  paulk
443 # David Ponce's fixes for various semantic-list matching
444 # problems. For example, this caused the parser to find a variable
445 # declaration for a method declaration like the following:
446 #
447 #   [modifiers] type name(..., any_occurence_of[], ...);
448 #
449 # Revision 1.14  2000/10/20 04:00:38  paulk
450 # *** empty log message ***
451 #
452 # Revision 1.13  2000/08/19 06:37:58  paulk
453 # Changed regular expressions for modifiers to match whole words.
454 #
455 # Revision 1.12  2000/08/16 05:03:55  paulk
456 # Changed regular expression for modifier to match only words.
457 #
458 # Revision 1.11  2000/07/08 07:09:31  paulk
459 # Latest updates from David Ponce.
460 #
461 # Revision 1.8  2000/06/21 06:57:23  paulk
462 # Added rules for arithmetic and string expressions.
463 #
464 # Revision 1.7  2000/06/16 06:48:40  paulk
465 # Now handles arrays in field declarations.
466 #
467 # Revision 1.6  2000/06/09 04:08:43  paulk
468 # Added volatile to list of variable modifiers. Thanks to David Ponce and Mike Bowler.
469 #
470 # Revision 1.5  2000/05/26 09:14:10  paulk
471 # Updated grammar to handle argument variables with modifiers and array arguments.
472 #
473 # Revision 1.4  2000/05/16 04:41:28  paulk
474 # *** empty log message ***
475 #
476 # Revision 1.3  2000/05/11 04:41:05  paulk
477 # Now handles native method declarations.
478 #
479 # Revision 1.2  2000/05/11 02:49:41  paulk
480 # Now parses constructors.
481 #
482 # Revision 1.1  2000/05/02 04:10:23  paulk
483 # Initial revision.
484 #
485
486 # End of java.bnf