summaryrefslogtreecommitdiff
path: root/bnf
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2015-04-15 14:59:04 +0800
committerJohn Hodge <tpg@mutabah.net>2015-04-15 14:59:04 +0800
commit691d304e45b8e629cf2b086613accf048b7f8c48 (patch)
tree9d999e3ce8f0ac47f0cc937965af5ef335fe2a58 /bnf
parent6852317fdcab29da0f125d4a6642350e4ac360d6 (diff)
downloadmrust-691d304e45b8e629cf2b086613accf048b7f8c48.tar.gz
BNF - Now parses sample down to '>>' -> '>' '>' decomposition
Diffstat (limited to 'bnf')
-rw-r--r--bnf/Makefile2
-rw-r--r--bnf/rust.lex12
-rw-r--r--bnf/rust.y85
3 files changed, 84 insertions, 15 deletions
diff --git a/bnf/Makefile b/bnf/Makefile
index d9058a7b..75465648 100644
--- a/bnf/Makefile
+++ b/bnf/Makefile
@@ -10,7 +10,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 --debug --verbose
+ yacc -o $@ $< -d --verbose
rust.lex.c: rust.lex
lex -o $@ $<
diff --git a/bnf/rust.lex b/bnf/rust.lex
index 71017b64..dd8406de 100644
--- a/bnf/rust.lex
+++ b/bnf/rust.lex
@@ -32,6 +32,8 @@ ident_c [a-zA-Z_]
"mut" { return RWD_mut; }
"pub" { return RWD_pub; }
+"let" { return RWD_let; }
+
"self" { return RWD_self; }
"super" { return RWD_super; }
@@ -50,7 +52,13 @@ ident_c [a-zA-Z_]
"==" { return DOUBLEEQUAL; }
"!=" { return EXCLAMEQUAL; }
+"&&" { return DOUBLEAMP; }
+"||" { return DOUBLEPIPE; }
+"<<" { return DOUBLELT; }
+">>" { return DOUBLEGT; }
+
"&" { return *yytext; }
+"|" { return *yytext; }
"!" { return *yytext; }
"." { return *yytext; }
":" { return *yytext; }
@@ -63,8 +71,10 @@ ident_c [a-zA-Z_]
"," { return *yytext; }
{ident_c}({ident_c}|[0-9])* { yylval.text = strdup(yytext); return IDENT; }
+{ident_c}({ident_c}|[0-9])*"!" { yylval.text = strdup(yytext); return MACRO; }
[0-9]{dec_digit}*"."{dec_digit}+ { yylval.realnum = strtod(yytext, NULL); return FLOAT; }
[0-9]{dec_digit}* { yylval.integer = strtoull(yytext, 0, NULL); return INTEGER; }
+0x[0-9a-fA-F]* { yylval.integer = strtoull(yytext, 0, NULL); return INTEGER; }
'(\\.|[^\\'])+' { return CHARLIT; }
@@ -72,7 +82,7 @@ ident_c [a-zA-Z_]
%%
int main() {
- yydebug = 1;
+ //yydebug = 1;
yyparse();
return 0;
}
diff --git a/bnf/rust.y b/bnf/rust.y
index 888c50c5..227562e5 100644
--- a/bnf/rust.y
+++ b/bnf/rust.y
@@ -1,9 +1,10 @@
-%token <text> IDENT LIFETIME STRING
+%token <text> IDENT LIFETIME STRING MACRO
%token <integer> INTEGER CHARLIT
%token <realnum> FLOAT
%token SUPER_ATTR SUB_ATTR DOC_COMMENT SUPER_DOC_COMMENT
%token DOUBLECOLON THINARROW FATARROW DOUBLEDOT TRIPLEDOT
-%token DOUBLEEQUAL EXCLAMEQUAL
+%token DOUBLEEQUAL EXCLAMEQUAL DOUBLEPIPE DOUBLEAMP
+%token DOUBLELT DOUBLEGT
%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_mut RWD_pub RWD_where
%token RWD_let
@@ -18,6 +19,7 @@
}
%debug
+%error-verbose
%{
#include <stdio.h>
@@ -95,6 +97,7 @@ item
| opt_pub RWD_fn fn_def
| opt_pub RWD_use use_def
| opt_pub RWD_static static_def
+ | opt_pub RWD_const const_def
| opt_pub RWD_struct struct_def
| opt_pub RWD_enum enum_def
| opt_pub RWD_trait trait_def
@@ -119,7 +122,7 @@ fn_def_self
| '&' RWD_self
| '&' RWD_mut RWD_self
;
-fn_def_arg_list: fn_def_arg_list fn_def_arg | ;
+fn_def_arg_list: fn_def_arg | fn_def_arg_list ',' fn_def_arg;
fn_def_arg : irrefutable_pattern ':' type;
/* --- Use --- */
@@ -238,7 +241,11 @@ type_path_segs
: type_path_segs DOUBLECOLON type_path_seg
| type_path_seg
;
-type_path_seg: IDENT | IDENT '<' type_exprs '>';
+type_path_seg
+ : IDENT
+ | IDENT '<' type_exprs '>'
+/* | IDENT '<' type_exprs DOUBLEGT { } */
+ ;
type_exprs: type_exprs ',' type | type;
/*
@@ -264,7 +271,7 @@ Patterns
irrefutable_pattern
: tuple_pattern
| bind_pattern
- | struct_pattern
+ /*| struct_pattern */
;
tuple_pattern: '(' ')';
@@ -272,22 +279,22 @@ tuple_pattern: '(' ')';
bind_pattern: IDENT;
struct_pattern
- : type_path '{' '}'
- | type_path tuple_pattern
+ : expr_path '{' '}'
+ | expr_path tuple_pattern
;
refutable_pattern
: IDENT { /* maybe bind */ }
- | IDENT '@' nonbind_pattern;
+ | IDENT '@' nonbind_pattern
| '&' refutable_pattern
| nonbind_pattern;
nonbind_pattern
: '_' { }
| DOUBLEDOT { }
- | type_path '(' refutable_pattern_list opt_comma ')'
- | type_path '{' refutable_struct_patern '}'
- | type_path
+ | expr_path '(' refutable_pattern_list opt_comma ')'
+ | expr_path '{' refutable_struct_patern '}'
+ | expr_path
/* | '&' nonbind_pattern */
;
@@ -322,13 +329,56 @@ expr: expr_blocks;
expr_blocks
: RWD_match expr '{' match_arms opt_comma '}' { }
- | expr_value
+ | RWD_if if_block
+ | expr_0
+ ;
+if_block
+ : expr code { }
+ | expr code RWD_else code { }
+ | expr code RWD_else RWD_if if_block { }
;
match_arms
: match_arms ',' match_arm
| match_arm
;
-match_arm: refutable_pattern FATARROW expr { };
+match_arm: refutable_pattern FATARROW expr { bnf_trace("match_arm"); };
+
+expr_0: expr_bor;
+
+expr_bor: expr_band | expr_bor DOUBLEPIPE expr_band { } ;
+expr_band: expr_equ | expr_band DOUBLEAMP expr_equ { } ;
+expr_equ
+ : expr_cmp
+ | expr_equ DOUBLEEQUAL expr_cmp
+ | expr_equ EXCLAMEQUAL expr_cmp
+ ;
+expr_cmp
+ : expr_or
+ | expr_cmp '<' expr_or {}
+ | expr_cmp '>' expr_or {}
+ ;
+expr_or: expr_and | expr_or '|' expr_and { };
+expr_and: expr_xor | expr_and '&' expr_xor { };
+expr_xor: expr_8 | expr_xor '^' expr_8 { };
+expr_8
+ : expr_9
+ | expr_8 DOUBLELT expr_9 {}
+ | expr_8 DOUBLEGT expr_9 {}
+ ;
+expr_9
+ : expr_cast
+ | expr_9 '+' expr_cast {}
+ | expr_9 '-' expr_cast {}
+ ;
+/* 10: Cast */
+expr_cast: expr_11 | expr_cast RWD_as type { bnf_trace("expr:cast"); };
+/* 11: Times/Div/Modulo */
+expr_11
+ : expr_value
+ | expr_11 '*' expr_value {}
+ | expr_11 '/' expr_value {}
+ | expr_11 '%' expr_value {}
+ ;
expr_value
: CHARLIT | INTEGER | FLOAT | STRING
@@ -336,6 +386,7 @@ expr_value
| expr_path '{' struct_literal_list '}'
| expr_path
| '(' expr ')'
+ | MACRO tt_paren { bnf_trace("Expr macro invocation"); }
;
expr_list: expr_list ',' expr | expr | /* mt */;
@@ -345,3 +396,11 @@ struct_literal_list
| struct_literal_list IDENT ':' expr ','
| ;
+
+tt_list: | tt_list tt_item;
+tt_item: tt_paren | tt_brace | tt_square | tt_tok;
+tt_tok: IDENT | '+' | '-' | '.' | '!' | RWD_self;
+tt_paren: '(' tt_list ')';
+tt_brace: '{' tt_list '}';
+tt_square: '[' tt_list ']';
+