Skip to content

Commit efc7c71

Browse files
committed
Add try_init_token_kind for tokens that are the root of a syntax tree
1 parent 03621ad commit efc7c71

File tree

8 files changed

+41
-7
lines changed

8 files changed

+41
-7
lines changed

vhdl_lang/src/syntax/concurrent_statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ pub fn parse_concurrent_statement(
555555
diagnostics: &mut dyn DiagnosticHandler,
556556
) -> ParseResult<ConcurrentStatement> {
557557
let statement = {
558-
try_token_kind!(
558+
try_init_token_kind!(
559559
token,
560560
Block => {
561561
ConcurrentStatement::Block(parse_block_statement(stream, diagnostics)?)

vhdl_lang/src/syntax/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn parse_context(
9696
let end_ident;
9797
loop {
9898
let token = stream.expect()?;
99-
try_token_kind!(
99+
try_init_token_kind!(
100100
token,
101101
Library => items.push(parse_library_clause_no_keyword(token, stream)?.map_into(ContextItem::Library)),
102102
Use => items.push(parse_use_clause_no_keyword(token, stream)?.map_into(ContextItem::Use)),

vhdl_lang/src/syntax/design_unit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub fn parse_design_file(
174174
let mut design_units = vec![];
175175

176176
while let Some(token) = stream.peek()? {
177-
try_token_kind!(
177+
try_init_token_kind!(
178178
token,
179179
Library => {
180180
match parse_library_clause(stream) {

vhdl_lang/src/syntax/names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ fn to_suffix(token: Token) -> ParseResult<WithPos<DesignatorOrAll>> {
281281
/// Inside of << >>
282282
fn parse_inner_external_name(stream: &mut TokenStream) -> ParseResult<ExternalName> {
283283
let token = stream.expect()?;
284-
let class = try_token_kind!(
284+
let class = try_init_token_kind!(
285285
token,
286286
Signal => ExternalObjectClass::Signal,
287287
Constant => ExternalObjectClass::Constant,

vhdl_lang/src/syntax/object_declaration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn parse_object_declaration_kind(
4646

4747
pub fn parse_object_declaration(stream: &mut TokenStream) -> ParseResult<Vec<ObjectDeclaration>> {
4848
let token = stream.expect()?;
49-
let result = try_token_kind!(
49+
let result = try_init_token_kind!(
5050
token,
5151
Constant => parse_object_declaration_kind(stream, ObjectClass::Constant)?,
5252
Signal => parse_object_declaration_kind(stream, ObjectClass::Signal)?,

vhdl_lang/src/syntax/sequential_statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ fn parse_unlabeled_sequential_statement(
534534
diagnostics: &mut dyn DiagnosticHandler,
535535
) -> ParseResult<SequentialStatement> {
536536
let statement = {
537-
try_token_kind!(
537+
try_init_token_kind!(
538538
token,
539539
Wait => SequentialStatement::Wait(parse_wait_statement_known_keyword(stream)?),
540540
Assert => SequentialStatement::Assert(parse_assert_statement_known_keyword(stream)?),

vhdl_lang/src/syntax/tokens/tokenizer.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,40 @@ macro_rules! try_token_kind {
217217
}
218218
}
219219

220+
/// Expect any number of token kind patterns, return on no match with
221+
/// error diagnostic based on expected kinds
222+
///
223+
/// Unlike the try_token_kind this macro gives errors always on the next token
224+
/// Example:
225+
///
226+
/// entity ent is
227+
/// end entity;
228+
/// ~ <- error should not be here
229+
///
230+
/// foo
231+
/// ~~~ <- error should be here
232+
#[macro_export]
233+
macro_rules! try_init_token_kind {
234+
($token:expr, $($($kind:ident)|+ => $result:expr),*) => {
235+
match $token.kind {
236+
$(
237+
$($kind)|+ => $result
238+
),*,
239+
_ => {
240+
let kinds = vec![
241+
$(
242+
$(
243+
$kind,
244+
)*
245+
)*
246+
];
247+
248+
return Err($token.kinds_error(&kinds));
249+
}
250+
}
251+
}
252+
}
253+
220254
pub fn kind_str(kind: Kind) -> &'static str {
221255
match kind {
222256
// Keywords

vhdl_lang/src/syntax/type_declaration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub fn parse_protected_type_declaration(
124124
loop {
125125
let token = stream.peek_expect()?;
126126

127-
try_token_kind!(
127+
try_init_token_kind!(
128128
token,
129129
Impure | Function | Procedure => items.push(ProtectedTypeDeclarativeItem::Subprogram(
130130
parse_subprogram_declaration(stream, diagnostics)?,

0 commit comments

Comments
 (0)