* doc/misc/eshell.texi: Fill most of the missing sections.
[emacs.git] / etc / grammars / c.by
blobf5b819a3006a4864e371103e10eda4215b2e75c6
1 ;;; c.by -- LL grammar for C/C++ language specification
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;;  2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5 ;;
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;;         David Ponce <david@dponce.com>
8 ;;         Klaus Berndl <klaus.berndl@sdm.de>
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
25 ;; TODO:  From Nate Schley
26 ;; >  * Can't parse signature element: "const char* const rmc_ClrTxt"
27 ;; >  * Can't parse signature element: "char* const dellog_ClrTxt"
28 ;; >  * Can't parse signature element: "const char* dellog_SetTxt"
29 ;; >  * Can't parse signature element: "const RmcCmdSSPADetailedStatus& status"
30 ;; >
31 ;; > And FWIW I have seen the following argument cases not handled, even
32 ;; > with no leading/trailing spaces in the split:
33 ;; >
34 ;; >  * Can't parse signature element: "const bool currentAlarmStatus"
35 ;; >  * Can't parse signature element: "unsigned char mode"
36 ;; >  * Can't parse signature element: "TskTimingTask* tsktimingtask"
37 ;; >  * Can't parse signature element: "unsigned char htrStatus"
38 ;; >  * Can't parse signature element: "char trackPower[]"
39 ;; >  * Can't parse signature element: "const RmcCmdMCDetailedStatus& status"
40 ;; >  * Can't parse signature element: "RmcBucStatus* rftBucStatus"
42 %package semantic-c-by
44 %languagemode  c-mode c++-mode
45 %start         declaration
46 %scopestart    codeblock
48 %token <punctuation>   HASH       "\\`[#]\\'"
49 %token <punctuation>   PERIOD     "\\`[.]\\'"
50 %token <punctuation>   COLON      "\\`[:]\\'"
51 %token <punctuation>   SEMICOLON  "\\`[;]\\'"
52 %token <punctuation>   STAR       "\\`[*]\\'"
53 %token <punctuation>   AMPERSAND  "\\`[&]\\'"
54 %token <punctuation>   DIVIDE     "\\`[/]\\'"
55 %token <punctuation>   PLUS       "\\`[+]\\'"
56 %token <punctuation>   MINUS      "\\`[-]\\'"
57 %token <punctuation>   BANG       "\\`[!]\\'"
58 %token <punctuation>   EQUAL      "\\`[=]\\'"
59 %token <punctuation>   LESS       "\\`[<]\\'"
60 %token <punctuation>   GREATER    "\\`[>]\\'"
61 %token <punctuation>   COMA       "\\`[,]\\'"
62 %token <punctuation>   TILDE      "\\`[~]\\'"
63 %token <punctuation>   MOD        "\\`[%]\\'"
64 %token <punctuation>   HAT        "\\`\\^\\'"
65 %token <punctuation>   OR         "\\`[|]\\'"
66 %token <string>        C          "\"C\""
67 %token <string>        CPP        "\"C\\+\\+\""
68 %token <number>        ZERO       "^0$"
69 %token <symbol>        RESTRICT   "\\<\\(__\\)?restrict\\>"
70 %token <open-paren>    LPAREN     "("
71 %token <close-paren>   RPAREN     ")"
72 %token <open-paren>    LBRACE     "{"
73 %token <close-paren>   RBRACE     "}"
74 %token <semantic-list> BRACK_BLCK "\\[.*\\]$"
75 %token <semantic-list> PAREN_BLCK "^("
76 %token <semantic-list> BRACE_BLCK "^{"
77 %token <semantic-list> VOID_BLCK  "^(void)$"
78 %token <semantic-list> PARENS     "()"
79 %token <semantic-list> BRACKETS   "\\[\\]"
81 %token EXTERN "extern"
82 %put EXTERN summary "Declaration Modifier: extern <type> <name> ..."
83 %token STATIC "static"
84 %put STATIC summary "Declaration Modifier: static <type> <name> ..."
85 %token CONST "const"
86 %put CONST summary "Declaration Modifier: const <type> <name> ..."
87 %token VOLATILE "volatile"
88 %put VOLATILE summary "Declaration Modifier: volatile <type> <name> ..."
89 %token REGISTER "register"
90 %put REGISTER summary "Declaration Modifier: register <type> <name> ..."
91 %token SIGNED "signed"
92 %put SIGNED summary "Numeric Type Modifier: signed <numeric type> <name> ..."
93 %token UNSIGNED "unsigned"
94 %put UNSIGNED summary "Numeric Type Modifier: unsigned <numeric type> <name> ..."
96 %token INLINE "inline"
97 %put INLINE summary "Function Modifier: inline <return  type> <name>(...) {...};"
98 %token VIRTUAL "virtual"
99 %put VIRTUAL summary "Method Modifier: virtual <type> <name>(...) ..."
100 %token MUTABLE "mutable"
101 %put MUTABLE summary "Member Declaration Modifier: mutable <type> <name> ..."
103 %token STRUCT "struct"
104 %put STRUCT summary "Structure Type Declaration: struct [name] { ... };"
105 %token UNION "union"
106 %put UNION summary "Union Type Declaration: union [name] { ... };"
107 %token ENUM "enum"
108 %put ENUM summary "Enumeration Type Declaration: enum [name] { ... };"
109 %token TYPEDEF "typedef"
110 %put TYPEDEF summary "Arbitrary Type Declaration: typedef <typedeclaration> <name>;"
111 %token CLASS "class"
112 %put CLASS summary "Class Declaration: class <name>[:parents] { ... };"
113 %token TYPENAME "typename"
114 %put TYPENAME summary "typename is used to handle a qualified name as a typename;"
115 %token NAMESPACE "namespace"
116 %put NAMESPACE summary "Namespace Declaration: namespace <name> { ... };"
117 %token USING "using"
118 %put USING summary "using <namespace>;"
120 %token NEW "new"
121 %put NEW summary "new <classname>();"
122 %token DELETE "delete"
123 %put DELETE summary "delete <object>;"
125 ;; Despite this, this parser can find templates by ignoring the TEMPLATE
126 ;; keyword, and finding the class/method being templateized.
127 %token TEMPLATE "template"
128 %put TEMPLATE summary "template <class TYPE ...> TYPE_OR_FUNCTION"
130 %token THROW "throw"
131 %put THROW summary "<type> <methoddef> (<method args>) throw (<exception>) ..."
132 %token REENTRANT "reentrant"
133 %put REENTRANT summary "<type> <methoddef> (<method args>) reentrant ..."
134 %token TRY "try"
135 %token CATCH "catch"
136 %put { TRY CATCH } summary "try { <body> } catch { <catch code> }"
138 ;; Leave these alone for now.
139 %token OPERATOR "operator"
140 %token PUBLIC "public"
141 %token PRIVATE "private"
142 %token PROTECTED "protected"
143 %token FRIEND "friend"
144 %put FRIEND summary "friend class <CLASSNAME>"
146 ;; These aren't used for parsing, but is a useful place to describe the keywords.
147 %token IF "if"
148 %token ELSE "else"
149 %put {IF ELSE} summary  "if (<condition>) { code } [ else { code } ]"
151 %token DO "do"
152 %token WHILE "while"
153 %put DO summary " do { code } while (<condition>);"
154 %put WHILE summary "do { code } while (<condition>); or while (<condition>) { code };"
156 %token FOR "for"
157 %put FOR summary "for(<init>; <condition>; <increment>) { code }"
159 %token SWITCH "switch"
160 %token CASE "case"
161 %token DEFAULT "default"
162 %put {SWITCH CASE DEFAULT} summary
163 "switch (<variable>) { case <constvalue>: code; ... default: code; }"
165 %token RETURN "return"
166 %put RETURN summary "return <value>;"
168 %token BREAK "break"
169 %put BREAK summary "Non-local exit within a loop or switch (for, do/while, switch): break;"
170 %token CONTINUE "continue"
171 %put CONTINUE summary "Non-local continue within a loop (for, do/while): continue;"
173 %token SIZEOF "sizeof"
174 %put SIZEOF summary "Compile time macro: sizeof(<type or variable>) // size in bytes"
176 ;; Types
177 %token VOID "void"
178 %put VOID summary "Built in typeless type: void"
179 %token CHAR "char"
180 %put CHAR summary "Integral Character Type: (0 to 256)"
181 %token WCHAR "wchar_t"
182 %put WCHAR summary "Wide Character Type"
183 %token SHORT "short"
184 %put SHORT summary "Integral Primitive Type: (-32768 to 32767)"
185 %token INT "int"
186 %put INT summary "Integral Primitive Type: (-2147483648 to 2147483647)"
187 %token LONG "long"
188 %put LONG summary "Integral primitive type (-9223372036854775808 to 9223372036854775807)"
189 %token FLOAT "float"
190 %put FLOAT summary "Primitive floating-point type (single-precision 32-bit IEEE 754)"
191 %token DOUBLE "double"
192 %put DOUBLE summary "Primitive floating-point type (double-precision 64-bit IEEE 754)"
193 %token BOOL "bool"
194 %put BOOL summary "Primitive boolean type"
196 %token UNDERP "_P"
197 %token UNDERUNDERP "__P"
198 %put UNDERP summary "Common macro to eliminate prototype compatibility on some compilers"
199 %put UNDERUNDERP summary "Common macro to eliminate prototype compatibility on some compilers"
203 declaration
204   : macro
205   | type
206  ;; TODO: Klaus Berndl: Is the define here necessary or even wrong?
207  ;; Is this part not already covered by macro??
208   | define
209   | var-or-fun
210   | extern-c
211   | template
212   | using
213   ;
215 codeblock
216   : define
217   | codeblock-var-or-fun
218   | type ;; type is less likely to be used here.
219   | using
220   ;
222 extern-c-contents
223   : open-paren
224     ( nil )
225   | declaration
226   | close-paren
227     ( nil )
228   ;
230 extern-c
231   : EXTERN C semantic-list
232  ;; Extern C commands which contain a list need to have the
233  ;; entries of the list extracted, and spliced into the main
234  ;; list of entries.  This must be done via the function
235  ;; that expands singular nonterminals, such as int x,y;
236     (TAG "C" 'extern :members (EXPANDFULL $3 extern-c-contents) )
237   | EXTERN CPP semantic-list
238     (TAG "C" 'extern :members (EXPANDFULL $3 extern-c-contents) )
239   | EXTERN C
240  ;; A plain extern "C" call should add something to the token,
241  ;; but just strip it from the buffer here for now.
242     ( nil )
243   | EXTERN CPP
244     ( nil )
245   ;
247 macro
248   : spp-macro-def
249     (VARIABLE-TAG $1 nil nil :constant-flag t )
250   | spp-system-include
251     (INCLUDE-TAG $1 t)
252   | spp-include
253     (INCLUDE-TAG $1 nil)
254   ;
256 ;; This is used in struct parts.
257 define
258   : spp-macro-def
259     (VARIABLE-TAG $1 nil nil :constant-flag t)
260   | spp-macro-undef
261     ( nil )
262   ;
264 ;; In C++, structures can have the same things as classes.
265 ;; So delete this somday in the figure.
267 ;;structparts : semantic-list
268 ;;            (EXPANDFULL $1 structsubparts)
269 ;;          ;
271 ;;structsubparts : LBRACE
272 ;;               ( nil )
273 ;;             | RBRACE
274 ;;               ( nil )
275 ;;             | var-or-fun
276 ;;             | define
277 ;;             ;; sometimes there are defines in structs.
278 ;;             ;
280 unionparts
281   : semantic-list
282     (EXPANDFULL $1 classsubparts)
283   ;
285 opt-symbol
286   : symbol
287   | ;;EMPTY
288   ;
290 ;; @todo - support 'friend' construct.
291 classsubparts
292   : LBRACE
293     ( nil )
294   | RBRACE
295     ( nil )
296   | class-protection opt-symbol COLON
297  ;; For QT, they may put a `slot' keyword between the protection
298  ;; and the COLON.  @todo - Have the QT stuff use macros.
299     (TAG (car $1) 'label)
300   | var-or-fun
301   | FRIEND func-decl
302     (TAG (car $2) 'friend)
303   | FRIEND CLASS symbol
304     (TAG $3 'friend)
305   | type
306   | define
307   | template
308   | ;;EMPTY
309   ;
311 opt-class-parents
312   : COLON class-parents opt-template-specifier
313     ( $2 )
314   | ;;EMPTY
315     ( )
316   ;
318 one-class-parent
319   : opt-class-protection opt-class-declmods namespace-symbol
320     (TYPE-TAG (car $3) "class" nil nil :protection (car $1))
321   | opt-class-declmods opt-class-protection namespace-symbol
322     (TYPE-TAG (car $3) "class" nil nil :protection (car $2))
323   ;
325 class-parents
326   : one-class-parent COMA class-parents
327     ( ,(cons ,$1 $3 ) )
328   | one-class-parent
329     ( $1 )
330   ;
332 opt-class-declmods
333   : class-declmods opt-class-declmods
334     ( nil )
335   | ;;EMPTY
336   ;
338 class-declmods
339   : VIRTUAL
340   ;
342 class-protection
343   : PUBLIC
344   | PRIVATE
345   | PROTECTED
346   ;
348 opt-class-protection
349   : class-protection
350     ( ,$1 )
351   | ;;EMPTY - Same as private
352     ( "unspecified" )
353   ;
355 namespaceparts
356   : semantic-list
357     (EXPANDFULL $1 namespacesubparts)
358   ;
360 namespacesubparts
361   : LBRACE
362     ( nil )
363   | RBRACE
364     ( nil )
365   | type
366   | var-or-fun
367   | define
368   | class-protection COLON
369     (TAG (car $1) 'label)
370  ;; In C++, this label in a classsubpart represents
371  ;; PUBLIC or PRIVATE bits.  Ignore them for now.
372   | template
373   | using
374   | ;;EMPTY
375   ;
377 enumparts
378   : semantic-list
379     (EXPANDFULL $1 enumsubparts)
380   ;
382 enumsubparts
383   : symbol opt-assign
384     (VARIABLE-TAG $1 "int" (car $2) :constant-flag t )
385   | LBRACE
386     ( nil )
387   | RBRACE
388     ( nil )
389   | COMA
390     ( nil )
391   ;
393 opt-name
394   : symbol
395   | ;;EMPTY
396     ( "" )
397   ;
399 typesimple
400   : struct-or-class opt-class opt-name opt-template-specifier
401     opt-class-parents semantic-list
402     (TYPE-TAG (car $3) (car $1)
403           (let ((semantic-c-classname (cons (car ,$3) (car ,$1))))
404             (EXPANDFULL $6 classsubparts))
405           $5
406           :template-specifier $4
407           :parent (car ,$2))
408   | struct-or-class opt-class opt-name opt-template-specifier
409     opt-class-parents
410     (TYPE-TAG (car $3) (car $1) nil $5
411               :template-specifier $4
412               :prototype t
413               :parent (car ,$2))
414   | UNION opt-class opt-name unionparts
415     (TYPE-TAG (car $3) $1 $4 nil
416               :parent (car ,$2))
417   | ENUM opt-class opt-name enumparts
418     (TYPE-TAG (car $3) $1 $4 nil
419               :parent (car ,$2))
420  ;; Klaus Berndl: a typedef can be a typeformbase with all this
421  ;; declmods stuff.
422   | TYPEDEF declmods typeformbase cv-declmods typedef-symbol-list
423  ;;;; We put the type this typedef renames into PARENT
424  ;;;; but will move it in the expand function.
425     (TYPE-TAG $5 $1 nil (list $3) )
426   ;
428 typedef-symbol-list
429   : typedefname COMA typedef-symbol-list
430     ( ,(cons $1 $3) )
431   | typedefname
432     ( $1 )
433   ;
435 ;; TODO: Klaus Berndl: symbol -> namespace-symbol?!  Answer: Probably
436 ;; symbol is correct here!
437 typedefname
438   : opt-stars symbol opt-bits opt-array
439     ( $1 $2 )
440   ;
442 struct-or-class
443   : STRUCT
444   | CLASS
445   ;
447 type
448   : typesimple SEMICOLON
449     ( ,$1 )
450  ;; named namespaces like "namespace XXX {"
451   | NAMESPACE symbol namespaceparts
452     (TYPE-TAG $2 $1 $3 nil )
453  ;; unnamed namespaces like "namespace {"
454   | NAMESPACE namespaceparts
455     (TYPE-TAG "unnamed" $1 $2 nil )
456  ;; David Engster: namespace alias like "namespace foo = bar;"
457   | NAMESPACE symbol EQUAL typeformbase SEMICOLON
458     (TYPE-TAG $2 $1 (list (TYPE-TAG (car $4) $1 nil nil)) nil :kind 'alias )
459   ;
461 ;; Klaus Berndl: We must parse "using namespace XXX" too
463 ;; Using is vaguely like an include statement in the named portions
464 ;; of the code.  We should probably specify a new token type for this.
466 using
467   : USING usingname SEMICOLON
468     (TAG (car $2) 'using :type ,$2 )
469   ;
471 ;; Jan Moringen: Differentiate between 'using' and 'using namespace'
472 ;; Adapted to creating type tags by EML.
473 usingname
474   : typeformbase
475     (TYPE-TAG (car $1) "class" nil nil :prototype t)
476   | NAMESPACE typeformbase
477     (TYPE-TAG (car $2) "namespace" nil nil :prototype t)
478   ;
480 template
481   : TEMPLATE template-specifier opt-friend template-definition
482     ( ,(semantic-c-reconstitute-template $4 ,$2) )
483   ;
485 opt-friend
486   : FRIEND
487   | ;;EMPTY
488   ;
490 opt-template-specifier
491   : template-specifier
492     ( ,$1 )
493   | ;;EMPTY
494     ( )
495   ;
497 template-specifier
498   : LESS template-specifier-types GREATER
499     ( ,$2 )
500   ;
502 template-specifier-types
503   : template-var template-specifier-type-list
504     ( ,(cons ,$1 ,$2 ) )
505   | ;;EMPTY
506   ;
508 template-specifier-type-list
509   : COMA template-specifier-types
510     ( ,$2 )
511   | ;;EMPTY
512     ( )
513   ;
515 ;; template-var
516 ;;   : template-type opt-stars opt-template-equal
517 ;;     ( ,(cons (concat (car $1) (make-string (car ,$2) ?*))
518 ;;              (cdr $1)))
519 ;;  ;; Klaus Berndl: for template-types the template-var can also be
520 ;;  ;; literals or constants.  Example: map<ClassX, ClassY, 10>
521 ;;  ;; map_size10_var; This parses also template<class T, 0> which is
522 ;;  ;; nonsense but who cares....
523 ;;   | string
524 ;;     ( $1 )
525 ;;   | number
526 ;;     ( $1 )
527 ;;   ;
529 template-var
530   :
531  ;; Klaus Berndl: The following handles all template-vars of
532  ;; template-definitions
533     template-type opt-template-equal
534     ( ,(cons (car $1) (cdr $1)) )
535  ;; Klaus Berndl: for template-types the template-var can also be
536  ;; literals or constants.
537  ;; Example: map<ClassX, ClassY, 10> map_size10_var; This parses also
538  ;; template<class T, 0> which is nonsense but who cares....
539   | string
540     ( $1 )
541   | number
542     ( $1 )
543  ;; Klaus Berndl: In template-types arguments can be any symbols with
544  ;; optional address-operator (&) and optional dereferencing operator
545  ;; (*).  Example map<ClassX, ClassY, *size_var_ptr> sized_map_var.
546   | opt-stars opt-ref namespace-symbol
547     ( ,$3 )
548  ;; Some code can compile down into a number, but starts out as an
549  ;; expression, such as "sizeof(a)", or (sizeof(a)/sizeof(b))
550   | semantic-list
551     ( $1 )
552   | SIZEOF semantic-list
553     ( $2 )
554   ;
556 opt-template-equal
557   : EQUAL symbol LESS template-specifier-types GREATER
558     ( $2 )
559   | EQUAL symbol
560     ( $2 )
561   | ;;EMPTY
562     ( )
563   ;
565 template-type
566   : CLASS symbol
567     (TYPE-TAG $2 "class" nil nil )
568   | STRUCT symbol
569     (TYPE-TAG $2 "struct" nil nil )
570  ;; TODO: Klaus Berndl: For the moment is is ok, that we parse the C++
571  ;; keyword typename as a class....
572   | TYPENAME symbol
573     (TYPE-TAG $2 "class" nil nil)
574  ;; Klaus Berndl: template-types can be all flavors of variable-args
575  ;; but here the argument is ignored, only the type stuff is needed.
576   | declmods typeformbase cv-declmods opt-stars
577     opt-ref variablearg-opt-name
578     (TYPE-TAG (car $2) nil nil nil
579               :constant-flag (if (member "const" (append $1 $3)) t nil)
580               :typemodifiers (delete "const" (append $1 $3))
581               :reference (car ,$5)
582               :pointer (car $4)
583               )
584   ;
586 template-definition
587   : type
588     ( ,$1 )
589   | var-or-fun
590     ( ,$1 )
591   ;
593 opt-stars
594   : STAR opt-starmod opt-stars
595     ( (1+ (car $3)) )
596   | ;;EMPTY
597     ( 0 )
598   ;
600 opt-starmod
601   : STARMOD opt-starmod
602     ( ,(cons (,car ,$1) $2) )
603   | ;;EMPTY
604     ()
605   ;
607 STARMOD
608   : CONST
609   ;
611 declmods
612   : DECLMOD declmods
613     ( ,(cons ,(car ,$1) $2 ) )
614   | DECLMOD
615     ( ,$1 )
616   | ;;EMPTY
617     ()
618   ;
620 DECLMOD
621   : EXTERN
622   | STATIC
623   | CVDECLMOD
624  ;; Klaus Berndl: IMHO signed and unsigned are not decl-modes but
625  ;; these are only valid for some buildin-types like short, int
626  ;; etc... whereas "real" declmods are valid for all types, buildin
627  ;; and user-defined!  SIGNED UNSIGNED
628   | INLINE
629   | REGISTER
630   | FRIEND
631  ;; Klaus Berndl: There can be a few cases where TYPENAME is not
632  ;; allowed in C++-syntax but better than not recognizing the allowed
633  ;; situations.
634   | TYPENAME
635   | METADECLMOD
636  ;; This is a hack in case we are in a class.
637   | VIRTUAL
638   ;
640 metadeclmod
641   : METADECLMOD
642     ()
643   | ;;EMPTY
644     ()
645   ;
647 CVDECLMOD
648   : CONST
649   | VOLATILE
650   ;
652 cv-declmods
653   : CVDECLMOD cv-declmods
654     ( ,(cons ,(car ,$1) $2 ) )
655   | CVDECLMOD
656     ( ,$1 )
657   | ;;EMPTY
658     ()
659   ;
661 METADECLMOD
662   : VIRTUAL
663   | MUTABLE
664   ;
666 ;; C++: A type can be modified into a reference by "&"
667 opt-ref
668   : AMPERSAND
669     ( 1 )
670   | ;;EMPTY
671     ( 0 )
672   ;
674 typeformbase
675   : typesimple
676     ( ,$1 )
677   | STRUCT symbol
678     (TYPE-TAG $2 $1 nil nil )
679   | UNION symbol
680     (TYPE-TAG $2 $1 nil nil )
681   | ENUM symbol
682     (TYPE-TAG $2 $1 nil nil )
683   | builtintype
684     ( ,$1 )
685   | symbol template-specifier
686     (TYPE-TAG $1 "class" nil nil :template-specifier $2)
687  ;;| namespace-symbol opt-stars opt-template-specifier
688  ;;| namespace-symbol opt-template-specifier
689   | namespace-symbol-for-typeformbase opt-template-specifier
690     (TYPE-TAG (car $1) "class" nil nil
691               :template-specifier $2)
692   | symbol
693     ( $1 )
694   ;
696 signedmod
697   : UNSIGNED
698   | SIGNED
699   ;
701 ;; Klaus Berndl: builtintype-types was builtintype
702 builtintype-types
703   : VOID
704   | CHAR
705  ;; Klaus Berndl: Added WCHAR
706   | WCHAR
707   | SHORT INT
708     ( (concat $1 " " $2) )
709   | SHORT
710   | INT
711   | LONG INT
712     ( (concat $1 " " $2) )
713   | FLOAT
714   | DOUBLE
715   | BOOL
716   | LONG DOUBLE
717     ( (concat $1 " " $2) )
718  ;; TODO: Klaus Berndl: Is there a long long, i think so?!
719   | LONG LONG
720     ( (concat $1 " " $2) )
721   | LONG
722   ;
724 builtintype
725   : signedmod builtintype-types
726     ( (concat (car $1) " " (car $2)) )
727   | builtintype-types
728     ( ,$1 )
729  ;; Klaus Berndl: unsigned is synonym for unsigned int and signed for
730  ;; signed int. To make this confusing stuff clear we add here the
731  ;; int.
732   | signedmod
733     ( (concat (car $1) " int") )
734   ;
736 ;; Klaus Berndl: This parses also nonsense like "const volatile int
737 ;; const volatile const const volatile a ..." but IMHO nobody writes
738 ;; such code. Normaly we shoud define a rule like typeformbase-mode
739 ;; which exactly defines the different allowed cases and combinations
740 ;; of declmods (minus the CVDECLMOD) typeformbase and cv-declmods so
741 ;; we could recognize more invalid code but IMHO this is not worth the
742 ;; effort...
743 codeblock-var-or-fun
744   : declmods typeformbase declmods
745     opt-ref var-or-func-decl
746     ( ,(semantic-c-reconstitute-token ,$5 $1 $2 ) )
747   ;
749 var-or-fun
750   : codeblock-var-or-fun
751     ( ,$1 )
752  ;; it is possible for a function to not have a type, and
753  ;; it is then assumed to be an int.  How annoying.
754  ;; In C++, this could be a constructor or a destructor.
755  ;; Even more annoying.  Only ever do this for regular
756  ;; top-level items.  Ignore this problem in code blocks
757  ;; so that we don't have to deal with regular code
758  ;; being erroneously converted into types.
759   | declmods var-or-func-decl
760     ( ,(semantic-c-reconstitute-token ,$2 $1 nil ) )
761   ;
763 var-or-func-decl
764   : func-decl
765     ( ,$1 )
766   | var-decl
767     ( ,$1 )
768   ;
770 func-decl
771   : opt-stars opt-class opt-destructor functionname
772     opt-template-specifier
773     opt-under-p
774     arg-list
775     opt-post-fcn-modifiers
776     opt-throw
777     opt-initializers
778     fun-or-proto-end
779     ( ,$4 'function
780           ;; Extra stuff goes in here.
781           ;; Continue with the stuff we found in
782           ;; this definition
783           $2 $3 $7 $9 $8 ,$1 ,$11 $5 ,$10)
784   | opt-stars opt-class opt-destructor functionname
785     opt-template-specifier
786     opt-under-p
787  ;; arg-list   - - ini this case, a try implies a fcn.
788     opt-post-fcn-modifiers
789     opt-throw
790     opt-initializers
791     fun-try-end
792     ( ,$4 'function
793           ;; Extra stuff goes in here.
794           ;; Continue with the stuff we found in
795           ;; this definition
796           $2 $3 nil $8 $7 ,$1 ,$10 $5 ,$9)
797   ;
799 var-decl
800   : varnamelist SEMICOLON
801     ( $1 'variable )
802   ;
804 opt-under-p
805   : UNDERP
806     ( nil )
807   | UNDERUNDERP
808     ( nil )
809   | ;;EMPTY
810   ;
812 ;; Klaus Berndl: symbol -> namespace-symbol
813 opt-initializers
814   : COLON namespace-symbol semantic-list opt-initializers
815   | COMA namespace-symbol semantic-list opt-initializers
816   | ;;EMPTY
817   ;
819 opt-post-fcn-modifiers
820   : post-fcn-modifiers opt-post-fcn-modifiers
821     ( ,(cons ,$1 $2) )
822   | ;;EMPTY
823     ( nil )
824   ;
826 post-fcn-modifiers
827   : REENTRANT
828   | CONST
829   ;
831 opt-throw
832   : THROW semantic-list
833     ( EXPAND $2 throw-exception-list )
834   | ;;EMPTY
835   ;
837 ;; Is this true?  I don't actually know.
838 throw-exception-list
839   : namespace-symbol COMA throw-exception-list
840     ( ,(cons (car $1) $3) )
841   | namespace-symbol RPAREN
842     ( ,$1 )
843   | symbol RPAREN
844     ( $1 )
845   | LPAREN throw-exception-list
846     ( ,$2 )
847   | RPAREN
848     (  )
849   ;
851 opt-bits
852   : COLON number
853     ( $2 )
854   | ;;EMPTY
855     ( nil )
856   ;
858 opt-array
859   : BRACK_BLCK opt-array
860  ;; Eventually we want to replace the 1 below with a size
861  ;; (if available)
862     ( (cons 1 (car ,$2) ) )
863   | ;;EMPTY
864     ( nil )
865   ;
867 opt-assign
868   : EQUAL expression
869     ( $2 )
870   | ;;EMPTY
871     ( nil )
872   ;
874 opt-restrict
875   : RESTRICT
876   | ;;EMPTY
877   ;
879 ;; Klaus Berndl: symbol -> namespace-symbol?! I think so. Can be that
880 ;; then also some invalid C++-syntax is parsed but this is better than
881 ;; not parsing valid syntax.
882 varname
883   : opt-stars opt-restrict namespace-symbol opt-bits opt-array 
884     ( ,$3 ,$1 ,$4 ,$5 )
887 ;; I should store more in this def, but leave it simple for now.
888 ;; Klaus Berndl: const and volatile can be written after the type!
889 variablearg
890   : declmods typeformbase cv-declmods opt-ref variablearg-opt-name
891     ( VARIABLE-TAG (list $5) $2 nil
892                    :constant-flag (if (member "const" (append $1 $3)) t nil)
893                    :typemodifiers (delete "const" (append $1 $3))
894                    :reference (car ,$4)
895                    )
896   ;
898 variablearg-opt-name
899   : varname
900     ( ,$1 )
901  ;; Klaus Berndl: This allows variableargs without a arg-name being
902  ;; parsed correct even if there several pointers (*)
903   | opt-stars
904     ( "" ,$1 nil nil nil )
905   ;
907 varname-opt-initializer
908   : semantic-list
909   | opt-assign
910   | ;; EMPTY
911   ;
913 varnamelist
914   : opt-ref varname varname-opt-initializer COMA varnamelist
915     ( ,(cons $2 $5) )
916   | opt-ref varname varname-opt-initializer
917     ( $2 )
918   ;
920 ;; Klaus Berndl: Is necessary to parse stuff like
921 ;;     class list_of_facts : public list<fact>, public entity
922 ;; and
923 ;;     list <shared_ptr<item> >::const_iterator l;
924 ;; Parses also invalid(?) and senseless(?) c++-syntax like
925 ;;     symbol<template-spec>::symbol1<template-spec1>::test_iterator
926 ;; but better parsing too much than to less
927 namespace-symbol
928   : symbol opt-template-specifier COLON COLON namespace-symbol
929     ( (concat $1 "::" (car $5)) )
930   | symbol opt-template-specifier
931     ( $1 )
932   ;
934 ;; Don't pull an optional template specifier at the end of the
935 ;; namespace symbol so that it can be picked up by the type.
936 namespace-symbol-for-typeformbase
937   : symbol opt-template-specifier COLON COLON namespace-symbol-for-typeformbase
938     ( (concat $1 "::" (car $5)) )
939   | symbol
940     ( $1 )
941   ;
942 ;; namespace-symbol
943 ;;   : symbol COLON COLON namespace-symbol
944 ;;     ( (concat $1 "::" (car $4)) )
945 ;;   | symbol
946 ;;     ( $1 )
947 ;;   ;
949 namespace-opt-class
950   : symbol COLON COLON namespace-opt-class
951     ( (concat $1 "::" (car $4)) )
952  ;; Klaus Berndl: We must recognize template-specifiers here so we can
953  ;; parse correctly the method-implementations of template-classes
954  ;; outside the template-class-declaration Example:
955  ;; TemplateClass1<T>::method_1(...)
956   | symbol opt-template-specifier COLON COLON
957     ( $1 )
958   ;
960 ;; Klaus Berndl: The opt-class of a func-decl must be able to
961 ;; recognize opt-classes with namespaces, e.g.
962 ;; Test1::Test2::classname::
963 opt-class
964   : namespace-opt-class
965     ( ,$1 )
966   | ;;EMPTY
967     ( nil )
968   ;
970 opt-destructor
971   : TILDE
972     ( t )
973   | ;;EMPTY
974     ( nil )
975   ;
977 arg-list
978   : PAREN_BLCK knr-arguments
979     ( ,$2 )
980   | PAREN_BLCK
981     (EXPANDFULL $1 arg-sub-list)
982   | VOID_BLCK
983     ( )
984   ;
986 knr-varnamelist
987   : varname COMA knr-varnamelist
988     ( ,(cons $1 $3) )
989   | varname
990     ( $1 )
991   ;
994 knr-one-variable-decl
995   : declmods typeformbase cv-declmods knr-varnamelist
996     ( VARIABLE-TAG (nreverse $4) $2 nil
997                    :constant-flag (if (member "const" (append $3)) t nil)
998                    :typemodifiers (delete "const" $3)
999                    )
1000   ;
1002 knr-arguments
1003   : knr-one-variable-decl SEMICOLON knr-arguments
1004     ( ,(append (semantic-expand-c-tag ,$1) ,$3) )
1005   | knr-one-variable-decl SEMICOLON
1006     ( ,(semantic-expand-c-tag ,$1) )
1007   ;
1009 arg-sub-list
1010   : variablearg
1011     ( ,$1 )
1012   | PERIOD PERIOD PERIOD RPAREN
1013     (VARIABLE-TAG "..." "vararg" nil)
1014   | COMA
1015     ( nil )
1016   | LPAREN
1017     ( nil )
1018   | RPAREN
1019     ( nil )
1020   ;
1022 operatorsym
1023   : LESS LESS EQUAL
1024     ( "<<=" )
1025   | GREATER GREATER EQUAL
1026     ( ">>=" )
1027   | LESS LESS
1028     ( "<<" )
1029   | GREATER GREATER
1030     ( ">>" )
1031   | EQUAL EQUAL
1032     ( "==" )
1033   | LESS EQUAL
1034     ( "<=" )
1035   | GREATER EQUAL
1036     ( ">=" )
1037   | BANG EQUAL
1038     ( "!=" )
1039   | PLUS EQUAL
1040     ( "+=" )
1041   | MINUS EQUAL
1042     ( "-=" )
1043   | STAR EQUAL
1044     ( "*=" )
1045   | DIVIDE EQUAL
1046     ( "/=" )
1047   | MOD EQUAL
1048     ( "%=" )
1049   | AMPERSAND EQUAL
1050     ( "&=" )
1051   | OR EQUAL
1052     ( "|=" )
1053   | MINUS GREATER STAR
1054     ( "->*" )
1055   | MINUS GREATER
1056     ( "->" )
1057   | PARENS
1058     ( "()" )
1059   | BRACKETS
1060     ( "[]" )
1061   | LESS
1062   | GREATER
1063   | STAR
1064   | PLUS PLUS
1065     ( "++" )
1066   | PLUS
1067   | MINUS MINUS
1068     ( "--" )
1069   | MINUS
1070   | AMPERSAND AMPERSAND
1071     ( "&&" )
1072   | AMPERSAND
1073   | OR OR
1074     ( "||" )
1075   | OR
1076   | DIVIDE
1077   | EQUAL
1078   | BANG
1079   | TILDE
1080   | MOD
1081   | COMA
1082  ;; HAT EQUAL seems to have a really unpleasant result and
1083  ;; breaks everything after it.  Leave it at the end, though it
1084  ;; doesn't seem to work.
1085   | HAT EQUAL
1086     ( "^=" )
1087   | HAT
1088   ;
1090 functionname
1091   : OPERATOR operatorsym
1092     ( ,$2 )
1093   | semantic-list
1094     ( EXPAND $1 function-pointer )
1095   | symbol
1096     ( $1 )
1097   ;
1099 function-pointer
1100   : LPAREN STAR symbol RPAREN
1101     ( (concat "*" $3) )
1102   ;
1104 fun-or-proto-end
1105   : SEMICOLON
1106     ( t )
1107   | semantic-list
1108     ( nil )
1109  ;; Here is an anoying feature of C++ pure virtual methods
1110   | EQUAL ZERO SEMICOLON
1111     ( :pure-virtual-flag )
1112   | fun-try-end
1113     ( nil )
1114   ;
1116 fun-try-end
1117   : TRY opt-initializers BRACE_BLCK fun-try-several-catches
1118     ( nil )
1119   ;
1121 fun-try-several-catches
1122   : CATCH PAREN_BLCK BRACE_BLCK fun-try-several-catches
1123     ( )
1124   | CATCH BRACE_BLCK fun-try-several-catches
1125     ( )
1126   | ;; EMPTY
1127     ( )
1128   ;
1130 type-cast
1131   : semantic-list
1132     ( EXPAND $1 type-cast-list )
1133   ;
1135 type-cast-list
1136   : open-paren typeformbase close-paren
1137   ;
1139 opt-stuff-after-symbol
1140   : PAREN_BLCK
1141   | BRACK_BLCK
1142   | ;; EMPTY
1143   ;
1145 multi-stage-dereference
1146   : namespace-symbol opt-stuff-after-symbol PERIOD multi-stage-dereference ;; method call
1147   | namespace-symbol opt-stuff-after-symbol MINUS GREATER multi-stage-dereference ;;method call
1148   | namespace-symbol opt-stuff-after-symbol
1149   ;
1151 string-seq
1152   : string string-seq
1153     ( (concat $1 (car $2)) )
1154   | string
1155     ( $1 )
1156   ;
1158 expr-start
1159   : MINUS
1160   | PLUS
1161   | STAR
1162   | AMPERSAND
1163   ;
1165 expr-binop
1166   : MINUS
1167   | PLUS
1168   | STAR
1169   | DIVIDE
1170   | AMPERSAND AMPERSAND
1171   | AMPERSAND
1172   | OR OR
1173   | OR
1174  ;; There are more.
1175   ;
1177 ;; Use expression for parsing only.  Don't actually return anything
1178 ;; for now.  Hopefully we can fix this later.
1179 expression
1180   : unaryexpression expr-binop unaryexpression
1181     ( (identity start) (identity end) )
1182   | unaryexpression
1183     ( (identity start) (identity end) )
1184   ;
1186 unaryexpression
1187   : number
1188   | multi-stage-dereference
1189   | NEW multi-stage-dereference
1190   | NEW builtintype-types semantic-list
1191  ;; Klaus Berndl: symbol -> namespace-symbol!
1192   | namespace-symbol
1193  ;; Klaus Berndl: C/C++ allows sequences of strings which are
1194  ;; concatenated by the precompiler to one string
1195   | string-seq
1196   | type-cast expression  ;; A cast to some other type
1197  ;; Casting the results of one expression to something else.
1198   | semantic-list expression
1199   | semantic-list
1200   | expr-start expression
1201   ;
1203 ;;; c.by ends here