Advertisement
Guest User

Untitled

a guest
Jun 11th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.60 KB | None | 0 0
  1. grammar Go::Grammar;
  2.  
  3. token TOP {
  4. ^ <source_file>
  5. # $
  6. }
  7.  
  8. token ws {
  9. [<[\ \t\r\n]>]*
  10. }
  11.  
  12. token semi {
  13. ';'
  14. }
  15.  
  16. token automatic_semicolon {
  17. [ [<?after <.identifier> >
  18. || <?after <.basic_lit> >
  19. || <?after 'break'>
  20. || <?after 'continue'>
  21. || <?after 'fallthrough'>
  22. || <?after 'return'>
  23. || <?after '++'>
  24. || <?after '--'>
  25. || <?after ')'>
  26. || <?after ']'>
  27. || <?after '}'>
  28. ] [\n | $$ | $] ]
  29. | <?before ')'>
  30. | <?before '}'>
  31. }
  32.  
  33. ######################################################################
  34. ## Lexical elements
  35.  
  36. #### Characters
  37.  
  38. regex unicode_letter {
  39. <.alpha>
  40. }
  41.  
  42. regex unicode_digit {
  43. <.digit>
  44. }
  45.  
  46. #### Letters and digits
  47.  
  48. regex letter {
  49. <unicode_letter> | '_'
  50. }
  51.  
  52. regex decimal_digit {
  53. <.digit>
  54. }
  55.  
  56. regex octal_digit {
  57. <[0..7]>
  58. }
  59.  
  60. regex hex_digit {
  61. <.xdigit>
  62. }
  63.  
  64. #### Comments
  65. #### Semicolons
  66. #### Identifiers
  67.  
  68. token identifier {
  69. <.letter> [<.letter> | <.unicode_digit>]*
  70. }
  71.  
  72. #### Keywords
  73. #### Operators and Delimiters
  74. #### Integer literals
  75.  
  76. token int_lit {
  77. | <decimal_lit>
  78. | <octal_lit>
  79. | <hex_lit>
  80. }
  81.  
  82. token decimal_lit {
  83. <[1..9]> <.decimal_digit>*
  84. }
  85.  
  86. token octal_lit {
  87. 0 <.octal_digit>*
  88. }
  89.  
  90. token hex_lit {
  91. 0x <.hex_digit>+
  92. }
  93.  
  94. #### Floating-point literals
  95.  
  96. token float_lit {
  97. | (<.decimals> '.' <.decimals>?) <exponent>?
  98. | <.decimals> <exponent>
  99. | '.' <.decimals> <exponent>?
  100. }
  101.  
  102. token decimals {
  103. <.decimal_digit>+
  104. }
  105.  
  106. token exponent {
  107. <[eE]> <[+-]>? <.decimals>
  108. }
  109.  
  110. #### Imaginary literals
  111.  
  112. token imaginary_lit {
  113. [<decimals> | <float_lit>] i
  114. }
  115.  
  116. #### Character literals
  117.  
  118. # TODO
  119. token char_lit {
  120. "'" ~ "'" <-[\']>*
  121. }
  122.  
  123. # TODO
  124. token unicode_value {
  125. "'" "'"
  126. }
  127.  
  128. # TODO
  129. token byte_value {
  130. "'" "'"
  131. }
  132.  
  133. # TODO
  134. token octal_byte_value {
  135. "'" "'"
  136. }
  137.  
  138. # TODO
  139. token hex_byte_value {
  140. "'" "'"
  141. }
  142.  
  143. # TODO
  144. token little_u_value {
  145. "'" "'"
  146. }
  147.  
  148. # TODO
  149. token bit_u_value {
  150. "'" "'"
  151. }
  152.  
  153. # TODO
  154. token escaped_char {
  155. # '\\' <[abfnrtv\\\"\']>
  156. }
  157.  
  158. #### String literals
  159.  
  160. token string_lit {
  161. '"' ~ '"' <-[\"]>*
  162. }
  163.  
  164. # TODO
  165.  
  166. ######################################################################
  167. ## Types
  168.  
  169. rule type {
  170. | <type_lit>
  171. | <type_name>
  172. | '(' <type> ')'
  173. }
  174.  
  175. rule type_name {
  176. <qualified_ident>
  177. }
  178.  
  179. rule type_lit {
  180. | <array_type>
  181. | <struct_type>
  182. | <pointer_type>
  183. | <function_type>
  184. | <interface_type>
  185. | <slice_type>
  186. | <map_type>
  187. | <channel_type>
  188. }
  189.  
  190. #### Array types
  191.  
  192. rule array_type {
  193. '[' <array_length> ']' <element_type>
  194. }
  195.  
  196. rule array_length {
  197. <expression>
  198. }
  199.  
  200. rule element_type {
  201. <type>
  202. }
  203.  
  204. #### Slice types
  205.  
  206. rule slice_type {
  207. '[' ']' <element_type>
  208. }
  209.  
  210. #### Struct types
  211.  
  212. rule struct_type {
  213. struct '{' [<field_decl> <.semi>]* '}'
  214. }
  215.  
  216. rule field_decl {
  217. [<identifier_list> <type> | <anonymous_field>] <tag>?
  218. }
  219.  
  220. rule anonymous_field {
  221. '*'? <type_name>
  222. }
  223.  
  224. rule tag {
  225. <.string_lit>
  226. }
  227.  
  228. #### Pointer types
  229.  
  230. rule pointer_type {
  231. '*' <base_type>
  232. }
  233.  
  234. rule base_type {
  235. <type>
  236. }
  237.  
  238. #### Function types
  239.  
  240. rule function_type {
  241. func <signature>
  242. }
  243.  
  244. rule signature {
  245. <parameters> <result>?
  246. }
  247.  
  248. rule result {
  249. <parameters> | <type>
  250. }
  251.  
  252. rule parameters {
  253. '(' <parameter_list>? ','? ')'
  254. }
  255.  
  256. rule parameter_list {
  257. <parameter_decl>+ % ','
  258. }
  259.  
  260. rule parameter_decl {
  261. <identifier_list>? '...'? <type>
  262. }
  263.  
  264. #### Interface types
  265.  
  266. rule interface_type {
  267. interface '{' [<method_spec> <.semi>]* '}'
  268. }
  269.  
  270. rule method_spec {
  271. | <method_name> <signature>
  272. | <interface_type_name>
  273. }
  274.  
  275. rule method_name {
  276. <identifier>
  277. }
  278.  
  279. rule interface_type_name {
  280. <type_name>
  281. }
  282.  
  283. #### Map types
  284.  
  285. rule map_type {
  286. map '[' <key_type> ']' <element_type>
  287. }
  288.  
  289. rule key_type {
  290. <type>
  291. }
  292.  
  293. #### Channel types
  294.  
  295. rule channel_type {
  296. [
  297. | chan '<-'?
  298. | '<-' chan
  299. ] <element_type>
  300. }
  301.  
  302. #### Blocks
  303.  
  304.  
  305. ######################################################################
  306. ## Declarations and scope
  307.  
  308. rule declaration {
  309. | <const_decl>
  310. | <type_decl>
  311. | <var_decl>
  312. }
  313.  
  314. rule top_level_decl {
  315. | <declaration>
  316. | <function_decl>
  317. | <method_decl>
  318. }
  319.  
  320. #### Constant declarations
  321.  
  322. rule const_decl {
  323. const [<const_spec> | '(' [<const_spec> <.semi>]* ')']
  324. }
  325.  
  326. rule const_spec {
  327. <identifier_list> [<type>? '=' <expression_list>]
  328. }
  329.  
  330. rule identifier_list {
  331. <identifier>+ % [',' <.ws>]
  332. }
  333.  
  334. rule expression_list {
  335. <expression>+ % [',' <.ws>]
  336. }
  337.  
  338. #### Type declarations
  339.  
  340. rule type_decl {
  341. type [<type_spec> | '(' [<type_spec> <.semi>]* ')']
  342. }
  343.  
  344. rule type_spec {
  345. <identifier> <type>
  346. }
  347.  
  348. #### Variable declarations
  349.  
  350. rule var_decl {
  351. var [<var_spec> | '(' [<var_spec> <.semi>]* ')']
  352. }
  353.  
  354. rule var_spec {
  355. <identifier_list>
  356. [
  357. | <type> ['=' <expression_list>]?
  358. | '=' <expression_list>
  359. ]
  360. }
  361.  
  362. #### Short variable declarations
  363.  
  364. rule short_var_decl {
  365. <identifier_list> ':=' <expression_list>
  366. }
  367.  
  368. #### Function declarations
  369.  
  370. rule function_decl {
  371. func <function_name> <signature> <body>?
  372. }
  373.  
  374. rule function_name {
  375. <identifier>
  376. }
  377.  
  378. rule body {
  379. <block>
  380. }
  381.  
  382. #### Method declarations
  383.  
  384. rule method_decl {
  385. func <receiver> <method_name> <signature> <body>?
  386. }
  387.  
  388. rule receiver {
  389. '(' <identifier>? '*'? <base_type_name> ')'
  390. }
  391.  
  392. rule base_type_name {
  393. <identifier>
  394. }
  395.  
  396. ######################################################################
  397. ## Expressions
  398.  
  399. #### Operands
  400.  
  401. rule operand {
  402. | <literal>
  403. | <qualified_ident>
  404. | <method_expr>
  405. | '(' <expression> ')'
  406. }
  407.  
  408. rule literal {
  409. | <basic_lit>
  410. | <composite_lit>
  411. | <function_lit>
  412. }
  413.  
  414. rule basic_lit {
  415. | <int_lit>
  416. | <float_lit>
  417. | <imaginary_lit>
  418. | <char_lit>
  419. | <string_lit>
  420. }
  421.  
  422. #### Qualified identifiers
  423.  
  424. rule qualified_ident {
  425. [<package_name> '.']? <identifier>
  426. }
  427.  
  428. #### Composite literals
  429.  
  430. rule composite_lit { <literal_type> <literal_value> }
  431.  
  432. rule literal_type {
  433. | <struct_type>
  434. | <array_type>
  435. | '[...]' <element_type>
  436. | <slice_type>
  437. | <map_type>
  438. | <type_name>
  439. }
  440.  
  441. rule literal_value { '{' [<element>* % ','] ','? '}' }
  442. rule element { [<key> ':']? <value> }
  443. rule key { <field_name> | <element_index> }
  444. rule field_name { <identifier> }
  445. rule element_index { <expression> }
  446. rule value { <expression> | <literal_value> }
  447.  
  448. #### Function literals
  449.  
  450. rule function_lit {
  451. <function_type> <body>
  452. }
  453.  
  454. #### Primary expressions
  455.  
  456. rule primary_expr {
  457. | <operand>
  458. | <conversion>
  459. | <builtin_call>
  460. | <primary_expr> <selector>
  461. | <primary_expr> <index>
  462. | <primary_expr> <slice>
  463. | <primary_expr> <type_assertion>
  464. | <primary_expr> <call>
  465. }
  466.  
  467. #### Selectors
  468.  
  469. rule selector {
  470. '.' <identifier>
  471. }
  472.  
  473. #### Indexes
  474.  
  475. rule index {
  476. '[' <expression> ']'
  477. }
  478.  
  479. #### Slices
  480.  
  481. rule slice {
  482. '[' <expression>? ':' <expression>? ']'
  483. }
  484.  
  485. #### Type assertions
  486.  
  487. rule type_assertion {
  488. '.' '(' <type> ')'
  489. }
  490.  
  491. #### Calls
  492.  
  493. rule call {
  494. '(' <argument_list>? ','? ')'
  495. }
  496.  
  497. rule argument_list {
  498. <expression_list> '...'?
  499. }
  500.  
  501. #### Operators
  502.  
  503. rule expression {
  504. | <unary_expr>
  505. | <binary_expr>
  506. }
  507.  
  508. rule binary_expr {
  509. <expression> <binary_op> <unary_expr>
  510. }
  511.  
  512. rule unary_expr {
  513. | <primary_expr>
  514. | <unary_op> <unary_expr>
  515. }
  516.  
  517. #### Operator precedence
  518. token binary_op {
  519. | <mul_op>
  520. | <add_op>
  521. | <rel_op>
  522. | '&&'
  523. | '||'
  524. }
  525.  
  526. #proto token binary_op {*}
  527. #token binary_op:sym<||> { <sym> }
  528. #token binary_op:sym<&&> { <sym> }
  529. #token binary_op:sym<rel> { <rel_op> }
  530. #token binary_op:sym<add> { <add_op> }
  531. #token binary_op:sym<mul> { <mul_op> }
  532.  
  533. proto token rel_op {*}
  534. token rel_op:sym<==> { <sym> }
  535. token rel_op:sym<!=> { <sym> }
  536. token rel_op:sym«<» { <sym> }
  537. token rel_op:sym«<=» { <sym> }
  538. token rel_op:sym«>» { <sym> }
  539. token rel_op:sym«>=» { <sym> }
  540.  
  541. proto token add_op {*}
  542. token add_op:sym<+> { <sym> }
  543. token add_op:sym<-> { <sym> }
  544. token add_op:sym<|> { <sym> }
  545. token add_op:sym<^> { <sym> }
  546.  
  547. proto token mul_op {*}
  548. token mul_op:sym<*> { <sym> }
  549. token mul_op:sym</> { <sym> }
  550. token mul_op:sym<%> { <sym> }
  551. token mul_op:sym«<<» { <sym> }
  552. token mul_op:sym«>>» { <sym> }
  553. token mul_op:sym<&> { <sym> }
  554. token mul_op:sym<&^> { <sym> }
  555.  
  556. proto token unary_op {*}
  557. token unary_op:sym<+> { <sym> }
  558. token unary_op:sym<-> { <sym> }
  559. token unary_op:sym<!> { <sym> }
  560. token unary_op:sym<^> { <sym> }
  561. token unary_op:sym<*> { <sym> }
  562. token unary_op:sym<&> { <sym> }
  563. token unary_op:sym«<-» { <sym> }
  564.  
  565. #### Method expressions
  566.  
  567. rule method_expr {
  568. <receiver_type> '.' <method_name>
  569. }
  570.  
  571. rule receiver_type {
  572. <type_name>
  573. | '(' '*' <type_name> ')'
  574. }
  575.  
  576. #### Conversions
  577.  
  578. rule conversion {
  579. <type> '(' <expression> ')'
  580. }
  581.  
  582. ######################################################################
  583. ## Statements
  584.  
  585. rule statement_list { <statement>* % <.semi> }
  586.  
  587. rule statement {
  588. | <declaration>
  589. | <labeled_stmt>
  590. | <simple_stmt>
  591. | <keyword_stmt>
  592. | <if_stmt>
  593. | <block>
  594. }
  595.  
  596. rule simple_stmt {
  597. | <expression_stmt>
  598. # | <send_stmt>
  599. | <incdec_stmt>
  600. | <assignment>
  601. | <short_var_decl>
  602. }
  603.  
  604. # <keyword_stmt> is not in the Go spec
  605. proto rule keyword_stmt {*}
  606.  
  607. #### Empty statements (handled above?)
  608.  
  609. #### Labeled statements
  610.  
  611. rule labeled_stmt { <label> ':' <statement> }
  612. rule label { <identifier> }
  613.  
  614. #### Expression statements
  615.  
  616. rule expression_stmt { <expression> }
  617.  
  618. #### Send statements
  619.  
  620. rule send_stmt { <channel> '<-' <expression> }
  621. rule channel { <expression> }
  622.  
  623. #### IncDec statements
  624.  
  625. rule incdec_stmt {
  626. | <inc_stmt>
  627. | <dec_stmt>
  628. }
  629.  
  630. rule inc_stmt { <expression> '++' }
  631. rule dec_stmt { <expression> '--' }
  632.  
  633. #### Assignments
  634.  
  635. rule assignment {
  636. <expr_list> <assign_op> <expr_list>
  637. }
  638.  
  639. #### If statements
  640.  
  641. proto rule if_stmt {*}
  642. rule if_stmt:sym<if> {
  643. <sym> <opt_simple_stmt> <expression> <block>
  644. <else_stmt>?
  645. }
  646.  
  647. proto rule else_stmt {*}
  648. rule else_stmt:sym<else> {
  649. <sym> [ <if_stmt> | <block> ]
  650. }
  651.  
  652. rule opt_simple_stmt {
  653. [ [ <simple_stmt> | <?> ] <.semi> ]?
  654. }
  655.  
  656. #### Switch statements
  657. rule keyword_stmt:sym<switch> { <sym> <opt_simple_stmt> [ <type_switch> | <expr_switch> ] }
  658.  
  659. proto rule expr_case {*}
  660. rule expr_case:sym<case> { <sym> <expression_list> }
  661. rule expr_case:sym<default> { <sym> }
  662. rule expr_clause { <exprcase> ':' <statement_list>* }
  663. rule expr_switch {
  664. <expression>? '{' <expr_clause> '}'
  665. }
  666.  
  667. proto rule type_case {*}
  668. rule type_case:sym<case> { <sym> <type_list> }
  669. rule type_case:sym<default> { <sym> }
  670. rule type_clause { <type_case> ':' <statement_list>* }
  671. rule type_guard { [ <identifier> ':=' ]? <primary_expr> '.(type)' }
  672. rule type_list { <type> ** ',' }
  673. rule type_switch {
  674. <type_guard> "{" { <type_clause> } "}"
  675. }
  676.  
  677. #### Select statements
  678. rule keyword_stmt:sym<select> { <sym> <comm_select> }
  679.  
  680. proto rule comm_case {*}
  681. rule comm_case:sym<case> { <sym> [ <send_stmt> | <recv_stmt> ] }
  682. rule comm_case:sym<default> { <sym> }
  683. rule expr_assign { <expression> [ ',' <expression> ]? [ '=' | ':=' ]}
  684. rule recv_expr { <expression> }
  685. rule recv_stmt { <expr_assign>? <recv_expr> }
  686. rule comm_clause { <comm_case> ':' <statement_list> }
  687. rule comm_select {
  688. '{' <comm_clause>* '}'
  689. }
  690.  
  691. #### For statements
  692. rule keyword_stmt:sym<for> { <sym> [ <condition> | <for_clause> | <range_clause> ] <block> }
  693.  
  694. rule condition { <expression> }
  695. rule init_stmt { [ <simple_stmt> | <?> ] }
  696. rule post_stmt { [ <simple_stmt> | <?> ] }
  697. rule for_clause { <init_stmt>? ';' <condition>? ';' <post_stmt>? }
  698. rule range_clause { <expr_assign> 'range' <expression> }
  699.  
  700. #### Go statements
  701. rule keyword_stmt:sym<go> { <sym> <expression> }
  702. #### Return statements
  703. rule keyword_stmt:sym<return> { <sym> <expression_list>? }
  704. #### Break statements
  705. rule keyword_stmt:sym<break> { <sym> <label>? }
  706. #### Continue statements
  707. rule keyword_stmt:sym<continue> { <sym> <label>? }
  708. #### Goto statements
  709. rule keyword_stmt:sym<goto> { <sym> <label> }
  710. #### Fallthrough statements
  711. rule keyword_stmt:sym<fallthrough> { <sym> }
  712. #### Defer statements
  713. rule keyword_stmt:sym<defer> { <sym> <expression> }
  714.  
  715. ######################################################################
  716. ## Packages
  717.  
  718. #### Source file organization
  719.  
  720. rule source_file {
  721. <package_clause> <.semi>
  722. <import_decls>
  723. <top_level_decls>
  724. }
  725.  
  726. rule top_level_decls {
  727. [<top_level_decl> <.semi>]*
  728. }
  729.  
  730. #### Package clause
  731. rule package_clause {
  732. package <package_name>
  733. }
  734.  
  735. rule package_name {
  736. <identifier>
  737. }
  738.  
  739. #### Import declarations
  740.  
  741. rule import_decls {
  742. [<import_decl> <.semi>]*
  743. }
  744.  
  745. rule import_decl {
  746. import <import_specs>
  747. }
  748.  
  749. rule import_specs {
  750. | <import_spec>
  751. | '(' [<import_spec> <.semi>]* ')'
  752. }
  753.  
  754. rule import_spec {
  755. ['.' | <package_name>]? <import_path>
  756. }
  757.  
  758. rule import_path {
  759. <.string_lit>
  760. }
  761.  
  762. #rule method_expr {
  763. # <receiver_type> '.' <method_name>
  764. #}
  765. #
  766. #rule receiver_type {
  767. # <type_name> | [ '(' '*' <type_name> ')' ]
  768. #}
  769. #
  770. #rule conversion {
  771. # <type> '(' <expression> ')'
  772. #}
  773. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement