@@ -30,18 +30,14 @@ SubstituteCtx::substitute_metavar (std::unique_ptr<AST::Token> &metavar)
30
30
return expanded;
31
31
}
32
32
33
- std::vector<std::unique_ptr<AST::Token>>
34
- SubstituteCtx::substitute_repetition (
35
- size_t pattern_start, size_t pattern_end,
36
- std::unique_ptr<AST::Token> separator_token )
33
+ bool
34
+ SubstituteCtx::check_repetition_amount ( size_t pattern_start,
35
+ size_t pattern_end,
36
+ size_t &expected_repetition_amount )
37
37
{
38
- rust_assert (pattern_end < macro.size ());
39
-
40
- std::vector<std::unique_ptr<AST::Token>> expanded;
38
+ bool first_fragment_found = false ;
39
+ bool is_valid = true ;
41
40
42
- // Find the first fragment and get the amount of repetitions that we should
43
- // perform
44
- size_t repeat_amount = 0 ;
45
41
for (size_t i = pattern_start; i < pattern_end; i++)
46
42
{
47
43
if (macro.at (i)->get_id () == DOLLAR_SIGN)
@@ -59,17 +55,48 @@ SubstituteCtx::substitute_repetition (
59
55
rust_error_at (frag_token->get_locus (),
60
56
" metavar %s used in repetition does not exist" ,
61
57
frag_token->get_str ().c_str ());
62
- // FIXME:
63
- return expanded ;
58
+
59
+ is_valid = false ;
64
60
}
65
61
66
- // FIXME: Refactor, ugly
67
- repeat_amount = it->second .get_match_amount ();
62
+ size_t repeat_amount = it->second .get_match_amount ();
63
+ if (!first_fragment_found)
64
+ {
65
+ first_fragment_found = true ;
66
+ expected_repetition_amount = repeat_amount;
67
+ }
68
+ else
69
+ {
70
+ if (repeat_amount != expected_repetition_amount)
71
+ {
72
+ rust_error_at (
73
+ frag_token->get_locus (),
74
+ " different amount of matches used in merged "
75
+ " repetitions: expected %ld, got %ld" ,
76
+ expected_repetition_amount, repeat_amount);
77
+ is_valid = false ;
78
+ }
79
+ }
68
80
}
69
81
}
70
82
}
71
83
84
+ return is_valid;
85
+ }
86
+
87
+ std::vector<std::unique_ptr<AST::Token>>
88
+ SubstituteCtx::substitute_repetition (
89
+ size_t pattern_start, size_t pattern_end,
90
+ std::unique_ptr<AST::Token> separator_token)
91
+ {
92
+ rust_assert (pattern_end < macro.size ());
93
+
94
+ size_t repeat_amount = 0 ;
95
+ if (!check_repetition_amount (pattern_start, pattern_end, repeat_amount))
96
+ return {};
97
+
72
98
rust_debug (" repetition amount to use: %lu" , repeat_amount);
99
+ std::vector<std::unique_ptr<AST::Token>> expanded;
73
100
std::vector<std::unique_ptr<AST::Token>> new_macro;
74
101
75
102
// We want to generate a "new macro" to substitute with. This new macro
0 commit comments