-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.lalrpop
84 lines (73 loc) · 3.4 KB
/
grammar.lalrpop
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
use ::std::str::FromStr;
use crate::ast::{span, Span};
use crate::ast::evolution::*;
use crate::ast::object::*;
use crate::ast::term::*;
grammar;
match {
// Turn newlines into a token (they are skipped by default)
r"(\r\n|\n|\r)(\s|\r|\n)*" => END,
} else {
// Match string literals and escaped tokens
r#"null"# => NULL,
r#"("[^"]*"|([^"]*\\")*)"# => STR,
r#"`[a-zA-Z_][a-zA-Z0-9_]*`"# => ESC_IDEN,
} else {
// Ignore comments (only full-line for now)
r#"\s*#[^\r\n]*(\r\n|\n|\r)(\s|\r|\n)*"# => { },
} else {
// Ignore any other whitespace (except newlines above)
r"\s+" => { },
} else {
_
}
pub evolution: EvolutionAst = {
END?
"apivolve" <v:r"v[0-9]+\.[0-9]+(\.[0-9]+)?"> END
<ds:(<dependency>)*>
// TODO @mark: is bump still used?
<vb:(<bump>)?>
<es:(<block>)*>
END? => EvolutionAst::new(v.to_owned(), vb, ds, es),
};
dependency: Dependency = {
"depends on" <p:STR> "dynamic" END => Dependency::dynamic(Path::new(p)),
// "depends on" <p:path> "hash" <hash:"'[a-zA-Z0-9]+'"> END => Dependency::fixed(p, hash),
"depends on" <p:STR> "hash" <hash:STR> END => Dependency::fixed(Path::new(p), hash),
};
bump: (Span, VersionBump) = {
<l: @L> "bump" "patch" <r: @R> END => (span(l, r), VersionBump::Patch),
<l: @L> "bump" "minor" <r: @R> END => (span(l, r), VersionBump::Minor),
<l: @L> "bump" "major" <r: @R> END => (span(l, r), VersionBump::Major),
};
block: Block = {
"add" "object" <name:identifier> "{" END "description" <l: @L> <desc:STR> <r: @R> END <fields:(<object_field>)*> "}" =>
Block::Obj(ObjectEvolution::new(name, ObjectOp::Add(ObjectAdd::with_desc((span(l, r), desc), fields)))),
"add" "object" <name:identifier> "{" END <fields:(<object_field>)*> "}" =>
Block::Obj(ObjectEvolution::new(name, ObjectOp::Add(ObjectAdd::minimal(fields)))),
"change" "object" <name:identifier> "{" END "description" <l: @L> <desc:STR> <r: @R> END <fields:(<object_field>)*> "}" =>
Block::Obj(ObjectEvolution::new(name, ObjectOp::Change(ObjectChange::with_desc((span(l, r), desc), fields)))),
"change" "object" <name:identifier> "{" END <fields:(<object_field>)*> "}" =>
Block::Obj(ObjectEvolution::new(name, ObjectOp::Change(ObjectChange::minimal(fields)))),
};
object_field: FieldEvolution = {
"add" <name:identifier> END? <props:object_field_prop*> => FieldEvolution::new(name, FieldOp::Add(props)),
"change" <name:identifier> END? <props:object_field_prop*> => FieldEvolution::new(name, FieldOp::Change(props)),
"delete" <name:identifier> END => FieldEvolution::new(name, FieldOp::Delete()),
}
object_field_prop: FieldProperty = {
"name" <n:identifier> END? => FieldProperty::Name(n),
"type" <n:identifier> END? => FieldProperty::Type(n),
"description" <l: @L> <d:STR> <r: @R> END? => FieldProperty::Description(d.to_owned(), span(l, r)),
"default" <v:value> END? => FieldProperty::Default(Expression::Val(v)),
}
identifier: Iden = {
<l: @L> <n:r"[a-zA-Z_][a-zA-Z0-9_]*"> <r: @R> => Iden::new(n.to_owned(), l, r),
<l: @L> <n:ESC_IDEN> <r: @R> => Iden::new_backticked(n.to_owned(), l, r),
};
value: Value = {
<l: @L> <v:STR> <r: @R> => Value::str(v, span(l, r)),
<l: @L> <v:r"[-+]?[0-9]+"> <r: @R> => Value::int(v, span(l, r)),
<l: @L> <v:r"[-+]?[0-9]+\.[0-9]+"> <r: @R> => Value::real(v, span(l, r)),
<l: @L> <v:NULL> <r: @R> => Value::None(span(l, r)),
}