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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
extern crate reproto_core as core;
extern crate reproto_lexer as lexer;

use core::{Loc, RpNumber, Span};
use std::borrow::Cow;
use std::ops;
use std::vec;

/// Items can be commented and have attributes.
///
/// This is an intermediate structure used to return these properties.
///
/// ```ignore
/// /// This is a comment.
/// #[foo]
/// #[foo(value = "hello")]
/// <item>
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct Item<'input, T> {
    pub comment: Vec<Cow<'input, str>>,
    pub attributes: Vec<Loc<Attribute<'input>>>,
    pub item: Loc<T>,
}

/// Item derefs into target.
impl<'input, T> ops::Deref for Item<'input, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        Loc::borrow(&self.item)
    }
}

/// Name value pair.
///
/// Is associated with attributes:
///
/// ```ignore
/// #[attribute(name = <value>)]
/// ```
#[derive(Debug, PartialEq, Eq)]
pub enum AttributeItem<'input> {
    Word(Loc<Value<'input>>),
    NameValue {
        name: Loc<Cow<'input, str>>,
        value: Loc<Value<'input>>,
    },
}

/// An attribute.
///
/// Attributes are metadata associated with elements.
///
/// ```ignore
/// #[word]
/// ```
///
/// or:
///
/// ```ignore
/// #[name_value(foo = <value>, bar = <value>)]
/// ```
#[derive(Debug, PartialEq, Eq)]
pub enum Attribute<'input> {
    Word(Loc<Cow<'input, str>>),
    List(Loc<Cow<'input, str>>, Vec<AttributeItem<'input>>),
}

/// A type.
///
/// For example: `u32`, `::Relative::Name`, or `bytes`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type<'input> {
    Double,
    Float,
    Signed {
        size: usize,
    },
    Unsigned {
        size: usize,
    },
    Boolean,
    String,
    Bytes,
    Any,
    /// ISO-8601 for date and time.
    DateTime,
    Name {
        name: Loc<Name<'input>>,
    },
    Array {
        inner: Box<Loc<Type<'input>>>,
    },
    Map {
        key: Box<Loc<Type<'input>>>,
        value: Box<Loc<Type<'input>>>,
    },
    /// A complete error.
    Error,
}

/// Any kind of declaration.
#[derive(Debug, PartialEq, Eq)]
pub enum Decl<'input> {
    Type(Item<'input, TypeBody<'input>>),
    Tuple(Item<'input, TupleBody<'input>>),
    Interface(Item<'input, InterfaceBody<'input>>),
    Enum(Item<'input, EnumBody<'input>>),
    Service(Item<'input, ServiceBody<'input>>),
}

impl<'input> Decl<'input> {
    /// Get the local name for the declaration.
    pub fn name(&self) -> Loc<&str> {
        use self::Decl::*;

        let name: &Loc<Cow<str>> = match *self {
            Type(ref body) => &body.name,
            Tuple(ref body) => &body.name,
            Interface(ref body) => &body.name,
            Enum(ref body) => &body.name,
            Service(ref body) => &body.name,
        };

        Loc::map(Loc::as_ref(name), |n| n.as_ref())
    }

    /// Get all the sub-declarations of this declaraiton.
    pub fn decls(&self) -> Decls {
        use self::Decl::*;

        let decls = match *self {
            Type(ref body) => body.decls(),
            Tuple(ref body) => body.decls(),
            Interface(ref body) => body.decls(),
            Enum(ref body) => body.decls(),
            Service(ref body) => body.decls(),
        };

        Decls {
            iter: decls.into_iter(),
        }
    }

    /// Comment.
    pub fn comment(&self) -> &Vec<Cow<'input, str>> {
        use self::Decl::*;

        match *self {
            Type(ref body) => &body.comment,
            Tuple(ref body) => &body.comment,
            Interface(ref body) => &body.comment,
            Enum(ref body) => &body.comment,
            Service(ref body) => &body.comment,
        }
    }
}

pub struct Decls<'a, 'input: 'a> {
    iter: vec::IntoIter<&'a Decl<'input>>,
}

impl<'a, 'input: 'a> Iterator for Decls<'a, 'input> {
    type Item = &'a Decl<'input>;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

/// The body of an enum declaration.
///
/// ```ignore
/// enum <name> as <ty> {
///   <variants>
///
///   <members>
/// }
/// ```
///
/// Note: members must only be options.
#[derive(Debug, PartialEq, Eq)]
pub struct EnumBody<'input> {
    pub name: Loc<Cow<'input, str>>,
    pub ty: Loc<Type<'input>>,
    pub variants: Vec<Item<'input, EnumVariant<'input>>>,
    pub members: Vec<EnumMember<'input>>,
}

impl<'input> EnumBody<'input> {
    /// Access all inner declarations.
    fn decls(&self) -> Vec<&Decl<'input>> {
        Vec::new()
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct EnumVariant<'input> {
    pub name: Loc<Cow<'input, str>>,
    pub argument: Option<Loc<Value<'input>>>,
}

/// A member in a tuple, type, or interface.
#[derive(Debug, PartialEq, Eq)]
pub enum EnumMember<'input> {
    Code(Loc<Code<'input>>),
}

/// A field.
///
/// ```ignore
/// <name><modifier>: <ty> as <field_as>
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct Field<'input> {
    pub required: bool,
    pub name: Cow<'input, str>,
    pub ty: Loc<Type<'input>>,
    pub field_as: Option<String>,
    /// If the end-of-line indicator present.
    /// A `false` value should indicate an error.
    pub endl: bool,
}

/// A file.
///
/// ```ignore
/// <uses>
///
/// <options>
///
/// <decls>
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct File<'input> {
    pub comment: Vec<Cow<'input, str>>,
    pub attributes: Vec<Loc<Attribute<'input>>>,
    pub uses: Vec<Loc<UseDecl<'input>>>,
    pub decls: Vec<Decl<'input>>,
}

impl<'input> Field<'input> {
    pub fn is_optional(&self) -> bool {
        !self.required
    }
}

/// A name.
///
/// Either:
///
/// ```ignore
/// ::Relative::Name
/// ```
///
/// Or:
///
/// ```ignore
/// <prefix::>Absolute::Name
/// ```
///
/// Note: prefixes names are _always_ imported with `UseDecl`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Name<'input> {
    Relative {
        parts: Vec<Loc<Cow<'input, str>>>,
    },
    Absolute {
        prefix: Option<Loc<Cow<'input, str>>>,
        parts: Vec<Loc<Cow<'input, str>>>,
    },
}

/// The body of an interface declaration
///
/// ```ignore
/// interface <name> {
///   <members>
///   <sub_types>
/// }
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct InterfaceBody<'input> {
    pub name: Loc<Cow<'input, str>>,
    pub members: Vec<TypeMember<'input>>,
    pub sub_types: Vec<Item<'input, SubType<'input>>>,
}

impl<'input> InterfaceBody<'input> {
    /// Access all inner declarations.
    fn decls(&self) -> Vec<&Decl<'input>> {
        let mut out = Vec::new();

        for m in &self.members {
            if let TypeMember::InnerDecl(ref decl) = *m {
                out.push(decl);
            }
        }

        out
    }

    /// Access all fields.
    pub fn fields(&self) -> Vec<&Field<'input>> {
        let mut out = Vec::new();

        for m in &self.members {
            if let TypeMember::Field(ref field) = *m {
                out.push(Loc::borrow(&field.item));
            }
        }

        out
    }
}

/// A contextual code-block.
#[derive(Debug, PartialEq, Eq)]
pub struct Code<'input> {
    pub attributes: Vec<Loc<Attribute<'input>>>,
    pub context: Loc<Cow<'input, str>>,
    pub content: Vec<Cow<'input, str>>,
}

/// A member in a tuple, type, or interface.
#[derive(Debug, PartialEq, Eq)]
pub enum TypeMember<'input> {
    Field(Item<'input, Field<'input>>),
    Code(Loc<Code<'input>>),
    InnerDecl(Decl<'input>),
}

/// The body of a service declaration.
///
/// ```ignore
/// service <name> {
///   <members>
/// }
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct ServiceBody<'input> {
    pub name: Loc<Cow<'input, str>>,
    pub members: Vec<ServiceMember<'input>>,
}

impl<'input> ServiceBody<'input> {
    /// Access all inner declarations.
    fn decls(&self) -> Vec<&Decl<'input>> {
        let mut out = Vec::new();

        for m in &self.members {
            if let ServiceMember::InnerDecl(ref decl) = *m {
                out.push(decl);
            }
        }

        out
    }

    /// Access all endpoints.
    pub fn endpoints(&self) -> Vec<&Endpoint<'input>> {
        let mut out = Vec::new();

        for m in &self.members {
            if let ServiceMember::Endpoint(ref endpoint) = *m {
                out.push(Loc::borrow(&endpoint.item));
            }
        }

        out
    }
}

/// A member of a service declaration.
#[derive(Debug, PartialEq, Eq)]
pub enum ServiceMember<'input> {
    Endpoint(Item<'input, Endpoint<'input>>),
    InnerDecl(Decl<'input>),
}

/// The argument in and endpoint.
#[derive(Debug, PartialEq, Eq)]
pub struct EndpointArgument<'input> {
    pub ident: Loc<Cow<'input, str>>,
    pub channel: Loc<Channel<'input>>,
}

/// An endpoint
///
/// ```ignore
/// <id>(<arguments>) -> <response> as <alias> {
///   <options>
/// }
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct Endpoint<'input> {
    pub id: Loc<Cow<'input, str>>,
    pub alias: Option<String>,
    pub arguments: Vec<EndpointArgument<'input>>,
    pub response: Option<Loc<Channel<'input>>>,
}

/// Describes how data is transferred over a channel.
///
/// ```ignore
/// Unary(stream <ty>)
/// Streaming(<ty>)
/// ```
#[derive(Debug, PartialEq, Eq)]
pub enum Channel<'input> {
    /// Single send.
    Unary { ty: Loc<Type<'input>> },
    /// Multiple sends.
    Streaming { ty: Loc<Type<'input>> },
}

impl<'input> Channel<'input> {
    /// Access the type of the channel.
    pub fn ty(&self) -> &Loc<Type<'input>> {
        use self::Channel::*;

        match *self {
            Unary { ref ty } => ty,
            Streaming { ref ty } => ty,
        }
    }
}

/// The body of a sub-type
///
/// ```ignore
/// <name> as <alias> {
///     <members>
/// }
/// ```
/// Sub-types in interface declarations.
#[derive(Debug, PartialEq, Eq)]
pub struct SubType<'input> {
    pub name: Loc<Cow<'input, str>>,
    pub members: Vec<TypeMember<'input>>,
    pub alias: Option<Loc<Value<'input>>>,
}

/// The body of a tuple
///
/// ```ignore
/// tuple <name> {
///     <members>
/// }
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct TupleBody<'input> {
    pub name: Loc<Cow<'input, str>>,
    pub members: Vec<TypeMember<'input>>,
}

impl<'input> TupleBody<'input> {
    /// Access all inner declarations.
    fn decls(&self) -> Vec<&Decl<'input>> {
        let mut out = Vec::new();

        for m in &self.members {
            if let TypeMember::InnerDecl(ref decl) = *m {
                out.push(decl);
            }
        }

        out
    }

    /// Access all fields.
    pub fn fields(&self) -> Vec<&Field<'input>> {
        let mut out = Vec::new();

        for m in &self.members {
            if let TypeMember::Field(ref field) = *m {
                out.push(Loc::borrow(&field.item));
            }
        }

        out
    }
}

/// The body of a type
///
/// ```ignore
/// type <name> {
///     <members>
/// }
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct TypeBody<'input> {
    pub name: Loc<Cow<'input, str>>,
    pub members: Vec<TypeMember<'input>>,
}

impl<'input> TypeBody<'input> {
    /// Access all inner declarations.
    fn decls(&self) -> Vec<&Decl<'input>> {
        let mut out = Vec::new();

        for m in &self.members {
            if let TypeMember::InnerDecl(ref decl) = *m {
                out.push(decl);
            }
        }

        out
    }

    /// Access all fields.
    pub fn fields(&self) -> Vec<&Field<'input>> {
        let mut out = Vec::new();

        for m in &self.members {
            if let TypeMember::Field(ref field) = *m {
                out.push(Loc::borrow(&field.item));
            }
        }

        out
    }
}

/// A package declaration.
#[derive(Debug, PartialEq, Eq)]
pub enum Package<'input> {
    /// A parsed package.
    Package { parts: Vec<Loc<Cow<'input, str>>> },
    /// A recovered error.
    Error,
}

/// A use declaration
///
/// ```ignore
/// use <package> "<range>" as <alias>;
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct UseDecl<'input> {
    pub package: Loc<Package<'input>>,
    pub range: Option<Loc<String>>,
    pub alias: Option<Loc<Cow<'input, str>>>,
    /// If the end-of-line indicator present.
    /// A empty value should indicate an error.
    pub endl: Option<Span>,
}

/// A literal value
///
/// For example, `"string"`, `42.0`, and `foo`.
#[derive(Debug, PartialEq, Eq)]
pub enum Value<'input> {
    String(String),
    Number(RpNumber),
    Identifier(Cow<'input, str>),
    Array(Vec<Loc<Value<'input>>>),
}

/// A part of a step.
#[derive(Debug, PartialEq, Eq)]
pub enum PathPart<'input> {
    Variable(Cow<'input, str>),
    Segment(String),
}

/// A step in a path specification.
#[derive(Debug, PartialEq, Eq)]
pub struct PathStep<'input> {
    pub parts: Vec<PathPart<'input>>,
}

/// A path specification.
#[derive(Debug, PartialEq, Eq)]
pub struct PathSpec<'input> {
    pub steps: Vec<PathStep<'input>>,
}