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
|
use std::collections::HashMap;
use std::env;
use std::fmt;
use regex::Regex;
use super::functions;
use super::token::{Token, TokenString};
#[derive(Debug, Clone)]
pub(crate) enum MacroSource {
File,
CommandLineOrMakeflags,
Environment,
Builtin,
}
pub(crate) trait LookupInternal: for<'a> Fn(&'a str) -> String {}
impl<F: for<'a> Fn(&'a str) -> String> LookupInternal for F {}
pub(crate) trait MacroSetLike: Sized {
fn lookup_internal(&self, name: &str) -> String;
fn get(&self, name: &str) -> Option<&(MacroSource, TokenString)>;
fn expand(&self, text: &TokenString) -> String {
let mut result = String::new();
for token in text.tokens() {
match token {
Token::Text(t) => result.push_str(t),
Token::MacroExpansion { name, replacement } => {
let internal_macro_names = &['@', '?', '<', '*'][..];
let internal_macro_suffices = &['D', 'F'][..];
let just_internal = name.len() == 1 && name.starts_with(internal_macro_names);
let suffixed_internal = name.len() == 2
&& name.starts_with(internal_macro_names)
&& name.ends_with(internal_macro_suffices);
let macro_value = if just_internal || suffixed_internal {
self.lookup_internal(name)
} else {
self.get(name)
.map_or_else(String::new, |(_, macro_value)| self.expand(macro_value))
};
let macro_value = match replacement {
Some((subst1, subst2)) => {
let subst1 = self.expand(subst1);
let subst1_suffix = regex::escape(&subst1);
let subst1_suffix =
Regex::new(&format!(r"{}\b", subst1_suffix)).unwrap();
let subst2 = self.expand(subst2);
subst1_suffix.replace_all(¯o_value, subst2).to_string()
}
None => macro_value,
};
result.push_str(¯o_value);
}
Token::FunctionCall { name, args } => {
result.push_str(&functions::expand_call(name, args, self));
}
}
}
result
}
fn with_lookup<'a, 's: 'a>(
&'s self,
lookup: &'a dyn LookupInternal,
) -> MacroSetLikeWithLookup<'a, Self> {
MacroSetLikeWithLookup {
parent: self,
lookup,
}
}
fn with_overlay(&self) -> MacroSetLikeWithOverlay<Self> {
MacroSetLikeWithOverlay {
parent: self,
data: HashMap::new(),
}
}
}
pub(crate) struct MacroSetLikeWithLookup<'a, Parent: MacroSetLike> {
parent: &'a Parent,
lookup: &'a dyn LookupInternal,
}
impl<'a, Parent: MacroSetLike> MacroSetLike for MacroSetLikeWithLookup<'a, Parent> {
fn lookup_internal(&self, name: &str) -> String {
(self.lookup)(name)
}
fn get(&self, name: &str) -> Option<&(MacroSource, TokenString)> {
self.parent.get(name)
}
}
pub(crate) struct MacroSetLikeWithOverlay<'a, Parent: MacroSetLike> {
parent: &'a Parent,
data: HashMap<String, (MacroSource, TokenString)>,
}
impl<'a, Parent: MacroSetLike> MacroSetLike for MacroSetLikeWithOverlay<'a, Parent> {
fn lookup_internal(&self, name: &str) -> String {
self.parent.lookup_internal(name)
}
fn get(&self, name: &str) -> Option<&(MacroSource, TokenString)> {
self.data.get(name).or_else(|| self.parent.get(name))
}
}
impl<'a, Parent: MacroSetLike> MacroSetLikeWithOverlay<'a, Parent> {
pub(crate) fn set(&mut self, name: String, text: TokenString) {
self.data.insert(name, (MacroSource::File, text));
}
}
#[derive(Clone, Debug)]
pub(crate) struct MacroSet {
data: HashMap<String, (MacroSource, TokenString)>,
}
impl MacroSetLike for MacroSet {
fn lookup_internal(&self, _: &str) -> String {
panic!("can't lookup an internal macro value from a plain MacroSet");
}
fn get(&self, name: &str) -> Option<&(MacroSource, TokenString)> {
self.data.get(name)
}
}
impl MacroSet {
pub(crate) fn new() -> Self {
Self {
data: HashMap::new(),
}
}
pub(crate) fn add_builtins(&mut self) {
for (k, v) in builtins() {
self.data.insert(k.into(), (MacroSource::Builtin, v));
}
}
pub(crate) fn add_env(&mut self) {
for (k, v) in env::vars() {
if k != "MAKEFLAGS" && k != "SHELL" {
self.data
.insert(k, (MacroSource::Environment, TokenString::text(v)));
}
}
}
pub(crate) fn set(&mut self, name: String, source: MacroSource, text: TokenString) {
self.data.insert(name, (source, text));
}
pub(crate) fn is_defined(&self, name: &str) -> bool {
self.data.contains_key(name)
}
// `remove` is fine, but I think for "remove-and-return" `pop` is better
pub(crate) fn pop(&mut self, name: &str) -> Option<(MacroSource, TokenString)> {
self.data.remove(name)
}
}
impl fmt::Display for MacroSet {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let pieces = self
.data
.iter()
.map(|(k, (_, v))| format!("{}={}", k, v))
.collect::<Vec<_>>();
write!(f, "{}", pieces.join("\n"))
}
}
fn builtins() -> Vec<(&'static str, TokenString)> {
// Fuck it, might as well.
macro_rules! handle {
($value:ident) => {
stringify!($value)
};
($value:literal) => {
$value
};
}
macro_rules! make {
($($name:ident=$value:tt)+) => {vec![$(
(stringify!($name), handle!($value).parse().unwrap())
),+]};
}
make![
MAKE=makers
AR=ar
YACC=yacc
YFLAGS=""
LEX=lex
LFLAGS=""
LDFLAGS=""
AS=as
CC=cc
CXX="g++"
CPP="$(CC) -E"
FC=f77
PC=pc
CO=co
GET=get
LINT=lint
MAKEINFO=makeinfo
TEX=tex
TEXI2DVI=texi2dvi
WEAVE=weave
CWEAVE=cweave
TANGLE=tangle
CTANGLE=ctangle
RM="rm -f"
ARFLAGS="rv"
CFLAGS=""
FFLAGS=""
]
}
|