69 lines
1.6 KiB
Rust
69 lines
1.6 KiB
Rust
use nom::Parser;
|
|
use super::{
|
|
SpannedData,
|
|
Token,
|
|
SpanRef,
|
|
PResult,
|
|
};
|
|
|
|
fn parse_token(span: SpanRef) -> PResult<SpannedData<Token>> {
|
|
nom::branch::alt((
|
|
super::strings::parse_string,
|
|
super::Path::parse,
|
|
super::op_kw_ident::op_ident_or_keyword,
|
|
super::Number::parse,
|
|
super::CurlyBrackets::parse,
|
|
super::SquareBrackets::parse,
|
|
super::RoundBrackets::parse,
|
|
Parser::into(super::Interpolate::parse),
|
|
))(span)
|
|
}
|
|
|
|
fn skip_ws(span: SpanRef) -> PResult<()> {
|
|
nom::multi::many0_count(nom::branch::alt((
|
|
nom::character::complete::multispace1.map(|_| ()),
|
|
// `# ...` comments
|
|
nom::sequence::pair(
|
|
nom::bytes::complete::tag("#"),
|
|
nom::bytes::complete::is_not("\n\r"),
|
|
).map(|_| ()),
|
|
// /* ... */ comments
|
|
nom::sequence::tuple((
|
|
nom::bytes::complete::tag("/*"),
|
|
nom::bytes::complete::take_until("*/"),
|
|
nom::bytes::complete::tag("*/"),
|
|
)).map(|_| ()),
|
|
))).map(|_| ()).parse(span)
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct TokenList(pub Vec<SpannedData<Token>>);
|
|
|
|
impl TokenList {
|
|
pub fn parse_expression(span: SpanRef) -> PResult<Self> {
|
|
nom::sequence::preceded(
|
|
skip_ws,
|
|
nom::multi::many0(
|
|
nom::sequence::terminated(parse_token, skip_ws)
|
|
),
|
|
).map(Self).parse(span)
|
|
}
|
|
|
|
pub fn parse_file(span: SpanRef) -> PResult<Self> {
|
|
nom::combinator::all_consuming(Self::parse_expression)(span)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for TokenList {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
if let Some(head) = self.0.first() {
|
|
head.fmt(f)?;
|
|
for elem in &self.0[1..] {
|
|
f.write_str(" ")?;
|
|
elem.fmt(f)?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|