vhdl_syntax 0.2.0

VHDL parser for lossless concrete syntax trees
Documentation
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c)  2025, Lukas Scheller lukasscheller@icloud.com

use crate::parser::Parser;
use crate::syntax::node_kind::NodeKind::*;
use crate::tokens::token_kind::Keyword as Kw;
use crate::tokens::token_kind::TokenKind::*;

impl Parser {
    pub(crate) fn signature(&mut self) {
        // LRM ยง4.5.3
        // signature ::= `[` [ name { `,` name } ] [ `return` name ] `]`;
        self.node(Signature, |p| {
            p.expect_token(LeftSquare);

            if !p.next_is_one_of([Keyword(Kw::Return), RightSquare]) {
                p.separated_list(TypeMarkList, Parser::name, Comma);
            }

            if p.next_is(Keyword(Kw::Return)) {
                p.node(ReturnType, |p| {
                    p.skip(); // Kw::Return
                    p.name();
                });
            }

            p.expect_token(RightSquare);
        });
    }
}

#[cfg(test)]
mod tests {
    use crate::parser::{test_utils::to_test_text, Parser};

    #[test]
    fn parse_signature() {
        insta::assert_snapshot!(to_test_text(
            Parser::signature,
            "[natural, bit return unsigned]"
        ));
        insta::assert_snapshot!(to_test_text(Parser::signature, "[]"));
        insta::assert_snapshot!(to_test_text(Parser::signature, "[return ret_t]"));
        insta::assert_snapshot!(to_test_text(Parser::signature, "[arg1_t, arg2_t]"));
        insta::assert_snapshot!(to_test_text(Parser::signature, "[arg1_t]"));
    }
}