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
use std::fmt;
use ast::Ast;
use errors::{Error, ErrorCollector};
use tokenizer::Pos;
pub type IncludeHandler<'a> =
Fn(&Pos, &Include, &ErrorCollector, &Options) -> Ast + 'a;
#[derive(Debug)]
pub enum Include<'a> {
File { filename: &'a str },
Sequence { pattern: &'a str },
Mapping { pattern: &'a str },
#[doc(hidden)]
__Nonexhaustive,
}
pub struct Options<'a> {
include_handler: Box<IncludeHandler<'a>>,
}
pub trait DoInclude {
fn include(&self, pos: &Pos, _: &Include, err: &ErrorCollector) -> Ast;
}
impl<'a> DoInclude for Options<'a> {
fn include(&self, pos: &Pos, incl: &Include, err: &ErrorCollector) -> Ast {
(self.include_handler)(pos, incl, err, self)
}
}
fn unsupported_include(pos: &Pos, _: &Include,
err: &ErrorCollector, _: &Options)
-> Ast
{
err.add_error(Error::preprocess_error(pos,
format!("Includes are not supported")));
return Ast::void(pos);
}
impl<'a> Options<'a> {
pub fn default() -> Options<'a> {
Options {
include_handler: Box::new(unsupported_include),
}
}
pub fn allow_include<F>(&mut self, f: F)
-> &mut Options<'a>
where F: Fn(&Pos, &Include, &ErrorCollector, &Options) -> Ast + 'a
{
self.include_handler = Box::new(f);
self
}
}
impl<'a> fmt::Debug for Options<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Options")
.finish()
}
}