1// This file is part of PyANTLR. See LICENSE.txt for license 2// details..........Copyright (C) Wolfgang Haefelinger, 2004. 3// 4// $Id$ 5 6options { 7 language=Python; 8} 9 10class idl_p extends Parser; 11options { 12 exportVocab=IDL; 13 buildAST=true; 14} 15 16specification 17 : (definition)+ 18 ; 19 20 21definition 22 : ( type_dcl SEMI! 23 | const_dcl SEMI! 24 | except_dcl SEMI! 25 | interf SEMI! 26 | module SEMI! 27 ) 28 ; 29 30module 31 : "module" 32 identifier 33 LCURLY d:definition_list RCURLY 34 ; 35 36definition_list 37 : (definition)+ 38 ; 39 40 41interf 42 : "interface" 43 identifier 44 inheritance_spec 45 (interface_body)? 46 ; 47 48 49 50interface_body 51 : LCURLY! (export)* RCURLY! 52 ; 53 54export 55 : ( type_dcl SEMI 56 | const_dcl SEMI 57 | except_dcl SEMI 58 | attr_dcl SEMI 59 | op_dcl SEMI 60 ) 61 ; 62 63 64inheritance_spec 65 : COLON scoped_name_list 66 | 67 ; 68 69scoped_name_list 70 : scoped_name (COMMA scoped_name)* 71 ; 72 73 74scoped_name 75 : opt_scope_op identifier (SCOPEOP identifier)* 76 ; 77 78opt_scope_op 79 : SCOPEOP 80 | 81 ; 82 83const_dcl 84 : "const" const_type identifier ASSIGN const_exp 85 ; 86 87const_type 88 : integer_type 89 | char_type 90 | boolean_type 91 | floating_pt_type 92 | string_type 93 | scoped_name 94 ; 95 96 97/* EXPRESSIONS */ 98 99const_exp 100 : or_expr 101 ; 102 103or_expr 104 : xor_expr 105 ( or_op 106 xor_expr 107 )* 108 ; 109 110or_op 111 : OR 112 ; 113 114 115xor_expr 116 : and_expr 117 ( xor_op 118 and_expr 119 )* 120 ; 121 122xor_op 123 : XOR 124 ; 125 126 127and_expr 128 : shift_expr 129 ( and_op 130 shift_expr 131 )* 132 ; 133 134 135and_op 136 : AND 137 ; 138 139 140shift_expr 141 : add_expr 142 ( shift_op 143 add_expr 144 )* 145 ; 146 147shift_op 148 : LSHIFT 149 | RSHIFT 150 ; 151 152 153add_expr 154 : mult_expr 155 ( add_op 156 mult_expr 157 )* 158 ; 159 160add_op 161 : PLUS 162 | MINUS 163 ; 164 165 166mult_expr 167 : unary_expr 168 ( mult_op 169 unary_expr 170 )* 171 ; 172 173mult_op 174 : STAR 175 | DIV 176 | MOD 177 ; 178 179unary_expr 180 : unary_operator primary_expr 181 | primary_expr 182 ; 183 184unary_operator 185 : MINUS 186 | PLUS 187 | TILDE 188 ; 189 190// Node of type TPrimaryExp serves to avoid inf. recursion on tree parse 191primary_expr 192 : scoped_name 193 | literal 194 | LPAREN const_exp RPAREN 195 ; 196 197literal 198 : integer_literal 199 | string_literal 200 | character_literal 201 | floating_pt_literal 202 | boolean_literal 203 ; 204 205boolean_literal 206 : "TRUE" 207 | "FALSE" 208 ; 209 210positive_int_const 211 : const_exp 212 ; 213 214 215type_dcl 216 : "typedef" type_declarator 217 | struct_type 218 | union_type 219 | enum_type 220 | 221 | "native" simple_declarator 222 ; 223 224type_declarator 225 : type_spec declarators 226 ; 227 228type_spec 229 : simple_type_spec 230 | constr_type_spec 231 ; 232 233simple_type_spec 234 : base_type_spec 235 | template_type_spec 236 | scoped_name 237 ; 238 239base_type_spec 240 : integer_type 241 | char_type 242 | boolean_type 243 | floating_pt_type 244 | "octet" 245 | "any" 246 ; 247 248 249integer_type 250 : ("unsigned")? ("short" | "long") 251 ; 252 253char_type 254 : "char" 255 ; 256 257floating_pt_type 258 : "float" 259 | "double" 260 ; 261 262boolean_type 263 : "boolean" 264 ; 265 266template_type_spec 267 : sequence_type 268 | string_type 269 ; 270 271constr_type_spec 272 : struct_type 273 | union_type 274 | enum_type 275 ; 276 277declarators 278 : declarator (COMMA declarator)* 279 ; 280 281declarator 282 : identifier opt_fixed_array_size 283 ; 284 285opt_fixed_array_size 286 : (fixed_array_size)* 287 ; 288 289 290simple_declarator 291 : identifier 292 ; 293 294struct_type 295 : "struct" 296 identifier 297 LCURLY member_list RCURLY 298 ; 299 300member_list 301 : (member)+ 302 ; 303 304member 305 : type_spec declarators SEMI 306 ; 307 308union_type 309 : "union" 310 identifier 311 "switch" LPAREN switch_type_spec RPAREN 312 LCURLY switch_body RCURLY 313 ; 314 315switch_type_spec 316 : integer_type 317 | char_type 318 | boolean_type 319 | enum_type 320 | scoped_name 321 ; 322 323switch_body 324 : case_stmt_list 325 ; 326 327case_stmt_list 328 : (case_stmt)+ 329 ; 330 331case_stmt 332 : case_label_list element_spec SEMI 333 ; 334 335case_label_list 336 : (case_label)+ 337 ; 338 339 340case_label 341 : "case" const_exp COLON 342 | "default" COLON 343 ; 344 345element_spec 346 : type_spec declarator 347 ; 348 349enum_type 350 : "enum" identifier LCURLY enumerator_list RCURLY 351 ; 352 353enumerator_list 354 : enumerator (COMMA enumerator)* 355 ; 356 357enumerator 358 : identifier 359 ; 360 361sequence_type 362 : "sequence" 363 LT simple_type_spec opt_pos_int GT 364 ; 365 366opt_pos_int 367 : (COMMA positive_int_const)? 368 ; 369 370string_type 371 : "string" opt_pos_int_br 372 ; 373 374opt_pos_int_br 375 : (LT positive_int_const GT)? 376 ; 377 378fixed_array_size 379 : LBRACK positive_int_const RBRACK 380 ; 381 382 383attr_dcl 384 : ("readonly")? 385 "attribute" param_type_spec 386 simple_declarator_list 387 ; 388 389simple_declarator_list 390 : simple_declarator (COMMA simple_declarator)* 391 ; 392 393except_dcl 394 : "exception" 395 identifier 396 LCURLY opt_member_list RCURLY 397 ; 398 399 400opt_member_list 401 : (member)* 402 ; 403 404op_dcl 405 : op_attribute op_type_spec 406 identifier 407 parameter_dcls 408 opt_raises_expr c:opt_context_expr 409 ; 410 411opt_raises_expr 412 : (raises_expr)? 413 ; 414 415opt_context_expr 416 : (context_expr)? 417 ; 418 419op_attribute 420 : "oneway" 421 | 422 ; 423 424op_type_spec 425 : param_type_spec 426 | "void" 427 ; 428 429parameter_dcls 430 : LPAREN (param_dcl_list)? RPAREN! 431 ; 432 433param_dcl_list 434 : param_dcl (COMMA param_dcl)* 435 ; 436 437param_dcl 438 : param_attribute param_type_spec simple_declarator 439 ; 440 441param_attribute 442 : "in" 443 | "out" 444 | "inout" 445 ; 446 447raises_expr 448 : "raises" LPAREN scoped_name_list RPAREN 449 ; 450 451context_expr 452 : "context" LPAREN string_literal_list RPAREN 453 ; 454 455string_literal_list 456 : string_literal (COMMA! string_literal)* 457 ; 458 459param_type_spec 460 : base_type_spec 461 | string_type 462 | scoped_name 463 ; 464 465integer_literal 466 : INT 467 | OCTAL 468 | HEX 469 ; 470 471string_literal 472 : (STRING_LITERAL)+ 473 ; 474 475character_literal 476 : CHAR_LITERAL 477 ; 478 479floating_pt_literal 480 : f:FLOAT 481 ; 482 483identifier 484 : IDENT 485 ; 486 487 488/* IDL LEXICAL RULES */ 489class idl_l extends Lexer; 490options { 491 exportVocab=IDL; 492 k=4; 493} 494 495SEMI 496options { 497 paraphrase = ";"; 498} 499 : ';' 500 ; 501 502QUESTION 503options { 504 paraphrase = "?"; 505} 506 : '?' 507 ; 508 509LPAREN 510options { 511 paraphrase = "("; 512} 513 : '(' 514 ; 515 516RPAREN 517options { 518 paraphrase = ")"; 519} 520 : ')' 521 ; 522 523LBRACK 524options { 525 paraphrase = "["; 526} 527 : '[' 528 ; 529 530RBRACK 531options { 532 paraphrase = "]"; 533} 534 : ']' 535 ; 536 537LCURLY 538options { 539 paraphrase = "{"; 540} 541 : '{' 542 ; 543 544RCURLY 545options { 546 paraphrase = "}"; 547} 548 : '}' 549 ; 550 551OR 552options { 553 paraphrase = "|"; 554} 555 : '|' 556 ; 557 558XOR 559options { 560 paraphrase = "^"; 561} 562 : '^' 563 ; 564 565AND 566options { 567 paraphrase = "&"; 568} 569 : '&' 570 ; 571 572COLON 573options { 574 paraphrase = ":"; 575} 576 : ':' 577 ; 578 579COMMA 580options { 581 paraphrase = ","; 582} 583 : ',' 584 ; 585 586DOT 587options { 588 paraphrase = "."; 589} 590 : '.' 591 ; 592 593ASSIGN 594options { 595 paraphrase = "="; 596} 597 : '=' 598 ; 599 600NOT 601options { 602 paraphrase = "!"; 603} 604 : '!' 605 ; 606 607LT 608options { 609 paraphrase = "<"; 610} 611 : '<' 612 ; 613 614LSHIFT 615options { 616 paraphrase = "<<"; 617} 618 : "<<" 619 ; 620 621GT 622options { 623 paraphrase = ">"; 624} 625 : '>' 626 ; 627 628RSHIFT 629options { 630 paraphrase = ">>"; 631} 632 : ">>" 633 ; 634 635DIV 636options { 637 paraphrase = "/"; 638} 639 : '/' 640 ; 641 642PLUS 643options { 644 paraphrase = "+"; 645} 646 : '+' 647 ; 648 649MINUS 650options { 651 paraphrase = "-"; 652} 653 : '-' 654 ; 655 656TILDE 657options { 658 paraphrase = "~"; 659} 660 : '~' 661 ; 662 663STAR 664options { 665 paraphrase = "*"; 666} 667 : '*' 668 ; 669 670MOD 671options { 672 paraphrase = "%"; 673} 674 : '%' 675 ; 676 677 678SCOPEOP 679options { 680 paraphrase = "::"; 681} 682 : "::" 683 ; 684 685WS 686options { 687 paraphrase = "white space"; 688} 689 : (' ' 690 | '\t' 691 | '\n' { $newline; } 692 | '\r') 693 { $setType(SKIP); } 694 ; 695 696 697PREPROC_DIRECTIVE 698options { 699 paraphrase = "a preprocessor directive"; 700} 701 702 : 703 '#' 704 (~'\n')* '\n' 705 { $setType(SKIP); } 706 ; 707 708 709SL_COMMENT 710options { 711 paraphrase = "a comment"; 712} 713 714 : 715 "//" 716 (~'\n')* '\n' 717 { 718 $setType(SKIP) 719 $newline() 720 } 721 ; 722 723ML_COMMENT 724options { 725 paraphrase = "a comment"; 726} 727 : 728 "/*" 729 ( 730 STRING_LITERAL 731 | CHAR_LITERAL 732 | '\n' { $newline; } 733 | '*' ~'/' 734 | ~'*' 735 )* 736 "*/" 737 { $setType(SKIP); } 738 ; 739 740CHAR_LITERAL 741options { 742 paraphrase = "a character literal"; 743} 744 : 745 '\'' 746 ( ESC | ~'\'' ) 747 '\'' 748 ; 749 750STRING_LITERAL 751options { 752 paraphrase = "a string literal"; 753} 754 : 755 '"' 756 (ESC|~'"')* 757 '"' 758 ; 759 760 761protected 762ESC 763options { 764 paraphrase = "an escape sequence"; 765} 766 : '\\' 767 ( 'n' 768 | 't' 769 | 'v' 770 | 'b' 771 | 'r' 772 | 'f' 773 | 'a' 774 | '\\' 775 | '?' 776 | '\'' 777 | '"' 778 | ('0' | '1' | '2' | '3') 779 ( 780 /* Since a digit can occur in a string literal, 781 * which can follow an ESC reference, ANTLR 782 * does not know if you want to match the digit 783 * here (greedy) or in string literal. 784 * The same applies for the next two decisions 785 * with the warnWhenFollowAmbig option. 786 */ 787 options { 788 warnWhenFollowAmbig = false; 789 } 790 : OCTDIGIT 791 ( 792 options { 793 warnWhenFollowAmbig = false; 794 } 795 : OCTDIGIT 796 )? 797 )? 798 | 'x' HEXDIGIT 799 ( 800 options { 801 warnWhenFollowAmbig = false; 802 } 803 : HEXDIGIT 804 )? 805 ) 806 ; 807 808protected 809VOCAB 810options { 811 paraphrase = "an escaped character value"; 812} 813 : '\3'..'\377' 814 ; 815 816protected 817DIGIT 818options { 819 paraphrase = "a digit"; 820} 821 : '0'..'9' 822 ; 823 824protected 825OCTDIGIT 826options { 827 paraphrase = "an octal digit"; 828} 829 : '0'..'7' 830 ; 831 832protected 833HEXDIGIT 834options { 835 paraphrase = "a hexadecimal digit"; 836} 837 : ('0'..'9' | 'a'..'f' | 'A'..'F') 838 ; 839 840 841/* octal literals are detected by checkOctal */ 842 843HEX 844options { 845 paraphrase = "a hexadecimal value value"; 846} 847 848 : ("0x" | "0X") (HEXDIGIT)+ 849 ; 850 851INT 852options { 853 paraphrase = "an integer value"; 854} 855 : (DIGIT)+ // base-10 856 ( '.' (DIGIT)* {$setType(FLOAT);} 857 (('e' | 'E') ('+' | '-')? (DIGIT)+)? 858 | ('e' | 'E') ('+' | '-')? (DIGIT)+ {$setType(FLOAT);} 859 )? 860 ; 861 862FLOAT 863options { 864 paraphrase = "an floating point value"; 865} 866 867 : '.' (DIGIT)+ (('e' | 'E') ('+' | '-')? (DIGIT)+)? 868 ; 869 870IDENT 871options { 872 testLiterals = true; 873 paraphrase = "an identifer"; 874} 875 876 : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* 877 ; 878