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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use std::str::{self, FromStr};
use std::iter;
use std::fmt;

use util::{CargoError, CargoResult, human};

#[derive(Clone, PartialEq, Debug)]
pub enum Cfg {
    Name(String),
    KeyPair(String, String),
}

#[derive(Clone, PartialEq, Debug)]
pub enum CfgExpr {
    Not(Box<CfgExpr>),
    All(Vec<CfgExpr>),
    Any(Vec<CfgExpr>),
    Value(Cfg),
}

#[derive(PartialEq)]
enum Token<'a> {
    LeftParen,
    RightParen,
    Ident(&'a str),
    Comma,
    Equals,
    String(&'a str),
}

struct Tokenizer<'a> {
    s: iter::Peekable<str::CharIndices<'a>>,
    orig: &'a str,
}

struct Parser<'a> {
    t: iter::Peekable<Tokenizer<'a>>,
}

impl FromStr for Cfg {
    type Err = Box<CargoError>;

    fn from_str(s: &str) -> CargoResult<Cfg> {
        let mut p = Parser::new(s);
        let e = p.cfg()?;
        if p.t.next().is_some() {
            bail!("malformed cfg value or key/value pair")
        }
        Ok(e)
    }
}

impl fmt::Display for Cfg {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Cfg::Name(ref s) => s.fmt(f),
            Cfg::KeyPair(ref k, ref v) => write!(f, "{} = \"{}\"", k, v),
        }
    }
}

impl CfgExpr {
    pub fn matches(&self, cfg: &[Cfg]) -> bool {
        match *self {
            CfgExpr::Not(ref e) => !e.matches(cfg),
            CfgExpr::All(ref e) => e.iter().all(|e| e.matches(cfg)),
            CfgExpr::Any(ref e) => e.iter().any(|e| e.matches(cfg)),
            CfgExpr::Value(ref e) => cfg.contains(e),
        }
    }
}

impl FromStr for CfgExpr {
    type Err = Box<CargoError>;

    fn from_str(s: &str) -> CargoResult<CfgExpr> {
        let mut p = Parser::new(s);
        let e = p.expr()?;
        if p.t.next().is_some() {
            bail!("can only have one cfg-expression, consider using all() or \
                   any() explicitly")
        }
        Ok(e)
    }
}

impl fmt::Display for CfgExpr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            CfgExpr::Not(ref e) => write!(f, "not({})", e),
            CfgExpr::All(ref e) => write!(f, "all({})", CommaSep(e)),
            CfgExpr::Any(ref e) => write!(f, "any({})", CommaSep(e)),
            CfgExpr::Value(ref e) => write!(f, "{}", e),
        }
    }
}

struct CommaSep<'a, T: 'a>(&'a [T]);

impl<'a, T: fmt::Display> fmt::Display for CommaSep<'a, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (i, v) in self.0.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{}", v)?;
        }
        Ok(())
    }
}

impl<'a> Parser<'a> {
    fn new(s: &'a str) -> Parser<'a> {
        Parser {
            t: Tokenizer {
                s: s.char_indices().peekable(),
                orig: s,
            }.peekable(),
        }
    }

    fn expr(&mut self) -> CargoResult<CfgExpr> {
        match self.t.peek() {
            Some(&Ok(Token::Ident(op @ "all"))) |
            Some(&Ok(Token::Ident(op @ "any"))) => {
                self.t.next();
                let mut e = Vec::new();
                self.eat(Token::LeftParen)?;
                while !self.try(Token::RightParen) {
                    e.push(self.expr()?);
                    if !self.try(Token::Comma) {
                        self.eat(Token::RightParen)?;
                        break
                    }
                }
                if op == "all" {
                    Ok(CfgExpr::All(e))
                } else {
                    Ok(CfgExpr::Any(e))
                }
            }
            Some(&Ok(Token::Ident("not"))) => {
                self.t.next();
                self.eat(Token::LeftParen)?;
                let e = self.expr()?;
                self.eat(Token::RightParen)?;
                Ok(CfgExpr::Not(Box::new(e)))
            }
            Some(&Ok(..)) => self.cfg().map(CfgExpr::Value),
            Some(&Err(..)) => {
                Err(self.t.next().unwrap().err().unwrap())
            }
            None => bail!("expected start of a cfg expression, \
                           found nothing"),
        }
    }

    fn cfg(&mut self) -> CargoResult<Cfg> {
        match self.t.next() {
            Some(Ok(Token::Ident(name))) => {
                let e = if self.try(Token::Equals) {
                    let val = match self.t.next() {
                        Some(Ok(Token::String(s))) => s,
                        Some(Ok(t)) => bail!("expected a string, found {}",
                                             t.classify()),
                        Some(Err(e)) => return Err(e),
                        None => bail!("expected a string, found nothing"),
                    };
                    Cfg::KeyPair(name.to_string(), val.to_string())
                } else {
                    Cfg::Name(name.to_string())
                };
                Ok(e)
            }
            Some(Ok(t)) => bail!("expected identifier, found {}", t.classify()),
            Some(Err(e)) => Err(e),
            None => bail!("expected identifier, found nothing"),
        }
    }

    fn try(&mut self, token: Token<'a>) -> bool {
        match self.t.peek() {
            Some(&Ok(ref t)) if token == *t => {}
            _ => return false,
        }
        self.t.next();
        true
    }

    fn eat(&mut self, token: Token<'a>) -> CargoResult<()> {
        match self.t.next() {
            Some(Ok(ref t)) if token == *t => Ok(()),
            Some(Ok(t)) => bail!("expected {}, found {}", token.classify(),
                                 t.classify()),
            Some(Err(e)) => Err(e),
            None => bail!("expected {}, but cfg expr ended", token.classify()),
        }
    }
}

impl<'a> Iterator for Tokenizer<'a> {
    type Item = CargoResult<Token<'a>>;

    fn next(&mut self) -> Option<CargoResult<Token<'a>>> {
        loop {
            match self.s.next() {
                Some((_, ' ')) => {}
                Some((_, '(')) => return Some(Ok(Token::LeftParen)),
                Some((_, ')')) => return Some(Ok(Token::RightParen)),
                Some((_, ',')) => return Some(Ok(Token::Comma)),
                Some((_, '=')) => return Some(Ok(Token::Equals)),
                Some((start, '"')) => {
                    while let Some((end, ch)) = self.s.next() {
                        if ch == '"' {
                            return Some(Ok(Token::String(&self.orig[start+1..end])))
                        }
                    }
                    return Some(Err(human("unterminated string in cfg".to_string())))
                }
                Some((start, ch)) if is_ident_start(ch) => {
                    while let Some(&(end, ch)) = self.s.peek() {
                        if !is_ident_rest(ch) {
                            return Some(Ok(Token::Ident(&self.orig[start..end])))
                        } else {
                            self.s.next();
                        }
                    }
                    return Some(Ok(Token::Ident(&self.orig[start..])))
                }
                Some((_, ch)) => {
                    return Some(Err(human(format!("unexpected character in \
                                                   cfg `{}`, expected parens, \
                                                   a comma, an identifier, or \
                                                   a string", ch))))
                }
                None => return None
            }
        }
    }
}

fn is_ident_start(ch: char) -> bool {
    ch == '_' || ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')
}

fn is_ident_rest(ch: char) -> bool {
    is_ident_start(ch) || ('0' <= ch && ch <= '9')
}

impl<'a> Token<'a> {
    fn classify(&self) -> &str {
        match *self {
            Token::LeftParen => "`(`",
            Token::RightParen => "`)`",
            Token::Ident(..) => "an identifier",
            Token::Comma => "`,`",
            Token::Equals => "`=`",
            Token::String(..) => "a string",
        }
    }
}