asn1_compiler/parser/
int.rs

1//! 'parser' Inernal module, API functions from this module are exported.
2
3use crate::tokenizer::Token;
4use anyhow::Result;
5
6use crate::parser::asn::structs::module::Asn1Module;
7
8use super::asn::parse_module;
9
10/// Parse the tokens into internal Asn1Module representation
11///
12/// Token obtained from running [`tokenize`][`crate::tokenizer::tokenize] on an ANS file are parsed
13/// into an internal representation of [`Asn1Module`][`crate::structs::Asn1Module`]. Semantic
14/// errors during parsing the tokens are returned as appropriate variant of `Error`.
15pub fn parse(tokens: &mut Vec<Token>) -> Result<Vec<Asn1Module>> {
16    // Get rid of the comments, it complicates things
17    tokens.retain(|x| !x.is_comment());
18
19    let mut modules = vec![];
20    let mut total = 0;
21    loop {
22        let (module, consumed) = parse_module(&tokens[total..])?;
23        log::trace!(
24            "Module '{}' parsed, adding to the list of modules to be compiled...",
25            module.get_module_name()
26        );
27        modules.push(module);
28        total += consumed;
29        if total == tokens.len() {
30            break;
31        }
32    }
33    Ok(modules)
34}
35
36// TODO: Test cases, at-least single-module, multiple modules etc.