|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 | +// You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +// |
| 5 | +// Copyright (c) 2018, Olof Kraigher olof.kraigher@gmail.com |
| 6 | + |
| 7 | +use super::common::ParseResult; |
| 8 | +use super::expression::parse_expression; |
| 9 | +use super::names::parse_name; |
| 10 | +use super::separated_list::parse_ident_list; |
| 11 | +use super::tokens::{Kind::*, TokenSpan}; |
| 12 | +use crate::ast::token_range::WithTokenSpan; |
| 13 | +use crate::ast::*; |
| 14 | +use vhdl_lang::syntax::parser::ParsingContext; |
| 15 | + |
| 16 | +/// LRM 7.4 Disconnection Specification |
| 17 | +pub fn parse_disconnection_specification( |
| 18 | + ctx: &mut ParsingContext<'_>, |
| 19 | +) -> ParseResult<WithTokenSpan<DisconnectionSpecification>> { |
| 20 | + let start_token = ctx.stream.expect_kind(Disconnect)?; |
| 21 | + if ctx.stream.next_kind_is(Identifier) { |
| 22 | + let _ = parse_ident_list(ctx); |
| 23 | + } else if ctx.stream.next_kind_is(All) { |
| 24 | + ctx.stream.expect_kind(All)?; |
| 25 | + } else { |
| 26 | + ctx.stream.expect_kind(Others)?; |
| 27 | + } |
| 28 | + ctx.stream.expect_kind(Colon)?; |
| 29 | + let _ = parse_name(ctx); |
| 30 | + ctx.stream.expect_kind(After)?; |
| 31 | + let _ = parse_expression(ctx); |
| 32 | + let end_token = ctx.stream.expect_kind(SemiColon)?; |
| 33 | + Ok(WithTokenSpan::new( |
| 34 | + DisconnectionSpecification {}, |
| 35 | + TokenSpan::new(start_token, end_token), |
| 36 | + )) |
| 37 | +} |
| 38 | + |
| 39 | +#[cfg(test)] |
| 40 | +mod tests { |
| 41 | + use super::*; |
| 42 | + use crate::syntax::test::Code; |
| 43 | + |
| 44 | + #[test] |
| 45 | + fn disconnect_spec_with_scalar_or_composite_signal() { |
| 46 | + let code = Code::new( |
| 47 | + "\ |
| 48 | +disconnect S: T after 42 ms; |
| 49 | +", |
| 50 | + ); |
| 51 | + assert_eq!( |
| 52 | + code.with_stream_no_diagnostics(parse_disconnection_specification), |
| 53 | + WithTokenSpan::new(DisconnectionSpecification {}, code.token_span()) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn disconnect_spec_with_scalar_or_composite_signal_variant() { |
| 59 | + let code = Code::new( |
| 60 | + "\ |
| 61 | +disconnect foo, bar, foobar : unsigned(3 downto 0) after CLK_PERIOD; |
| 62 | +", |
| 63 | + ); |
| 64 | + assert_eq!( |
| 65 | + code.with_stream_no_diagnostics(parse_disconnection_specification), |
| 66 | + WithTokenSpan::new(DisconnectionSpecification {}, code.token_span()) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn disconnect_spec_explicit_with_others() { |
| 72 | + let code = Code::new( |
| 73 | + "\ |
| 74 | +disconnect others: std_logic after 42 ms; |
| 75 | +", |
| 76 | + ); |
| 77 | + assert_eq!( |
| 78 | + code.with_stream_no_diagnostics(parse_disconnection_specification), |
| 79 | + WithTokenSpan::new(DisconnectionSpecification {}, code.token_span()) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn disconnect_spec_explicit_with_all() { |
| 85 | + let code = Code::new( |
| 86 | + "\ |
| 87 | +disconnect all : T after 42 ms; |
| 88 | +", |
| 89 | + ); |
| 90 | + assert_eq!( |
| 91 | + code.with_stream_no_diagnostics(parse_disconnection_specification), |
| 92 | + WithTokenSpan::new(DisconnectionSpecification {}, code.token_span()) |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
0 commit comments