Skip to content

Instantly share code, notes, and snippets.

@chai2010
Forked from grihabor/golang.lark
Created March 24, 2021 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chai2010/767d5aa3348616e46a24ebbc4ad9ed46 to your computer and use it in GitHub Desktop.
Save chai2010/767d5aa3348616e46a24ebbc4ad9ed46 to your computer and use it in GitHub Desktop.
Lark ebnf grammar for golang
go_file: decl_package [imports] ([decl_type] [decl_func])*
decl_package: "package" ident
imports: "import" single_import
| "import" multiple_imports
single_import: ["("] ESCAPED_STRING [")"]
multiple_imports: "(" ESCAPED_STRING+ ")"
stmt: stmt_var
| stmt_for
| stmt_if
| stmt_switch
| stmt_binary
| stmt_return
| stmt_block
| expr
stmt_block: "{" stmt* "}"
expr_multiple: expr ["," expr]*
stmt_return: "return" expr_multiple
stmt_binary: expr_multiple stmt_binary_op expr_multiple
stmt_switch: "switch" stmt "{" expr_switch_case* "}"
expr_switch_case: "case" type ":" stmt*
| "case" expr ":" stmt*
| "default" ":" stmt*
stmt_var: "var" ident_typed
| "var" ident [type] "\:=" expr
stmt_for: stmt_for_regular | stmt_for_range
stmt_for_regular: "for" [stmt_binary [";" expr [";" expr]]] stmt_block
stmt_for_range: "for" expr_multiple ":=" "range" expr stmt_block
stmt_if: "if" [expr ";"] expr stmt_block ["else" stmt_block]
expr: ident
| expr_field
| expr_binary
| expr_unary
| expr_func_call
| expr_make_call
| expr_braces
| expr_index
| expr_type_cast
| expr_lit_string
| expr_lit_number
| expr_lit_nil
| expr_lit_type
| expr_lit_func
expr_field: expr "." expr
expr_lit_func: "func" func_arguments [type_func_return] stmt_block
expr_index: expr "[" expr "]"
HEX: /0x[0-9a-f]+/
expr_lit_number: HEX | NUMBER
expr_lit_type: type_inline "{" (expr_lit_type_fields | expr_lit_type_fields_named) "}"
expr_lit_type_fields: expr_lit_type_field ["," expr_lit_type_field]* [","]
expr_lit_type_fields_named: expr_lit_type_field_named ["," expr_lit_type_field_named]* [","]
expr_lit_type_field: expr
expr_lit_type_field_named: ident ":" expr
expr_lit_string: ESCAPED_STRING
expr_lit_nil: "nil"
expr_type_cast: expr "." "(" type ")"
expr_binary: expr expr_binary_op expr
expr_unary: expr_unary_prefix_op expr
| expr expr_unary_suffix_op
expr_binary_op: "+" | "-" | "*" | "/" | "&&" | "||" | "!=" | "==" | ">=" | "<=" | "%"
expr_unary_prefix_op: "!" | "&" | "*"
expr_unary_suffix_op: "..."
stmt_binary_op: "<-" | "->" | ":=" | "=" | "+=" | "-=" | "*=" | "/=" | "^="
expr_func_call: expr "(" [expr ("," expr)*] ")"
expr_make_call: "make" "(" type "," expr ")"
| "make" "(" type "," expr "," expr ")"
expr_braces: "(" expr ")"
decl_type: decl_struct
| decl_interface
| decl_typedef
decl_struct: "type" ident "struct" "{" struct_item* "}"
struct_item: ident_typed
| type_named
ident: CNAME
ident_typed: ident type
type_named: ident
| ident_scoped
| type_ptr_named
type: type_inline
| type_ptr
| type_chan
| type_lit
type_lit: "string" | "int" | "float" | "byte"
type_inline: ident
| ident_scoped
| type_slice
| type_array
| type_map
| type_struct_inline
ident_scoped: ident "." ident
type_slice: "[" "]" type
type_array: "[" INT "]" type
type_map: "map" "[" type "]" type
type_ptr: "*" type
type_ptr_named: "*" type_named
type_chan: "chan" type
| "chan" "<-" type
| "chan" "->" type
type_struct_inline: "struct" "{" struct_item* "}"
decl_interface: "type" ident "interface" "{" interface_item* "}"
interface_item: func_name func_arguments [type_func_return]
func_name: ident
func_arguments: "(" [func_argument ("," func_argument)*] ")"
func_argument: ident_typed | type
decl_func: "func" [func_receiever] func_name func_arguments [type_func_return] stmt_block
func_receiever: "(" ident type_receiver ")"
type_receiver: ident
| "*" ident
type_func_return: type
| func_arguments
decl_typedef: "type" ident type
CPP_COMMENT: /\/\/[^\n]*/x
%import common.CNAME
%import common.WS
%import common.ESCAPED_STRING
%import common.NUMBER
%import common.INT
%ignore WS
%ignore CPP_COMMENT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment