mirror of
https://github.com/Noratrieb/uwucc.git
synced 2026-01-14 08:35:08 +01:00
create preprocessor
This commit is contained in:
parent
21c0f4432e
commit
cfb6ef500a
6 changed files with 43 additions and 16 deletions
|
|
@ -3,4 +3,4 @@
|
|||
|
||||
pub fn generate() {
|
||||
println!("ud2");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ mod pre;
|
|||
mod pretty;
|
||||
mod token;
|
||||
|
||||
pub use parser::Parser;
|
||||
pub use crate::parser::Parser;
|
||||
|
||||
pub type Spanned<T> = (T, Span);
|
||||
|
||||
|
|
|
|||
|
|
@ -552,7 +552,7 @@ where
|
|||
impl<'src, I> Iterator for Parser<'src, I>
|
||||
where
|
||||
I: Iterator<Item = (Tok<'src>, Span)>,
|
||||
{
|
||||
{
|
||||
type Item = Result<Spanned<ExternalDecl>>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use crate::{
|
|||
ArithOpKind, Atom, BinaryOp, ComparisonKind, Expr, ExprBinary, ExprPostfix, ExprUnary,
|
||||
PostfixOp, UnaryOp,
|
||||
},
|
||||
parser::{expect, Parser, ParserError, Result, eat},
|
||||
parser::{eat, expect, Parser, ParserError, Result},
|
||||
pre::Punctuator as P,
|
||||
token::{Constant, Token as Tok},
|
||||
Span, Spanned,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
//!
|
||||
//! Code might be bad. Possibly.
|
||||
|
||||
use std::{fmt::Display, ops::Not};
|
||||
use std::{fmt::Display, iter::Enumerate, ops::Not, str::Bytes};
|
||||
|
||||
use peekmore::PeekMore;
|
||||
|
||||
|
|
@ -176,7 +176,7 @@ impl Display for Punctuator {
|
|||
}
|
||||
}
|
||||
|
||||
struct PLexer<'src, I>
|
||||
pub struct PLexer<'src, I>
|
||||
where
|
||||
I: Iterator<Item = (usize, u8)>,
|
||||
{
|
||||
|
|
@ -184,6 +184,15 @@ where
|
|||
src: peekmore::PeekMoreIterator<I>,
|
||||
}
|
||||
|
||||
impl<'src> PLexer<'src, Enumerate<Bytes<'src>>> {
|
||||
pub fn new(src_str: &'src str) -> Self {
|
||||
Self {
|
||||
src_str,
|
||||
src: src_str.bytes().enumerate().peekmore(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src, I> PLexer<'src, I>
|
||||
where
|
||||
I: Iterator<Item = (usize, u8)>,
|
||||
|
|
@ -409,19 +418,11 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn preprocess_tokens(src: &str) -> impl Iterator<Item = (PToken<'_>, Span)> {
|
||||
let lexer = PLexer {
|
||||
src_str: src,
|
||||
src: src.bytes().enumerate().peekmore(),
|
||||
};
|
||||
lexer
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
macro_rules! lex_test {
|
||||
($str:expr) => {
|
||||
let tokens = super::preprocess_tokens($str);
|
||||
let tokens = super::super::preprocess_tokens($str);
|
||||
let tokens = tokens.collect::<Vec<_>>();
|
||||
insta::assert_debug_snapshot!(tokens);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,3 +1,29 @@
|
|||
mod lexer;
|
||||
|
||||
pub use lexer::{preprocess_tokens, PToken, Punctuator};
|
||||
use lexer::PLexer;
|
||||
pub use lexer::{PToken, Punctuator};
|
||||
|
||||
use crate::Span;
|
||||
|
||||
pub struct Preprocessor<L> {
|
||||
lexer: L,
|
||||
}
|
||||
|
||||
impl<'src, L> Iterator for Preprocessor<L>
|
||||
where
|
||||
L: Iterator<Item = (PToken<'src>, Span)>,
|
||||
{
|
||||
type Item = (PToken<'src>, Span);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.lexer.next()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn preprocess_tokens(src: &str) -> impl Iterator<Item = (PToken<'_>, Span)> {
|
||||
let lexer = PLexer::new(src);
|
||||
|
||||
let preprocessor = Preprocessor { lexer };
|
||||
|
||||
preprocessor
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue