summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bnf/Makefile18
-rw-r--r--bnf/rust.y123
-rw-r--r--bnf/rust_expr.y.h8
-rw-r--r--bnf/rust_expr.y_tree.h12
4 files changed, 101 insertions, 60 deletions
diff --git a/bnf/Makefile b/bnf/Makefile
index b004d286..99bca9a3 100644
--- a/bnf/Makefile
+++ b/bnf/Makefile
@@ -1,6 +1,7 @@
.SUFFIXES:
+CXXFLAGS := -std=c++11 -x c++ -I . -g
OBJS := main.o rust.tab.o rust.lex.o
@@ -14,25 +15,28 @@ BIN := ./test.bin
all: $(BIN)
+clean:
+ rm $(BIN) $(OBJS)
+
test: $(BIN) $(TSTFILES)
$(foreach f,$(TSTFILES), $(DBG) ./test.bin "$f" &&) true
.PHONY: rust_tests
RUST_TESTS_DIR := $(RUSTSRC)test/
-rust_tests: $(patsubst $(RUST_TESTS_DIR)%.rs,output/rust/%.txt,$(wildcard $(RUST_TESTS_DIR)run-pass/*.rs))
+rust_tests: $(patsubst $(RUST_TESTS_DIR)%.rs,output/rust/%.txt,$(sort $(wildcard $(RUST_TESTS_DIR)run-pass/*.rs)))
output/rust/%.txt: $(RUST_TESTS_DIR)%.rs $(BIN)
@mkdir -p $(dir $@)
$(BIN) $< > $@ 2>&1
$(BIN): $(OBJS)
- g++ -std=c++11 $(OBJS) -o $@
+ g++ -std=c++11 $(OBJS) -o $@ -g
-%.o: %.cpp ast_types.hpp lex.hpp
- g++ -x c++ -std=c++11 $< -c -o $@ -I .
-%.o: .gen/%.cpp ast_types.hpp lex.hpp
- g++ -x c++ -std=c++11 $< -c -o $@ -I .
+%.o: %.cpp
+ g++ $(CXXFLAGS) $< -c -o $@ -MMD -MP -MF $@.dep
+%.o: .gen/%.cpp
+ g++ $(CXXFLAGS) $< -c -o $@ -MMD -MP -MF $@.dep
.gen/rust.tab.cpp: .gen/.rust.y
@mkdir -p $(dir $@)
@@ -57,3 +61,5 @@ $(BIN): $(OBJS)
main.cpp: .gen/rust.tab.hpp
+-include $(OBJS:%=%.dep)
+
diff --git a/bnf/rust.y b/bnf/rust.y
index 8922e7ca..4420fa20 100644
--- a/bnf/rust.y
+++ b/bnf/rust.y
@@ -49,7 +49,7 @@
%type <MetaItem*> meta_item
%type <MetaItems*> meta_items
-%type <Item*> item vis_item unsafe_item unsafe_vis_item
+%type <Item*> item macro_item vis_item unsafe_item unsafe_vis_item
%type <UseSet*> use_def
%type <TypeAlias*> type_def
%type <Module*> module_def
@@ -133,6 +133,7 @@ opt_lifetime: { $$ = nullptr; } | LIFETIME;
module_body
: { $$ = new ::std::vector< ::std::unique_ptr<Item> >(); }
| module_body attrs item { $3->add_attrs( consume($2) ); $1->push_back( box_raw($3) ); $$ = $1; }
+ | module_body attrs macro_item { $3->add_attrs( consume($2) ); $1->push_back( box_raw($3) ); $$ = $1; }
;
attrs
@@ -159,7 +160,12 @@ meta_item
;
-
+macro_item
+ : 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)); }
+ ;
/*
==========================
Root Items
@@ -174,10 +180,6 @@ item
| RWD_extern extern_block { $$ = $2; }
| 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)); }
;
/* Items for which visibility is valid */
vis_item
@@ -266,6 +268,7 @@ fn_def_arg: pattern ':' type;
fn_def_arg_list_PROTO: fn_def_arg_PROTO | fn_def_arg_list_PROTO ',' fn_def_arg_PROTO;
fn_def_arg_PROTO
: IDENT ':' type
+ | '_' ':' type
| RWD_mut IDENT ':' type
| type
;
@@ -351,34 +354,36 @@ enum_variant_
;
/* --- Trait --- */
-trait_def: IDENT generic_def trait_bounds '{' trait_items '}' { $$ = new Trait(); };
+trait_def: IDENT generic_def trait_bounds where_clause '{' trait_items '}' { $$ = new Trait(); };
trait_bounds: ':' trait_bound_list | ;
trait_bound_list: trait_bound_list '+' bound | bound;
trait_items: | trait_items attrs trait_item;
trait_item
- : RWD_type IDENT ';'
- | RWD_type IDENT ':' trait_bound_list ';'
- | RWD_type IDENT '=' type ';'
+ : RWD_type IDENT trait_bounds ';'
+ | RWD_type IDENT trait_bounds '=' type ';'
+ | RWD_const IDENT ':' type opt_assign_value ';'
| 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
;
+opt_assign_value: | '=' expr;
/* --- Impl --- */
-impl_def: impl_def_line '{' impl_items '}' { $$ = new Impl(); };
+impl_def: generic_def impl_def_line '{' impl_items '}' { $$ = new Impl(); };
impl_def_line
- : generic_def trait_path RWD_for type where_clause { bnf_trace(context, "trait impl"); }
- | generic_def '!' trait_path RWD_for type where_clause { bnf_trace(context, "negative trait impl"); }
- | generic_def trait_path RWD_for DOUBLEDOT where_clause { bnf_trace(context, "wildcard impl"); }
- | generic_def type where_clause { bnf_trace(context, "inherent impl"); }
+ : trait_path RWD_for type where_clause { bnf_trace(context, "trait impl"); }
+ | '!' trait_path RWD_for type where_clause { bnf_trace(context, "negative trait impl"); }
+ | trait_path RWD_for DOUBLEDOT where_clause { bnf_trace(context, "wildcard impl"); }
+ | type_noufcs where_clause { bnf_trace(context, "inherent impl"); }
;
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 ';'
+ | opt_pub RWD_const const_def
| MACRO tt_group_item
;
@@ -407,7 +412,7 @@ where_clause_ent
hrlb_def: | RWD_for '<' lifetime_list '>';
lifetime_list: LIFETIME | lifetime_list ',' LIFETIME
bounds: bounds '+' bound | bound;
-bound: LIFETIME | '?' trait_path | trait_path;
+bound: LIFETIME | '?' trait_path | hrlb_def trait_path;
/*
=========================================
@@ -455,7 +460,9 @@ type_path
: ufcs_path DOUBLECOLON IDENT
| trait_path
;
-ufcs_path: '<' ufcs_path_tail;
+ufcs_path
+ : '<' ufcs_path_tail
+ | DOUBLELT ufcs_path_tail DOUBLECOLON ufcs_path_tail;
ufcs_path_tail
: type '>'
| type RWD_as trait_path '>'
@@ -482,9 +489,17 @@ type
: trait_list
| type_ele
;
+type_noufcs
+ : trait_list
+ | trait_path
+ | type_nopath
+ ;
type_ele
: type_path
- | opt_unsafe RWD_fn '(' fn_def_arg_list_PROTO ')' fn_def_ret
+ | type_nopath
+ ;
+type_nopath
+ : 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
@@ -518,7 +533,14 @@ struct_pattern
: expr_path '{' struct_pattern_items '}'
| expr_path '(' pattern_list ')'
;
-struct_pattern_item: IDENT | IDENT ':' pattern | DOUBLEDOT;
+struct_pattern_item
+ : IDENT
+ | RWD_ref IDENT
+ | RWD_ref RWD_mut IDENT
+ | RWD_mut IDENT
+ | IDENT ':' pattern
+ | DOUBLEDOT
+ ;
struct_pattern_items: struct_pattern_items ',' struct_pattern_item | struct_pattern_item;
slice_pattern
@@ -550,6 +572,7 @@ nonbind_pattern
| '&' RWD_mut pattern
| DOUBLEAMP pattern
| DOUBLEAMP RWD_mut pattern
+ | RWD_box pattern
;
value_pattern
: expr_path
@@ -574,13 +597,11 @@ code: block { bnf_trace(context, "code parsed"); };
block: '{' block_contents '}';
-block_contents
- : block_lines
- | block_lines tail_expr
- ;
+block_contents: block_lines;
tail_expr
- : expr_noblock
+ : expr_noblock_NOBRACE
| flow_control
+ |
;
flow_control
: RWD_return {}
@@ -588,30 +609,27 @@ flow_control
| RWD_break opt_lifetime {}
| RWD_continue opt_lifetime {}
;
-block_lines: | block_lines block_line;
-block_line
- : RWD_let let_binding ';'
- | 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
+block_lines
+ :
+ | attrs expr
+ | attrs MACRO tt_paren
+ | super_attr block_lines
+ | ';' block_lines
+ | attrs expr ';' block_lines
+ | attrs expr_blocks block_lines
+ | attrs block block_lines
+ | attrs item block_lines
+ | attrs RWD_let let_binding ';' block_lines
+ | attrs MACRO IDENT tt_brace block_lines
+ | attrs MACRO tt_brace block_lines
+ | attrs MACRO tt_paren ';' block_lines
+ | LIFETIME ':' loop_block block_lines
;
opt_type_annotation: | ':' type;
let_binding
: pattern opt_type_annotation '=' expr
| pattern opt_type_annotation
-
-stmt
- : expr ';'
- | ';'
;
expr_list: expr_list ',' expr | expr | /* mt */;
@@ -624,12 +642,19 @@ struct_literal_list
expr
: '{' block_contents '}'
+ | attrs expr_noblock
| expr_noblock
;
expr_NOSTRLIT
: block
+ | attrs expr_noblock_NOSTRLIT
| expr_noblock_NOSTRLIT
;
+expr_NOBRACE
+ : block
+ | attrs expr_noblock_NOBRACE
+ | expr_noblock_NOBRACE
+ ;
expr_blocks
: RWD_match expr_NOSTRLIT '{' match_arms '}' { }
@@ -657,12 +682,12 @@ if_block_head
match_arms: match_arms_list match_arm_last;
match_arms_list
:
- | match_arms_list match_arm ','
- | match_arms_list match_arm_brace
+ | match_arms_list attrs match_arm ','
+ | match_arms_list attrs match_arm_brace
;
match_arm_last
- : match_arm
- | match_arm ','
+ : attrs match_arm
+ | attrs match_arm ','
;
match_pattern
: pattern
@@ -673,10 +698,10 @@ match_patterns
| match_pattern
;
match_arm
- : match_arm_expr
- | match_arm_brace
+ : match_arm_brace
+ | match_arm_expr
;
-match_arm_brace : match_patterns FATARROW '{' block_contents '}' { };
+match_arm_brace: match_patterns FATARROW '{' block_contents '}' { };
match_arm_expr: match_patterns FATARROW tail_expr { bnf_trace(context, "match_arm"); };
diff --git a/bnf/rust_expr.y.h b/bnf/rust_expr.y.h
index 9cb3621f..0b15553f 100644
--- a/bnf/rust_expr.y.h
+++ b/bnf/rust_expr.y.h
@@ -1,5 +1,5 @@
-assign_op: '=' | PLUSEQUAL | MINUSEQUAL | STAREQUAL | SLASHEQUAL | DOUBLELTEQUAL | DOUBLEGTEQUAL | PIPEEQUAL | AMPEQUAL | CARETEQUAL;
+assign_op: '=' | PLUSEQUAL | MINUSEQUAL | STAREQUAL | SLASHEQUAL | PERCENTEQUAL | DOUBLELTEQUAL | DOUBLEGTEQUAL | PIPEEQUAL | AMPEQUAL | CARETEQUAL;
closure_arg_list: | closure_arg_list_;
closure_arg_list_
@@ -25,3 +25,9 @@ closure_ret
#include "rust_expr.y_tree.h"
#undef SUFFIX
#undef _
+
+#define SUFFIX_is__NOBRACE
+#define _(v) v##_NOBRACE
+#include "rust_expr.y_tree.h"
+#undef SUFFIX
+#undef _
diff --git a/bnf/rust_expr.y_tree.h b/bnf/rust_expr.y_tree.h
index d0b4c876..09256f3e 100644
--- a/bnf/rust_expr.y_tree.h
+++ b/bnf/rust_expr.y_tree.h
@@ -1,7 +1,7 @@
_(expr_noblock): _(expr_assign);
_(expr_assign)
- : _(expr_0) assign_op _(expr_0)
+ : _(expr_0) assign_op _(expr_assign)
| _(expr_0)
;
@@ -80,6 +80,7 @@ _(expr_12)
| '*' _(expr_12)
/* | RWD_box expr_12 */
| '&' _(expr_12)
+ | '&' block /*HACK*/
| '&' RWD_mut _(expr_12)
| DOUBLEAMP _(expr_12) { }
| DOUBLEAMP RWD_mut _(expr_12) { }
@@ -99,13 +100,16 @@ _(expr_value)
| expr_blocks
| 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 ',' '}'
- | expr_path '{' struct_literal_list ',' DOUBLEDOT expr_0 '}'
+ | expr_path '{' struct_literal_list '}' { bnf_trace(context, "struct literal"); }
+ | expr_path '{' struct_literal_list ',' '}' { bnf_trace(context, "struct literal"); }
+ | expr_path '{' struct_literal_list ',' DOUBLEDOT expr_0 '}' { bnf_trace(context, "struct literal (with default)"); }
#endif
| expr_path
| RWD_self
| '(' expr ')'
+#ifndef SUFFIX_is__NOBRACE
+//| block
+#endif
| '(' ')'
| '(' expr ',' expr_list ')'
| '[' expr_list opt_comma ']'