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
use nom::{
    branch::alt,
    bytes::complete::{is_a, is_not, tag},
    character::complete::{alpha1, alphanumeric1, char, line_ending, space0, space1},
    combinator::{all_consuming, consumed, eof, map, opt, recognize, value},
    error::{context, VerboseError, VerboseErrorKind},
    multi::{count, many0, many1},
    sequence::{delimited, pair, preceded, terminated, tuple},
    IResult, Parser,
};
use tracing::{span, Level};

use super::{Binding, SchemaNode};
use crate::{Expression, Identifier, Special, Token};

type Res<T, U> = IResult<T, U, VerboseError<T>>;

mod builder;
use builder::SchemaNodeBuilder;

mod error;
pub use error::ParseError;

#[derive(Debug)]
pub enum NodeType {
    Directory,
    File,
}

/// Parses the given text representation into a tree of [`SchemaNode`]s
pub fn parse_schema(text: &str) -> std::result::Result<SchemaNode, ParseError> {
    let span = span!(Level::INFO, "parse_schema");
    let _enter = span.enter();

    // Strip several levels of initial indentation to help with indented literal schemas
    let any_indent = |s| {
        opt(alt((
            many1(operator(0)),
            many1(operator(1)),
            many1(operator(2)),
            many1(operator(3)),
            many1(operator(4)),
        )))(s)
    };
    // Parse and process entire schema and handle any errors that arise
    let (_, ops) = all_consuming(preceded(many0(blank_line), any_indent))(text).map_err(|e| {
        let e = match e {
            nom::Err::Error(e) | nom::Err::Failure(e) => e,
            nom::Err::Incomplete(_) => unreachable!(),
        };
        let mut error = None;
        for (r, e) in e.errors.iter().rev() {
            error = Some(ParseError::new(
                match e {
                    VerboseErrorKind::Nom(p) => {
                        format!("Invalid token while looking for: {p:?}")
                    }
                    _ => format!("Error parsing {e:?}"),
                },
                text,
                r,
                error.map(Box::new),
            ));
        }
        error.unwrap()
    })?;
    let ops = ops.unwrap_or_default();
    let schema_node = schema_node("root", text, text, false, NodeType::Directory, None, ops)?;
    if schema_node.match_pattern.is_some() {
        return Err(ParseError::new(
            "Top level :match is not allowed".into(),
            // TODO: Or is it? (Could alternatively say is_def=true)
            text,
            text.find("\n:match")
                .map(|pos| &text[pos + 1..pos + 7])
                .unwrap_or(text),
            None,
        ));
    }
    Ok(schema_node)
}

fn schema_node<'t>(
    line: &'t str,
    whole: &'t str,
    part: &'t str,
    is_def: bool,
    item_type: NodeType,
    symlink: Option<Expression<'t>>,
    ops: Vec<(&'t str, Operator<'t>)>,
) -> std::result::Result<SchemaNode<'t>, ParseError<'t>> {
    let part_parse_error = |e: anyhow::Error| ParseError::new(e.to_string(), whole, part, None);
    let mut builder = SchemaNodeBuilder::new(
        line,
        is_def,
        match item_type {
            NodeType::Directory => NodeType::Directory,
            NodeType::File => NodeType::File,
        },
        symlink,
    );
    for (span, op) in ops {
        match op {
            // Operators that affect the parent (when looking up this item)
            Operator::Match(expr) => builder.match_pattern(expr),
            Operator::Avoid(expr) => builder.avoid_pattern(expr),

            // Operators that apply to this item
            Operator::Use { name } => builder.use_definition(name),
            Operator::Mode(mode) => builder.mode(mode),
            Operator::Owner(owner) => builder.owner(owner),
            Operator::Group(group) => builder.group(group),
            Operator::Source(source) => builder.source(source),
            Operator::Target(target) => builder.target(target),

            // Operators that apply to child items
            Operator::Let { name, expr } => builder.let_var(name, expr),
            Operator::Item {
                line,
                binding,
                is_directory,
                link,
                children,
            } => {
                let sub_item_type = match is_directory {
                    false => NodeType::File,
                    true => NodeType::Directory,
                };
                let item_node =
                    schema_node(line, whole, span, false, sub_item_type, link, children).map_err(
                        |e| {
                            ParseError::new(
                                format!(r#"Problem within "{binding}""#),
                                whole,
                                span,
                                Some(Box::new(e)),
                            )
                        },
                    )?;
                builder.add_entry(binding, item_node)
            }
            Operator::Def {
                line,
                name,
                is_directory,
                link,
                children,
            } => {
                if let NodeType::File = item_type {
                    return Err(ParseError::new(
                        "Files cannot have child items".to_string(),
                        whole,
                        span,
                        None,
                    ));
                }
                let sub_item_type = match is_directory {
                    false => NodeType::File,
                    true => NodeType::Directory,
                };
                let properties =
                    schema_node(line, whole, span, true, sub_item_type, link, children).map_err(
                        |e| {
                            ParseError::new(
                                format!(r#"Error within definition "{name}""#),
                                whole,
                                span,
                                Some(Box::new(e)),
                            )
                        },
                    )?;

                if properties.match_pattern.is_some() {
                    return Err(ParseError::new(
                        ":def has own :match".to_owned(),
                        whole,
                        span,
                        None,
                    ));
                }
                builder.define(name, properties)
            }
        }
        .map_err(|s| ParseError::new(s.to_string(), whole, span, None))?
    }
    // TODO: Handle error spans, child errors?, etc.
    builder.build().map_err(part_parse_error)
}

fn indentation(level: usize) -> impl Fn(&str) -> Res<&str, &str> {
    move |s: &str| recognize(count(tag("    "), level))(s)
}

fn operator(level: usize) -> impl Fn(&str) -> Res<&str, (&str, Operator)> {
    // This is really just to make the op definitions tidier
    fn op<'a, O, P>(op: &'static str, second: P) -> impl FnMut(&'a str) -> Res<&'a str, O>
    where
        P: Parser<&'a str, O, VerboseError<&'a str>>,
    {
        context("op", preceded(tuple((tag(op), space1)), second))
    }

    move |s: &str| {
        let sep = |ch, second| preceded(delimited(space0, char(ch), space0), second);

        let let_op = tuple((op("let", identifier), sep('=', expression)));
        let use_op = op("use", identifier);
        let match_op = op("match", expression);
        let avoid_op = op("avoid", expression);
        let mode_op = op("mode", octal);
        let owner_op = op("owner", expression);
        let group_op = op("group", expression);
        let source_op = op("source", expression);
        let target_op = op("target", expression);

        consumed(alt((
            delimited(
                tuple((indentation(level), char(':'))),
                alt((
                    map(let_op, |(name, expr)| Operator::Let { name, expr }),
                    map(use_op, |name| Operator::Use { name }),
                    map(match_op, Operator::Match),
                    map(avoid_op, Operator::Avoid),
                    map(mode_op, Operator::Mode),
                    map(owner_op, Operator::Owner),
                    map(group_op, Operator::Group),
                    map(source_op, Operator::Source),
                    map(target_op, Operator::Target),
                )),
                end_of_lines,
            ),
            map(
                // $binding/ -> link
                //     children...
                tuple((
                    delimited(indentation(level), consumed(item_header), end_of_lines),
                    many0(operator(level + 1)),
                )),
                |((line, (binding, is_directory, link)), children)| Operator::Item {
                    line,
                    binding,
                    is_directory,
                    link,
                    children,
                },
            ),
            map(
                tuple((
                    delimited(indentation(level), consumed(def_header), end_of_lines),
                    many0(operator(level + 1)),
                )),
                |((line, (name, is_directory, link)), children)| Operator::Def {
                    line,
                    name,
                    is_directory,
                    link,
                    children,
                },
            ),
        )))(s)
    }
}

#[derive(Debug, Clone, PartialEq)]
enum Operator<'t> {
    Item {
        line: &'t str,
        binding: Binding<'t>,
        is_directory: bool,
        link: Option<Expression<'t>>,
        children: Vec<(&'t str, Operator<'t>)>,
    },
    Let {
        name: Identifier<'t>,
        expr: Expression<'t>,
    },
    Def {
        line: &'t str,
        name: Identifier<'t>,
        is_directory: bool,
        link: Option<Expression<'t>>,
        children: Vec<(&'t str, Operator<'t>)>,
    },
    Use {
        name: Identifier<'t>,
    },
    Match(Expression<'t>),
    Avoid(Expression<'t>),
    Mode(u16),
    Owner(Expression<'t>),
    Group(Expression<'t>),
    Source(Expression<'t>),
    Target(Expression<'t>),
}

fn blank_line(s: &str) -> Res<&str, &str> {
    alt((
        recognize(tuple((space0, line_ending))),
        recognize(tuple((space1, eof))),
        recognize(tuple((space0, comment, line_ending))),
        recognize(tuple((space0, comment, eof))),
    ))(s)
}

fn comment(s: &str) -> Res<&str, &str> {
    alt((
        recognize(tuple((tag("# "), is_not("\r\n")))),
        terminated(tag("#"), eof),
    ))(s)
}

/// Match and consume line endings and any following blank lines, or EOF
fn end_of_lines(s: &str) -> Res<&str, &str> {
    alt((recognize(tuple((line_ending, many0(blank_line)))), eof))(s)
}

fn binding(s: &str) -> Res<&str, Binding<'_>> {
    alt((
        map(preceded(char('$'), identifier), Binding::Dynamic),
        map(filename, Binding::Static),
    ))(s)
}

fn filename(s: &str) -> Res<&str, &str> {
    recognize(many1(alt((alphanumeric1, is_a("_-.@^+%=")))))(s)
}

// $name/ -> link
// name
fn item_header(s: &str) -> Res<&str, (Binding, bool, Option<Expression>)> {
    tuple((
        binding,
        map(opt(char('/')), |o| o.is_some()),
        opt(preceded(tuple((space1, tag("->"), space1)), expression)),
    ))(s)
}

// :def name/
// :def name -> link
fn def_header(s: &str) -> Res<&str, (Identifier, bool, Option<Expression>)> {
    preceded(
        tuple((tag(":def"), space1)),
        tuple((
            identifier,
            map(opt(char('/')), |o| o.is_some()),
            opt(preceded(tuple((space0, tag("->"), space0)), expression)),
        )),
    )(s)
}

fn octal(s: &str) -> Res<&str, u16> {
    map(is_a("01234567"), |mode| {
        u16::from_str_radix(mode, 8).unwrap()
    })(s)
}

fn identifier(s: &str) -> Res<&str, Identifier> {
    map(
        recognize(pair(
            alt((alpha1, tag("_"))),
            many0(alt((alphanumeric1, tag("_")))),
        )),
        Identifier::new,
    )(s)
}

/// Expression, such as "static/$varA/${varB}v2/${NAME}"
fn expression(s: &str) -> Res<&str, Expression> {
    map(many1(alt((non_variable, variable))), |tokens| {
        Expression::from(tokens)
    })(s)
}

/// A sequence of characters that are not part of any variable
fn non_variable(s: &str) -> Res<&str, Token> {
    map(is_not("$\n"), Token::Text)(s)
}

/// A variable name, optionally braced, prefixed by a dollar sign, such as `${example}`
fn variable(s: &str) -> Res<&str, Token> {
    let braced = |parser| alt((delimited(char('{'), parser, char('}')), parser));
    let vars = |s| {
        alt((
            value(
                Token::Special(Special::PathRelative),
                tag(Special::SAME_PATH_RELATIVE),
            ),
            value(
                Token::Special(Special::PathAbsolute),
                tag(Special::SAME_PATH_ABSOLUTE),
            ),
            value(
                Token::Special(Special::PathNameOnly),
                tag(Special::SAME_PATH_NAME),
            ),
            value(
                Token::Special(Special::ParentRelative),
                tag(Special::PARENT_PATH_RELATIVE),
            ),
            value(
                Token::Special(Special::ParentAbsolute),
                tag(Special::PARENT_PATH_ABSOLUTE),
            ),
            value(
                Token::Special(Special::ParentNameOnly),
                tag(Special::PARENT_PATH_NAME),
            ),
            value(Token::Special(Special::RootPath), tag(Special::ROOT_PATH)),
            map(identifier, Token::Variable),
        ))(s)
    };
    preceded(char('$'), braced(vars))(s)
}

#[cfg(test)]
mod tests;