mirror of
https://forge.katzen.cafe/katzen-cafe/iowo.git
synced 2024-12-22 10:05:52 +01:00
lang: kinda fun parsing things that can now parse attribute sets with one attribute
This commit is contained in:
parent
1711d17fa6
commit
be637846b1
2 changed files with 55 additions and 19 deletions
|
@ -40,7 +40,7 @@ pub mod parser_to_events {
|
|||
}
|
||||
|
||||
pub fn to_events(tokens: &[(SyntaxKind, &str)]) -> Vec<Event> {
|
||||
let mut only_toks: Vec<SyntaxKind> = tokens.iter().map(|(t, _)| *t).collect();
|
||||
let only_toks: Vec<SyntaxKind> = tokens.iter().map(|(t, _)| *t).collect();
|
||||
let res = parser().parse(&only_toks);
|
||||
res.unwrap()
|
||||
}
|
||||
|
@ -60,27 +60,28 @@ pub mod parser_to_events {
|
|||
})
|
||||
}};
|
||||
}
|
||||
macro_rules! parenthesized {
|
||||
($parser:expr) => {
|
||||
just(L_PAREN)
|
||||
.to(vec![Event::EatToken])
|
||||
.then($parser)
|
||||
.then(just(R_PAREN).to(vec![Event::EatToken]))
|
||||
.map(|((mut before, mut c), mut after)| {
|
||||
before.insert(0, Event::StartNode(PARENTHESIZED_EXPR));
|
||||
before.append(&mut c);
|
||||
before.append(&mut after);
|
||||
before.push(Event::FinishNode);
|
||||
before
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
pub fn parser<'toks>() -> impl Parser<'toks, &'toks [SyntaxKind], Vec<Event>> {
|
||||
let ws = one_of([WHITESPACE, NEWLINE])
|
||||
.to(Event::EatToken)
|
||||
.repeated()
|
||||
.collect::<Vec<Event>>();
|
||||
// let ws_without_newlines = just(WHITESPACE)
|
||||
// .to(Event::EatToken)
|
||||
// .repeated()
|
||||
// .collect::<Vec<Event>>();
|
||||
let parenthesized = |c| {
|
||||
just(L_PAREN)
|
||||
.to(vec![Event::EatToken])
|
||||
.then(c)
|
||||
.then(just(R_PAREN).to(vec![Event::EatToken]))
|
||||
.map(|((mut before, mut c), mut after)| {
|
||||
before.append(&mut c);
|
||||
before.append(&mut after);
|
||||
before
|
||||
})
|
||||
};
|
||||
let ident = just(IDENT).to(vec![Event::EatToken]);
|
||||
|
||||
let expr = recursive(|expr| {
|
||||
let lit = one_of([INT_NUM, FLOAT_NUM, STRING]).to(vec![
|
||||
|
@ -88,10 +89,39 @@ pub mod parser_to_events {
|
|||
Event::EatToken,
|
||||
Event::FinishNode,
|
||||
]);
|
||||
let attrset = just(L_CURLY)
|
||||
.then(
|
||||
padded!(just(IDENT).to(vec![
|
||||
Event::StartNode(ATTR),
|
||||
Event::StartNode(ATTR_NAME),
|
||||
Event::EatToken,
|
||||
Event::FinishNode
|
||||
]))
|
||||
.then(just(COLON))
|
||||
.then(padded!(expr.clone().map(|mut exp: Vec<Event>| {
|
||||
exp.insert(0, Event::StartNode(ATTR_VALUE));
|
||||
exp.push(Event::FinishNode);
|
||||
exp.push(Event::FinishNode);
|
||||
exp
|
||||
})))
|
||||
.map(|((mut name, _), mut value)| {
|
||||
// colon
|
||||
name.push(Event::EatToken);
|
||||
name.append(&mut value);
|
||||
name
|
||||
}),
|
||||
)
|
||||
.then(just(R_CURLY))
|
||||
.map(|((_, mut attrs), _)| {
|
||||
attrs.insert(0, Event::StartNode(ATTR_SET));
|
||||
attrs.insert(0, Event::EatToken);
|
||||
attrs.push(Event::EatToken);
|
||||
attrs.push(Event::FinishNode);
|
||||
attrs
|
||||
});
|
||||
|
||||
let atom = lit.clone().or(parenthesized(expr));
|
||||
let atom = lit.clone().or(attrset).or(parenthesized!(expr));
|
||||
|
||||
let ident = just(IDENT).to(vec![Event::EatToken]);
|
||||
let instr_name = ident
|
||||
.clone()
|
||||
.map(|mut v| {
|
||||
|
|
|
@ -1 +1,7 @@
|
|||
hello world test 42 3.14 "uwu"
|
||||
hello world test
|
||||
42
|
||||
(another command 3.14 "meow")
|
||||
"uwu"
|
||||
{
|
||||
some: attrs 42 (meow gay 1)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue