Skip to main content

arb/
parser.rs

1//! Token stream -> command tree. A command is the run of args between two
2//! separators; the first arg is the verb. Block tokens are parsed recursively.
3//! Each command records the char offset of its verb (for LSP diagnostics).
4
5use crate::ast::{Arg, Command};
6use crate::err::SpecError;
7use crate::lexer::{lex, Tok};
8
9/// Deepest `{ … }` nesting the parser will recurse into before failing closed.
10/// Real specs nest a handful deep; this only stops a pathological input from
11/// overflowing the stack and aborting the process (mirrors the expr parser's
12/// depth guard). 256 is far above any legitimate spec.
13const MAX_BLOCK_DEPTH: usize = 256;
14
15/// Parse spec source into a command tree. Inline `rust { ... }` FFI blocks are
16/// desugared to `__rust_compile "<b64>" LINE` commands before lexing (a no-op
17/// when the source has no `rust` keyword).
18pub fn parse(src: &str) -> Result<Vec<Command>, SpecError> {
19    let desugared = crate::rust_ffi::desugar(src);
20    parse_at(&desugared, 0, 0)
21}
22
23/// Parse `src`, treating its offsets as `base`-relative in the whole document
24/// (so a nested `{ … }` block's commands still point at absolute source spans).
25/// `depth` bounds recursion so a deeply nested block errors instead of blowing
26/// the stack.
27fn parse_at(src: &str, base: usize, depth: usize) -> Result<Vec<Command>, SpecError> {
28    if depth > MAX_BLOCK_DEPTH {
29        return Err(SpecError {
30            msg: "spec: blocks too deeply nested".into(),
31            span: Some((base, base + 1)),
32        });
33    }
34    let toks = lex(src)?;
35    let mut cmds = Vec::new();
36    let mut cur: Vec<Arg> = Vec::new();
37    let mut cur_pos: Option<usize> = None;
38    for (t, off) in &toks {
39        match t {
40            Tok::Sep => {
41                if !cur.is_empty() {
42                    cmds.push(finish(
43                        std::mem::take(&mut cur),
44                        cur_pos.take().unwrap_or(base),
45                    )?);
46                }
47            }
48            Tok::Word(w) => {
49                cur_pos.get_or_insert(base + off);
50                cur.push(Arg::Word(w.clone()));
51            }
52            Tok::Str(s) => {
53                cur_pos.get_or_insert(base + off);
54                cur.push(Arg::Str(s.clone()));
55            }
56            Tok::Block(raw) => {
57                cur_pos.get_or_insert(base + off);
58                // The block's inner text starts one char after the `{`.
59                cur.push(Arg::Block(parse_at(raw, base + off + 1, depth + 1)?));
60            }
61        }
62    }
63    if !cur.is_empty() {
64        cmds.push(finish(cur, cur_pos.unwrap_or(base))?);
65    }
66    Ok(cmds)
67}
68
69fn finish(mut args: Vec<Arg>, pos: usize) -> Result<Command, SpecError> {
70    let name = match args.first() {
71        Some(Arg::Word(s)) | Some(Arg::Str(s)) => s.clone(),
72        Some(Arg::Block(_)) => {
73            return Err(SpecError {
74                msg: "command cannot start with a block".into(),
75                span: Some((pos, pos + 1)),
76            })
77        }
78        None => {
79            return Err(SpecError {
80                msg: "empty command".into(),
81                span: Some((pos, pos + 1)),
82            })
83        }
84    };
85    args.remove(0);
86    Ok(Command { name, args, pos })
87}