Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grammar Go::Grammar;
- token TOP {
- ^ <source_file>
- # $
- }
- token ws {
- [<[\ \t\r\n]>]*
- }
- token semi {
- ';'
- }
- token automatic_semicolon {
- [ [<?after <.identifier> >
- || <?after <.basic_lit> >
- || <?after 'break'>
- || <?after 'continue'>
- || <?after 'fallthrough'>
- || <?after 'return'>
- || <?after '++'>
- || <?after '--'>
- || <?after ')'>
- || <?after ']'>
- || <?after '}'>
- ] [\n | $$ | $] ]
- | <?before ')'>
- | <?before '}'>
- }
- ######################################################################
- ## Lexical elements
- #### Characters
- regex unicode_letter {
- <.alpha>
- }
- regex unicode_digit {
- <.digit>
- }
- #### Letters and digits
- regex letter {
- <unicode_letter> | '_'
- }
- regex decimal_digit {
- <.digit>
- }
- regex octal_digit {
- <[0..7]>
- }
- regex hex_digit {
- <.xdigit>
- }
- #### Comments
- #### Semicolons
- #### Identifiers
- token identifier {
- <.letter> [<.letter> | <.unicode_digit>]*
- }
- #### Keywords
- #### Operators and Delimiters
- #### Integer literals
- token int_lit {
- | <decimal_lit>
- | <octal_lit>
- | <hex_lit>
- }
- token decimal_lit {
- <[1..9]> <.decimal_digit>*
- }
- token octal_lit {
- 0 <.octal_digit>*
- }
- token hex_lit {
- 0x <.hex_digit>+
- }
- #### Floating-point literals
- token float_lit {
- | (<.decimals> '.' <.decimals>?) <exponent>?
- | <.decimals> <exponent>
- | '.' <.decimals> <exponent>?
- }
- token decimals {
- <.decimal_digit>+
- }
- token exponent {
- <[eE]> <[+-]>? <.decimals>
- }
- #### Imaginary literals
- token imaginary_lit {
- [<decimals> | <float_lit>] i
- }
- #### Character literals
- # TODO
- token char_lit {
- "'" ~ "'" <-[\']>*
- }
- # TODO
- token unicode_value {
- "'" "'"
- }
- # TODO
- token byte_value {
- "'" "'"
- }
- # TODO
- token octal_byte_value {
- "'" "'"
- }
- # TODO
- token hex_byte_value {
- "'" "'"
- }
- # TODO
- token little_u_value {
- "'" "'"
- }
- # TODO
- token bit_u_value {
- "'" "'"
- }
- # TODO
- token escaped_char {
- # '\\' <[abfnrtv\\\"\']>
- }
- #### String literals
- token string_lit {
- '"' ~ '"' <-[\"]>*
- }
- # TODO
- ######################################################################
- ## Types
- rule type {
- | <type_lit>
- | <type_name>
- | '(' <type> ')'
- }
- rule type_name {
- <qualified_ident>
- }
- rule type_lit {
- | <array_type>
- | <struct_type>
- | <pointer_type>
- | <function_type>
- | <interface_type>
- | <slice_type>
- | <map_type>
- | <channel_type>
- }
- #### Array types
- rule array_type {
- '[' <array_length> ']' <element_type>
- }
- rule array_length {
- <expression>
- }
- rule element_type {
- <type>
- }
- #### Slice types
- rule slice_type {
- '[' ']' <element_type>
- }
- #### Struct types
- rule struct_type {
- struct '{' [<field_decl> <.semi>]* '}'
- }
- rule field_decl {
- [<identifier_list> <type> | <anonymous_field>] <tag>?
- }
- rule anonymous_field {
- '*'? <type_name>
- }
- rule tag {
- <.string_lit>
- }
- #### Pointer types
- rule pointer_type {
- '*' <base_type>
- }
- rule base_type {
- <type>
- }
- #### Function types
- rule function_type {
- func <signature>
- }
- rule signature {
- <parameters> <result>?
- }
- rule result {
- <parameters> | <type>
- }
- rule parameters {
- '(' <parameter_list>? ','? ')'
- }
- rule parameter_list {
- <parameter_decl>+ % ','
- }
- rule parameter_decl {
- <identifier_list>? '...'? <type>
- }
- #### Interface types
- rule interface_type {
- interface '{' [<method_spec> <.semi>]* '}'
- }
- rule method_spec {
- | <method_name> <signature>
- | <interface_type_name>
- }
- rule method_name {
- <identifier>
- }
- rule interface_type_name {
- <type_name>
- }
- #### Map types
- rule map_type {
- map '[' <key_type> ']' <element_type>
- }
- rule key_type {
- <type>
- }
- #### Channel types
- rule channel_type {
- [
- | chan '<-'?
- | '<-' chan
- ] <element_type>
- }
- #### Blocks
- ######################################################################
- ## Declarations and scope
- rule declaration {
- | <const_decl>
- | <type_decl>
- | <var_decl>
- }
- rule top_level_decl {
- | <declaration>
- | <function_decl>
- | <method_decl>
- }
- #### Constant declarations
- rule const_decl {
- const [<const_spec> | '(' [<const_spec> <.semi>]* ')']
- }
- rule const_spec {
- <identifier_list> [<type>? '=' <expression_list>]
- }
- rule identifier_list {
- <identifier>+ % [',' <.ws>]
- }
- rule expression_list {
- <expression>+ % [',' <.ws>]
- }
- #### Type declarations
- rule type_decl {
- type [<type_spec> | '(' [<type_spec> <.semi>]* ')']
- }
- rule type_spec {
- <identifier> <type>
- }
- #### Variable declarations
- rule var_decl {
- var [<var_spec> | '(' [<var_spec> <.semi>]* ')']
- }
- rule var_spec {
- <identifier_list>
- [
- | <type> ['=' <expression_list>]?
- | '=' <expression_list>
- ]
- }
- #### Short variable declarations
- rule short_var_decl {
- <identifier_list> ':=' <expression_list>
- }
- #### Function declarations
- rule function_decl {
- func <function_name> <signature> <body>?
- }
- rule function_name {
- <identifier>
- }
- rule body {
- <block>
- }
- #### Method declarations
- rule method_decl {
- func <receiver> <method_name> <signature> <body>?
- }
- rule receiver {
- '(' <identifier>? '*'? <base_type_name> ')'
- }
- rule base_type_name {
- <identifier>
- }
- ######################################################################
- ## Expressions
- #### Operands
- rule operand {
- | <literal>
- | <qualified_ident>
- | <method_expr>
- | '(' <expression> ')'
- }
- rule literal {
- | <basic_lit>
- | <composite_lit>
- | <function_lit>
- }
- rule basic_lit {
- | <int_lit>
- | <float_lit>
- | <imaginary_lit>
- | <char_lit>
- | <string_lit>
- }
- #### Qualified identifiers
- rule qualified_ident {
- [<package_name> '.']? <identifier>
- }
- #### Composite literals
- rule composite_lit { <literal_type> <literal_value> }
- rule literal_type {
- | <struct_type>
- | <array_type>
- | '[...]' <element_type>
- | <slice_type>
- | <map_type>
- | <type_name>
- }
- rule literal_value { '{' [<element>* % ','] ','? '}' }
- rule element { [<key> ':']? <value> }
- rule key { <field_name> | <element_index> }
- rule field_name { <identifier> }
- rule element_index { <expression> }
- rule value { <expression> | <literal_value> }
- #### Function literals
- rule function_lit {
- <function_type> <body>
- }
- #### Primary expressions
- rule primary_expr {
- | <operand>
- | <conversion>
- | <builtin_call>
- | <primary_expr> <selector>
- | <primary_expr> <index>
- | <primary_expr> <slice>
- | <primary_expr> <type_assertion>
- | <primary_expr> <call>
- }
- #### Selectors
- rule selector {
- '.' <identifier>
- }
- #### Indexes
- rule index {
- '[' <expression> ']'
- }
- #### Slices
- rule slice {
- '[' <expression>? ':' <expression>? ']'
- }
- #### Type assertions
- rule type_assertion {
- '.' '(' <type> ')'
- }
- #### Calls
- rule call {
- '(' <argument_list>? ','? ')'
- }
- rule argument_list {
- <expression_list> '...'?
- }
- #### Operators
- rule expression {
- | <unary_expr>
- | <binary_expr>
- }
- rule binary_expr {
- <expression> <binary_op> <unary_expr>
- }
- rule unary_expr {
- | <primary_expr>
- | <unary_op> <unary_expr>
- }
- #### Operator precedence
- token binary_op {
- | <mul_op>
- | <add_op>
- | <rel_op>
- | '&&'
- | '||'
- }
- #proto token binary_op {*}
- #token binary_op:sym<||> { <sym> }
- #token binary_op:sym<&&> { <sym> }
- #token binary_op:sym<rel> { <rel_op> }
- #token binary_op:sym<add> { <add_op> }
- #token binary_op:sym<mul> { <mul_op> }
- proto token rel_op {*}
- token rel_op:sym<==> { <sym> }
- token rel_op:sym<!=> { <sym> }
- token rel_op:sym«<» { <sym> }
- token rel_op:sym«<=» { <sym> }
- token rel_op:sym«>» { <sym> }
- token rel_op:sym«>=» { <sym> }
- proto token add_op {*}
- token add_op:sym<+> { <sym> }
- token add_op:sym<-> { <sym> }
- token add_op:sym<|> { <sym> }
- token add_op:sym<^> { <sym> }
- proto token mul_op {*}
- token mul_op:sym<*> { <sym> }
- token mul_op:sym</> { <sym> }
- token mul_op:sym<%> { <sym> }
- token mul_op:sym«<<» { <sym> }
- token mul_op:sym«>>» { <sym> }
- token mul_op:sym<&> { <sym> }
- token mul_op:sym<&^> { <sym> }
- proto token unary_op {*}
- token unary_op:sym<+> { <sym> }
- token unary_op:sym<-> { <sym> }
- token unary_op:sym<!> { <sym> }
- token unary_op:sym<^> { <sym> }
- token unary_op:sym<*> { <sym> }
- token unary_op:sym<&> { <sym> }
- token unary_op:sym«<-» { <sym> }
- #### Method expressions
- rule method_expr {
- <receiver_type> '.' <method_name>
- }
- rule receiver_type {
- <type_name>
- | '(' '*' <type_name> ')'
- }
- #### Conversions
- rule conversion {
- <type> '(' <expression> ')'
- }
- ######################################################################
- ## Statements
- rule statement_list { <statement>* % <.semi> }
- rule statement {
- | <declaration>
- | <labeled_stmt>
- | <simple_stmt>
- | <keyword_stmt>
- | <if_stmt>
- | <block>
- }
- rule simple_stmt {
- | <expression_stmt>
- # | <send_stmt>
- | <incdec_stmt>
- | <assignment>
- | <short_var_decl>
- }
- # <keyword_stmt> is not in the Go spec
- proto rule keyword_stmt {*}
- #### Empty statements (handled above?)
- #### Labeled statements
- rule labeled_stmt { <label> ':' <statement> }
- rule label { <identifier> }
- #### Expression statements
- rule expression_stmt { <expression> }
- #### Send statements
- rule send_stmt { <channel> '<-' <expression> }
- rule channel { <expression> }
- #### IncDec statements
- rule incdec_stmt {
- | <inc_stmt>
- | <dec_stmt>
- }
- rule inc_stmt { <expression> '++' }
- rule dec_stmt { <expression> '--' }
- #### Assignments
- rule assignment {
- <expr_list> <assign_op> <expr_list>
- }
- #### If statements
- proto rule if_stmt {*}
- rule if_stmt:sym<if> {
- <sym> <opt_simple_stmt> <expression> <block>
- <else_stmt>?
- }
- proto rule else_stmt {*}
- rule else_stmt:sym<else> {
- <sym> [ <if_stmt> | <block> ]
- }
- rule opt_simple_stmt {
- [ [ <simple_stmt> | <?> ] <.semi> ]?
- }
- #### Switch statements
- rule keyword_stmt:sym<switch> { <sym> <opt_simple_stmt> [ <type_switch> | <expr_switch> ] }
- proto rule expr_case {*}
- rule expr_case:sym<case> { <sym> <expression_list> }
- rule expr_case:sym<default> { <sym> }
- rule expr_clause { <exprcase> ':' <statement_list>* }
- rule expr_switch {
- <expression>? '{' <expr_clause> '}'
- }
- proto rule type_case {*}
- rule type_case:sym<case> { <sym> <type_list> }
- rule type_case:sym<default> { <sym> }
- rule type_clause { <type_case> ':' <statement_list>* }
- rule type_guard { [ <identifier> ':=' ]? <primary_expr> '.(type)' }
- rule type_list { <type> ** ',' }
- rule type_switch {
- <type_guard> "{" { <type_clause> } "}"
- }
- #### Select statements
- rule keyword_stmt:sym<select> { <sym> <comm_select> }
- proto rule comm_case {*}
- rule comm_case:sym<case> { <sym> [ <send_stmt> | <recv_stmt> ] }
- rule comm_case:sym<default> { <sym> }
- rule expr_assign { <expression> [ ',' <expression> ]? [ '=' | ':=' ]}
- rule recv_expr { <expression> }
- rule recv_stmt { <expr_assign>? <recv_expr> }
- rule comm_clause { <comm_case> ':' <statement_list> }
- rule comm_select {
- '{' <comm_clause>* '}'
- }
- #### For statements
- rule keyword_stmt:sym<for> { <sym> [ <condition> | <for_clause> | <range_clause> ] <block> }
- rule condition { <expression> }
- rule init_stmt { [ <simple_stmt> | <?> ] }
- rule post_stmt { [ <simple_stmt> | <?> ] }
- rule for_clause { <init_stmt>? ';' <condition>? ';' <post_stmt>? }
- rule range_clause { <expr_assign> 'range' <expression> }
- #### Go statements
- rule keyword_stmt:sym<go> { <sym> <expression> }
- #### Return statements
- rule keyword_stmt:sym<return> { <sym> <expression_list>? }
- #### Break statements
- rule keyword_stmt:sym<break> { <sym> <label>? }
- #### Continue statements
- rule keyword_stmt:sym<continue> { <sym> <label>? }
- #### Goto statements
- rule keyword_stmt:sym<goto> { <sym> <label> }
- #### Fallthrough statements
- rule keyword_stmt:sym<fallthrough> { <sym> }
- #### Defer statements
- rule keyword_stmt:sym<defer> { <sym> <expression> }
- ######################################################################
- ## Packages
- #### Source file organization
- rule source_file {
- <package_clause> <.semi>
- <import_decls>
- <top_level_decls>
- }
- rule top_level_decls {
- [<top_level_decl> <.semi>]*
- }
- #### Package clause
- rule package_clause {
- package <package_name>
- }
- rule package_name {
- <identifier>
- }
- #### Import declarations
- rule import_decls {
- [<import_decl> <.semi>]*
- }
- rule import_decl {
- import <import_specs>
- }
- rule import_specs {
- | <import_spec>
- | '(' [<import_spec> <.semi>]* ')'
- }
- rule import_spec {
- ['.' | <package_name>]? <import_path>
- }
- rule import_path {
- <.string_lit>
- }
- #rule method_expr {
- # <receiver_type> '.' <method_name>
- #}
- #
- #rule receiver_type {
- # <type_name> | [ '(' '*' <type_name> ')' ]
- #}
- #
- #rule conversion {
- # <type> '(' <expression> ')'
- #}
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement