vhdl_parser/syntax/
parser.rs

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
7use super::design_unit::parse_design_file;
8use super::tokens::{Symbols, TokenStream, Tokenizer};
9use crate::ast::DesignFile;
10use crate::data::*;
11use std::io;
12use std::sync::Arc;
13
14pub struct VHDLParser {
15    pub symbols: Arc<Symbols>,
16}
17
18pub type ParserResult = Result<(Source, DesignFile), io::Error>;
19
20impl VHDLParser {
21    pub fn new() -> VHDLParser {
22        let symbols = Arc::new(Symbols::new());
23        VHDLParser { symbols }
24    }
25
26    pub fn symbol(&self, name: &Latin1String) -> Symbol {
27        self.symbols.symtab().insert(name)
28    }
29
30    pub fn parse_design_source(
31        &self,
32        source: &Source,
33        diagnostics: &mut dyn DiagnosticHandler,
34    ) -> DesignFile {
35        let contents = source.contents();
36        let tokenizer = Tokenizer::new(&self.symbols, &source, ContentReader::new(&contents));
37        let mut stream = TokenStream::new(tokenizer);
38
39        match parse_design_file(&mut stream, diagnostics) {
40            Ok(design_file) => design_file,
41            Err(diagnostic) => {
42                diagnostics.push(diagnostic);
43                DesignFile::default()
44            }
45        }
46    }
47
48    pub fn parse_design_file(
49        &self,
50        file_name: &Path,
51        diagnostics: &mut dyn DiagnosticHandler,
52    ) -> ParserResult {
53        let source = Source::from_latin1_file(file_name)?;
54        let design_file = self.parse_design_source(&source, diagnostics);
55        Ok((source, design_file))
56    }
57}