diff options
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | bnf/Makefile | 2 | ||||
-rw-r--r-- | bnf/rust.lex | 35 | ||||
-rw-r--r-- | bnf/rust.y | 77 |
4 files changed, 102 insertions, 16 deletions
@@ -5,3 +5,7 @@ /.obj *.swp /output + +/bnf/*.c +/bnf/*.h +/bnf/*.output diff --git a/bnf/Makefile b/bnf/Makefile index f229d041..6e08add6 100644 --- a/bnf/Makefile +++ b/bnf/Makefile @@ -5,7 +5,7 @@ test.bin: rust.tab.c rust.lex.c gcc rust.tab.c rust.lex.c -o $@ rust.tab.c: rust.y - yacc -o $@ $< -d + yacc -o $@ $< -d --debug --verbose rust.lex.c: rust.lex lex -o $@ $< diff --git a/bnf/rust.lex b/bnf/rust.lex index a24e18de..4382b451 100644 --- a/bnf/rust.lex +++ b/bnf/rust.lex @@ -1,5 +1,8 @@ %{ #include "rust.tab.h" +#include <stdio.h> +void yyerror(const char *s); +extern int yydebug; %} dec_digit [0-9] @@ -7,10 +10,38 @@ ident_c [a-zA-Z_] %% -{ident_c}({ident_c}|[0-9]) { yylval.text = strdup(yytext); return IDENT; } +[/][/][^/].*\n { yylineno += 1; } +[/][/][/].*\n { yylineno += 1; } // TODO: Handle /// by desugaring +\n { yylineno += 1; } +\r /* */ +[ \t] /* */ +mod { return RWD_mod; } +use { return RWD_use; } +static { return RWD_static; } +const { return RWD_const; } +fn { return RWD_fn; } +as { return RWD_as; } +mut { return RWD_mut; } + +:: { return DOUBLECOLON; } +#![[] { return SUPER_ATTR; } +: { return *yytext; } +; { return *yytext; } += { return *yytext; } + +{ident_c}({ident_c}|[0-9])* { yylval.text = strdup(yytext); return IDENT; } +. { fprintf(stderr, "invalid character '%c' on line %i\n", *yytext, yylineno); exit(1); } %% int main() { - yylex(); + yydebug = 1; + yyparse(); return 0; } +void yyerror(const char *s) { + fprintf(stderr, "yyerror(%s) line %i, token \n", s, yylineno); +} +int yywrap(void) { + printf("done\n"); + return 1; +} @@ -1,13 +1,17 @@ %token IDENT LIFETIME STRING INTEGER FLOAT %token SUPER_ATTR SUB_ATTR -%token RWD_mod RWD_fn RWD_const RWD_static -%token RWD_where +%token DOUBLECOLON +%token RWD_mod RWD_fn RWD_const RWD_static RWD_use +%token RWD_as RWD_mut RWD_where +%token RWD_self RWD_super %start crate %union { char *text; } +%debug + %% /* @@ -45,19 +49,22 @@ Root Items ========================== */ item - : module_def - | fn_def + : RWD_mod module_def + | RWD_fn fn_def + | RWD_use use_def + | RWD_static static_def + ; /* | struct_def | enum_def - | trait_def*/ - ; + | trait_def + ;*/ module_def - : RWD_mod IDENT '{' module_body '}' - | RWD_mod IDENT ';' + : IDENT '{' module_body '}' + | IDENT ';' ; -fn_def: RWD_fn IDENT generic_def '(' fn_def_args ')' fn_def_ret where_clause '{' code '}'; +fn_def: IDENT generic_def '(' fn_def_args ')' fn_def_ret where_clause '{' code '}'; fn_def_ret : '-' '>' type @@ -66,6 +73,25 @@ fn_def_ret fn_def_args : fn_def_args fn_def_arg | ; fn_def_arg : irrefutable_pattern ':' type; +use_def + : path RWD_as IDENT ';' + | path '*' ';' + | path '{' use_picks '}' ';' + | path ';' +/* | RWD_use error ';' */ + ; +use_picks + : use_picks ',' path_item + | path_item + ; +path_item: IDENT | RWD_self; + + +static_def + : IDENT ':' type '=' expr ';' + | RWD_mut IDENT ':' type '=' expr ';' + ; + /* Generic paramters */ generic_def : '<' generic_def_list '>' | ; generic_def_list : generic_def_one | generic_def_list ',' generic_def_one | ; @@ -89,15 +115,38 @@ bound: LIFETIME | path; Paths ========================================= */ -path: 'p'; +path + : path DOUBLECOLON IDENT + | IDENT; + +type_path + : ufcs_path DOUBLECOLON IDENT + | DOUBLECOLON type_path_segs + | type_path_segs + ; +ufcs_path: '<' type RWD_as type_path '>'; +type_path_segs + : type_path_segs DOUBLECOLON IDENT + | type_path_segs DOUBLECOLON IDENT '<' type_exprs '>' + | IDENT '<' type_exprs '>' + | IDENT + ; +type_exprs: type_exprs ',' type | type; /* ========================================= Types ========================================= */ -type: - 'k'; +type + : type_path + | '&' type + | '&' RWD_mut type + | '*' RWD_const type + | '*' RWD_mut type + | '[' type ']' + | '[' type ';' expr ']' + ; /* ========================================= @@ -128,4 +177,6 @@ Expressions! ========================================= */ code: - '!'; + expr; +expr: + '!' |