use crate::parser::Parser;
use crate::standard::VHDLStandard;
use crate::syntax::node_kind::NodeKind::*;
use crate::tokens::token_kind::Keyword as Kw;
use crate::tokens::TokenKind::*;
impl Parser {
pub fn component_declaration(&mut self) {
self.start_node(ComponentDeclaration);
self.component_declaration_preamble();
self.start_node(ComponentDeclarationItems);
self.opt_generic_clause();
self.opt_port_clause();
self.end_node();
self.component_declaration_epilogue();
self.end_node();
}
pub fn component_declaration_preamble(&mut self) {
self.start_node(ComponentDeclarationPreamble);
self.expect_token(Keyword(Kw::Component));
self.identifier();
self.opt_token(Keyword(Kw::Is));
self.end_node();
}
pub fn component_declaration_epilogue(&mut self) {
self.start_node(ComponentDeclarationEpilogue);
self.expect_token(Keyword(Kw::End));
if self.standard().is_at_least(VHDLStandard::VHDL2019) {
self.opt_token(Keyword(Kw::Component));
} else {
self.expect_token(Keyword(Kw::Component));
}
self.opt_identifier();
self.expect_token(SemiColon);
self.end_node();
}
}
#[cfg(test)]
mod tests {
use crate::parser::test_utils::{to_test_text, to_test_text_with_standard};
use crate::parser::{parse_syntax_with_standard, Parser};
use crate::standard::VHDLStandard;
#[test]
fn simple_components() {
insta::assert_snapshot!(to_test_text(
Parser::component_declaration,
"\
component foo
end component;
"
));
insta::assert_snapshot!(to_test_text(
Parser::component_declaration,
"\
component foo is
end component;
"
));
insta::assert_snapshot!(to_test_text(
Parser::component_declaration,
"\
component foo is
end component foo;
",
));
}
#[test]
fn components_with_generics() {
insta::assert_snapshot!(to_test_text(
Parser::component_declaration,
"\
component foo is
generic (
foo : natural
);
end component;
",
));
}
#[test]
fn components_with_port() {
insta::assert_snapshot!(to_test_text(
Parser::component_declaration,
"\
component foo is
port (
foo : natural
);
end component;
",
));
}
#[test]
fn component_end_without_keyword_vhdl2019() {
insta::assert_snapshot!(to_test_text_with_standard(
VHDLStandard::VHDL2019,
Parser::component_declaration,
"\
component foo is
end;
"
));
insta::assert_snapshot!(to_test_text_with_standard(
VHDLStandard::VHDL2019,
Parser::component_declaration,
"\
component foo is
end foo;
"
));
}
#[test]
fn component_end_without_keyword_is_error_before_vhdl2019() {
let (_, diagnostics) = parse_syntax_with_standard(
VHDLStandard::VHDL2008,
"component foo is\nend;\n".bytes(),
Parser::component_declaration,
);
assert!(
!diagnostics.is_empty(),
"expected a diagnostic for missing 'component' keyword under VHDL-2008"
);
}
}