diff options
author | John Hodge <tpg@mutabah.net> | 2015-04-01 23:01:56 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2015-04-01 23:01:56 +0800 |
commit | 6d3c3b87c19ee4404231eecdee874ada3cbfcc7e (patch) | |
tree | f2b5b045d90f99fd080e827b7d7d3ea16b3ec2c9 | |
parent | 60dae965fdb55552d9328356dcfa36c015c6ef83 (diff) | |
download | mrust-6d3c3b87c19ee4404231eecdee874ada3cbfcc7e.tar.gz |
Hacking up a BNF grammar
-rw-r--r-- | bnf/Makefile | 13 | ||||
-rw-r--r-- | bnf/rust.lex | 16 | ||||
-rw-r--r-- | bnf/rust.y | 131 |
3 files changed, 160 insertions, 0 deletions
diff --git a/bnf/Makefile b/bnf/Makefile new file mode 100644 index 00000000..f229d041 --- /dev/null +++ b/bnf/Makefile @@ -0,0 +1,13 @@ + +.SUFFIXES: + +test.bin: rust.tab.c rust.lex.c + gcc rust.tab.c rust.lex.c -o $@ + +rust.tab.c: rust.y + yacc -o $@ $< -d + +rust.lex.c: rust.lex + lex -o $@ $< + + diff --git a/bnf/rust.lex b/bnf/rust.lex new file mode 100644 index 00000000..a24e18de --- /dev/null +++ b/bnf/rust.lex @@ -0,0 +1,16 @@ +%{ +#include "rust.tab.h" +%} + +dec_digit [0-9] +ident_c [a-zA-Z_] + +%% + +{ident_c}({ident_c}|[0-9]) { yylval.text = strdup(yytext); return IDENT; } + +%% +int main() { + yylex(); + return 0; +} diff --git a/bnf/rust.y b/bnf/rust.y new file mode 100644 index 00000000..67b48978 --- /dev/null +++ b/bnf/rust.y @@ -0,0 +1,131 @@ +%token IDENT LIFETIME STRING INTEGER FLOAT +%token SUPER_ATTR SUB_ATTR +%token RWD_mod RWD_fn RWD_const RWD_static +%token RWD_where +%start crate + +%union { + char *text; +} + +%% + +/* +========================== +Root +========================== +*/ +crate : super_attrs module_body; + +super_attrs + : super_attrs super_attr + | ; + +module_body + : attrs item module_body + | ; + +attrs + : attrs attr + | ; + +super_attr : SUPER_ATTR meta_items ']' +attr : SUB_ATTR meta_items ']'; +meta_items: meta_item | meta_items ',' meta_item | ; +meta_item + : IDENT '(' meta_items ')' + | IDENT '=' STRING + | IDENT ; + + + +/* +========================== +Root Items +========================== +*/ +item + : module_def + | fn_def +/* | struct_def + | enum_def + | trait_def*/ + ; + +module_def + : RWD_mod IDENT '{' module_body '}' + | RWD_mod IDENT ';' + ; + +fn_def: RWD_fn IDENT generic_def '(' fn_def_args ')' fn_def_ret where_clause '{' code '}'; + +fn_def_ret + : '-' '>' type + | ; + +fn_def_args : fn_def_args fn_def_arg | ; +fn_def_arg : irrefutable_pattern ':' type; + +/* Generic paramters */ +generic_def : '<' generic_def_list '>' | ; +generic_def_list : generic_def_one | generic_def_list ',' generic_def_one | ; +generic_def_one + : IDENT '=' type ':' bounds + | IDENT '=' type + | IDENT ':' bounds + | IDENT ; + +where_clause: RWD_where where_clauses; +where_clauses + : where_clause_ent ',' where_clauses + | where_clause_ent; +where_clause_ent + : type ':' bounds; +bounds: bounds '+' bound | bound; +bound: LIFETIME | path; + +/* +========================================= +Paths +========================================= +*/ +path: 'p'; + +/* +========================================= +Types +========================================= +*/ +type: + 'k'; + +/* +========================================= +Patterns +========================================= +*/ +irrefutable_pattern + : tuple_pattern + | bind_pattern + | struct_pattern + ; + +tuple_pattern: '(' ')'; + +bind_pattern: IDENT; + +struct_pattern + : path '{' '}' + | path tuple_pattern + ; + +//refutable_pattern: ; + + +/* +========================================= +Expressions! +========================================= +*/ +code: + '!'; |