summaryrefslogtreecommitdiff
path: root/src/queries.rs
blob: ae00060f570a320515233ed42a71ff7e84835300 (plain)
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
// this is mostly based on the s-exp tutorial
// https://github.com/rust-analyzer/rowan/blob/master/examples/s_expressions.rs

use itertools::Itertools;
use rowan::{GreenNode, GreenNodeBuilder, TextRange};

type ParseError = (TextRange, String);
type ParseResult<T> = Result<T, Vec<ParseError>>;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[allow(non_camel_case_types)]
#[repr(u16)]
pub enum SyntaxKind {
    L_BRACKET = 0, // '['
    R_BRACKET,     // ']'
    WORD,          // 'Attrset', 'meta', '.', '>', ...
    WHITESPACE,    // whitespaces is explicit
    ERROR,         // as well as errors

    // composite nodes
    LIST, // `[..]`
    ATOM, // wraps WORD
    ROOT, // top-level (a complete query)
}
use SyntaxKind::*;

impl From<SyntaxKind> for rowan::SyntaxKind {
    fn from(kind: SyntaxKind) -> Self {
        Self(kind as u16)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum QueryLang {}
impl rowan::Language for QueryLang {
    type Kind = SyntaxKind;
    fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
        assert!(raw.0 <= ROOT as u16);
        unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
    }
    fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
        kind.into()
    }
}

fn lex(text: &str) -> Vec<(SyntaxKind, String)> {
    fn tok(t: SyntaxKind) -> m_lexer::TokenKind {
        m_lexer::TokenKind(rowan::SyntaxKind::from(t).0)
    }
    fn kind(t: m_lexer::TokenKind) -> SyntaxKind {
        match t.0 {
            0 => L_BRACKET,
            1 => R_BRACKET,
            2 => WORD,
            3 => WHITESPACE,
            4 => ERROR,
            _ => unreachable!(),
        }
    }

    let lexer = m_lexer::LexerBuilder::new()
        .error_token(tok(ERROR))
        .tokens(&[
            (tok(L_BRACKET), r"\["),
            (tok(R_BRACKET), r"\]"),
            (tok(WORD), r"[^\s\[\]]+"),
            (tok(WHITESPACE), r"\s+"),
        ])
        .build();

    lexer
        .tokenize(text)
        .into_iter()
        .map(|t| (t.len, kind(t.kind)))
        .scan(0usize, |start_offset, (len, kind)| {
            let s: String = text[*start_offset..*start_offset + len].into();
            *start_offset += len;
            Some((kind, s))
        })
        .collect()
}


#[derive(Clone)]
pub struct Parse {
    pub green_node: GreenNode,
    pub errors: Vec<String>,
}

pub fn parse(text: &str) -> Parse {
    struct Parser {
        tokens: Vec<(SyntaxKind, String)>,
        builder: GreenNodeBuilder<'static>,
        errors: Vec<String>,
    }

    #[derive(Debug)]
    enum QexpRes {
        Ok,
        Eof,
        RBracket,
        LBracket
    }

    impl Parser {
        fn parse(mut self) -> Parse {
            // Make sure that the root node covers all source
            self.builder.start_node(ROOT.into());
            // Parse zero or more S-expressions
            loop {
                match self.word() {
                    QexpRes::Eof => break,
                    QexpRes::Ok => (),
                    unmatched_bracket => {
                        self.builder.start_node(ERROR.into());
                        self.bump(); // be sure to chug along in case of error
                        self.builder.finish_node();
                        self.errors.push(format!("lone `{:?}`", unmatched_bracket));
                    }
                }
            }
            // eat remaining whitespace
            self.skip_ws();
            self.builder.finish_node();

            Parse { green_node: self.builder.finish(), errors: self.errors }
        }
        fn list(&mut self) {
            assert_eq!(self.current(), Some(L_BRACKET));
            // Start the list node
            self.builder.start_node(LIST.into());
            self.bump(); // '['
            loop {
                match self.word() {
                    QexpRes::Eof => {
                        self.builder.start_node(ERROR.into());
                        self.errors.push("expected `]`".to_string());
                        self.builder.finish_node();
                        break;
                    }
                    QexpRes::RBracket => {
                        self.bump();
                        break;
                    }
                    QexpRes::LBracket => {
                        self.builder.start_node(ERROR.into());
                        self.errors.push("unexpected list".to_string());
                        self.bump();
                        self.builder.finish_node();
                    }
                    QexpRes::Ok => (),
                }
            }
            // close the list node
            self.builder.finish_node();
        }
        fn word(&mut self) -> QexpRes {
            // Eat leading whitespace
            self.skip_ws();
            // Either a list, an atom, a closing paren,
            // or an eof.
            let t = match self.current() {
                None => return QexpRes::Eof,
                Some(R_BRACKET) => return QexpRes::RBracket,
                Some(L_BRACKET) => return QexpRes::LBracket,
                Some(t) => t,
            };
            match t {
                WORD => {
                    self.builder.start_node(ATOM.into());
                    self.bump();
                    self.skip_ws();
                    if Some(L_BRACKET) == self.current() {
                        self.list();
                    }
                    self.builder.finish_node();
                }
                ERROR => self.bump(),
                _ => unreachable!(),
            }
            QexpRes::Ok
        }
        /// Advance one token, adding it to the current branch of the tree builder.
        fn bump(&mut self) {
            let (kind, text) = self.tokens.pop().unwrap();
            self.builder.token(kind.into(), text.as_str());
        }
        /// Peek at the first unprocessed token
        fn current(&self) -> Option<SyntaxKind> {
            self.tokens.last().map(|(kind, _)| *kind)
        }
        fn skip_ws(&mut self) {
            while self.current() == Some(WHITESPACE) {
                self.bump()
            }
        }
    }

    let mut tokens = lex(text);
    tokens.reverse();
    Parser { tokens, builder: GreenNodeBuilder::new(), errors: Vec::new() }.parse()
}

/// To work with the parse results we need a view into the
/// green tree - the Syntax tree.
/// It is also immutable, like a GreenNode,
/// but it contains parent pointers, offsets, and
/// has identity semantics.

pub type SyntaxNode = rowan::SyntaxNode<QueryLang>;
#[allow(unused)]
type SyntaxToken = rowan::SyntaxToken<QueryLang>;
#[allow(unused)]
type SyntaxElement = rowan::NodeOrToken<SyntaxNode, SyntaxToken>;

impl Parse {
    pub fn syntax(&self) -> SyntaxNode {
        SyntaxNode::new_root(self.green_node.clone())
    }
}

/// Let's check that the parser works as expected
#[test]
fn test_parser() {
    let text = "Inherit > mdDoc[something]";
    let node = parse(text).syntax();
    assert_eq!(
        format!("{:?}", node),
        "ROOT@0..26"
    );
    assert_eq!(node.children().count(), 3);
    let children = node
        .descendants_with_tokens()
        .map(|child| format!("{:?}@{:?}", child.kind(), child.text_range()))
        .collect::<Vec<_>>();

    assert_eq!(
        children,
        vec![
            "ROOT@0..26".to_string(),
             "ATOM@0..8".to_string(),
              "WORD@0..7".to_string(),
              "WHITESPACE@7..8".to_string(), // note, explicit whitespace!
             "ATOM@8..10".to_string(),
              "WORD@8..9".to_string(),
              "WHITESPACE@9..10".to_string(),
             "ATOM@10..26".to_string(),
              "WORD@10..15".to_string(),
              "LIST@15..26".to_string(),
               "L_BRACKET@15..16".to_string(),
                "ATOM@16..25".to_string(),
                 "WORD@16..25".to_string(),
               "R_BRACKET@25..26".to_string()
        ]
    );
}


#[derive(Debug, Clone)]
pub struct Query {
    pub filters: Vec<Filter>
}

#[derive(Debug, Clone)]
pub struct Filter {
    pub selector: Selector,
    pub args: Option<Operator>
}

#[derive(Debug, Clone)]
pub enum Selector {
    Down,
    DownRecursive,
    Up,
    UpRecursive,
    NixSyntaxNode(rnix::SyntaxKind),
    NixSyntaxRole(NixSyntaxRole),
    Named(String)
}

#[derive(Debug, Clone)]
pub enum NixSyntaxRole {
    Argument,
    Function,
    Attribute,
    // TODO
}

#[derive(Debug, Clone)]
pub enum Operator {
    Remove,
    Keep
}

impl NixSyntaxRole {
    fn from_str(from: &str) -> Option<NixSyntaxRole> {
        use NixSyntaxRole::*;
        Some(match from {
            "Argument" => Argument,
            "Function" => Function,
            "Attribute" => Attribute,
            _ => return None
        })
    }
}

impl Parse {

    pub fn to_query(&self) -> ParseResult<Query> {

        fn parse_operator(node: SyntaxNode) -> ParseResult<Operator> {
            match node.to_string().as_str() {
                "remove" => Ok(Operator::Remove),
                "keep" => Ok(Operator::Keep),
                unknown => Err(vec![(node.text_range(), format!("unknown operator {unknown}"))])
            }
        }
        fn parse_args(node: SyntaxNode) -> ParseResult<Option<Operator>> {
            let list_node = node
                .children()
                .find(|n| n.kind() == LIST);

            if let Some(node) = list_node {
                let args = node
                    .children()
                    .map(|child| {
                        match child.kind() {
                            ATOM => parse_operator(child),
                            _ => unreachable!()
                        }
                    })
                    .collect::<ParseResult<Vec<Operator>>>();

                match args {
                    Err(e) => Err(e),
                    Ok(ops) if ops.len() == 1 => Ok(Some(ops.into_iter().exactly_one().unwrap())),
                    _ => Err(vec![(node.text_range(), "cannot have multiple operators at the same node (for now)".to_string())])
                }
            } else {
                Ok(None)
            }
        }

        fn parse_filter(node: SyntaxNode) -> ParseResult<Filter> {
            let text = match node.green().children().next() {
                Some(rowan::NodeOrToken::Token(token)) => token.text().to_string(),
                _ => unreachable!(),
            };

            use Selector::*;
            let selector = match text.as_str() {
                ">" => Down,
                ">>" => DownRecursive,
                "<" => Up,
                "<<" => UpRecursive,
                "Inherit" => NixSyntaxNode(rnix::SyntaxKind::NODE_INHERIT),
                "String" => NixSyntaxNode(rnix::SyntaxKind::NODE_STRING),
                // TODO other syntax nodes
                name => if let Some(role) = self::NixSyntaxRole::from_str(name) {
                    NixSyntaxRole(role)
                } else {
                    Named(name.to_owned())
                },
            };

            let args = parse_args(node)?;

            let filter = Filter {
                selector,
                args
            };
            Ok(filter)
        }

        let root = self.syntax();
        assert_eq!(root.kind(), ROOT);

        let (errors, filters): (Vec<_>, Vec<_>) = root.children()
            .map(parse_filter)
            .partition_map(From::from);

        if errors.len() == 0 {
            Ok(Query { filters })
        } else {
            Err(errors.concat())
        }
    }
}

pub fn print_query_errors(content: &str, mut errors: Vec<ParseError>) {
    errors.sort_by_key(|(range, _)| range.start());

    let mut errors_iter = errors.into_iter().peekable();

    eprintln!("Errors in Query:");

    let mut line_start = 0usize;
    for line in content.split('\n') {

        eprintln!("| {line}");
        let line_end: usize = line_start + line.len();

        while Some(true) == errors_iter.peek().map(|e| Into::<usize>::into(e.0.end()) < line_end) {
            match errors_iter.next() {
                None => return,
                Some((range, msg)) => {
                    eprintln!(
                        "| {pad}{line} {msg}",
                        pad = " ".repeat(Into::<usize>::into(range.start()).checked_sub(line_start).unwrap_or(0)),
                        line = "^".repeat(range.len().into())
                    )
                }
            }
        }


        line_start += line_end + 1;
    }
}