diff options
author | John Hodge <tpg@mutabah.net> | 2015-09-28 09:24:19 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2015-09-28 09:24:19 +0800 |
commit | f56b813d42ebb4629345f63e6ea2c4704bb28e0a (patch) | |
tree | 904720985530577af64306f8c9049bbea4cc6c63 | |
parent | be427e449c1d492050279311ccecc3de8b17e838 (diff) | |
download | mrust-f56b813d42ebb4629345f63e6ea2c4704bb28e0a.tar.gz |
BNF - Now parses libstd
-rw-r--r-- | bnf/rust.lex | 18 | ||||
-rw-r--r-- | bnf/rust.y | 73 | ||||
-rw-r--r-- | bnf/rust_expr.y_tree.h | 13 | ||||
-rw-r--r-- | bnf/rust_tts.y.h | 2 |
4 files changed, 81 insertions, 25 deletions
diff --git a/bnf/rust.lex b/bnf/rust.lex index 21080235..d98024ae 100644 --- a/bnf/rust.lex +++ b/bnf/rust.lex @@ -37,8 +37,7 @@ int_suffix ([ui](size|8|16|32|64))? %% -"//"[^/].*\n { } -"///".*\n { lvalp->DOC_COMMENT = new ::std::string(yytext+1, strlen(yytext)-2); return DOC_COMMENT; } +"//".*\n { if(yytext[2] == '/' && yytext[3] != '/') { lvalp->DOC_COMMENT = new ::std::string(yytext+1, strlen(yytext)-2); return DOC_COMMENT; } } "//!".*\n { lvalp->SUPER_DOC_COMMENT = new ::std::string(yytext+1, strlen(yytext)-2); return SUPER_DOC_COMMENT; } "/*" { handle_block_comment(); /* TODO: Handle doc comments */ } \n /* */ @@ -67,6 +66,7 @@ int_suffix ([ui](size|8|16|32|64))? "let" { return RWD_let; } "ref" { return RWD_ref; } +"move" { return RWD_move; } "box" { return RWD_box; } "self" { return RWD_self; } @@ -148,6 +148,7 @@ int_suffix ([ui](size|8|16|32|64))? [0-9]{dec_digit}*(f32|f64) { lvalp->FLOAT = strtod(yytext, NULL); return FLOAT; } [0-9]{dec_digit}*{int_suffix} { lvalp->INTEGER = strtoull(yytext, NULL, 0); return INTEGER; } 0x[0-9a-fA-F_]+{int_suffix} { lvalp->INTEGER = strtoull(yytext, NULL, 0); return INTEGER; } +0o[0-7]+{int_suffix} { lvalp->INTEGER = strtoull(yytext, NULL, 0); return INTEGER; } 0b[01_]+{int_suffix} { lvalp->INTEGER = strtoull(yytext, NULL, 0); return INTEGER; } {ident_c}({ident_c}|[0-9])*"!" { lvalp->MACRO = new ::std::string(yytext, 0, strlen(yytext)-1); return MACRO; } '{ident_c}{ident_c}* { lvalp->LIFETIME = new ::std::string(yytext, 1); return LIFETIME; } @@ -216,10 +217,16 @@ uint32_t parse_char_literal(const char *_s) { { case 'n': rv += '\n'; break; case 'r': rv += '\r'; break; + case 't': rv += '\t'; break; case '"': rv += '"'; break; case '0': rv += '\0'; break; case '\\': rv += '\\'; break; - case '\n': break; + case '\'': rv += '\''; break; + case '\n': + while( isblank(*s) ) + s ++; + s --; + break; case 'x': rv += (char)strtoul((const char*)(s+1), NULL, 16); s += 2; @@ -273,6 +280,7 @@ loop: for(; *s == '#'; s++) num_hash ++; assert(*s == '"'); + printf("handle_raw_string - num_hash=%i\n", num_hash); ::std::string rv; @@ -291,11 +299,15 @@ loop: if( i == num_hash ) { break; } + unput(c); // Didn't find enough, append to output rv += '"'; while(i--) rv += '#'; } + else if( c <= 0 ) { + break; + } else { rv += c; } @@ -31,7 +31,7 @@ %token DOUBLELT DOUBLEGT DOUBLELTEQUAL DOUBLEGTEQUAL %token RWD_mod RWD_fn RWD_const RWD_static RWD_use RWD_struct RWD_enum RWD_trait RWD_impl RWD_type %token RWD_as RWD_in RWD_mut RWD_ref RWD_pub RWD_where RWD_unsafe -%token RWD_let RWD_box +%token RWD_let RWD_box RWD_move %token RWD_self RWD_super %token RWD_match RWD_if RWD_while RWD_loop RWD_for RWD_else %token RWD_return RWD_break RWD_continue @@ -175,6 +175,7 @@ item | RWD_extern RWD_crate extern_crate { $$ = $3; } | RWD_pub RWD_extern RWD_crate extern_crate { $$ = $4; $$->set_pub(); } | MACRO IDENT tt_brace { $$ = new Macro(consume($1), consume($2), consume($3)); } + | MACRO IDENT tt_paren ';' { $$ = new Macro(consume($1), consume($2), consume($3)); } | MACRO tt_brace { $$ = new Macro(consume($1), consume($2)); } | MACRO tt_paren ';' { $$ = new Macro(consume($1), consume($2)); } ; @@ -191,7 +192,8 @@ vis_item ; /* Possibily unsafe visibility item */ unsafe_vis_item - : fn_qualifiers RWD_fn fn_def { $$ = $3; } + : RWD_fn fn_def { $$ = $2; } + | fn_qualifiers RWD_fn fn_def { $$ = $3; } | RWD_trait trait_def { $$ = $2; } ; unsafe_item @@ -213,6 +215,7 @@ extern_items ; extern_item : attrs opt_pub RWD_fn fn_def_hdr ';' { $$ = $4; if($2) $$->set_pub(); $$->add_attrs( consume($1) ); } + | attrs opt_pub RWD_static opt_mut IDENT ':' type ';' { $$ = new Global(); } ; module_def @@ -232,8 +235,20 @@ fn_def_ret | THINARROW '!' ; -fn_def_args: /* empty */ | fn_def_self | fn_def_self ',' fn_def_arg_list | fn_def_arg_list; -fn_def_args_PROTO: /* empty */ | fn_def_self | fn_def_self ',' fn_def_arg_list_PROTO | fn_def_arg_list_PROTO; +fn_def_args + : /* empty */ + | fn_def_self + | fn_def_self ',' fn_def_arg_list opt_comma + | fn_def_arg_list opt_comma + | fn_def_arg_list ',' TRIPLEDOT + ; +fn_def_args_PROTO + : /* empty */ + | fn_def_self + | fn_def_self ',' fn_def_arg_list_PROTO opt_comma + | fn_def_arg_list_PROTO opt_comma + | fn_def_arg_list_PROTO ',' TRIPLEDOT + ; fn_def_self : RWD_self @@ -256,8 +271,7 @@ fn_def_arg_PROTO ; fn_qualifiers - : - | RWD_extern extern_abi + : RWD_extern extern_abi | RWD_const | RWD_unsafe | RWD_unsafe RWD_const @@ -271,17 +285,18 @@ type_def use_def : RWD_self use_def_tail { $$ = new UseSet( Path(Path::TagSelf()), consume($2) ); } | RWD_self DOUBLECOLON use_path use_def_tail { $$ = new UseSet(/* TODO */); } - | RWD_super use_def_tail { $$ = new UseSet( Path(Path::TagSuper()), consume($2) ); } - | RWD_super DOUBLECOLON use_path use_def_tail { $$ = new UseSet(/* TODO */); } + | RWD_super super_chain use_def_tail { $$ = new UseSet( Path(Path::TagSuper()), consume($3) ); } + | RWD_super super_chain DOUBLECOLON use_path use_def_tail { $$ = new UseSet(/* TODO */); } | DOUBLECOLON use_path use_def_tail { $$ = new UseSet(/* TODO */); } | use_path use_def_tail { $$ = new UseSet(/* TODO */); } - | '{' use_picks '}' ';' { $$ = new UseSet( Path(Path::TagAbs()), consume($2) ); } - | DOUBLECOLON '{' use_picks '}' ';' { $$ = new UseSet( Path(Path::TagAbs()), consume($3) ); } + | '{' use_picks opt_comma '}' ';' { $$ = new UseSet( Path(Path::TagAbs()), consume($2) ); } + | DOUBLECOLON '{' use_picks opt_comma '}' ';' { $$ = new UseSet( Path(Path::TagAbs()), consume($3) ); } ; +super_chain: | super_chain DOUBLECOLON RWD_super; use_def_tail : RWD_as IDENT ';' { $$ = new UseItems(UseItems::TagRename(), consume($2) ); } | DOUBLECOLON '*' ';' { $$ = new UseItems(UseItems::TagWildcard()); } - | DOUBLECOLON '{' use_picks '}' ';' { $$ = new UseItems( consume($3) ); } + | DOUBLECOLON '{' use_picks opt_comma '}' ';' { $$ = new UseItems( consume($3) ); } | ';' { $$ = new UseItems(); } /* | RWD_use error ';' */ ; @@ -345,7 +360,9 @@ trait_item : RWD_type IDENT ';' | RWD_type IDENT ':' trait_bound_list ';' | RWD_type IDENT '=' type ';' + | opt_unsafe RWD_fn fn_def_hdr_PROTO ';' | opt_unsafe fn_qualifiers RWD_fn fn_def_hdr_PROTO ';' + | opt_unsafe RWD_fn fn_def_hdr_PROTO code | opt_unsafe fn_qualifiers RWD_fn fn_def_hdr_PROTO code ; @@ -360,6 +377,7 @@ impl_def_line impl_items: | impl_items attrs impl_item; impl_item : opt_pub opt_unsafe fn_qualifiers RWD_fn fn_def + | opt_pub opt_unsafe RWD_fn fn_def | opt_pub RWD_type generic_def IDENT '=' type ';' | MACRO tt_group_item ; @@ -466,8 +484,8 @@ type ; type_ele : type_path - | RWD_fn '(' type_list ')' fn_def_ret - | RWD_extern extern_abi RWD_fn '(' type_list ')' fn_def_ret + | opt_unsafe RWD_fn '(' fn_def_arg_list_PROTO ')' fn_def_ret + | opt_unsafe RWD_extern extern_abi RWD_fn '(' fn_def_arg_list_PROTO ')' fn_def_ret | '_' | '&' opt_lifetime type_ele | DOUBLEAMP opt_lifetime type_ele @@ -500,9 +518,14 @@ struct_pattern : expr_path '{' struct_pattern_items '}' | expr_path '(' pattern_list ')' ; -struct_pattern_item: IDENT | IDENT ':' pattern; +struct_pattern_item: IDENT | IDENT ':' pattern | DOUBLEDOT; struct_pattern_items: struct_pattern_items ',' struct_pattern_item | struct_pattern_item; +slice_pattern + : '[' pattern_list ']' + | '[' pattern_list ',' DOUBLEDOT ']' + ; + pattern : /*IDENT { /* maybe bind * / } |*/ IDENT '@' nonbind_pattern @@ -522,6 +545,7 @@ nonbind_pattern | tuple_pattern | value_pattern | value_pattern TRIPLEDOT value_pattern + | slice_pattern | '&' pattern | '&' RWD_mut pattern | DOUBLEAMP pattern @@ -530,6 +554,7 @@ nonbind_pattern value_pattern : expr_path | INTEGER + | '-' INTEGER | CHARLIT | STRING ; @@ -554,7 +579,7 @@ block_contents | block_lines tail_expr ; tail_expr - : expr + : expr_noblock | flow_control ; flow_control @@ -566,10 +591,15 @@ flow_control block_lines: | block_lines block_line; block_line : RWD_let let_binding ';' - | MACRO IDENT tt_brace + | MACRO IDENT tt_brace + | MACRO IDENT tt_paren ';' + | MACRO tt_brace | super_attr | attrs item | expr_blocks + | RWD_unsafe RWD_extern extern_abi RWD_fn fn_def + | RWD_unsafe RWD_fn fn_def + | block | stmt | LIFETIME ':' loop_block ; @@ -581,6 +611,7 @@ let_binding stmt : expr ';' + | ';' ; expr_list: expr_list ',' expr | expr | /* mt */; @@ -591,13 +622,21 @@ struct_literal_list | struct_literal_ent ; +expr + : '{' block_contents '}' + | expr_noblock + ; +expr_NOSTRLIT + : block + | expr_noblock_NOSTRLIT + ; + expr_blocks : RWD_match expr_NOSTRLIT '{' match_arms '}' { } | RWD_if if_block | RWD_unsafe '{' block_contents '}' { } | flow_control | loop_block - | block ; loop_block : RWD_loop '{' block_contents '}' { } diff --git a/bnf/rust_expr.y_tree.h b/bnf/rust_expr.y_tree.h index 82707da9..d0b4c876 100644 --- a/bnf/rust_expr.y_tree.h +++ b/bnf/rust_expr.y_tree.h @@ -1,4 +1,4 @@ -_(expr): _(expr_assign); +_(expr_noblock): _(expr_assign); _(expr_assign) : _(expr_0) assign_op _(expr_0) @@ -97,7 +97,7 @@ _(expr_fc) _(expr_value) : CHARLIT | INTEGER | FLOAT | STRING | expr_blocks - | expr_path '(' expr_list ')' { bnf_trace(context, "function call"); } + | expr_path '(' expr_list opt_comma ')' { bnf_trace(context, "function call"); } #ifndef SUFFIX_is__NOSTRLIT | expr_path '{' struct_literal_list '}' | expr_path '{' struct_literal_list ',' '}' @@ -111,6 +111,11 @@ _(expr_value) | '[' expr_list opt_comma ']' | '[' expr ';' expr ']' | MACRO tt_paren { bnf_trace(context, "Expr macro invocation"); } - | '|' closure_arg_list '|' closure_ret expr - | DOUBLEPIPE expr + | MACRO tt_square { bnf_trace(context, "Expr macro invocation"); } + | _(closure) + | RWD_move _(closure) + ; +_(closure) + : '|' closure_arg_list '|' closure_ret expr + | DOUBLEPIPE closure_ret expr ; diff --git a/bnf/rust_tts.y.h b/bnf/rust_tts.y.h index 366ffa3d..78d3d581 100644 --- a/bnf/rust_tts.y.h +++ b/bnf/rust_tts.y.h @@ -7,7 +7,7 @@ tt_tok | _T(FLOAT) | _C(',') | _C(';') | _C('_') | _T(RWD_self) | _T(RWD_super) | _T(RWD_mut) | _T(RWD_ref) | _T(RWD_let) | _T(RWD_where) | _T(RWD_pub) | _T(RWD_in) | _T(RWD_as) - | _T(RWD_for ) | _T(RWD_while) | _T(RWD_loop) | _T(RWD_if) | _T(RWD_else) | _T(RWD_match) | _T(RWD_box) + | _T(RWD_for ) | _T(RWD_while) | _T(RWD_loop) | _T(RWD_if) | _T(RWD_else) | _T(RWD_match) | _T(RWD_box) | _T(RWD_move) | _T(RWD_return) | _T(RWD_continue) | _T(RWD_break) | _T(RWD_impl) | _T(RWD_struct) | _T(RWD_enum) | _T(RWD_fn) | _T(RWD_type) | _T(RWD_static) | _T(RWD_const) | _T(RWD_trait) | _T(RWD_use) | _T(RWD_extern) | _T(RWD_crate) | _T(RWD_unsafe) |