curlyconf 0.1.0

Configuration file parser
Documentation
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fs;
use std::fmt;
use std::io::{self, ErrorKind};
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;

use once_cell::sync::Lazy;
use regex::Regex;

use crate::error::{Error, Result};
use crate::tokenizer::*;

pub struct Parser {
    tokenizer: Tokenizer,
    mode: Mode,
    pub watcher:    Option<Watcher>,
}

impl Parser {
    pub fn from_file(name: impl Into<String>, mode: Mode, mut watcher: Option<Watcher>) -> io::Result<Parser> {
        let name = name.into();
        if let Some(watcher) = watcher.as_mut() {
            watcher.add_file(&name)?;
        }
        let tokenizer = Tokenizer::from_file(name, mode)?;
        Ok(Parser { tokenizer, mode, watcher })
    }

    pub fn from_string(name: impl Into<String>, mode: Mode, watcher: Option<Watcher>) -> Parser {
        let tokenizer = Tokenizer::from_string(name, "config-text", mode);
        Parser { tokenizer, mode, watcher }
    }

    pub fn include(&mut self, name: impl Into<String>, curfile: &str) -> io::Result<()> {
        // Run a glob on the filename.
        let paths = ExpandedPath::expand_relative(name, curfile)?;

        // Now parse and insert these files one by one.
        for file in &paths {
            let tokenizer = Tokenizer::from_file(file.to_string(), self.mode)?;
            self.tokenizer.insert(tokenizer);
        }
        if let Some(watcher) = self.watcher.as_mut() {
            watcher.add_included_paths(paths);
        }

        Ok(())
    }

    pub fn last_span(&mut self) -> TokenSpan {
        self.tokenizer.last_span()
    }

    // Read the next token, and translate unexpected tokens to errors.
    fn do_token(&mut self, eof_ok: bool) -> Result<Token> {

        let token = self.tokenizer.next_token();
        match token.ttype {
            TokenType::Eof => {
                if eof_ok {
                    return Ok(token.clone());
                }
                return Err(Error::new("unexpected end-of-file", token.span()));
            }
            TokenType::UnterminatedString => {
                return Err(Error::new("unterminated string literal", token.span()));
            }
            TokenType::Unknown => {
                return Err(Error::new("unexpected token", token.span()));
            }
            _ => {}
        }

        Ok(token)
    }

    // Get the next token.
    pub fn next_token(&mut self) -> Result<Token> {
        self.do_token(false)
    }

    // Check if we are at EOF (that is, self.next_token() will hit EOF).
    pub fn is_eof(&mut self) -> bool {
        let pos = self.save_pos();
        let mut is_eof = false;
        loop {
            match self.do_token(true) {
                Ok(token) => match token.ttype {
                    TokenType::Nl => {}
                    TokenType::Eof => {
                        is_eof = true;
                        break;
                    }
                    _ => break,
                },
                Err(_) => break,
            }
        }
        self.restore_pos(pos);
        is_eof
    }

    // Expect a certain token-type.
    pub fn expect(&mut self, want: TokenType) -> Result<Token> {
        debug!("expect {:?}", want);
        // skip newlines, _unless_ that is what we are looking for.
        let mut token = loop {
            let t = self.next_token()?;
            if t.ttype == want || t.ttype != TokenType::Nl {
                break t;
            }
        };

        if token.ttype == TokenType::Word {
            if want == TokenType::Ident && is_ident(&mut token) {
                debug!("+-- found {:?}", token);
                return Ok(token);
            }
            if want == TokenType::Expr && is_expr(&mut token) {
                debug!("+-- found {:?}", token);
                return Ok(token);
            }
        }

        if want == token.ttype {
            debug!("+-- found {:?}", token);
            return Ok(token);
        }

        debug!("+-- failed, got {:?}", token);
        Err(Error::new(format!("expected {}", want.as_str()), token.span()))
    }

    // Peek at the next token. Returns Ok(None) on EOF instead of an error.
    pub fn peek(&mut self) -> Result<Option<Token>> {
        let pos = self.save_pos();
        let token = loop {
            let token = self.do_token(true)?;
            if token.ttype != TokenType::Nl {
                break token;
            }
        };
        self.restore_pos(pos);
        if token.ttype == TokenType::Eof {
            Ok(None)
        } else {
            Ok(Some(token))
        }
    }

    // Peek at the next token. Returns Ok(None) on EOF instead of an error.
    #[allow(dead_code)]
    pub fn peeknl(&mut self) -> Result<Option<Token>> {
        let pos = self.save_pos();
        let token = self.do_token(true)?;
        self.restore_pos(pos);
        if token.ttype == TokenType::Eof {
            Ok(None)
        } else {
            Ok(Some(token))
        }
    }

    pub fn lookahead(&mut self, how_far: usize) -> Lookahead {
        Lookahead::new(self, how_far)
    }

    pub fn lookaheadnl(&mut self, how_far: usize) -> Lookahead {
        if self.tokenizer.mode == Mode::Semicolon {
            Lookahead::new(self, how_far)
        } else {
            Lookahead::newnl(self, how_far)
        }
    }

    pub fn save_pos(&mut self) -> Cursor {
        self.tokenizer.save_cursor()
    }

    pub fn restore_pos(&mut self, pos: Cursor) {
        self.tokenizer.restore_cursor(pos);
    }
}

pub struct Lookahead {
    token: Vec<Result<Token>>,
    span: Option<TokenSpan>,
    next_pos: Vec<Cursor>,
    expected: Vec<Cow<'static, str>>,
    found: bool,
}

impl Lookahead {
    fn do_new(parser: &mut Parser, depth: usize, skipnl: bool) -> Lookahead {
        let pos = parser.save_pos();
        let mut token = Vec::new();
        let mut span = None;
        let mut next_pos = Vec::new();
        for _ in 0..depth {
            let t = loop {
                match parser.do_token(true) {
                    Ok(token) if token.ttype != TokenType::Nl || !skipnl => {
                        if span.is_none() {
                            span = Some(token.span());
                        }
                        break Ok(token);
                    },
                    Err(e) => break Err(e),
                    _ => {}
                }
            };
            token.push(t);
            next_pos.push(parser.save_pos());
        }
        parser.restore_pos(pos);

        Lookahead {
            token,
            span,
            next_pos,
            expected: Vec::new(),
            found: false,
        }
    }

    pub(crate) fn new(parser: &mut Parser, how_far: usize) -> Lookahead {
        Lookahead::do_new(parser, how_far, true)
    }

    pub(crate) fn newnl(parser: &mut Parser, how_far: usize) -> Lookahead {
        Lookahead::do_new(parser, how_far, false)
    }

    pub fn peek(&mut self, want: TokenType) -> Result<Option<Token>> {
        self.peekx(&[want])
    }

    pub fn peek2(&mut self, want1: TokenType, want2: TokenType) -> Result<Option<Token>> {
        self.peekx(&[want1, want2])
    }

    #[allow(dead_code)]
    pub fn peek3(
        &mut self,
        want1: TokenType,
        want2: TokenType,
        want3: TokenType,
    ) -> Result<Option<Token>> {
        self.peekx(&[want1, want2, want3])
    }

    fn peekx(&mut self, want: &[TokenType]) -> Result<Option<Token>> {
        if self.found {
            return Ok(None);
        }
        let mut found = true;
        let mut tokens = std::collections::VecDeque::new();

        for i in 0..want.len() {
            match self.token[i] {
                Ok(ref t) => {
                    let t = t.clone();
                    if let Some(t) = self.compare(t, &want[i]) {
                        tokens.push_back(t);
                    } else {
                        found = false;
                        break;
                    }
                }
                Err(ref e) => {
                    if i == 0 {
                        return Err(e.clone());
                    }
                    found = false;
                    break;
                }
            }
        }

        if found {
            self.found = true;
            self.next_pos[0] = self.next_pos[tokens.len() - 1];
            loop {
                let token = tokens.pop_front().unwrap();
                let t = token.ttype;
                if tokens.len() == 0
                    || t == TokenType::Word
                    || t == TokenType::Ident
                    || t == TokenType::Expr
                {
                    return Ok(Some(token));
                }
            }
        }

        let want_str = want
            .iter()
            .map(|t| t.as_str())
            .collect::<Vec<_>>()
            .join(" ");
        self.expected.push(Cow::Owned(want_str));
        Ok(None)
    }

    fn compare(&mut self, mut token: Token, want: &TokenType) -> Option<Token> {
        if token.ttype == TokenType::Word {
            if want == &TokenType::Ident && is_ident(&mut token) {
                debug!("+-- found {:?}", token);
                return Some(token);
            }
            if want == &TokenType::Expr && is_expr(&mut token) {
                debug!("+-- found {:?}", token);
                return Some(token);
            }
        }

        if want == &token.ttype {
            debug!("+-- found {:?}", token);
            return Some(token);
        }

        debug!("+-- failed, got {:?}", token);

        None
    }

    pub fn advance(&mut self, parser: &mut Parser) {
        debug!("lookahead::advance: to {:?}", self.next_pos);
        parser.restore_pos(self.next_pos[0]);
    }

    pub fn end(&mut self) -> Result<()> {
        if self.found {
            return Ok(());
        }
        let last = if self.expected.len() > 1 {
            self.expected.pop()
        } else {
            None
        };
        let mut msg = "expected ".to_string() + &(self.expected.join(", "));
        if let Some(last) = last {
            msg += &format!(" or {}", last);
        }
        Err(Error::new(msg, self.span.take().unwrap()))
    }

    pub fn error<T>(&mut self) -> Result<T> {
        Err(self.end().unwrap_err())
    }
}

pub(crate) fn is_ident(token: &mut Token) -> bool {
    static RE_IDENT: Lazy<Regex> = Lazy::new(|| {
        let re = r"^[_A-Za-z][-_0-9A-Za-z]*";
        Regex::new(re).expect("could not compile RE_IDENT regexp")
    });
    if token.ttype != TokenType::Word && token.ttype != TokenType::Ident {
        return false;
    }
    if RE_IDENT.is_match(token.value()) {
        token.ttype = TokenType::Ident;
        true
    } else {
        false
    }
}

fn is_expr(token: &mut Token) -> bool {
    if token.ttype == TokenType::Word {
        token.ttype = TokenType::Expr;
    }
    token.ttype == TokenType::Expr
}

struct ExpandedPath {
    name:   String,
    paths:  BTreeMap<String, SystemTime>,
}

impl ExpandedPath {
    fn expand(name: impl Into<String>) -> io::Result<ExpandedPath> {
        let name = name.into();

        // Try to expand.
        let globresult = glob::glob(&name).map_err(|e| {
            io::Error::new(ErrorKind::InvalidData, format!("{}: {}", name, e.msg))
        })?;
        let files = globresult.collect::<Result<Vec<_>, _>>().map_err(|e| {
            io::Error::new(e.error().kind(), format!("{}: {}", e.path().to_string_lossy(), e.error()))
        })?;
        if files.is_empty() {
            return Err(io::Error::new(ErrorKind::NotFound, format!("{}: file not found", name)));
        }

        // Now parse and insert these files one by one.
        let mut paths = BTreeMap::new();
        for file in files.into_iter().map(|p| p.to_string_lossy().to_string()) {
            let modified = fs::metadata(&file).and_then(|m| m.modified()).map_err(|e| {
                io::Error::new(e.kind(), format!("{}: {}", file, e))
            })?;
            paths.insert(file, modified);
        }

        Ok(ExpandedPath { name, paths })
    }

    fn expand_relative(name: impl Into<String>, relative_to: impl AsRef<str>) -> io::Result<ExpandedPath> {
        let mut name = name.into();
        let relative_to = relative_to.as_ref();
        let path = Path::new(&name);
        if path.is_relative() {
            // Take the parent of the current file, and build a new path.
            if let Some(parent) = Path::new(relative_to).parent() {
                let mut pathbuf = parent.to_path_buf();
                pathbuf.push(&path);
                name = pathbuf.to_string_lossy().to_string();
            }
        }
        ExpandedPath::expand(name)
    }

    fn changed(&self) -> bool {
        // expand the same name again.
        let mut new = match ExpandedPath::expand(&self.name) {
            Ok(p) => p,
            Err(_) => return true,
        };

        // compare old and new.
        for (path, time) in &self.paths {
            match new.paths.get(path) {
                Some(ntime) => {
                    if time != ntime {
                        return true;
                    }
                    new.paths.remove(path);
                },
                None => return true,
            }
        }
        new.paths.len() > 0
    }
}

impl<'a> IntoIterator for &'a ExpandedPath {
    type Item = &'a str;
    type IntoIter = Box<dyn Iterator<Item=&'a str> + 'a>;

    fn into_iter(self) -> Self::IntoIter {
        Box::new(self.paths.keys().map(|p| p.as_str()))
    }
}

/// Check if the configuration has changed on disk.
///
/// A `Watcher` contains the list of files that were parsed to build
/// the configuration. It can be used to watch those files for changes.
/// A watcher is initialized by using a [`crate::Builder`] and
/// passing it to its [watcher][`crate::Builder::watcher`] method.
#[derive(Clone)]
pub struct Watcher {
    inner:  Arc<Mutex<WatcherInner>>,
}

impl fmt::Debug for Watcher {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[private]")
    }
}

impl Default for Watcher {
    fn default() -> Watcher {
        let inner = WatcherInner {
            epaths: Vec::new(),
        };
        Watcher{ inner: Arc::new(Mutex::new(inner)) }
    }
}

struct WatcherInner {
    epaths:      Vec<ExpandedPath>,
}

impl Watcher {
    /// Create a new watcher.
    pub fn new() -> Watcher {
        Watcher::default()
    }

    /// Add an extra file to be watched for changes.
    pub fn add_file(&self, file: &str) -> io::Result<()> {
        let mut inner = self.inner.lock().unwrap();
        inner.epaths.push(ExpandedPath::expand(file)?);
        Ok(())
    }

    fn add_included_paths(&self, paths: ExpandedPath) {
        let mut inner = self.inner.lock().unwrap();
        inner.epaths.push(paths);
    }

    /// Check if any configuration files have changed.
    pub fn changed(&self) -> bool {
        let inner = self.inner.lock().unwrap();
        inner.epaths.iter().any(|p| p.changed())
    }
}