Formatter introduced
This commit is contained in:
@@ -59,7 +59,7 @@ pub async fn parse_item(
|
||||
) -> OrcRes<Vec<Item>> {
|
||||
match item.pop_front() {
|
||||
Some((TokTree { tok: Token::Name(n), .. }, postdisc)) => match n {
|
||||
n if *n == item.i("export").await => match try_pop_no_fluff(postdisc).await? {
|
||||
n if *n == item.i().i("export").await => match try_pop_no_fluff(postdisc).await? {
|
||||
Parsed { output: TokTree { tok: Token::Name(n), .. }, tail } =>
|
||||
parse_exportable_item(ctx, path, comments, true, n.clone(), tail).await,
|
||||
Parsed { output: TokTree { tok: Token::NS, .. }, tail } => {
|
||||
@@ -70,12 +70,12 @@ pub async fn parse_item(
|
||||
([], Some(n)) =>
|
||||
ok.push(Item { comments: comments.clone(), pos, kind: ItemKind::Export(n) }),
|
||||
(_, Some(_)) => ctx.reporter().report(mk_err(
|
||||
tail.i("Compound export").await,
|
||||
tail.i().i("Compound export").await,
|
||||
"Cannot export compound names (names containing the :: separator)",
|
||||
[pos.into()],
|
||||
)),
|
||||
(_, None) => ctx.reporter().report(mk_err(
|
||||
tail.i("Wildcard export").await,
|
||||
tail.i().i("Wildcard export").await,
|
||||
"Exports cannot contain the globstar *",
|
||||
[pos.into()],
|
||||
)),
|
||||
@@ -85,12 +85,12 @@ pub async fn parse_item(
|
||||
Ok(ok)
|
||||
},
|
||||
Parsed { output, tail } => Err(mk_errv(
|
||||
tail.i("Malformed export").await,
|
||||
tail.i().i("Malformed export").await,
|
||||
"`export` can either prefix other lines or list names inside ::( ) or ::[ ]",
|
||||
[Pos::Range(output.range.clone()).into()],
|
||||
)),
|
||||
},
|
||||
n if *n == item.i("import").await => parse_import(ctx, postdisc).await.map(|v| {
|
||||
n if *n == item.i().i("import").await => parse_import(ctx, postdisc).await.map(|v| {
|
||||
Vec::from_iter(v.into_iter().map(|(t, pos)| Item {
|
||||
comments: comments.clone(),
|
||||
pos,
|
||||
@@ -99,10 +99,11 @@ pub async fn parse_item(
|
||||
}),
|
||||
n => parse_exportable_item(ctx, path, comments, false, n.clone(), postdisc).await,
|
||||
},
|
||||
Some(_) =>
|
||||
Err(mk_errv(item.i("Expected a line type").await, "All lines must begin with a keyword", [
|
||||
Pos::Range(item.pos()).into(),
|
||||
])),
|
||||
Some(_) => Err(mk_errv(
|
||||
item.i().i("Expected a line type").await,
|
||||
"All lines must begin with a keyword",
|
||||
[Pos::Range(item.pos()).into()],
|
||||
)),
|
||||
None => unreachable!("These lines are filtered and aggregated in earlier stages"),
|
||||
}
|
||||
}
|
||||
@@ -124,20 +125,20 @@ pub async fn parse_exportable_item(
|
||||
discr: Tok<String>,
|
||||
tail: ParsSnippet<'_>,
|
||||
) -> OrcRes<Vec<Item>> {
|
||||
let kind = if discr == tail.i("mod").await {
|
||||
let kind = if discr == tail.i().i("mod").await {
|
||||
let (name, body) = parse_module(ctx, path, tail).await?;
|
||||
ItemKind::Member(Member::new(name, MemberKind::Mod(body)))
|
||||
} else if discr == tail.i("const").await {
|
||||
} else if discr == tail.i().i("const").await {
|
||||
let (name, val) = parse_const(tail).await?;
|
||||
let locator = CodeLocator::to_const(tail.i(&path.push(name.clone()).unreverse()).await);
|
||||
let locator = CodeLocator::to_const(tail.i().i(&path.push(name.clone()).unreverse()).await);
|
||||
ItemKind::Member(Member::new(name, MemberKind::Const(Code::from_code(locator, val))))
|
||||
} else if let Some(sys) = ctx.systems().find(|s| s.can_parse(discr.clone())) {
|
||||
let line = sys.parse(tail.to_vec(), exported, comments).await?;
|
||||
return parse_items(ctx, path, Snippet::new(tail.prev(), &line, tail.interner())).await;
|
||||
return parse_items(ctx, path, Snippet::new(tail.prev(), &line, tail.i())).await;
|
||||
} else {
|
||||
let ext_lines = ctx.systems().flat_map(System::line_types).join(", ");
|
||||
return Err(mk_errv(
|
||||
tail.i("Unrecognized line type").await,
|
||||
tail.i().i("Unrecognized line type").await,
|
||||
format!("Line types are: const, mod, macro, grammar, {ext_lines}"),
|
||||
[Pos::Range(tail.prev().range.clone()).into()],
|
||||
));
|
||||
@@ -154,18 +155,18 @@ pub async fn parse_module(
|
||||
Parsed { output: TokTree { tok: Token::Name(n), .. }, tail } => (n.clone(), tail),
|
||||
Parsed { output, .. } => {
|
||||
return Err(mk_errv(
|
||||
tail.i("Missing module name").await,
|
||||
format!("A name was expected, {} was found", output.print().await),
|
||||
tail.i().i("Missing module name").await,
|
||||
format!("A name was expected, {} was found", tail.fmt(output).await),
|
||||
[Pos::Range(output.range.clone()).into()],
|
||||
));
|
||||
},
|
||||
};
|
||||
let Parsed { output, tail: surplus } = try_pop_no_fluff(tail).await?;
|
||||
expect_end(surplus).await?;
|
||||
let Some(body) = output.as_s(Paren::Round, tail.interner()) else {
|
||||
let Some(body) = output.as_s(Paren::Round, tail.i()) else {
|
||||
return Err(mk_errv(
|
||||
tail.i("Expected module body").await,
|
||||
format!("A ( block ) was expected, {} was found", output.print().await),
|
||||
tail.i().i("Expected module body").await,
|
||||
format!("A ( block ) was expected, {} was found", tail.fmt(output).await),
|
||||
[Pos::Range(output.range.clone()).into()],
|
||||
));
|
||||
};
|
||||
@@ -177,16 +178,16 @@ pub async fn parse_const(tail: ParsSnippet<'_>) -> OrcRes<(Tok<String>, Vec<Pars
|
||||
let Parsed { output, tail } = try_pop_no_fluff(tail).await?;
|
||||
let Some(name) = output.as_name() else {
|
||||
return Err(mk_errv(
|
||||
tail.i("Missing module name").await,
|
||||
format!("A name was expected, {} was found", output.print().await),
|
||||
tail.i().i("Missing module name").await,
|
||||
format!("A name was expected, {} was found", tail.fmt(output).await),
|
||||
[Pos::Range(output.range.clone()).into()],
|
||||
));
|
||||
};
|
||||
let Parsed { output, tail } = try_pop_no_fluff(tail).await?;
|
||||
if !output.is_kw(tail.i("=").await) {
|
||||
if !output.is_kw(tail.i().i("=").await) {
|
||||
return Err(mk_errv(
|
||||
tail.i("Missing walrus := separator").await,
|
||||
format!("Expected operator := , found {}", output.print().await),
|
||||
tail.i().i("Missing = separator").await,
|
||||
format!("Expected = , found {}", tail.fmt(output).await),
|
||||
[Pos::Range(output.range.clone()).into()],
|
||||
));
|
||||
}
|
||||
@@ -200,7 +201,7 @@ pub async fn parse_mtree(mut snip: ParsSnippet<'_>) -> OrcRes<Vec<MacTree>> {
|
||||
let (range, tok, tail) = match &ttree.tok {
|
||||
Token::S(p, b) => (
|
||||
ttree.range.clone(),
|
||||
MTok::S(*p, parse_mtree(Snippet::new(ttree, b, snip.interner())).boxed_local().await?),
|
||||
MTok::S(*p, parse_mtree(Snippet::new(ttree, b, snip.i())).boxed_local().await?),
|
||||
tail,
|
||||
),
|
||||
Token::Name(tok) => {
|
||||
@@ -210,7 +211,7 @@ pub async fn parse_mtree(mut snip: ParsSnippet<'_>) -> OrcRes<Vec<MacTree>> {
|
||||
let Parsed { output, tail } = try_pop_no_fluff(tail).await?;
|
||||
let Some(seg) = output.as_name() else {
|
||||
return Err(mk_errv(
|
||||
tail.i("Namespaced name interrupted").await,
|
||||
tail.i().i("Namespaced name interrupted").await,
|
||||
"In expression context, :: must always be followed by a name.\n\
|
||||
::() is permitted only in import and export items",
|
||||
[Pos::Range(output.range.clone()).into()],
|
||||
@@ -220,15 +221,11 @@ pub async fn parse_mtree(mut snip: ParsSnippet<'_>) -> OrcRes<Vec<MacTree>> {
|
||||
snip = tail;
|
||||
end = output.range.end;
|
||||
}
|
||||
(
|
||||
ttree.range.start..end,
|
||||
MTok::Name(Sym::new(segments, snip.interner()).await.unwrap()),
|
||||
snip,
|
||||
)
|
||||
(ttree.range.start..end, MTok::Name(Sym::new(segments, snip.i()).await.unwrap()), snip)
|
||||
},
|
||||
Token::NS => {
|
||||
return Err(mk_errv(
|
||||
tail.i("Unexpected :: in macro pattern").await,
|
||||
tail.i().i("Unexpected :: in macro pattern").await,
|
||||
":: can only follow a name outside export statements",
|
||||
[Pos::Range(ttree.range.clone()).into()],
|
||||
));
|
||||
@@ -236,10 +233,10 @@ pub async fn parse_mtree(mut snip: ParsSnippet<'_>) -> OrcRes<Vec<MacTree>> {
|
||||
Token::Ph(ph) => (ttree.range.clone(), MTok::Ph(ph.clone()), tail),
|
||||
Token::Atom(_) | Token::Macro(_) => {
|
||||
return Err(mk_errv(
|
||||
tail.i("Unsupported token in macro patterns").await,
|
||||
tail.i().i("Unsupported token in macro patterns").await,
|
||||
format!(
|
||||
"Macro patterns can only contain names, braces, and lambda, not {}.",
|
||||
ttree.print().await
|
||||
tail.fmt(ttree).await
|
||||
),
|
||||
[Pos::Range(ttree.range.clone()).into()],
|
||||
));
|
||||
@@ -249,13 +246,13 @@ pub async fn parse_mtree(mut snip: ParsSnippet<'_>) -> OrcRes<Vec<MacTree>> {
|
||||
Token::LambdaHead(arg) => (
|
||||
ttree.range.start..snip.pos().end,
|
||||
MTok::Lambda(
|
||||
parse_mtree(Snippet::new(ttree, arg, snip.interner())).boxed_local().await?,
|
||||
parse_mtree(Snippet::new(ttree, arg, snip.i())).boxed_local().await?,
|
||||
parse_mtree(tail).boxed_local().await?,
|
||||
),
|
||||
Snippet::new(ttree, &[], snip.interner()),
|
||||
Snippet::new(ttree, &[], snip.i()),
|
||||
),
|
||||
Token::Slot(_) | Token::X(_) =>
|
||||
panic!("Did not expect {} in parsed token tree", &ttree.tok.print().await),
|
||||
panic!("Did not expect {} in parsed token tree", tail.fmt(ttree).await),
|
||||
};
|
||||
mtreev.push(MTree { pos: Pos::Range(range.clone()), tok: Rc::new(tok) });
|
||||
snip = tail;
|
||||
@@ -272,7 +269,7 @@ pub async fn parse_macro(
|
||||
Parsed { tail, output: o @ TokTree { tok: Token::S(Paren::Round, b), .. } } => (tail, o, b),
|
||||
Parsed { output, .. } => {
|
||||
return Err(mk_errv(
|
||||
tail.i("m").await,
|
||||
tail.i().i("m").await,
|
||||
"Macro blocks must either start with a block or a ..$:number",
|
||||
[Pos::Range(output.range.clone()).into()],
|
||||
));
|
||||
@@ -281,24 +278,22 @@ pub async fn parse_macro(
|
||||
expect_end(surplus).await?;
|
||||
let mut errors = Vec::new();
|
||||
let mut rules = Vec::new();
|
||||
for (i, item) in
|
||||
line_items(Snippet::new(prev, block, tail.interner())).await.into_iter().enumerate()
|
||||
{
|
||||
for (i, item) in line_items(Snippet::new(prev, block, tail.i())).await.into_iter().enumerate() {
|
||||
let Parsed { tail, output } = try_pop_no_fluff(item.tail).await?;
|
||||
if !output.is_kw(tail.i("rule").await) {
|
||||
if !output.is_kw(tail.i().i("rule").await) {
|
||||
errors.extend(mk_errv(
|
||||
tail.i("non-rule in macro").await,
|
||||
format!("Expected `rule`, got {}", output.print().await),
|
||||
tail.i().i("non-rule in macro").await,
|
||||
format!("Expected `rule`, got {}", tail.fmt(output).await),
|
||||
[Pos::Range(output.range.clone()).into()],
|
||||
));
|
||||
continue;
|
||||
};
|
||||
let arrow = tail.i("=>").await;
|
||||
let arrow = tail.i().i("=>").await;
|
||||
let (pat, body) = match tail.split_once(|t| t.is_kw(arrow.clone())) {
|
||||
Some((a, b)) => (a, b),
|
||||
None => {
|
||||
errors.extend(mk_errv(
|
||||
tail.i("no => in macro rule").await,
|
||||
tail.i().i("no => in macro rule").await,
|
||||
"The pattern and body of a rule must be separated by a =>",
|
||||
[Pos::Range(tail.pos()).into()],
|
||||
));
|
||||
@@ -310,7 +305,7 @@ pub async fn parse_macro(
|
||||
pos: Pos::Range(tail.pos()),
|
||||
pattern: parse_mtree(pat).await?,
|
||||
kind: RuleKind::Native(Code::from_code(
|
||||
CodeLocator::to_rule(tail.i(&path.unreverse()).await, macro_i, i as u16),
|
||||
CodeLocator::to_rule(tail.i().i(&path.unreverse()).await, macro_i, i as u16),
|
||||
body.to_vec(),
|
||||
)),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user