use crate::parser::Parser;
use crate::syntax::node_kind::NodeKind::*;
use crate::tokens::Keyword as Kw;
use crate::tokens::TokenKind::*;
fn is_start_of_attribute_name(parser: &mut Parser) -> bool {
match parser.peek_token() {
Tick => {
matches!(parser.peek_nth_token(1), Identifier | Keyword(_))
}
LeftSquare => {
let mut idx = 1;
let mut bracket_count = 1;
while bracket_count > 0 {
match parser.peek_nth_token(idx) {
LeftSquare => bracket_count += 1,
RightSquare => bracket_count -= 1,
Eof => {
return false;
}
_ => {}
}
idx += 1;
}
parser.next_nth_is(Tick, idx)
}
_ => false,
}
}
impl Parser {
pub fn name(&mut self) {
self.start_node(Name);
if self.next_is(LtLt) {
self.external_name();
} else {
self.expect_one_of_tokens([Identifier, StringLiteral, CharacterLiteral]);
}
while self.opt_name_tail() {}
if self.next_is(Keyword(Kw::Range)) && !self.next_nth_is(BOX, 1) {
self.range_constraint();
}
self.end_node();
}
pub fn type_mark(&mut self) {
self.name()
}
pub(crate) fn opt_designator(&mut self) {
self.opt_tokens([Identifier, StringLiteral]);
}
pub(crate) fn designator(&mut self) {
self.expect_one_of_tokens([Identifier, StringLiteral, CharacterLiteral]);
}
pub(crate) fn opt_label(&mut self) {
if self.next_is(Identifier) && self.next_nth_is(Colon, 1) {
self.start_node(Label);
self.skip_n(2);
self.end_node();
}
}
pub(crate) fn name_list(&mut self) {
self.start_node(NameList);
self.separated_list(Parser::name, Comma);
self.end_node();
}
fn suffix(&mut self) {
self.expect_one_of_tokens([
Identifier,
StringLiteral,
CharacterLiteral,
Keyword(Kw::All),
]);
}
fn opt_name_tail(&mut self) -> bool {
match self.peek_token() {
Dot => {
self.start_node(SelectedName);
self.expect_token(Dot);
self.suffix();
self.end_node();
true
}
LeftPar => {
self.start_node(ParenthesizedName);
self.expect_token(LeftPar);
if !self.next_is(RightPar) {
self.association_list();
}
self.expect_token(RightPar);
self.end_node();
true
}
_ => {
if is_start_of_attribute_name(self) {
self.start_node(AttributeName);
if self.next_is(LeftSquare) {
self.signature();
}
self.expect_token(Tick);
if matches!(self.peek_token(), Keyword(_) | Identifier) {
self.skip();
}
self.end_node();
true
} else {
false
}
}
}
}
pub fn external_name(&mut self) {
let checkpoint = self.checkpoint();
self.expect_token(LtLt);
let tok = self.expect_one_of_tokens([
Keyword(Kw::Constant),
Keyword(Kw::Signal),
Keyword(Kw::Variable),
]);
match tok {
Some(Keyword(Kw::Signal)) => self.start_node_at(checkpoint, ExternalSignalName),
Some(Keyword(Kw::Variable)) => self.start_node_at(checkpoint, ExternalVariableName),
_ => self.start_node_at(checkpoint, ExternalConstantName),
}
self.external_pathname();
self.expect_token(Colon);
self.subtype_indication();
self.expect_token(GtGt);
self.end_node();
}
fn external_pathname(&mut self) {
match_next_token!(self,
CommAt => {
self.start_node(PackagePathname);
self.skip();
self.identifier();
while self.opt_token(Dot) {
self.identifier();
}
},
Dot => {
self.start_node(AbsolutePathname);
self.skip();
self.partial_pathname();
},
Circ, Identifier => {
self.start_node(RelativePathname);
while self.opt_token(Circ) {
self.expect_token(Dot);
}
self.partial_pathname();
});
self.end_node();
}
fn partial_pathname(&mut self) {
self.start_node(PartialPathname);
self.identifier();
loop {
if self.next_is(LeftPar) {
self.start_node(ParenthesizedExpressionOrAggregate);
self.expect_token(LeftPar);
self.expression();
self.expect_token(RightPar);
self.end_node();
self.expect_token(Dot);
} else if !self.opt_token(Dot) {
break;
}
self.identifier();
}
self.end_node();
}
pub fn choices(&mut self) {
self.start_node(Choices);
self.separated_list(Parser::choice, Bar);
self.end_node();
}
pub fn choice(&mut self) {
if self.next_is(Keyword(Kw::Others)) {
self.start_node(OthersChoice);
self.skip();
self.end_node();
return;
}
self.start_node(ExpressionChoice);
self.expression();
self.end_node();
}
}
#[cfg(test)]
mod tests {
use crate::parser::{test_utils::to_test_text, Parser};
fn name_to_test_text(code: &str) -> String {
to_test_text(Parser::name, code)
}
#[test]
fn test_identifier_list() {
insta::assert_snapshot!(to_test_text(Parser::identifier_list, "foo, bar, baz"));
}
#[test]
fn test_simple_name() {
insta::assert_snapshot!(name_to_test_text("foo"));
}
#[test]
fn test_characer_name() {
insta::assert_snapshot!(name_to_test_text("'a'"));
}
#[test]
fn test_operator_symbol() {
insta::assert_snapshot!(name_to_test_text("\"+\""));
insta::assert_snapshot!(name_to_test_text("\"AND\""));
insta::assert_snapshot!(name_to_test_text("\"and\""));
}
#[test]
fn test_parse_selected_name_multiple() {
insta::assert_snapshot!(name_to_test_text("foo.bar.baz"));
}
#[test]
fn test_parse_selected_name_all() {
insta::assert_snapshot!(name_to_test_text("foo.all"));
}
#[test]
fn test_slice_name_range() {
insta::assert_snapshot!(name_to_test_text("prefix(0 to 3)"));
insta::assert_snapshot!(name_to_test_text("prefix(3 downto 0)"));
}
#[test]
fn test_slice_range_attribute() {
insta::assert_snapshot!(name_to_test_text("prefix(foo(0)'range)"));
}
#[test]
fn test_attribute_name() {
insta::assert_snapshot!(name_to_test_text("prefix'foo"));
insta::assert_snapshot!(name_to_test_text("prefix'range"));
insta::assert_snapshot!(name_to_test_text("prefix'subtype"));
insta::assert_snapshot!(name_to_test_text("prefix'element"));
}
#[test]
fn test_attribute_name_expression() {
insta::assert_snapshot!(name_to_test_text("prefix'foo(expr+1)"));
}
#[test]
fn test_attribute_name_signature_expression() {
insta::assert_snapshot!(name_to_test_text("prefix[return natural]'foo(expr+1)"));
}
#[test]
fn test_function_call_no_formal() {
insta::assert_snapshot!(name_to_test_text("foo(0)"));
}
#[test]
fn test_function_call_many() {
insta::assert_snapshot!(name_to_test_text("prefix(0, 1)(3).suffix"));
}
#[test]
fn test_function_call() {
insta::assert_snapshot!(name_to_test_text("foo(arg => 0)"));
}
#[test]
fn test_external_name_implicit_relative() {
insta::assert_snapshot!(name_to_test_text("<< signal dut.foo : std_logic >>"));
}
#[test]
fn test_external_name_explicit_relative() {
insta::assert_snapshot!(name_to_test_text("<< signal ^.dut.gen : std_logic >>"));
}
#[test]
fn test_external_name_explicit_relative_multiple_levels() {
insta::assert_snapshot!(name_to_test_text("<< signal ^.^.^.dut.gen : std_logic >>"));
}
#[test]
fn test_external_name_absolute() {
insta::assert_snapshot!(name_to_test_text("<< signal .dut.gen : std_logic >>"));
}
#[test]
fn test_external_name_package() {
insta::assert_snapshot!(name_to_test_text("<< signal @lib.pkg : std_logic >>"));
}
#[test]
fn test_external_name_object_classes() {
insta::assert_snapshot!(name_to_test_text("<< constant dut.foo : std_logic >>"));
insta::assert_snapshot!(name_to_test_text("<< signal dut.foo : std_logic >>"));
insta::assert_snapshot!(name_to_test_text("<< variable dut.foo : std_logic >>"));
}
#[test]
fn empty_association_list() {
insta::assert_snapshot!(name_to_test_text("foo()"));
}
}