Skip to main content

scarf_parser/
lib.rs

1// =======================================================================
2// lib.rs
3// =======================================================================
4//! A SystemVerilog preprocessor and parser
5//!
6//! `scarf-parser` provides capabilities for transformting a SystemVerilog
7//! source file into a CST compliant with IEEE 1800-2023, with an
8//! emphasis on informative error messages. It can be used as the
9//! front-end for other tools looking to interpret SystemVerilog designs.
10//!
11//! ## Features
12//!
13//!  - `lossless`: Equivalent to the `lossless` feature for [`scarf_syntax`].
14//!    Produces a CST with room for non-trivia nodes, but does not actually
15//!    parse any from provided sources
16//!  - `parse_lossless`: Extends `lossless` to parse non-trivia tokens.
17//!    Due to their arbitrary position in source files, this adds a
18//!    measurable performance decrease, and should only be used if
19//!    newlines/comments are needed.
20
21mod error;
22pub mod lexer;
23pub mod parser;
24pub mod preprocessor;
25pub mod report;
26use ariadne::{Color, Label};
27pub use error::*;
28use lexer::*;
29pub use lexer::{LexedSource, Token, lex};
30pub use parser::parse;
31use parser::*;
32pub(crate) use preprocessor::*;
33pub use preprocessor::{
34    PreprocessorCache, PreprocessorError, PreprocessorState, preprocess,
35};
36use winnow::Parser;
37use winnow::stream::TokenSlice;
38#[cfg(test)]
39pub mod test;
40pub use scarf_syntax::Span;
41#[cfg(test)]
42pub use test::*;
43
44/// A string and its associated [`Span`] in the source files
45#[derive(Clone, Debug, PartialEq, Eq)]
46pub struct SpannedString<'a>(pub &'a str, pub Span<'a>);
47
48/// A token and its location in the source code
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct SpannedToken<'s>(pub Token<'s>, pub Span<'s>);
51impl<'s> PartialEq<Token<'s>> for SpannedToken<'s> {
52    fn eq(&self, other: &Token) -> bool {
53        self.0 == *other
54    }
55}
56impl<'s> From<(Token<'s>, Span<'s>)> for SpannedToken<'s> {
57    fn from(item: (Token<'s>, Span<'s>)) -> Self {
58        (item.0, item.1).into()
59    }
60}