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
#[macro_use]
extern crate lazy_static;
extern crate regex;
mod error;
pub use crate::error::Error;
use regex::Regex;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::marker::PhantomData;
#[derive(Debug, Default)]
pub struct Context<'a> {
included_files: BTreeMap<String, String>,
phantom: PhantomData<&'a String>,
}
impl<'a> Context<'a> {
pub fn new() -> Self {
Context {
..Default::default()
}
}
pub fn include<S>(&mut self, name: S, src: S) -> &mut Self
where
S: Into<String>,
{
self.included_files.insert(name.into(), src.into());
self
}
pub fn expand<S>(&self, src: S) -> Result<String, Error>
where
S: Into<String>,
{
let mut expanded_src = Vec::new();
self.expand_recursive(
&mut expanded_src,
&src.into(),
None,
&mut Vec::new(),
&mut BTreeSet::new(),
).map(move |_| expanded_src.join("\n"))
}
fn expand_recursive(
&'a self,
expanded_src: &mut Vec<String>,
src: &'a str,
in_file: Option<&'a str>,
include_stack: &mut Vec<&'a str>,
include_set: &mut BTreeSet<&'a str>,
) -> Result<(), Error> {
lazy_static! {
static ref INCLUDE_RE: Regex = Regex::new(
r#"^\s*#\s*(pragma\s*)?include\s+[<"](?P<file>.*)[>"]"#
).expect("failed to compile INCLUDE_RE regex");
}
let mut need_line_directive = false;
for (line_num, line) in src.lines().enumerate() {
if let Some(caps) = INCLUDE_RE.captures(line) {
let cap_match = caps
.name("file")
.expect("Could not find capture group with name \"file\"");
let included_file = cap_match.as_str();
if include_set.contains(&included_file) {
continue;
}
if include_stack.contains(&included_file) {
let in_file = in_file.map(|s| s.to_string());
let problem_include = included_file.to_string();
let include_stack = include_stack.into_iter().map(|s| s.to_string()).collect();
return Err(Error::RecursiveInclude {
in_file: in_file,
line_num: line_num,
problem_include: problem_include,
include_stack: include_stack,
});
}
if let Some(src) = self.included_files.get(included_file) {
include_stack.push(&included_file);
self.expand_recursive(
expanded_src,
&src,
Some(included_file),
include_stack,
include_set,
)?;
include_stack.pop();
need_line_directive = true;
} else {
let in_file = in_file.map(|s| s.to_string());
let problem_include = included_file.to_string();
return Err(Error::FileNotFound {
in_file: in_file,
line_num: line_num,
problem_include: problem_include,
});
}
} else {
if need_line_directive {
expanded_src.push(format!("#line {} 0", line_num + 1));
}
need_line_directive = false;
expanded_src.push(String::from(line));
}
}
if let Some(in_file) = in_file {
include_set.insert(in_file);
}
Ok(())
}
}