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_opts, 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, true)
21}
22
23/// Verbs whose `{ … }` argument is NOT a command list. `sel`'s brace holds a CSS
24/// selector, where `#main` is an ID and not a comment, so that block is re-lexed
25/// with the `#` comment rule off. Every other brace in the language holds
26/// commands and keeps it.
27fn brace_is_not_commands(verb: &str) -> bool {
28    verb == "sel"
29}
30
31/// Parse `src`, treating its offsets as `base`-relative in the whole document
32/// (so a nested `{ … }` block's commands still point at absolute source spans).
33/// `depth` bounds recursion so a deeply nested block errors instead of blowing
34/// the stack.
35fn parse_at(
36    src: &str,
37    base: usize,
38    depth: usize,
39    hash_comments: bool,
40) -> Result<Vec<Command>, SpecError> {
41    if depth > MAX_BLOCK_DEPTH {
42        return Err(SpecError {
43            msg: "spec: blocks too deeply nested".into(),
44            span: Some((base, base + 1)),
45        });
46    }
47    // `depth > 0` means this text is a `{ … }` body, the only place a jq literal
48    // may start a command (see `lexer::lex`).
49    let toks = lex_opts(src, depth > 0, hash_comments)?;
50    let mut cmds = Vec::new();
51    let mut cur: Vec<Arg> = Vec::new();
52    let mut cur_pos: Option<usize> = None;
53    for (t, off) in &toks {
54        match t {
55            Tok::Sep => {
56                if !cur.is_empty() {
57                    cmds.push(finish(
58                        std::mem::take(&mut cur),
59                        cur_pos.take().unwrap_or(base),
60                    )?);
61                }
62            }
63            Tok::Word(w) => {
64                cur_pos.get_or_insert(base + off);
65                cur.push(Arg::Word(w.clone()));
66            }
67            Tok::Str(s) => {
68                cur_pos.get_or_insert(base + off);
69                cur.push(Arg::Str(s.clone()));
70            }
71            Tok::Block(raw) => {
72                cur_pos.get_or_insert(base + off);
73                // A block ARGUMENT of `sel` is a CSS selector, so its `#` is an ID
74                // and not a comment. The command's verb is already `cur[0]` by the
75                // time an argument block arrives.
76                let sel_arg = matches!(cur.first(), Some(Arg::Word(w)) if brace_is_not_commands(w));
77                // The block's inner text starts one char after the `{`.
78                cur.push(Arg::Block(parse_at(
79                    raw,
80                    base + off + 1,
81                    depth + 1,
82                    hash_comments && !sel_arg,
83                )?));
84            }
85        }
86    }
87    if !cur.is_empty() {
88        cmds.push(finish(cur, cur_pos.unwrap_or(base))?);
89    }
90    Ok(cmds)
91}
92
93fn finish(mut args: Vec<Arg>, pos: usize) -> Result<Command, SpecError> {
94    let name = match args.first() {
95        Some(Arg::Word(s)) | Some(Arg::Str(s)) => s.clone(),
96        Some(Arg::Block(_)) => {
97            return Err(SpecError {
98                msg: "command cannot start with a block".into(),
99                span: Some((pos, pos + 1)),
100            })
101        }
102        None => {
103            return Err(SpecError {
104                msg: "empty command".into(),
105                span: Some((pos, pos + 1)),
106            })
107        }
108    };
109    args.remove(0);
110    Ok(Command { name, args, pos })
111}