Skip to main content

inlined_parser/
inlined_parser.rs

1#![allow(dead_code)]
2
3#![allow(non_snake_case)]
4#![deny(deprecated)]
5
6#[cfg(not(test))]
7pub use base::{Node, ParseState, Data, Children, NodeContents, PreOrderNodes};
8
9#[macro_use]
10mod base {
11  pub use self::not::NotEx;
12  pub use self::and::And;
13  pub use self::fuse::Fuse;
14  pub use self::char_class::CharClass;
15  pub use self::literal::Literal;
16  pub use self::dot::Dot;
17  pub use self::option::OptionEx;
18  pub use self::star::Star;
19  pub use self::plus::Plus;
20  pub use self::or::Or;
21  pub use self::sequence::Sequence;
22  pub use self::wrap::WrapEx;
23  pub use self::node::{Node, NodeContents, Data, Children, PreOrderNodes};
24  mod node {
25    use std::fmt;
26    use std::str;
27    use std::fmt::{Result};
28    pub use self::NodeContents::{Data, Children};
29
30    static NO_NAME : &'static str = "<none>";
31
32    pub struct PreOrderNodes<'a, 'b:'a> {
33      queue: Vec<&'a Node<'b>>
34    }
35
36    impl<'a, 'b:'a> Iterator for PreOrderNodes<'a, 'b> {
37      type Item = &'a Node<'b>;
38
39      fn next( &mut self ) -> Option<&'a Node<'b>> {
40        match self.queue.pop() {
41          Some( node ) => {
42            match node.contents {
43              Children( ref x ) => {
44                for child in x.iter().rev() {
45                  self.queue.push( child )
46                }
47              }
48              _ => ()
49            };
50            Some( node )
51          }
52          _ => None
53        }
54      }
55    }
56
57
58    #[derive(Debug, PartialEq)]
59    pub enum NodeContents<'a> {
60      /// A `&[u8]` byte slice this node matched in the parse input. Only leaf nodes
61      /// have `Data` contents.
62      Data( &'a [u8] ),
63
64      /// Children of the node, if any. Only non-leaf nodes have `Children`
65      /// contents.
66      Children( Vec<Node<'a>> )
67    }
68
69
70    #[derive(PartialEq)]
71    pub struct Node<'a> {
72      /// The name of the node.
73      pub name: &'static str,
74
75      /// The (inclusive) start index of the range this node matches. It's the byte
76      /// (NOT char) offset of the parse input.
77      pub start: usize,
78
79      /// The (exclusive) end index of the range this node matches. It's the byte
80      /// (NOT char) offset of the parse input.
81      pub end: usize,
82
83      /// The contents of the node; this can be either children nodes or a matched
84      /// `&[u8]` slice.
85      pub contents: NodeContents<'a>
86    }
87
88
89    fn indent( formatter: &mut fmt::Formatter, indent_spaces: u32 )
90        -> fmt::Result {
91      for _ in 0 .. indent_spaces {
92        try!( write!( formatter, " " ) )
93      }
94      Ok(())
95    }
96
97
98    impl<'a> Node<'a> {
99      fn format( &self, formatter: &mut fmt::Formatter, indent_spaces: u32 )
100          -> fmt::Result {
101        try!( indent( formatter, indent_spaces ) );
102        try!( write!( formatter,
103                      "{0:?} [{1:?}, {2:?}>",
104                      self.displayName(), self.start, self.end ) );
105
106        match self.contents {
107          Data( data ) => {
108            match str::from_utf8( data ) {
109              Ok( string ) => {
110                try!( writeln!( formatter,
111                                ": \"{0:?}\"",
112                                string ) );
113              }
114              _ => {
115                try!( writeln!( formatter,
116                                ": \"{0:?}\"",
117                                data ) );
118              }
119            }
120          }
121          Children( ref children ) => {
122            try!( writeln!( formatter, "" ) );
123            for child in children.iter() {
124              try!( child.format( formatter, indent_spaces + 1) )
125            }
126          }
127        };
128
129        Ok(())
130      }
131
132      /// The node name if set, or "<none>" if unset.
133      pub fn displayName( &self ) -> &'static str {
134        if !self.name.is_empty() {
135          self.name
136        } else {
137          NO_NAME
138        }
139      }
140
141      /// Creates a `Node` with an empty name.
142      pub fn withoutName( start: usize, end: usize, contents: NodeContents<'a> )
143          -> Node<'a> {
144        Node { name: "", start: start, end: end, contents: contents }
145      }
146
147      /// Creates a `Node` with the provided `name` and makes it a parent of the
148      /// provided `children`.
149      pub fn withChildren( name: &'static str, mut children: Vec<Node<'a>> )
150          -> Node<'a> {
151        if children.len() == 1 && children[ 0 ].name.is_empty() {
152          match children.pop() {
153            Some( mut child ) => {
154              child.name = name;
155              return child;
156            }
157            _ => ()
158          }
159        }
160
161        let start = if children.len() != 0 {
162          children[ 0 ].start
163        } else {
164          0
165        };
166
167        let end = children.last().map_or( 0, |node| node.end );
168
169        Node { name: name,
170               start: start,
171               end: end,
172               contents: Children( children ) }
173      }
174
175
176      /// Traverses the tree rooted at the node with pre-order traversal. Includes
177      /// the `self` node as the first node.
178      #[allow(dead_code)]
179      pub fn preOrder<'b>( &'b self ) -> PreOrderNodes<'b, 'a> {
180        PreOrderNodes { queue: vec!( self ) }
181      }
182
183
184      /// Concatenates and returns all `&[u8]` data in the leaf nodes beneath
185      /// the current node.
186      #[allow(dead_code)]
187      pub fn matchedData( &self ) -> Vec<u8> {
188        match self.contents {
189          Data( x ) => x.to_vec(),
190          Children( ref children ) => {
191            let mut out : Vec<u8> = vec!();
192            for child in children.iter() {
193              out.extend( child.matchedData() );
194            }
195            out
196          }
197        }
198      }
199    }
200
201    impl<'a> fmt::Debug for Node<'a> {
202      fn fmt( &self, formatter: &mut fmt::Formatter ) -> fmt::Result {
203        self.format( formatter, 0 )
204      }
205    }
206  }
207  #[cfg(test)]
208  #[macro_use]
209  pub mod test_utils {
210    use base::ParseState;
211
212    pub fn ToParseState<'a>( bytes: &'a [u8] ) -> ParseState<'a> {
213      ParseState { input: bytes, offset: 0 }
214    }
215
216    macro_rules! input_state( ( $ex:expr ) => ( {
217          use base::ParseState;
218          ParseState { input: $ex.as_bytes(), offset: 0 }
219        } ) );
220  }
221
222  #[macro_use]
223  mod literal {
224    use super::{Expression, ParseState, ParseResult};
225
226    macro_rules! lit( ( $ex:expr ) => (
227          &base::Literal::new( $ex.as_bytes() ) ) );
228
229
230    pub struct Literal {
231      text: &'static [u8]
232    }
233
234
235    impl Literal {
236      pub fn new( text: &'static [u8] ) -> Literal {
237        Literal { text: text }
238      }
239    }
240
241
242    impl Expression for Literal {
243      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
244          Option< ParseResult<'a> > {
245        if parse_state.input.len() < self.text.len() ||
246           &parse_state.input[ .. self.text.len() ] != self.text {
247          return None;
248        }
249
250        parse_state.offsetToResult( parse_state.offset + self.text.len() )
251      }
252    }
253  }
254  #[macro_use]
255  mod char_class {
256    use base::unicode::{bytesFollowing, readCodepoint};
257    use super::{Expression, ParseState, ParseResult};
258
259    macro_rules! class( ( $ex:expr ) => (
260          &base::CharClass::new( $ex.as_bytes() ) ) );
261
262
263    fn toU32Vector( input: &[u8] ) -> Vec<u32> {
264      let mut i = 0;
265      let mut out_vec : Vec<u32> = vec!();
266      loop {
267        match input.get( i ) {
268          Some( byte ) => match bytesFollowing( *byte ) {
269            Some( num_following ) => {
270              if num_following > 0 {
271                match readCodepoint( &input[ i.. ] ) {
272                  Some( ch ) => {
273                    out_vec.push( ch as u32 );
274                    i += num_following + 1
275                  }
276                  _ => { out_vec.push( *byte as u32 ); i += 1 }
277                };
278              } else { out_vec.push( *byte as u32 ); i += 1 }
279            }
280            _ => { out_vec.push( *byte as u32 ); i += 1 }
281          },
282          _ => return out_vec
283        }
284      }
285    }
286
287
288    pub struct CharClass {
289      single_chars: Vec<u32>,
290      ranges: Vec<( u32, u32 )>
291    }
292
293
294    impl CharClass {
295      pub fn new( contents: &[u8] ) -> CharClass {
296        fn rangeAtIndex( index: usize, chars: &[u32] ) -> Option<( u32, u32 )> {
297          match ( chars.get( index ),
298                  chars.get( index + 1 ),
299                  chars.get( index + 2 ) ) {
300            ( Some( char1 ), Some( char2 ), Some( char3 ) )
301                if *char2 == '-' as u32 => Some( ( *char1, *char3 ) ),
302            _ => None
303          }
304        }
305
306        let chars = toU32Vector( &contents );
307        let mut char_class = CharClass { single_chars: Vec::new(),
308                                         ranges: Vec::new() };
309        let mut index = 0;
310        loop {
311          match rangeAtIndex( index, &chars ) {
312            Some( range ) => {
313              char_class.ranges.push( range );
314              index += 3;
315            }
316            _ => {
317              if index >= chars.len() {
318                break
319              }
320              char_class.single_chars.push( chars[ index ] );
321              index += 1;
322            }
323          };
324        }
325
326        char_class
327      }
328
329      fn matches( &self, character: u32 ) -> bool {
330        return self.single_chars.contains( &character ) ||
331          self.ranges.iter().any(
332            | &(from, to) | character >= from && character <= to );
333      }
334
335
336      fn applyToUtf8<'a>( &self, parse_state: &ParseState<'a> ) ->
337          Option< ParseResult<'a> > {
338        match readCodepoint( parse_state.input ) {
339          Some( ch ) if self.matches( ch as u32 ) => {
340            let num_following = bytesFollowing( parse_state.input[ 0 ] ).unwrap();
341            parse_state.offsetToResult( parse_state.offset + num_following + 1 )
342          }
343          _ => None
344        }
345      }
346
347
348      fn applyToBytes<'a>( &self, parse_state: &ParseState<'a> ) ->
349          Option< ParseResult<'a> > {
350        match parse_state.input.get( 0 ) {
351          Some( byte ) if self.matches( *byte as u32 ) => {
352            parse_state.offsetToResult( parse_state.offset + 1 )
353          }
354          _ => None
355        }
356      }
357    }
358
359
360    impl Expression for CharClass {
361      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
362          Option< ParseResult<'a> > {
363        self.applyToUtf8( parse_state ).or( self.applyToBytes( parse_state ) )
364      }
365    }
366  }
367  #[macro_use]
368  mod not {
369    use super::{Expression, ParseState, ParseResult};
370
371    macro_rules! not( ( $ex:expr ) => ( &base::NotEx::new($ex) ); );
372
373    pub struct NotEx<'a> {
374      expr: &'a ( Expression + 'a )
375    }
376
377
378    impl<'a> NotEx<'a> {
379      pub fn new( expr: &Expression ) -> NotEx {
380        NotEx { expr: expr }
381      }
382    }
383
384
385    impl<'b> Expression for NotEx<'b> {
386      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
387          Option< ParseResult<'a> > {
388        match self.expr.apply( parse_state ) {
389          Some( _ ) => None,
390          _ => Some( ParseResult::fromParseState( *parse_state ) )
391        }
392      }
393    }
394  }
395  #[macro_use]
396  mod and {
397
398    use super::{Expression, ParseState, ParseResult};
399
400    macro_rules! and( ( $ex:expr ) => ( &base::And::new( $ex ) ); );
401
402    pub struct And<'a> {
403      expr: &'a ( Expression + 'a )
404    }
405
406
407    impl<'a> And<'a> {
408      pub fn new( expr: &Expression ) -> And {
409        And { expr: expr }
410      }
411    }
412
413
414    impl<'b> Expression for And<'b> {
415      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
416          Option< ParseResult<'a> > {
417        match self.expr.apply( parse_state ) {
418          Some( _ ) => Some( ParseResult::fromParseState( *parse_state ) ),
419          _ => None
420        }
421      }
422    }
423  }
424  mod dot {
425    use super::{Expression, ParseState, ParseResult};
426    use base::unicode::{bytesFollowing, readCodepoint};
427
428    pub struct Dot;
429    impl Expression for Dot {
430      fn apply<'a>( &self, parse_state: &ParseState<'a> ) -> Option< ParseResult<'a> > {
431        match readCodepoint( parse_state.input ) {
432          Some( _ ) => {
433            let num_following = bytesFollowing( parse_state.input[ 0 ] ).unwrap();
434            return parse_state.offsetToResult(
435              parse_state.offset + num_following + 1 )
436          }
437          _ => ()
438        }
439
440        match parse_state.input.get( 0 ) {
441          Some( _ ) => parse_state.offsetToResult( parse_state.offset + 1 ),
442          _ => None
443        }
444      }
445    }
446  }
447  #[macro_use]
448  mod option {
449    use super::{Expression, ParseState, ParseResult};
450
451    macro_rules! opt( ( $ex:expr ) => ( &base::OptionEx::new( $ex ) ); );
452
453    pub struct OptionEx<'a> {
454      expr: &'a ( Expression + 'a )
455    }
456
457
458    impl<'a> OptionEx<'a> {
459      pub fn new( expr: &Expression ) -> OptionEx {
460        OptionEx { expr: expr }
461      }
462    }
463
464
465    impl<'b> Expression for OptionEx<'b> {
466      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
467          Option< ParseResult<'a> > {
468        self.expr.apply( parse_state ).or(
469          Some( ParseResult::fromParseState( *parse_state ) ) )
470      }
471    }
472  }
473  #[macro_use]
474  mod star {
475    use super::{Expression, ParseState, ParseResult};
476
477    macro_rules! star( ( $ex:expr ) => ( &base::Star::new( $ex ) ); );
478
479    pub struct Star<'a> {
480      expr: &'a ( Expression + 'a )
481    }
482
483
484    impl<'b> Star<'b> {
485      pub fn new( expr: &Expression ) -> Star {
486        Star { expr: expr }
487      }
488    }
489
490
491    impl<'b> Expression for Star<'b> {
492      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
493          Option< ParseResult<'a> > {
494        let mut final_result = ParseResult::fromParseState( *parse_state );
495        loop {
496          match self.expr.apply( &final_result.parse_state ) {
497            Some( result ) => {
498              final_result.parse_state = result.parse_state;
499              final_result.nodes.extend( result.nodes.into_iter() );
500            }
501            _ => break
502          }
503        }
504        Some( final_result )
505      }
506    }
507  }
508  #[macro_use]
509  mod plus {
510    use super::{Expression, ParseState, ParseResult};
511
512    macro_rules! plus( ( $ex:expr ) => ( &base::Plus::new( $ex ) ); );
513
514    pub struct Plus<'a> {
515      expr: &'a ( Expression + 'a )
516    }
517
518
519    impl<'b> Plus<'b> {
520      pub fn new( expr: &Expression ) -> Plus {
521        Plus { expr: expr }
522      }
523    }
524
525
526    impl<'b> Expression for Plus<'b> {
527      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
528          Option< ParseResult<'a> > {
529        let mut final_result = ParseResult::fromParseState( *parse_state );
530        let mut num_matches = 0;
531        loop {
532          match self.expr.apply( &final_result.parse_state ) {
533            Some( result ) => {
534              final_result.parse_state = result.parse_state;
535              final_result.nodes.extend( result.nodes.into_iter() );
536              num_matches += 1;
537            }
538            _ => break
539          }
540        }
541
542        if num_matches > 0 {
543          Some( final_result )
544        } else {
545          None
546        }
547      }
548    }
549  }
550  #[macro_use]
551  mod or {
552    use super::{Expression, ParseState, ParseResult};
553
554    macro_rules! or( ( $( $ex:expr ),* ) => (
555        &base::Or::new( &[ $( $ex ),* ] ) ); );
556
557    pub struct Or<'a> {
558      exprs: &'a [&'a (Expression + 'a)]
559    }
560
561
562    impl<'b> Or<'b> {
563      pub fn new<'a>( exprs: &'a [&Expression] ) -> Or<'a> {
564        Or { exprs: exprs }
565      }
566    }
567
568
569    impl<'b> Expression for Or<'b> {
570      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
571          Option< ParseResult<'a> > {
572        for expr in self.exprs.iter() {
573          match expr.apply( parse_state ) {
574            result @ Some( _ ) => return result,
575            _ => ()
576          }
577        }
578        None
579      }
580    }
581  }
582  #[macro_use]
583  mod fuse {
584    use super::{Expression, ParseState, ParseResult};
585
586    macro_rules! fuse( ( $ex:expr ) => ( &base::Fuse::new( $ex ) ); );
587
588    pub struct Fuse<'a> {
589      expr: &'a ( Expression + 'a )
590    }
591
592
593    impl<'a> Fuse<'a> {
594      pub fn new( expr: & Expression ) -> Fuse {
595        Fuse { expr: expr }
596      }
597    }
598
599
600    impl<'b> Expression for Fuse<'b> {
601      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
602          Option< ParseResult<'a> > {
603        self.expr.apply( parse_state ).and_then(
604          |result| parse_state.offsetToResult( result.parse_state.offset ) )
605      }
606    }
607  }
608  #[macro_use]
609  mod sequence {
610    use super::{Expression, ParseState, ParseResult};
611
612    macro_rules! seq( ( $( $ex:expr ),* ) => (
613        &base::Sequence::new( &[ $( $ex ),* ] ) ); );
614
615    pub struct Sequence<'a> {
616      exprs: &'a [&'a (Expression + 'a)]
617    }
618
619
620    impl<'b> Sequence<'b> {
621      pub fn new<'a>( exprs: &'a [&Expression] ) -> Sequence<'a> {
622        Sequence { exprs: exprs }
623      }
624    }
625
626
627    impl<'b> Expression for Sequence<'b> {
628      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
629          Option< ParseResult<'a> > {
630        let mut final_result = ParseResult::fromParseState( *parse_state );
631        for expr in self.exprs.iter() {
632          match expr.apply( &final_result.parse_state ) {
633            Some( result ) => {
634              final_result.parse_state = result.parse_state;
635              final_result.nodes.extend( result.nodes.into_iter() );
636            }
637            _ => return None
638          }
639        }
640        Some( final_result )
641      }
642    }
643  }
644  #[macro_use]
645  mod wrap {
646    use super::{Expression, ParseState, ParseResult, Rule};
647
648    macro_rules! ex( ( $ex:expr ) => ( &base::WrapEx{ rule: $ex } ); );
649
650    pub struct WrapEx {
651      pub rule: Rule
652    }
653
654
655    impl Expression for WrapEx {
656      fn apply<'a>( &self, parse_state: &ParseState<'a> ) ->
657          Option< ParseResult<'a> > {
658        (self.rule)( parse_state )
659      }
660    }
661  }
662  mod unicode {
663    use std::char;
664    pub static UTF8_1BYTE_FOLLOWING: u8 = 0b11000000;
665    pub static UTF8_2BYTE_FOLLOWING: u8 = 0b11100000;
666    pub static UTF8_3BYTE_FOLLOWING: u8 = 0b11110000;
667
668    pub fn readCodepoint( input: &[u8] ) -> Option< char > {
669      fn isContinuationByte( byte: u8 ) -> bool {
670        byte & 0b11000000 == 0b10000000
671      }
672
673      fn codepointBitsFromLeadingByte( byte: u8 ) -> u32 {
674        let good_bits =
675          if isAscii( byte ) {
676            byte
677          } else if byte & 0b11100000 == UTF8_1BYTE_FOLLOWING {
678            byte & 0b00011111
679          } else if byte & 0b11110000 == UTF8_2BYTE_FOLLOWING {
680            byte & 0b00001111
681          } else {
682            byte & 0b00000111
683          };
684        good_bits as u32
685      }
686
687      fn codepointBitsFromContinuationByte( byte: u8 ) -> u32 {
688        ( byte & 0b00111111 ) as u32
689      }
690
691      input.get( 0 )
692        .and_then( |first_byte| {
693          bytesFollowing( *first_byte ).and_then( |num_following| {
694            let mut codepoint: u32 =
695              codepointBitsFromLeadingByte( *first_byte ) << 6 * num_following;
696            for i in 1 .. num_following + 1 {
697              match input.get( i ) {
698                Some( byte ) if isContinuationByte( *byte ) => {
699                  codepoint |= codepointBitsFromContinuationByte( *byte ) <<
700                    6 * ( num_following - i );
701                }
702                _ => return None
703              }
704            }
705            char::from_u32( codepoint )
706          })
707        })
708    }
709
710
711    pub fn bytesFollowing( byte: u8 ) -> Option< usize > {
712      if isAscii( byte ) {
713        Some( 0 )
714      } else if byte & 0b11100000 == UTF8_1BYTE_FOLLOWING {
715        Some( 1 )
716      } else if byte & 0b11110000 == UTF8_2BYTE_FOLLOWING {
717        Some( 2 )
718      } else if byte & 0b11111000 == UTF8_3BYTE_FOLLOWING {
719        Some( 3 )
720      } else {
721        None
722      }
723    }
724
725
726    pub fn isAscii( byte: u8 ) -> bool {
727      return byte & 0b10000000 == 0;
728    }
729  }
730
731
732  #[doc(hidden)]
733  #[derive(Debug, Clone, PartialEq, Copy)]
734  pub struct ParseState<'a> {
735    pub input: &'a [u8],
736    pub offset: usize
737  }
738
739
740  impl<'a> ParseState<'a> {
741    fn advanceTo( &self, new_offset: usize ) -> ParseState<'a> {
742      let mut clone = self.clone();
743      clone.input = &clone.input[ new_offset - clone.offset .. ];
744      clone.offset = new_offset;
745      clone
746    }
747
748    fn sliceTo( &self, new_offset: usize ) -> &'a [u8] {
749      &self.input[ .. new_offset - self.offset ]
750    }
751
752    fn offsetToResult( &self, new_offset: usize )
753        -> Option< ParseResult<'a> > {
754      Some( ParseResult::oneNode(
755              Node::withoutName( self.offset,
756                                 new_offset,
757                                 Data( self.sliceTo( new_offset ) ) ),
758              self.advanceTo( new_offset ) ) )
759    }
760  }
761
762  #[doc(hidden)]
763  pub struct ParseResult<'a> {
764    pub nodes: Vec< Node<'a> >,
765    pub parse_state: ParseState<'a>
766  }
767
768
769  impl<'a> ParseResult<'a> {
770    pub fn oneNode( node: Node<'a>, parse_state: ParseState<'a> )
771        -> ParseResult<'a> {
772      ParseResult { nodes: vec!( node ), parse_state: parse_state }
773    }
774
775    pub fn fromParseState( parse_state: ParseState<'a> ) -> ParseResult<'a> {
776      ParseResult { nodes: vec!(), parse_state: parse_state }
777    }
778  }
779
780
781  pub trait Expression {
782    fn apply<'a>( &self, parse_state: &ParseState<'a> )
783        -> Option< ParseResult<'a> >;
784  }
785
786  pub type Rule = for<'a> fn( &ParseState<'a> ) -> Option< ParseResult<'a> >;
787}
788
789macro_rules! rule(
790  (
791    $name:ident <- $body:expr
792  ) => (
793    pub fn $name<'a>( parse_state: &base::ParseState<'a> )
794         -> std::option::Option< base::ParseResult<'a> > {
795      use base::Expression;
796      use base::Node;
797      use base::ParseResult;
798      use std::clone::Clone;
799      use std::option::Option::{Some, None};
800
801      match $body.apply( parse_state ) {
802        Some( result ) => {
803          let state = result.parse_state.clone();
804          Some( ParseResult::oneNode(
805              Node::withChildren( stringify!( $name ), result.nodes ), state ) )
806        }
807        _ => None
808      }
809    }
810  );
811);
812
813#[cfg(not(test))]
814pub fn parse<'a>( input: &'a [u8] ) -> Option< Node<'a> > {
815  let parse_state = ParseState { input: input, offset: 0 };
816  match rules::Grammar( &parse_state ) {
817    Some( result ) => Some( result.nodes.into_iter().next().unwrap() ),
818    _ => None
819  }
820}
821
822
823mod rules {
824  #![no_implicit_prelude]
825
826  use base;
827  use std;
828
829  rule!( Grammar <- seq!( ex!( Spacing ), plus!( ex!( Definition ) ), ex!( EndOfFile ) ) );
830  rule!( Definition <- seq!( ex!( Identifier ), ex!( ARROW ), ex!( Expression ) ) );
831  rule!( Expression <- seq!( ex!( Sequence ), star!( seq!( ex!( SLASH ), ex!( Sequence ) ) ) ) );
832  rule!( Sequence <- star!( ex!( Prefix ) ) );
833  rule!( Prefix <- seq!( opt!( or!( ex!( AND ), ex!( NOT ), ex!( FUSE ) ) ), ex!( Suffix ) ) );
834  rule!( Suffix <- seq!( ex!( Primary ), opt!( or!( ex!( QUESTION ), ex!( STAR ), ex!( PLUS ) ) ) ) );
835  rule!( Primary <- or!( seq!( ex!( Identifier ), not!( ex!( ARROW ) ) ), seq!( ex!( OPEN ), ex!( Expression ), ex!( CLOSE ) ), ex!( Literal ), ex!( Class ), ex!( DOT ) ) );
836  rule!( Identifier <- seq!( fuse!( seq!( ex!( IdentStart ), star!( ex!( IdentCont ) ) ) ), ex!( Spacing ) ) );
837  rule!( IdentStart <- class!( "a-zA-Z_" ) );
838  rule!( IdentCont <- or!( ex!( IdentStart ), class!( "0-9" ) ) );
839  rule!( Literal <- seq!( fuse!( or!( seq!( class!( "'" ), star!( seq!( not!( class!( "'" ) ), ex!( Char ) ) ), class!( "'" ) ), seq!( class!( "\"" ), star!( seq!( not!( class!( "\"" ) ), ex!( Char ) ) ), class!( "\"" ) ) ) ), ex!( Spacing ) ) );
840  rule!( Class <- seq!( lit!( "[" ), star!( seq!( not!( lit!( "]" ) ), ex!( Range ) ) ), lit!( "]" ), ex!( Spacing ) ) );
841  rule!( Range <- or!( seq!( ex!( Char ), lit!( "-" ), ex!( Char ) ), ex!( Char ) ) );
842  rule!( Char <- or!( seq!( lit!( "\\" ), class!( "nrt'\"[]\\" ) ), seq!( lit!( "\\" ), class!( "0-2" ), class!( "0-7" ), class!( "0-7" ) ), seq!( lit!( "\\" ), class!( "0-7" ), opt!( class!( "0-7" ) ) ), seq!( not!( lit!( "\\" ) ), &base::Dot ) ) );
843  rule!( ARROW <- or!( ex!( FUSEARROW ), ex!( LEFTARROW ) ) );
844  rule!( LEFTARROW <- seq!( lit!( "<-" ), ex!( Spacing ) ) );
845  rule!( FUSEARROW <- seq!( lit!( "<~" ), ex!( Spacing ) ) );
846  rule!( SLASH <- seq!( lit!( "/" ), ex!( Spacing ) ) );
847  rule!( AND <- seq!( lit!( "&" ), ex!( Spacing ) ) );
848  rule!( NOT <- seq!( lit!( "!" ), ex!( Spacing ) ) );
849  rule!( QUESTION <- seq!( lit!( "?" ), ex!( Spacing ) ) );
850  rule!( STAR <- seq!( lit!( "*" ), ex!( Spacing ) ) );
851  rule!( PLUS <- seq!( lit!( "+" ), ex!( Spacing ) ) );
852  rule!( OPEN <- seq!( lit!( "(" ), ex!( Spacing ) ) );
853  rule!( CLOSE <- seq!( lit!( ")" ), ex!( Spacing ) ) );
854  rule!( DOT <- seq!( lit!( "." ), ex!( Spacing ) ) );
855  rule!( FUSE <- seq!( lit!( "~" ), ex!( Spacing ) ) );
856  rule!( Spacing <- fuse!( star!( or!( ex!( Space ), ex!( Comment ) ) ) ) );
857  rule!( Comment <- fuse!( seq!( lit!( "#" ), star!( seq!( not!( ex!( EndOfLine ) ), &base::Dot ) ), ex!( EndOfLine ) ) ) );
858  rule!( Space <- or!( lit!( " " ), lit!( "\t" ), ex!( EndOfLine ) ) );
859  rule!( EndOfLine <- or!( lit!( "\r\n" ), lit!( "\n" ), lit!( "\r" ) ) );
860  rule!( EndOfFile <- not!( &base::Dot ) );
861  
862}