print_parse_errors

Function print_parse_errors 

Source
pub fn print_parse_errors(
    filename: &str,
    source: &str,
    errors: &[Rich<'_, char>],
)
Expand description

Pretty-print parse errors using Ariadne

Examples found in repository?
examples/usage.rs (line 48)
30fn example_1() {
31    println!("Example 1: Parsing a simple component\n");
32
33    let input = r#"Text("Hello, World!")"#;
34    println!("Input: {}\n", input);
35
36    match parse_component(input) {
37        Ok(component) => {
38            println!("✓ Component name: {}", component.name);
39            println!("✓ Arguments count: {}", component.arguments.arguments.len());
40
41            if let Some(Argument::Positioned { value, .. }) =
42                component.arguments.arguments.first()
43            {
44                println!("✓ First argument: {:?}", value);
45            }
46        }
47        Err(errors) => {
48            print_parse_errors("input.hypen", input, &errors);
49        }
50    }
51}
52
53fn example_2() {
54    println!("Example 2: Extracting named and positional arguments\n");
55
56    let input = r#"Button(text: "Click Me", enabled: true, color: blue)"#;
57    println!("Input: {}\n", input);
58
59    match parse_component(input) {
60        Ok(component) => {
61            println!("✓ Component: {}", component.name);
62
63            // Get named argument
64            if let Some(text) = component.arguments.get_named("text") {
65                println!("✓ Text argument: {:?}", text);
66            }
67
68            // Get boolean argument
69            if let Some(Value::Boolean(enabled)) = component.arguments.get_named("enabled") {
70                println!("✓ Enabled: {}", enabled);
71            }
72
73            // Iterate all arguments
74            println!("\nAll arguments:");
75            for (i, arg) in component.arguments.arguments.iter().enumerate() {
76                match arg {
77                    Argument::Named { key, value } => {
78                        println!("  [{}] {} = {:?}", i, key, value);
79                    }
80                    Argument::Positioned { position, value } => {
81                        println!("  [{}] pos {} = {:?}", i, position, value);
82                    }
83                }
84            }
85        }
86        Err(errors) => {
87            print_parse_errors("input.hypen", input, &errors);
88        }
89    }
90}
91
92fn example_3() {
93    println!("Example 3: Working with applicators (styling)\n");
94
95    let input = r#"
96        Text("Styled Text")
97            .fontSize(18)
98            .color(blue)
99            .padding(16)
100    "#;
101    println!("Input: {}\n", input);
102
103    match parse_component(input) {
104        Ok(component) => {
105            println!("✓ Component: {}", component.name);
106            println!("✓ Applicators count: {}", component.applicators.len());
107
108            println!("\nApplicators:");
109            for applicator in &component.applicators {
110                print!("  .{}(", applicator.name);
111                for (i, arg) in applicator.arguments.arguments.iter().enumerate() {
112                    if i > 0 {
113                        print!(", ");
114                    }
115                    match arg {
116                        Argument::Named { key, value } => print!("{}: {:?}", key, value),
117                        Argument::Positioned { value, .. } => print!("{:?}", value),
118                    }
119                }
120                println!(")");
121            }
122        }
123        Err(errors) => {
124            print_parse_errors("input.hypen", input, &errors);
125        }
126    }
127}
128
129fn example_4() {
130    println!("Example 4: Navigating the component tree\n");
131
132    let input = r#"
133        Column {
134            Text("Header")
135                .fontSize(24)
136
137            Row {
138                Button("Left")
139                Button("Right")
140            }
141
142            Text("Footer")
143        }
144    "#;
145    println!("Input: {}\n", input);
146
147    match parse_component(input) {
148        Ok(component) => {
149            println!("✓ Root component: {}", component.name);
150            println!("✓ Children count: {}", component.children.len());
151
152            // Walk the tree
153            println!("\nComponent tree:");
154            print_tree(&component, 0);
155
156            // Flatten to list
157            let all_components = component.flatten();
158            println!("\nFlattened tree ({} total components):", all_components.len());
159            for comp in &all_components {
160                println!(
161                    "  - {} (applicators: {})",
162                    comp.name,
163                    comp.applicators.len()
164                );
165            }
166        }
167        Err(errors) => {
168            print_parse_errors("input.hypen", input, &errors);
169        }
170    }
171}
172
173fn print_tree(component: &hypen_parser::ComponentSpecification, depth: usize) {
174    let indent = "  ".repeat(depth);
175    print!("{}{}", indent, component.name);
176
177    if !component.applicators.is_empty() {
178        print!(" [");
179        for (i, app) in component.applicators.iter().enumerate() {
180            if i > 0 {
181                print!(", ");
182            }
183            print!(".{}", app.name);
184        }
185        print!("]");
186    }
187    println!();
188
189    for child in &component.children {
190        print_tree(child, depth + 1);
191    }
192}
193
194fn example_5() {
195    println!("Example 5: Proper error handling\n");
196
197    let inputs = vec![
198        r#"Text("Valid input")"#,
199        r#"Text("Missing closing paren"#,
200        r#"Column { Text("Unclosed block"#,
201    ];
202
203    for (i, input) in inputs.iter().enumerate() {
204        println!("Input {}: {}\n", i + 1, input);
205
206        match parse_component(input) {
207            Ok(component) => {
208                println!("✓ Success: parsed component '{}'", component.name);
209            }
210            Err(errors) => {
211                println!("✗ Parse error:");
212                print_parse_errors(&format!("input-{}.hypen", i + 1), input, &errors);
213            }
214        }
215
216        if i < inputs.len() - 1 {
217            println!("\n{}\n", "-".repeat(60));
218        }
219    }
220}
More examples
Hide additional examples
examples/pretty_errors.rs (line 10)
3fn main() {
4    println!("=== Hypen Parser Error Examples with Ariadne ===\n");
5
6    // Example 1: Unclosed string
7    println!("Example 1: Unclosed string\n");
8    let input1 = r#"Text("Hello"#;
9    if let Err(errors) = parse_component(input1) {
10        print_parse_errors("example.hypen", input1, &errors);
11    }
12
13    println!("\n{}\n", "=".repeat(60));
14
15    // Example 2: Unclosed parentheses
16    println!("Example 2: Unclosed parentheses\n");
17    let input2 = r#"Text("Hello""#;
18    if let Err(errors) = parse_component(input2) {
19        print_parse_errors("example.hypen", input2, &errors);
20    }
21
22    println!("\n{}\n", "=".repeat(60));
23
24    // Example 3: Unclosed block
25    println!("Example 3: Unclosed block\n");
26    let input3 = r#"Column {
27    Text("Hello")
28    Row {
29        Text("World")
30"#;
31    if let Err(errors) = parse_component(input3) {
32        print_parse_errors("example.hypen", input3, &errors);
33    }
34
35    println!("\n{}\n", "=".repeat(60));
36
37    // Example 4: Invalid syntax in arguments
38    println!("Example 4: Invalid syntax in arguments\n");
39    let input4 = r#"Text(key: , value: 123)"#;
40    if let Err(errors) = parse_component(input4) {
41        print_parse_errors("example.hypen", input4, &errors);
42    }
43
44    println!("\n{}\n", "=".repeat(60));
45
46    // Example 5: Complex nested error
47    println!("Example 5: Complex nested error\n");
48    let input5 = r#"Column {
49    Text("Header")
50        .fontSize(18)
51        .color(blue)
52
53    Row {
54        Button("Sign In")
55            .padding(16
56        Text("Footer")
57    }
58}
59"#;
60    if let Err(errors) = parse_component(input5) {
61        print_parse_errors("example.hypen", input5, &errors);
62    }
63
64    println!("\n{}\n", "=".repeat(60));
65
66    // Example 6: Show a successful parse
67    println!("Example 6: Valid input (success)\n");
68    let input6 = r#"Column {
69    Text("Hello")
70        .fontSize(18)
71}"#;
72    match parse_component(input6) {
73        Ok(component) => println!("✓ Successfully parsed component: {}\n", component.name),
74        Err(errors) => print_parse_errors("example.hypen", input6, &errors),
75    }
76}