Skip to main content

fncc_core/
parser.rs

1use pest::Parser;
2
3#[derive(pest_derive::Parser)]
4#[grammar = "parser/fncc.pest"]
5pub struct FnccParser;
6
7use pest::iterators::Pair;
8
9#[derive(Debug, Clone, PartialEq)]
10pub struct ComponentImport {
11    pub name: String,
12    pub source: ImportSource,
13}
14
15#[derive(Debug, Clone, PartialEq)]
16pub enum ImportSource {
17    /// Path to another .fui file, e.g. "ui::components::Button"
18    FuiPath(String),
19    /// A GPUI native component, e.g. `use gpui::TextInput;`
20    Gpui,
21}
22
23#[derive(Debug, Clone, PartialEq)]
24pub struct Document {
25    pub frontmatter: Option<String>,
26    pub state_type: Option<String>,
27    pub imports: Vec<ComponentImport>,
28    pub root: Element,
29}
30
31#[derive(Debug, Clone, PartialEq)]
32pub struct Element {
33    pub name: String,
34    pub attrs: Vec<(String, AttrValue)>,
35    pub children: Vec<Node>,
36}
37
38#[derive(Debug, Clone, PartialEq)]
39pub enum AttrValue {
40    String(String),
41    Interpolation(String),
42}
43
44#[derive(Debug, Clone, PartialEq)]
45pub enum Node {
46    Element(Element),
47    Text(String),
48    Interpolation(String),
49}
50
51pub fn parse(source: &str) -> Result<Document, String> {
52    let mut pairs = FnccParser::parse(Rule::document, source).map_err(|e| format!("parse error: {e}"))?;
53
54    let pair = pairs.next().expect("document should exist");
55
56    let mut frontmatter = None;
57    let mut state_type = None;
58    let mut imports = Vec::new();
59    let mut root = None;
60
61    for inner in pair.into_inner() {
62        match inner.as_rule() {
63            Rule::frontmatter => {
64                let content = inner.as_str().trim();
65                let content = content
66                    .strip_prefix("---")
67                    .and_then(|s| s.strip_suffix("---"))
68                    .map(|s| s.trim().to_string());
69
70                if let Some(ref raw) = content {
71                    // extract @state directive and component imports
72                    let mut clean_lines = Vec::new();
73                    let mut component_imports = Vec::new();
74                    for line in raw.lines() {
75                        let trimmed = line.trim();
76                        if let Some(st) = trimmed.strip_prefix("@state ") {
77                            state_type = Some(st.trim().to_string());
78                        } else if is_component_import_line(trimmed) {
79                            if let Some(imports) = parse_component_imports(trimmed) {
80                                component_imports.extend(imports);
81                            }
82                            // Only strip from emitted Rust if it's a .fui import,
83                            // not a gpui import (those are real Rust)
84                            if !trimmed.starts_with("use gpui::") {
85                                continue;
86                            }
87                            clean_lines.push(line);
88                        } else {
89                            clean_lines.push(line);
90                        }
91                    }
92                    imports = component_imports;
93                    let clean = clean_lines.join("\n");
94                    if !clean.trim().is_empty() {
95                        frontmatter = Some(clean);
96                    }
97                }
98            }
99            Rule::element => {
100                root = Some(parse_element(inner));
101            }
102            _ => {}
103        }
104    }
105
106    Ok(Document {
107        frontmatter,
108        state_type,
109        imports,
110        root: root.expect("document must have a root element"),
111    })
112}
113
114fn parse_element(pair: Pair<Rule>) -> Element {
115    let mut name = String::new();
116    let mut attrs = Vec::new();
117    let mut children = Vec::new();
118    let mut close_name: Option<String> = None;
119
120    for inner in pair.into_inner() {
121        match inner.as_rule() {
122            Rule::open_tag => {
123                for tag_inner in inner.into_inner() {
124                    match tag_inner.as_rule() {
125                        Rule::tag_name => {
126                            name = tag_inner.as_str().to_string();
127                        }
128                        Rule::attr => {
129                            let (aname, avalue) = parse_attr(tag_inner);
130                            attrs.push((aname, avalue));
131                        }
132                        _ => {}
133                    }
134                }
135            }
136            Rule::self_closing_tag => {
137                for tag_inner in inner.into_inner() {
138                    match tag_inner.as_rule() {
139                        Rule::tag_name => {
140                            name = tag_inner.as_str().to_string();
141                        }
142                        Rule::attr => {
143                            let (aname, avalue) = parse_attr(tag_inner);
144                            attrs.push((aname, avalue));
145                        }
146                        _ => {}
147                    }
148                }
149            }
150            Rule::children => {
151                for child in inner.into_inner() {
152                    let actual = child.into_inner().next().expect("node should have one child");
153                    match actual.as_rule() {
154                        Rule::element => {
155                            children.push(Node::Element(parse_element(actual)));
156                        }
157                        Rule::inner_text => {
158                            let text = actual.as_str().trim().to_string();
159                            if !text.is_empty() {
160                                children.push(Node::Text(text));
161                            }
162                        }
163                        Rule::interpolation => {
164                            let expr = actual.as_str().trim();
165                            let expr = expr.strip_prefix('{').and_then(|s| s.strip_suffix('}')).unwrap_or(expr);
166                            children.push(Node::Interpolation(expr.to_string()));
167                        }
168                        _ => {}
169                    }
170                }
171            }
172            Rule::close_tag => {
173                for tag_inner in inner.into_inner() {
174                    if tag_inner.as_rule() == Rule::tag_name {
175                        close_name = Some(tag_inner.as_str().to_string());
176                    }
177                }
178            }
179            _ => {}
180        }
181    }
182
183    if let Some(ref close) = close_name {
184        assert_eq!(&name, close, "mismatched close tag: </{close}> does not match <{name}>");
185    }
186
187    Element { name, attrs, children }
188}
189
190fn parse_attr(pair: Pair<Rule>) -> (String, AttrValue) {
191    let mut attr_name = String::new();
192    let mut attr_value = AttrValue::String(String::new());
193
194    for inner in pair.into_inner() {
195        match inner.as_rule() {
196            Rule::attr_name => {
197                attr_name = inner.as_str().to_string();
198            }
199            Rule::attr_value => {
200                let val = inner.as_str();
201                let val = val.strip_prefix('"').and_then(|s| s.strip_suffix('"')).unwrap_or(val);
202                let trimmed = val.trim();
203                if trimmed.starts_with('{') && trimmed.ends_with('}') {
204                    let expr = &trimmed[1..trimmed.len() - 1].trim();
205                    attr_value = AttrValue::Interpolation(expr.to_string());
206                } else {
207                    attr_value = AttrValue::String(val.to_string());
208                }
209            }
210            Rule::interpolation => {
211                let expr = inner.as_str().trim();
212                let expr = expr.strip_prefix('{').and_then(|s| s.strip_suffix('}')).unwrap_or(expr);
213                attr_value = AttrValue::Interpolation(expr.to_string());
214            }
215            _ => {}
216        }
217    }
218
219    (attr_name, attr_value)
220}
221
222/// Check if a trimmed line is a component import (`use ui::...` or `use gpui::...`)
223fn is_component_import_line(line: &str) -> bool {
224    (line.starts_with("use ui::") || line.starts_with("use gpui::")) && line.ends_with(';')
225}
226
227/// Parse component imports from a `use` line.
228/// Supports:
229/// - `use ui::components::Button;`
230/// - `use ui::components::{Button, Input};`
231/// - `use gpui::TextInput;`
232fn parse_component_imports(line: &str) -> Option<Vec<ComponentImport>> {
233    let trimmed = line.strip_prefix("use ")?.strip_suffix(';')?.trim();
234    if trimmed.starts_with("ui::") {
235        parse_fui_imports(trimmed)
236    } else if trimmed.starts_with("gpui::") {
237        parse_gpui_imports(trimmed)
238    } else {
239        None
240    }
241}
242
243fn parse_fui_imports(path: &str) -> Option<Vec<ComponentImport>> {
244    // Remove leading "ui::"
245    let rest = path.strip_prefix("ui::")?;
246    if let Some(brace_start) = rest.find('{') {
247        // Grouped: "components::{Button, Input}"
248        let prefix = rest[..brace_start].trim_end_matches("::");
249        let inner = &rest[brace_start + 1..];
250        let inner = inner.split('}').next()?.trim();
251        let names: Vec<String> = inner
252            .split(',')
253            .map(|s| s.trim().to_string())
254            .filter(|s| !s.is_empty())
255            .collect();
256        if names.is_empty() {
257            return None;
258        }
259        Some(
260            names
261                .into_iter()
262                .map(|name| ComponentImport {
263                    name: name.clone(),
264                    source: ImportSource::FuiPath(format!("ui::{}::{}", prefix, name)),
265                })
266                .collect(),
267        )
268    } else {
269        // Simple: "components::Button"
270        let name = rest.split("::").last()?.to_string();
271        Some(vec![ComponentImport {
272            name: name.clone(),
273            source: ImportSource::FuiPath(format!("ui::{}", rest)),
274        }])
275    }
276}
277
278fn parse_gpui_imports(path: &str) -> Option<Vec<ComponentImport>> {
279    let name = path.strip_prefix("gpui::")?;
280    // No grouped imports for gpui (for now)
281    if name.contains('{') || name.contains('}') || name.contains("::") {
282        return None;
283    }
284    Some(vec![ComponentImport {
285        name: name.to_string(),
286        source: ImportSource::Gpui,
287    }])
288}
289
290#[cfg(test)]
291mod tests {
292    use super::*;
293
294    // --- Happy path ---
295
296    #[test]
297    fn test_simple_element_parses_correctly() {
298        let doc = parse("<Text size=\"xl\">hello</Text>").unwrap();
299        assert_eq!(doc.root.name, "Text");
300        assert_eq!(doc.root.attrs.len(), 1);
301        assert_eq!(doc.root.attrs[0].0, "size");
302        assert_eq!(doc.root.attrs[0].1, AttrValue::String("xl".into()));
303        assert_eq!(doc.root.children.len(), 1);
304        assert_eq!(doc.root.children[0], Node::Text("hello".into()));
305    }
306
307    #[test]
308    fn test_frontmatter_with_imports() {
309        let doc = parse("---\nuse crate::lib::State;\n---\n<App></App>").unwrap();
310        assert_eq!(doc.frontmatter, Some("use crate::lib::State;".to_string()));
311        assert_eq!(doc.root.name, "App");
312    }
313
314    #[test]
315    fn test_interpolation_in_text() {
316        let doc = parse("<Text>{state.msg}</Text>").unwrap();
317        assert_eq!(doc.root.children.len(), 1);
318        assert_eq!(doc.root.children[0], Node::Interpolation("state.msg".to_string()));
319    }
320
321    #[test]
322    fn test_nested_elements_with_attrs() {
323        let src = "<Stack direction=\"vertical\">\n  <Text>oi</Text>\n</Stack>";
324        let doc = parse(src).unwrap();
325        assert_eq!(doc.root.name, "Stack");
326        assert_eq!(doc.root.children.len(), 1);
327        let child = match &doc.root.children[0] {
328            Node::Element(el) => el,
329            _ => panic!("expected element node"),
330        };
331        assert_eq!(child.name, "Text");
332        assert_eq!(child.children.len(), 1);
333        assert_eq!(child.children[0], Node::Text("oi".to_string()));
334    }
335
336    #[test]
337    fn test_frontmatter_with_state_directive() {
338        let doc = parse("---\n@state CounterState\n---\n<App></App>").unwrap();
339        assert_eq!(doc.state_type, Some("CounterState".to_string()));
340        assert_eq!(doc.frontmatter, None);
341    }
342
343    #[test]
344    fn test_frontmatter_with_state_and_imports() {
345        let doc = parse("---\nuse crate::state::MyState;\n@state MyState\n---\n<App></App>").unwrap();
346        assert_eq!(doc.state_type, Some("MyState".to_string()));
347        assert_eq!(doc.frontmatter, Some("use crate::state::MyState;".to_string()));
348    }
349
350    #[test]
351    fn test_self_closing_tag() {
352        let doc = parse("<Button onclick=\"handle_click\" />").unwrap();
353        assert_eq!(doc.root.name, "Button");
354        assert_eq!(doc.root.attrs.len(), 1);
355        assert_eq!(doc.root.attrs[0].0, "onclick");
356        assert!(doc.root.children.is_empty());
357    }
358
359    #[test]
360    fn text_interpolation_in_attribute() {
361        let doc = parse("<Text size=\"{state.size}\">hey</Text>").unwrap();
362        assert_eq!(doc.root.attrs[0].1, AttrValue::Interpolation("state.size".into()));
363    }
364
365    #[test]
366    fn test_multiple_attributes() {
367        let src = "<Stack direction=\"horizontal\" gap=\"16\" id=\"main-stack\"></Stack>";
368        let doc = parse(src).unwrap();
369        assert_eq!(doc.root.attrs.len(), 3);
370        assert_eq!(doc.root.attrs[0].0, "direction");
371        assert_eq!(doc.root.attrs[1].0, "gap");
372        assert_eq!(doc.root.attrs[2].0, "id");
373    }
374
375    // --- Edge cases ---
376
377    #[test]
378    fn test_element_with_no_children() {
379        let doc = parse("<Div></Div>").unwrap();
380        assert_eq!(doc.root.name, "Div");
381        assert!(doc.root.children.is_empty());
382    }
383
384    #[test]
385    fn test_element_with_no_attributes() {
386        let doc = parse("<View></View>").unwrap();
387        assert_eq!(doc.root.name, "View");
388        assert!(doc.root.attrs.is_empty());
389    }
390
391    #[test]
392    fn test_whitespace_only_text_is_ignored() {
393        let doc = parse("<Text>   \n  </Text>").unwrap();
394        assert!(doc.root.children.is_empty());
395    }
396
397    #[test]
398    fn test_mixed_children_text_and_interpolation() {
399        let src = "<Text>Hello {name} !</Text>";
400        let doc = parse(src).unwrap();
401        assert_eq!(doc.root.children.len(), 3);
402        assert_eq!(doc.root.children[0], Node::Text("Hello".into()));
403        assert_eq!(doc.root.children[1], Node::Interpolation("name".into()));
404    }
405
406    #[test]
407    fn test_deeply_nested_elements() {
408        let src = "<A><B><C><D><E></E></D></C></B></A>";
409        let doc = parse(src).unwrap();
410        assert_eq!(doc.root.name, "A");
411        match &doc.root.children[0] {
412            Node::Element(b) => {
413                assert_eq!(b.name, "B");
414                match &b.children[0] {
415                    Node::Element(c) => {
416                        assert_eq!(c.name, "C");
417                        match &c.children[0] {
418                            Node::Element(d) => {
419                                assert_eq!(d.name, "D");
420                                match &d.children[0] {
421                                    Node::Element(e) => assert_eq!(e.name, "E"),
422                                    _ => panic!("expected element E"),
423                                }
424                            }
425                            _ => panic!("expected element D"),
426                        }
427                    }
428                    _ => panic!("expected element C"),
429                }
430            }
431            _ => panic!("expected element B"),
432        }
433    }
434
435    #[test]
436    fn test_tag_name_with_hyphen() {
437        let doc = parse("<my-component></my-component>").unwrap();
438        assert_eq!(doc.root.name, "my-component");
439    }
440
441    #[test]
442    fn test_tag_name_starting_with_underscore() {
443        let doc = parse("<_custom></_custom>").unwrap();
444        assert_eq!(doc.root.name, "_custom");
445    }
446
447    #[test]
448    fn test_frontmatter_only_with_state_and_other_lines() {
449        let src = "---\n@state MyState\nconst X: i32 = 42;\n---\n<Root></Root>";
450        let doc = parse(src).unwrap();
451        assert_eq!(doc.state_type, Some("MyState".into()));
452        assert_eq!(doc.frontmatter, Some("const X: i32 = 42;".into()));
453    }
454
455    #[test]
456    fn test_multiline_interpolation_expression() {
457        let src = "<Text>{ state . count }</Text>";
458        let doc = parse(src).unwrap();
459        match &doc.root.children[0] {
460            Node::Interpolation(expr) => {
461                assert!(expr.contains("state"));
462                assert!(expr.contains("count"));
463            }
464            _ => panic!("expected interpolation"),
465        }
466    }
467
468    // --- Invalid inputs / Error handling ---
469
470    #[test]
471    fn test_empty_string_returns_error() {
472        let result = parse("");
473        assert!(result.is_err());
474    }
475
476    #[test]
477    fn test_only_whitespace_returns_error() {
478        let result = parse("   \n  \t  ");
479        assert!(result.is_err());
480    }
481
482    #[test]
483    fn test_unclosed_tag_returns_error() {
484        let result = parse("<Text>unclosed");
485        assert!(result.is_err());
486    }
487
488    #[test]
489    fn test_unopened_close_tag_returns_error() {
490        let result = parse("</Text>");
491        assert!(result.is_err());
492    }
493
494    #[test]
495    #[should_panic(expected = "mismatched close tag")]
496    fn test_mismatched_close_tag_panics() {
497        parse("<Div></Text>").unwrap();
498    }
499
500    #[test]
501    fn test_invalid_tag_name_returns_error() {
502        let result = parse("<123invalid></123invalid>");
503        assert!(result.is_err());
504    }
505
506    #[test]
507    fn test_unclosed_frontmatter_returns_error() {
508        let result = parse("---\n@state Foo\n<Root></Root>");
509        assert!(result.is_err());
510    }
511
512    #[test]
513    fn test_invalid_attribute_syntax_returns_error() {
514        let result = parse("<Text size=>hello</Text>");
515        assert!(result.is_err());
516    }
517
518    #[test]
519    fn test_invalid_attribute_value_no_quotes_returns_error() {
520        let result = parse("<Text size=xl>hello</Text>");
521        assert!(result.is_err());
522    }
523
524    #[test]
525    fn test_self_closing_tag_with_content_before_close_errors() {
526        // This should parse as: <Text> with children "hello" and then <Button /> after
527        // Actually, it's valid because <Text>hello<Button/ ></Text> is well-formed
528        let result = parse("<Text>hello<Button/></Text>");
529        assert!(result.is_ok());
530    }
531
532    // --- Contract / Structural tests ---
533
534    #[test]
535    fn test_document_structure_contract() {
536        let doc = parse("<Root></Root>").unwrap();
537        assert_eq!(doc.root.name, "Root");
538        assert!(doc.frontmatter.is_none());
539        assert!(doc.state_type.is_none());
540        assert!(doc.root.attrs.is_empty());
541        assert!(doc.root.children.is_empty());
542    }
543
544    #[test]
545    fn test_node_variants_contract() {
546        let src = r#"<Container>
547            text content
548            <Inner />
549            {interp}
550        </Container>"#;
551        let doc = parse(src).unwrap();
552        let types: Vec<&str> = doc
553            .root
554            .children
555            .iter()
556            .map(|n| match n {
557                Node::Text(_) => "text",
558                Node::Element(_) => "element",
559                Node::Interpolation(_) => "interpolation",
560            })
561            .collect();
562        assert_eq!(types, ["text", "element", "interpolation"]);
563    }
564
565    #[test]
566    fn test_attr_value_types_contract() {
567        let src = "<Text size=\"lg\" data-value=\"{expr}\" />";
568        let doc = parse(src).unwrap();
569        assert_eq!(doc.root.attrs[0].1, AttrValue::String("lg".into()));
570        assert_eq!(doc.root.attrs[1].1, AttrValue::Interpolation("expr".into()));
571    }
572
573    // --- Regression tests ---
574
575    #[test]
576    fn test_regression_trailing_whitespace_after_tag() {
577        let doc = parse("<Text>hello</Text>  ").unwrap();
578        assert_eq!(doc.root.name, "Text");
579    }
580
581    #[test]
582    fn test_regression_leading_whitespace_before_tag() {
583        let doc = parse("  <Text>hello</Text>").unwrap();
584        assert_eq!(doc.root.name, "Text");
585    }
586
587    #[test]
588    fn test_regression_nested_same_component_name() {
589        let src = "<Item><Item><Item></Item></Item></Item>";
590        let doc = parse(src).unwrap();
591        assert_eq!(doc.root.name, "Item");
592        if let Node::Element(child) = &doc.root.children[0] {
593            assert_eq!(child.name, "Item");
594            if let Node::Element(grandchild) = &child.children[0] {
595                assert_eq!(grandchild.name, "Item");
596            } else {
597                panic!("expected inner Item");
598            }
599        } else {
600            panic!("expected child Item");
601        }
602    }
603
604    #[test]
605    fn test_regression_interpolation_with_adjacent_text() {
606        let doc = parse("<Text>Count: {count} items</Text>").unwrap();
607        assert_eq!(doc.root.children.len(), 3);
608        assert_eq!(doc.root.children[0], Node::Text("Count:".into()));
609        assert_eq!(doc.root.children[1], Node::Interpolation("count".into()));
610        assert_eq!(doc.root.children[2], Node::Text("items".into()));
611    }
612
613    // --- Component imports ---
614
615    #[test]
616    fn test_simple_use_ui_import() {
617        let doc = parse("---\nuse ui::components::Button;\n---\n<Stack></Stack>").unwrap();
618        assert_eq!(doc.imports.len(), 1);
619        assert_eq!(doc.imports[0].name, "Button");
620        assert_eq!(
621            doc.imports[0].source,
622            ImportSource::FuiPath("ui::components::Button".into())
623        );
624    }
625
626    #[test]
627    fn test_use_ui_import_stripped_from_frontmatter() {
628        let doc = parse("---\nuse ui::components::Button;\n---\n<Stack></Stack>").unwrap();
629        assert!(doc.frontmatter.is_none());
630    }
631
632    #[test]
633    fn test_use_crate_import_preserved() {
634        let doc = parse("---\nuse crate::lib::State;\n---\n<App></App>").unwrap();
635        assert_eq!(doc.frontmatter, Some("use crate::lib::State;".to_string()));
636        assert!(doc.imports.is_empty());
637    }
638
639    #[test]
640    fn test_use_ui_and_crate_mixed() {
641        let src = "---\nuse crate::prelude::*;\nuse ui::components::Header;\n---\n<App></App>";
642        let doc = parse(src).unwrap();
643        assert_eq!(doc.imports.len(), 1);
644        assert_eq!(doc.imports[0].name, "Header");
645        assert!(doc.frontmatter.as_ref().unwrap().contains("use crate::prelude::*;"));
646        assert!(!doc.frontmatter.as_ref().unwrap().contains("use ui::"));
647    }
648
649    #[test]
650    fn test_grouped_use_ui_import() {
651        let src = "---\nuse ui::components::{Button, Input, Card};\n---\n<Stack></Stack>";
652        let doc = parse(src).unwrap();
653        assert_eq!(doc.imports.len(), 3);
654        assert_eq!(doc.imports[0].name, "Button");
655        assert_eq!(doc.imports[1].name, "Input");
656        assert_eq!(doc.imports[2].name, "Card");
657        for imp in &doc.imports {
658            match &imp.source {
659                ImportSource::FuiPath(p) => assert!(p.starts_with("ui::components::")),
660                _ => panic!("expected FuiPath"),
661            }
662        }
663    }
664
665    #[test]
666    fn test_use_gpui_import_kept_in_frontmatter() {
667        let doc = parse("---\nuse gpui::TextInput;\n---\n<Stack></Stack>").unwrap();
668        assert_eq!(doc.imports.len(), 1);
669        assert_eq!(doc.imports[0].name, "TextInput");
670        assert_eq!(doc.imports[0].source, ImportSource::Gpui);
671        // gpui imports are real Rust — stay in frontmatter
672        assert!(doc.frontmatter.as_ref().unwrap().contains("use gpui::TextInput;"));
673    }
674
675    #[test]
676    fn test_use_ui_with_state_and_imports() {
677        let src = "---\n@state AppState\nuse ui::components::Header;\n---\n<Stack></Stack>";
678        let doc = parse(src).unwrap();
679        assert_eq!(doc.state_type, Some("AppState".into()));
680        assert_eq!(doc.imports.len(), 1);
681        assert_eq!(doc.imports[0].name, "Header");
682    }
683
684    #[test]
685    fn test_imports_default_empty() {
686        let doc = parse("<Text>hi</Text>").unwrap();
687        assert!(doc.imports.is_empty());
688    }
689
690    #[test]
691    fn test_use_ui_with_deep_path() {
692        let doc = parse("---\nuse ui::layout::sidebar::SidePanel;\n---\n<Root></Root>").unwrap();
693        assert_eq!(doc.imports.len(), 1);
694        assert_eq!(doc.imports[0].name, "SidePanel");
695        assert_eq!(
696            doc.imports[0].source,
697            ImportSource::FuiPath("ui::layout::sidebar::SidePanel".into())
698        );
699    }
700}