Skip to content

Instantly share code, notes, and snippets.

@DanielKeep
Created September 4, 2015 13:02
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 DanielKeep/1d9f1868178857aa476c to your computer and use it in GitHub Desktop.
Save DanielKeep/1d9f1868178857aa476c to your computer and use it in GitHub Desktop.
Rust grammar WIP

Grammar

Based on libsyntax for commit 8d91bbd90.

crate_mod : inner_attributes mod_items ;

inner_attributes : inner_attribute* ;

inner_attribute : InnerDocComment
                | '#' '!' attribute
                ;

outer_attributes : outer_attribute* ;

outer_attribute : OuterDocComment
                | '#' attribute
                ;

attribute : '[' meta_item ']' ;

meta_item : NtMeta
          | Ident [ '=' String | '(' meta_seq ')' ]? ;

meta_seq : meta_item [ ',' meta_item ]* ','? ;

mod_items : item* ;

item : outer_attributes item_or_macro ;

item_or_macro : item_not_macro
              | TODO ;

item_not_macro : NtItem
               | visibility? [ item_use
                             | item_extern_crate
                             | item_extern_block
                             | item_static
                             | item_const
                             | item_trait
                             | item_impl
                             | item_fn
                             | item_mod
                             | item_type
                             | item_enum
                             | item_struct
                             | macro_item
                             ]
               ;

visibility : "pub"? ;

item_use : "use" view_path ';' ;

view_path : '::'? [ '{' path_list_items '}'
                  | Ident [ '::' Ident ]* [ '::' '{' path_list_items '}'
                                          | '::' '*'
                                          ]
                  | Ident [ '::' Ident ]* "as" Ident
                  ] ;

path_list_items : path_list_item [ ',' path_list_item ]* ','? ;

path_list_item : Ident | "self" ;

item_extern_crate : "extern" "crate" Ident [ "as" Ident ] ';' ;

item_extern_block : "extern" abi? '{' inner_attributes item_foreign* '}' ;

item_foreign : outer_attributes
               [ visibility? [ item_foreign_static | item_foreign_fn ] ] ;

item_foreign_static : "static" "mut"? Ident ':' type_sum ';' ;

item_foreign_fn : "fn" fn_header fn_decl_named where_clause ';' ;

item_static : "static" "mut"? item_const_binding ;

item_const_fn : "const" item_fn ;

item_const : "const" item_const_binding ;

item_const_binding : Ident ':' type_sum '=' expr ';' ;

item_trait : "unsafe"? "trait" Ident generics
             [ ':' type_param_bounds_bare ]?
             where_clause
             trait_items ;

trait_items : [ NtTraitItem
              | outer_attributes [ trait_type
                                 | trait_const
                                 | trait_fn
                                 ]
              ] ;

trait_type : "type" type_param ';' ;

trait_const : "const" Ident ':' type_sum [ '=' expr ] ';' ;

trait_fn : TODO fn_front_matter Ident generics fn_decl_with_self where_clause
           [ ';'
           | inner_attrs_and_block
           ] ;

item_impl : "unsafe"? "impl" 
            [ generics {?= '('} type_sum where_clause impl_body
            | generics '!'? type_sum
              [ "for" type_sum ]? where_clause impl_body
            | {?= '('} type_sum
              [ '..' '{' '}'
              | where_clause impl_body
              ]
            | '!'? type_sum
              [ "for"? '..' '{' '}' 
              | [ "for" type_sum ]? where_clause impl_body
              ]
            ] ;

impl_body : '{' inner_attributes impl_items '}' ;

impl_items : impl_item* ;

impl_item : outer_attributes visibility?
            [ impl_type
            | impl_const
            | impl_method
            ] ;

impl_type : "type" Ident '=' type_sum ';' ;

impl_const : "const" Ident ':' type_sum '=' expr ';' ;

impl_method : macro_item
            | fn_front_matter Ident generics fn_decl_with_self_names
              where_clause inner_attrs_and_block ;

item_fn : "unsafe"? [ "extern" abi? ]? "fn" TODO ;

item_mod : "mod" TODO ;

item_type : "type" TODO ;

item_enum : "enum" TODO ;

item_struct : "struct" TODO ;

macro_item : Ident '!' [ '(' tts ')' ';' | '{' tts '}' ] ;

fn_front_matter : [ "const" | unsafety? [ "extern" abi ]? ] "fn" ;

fn_header : Ident generics ;

fn_decl : fn_args_named ret_type ;

fn_decl_variadic : fn_args_named_variadic ret_type ;

// parse_fn_decl_with_self(parse_arg_general(true))
fn_decl_with_self_names : '('
                          [ // parser:4306
                          | [ self_arg ',' ]? 
                          ] ')' ;

self_arg : '&' "self"
         | '&' "mut" "self"
         | '&' Lifetime "self"
         | '&' Lifetime "mut" "self"
         | "mut"? "self" [ ':' type_sum ]?
         ;

// parse_fn_decl_with_self(parse_arg_general(false))
fn_decl_with_self : TODO ;

fn_args : TODO ;

fn_args_named : TODO ;

fn_args_variadic : TODO ;

fn_args_named_variadic : TODO ;

ret_type : TODO ;

unsafety : "unsafe"? ;

generics : TODO ;

where_clause : TODO ;

type_sum : TODO ;

type_param : Ident [ ':' type_param_bounds_modified ]? [ '=' type_sum ] ;

type_param_bounds_bare : type_param_bound_bare
                         [ '+' type_param_bound_bare ]* ;

type_param_bound_bare : TODO [ Lifetime | poly_trait_ref ]

type_param_bounds_modified : type_param_bound_modified
                             [ '+' type_param_bound_modified ]* ;

type_param_bound_modified : TODO [ Lifetime | '?'? poly_trait_ref ]

poly_trait_ref : late_bound_lifetime_defs ;

late_bound_lifetime_defs : [ "for" '<' lifetime_defs '>' ] ;

lifetime_defs : TODO ;

literal_maybe_minus : '-'? literal;

literal : "true" | "false" | token_literal ;

token_literal : NtExpr(ExprLit)
              | Byte | ByteString | RawByteString
              | Char | String | RawString
              | Float | Integer
              ;

Lexical Structure

comments : Comment | OuterDocComment | InnerDocComment ;
token : Lifetime
      | Byte | ByteString | RawByteString
      | Char | String | RawString
      | '_' | ModIdent | Ident | Float | Integer
      | ';' | ',' | '...' | '..' | '.'
      | '(' | ')' | '{' | '}' | '[' | ']'
      | '@' | '#' | '~' | '?' | '::' | ':' | '$'
      | '==' | '=>' | '='
      | '!=' | '!'
      | '<=' | '<<' | '<-' | '<'
      | '>=' | '>>' | '>'
      | '->' | '-'
      | '&&' | '&'
      | '||' | '|'
      | '+' | '*' | '/' | '^' | '%'

Byte : 'b\'' ByteBody '\'' Suffix? ;
ByteString : 'b"' ByteStringBody '"' Suffix? ;
RawByteString : 'br' RawByteStringQuote Suffix? ;

Char : '\'' CharBody '\'' Suffix? ;
String : '"' StringBody '"' Suffix? ;
RawString : 'r' RawStringQuote Suffix? ;

ByteBody : SimpleCharEscape | ByteEscape
         | [\x00-\x08\x0b\x0c\x0e-\x5b\x5d-\x7f] ;
ByteStringBody : ByteBody | LineContEscape ;
RawByteStringQuote : '#' RawByteStringQuote '#'
                   | '"' RawByteStringBody*? '"'
                   ;
RawByteStringBody : [\x00-\x7f] ;

CharBody : SimpleCharEscape | AsciiEscape | UnicodeEscape
         | ~[\\\t\n\r'] ;
StringBody : CharBody | LineContEscape ;
RawStringQuote : '#' RawStringQuote '#'
               | '"' RawStringBody*? '"'
               ;
RawStringBody : [\u{0}-\u{10FFFF}] ;

SimpleCharEscape : '\\' [ 'n' | 'r' | 't' | '\\' | '\'' | '"' | '0' ] ;
AsciiEscape : '\\x' '0'..'7' HexDigit ;
ByteEscape : '\\x' HexDigit HexDigit ;
UnicodeEscape : '\\u{' HexDigit+6 '}' ;
LineContEscape : '\\' '\r'? '\n' [ \n\t\r]* ;

Lifetime : '\'' [ Ident | "static" ] {?! '\''} ;

ModIdent : Ident {?= "::"};
Ident : {?! Keyword | '_'} IdentStart IdentContinue* ;

// http://www.unicode.org/reports/tr31/#D1
IdentStart : XID_Start ;
IdentContinue : XID_Continue ;

Float : DecDigit+ [ '.' [ DecDigit* FloatExponent? | {?! '.' | XID_Start} ]
                  | FloatExponent
                  ] Suffix? ;
FloatExponent : [ 'e' | 'E' ] [ '-' | '+' ]? DecDigit+ ;

Integer : [ '0b' [ BinDigit | '_' ]+
          | '0o' [ OctDigit | '_' ]+
          | '0x' [ HexDigit | '_' ]+
          | [ DecDigit | '_' ]+
          ] Suffix? ;

BinDigit : '0'..'1' ;
OctDigit : '0'..'7' ;
DecDigit : '0'..'9' ;
HexDigit : '0'..'9' | 'a'..'f' | 'A'..'F' ;

Suffix : Ident ;

Comment : TODO ;
OuterDocComment : TODO ;
InnerDocComment : TODO ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment