futilehdl/src/parser.rs

71 lines
1.9 KiB
Rust
Raw Normal View History

2022-02-02 00:41:19 +00:00
pub mod declaration;
2022-02-01 18:46:06 +00:00
pub mod error;
2022-02-02 00:31:59 +00:00
pub mod expression;
2022-02-01 18:46:06 +00:00
mod literals;
2022-01-17 00:15:27 +00:00
pub mod module;
pub mod proc;
2022-02-01 22:14:11 +00:00
pub mod tokens;
2022-01-16 21:06:52 +00:00
2022-01-01 21:43:38 +00:00
use nom::{
branch::alt,
bytes::complete::tag,
2022-02-02 00:03:03 +00:00
character::complete::{alpha1, alphanumeric1, multispace0},
2022-01-16 21:10:52 +00:00
combinator::{map, opt, recognize},
2022-02-01 18:46:06 +00:00
error::{ErrorKind, ParseError},
2022-01-16 21:10:52 +00:00
multi::{many0, separated_list0},
2022-02-02 00:03:03 +00:00
sequence::{delimited, pair, preceded, separated_pair, tuple},
2022-01-01 21:43:38 +00:00
};
2022-02-01 01:00:27 +00:00
use nom_greedyerror::GreedyError;
2022-01-14 14:32:00 +00:00
use nom_locate::LocatedSpan;
2022-01-04 16:28:43 +00:00
// custom span type for nom_locate
2022-01-04 19:05:10 +00:00
pub type Span<'a> = LocatedSpan<&'a str>;
2022-01-04 16:24:49 +00:00
2022-02-01 01:00:27 +00:00
pub type IErr<I> = GreedyError<I, ErrorKind>;
2022-01-04 16:28:43 +00:00
// custom IResult type for VerboseError
2022-02-01 01:00:27 +00:00
pub type IResult<I, O, E = IErr<I>> = nom::IResult<I, O, E>;
2022-01-04 16:28:43 +00:00
2022-02-02 00:41:19 +00:00
pub use crate::parser::declaration::{
assign_statement, declaration, typename, Assign, NetDecl, TypeName,
};
2022-02-02 00:31:59 +00:00
pub use crate::parser::expression::{expression, Call, Expression, Operation};
2022-01-16 21:06:52 +00:00
pub use crate::parser::module::{module, Module, ModuleItem, PortDirection};
2022-02-02 00:00:11 +00:00
use crate::parser::tokens::{token, TokenKind as tk, TokenSpan};
2022-01-01 21:43:38 +00:00
2022-02-02 00:00:11 +00:00
pub fn parse(input: TokenSpan) -> IResult<TokenSpan, Module> {
module(input)
2022-01-04 16:24:21 +00:00
}
2022-01-01 21:43:38 +00:00
#[cfg(test)]
mod test {
use super::*;
2022-01-17 00:15:27 +00:00
use nom::combinator::all_consuming;
2022-01-01 21:43:38 +00:00
#[test]
fn test_operation() {
2022-01-04 19:05:10 +00:00
operation(" a | b ".into()).unwrap();
operation(" a & b ".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
#[test]
fn test_expression() {
2022-01-04 19:05:10 +00:00
expression(" a ".into()).unwrap();
expression(" a | b ".into()).unwrap();
expression(" a | b | c ".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
#[test]
fn test_assignment() {
2022-01-17 00:15:27 +00:00
// TODO: make wrapper and use for all tests
all_consuming(assign_statement)(" a = b ".into()).unwrap();
all_consuming(assign_statement)(" a = b | c ".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
#[test]
fn test_call() {
2022-01-04 19:05:10 +00:00
call_item("thing ( )".into()).unwrap();
call_item("thing ( a , b , c )".into()).unwrap();
call_item("thing(a,b,c)".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
}