Skip to main content

open_vaf/parser/
branch.rs

1/*
2 * ******************************************************************************************
3 * Copyright (c) 2019 Pascal Kuthe. This file is part of the OpenVAF project.
4 * It is subject to the license terms in the LICENSE file found in the top-level directory
5 *  of this distribution and at  https://gitlab.com/DSPOM/OpenVAF/blob/master/LICENSE.
6 *  No part of OpenVAF, including this file, may be copied, modified, propagated, or
7 *  distributed except according to the terms contained in the LICENSE file.
8 * *****************************************************************************************
9 */
10use crate::ast::{Branch, BranchAccess, BranchDeclaration};
11
12use crate::ir::{AttributeNode, Attributes, Node};
13use crate::parser::error::Result;
14use crate::parser::error::Type::UnexpectedToken;
15use crate::parser::lexer::Token;
16use crate::parser::{Error, Parser};
17use crate::symbol_table::SymbolDeclaration;
18
19impl<'lt, 'source_map> Parser<'lt, 'source_map> {
20    pub fn parse_branch_declaration(&mut self, attributes: Attributes) -> Result {
21        let start = self.preprocessor.current_start();
22        self.expect(Token::ParenOpen)?;
23        let branch = self.parse_branch()?;
24        self.expect(Token::ParenClose)?;
25        self.parse_list(
26            |sel: &mut Self| {
27                let name = sel.parse_identifier(false)?;
28                let source = sel.span_to_current_end(start);
29                let branch_decl = sel.ast.branches.push(AttributeNode {
30                    attributes,
31                    contents: BranchDeclaration {
32                        name,
33                        branch: branch.clone(),
34                    },
35                    source,
36                });
37                sel.insert_symbol(
38                    sel.ast[branch_decl].contents.name,
39                    SymbolDeclaration::Branch(branch_decl),
40                );
41                Ok(())
42            },
43            Token::Semicolon,
44            true,
45        )?;
46        Ok(())
47    }
48
49    pub fn parse_branch(&mut self) -> Result<Branch> {
50        if self.look_ahead()? == Token::OpLess {
51            self.consume_lookahead();
52            let res = Branch::Port(self.parse_hierarchical_identifier()?);
53            self.expect(Token::OpGreater)?;
54            Ok(res)
55        } else {
56            let first_net_name = self.parse_hierarchical_identifier()?;
57            let (token, source) = self.look_ahead_with_span()?;
58            match token {
59                Token::Comma => {
60                    self.consume_lookahead();
61                    let second_net_name = self.parse_hierarchical_identifier()?;
62                    Ok(Branch::Nets(first_net_name, second_net_name))
63                }
64                Token::ParenClose => Ok(Branch::NetToGround(first_net_name)),
65                _ => Err(Error {
66                    source,
67                    error_type: UnexpectedToken {
68                        expected: vec![Token::Comma],
69                    },
70                }),
71            }
72        }
73    }
74
75    pub fn parse_branch_access(&mut self) -> Result<Node<BranchAccess>> {
76        self.expect(Token::ParenOpen)?;
77        let start = self.preprocessor.current_start();
78        let res = if self.look_ahead()? == Token::OpLess {
79            self.consume_lookahead();
80            let res = Branch::Port(self.parse_hierarchical_identifier()?);
81            self.expect(Token::OpGreater)?;
82            BranchAccess::Implicit(res)
83        } else {
84            let first_net_name_or_identifer = self.parse_hierarchical_identifier()?;
85            if self.look_ahead()? == Token::Comma {
86                let second_net_name = self.parse_hierarchical_identifier()?;
87                BranchAccess::Implicit(Branch::Nets(first_net_name_or_identifer, second_net_name))
88            } else {
89                BranchAccess::BranchOrNodePotential(first_net_name_or_identifer)
90            }
91        };
92
93        self.expect(Token::ParenClose)?;
94        Ok(Node::new(res, self.span_to_current_end(start)))
95    }
96}