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
use std::collections::HashSet;
use environment::LexingEnvironment;
use tokens::{ LexerOptions, TokenIter };
use self::matchers::Matchers;

mod delimiters;
mod matchers;

pub mod options;
pub mod iter;

/// Parses template file and converts it to a stream of tokens.
pub struct Lexer {
    options: LexerOptions,
    matchers: Matchers,
}

impl Lexer {

    /// Creates a new lexer with specified options and operator list.
    pub fn new(options: LexerOptions, operators: &HashSet<&'static str>) -> Lexer {
        Lexer {
            options: options,
            matchers: Matchers::new(
                &options,
                operators
            ),
        }
    }

    /// Initialize default lexer with default options.
    pub fn default(env: &LexingEnvironment) -> Lexer {
        Lexer::new(
            LexerOptions::default(),
            &env.operators
        )
    }

    /// Convert provided template into a token stream.
    pub fn tokens<'r, 'code>(&'r self, code: &'code str) -> TokenIter<'r, 'code>
    {
        TokenIter::new(self, code)
    }
}