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, .. }) =
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
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}