1use crate::ast::{Arg, Command};
6use crate::err::SpecError;
7use crate::lexer::{lex, Tok};
8
9const MAX_BLOCK_DEPTH: usize = 256;
14
15pub fn parse(src: &str) -> Result<Vec<Command>, SpecError> {
19 let desugared = crate::rust_ffi::desugar(src);
20 parse_at(&desugared, 0, 0)
21}
22
23fn 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 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}