idl.g
上传用户:afrynkmhm
上传日期:2007-01-06
资源大小:1262k
文件大小:11k
源码类别:

编译器/解释器

开发平台:

Others

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