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