1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
use std::collections::{BTreeSet, BTreeMap};
use std::path::Path;
use std::fs::File;
use std::rc::Rc;
use std::cell::RefCell;
use std::io::prelude::*;
use escape::escape;
use inflector::cases::{classcase, pascalcase, snakecase};
use pest::Parser;
use pest::iterators::Pairs;
use quote::__private::TokenStream;
use quote::*;
use crate::errors::*;
use crate::slice::module::Module;
use crate::slice::enumeration::Enum;
use crate::slice::structure::Struct;
use crate::slice::interface::Interface;
use crate::slice::function::Function;
use crate::slice::exception::Exception;
use crate::slice::class::Class;
use crate::slice::types::IceType;

use super::{escape, function_argument::FunctionArgument, function_return::FunctionReturn, function_throws::FunctionThrows, struct_member::StructMember};


#[derive(Parser)]
#[grammar = "slice/ice.pest"]
pub struct IceParser;

pub trait ParsedObject {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized;
}

pub trait ParsedModule {
    fn parse(&mut self, rule: &mut Pairs<Rule>) -> Result<(), Box<dyn std::error::Error>> where Self: Sized;
}


impl ParsedModule for Module {
    fn parse(&mut self, rule: &mut Pairs<Rule>) -> Result<(), Box<dyn std::error::Error>> where Self: Sized {
        let it = rule.next().ok_or(
            Box::new(ParsingError::new("No more items"))
        )?;
        if it.as_rule() != Rule::keyword_module {
            return Err(Box::new(ParsingError::new(
                &format!("Expected keyword module but found {:?}", it.as_rule())
            )));
        }
        let it = rule.next().ok_or(
            Box::new(ParsingError::new("No more items"))
        )?;
        if it.as_rule() != Rule::identifier {
            return Err(Box::new(ParsingError::new(
                &format!("Expected identifier but found {:?}", it.as_rule())
            )));
        }
        let name = it.as_str();
        let module = match self.sub_modules.iter_mut().find(|f| f.name == name) {
            Some(module) => {
                module
            }
            None => {
                let mut new_module = Module::new(Rc::clone(&self.type_map));
                new_module.name = String::from(name);
                new_module.full_name = format!("{}::{}", self.full_name, new_module.name);
                self.sub_modules.push(new_module);
                self.sub_modules.last_mut().ok_or(
                    Box::new(ParsingError::new("Unexpected. No module found."))
                )?
            }
        };

        for child in rule {
            match child.as_rule() {
                Rule::block_open => {},
                Rule::any_block => {
                    for block in child.into_inner() {
                        match block.as_rule() {
                            Rule::module_block => {
                                module.parse(&mut block.into_inner())?;
                            },
                            Rule::enum_block => {
                                let enumeration = Enum::parse(block.into_inner())?;
                                self.type_map.borrow_mut().insert(enumeration.id.to_string(), module.snake_name());
                                module.add_enum(enumeration);
                            },
                            Rule::struct_block => {
                                let structure = Struct::parse(block.into_inner())?;
                                self.type_map.borrow_mut().insert(structure.id.to_string(), module.snake_name());
                                module.add_struct(structure);
                            },
                            Rule::class_block => {
                                let class = Class::parse(block.into_inner())?;
                                self.type_map.borrow_mut().insert(class.id.to_string(), module.snake_name());
                                module.add_class(class);
                            },
                            Rule::interface_block => {
                                let interface = Interface::parse(block.into_inner())?;
                                module.add_interface(interface);
                            },
                            Rule::exception_block => {
                                let exception = Exception::parse(block.into_inner())?;
                                self.type_map.borrow_mut().insert(exception.id.to_string(), module.snake_name());
                                module.add_exception(exception);
                            }
                            _ => return Err(Box::new(ParsingError::new(
                                &format!("Unexpected rule {:?}", block.as_rule())
                            )))
                        }
                    }
                },
                Rule::typedef => {
                    let mut vartype = IceType::VoidType;
                    let mut id = "";
                    for item in child.into_inner() {
                        match item.as_rule() {
                            Rule::typename => { vartype = IceType::from(&escape::escape(item.as_str()))? },
                            Rule::identifier => { id = item.as_str(); },
                            Rule::typedef_end => {
                                self.type_map.borrow_mut().insert(String::from(id), module.snake_name());
                                module.add_typedef(id, vartype.clone());
                            },
                            _ => return Err(Box::new(ParsingError::new(
                                &format!("Unexpected rule {:?}", item.as_rule())
                            )))
                        }
                    }
                }
                Rule::block_close => {},
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            };
        }
        Ok(())
    }
}

impl ParsedObject for Enum {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        let mut enumeration = Enum::empty();
        for child in rule {
            match child.as_rule() {
                Rule::keyword_enum => {},
                Rule::identifier => { 
                    enumeration.ice_id = String::from(child.as_str());
                    let id_str = format_ident!("{}", classcase::to_class_case(&enumeration.ice_id));
                    enumeration.id = quote! { #id_str };
                },
                Rule::block_open => {},
                Rule::enum_lines => {
                    for line in child.into_inner() {
                        match line.as_rule() {
                            // TODO: maybe add struct for each enum line
                            Rule::enum_line => {
                                let mut id = "";
                                let mut value: Option<i32> = None;
                                for item in line.into_inner() {
                                    match item.as_rule() {
                                        Rule::identifier => {
                                            id = item.as_str();
                                        },
                                        Rule::numeric_value => {
                                            value = Some(item.as_str().parse()?);
                                        },
                                        _ => return Err(Box::new(ParsingError::new(
                                            &format!("Unexpected rule {:?}", item.as_rule())
                                        )))
                                    };
                                }
                                enumeration.add_variant(id, value);
                            },
                            _ => return Err(Box::new(ParsingError::new(
                                &format!("Unexpected rule {:?}", line.as_rule())
                            )))
                        }
                    }
                },
                Rule::block_close => {},
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            }
        }

        Ok(enumeration)
    }
}

impl ParsedObject for StructMember {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        let mut member = StructMember::empty();
        let mut optional = false;
        let mut optional_tag = 0;
        for child in rule {
            match child.as_rule() {
                Rule::keyword_optional => {
                    optional = true;
                    for line in child.into_inner() {
                        match line.as_rule() {
                            Rule::optional_tag => {
                                optional_tag = line.as_str().parse()?;
                            }
                            _ => return Err(Box::new(ParsingError::new(
                                &format!("Unexpected rule {:?}", line.as_rule())
                            )))
                        }
                    }
                },
                Rule::typename => {
                    if optional {
                        member.r#type = IceType::Optional(Box::new(IceType::from(child.as_str())?), optional_tag);
                    } else {
                        member.r#type = IceType::from(child.as_str())?;
                    }
                },
                Rule::identifier => {
                    member.ice_id = String::from(child.as_str());
                    let id_str = format_ident!("{}", escape::escape(&snakecase::to_snake_case(&member.ice_id)));
                    member.id = quote! { #id_str };
                },
                Rule::struct_line_default | Rule::class_line_default => {
                    // TODO
                }
                Rule::struct_line_end => {
                },
                Rule::class_line_end => {
                },
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            }
        }
        Ok(member)
    }
}

impl ParsedObject for Struct {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        let mut structure = Struct::empty();
        for child in rule {
            match child.as_rule() {
                Rule::keyword_struct => {},
                Rule::identifier => { 
                    structure.ice_id = String::from(child.as_str());
                    let id_str = format_ident!("{}", classcase::to_class_case(&structure.ice_id));
                    structure.id = quote! { #id_str };
                },
                Rule::block_open => {},

                Rule::struct_line => {
                    let member = StructMember::parse(child.into_inner())?;
                    structure.add_member(member);
                },
                Rule::block_close => {},
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            }
        }

        Ok(structure)
    }
}

impl ParsedObject for Class {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        let mut class = Class::empty();
        for child in rule {
            match child.as_rule() {
                Rule::keyword_class => {},
                Rule::identifier => { 
                    class.ice_id = String::from(child.as_str());
                    let id_str = format_ident!("{}", classcase::to_class_case(&class.ice_id));
                    class.id = quote! { #id_str };
                },
                Rule::extends => {
                    for line in child.into_inner() {
                        match line.as_rule() {
                            Rule::keyword_extends => { },
                            Rule::identifier => {
                                class.extends = Some(IceType::from(line.as_str())?);
                            },
                            _ => return Err(Box::new(ParsingError::new(
                                &format!("Unexpected rule {:?}", line.as_rule())
                            )))
                        }
                    }
                }
                Rule::block_open => {},
                Rule::class_line => {
                    let member = StructMember::parse(child.into_inner())?;
                    class.add_member(member);
                },
                Rule::block_close => {},
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            }
        }

        Ok(class)
    }
}

impl ParsedObject for Interface {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        let mut interface = Interface::empty();
        for child in rule {
            match child.as_rule() {
                Rule::keyword_interface => {},
                Rule::identifier => { 
                    interface.ice_id = String::from(child.as_str());
                    let id_str = format_ident!("{}", classcase::to_class_case(&interface.ice_id));
                    interface.id = quote! { #id_str };
                },
                Rule::block_open => {},
                Rule::function => {
                    interface.add_function(Function::parse(child.into_inner())?);
                },
                Rule::block_close => {},
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            }
        }
        Ok(interface)
    }
}

impl ParsedObject for Function {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        let mut function = Function::empty();
        for child in rule {
            match child.as_rule() {
                Rule::keyword_idempotent => {
                    function.set_idempotent();
                }
                Rule::fn_return_proxy => {
                    function.return_type.set_proxy();
                }
                Rule::fn_return => {
                    function.return_type = FunctionReturn::parse(child.into_inner())?;
                },
                Rule::fn_name => { 
                    function.ice_id = String::from(child.as_str());
                    let id_str = format_ident!("{}", snakecase::to_snake_case(&function.ice_id));
                    function.id = quote! { #id_str };
                    
                },
                Rule::fn_arg_open => {},
                Rule::fn_arg_list => {
                    for arg in child.into_inner() {
                        match arg.as_rule() {
                            Rule::fn_arg | Rule::fn_arg_out => {
                                let arg = FunctionArgument::parse(arg.into_inner())?;
                                function.add_argument(arg);
                            }
                            _ => return Err(Box::new(ParsingError::new(
                                &format!("Unexpected rule {:?}", arg.as_rule())
                            )))
                        }
                    }
                },
                Rule::fn_arg_close => {},
                Rule::fn_throws => {
                    function.throws = FunctionThrows::parse(child.into_inner())?;
                }
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            }
        }
        Ok(function)
    }
}

impl ParsedObject for FunctionThrows {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        for child in rule {
            match child.as_rule() {
                Rule::keyword_throws => {}
                Rule::identifier => {
                    return Ok(FunctionThrows::new(IceType::from(child.as_str())?));
                }
                _ => { }
            }
        }
        return Err(Box::new(ParsingError::new(
            &format!("Did not find throw identifier")
        )))
    }
}

impl ParsedObject for FunctionReturn {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        let mut return_type = IceType::VoidType;
        let mut optional = false;
        let mut optional_tag = 0;
        for child in rule {
            match child.as_rule() {
                Rule::keyword_optional => {
                    optional = true;
                    let tag = child.into_inner().next().ok_or(Box::new(ParsingError::new("No more items")))?;

                    if tag.as_rule() != Rule::optional_tag {
                        return Err(Box::new(ParsingError::new(
                            &format!("Expected keyword optional_tag but found {:?}", tag.as_rule())
                        )));
                    }
                    optional_tag = tag.as_str().parse()?;
                }
                Rule::identifier => {
                    if optional {
                        return_type = IceType::Optional(Box::new(IceType::from(child.as_str())?), optional_tag);
                    } else {
                        return_type = IceType::from(child.as_str())?;
                    }
                }
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            }
        }
        Ok(FunctionReturn::new(return_type))
    }
}

impl ParsedObject for FunctionArgument {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        let mut id = TokenStream::new();
        let mut optional = false;
        let mut optional_tag = 0;
        let mut typename = IceType::VoidType;
        let mut out = false;

        for child in rule {
            match child.as_rule() {
                Rule::typename => {
                    if optional {
                        typename = IceType::Optional(Box::new(IceType::from(child.as_str())?), optional_tag);
                    } else {
                        typename = IceType::from(child.as_str())?;
                    }
                },
                Rule::identifier => {
                    let id_str = format_ident!("{}", escape(&snakecase::to_snake_case(child.as_str())));
                    id = quote! { #id_str }
                },
                Rule::keyword_out => out = true,
                Rule::keyword_optional => {
                    optional = true;
                    let tag = child.into_inner().next().ok_or(Box::new(ParsingError::new("No more items")))?;

                    if tag.as_rule() != Rule::optional_tag {
                        return Err(Box::new(ParsingError::new(
                            &format!("Expected keyword optional_tag but found {:?}", tag.as_rule())
                        )));
                    }
                    optional_tag = tag.as_str().parse()?;
                }
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            }
        }
        Ok(FunctionArgument::new(id, typename, out))
    }
}

impl ParsedObject for Exception {
    fn parse(rule: Pairs<Rule>) -> Result<Self, Box<dyn std::error::Error>> where Self: Sized {
        let mut exception = Exception::empty();
        for child in rule {
            match child.as_rule() {
                Rule::keyword_exception => {},
                Rule::identifier => { 
                    exception.ice_id = String::from(child.as_str());
                    let id_str = format_ident!("{}", pascalcase::to_pascal_case(&exception.ice_id));
                    exception.id = quote! { #id_str };
                },
                Rule::block_open => {},
                Rule::extends => {
                    for line in child.into_inner() {
                        match line.as_rule() {
                            Rule::keyword_extends => { },
                            Rule::identifier => {
                                exception.extends = Some(IceType::from(line.as_str())?);
                            },
                            _ => return Err(Box::new(ParsingError::new(
                                &format!("Unexpected rule {:?}", line.as_rule())
                            )))
                        }
                    }
                }
                Rule::struct_line => {
                    let member = StructMember::parse(child.into_inner())?;
                    exception.add_member(member);
                },
                Rule::block_close => {},
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", child.as_rule())
                )))
            }
        }

        Ok(exception)
    }
}

impl Module {
    fn parse_file(&mut self, file: &mut File, include_dir: &Path, parsed_files: &mut BTreeSet<String>) -> Result<(), Box<dyn std::error::Error>> {
        let mut content = String::new();
        file.read_to_string(&mut content)?;

        let pairs = IceParser::parse(Rule::ice, &content).unwrap();
        for pair in pairs {
            match pair.as_rule() {
                Rule::ice => {
                    for child in pair.into_inner() {
                        match child.as_rule() {
                            Rule::file_include => {
                                for item in child.into_inner() {
                                    match item.as_rule() {
                                        Rule::keyword_include => {},
                                        Rule::identifier => {
                                            let include = include_dir.join(format!("{}.ice", item.as_str()));
                                            let include_str = String::from(include_dir.join(format!("{}.ice", item.as_str())).to_str().unwrap());
                                            println!("  parsing include {} ... ", include_str);
                                            if parsed_files.contains(&include_str) {
                                                println!("  skip file!");
                                            } else {
                                                parsed_files.insert(include_str);
                                                let mut include_file = File::open(include)?;
                                                self.parse_file(&mut include_file, include_dir, parsed_files)?;
                                                println!("  finished include");
                                            }
                                        }
                                        _ => return Err(Box::new(ParsingError::new(
                                            &format!("Unexpected rule {:?}", item.as_rule())
                                        )))
                                    }
                                }
                            }
                            Rule::module_block => {
                                self.parse(&mut child.into_inner())?;
                            },
                            Rule::EOI => {
                                return Ok(())
                            },
                            _ => return Err(Box::new(ParsingError::new(
                                &format!("Unexpected rule {:?}", child.as_rule())
                            )))
                        }
                    }
                },
                _ => return Err(Box::new(ParsingError::new(
                    &format!("Unexpected rule {:?}", pair.as_rule())
                )))
            };
        }

        Err(Box::new(ParsingError::new("Unexpected error while parsing")))
    }
}

pub fn parse_ice_files(ice_files: &Vec<String>, include_dir: &str) -> Result<Module, Box<dyn std::error::Error>> {
    let mut parsed_files = BTreeSet::new();
    let mut root = Module::new(Rc::new(RefCell::new(BTreeMap::new())));
    for item in ice_files {
        println!("parsing {} ... ", item);
        if parsed_files.contains(item) {
            println!("skip file!");
        } else {
            parsed_files.insert(item.clone());
            let mut file = File::open(Path::new(&item))?;
            root.parse_file(&mut file, Path::new(include_dir), &mut parsed_files)?;
            println!("finished parsing!");
        }
    }

    Ok(root)
}


#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_function() {
        assert!(IceParser::parse(Rule::function, "void test();").is_ok());
        assert!(IceParser::parse(Rule::function, "long test();").is_ok());
        assert!(IceParser::parse(Rule::function, "long test(long width);").is_ok());
        assert!(IceParser::parse(Rule::function, "long test(long width, long height);").is_ok());
        assert!(IceParser::parse(Rule::function, "long test(long radius, out long area);").is_ok());
        assert!(IceParser::parse(Rule::function, "long test(out long area);").is_ok());
        assert!(IceParser::parse(Rule::function, "long test(out long area) throws exception;").is_ok());

        assert!(IceParser::parse(Rule::function, "123abc test();").is_err());
        assert!(IceParser::parse(Rule::function, "void 123abc();").is_err());
        assert!(IceParser::parse(Rule::function, "void test(123abc width);").is_err());
        assert!(IceParser::parse(Rule::function, "void test(long 123abc);").is_err());
        assert!(IceParser::parse(Rule::function, "void test(long width height);").is_err());
        assert!(IceParser::parse(Rule::function, "void test(out 123abc height);").is_err());
        assert!(IceParser::parse(Rule::function, "void test(out long 123abc);").is_err());
        assert!(IceParser::parse(Rule::function, "void test(out long result, long input);").is_err());
    }

    #[test]
    fn test_module_block() {
        assert!(IceParser::parse(Rule::ice, "#pragma once\nmodule Test { }").is_ok());

        assert!(IceParser::parse(Rule::module_block, "module Test { }").is_ok());
        assert!(IceParser::parse(Rule::module_block, "module Test { module Test2 {} }").is_ok());
        assert!(IceParser::parse(Rule::module_block, "module Test { enum Test2 { First } }").is_ok());
        assert!(IceParser::parse(Rule::module_block, "module Test { struct Test2 { long width; } }").is_ok());
        assert!(IceParser::parse(Rule::module_block, "module Test { interface Test2 { void test(); } }").is_ok());

        assert!(IceParser::parse(Rule::module_block, "module {}").is_err());
        assert!(IceParser::parse(Rule::module_block, "struct Test {}").is_err());
        assert!(IceParser::parse(Rule::module_block, "interface Test {}").is_err());
        assert!(IceParser::parse(Rule::module_block, "enum Test {}").is_err());
        assert!(IceParser::parse(Rule::module_block, "module 12Test {}").is_err());
    }

    #[test]
    fn test_enum_block() {
        assert!(IceParser::parse(Rule::enum_lines, "First = 0").is_ok());
        assert!(IceParser::parse(Rule::enum_lines, "First = 0, Second = 1").is_ok());
        assert!(IceParser::parse(Rule::enum_lines, "First").is_ok());
        assert!(IceParser::parse(Rule::enum_lines, "First, Second").is_ok());

        assert!(IceParser::parse(Rule::enum_block, "enum Test { First = 0 }").is_ok());
        assert!(IceParser::parse(Rule::enum_block, "enum Test { First }").is_ok());
        assert!(IceParser::parse(Rule::enum_block, "enum Test { First = 0, Second = 1 }").is_ok());
        assert!(IceParser::parse(Rule::enum_block, "enum Test { First, Second }").is_ok());

        assert!(IceParser::parse(Rule::enum_block, "struct Test {}").is_err());
        assert!(IceParser::parse(Rule::enum_block, "interface Test {}").is_err());
        assert!(IceParser::parse(Rule::enum_block, "module Test {}").is_err());
        assert!(IceParser::parse(Rule::enum_block, "enum 12Test {}").is_err());
        assert!(IceParser::parse(Rule::enum_block, "enum Test { 123abc }").is_err());
        assert!(IceParser::parse(Rule::enum_block, "enum Test { 123abc = 1 }").is_err());
        assert!(IceParser::parse(Rule::enum_block, "enum Test { First = X }").is_err());
    }

    #[test]
    fn test_struct_block() {
        assert!(IceParser::parse(Rule::struct_block, "struct Test { long width; }").is_ok());
        assert!(IceParser::parse(Rule::struct_block, "struct Test { long width; long height; }").is_ok());

        assert!(IceParser::parse(Rule::struct_block, "struct Test { long width }").is_err());
        assert!(IceParser::parse(Rule::struct_block, "struct Test { }").is_err());
        assert!(IceParser::parse(Rule::struct_block, "enum Test {}").is_err());
        assert!(IceParser::parse(Rule::struct_block, "inteface Test {}").is_err());
        assert!(IceParser::parse(Rule::struct_block, "module Test {}").is_err());
        assert!(IceParser::parse(Rule::struct_block, "struct 12Test {}").is_err());
        assert!(IceParser::parse(Rule::struct_block, "struct Test { 12long width; }").is_err());
        assert!(IceParser::parse(Rule::struct_block, "struct Test { long 12width; }").is_err());
        assert!(IceParser::parse(Rule::struct_block, "struct Test { struct Test2 { } }").is_err());
    }

    #[test]
    fn test_interface_block() {
        assert!(IceParser::parse(Rule::interface_block, "interface Test { void test(); }").is_ok());
        assert!(IceParser::parse(Rule::interface_block, "interface Test { void test(long width); }").is_ok());
        assert!(IceParser::parse(Rule::interface_block, "interface Test { void test(long width, long height); }").is_ok());

        assert!(IceParser::parse(Rule::interface_block, "interface Test { void 12test(); }").is_err());
        assert!(IceParser::parse(Rule::interface_block, "interface Test { test(); }").is_err());
        assert!(IceParser::parse(Rule::interface_block, "interface Test { void test(long 12width); }").is_err());
        assert!(IceParser::parse(Rule::interface_block, "enum Test {}").is_err());
        assert!(IceParser::parse(Rule::interface_block, "struct Test {}").is_err());
        assert!(IceParser::parse(Rule::interface_block, "module Test {}").is_err());
    }
}