Skip to main content

vibe/parse/
mod.rs

1//! Recursive-descent parser for vibe-0.1.
2
3use crate::ast::*;
4use crate::error::{DiagCode, Diagnostic};
5use crate::lex::{Lexer, Token, TokenKind};
6use crate::span::Span;
7
8mod cond;
9mod expr;
10mod items;
11mod preamble;
12mod stmt;
13
14pub struct Parser<'a> {
15    pub(crate) lex: Lexer<'a>,
16    pub(crate) cur: Token,
17    /// Current nesting depth of value-producing `if` expressions.
18    pub(crate) cond_depth: u16,
19}
20
21impl<'a> Parser<'a> {
22    /// Creates a new `Parser`.
23    pub fn new(src: &'a str) -> Result<Self, Diagnostic> {
24        let mut lex = Lexer::new(src);
25        let cur = lex.next_token()?;
26        Ok(Self {
27            lex,
28            cur,
29            cond_depth: 0,
30        })
31    }
32
33    /// Parses program.
34    pub fn parse_program(&mut self) -> Result<Program, Diagnostic> {
35        let start = self.cur.span.start;
36        let mut locales = Vec::new();
37        while self.kw("locale") {
38            locales.push(self.parse_locale()?);
39        }
40        let module = if self.kw("module") {
41            Some(self.parse_module()?)
42        } else {
43            None
44        };
45        let mut imports = Vec::new();
46        while self.kw("import") {
47            imports.push(self.parse_import()?);
48        }
49        let mut prefixes = Vec::new();
50        while self.kw("prefix") {
51            prefixes.push(self.parse_prefix()?);
52        }
53        let mut requires = Vec::new();
54        loop {
55            if self.kw("using") {
56                requires.extend(self.parse_using()?);
57                continue;
58            }
59            if self.kw("requires") {
60                requires.extend(self.parse_requires()?);
61                continue;
62            }
63            break;
64        }
65        let mut items = Vec::new();
66        while self.cur.kind != TokenKind::Eof {
67            items.push(self.parse_item()?);
68        }
69        let end = items
70            .last()
71            .map(|i| item_span(i).end)
72            .unwrap_or(self.cur.span.end);
73        Ok(Program {
74            span: Span::new(start, end),
75            module,
76            imports,
77            prefixes,
78            locales,
79            requires,
80            items,
81        })
82    }
83
84    /// Parses cell-body.
85    pub fn parse_cell_body(&mut self) -> Result<Expr, Diagnostic> {
86        self.expect(TokenKind::Eq, "cell body must start with '='")?;
87        let expr = self.parse_expr()?;
88        if self.cur.kind == TokenKind::Semicolon {
89            self.bump()?;
90        }
91        if self.cur.kind != TokenKind::Eof {
92            return Err(self.err("unexpected tokens after cell expression"));
93        }
94        Ok(expr)
95    }
96    fn take_effect_class(&mut self) -> Result<Option<EffectClass>, Diagnostic> {
97        let class = match self.canonical_text() {
98            "pure" => Some(EffectClass::Pure),
99            "hot" => Some(EffectClass::Hot),
100            "cold" => Some(EffectClass::Cold),
101            "async" => return Ok(None),
102            "effect" => Some(EffectClass::External),
103            _ => None,
104        };
105        if class.is_some() && self.cur.kind == TokenKind::Keyword {
106            self.bump()?;
107        }
108        Ok(class)
109    }
110
111    fn take_literal(&mut self) -> Result<Literal, Diagnostic> {
112        if self.kw("true") {
113            self.bump()?;
114            return Ok(Literal::Bool(true));
115        }
116        if self.kw("false") {
117            self.bump()?;
118            return Ok(Literal::Bool(false));
119        }
120        if self.kw("null") {
121            self.bump()?;
122            return Ok(Literal::Null);
123        }
124        if self.cur.kind == TokenKind::String {
125            let span = self.cur.span;
126            let s = unquote(self.text(), span)?;
127            self.bump()?;
128            return Ok(Literal::String(s));
129        }
130        if self.cur.kind == TokenKind::Int {
131            let span = self.cur.span;
132            let lit = parse_int(self.text(), span)?;
133            self.bump()?;
134            return Ok(lit);
135        }
136        if self.cur.kind == TokenKind::Float {
137            let span = self.cur.span;
138            let bits = parse_float_bits(self.text(), span)?;
139            self.bump()?;
140            return Ok(Literal::Float(bits));
141        }
142        if self.cur.kind == TokenKind::Quantity {
143            let span = self.cur.span;
144            let raw = self.text();
145            let (bits, unit) = parse_quantity_literal(raw, span)?;
146            self.bump()?;
147            return Ok(Literal::Quantity { value: bits, unit });
148        }
149        if self.cur.kind == TokenKind::Color {
150            let span = self.cur.span;
151            let raw = self.text();
152            let color = parse_color_literal(raw, span)?;
153            self.bump()?;
154            return Ok(Literal::Color(color));
155        }
156        Err(self.err("expected literal"))
157    }
158
159    fn prev_span_of_lit(&self) -> Span {
160        // last bumped token is not stored; use cur as fallback
161        Span::point(self.cur.span.start)
162    }
163
164    fn prev_end(&self, fallback: u32) -> u32 {
165        if self.cur.span.start > 0 {
166            self.cur.span.start
167        } else {
168            fallback
169        }
170    }
171
172    fn expect(&mut self, kind: TokenKind, msg: &str) -> Result<Span, Diagnostic> {
173        if self.cur.kind == kind {
174            let span = self.cur.span;
175            self.bump()?;
176            Ok(span)
177        } else {
178            Err(self.err(msg))
179        }
180    }
181
182    fn expect_ident(&mut self) -> Result<String, Diagnostic> {
183        if self.cur.kind == TokenKind::Ident
184            || (self.cur.kind == TokenKind::Keyword && !is_stmt_kw(self.text()))
185        {
186            let s = self.text().to_string();
187            self.bump()?;
188            return Ok(s);
189        }
190        Err(self.err("expected identifier"))
191    }
192
193    /// Prefixed local names include SNOMED-style numeric concept ids (`snomed:386661006`).
194    fn expect_prefixed_local(&mut self) -> Result<String, Diagnostic> {
195        if self.cur.kind == TokenKind::Int
196            || self.cur.kind == TokenKind::Ident
197            || (self.cur.kind == TokenKind::Keyword && !is_stmt_kw(self.text()))
198        {
199            let s = self.text().to_string();
200            self.bump()?;
201            return Ok(s);
202        }
203        Err(self.err("expected prefixed local name"))
204    }
205
206    fn expect_string(&mut self) -> Result<String, Diagnostic> {
207        if self.cur.kind != TokenKind::String {
208            return Err(self.err("expected string"));
209        }
210        let s = unquote(self.text(), self.cur.span)?;
211        self.bump()?;
212        Ok(s)
213    }
214
215    fn eat_optional_semi(&mut self) -> Result<(), Diagnostic> {
216        if self.cur.kind == TokenKind::Semicolon {
217            self.bump()?;
218        }
219        Ok(())
220    }
221
222    fn kw(&self, name: &str) -> bool {
223        self.cur.kind == TokenKind::Keyword && self.canonical_text() == name
224    }
225
226    fn text(&self) -> &str {
227        self.cur.span.slice(self.lex.source())
228    }
229
230    fn bump(&mut self) -> Result<(), Diagnostic> {
231        self.cur = self.lex.next_token()?;
232        Ok(())
233    }
234
235    fn err(&self, msg: &str) -> Diagnostic {
236        Diagnostic::new(DiagCode::E001, self.cur.span, msg.to_string())
237    }
238}
239
240/// `true` when named-arg-key.
241pub(super) fn is_named_arg_key(s: &str) -> bool {
242    matches!(
243        s,
244        "take"
245            | "subject"
246            | "predicate"
247            | "object"
248            | "context"
249            | "topic"
250            | "schema"
251            | "scope"
252            | "steps"
253            | "workspace"
254            | "output"
255            | "tolerance"
256            // Civics / LocalHost stdlib kernels (Econ, Statistics, PhysicalUnits)
257            | "incomes"
258            | "utilities"
259            | "epsilon"
260            | "x"
261            | "y"
262            | "value"
263            | "from"
264            | "to"
265            | "poverty_line"
266    )
267}
268
269/// `true` when stmt-kw.
270pub(super) fn is_stmt_kw(s: &str) -> bool {
271    let canon = if let Some((_, c)) = crate::locale::LocaleRegistry::default().resolve(s) {
272        c
273    } else {
274        s
275    };
276    matches!(
277        canon,
278        "let"
279            | "if"
280            | "else"
281            | "for"
282            | "while"
283            | "match"
284            | "return"
285            | "yield"
286            | "transaction"
287            | "fn"
288            | "on"
289            | "const"
290            | "module"
291            | "import"
292            | "prefix"
293            | "requires"
294    )
295}
296
297/// Binary operator.
298pub(super) fn bin(left: Expr, op: BinOp, right: Expr) -> Expr {
299    Expr {
300        span: left.span.merge(right.span),
301        kind: ExprKind::Binary {
302            op,
303            left: Box::new(left),
304            right: Box::new(right),
305        },
306    }
307}
308
309/// Item span.
310pub(super) fn item_span(item: &Item) -> Span {
311    match item {
312        Item::Function(f) => f.span,
313        Item::Hook(h) => h.span,
314        Item::Const(c) => c.span,
315        Item::Enum(e) => e.span,
316        Item::Field(f) => f.span,
317        Item::Material(m) => m.span,
318        Item::Law(l) => l.span,
319        Item::Cell(c) => c.span,
320        Item::Present(p) => p.span,
321        Item::Bind(b) => b.span,
322        Item::Statement(s) => s.span(),
323    }
324}
325
326/// Parses color-literal.
327pub(super) fn parse_color_literal(
328    raw: &str,
329    span: Span,
330) -> Result<crate::ast::ColorLit, Diagnostic> {
331    let hex = raw.strip_prefix('#').unwrap_or(raw);
332    let digit = |i: usize| {
333        u8::from_str_radix(&hex[i..i + 1], 16)
334            .map_err(|_| Diagnostic::new(DiagCode::E001, span, "invalid hex color"))
335    };
336    let pair = |i: usize| {
337        u8::from_str_radix(&hex[i..i + 2], 16)
338            .map_err(|_| Diagnostic::new(DiagCode::E001, span, "invalid hex color"))
339    };
340    match hex.len() {
341        3 => Ok(crate::ast::ColorLit {
342            r: digit(0)? * 17,
343            g: digit(1)? * 17,
344            b: digit(2)? * 17,
345            a: 255,
346        }),
347        4 => Ok(crate::ast::ColorLit {
348            r: digit(0)? * 17,
349            g: digit(1)? * 17,
350            b: digit(2)? * 17,
351            a: digit(3)? * 17,
352        }),
353        6 => Ok(crate::ast::ColorLit {
354            r: pair(0)?,
355            g: pair(2)?,
356            b: pair(4)?,
357            a: 255,
358        }),
359        8 => Ok(crate::ast::ColorLit {
360            r: pair(0)?,
361            g: pair(2)?,
362            b: pair(4)?,
363            a: pair(6)?,
364        }),
365        _ => Err(Diagnostic::new(
366            DiagCode::E001,
367            span,
368            "color literal must be #rgb, #rgba, #rrggbb, or #rrggbbaa",
369        )),
370    }
371}
372
373/// Strips an IRI wrapper.
374pub(super) fn strip_iri(s: &str) -> String {
375    s.trim_start_matches('<').trim_end_matches('>').to_string()
376}
377
378/// Unquotes a string.
379pub(super) fn unquote(s: &str, span: Span) -> Result<String, Diagnostic> {
380    let inner = s
381        .strip_prefix('"')
382        .and_then(|x| x.strip_suffix('"'))
383        .ok_or_else(|| Diagnostic::new(DiagCode::E001, span, "bad string"))?;
384    let mut out = String::new();
385    let mut chars = inner.chars();
386    while let Some(c) = chars.next() {
387        if c == '\\' {
388            match chars.next() {
389                Some('\\') => out.push('\\'),
390                Some('"') => out.push('"'),
391                Some('n') => out.push('\n'),
392                Some('r') => out.push('\r'),
393                Some('t') => out.push('\t'),
394                Some('u') => {
395                    // \u{XXXX}
396                    if chars.next() != Some('{') {
397                        return Err(Diagnostic::new(DiagCode::E001, span, "expected \\u{XXXX}"));
398                    }
399                    let mut hex = String::new();
400                    loop {
401                        match chars.next() {
402                            Some('}') => break,
403                            Some(h) => hex.push(h),
404                            None => {
405                                return Err(Diagnostic::new(
406                                    DiagCode::E001,
407                                    span,
408                                    "unterminated \\u{}",
409                                ))
410                            }
411                        }
412                    }
413                    let cp = u32::from_str_radix(&hex, 16)
414                        .map_err(|_| Diagnostic::new(DiagCode::E001, span, "bad unicode escape"))?;
415                    out.push(char::from_u32(cp).ok_or_else(|| {
416                        Diagnostic::new(DiagCode::E001, span, "invalid codepoint")
417                    })?);
418                }
419                _ => return Err(Diagnostic::new(DiagCode::E001, span, "bad string escape")),
420            }
421        } else {
422            out.push(c);
423        }
424    }
425    Ok(out)
426}
427
428/// Parses int.
429pub(super) fn parse_int(s: &str, span: Span) -> Result<Literal, Diagnostic> {
430    let raw = s
431        .trim_end_matches("i32")
432        .trim_end_matches("u32")
433        .trim_end_matches("i64")
434        .trim_end_matches("u64")
435        .replace('_', "");
436    let is_unsigned = s.ends_with("u64") || s.ends_with("u32");
437    if let Some(hex) = raw.strip_prefix("0x").or_else(|| raw.strip_prefix("0X")) {
438        if is_unsigned {
439            u64::from_str_radix(hex, 16)
440                .map(Literal::UInt)
441                .map_err(|_| Diagnostic::new(DiagCode::E001, span, "bad hex integer"))
442        } else if let Ok(i) = i64::from_str_radix(hex, 16) {
443            Ok(Literal::Int(i))
444        } else if let Ok(u) = u64::from_str_radix(hex, 16) {
445            Ok(Literal::UInt(u))
446        } else {
447            Err(Diagnostic::new(DiagCode::E001, span, "bad hex integer"))
448        }
449    } else if is_unsigned {
450        raw.parse::<u64>()
451            .map(Literal::UInt)
452            .map_err(|_| Diagnostic::new(DiagCode::E001, span, "bad integer"))
453    } else if let Ok(i) = raw.parse::<i64>() {
454        Ok(Literal::Int(i))
455    } else if let Ok(u) = raw.parse::<u64>() {
456        Ok(Literal::UInt(u))
457    } else {
458        Err(Diagnostic::new(DiagCode::E001, span, "bad integer"))
459    }
460}
461
462/// Parses float-bits.
463pub(super) fn parse_float_bits(s: &str, span: Span) -> Result<u64, Diagnostic> {
464    let raw = s
465        .trim_end_matches("f32")
466        .trim_end_matches("f64")
467        .replace('_', "");
468    raw.parse::<f64>()
469        .map(f64::to_bits)
470        .map_err(|_| Diagnostic::new(DiagCode::E001, span, "bad float"))
471}
472
473/// Parses quantity-literal.
474pub(super) fn parse_quantity_literal(raw: &str, span: Span) -> Result<(u64, String), Diagnostic> {
475    let mut num_end = 0;
476    let chars: Vec<char> = raw.chars().collect();
477    while num_end < chars.len() {
478        let c = chars[num_end];
479        if c.is_ascii_digit()
480            || c == '.'
481            || c == '_'
482            || c == 'e'
483            || c == 'E'
484            || (c == '-' && num_end > 0 && (chars[num_end - 1] == 'e' || chars[num_end - 1] == 'E'))
485            || (c == '+' && num_end > 0 && (chars[num_end - 1] == 'e' || chars[num_end - 1] == 'E'))
486        {
487            num_end += 1;
488        } else {
489            break;
490        }
491    }
492    let num_str: String = chars[..num_end].iter().filter(|&&c| c != '_').collect();
493    let mut unit_str: String = chars[num_end..].iter().collect();
494    if unit_str.starts_with('[') && unit_str.ends_with(']') {
495        unit_str = unit_str[1..unit_str.len() - 1].to_string();
496    }
497    let val: f64 = num_str.parse().map_err(|_| {
498        Diagnostic::new(
499            DiagCode::E001,
500            span,
501            format!("invalid quantity number `{num_str}`"),
502        )
503    })?;
504    Ok((val.to_bits(), unit_str))
505}
506
507/// Parses program.
508pub fn parse_program(src: &str) -> Result<Program, Diagnostic> {
509    Parser::new(src)?.parse_program()
510}
511
512/// Parses cell.
513pub fn parse_cell(src: &str) -> Result<Expr, Diagnostic> {
514    Parser::new(src)?.parse_cell_body()
515}