ruprizzle-parser 0.1.0-alpha.2

Schema DSL parser for the ruprizzle ORM
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
//! Pest grammar binding and the walk from parse pairs to [`crate::ast`].
//!
//! Nothing here interprets the schema. It only turns a successful parse into
//! typed nodes with spans; every judgement about meaning happens in
//! [`crate::lower`] and [`crate::validate`].

use pest::Parser as _;
use pest::iterators::Pair;
use ruprizzle_core::span::Span;

use crate::ast::{
    Arg, Arity, Ast, Attr, Block, ConfigEntry, Decl, EnumDecl, FieldDecl, ModelDecl, Value,
    VariantDecl,
};

/// The generated Pest parser for `schema.ruprizzle`.
#[derive(pest_derive::Parser)]
#[grammar = "schema.pest"]
pub struct SchemaParser;

/// Parses source text into the loose AST.
///
/// # Errors
///
/// Returns the raw Pest error; [`crate::errors`] turns it into a diagnostic.
pub fn parse_ast(source: &str) -> Result<Ast, Box<pest::error::Error<Rule>>> {
    let mut pairs = SchemaParser::parse(Rule::schema, source).map_err(Box::new)?;
    let schema = pairs.next().expect("Rule::schema always yields one pair");

    let mut ast = Ast::default();
    for pair in schema.into_inner() {
        let decl = match pair.as_rule() {
            Rule::datasource => Decl::Datasource(block(pair)),
            Rule::generator => Decl::Generator(block(pair)),
            Rule::enum_def => Decl::Enum(enum_decl(pair)),
            Rule::model_def => Decl::Model(model_decl(pair)),
            Rule::EOI => continue,
            other => unreachable!("unexpected top-level rule {other:?}"),
        };
        ast.decls.push(decl);
    }
    Ok(ast)
}

fn span_of(pair: &Pair<'_, Rule>) -> Span {
    let s = pair.as_span();
    Span::new(s.start(), s.end())
}

/// The children of a declaration, with keyword tokens removed.
///
/// Keywords are atomic rather than silent so that `model` cannot match the start
/// of `modelish` (a silent rule would have implicit whitespace inserted before
/// its boundary check). The cost is a pair per keyword, dropped here so the rest
/// of the walk sees only meaningful children.
fn members(pair: Pair<'_, Rule>) -> Walk<'_> {
    pair.into_inner()
        .filter(|p| {
            !matches!(
                p.as_rule(),
                Rule::kw_datasource
                    | Rule::kw_generator
                    | Rule::kw_enum
                    | Rule::kw_model
                    | Rule::kw_env
            )
        })
        .collect::<Vec<_>>()
        .into_iter()
        .peekable()
}

/// A rewindable walk over a declaration's children.
type Walk<'a> = std::iter::Peekable<std::vec::IntoIter<Pair<'a, Rule>>>;

/// Collects leading `///` lines into one rustdoc string.
///
/// Returns `None` rather than an empty string so the IR can distinguish
/// "documented with nothing" from "not documented", which matters when deciding
/// whether to emit a rustdoc block at all.
fn take_docs(pairs: &mut Walk<'_>) -> Option<String> {
    let mut lines = Vec::new();
    while pairs
        .peek()
        .is_some_and(|p| p.as_rule() == Rule::doc_comment)
    {
        let comment = pairs.next().expect("peeked");
        let text = comment
            .into_inner()
            .find(|p| p.as_rule() == Rule::doc_text)
            .map(|p| p.as_str().trim_end().to_owned())
            .unwrap_or_default();
        lines.push(text);
    }
    if lines.is_empty() {
        None
    } else {
        Some(lines.join("\n"))
    }
}

fn block(pair: Pair<'_, Rule>) -> Block {
    let span = span_of(&pair);
    let mut inner = members(pair);
    let name_pair = inner.next().expect("block always has a name");
    let name = name_pair.as_str().to_owned();

    let entries = inner
        .map(|kv| {
            let span = span_of(&kv);
            let mut parts = kv.into_inner();
            let key = parts
                .next()
                .expect("config_kv has a key")
                .as_str()
                .to_owned();
            let value = value(parts.next().expect("config_kv has a value"));
            ConfigEntry { key, value, span }
        })
        .collect();

    Block {
        name,
        entries,
        span,
    }
}

fn enum_decl(pair: Pair<'_, Rule>) -> EnumDecl {
    let span = span_of(&pair);
    let mut inner = members(pair);
    let docs = take_docs(&mut inner);

    let name_pair = inner.next().expect("enum always has a name");
    let name_span = span_of(&name_pair);
    let name = name_pair.as_str().to_owned();

    let variants = inner.map(variant_decl).collect();

    EnumDecl {
        name,
        name_span,
        variants,
        docs,
        span,
    }
}

fn variant_decl(pair: Pair<'_, Rule>) -> VariantDecl {
    let span = span_of(&pair);
    let mut inner = members(pair);
    let docs = take_docs(&mut inner);

    let name = inner
        .next()
        .expect("variant always has a name")
        .as_str()
        .to_owned();
    let map = inner.next().map(|p| unescape(inner_text(&p)));

    VariantDecl {
        name,
        map,
        docs,
        span,
    }
}

fn model_decl(pair: Pair<'_, Rule>) -> ModelDecl {
    let span = span_of(&pair);
    let mut inner = members(pair);
    let docs = take_docs(&mut inner);

    let name_pair = inner.next().expect("model always has a name");
    let name_span = span_of(&name_pair);
    let name = name_pair.as_str().to_owned();

    let mut fields = Vec::new();
    let mut block_attrs = Vec::new();
    for member in inner {
        match member.as_rule() {
            Rule::field => fields.push(field_decl(member)),
            Rule::block_attr => block_attrs.push(attr(member)),
            other => unreachable!("unexpected model member {other:?}"),
        }
    }

    ModelDecl {
        name,
        name_span,
        fields,
        block_attrs,
        docs,
        span,
    }
}

fn field_decl(pair: Pair<'_, Rule>) -> FieldDecl {
    let span = span_of(&pair);
    let mut inner = members(pair);
    let docs = take_docs(&mut inner);

    let name_pair = inner.next().expect("field always has a name");
    let name_span = span_of(&name_pair);
    let name = name_pair.as_str().to_owned();

    // `field_type` is atomic — it has to be, so that a missing type is reported
    // as a field type rather than as a bare identifier — so the arity marker is
    // read off the text rather than from a child pair.
    let type_pair = inner.next().expect("field always has a type");
    let type_span = span_of(&type_pair);
    let written = type_pair.as_str();
    let (type_name, arity) = if let Some(base) = written.strip_suffix("[]") {
        (base, Arity::List)
    } else if let Some(base) = written.strip_suffix('?') {
        (base, Arity::Optional)
    } else {
        (written, Arity::Required)
    };
    let type_name = type_name.to_owned();

    let attrs = inner.map(attr).collect();

    FieldDecl {
        name,
        name_span,
        type_name,
        type_span,
        arity,
        attrs,
        docs,
        span,
    }
}

fn attr(pair: Pair<'_, Rule>) -> Attr {
    let span = span_of(&pair);
    let mut inner = pair.into_inner();
    let path = inner
        .next()
        .expect("attribute always has a path")
        .as_str()
        .to_owned();

    let args = inner
        .next()
        .map(|list| list.into_inner().map(arg).collect())
        .unwrap_or_default();

    Attr { path, args, span }
}

fn arg(pair: Pair<'_, Rule>) -> Arg {
    let inner = pair.into_inner().next().expect("arg wraps one value");
    match inner.as_rule() {
        Rule::named_arg => {
            let span = span_of(&inner);
            let mut parts = inner.into_inner();
            let name = parts
                .next()
                .expect("named argument has a name")
                .as_str()
                .to_owned();
            let value = value(parts.next().expect("named argument has a value"));
            Arg::Named { name, value, span }
        }
        _ => Arg::Positional(value(inner)),
    }
}

fn inner_text(pair: &Pair<'_, Rule>) -> String {
    pair.clone()
        .into_inner()
        .next()
        .map_or_else(|| pair.as_str().to_owned(), |p| p.as_str().to_owned())
}

fn value(pair: Pair<'_, Rule>) -> Value {
    let span = span_of(&pair);
    match pair.as_rule() {
        Rule::string => Value::Str(unescape(inner_text(&pair)), span),
        Rule::number => Value::Num(pair.as_str().to_owned(), span),
        Rule::boolean => Value::Bool(pair.as_str() == "true", span),
        Rule::ident => Value::Ident(pair.as_str().to_owned(), span),
        Rule::env_call => Value::Env(
            unescape(inner_text(
                &pair
                    .into_inner()
                    .find(|p| p.as_rule() == Rule::string)
                    .expect("env() takes a string"),
            )),
            span,
        ),
        Rule::func_call => {
            let mut inner = pair.into_inner();
            let name = inner
                .next()
                .expect("call always has a name")
                .as_str()
                .to_owned();
            Value::Func {
                name,
                args: inner.map(value).collect(),
                span,
            }
        }
        Rule::array => Value::Array(pair.into_inner().map(value).collect(), span),
        other => unreachable!("unexpected value rule {other:?}"),
    }
}

/// Resolves the escape sequences the grammar allows inside a string literal.
fn unescape(raw: String) -> String {
    if !raw.contains('\\') {
        return raw;
    }
    let mut out = String::with_capacity(raw.len());
    let mut chars = raw.chars();
    while let Some(c) = chars.next() {
        if c != '\\' {
            out.push(c);
            continue;
        }
        match chars.next() {
            Some('n') => out.push('\n'),
            Some('t') => out.push('\t'),
            Some('r') => out.push('\r'),
            Some(other) => out.push(other),
            None => out.push('\\'),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_every_production() {
        let src = r#"
// a line comment that must not eat the next doc comment
datasource db {
  provider = "postgres"
  url      = env("DATABASE_URL")
  strict   = true
}

generator client {
  output      = "src/db"
  module_name = "db"
}

/// A registered account.
enum Role {
  /// Ordinary user.
  USER
  ADMIN @map("admin")
}

/// A user.
model User {
  id        Uuid     @id @default(uuid7())
  email     String   @unique @db.VarChar(200)
  name      String?
  role      Role     @default(USER)
  posts     Post[]
  createdAt DateTime @default(now()) @map("created_at")

  @@index([email])
  @@map("users")
}

model Post {
  id       Uuid @id @default(uuid7())
  authorId Uuid @map("author_id")
  author   User @relation(fields: [authorId], references: [id], onDelete: Cascade)
}
"#;
        let ast = parse_ast(src).expect("fixture parses");
        assert_eq!(ast.decls.len(), 5);

        let user = ast.models().next().expect("User is the first model");
        assert_eq!(user.name, "User");
        assert_eq!(user.fields.len(), 6);
        assert_eq!(user.block_attrs.len(), 2);
        assert_eq!(user.docs.as_deref(), Some("A user."));
        assert_eq!(user.fields[4].arity, Arity::List);
        assert_eq!(user.fields[2].arity, Arity::Optional);

        let email = &user.fields[1];
        assert!(email.has_attr("unique"));
        let varchar = email.attr("db.VarChar").expect("native type attribute");
        assert_eq!(
            varchar.first_positional().map(Value::describe),
            Some("200".to_owned())
        );

        let role = ast.enums().next().expect("one enum");
        assert_eq!(role.docs.as_deref(), Some("A registered account."));
        assert_eq!(role.variants[0].docs.as_deref(), Some("Ordinary user."));
        assert_eq!(role.variants[1].map.as_deref(), Some("admin"));
    }

    #[test]
    fn doc_comments_survive_the_comment_rule() {
        // Trap 1 from the plan: a `COMMENT` rule without the `!"///"` lookahead
        // silently swallows doc comments, producing empty rustdoc and no error.
        let ast = parse_ast("/// kept\nmodel A {\n  id Uuid @id\n}\n").expect("parses");
        let model = ast.models().next().expect("one model");
        assert_eq!(model.docs.as_deref(), Some("kept"));
    }

    #[test]
    fn keywords_do_not_swallow_identifier_prefixes() {
        let ast = parse_ast("model modelish {\n  id Uuid @id\n}\n").expect("parses");
        assert_eq!(ast.models().next().expect("one model").name, "modelish");
    }

    #[test]
    fn relation_arguments_keep_their_shape() {
        let ast = parse_ast(
            "model Post {\n  author User @relation(\"written\", fields: [authorId], references: [id])\n}\n",
        )
        .expect("parses");
        let field = &ast.models().next().expect("one model").fields[0];
        let rel = field.attr("relation").expect("relation attribute");
        assert_eq!(
            rel.first_positional().and_then(Value::as_str),
            Some("written")
        );
        assert_eq!(
            rel.named("fields")
                .and_then(Value::as_array)
                .map(<[Value]>::len),
            Some(1)
        );
    }

    #[test]
    fn malformed_input_reports_a_location() {
        let err = parse_ast("model User {\n  email @unique\n}\n").expect_err("missing a type");
        let rendered = err.to_string();
        assert!(rendered.contains("2:"), "no line/column in {rendered}");
    }
}