-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgrammar.rs
More file actions
675 lines (573 loc) · 19 KB
/
grammar.rs
File metadata and controls
675 lines (573 loc) · 19 KB
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
use crate::lexicon::{Cursor, Intern, Interner, Lexeme, Symbol, TokenAlias, TokenList, TokenRef};
use crate::locator::Locator;
use crate::span::Span;
use generational_indextree::NodeEdge;
use sha2::Digest;
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};
use std::hash::Hash;
pub type NodeIdx = generational_indextree::NodeId;
/// The trait describing a language grammar.
// Note: we need those bounds on the trait itself to deal with the
// incorrect bounds generated by derive (https://github.com/rust-lang/rust/issues/26925).
pub trait Grammar: Clone + Default + Debug {
/// The type of lexemes (aka. tokens).
type Lex: Lexeme;
/// The type of syntax node kinds.
type Kind: Copy + Clone + PartialEq + Debug;
/// The type of parsing expression tags, for memoization.
type Tag: Copy + Clone + PartialEq + Eq + Hash + Debug;
}
#[derive(Clone)]
pub enum SyntaxTrunk<G: Grammar> {
Leaf(TokenAlias<G::Lex>),
Tree(G::Kind),
Error,
}
impl<G: Grammar> Debug for SyntaxTrunk<G> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
SyntaxTrunk::Leaf(t) => write!(f, "Leaf[{:?}]", t.kind()),
SyntaxTrunk::Tree(k) => write!(f, "Tree[{k:?}]"),
SyntaxTrunk::Error => write!(f, "Error"),
}
}
}
impl<G: Grammar> Copy for SyntaxTrunk<G> {}
/// A trait for internally mutable node state.
pub trait Core: Default + Clone + Debug {}
impl<C: Default + Clone + Debug> Core for C {}
/// The wrapper type around the syntax node core.
///
/// It is allocated on write as we assume that the number of nodes with a core
/// is small compared to the total number of nodes in a syntax tree.
///
/// An alternative would be to use an arena instead of heap allocations.
type CoreCell<T> = RefCell<Option<Box<T>>>;
#[derive(Clone, Debug)]
pub struct SyntaxNode<T: Core, G: Grammar>(SyntaxTrunk<G>, CoreCell<T>);
impl<T: Core, G: Grammar> SyntaxNode<T, G> {
pub fn new(trunk: SyntaxTrunk<G>) -> Self {
SyntaxNode(trunk, RefCell::new(None))
}
pub fn trunk(&self) -> &SyntaxTrunk<G> {
&self.0
}
pub fn core_from(&self, other: &Self) {
self.1.replace(other.1.borrow().clone());
}
pub fn has_core(&self) -> bool {
self.1.borrow().is_some()
}
pub fn core_ref(&self) -> Ref<T> {
Ref::map(self.1.borrow(), |r| {
r.as_ref().expect("core should exist").as_ref()
})
}
pub fn core_mut(&self) -> RefMut<T> {
RefMut::map(self.1.borrow_mut(), |r| {
r.get_or_insert_with(Box::default).as_mut()
})
}
}
type TreeArena<T, G> = generational_indextree::Arena<SyntaxNode<T, G>>;
pub struct SyntaxTree<T: Core, G: Grammar> {
tokens: TokenList<G::Lex>,
tree: TreeArena<T, G>,
root: Option<NodeIdx>,
}
impl<T: Core, G: Grammar> Interner for SyntaxTree<T, G> {
fn register<S: AsRef<str>>(&mut self, s: S) -> Symbol {
self.tokens.register(s)
}
fn resolve(&self, sym: Symbol) -> &str {
self.tokens.resolve(sym)
}
}
impl<T: Core, G: Grammar> Debug for SyntaxTree<T, G> {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
if let Some(root) = self.root {
NodeRef::from(self, root).fmt(f)
} else {
write!(f, "[empty tree]")
}
}
}
impl<T: Core, G: Grammar> SyntaxTree<T, G> {
pub fn new(tokens: TokenList<G::Lex>) -> Self {
SyntaxTree {
tokens,
tree: TreeArena::default(),
root: Option::default(),
}
}
pub fn locator(&self) -> &Locator {
self.tokens.locator()
}
pub fn finalize(mut self, root: NodeIdx) -> Self {
self.root = Some(root);
self
}
pub fn root(&self) -> NodeRef<T, G> {
NodeRef::from(self, self.root.unwrap())
}
pub fn detach(&self, from: NodeIdx) -> SyntaxTree<T, G> {
let tokens = TokenList::new(self.tokens.locator().clone());
let mut tree = SyntaxTree::new(tokens);
let mut parents: Vec<NodeIdx> = Vec::new();
let mut root: Option<NodeIdx> = None;
self.traverse(from).for_each(|edge| {
match edge {
NodeEdge::Start(id) => {
let node = self.node(id);
let new_trunk = match node.trunk() {
SyntaxTrunk::Leaf(t) => {
let cursor = t.cursor();
let (token, span) = self.tokens.token_span(cursor);
let new_value = token.value().copy(self, &mut tree);
let new_token =
<<G as Grammar>::Lex as Lexeme>::new(token.kind(), new_value);
let new_cursor = tree.tokens.push(new_token, span.range());
SyntaxTrunk::Leaf(tree.tokens.alias(new_cursor))
}
t => *t,
};
let new_syntax = SyntaxNode::new(new_trunk);
new_syntax.core_from(node);
let new_id = tree.new_node(new_syntax);
if let Some(parent) = parents.last() {
tree.append(*parent, new_id)
}
parents.push(new_id);
}
NodeEdge::End(_) => {
root = parents.pop();
}
};
});
tree.finalize(root.unwrap())
}
pub fn count(&self) -> usize {
self.tree.count()
}
fn node(&self, id: NodeIdx) -> &SyntaxNode<T, G> {
self.tree.get(id).unwrap().get()
}
fn children(&self, id: NodeIdx) -> impl Iterator<Item = NodeIdx> + '_ {
id.children(&self.tree)
}
fn reverse_children(&self, id: NodeIdx) -> impl Iterator<Item = NodeIdx> + '_ {
id.reverse_children(&self.tree)
}
fn ancestors(&self, id: NodeIdx) -> impl Iterator<Item = NodeIdx> + '_ {
id.ancestors(&self.tree)
}
fn descendants(&self, id: NodeIdx) -> impl Iterator<Item = NodeIdx> + '_ {
id.descendants(&self.tree)
}
fn traverse(&self, id: NodeIdx) -> impl Iterator<Item = NodeEdge> + '_ {
id.traverse(&self.tree)
}
fn new_node(&mut self, n: SyntaxNode<T, G>) -> NodeIdx {
self.tree.new_node(n)
}
fn append(&mut self, parent: NodeIdx, child: NodeIdx) {
parent.append(child, &mut self.tree);
}
}
pub struct NodeRef<'a, T: Core, G: Grammar> {
tree: &'a SyntaxTree<T, G>,
idx: NodeIdx,
}
// Note: for some reason the derive macro is not doing the right thing for Clone/Copy.
impl<'a, T: Core, G: Grammar> Clone for NodeRef<'a, T, G> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T: Core, G: Grammar> Copy for NodeRef<'a, T, G> {}
#[derive(Debug)]
pub enum NodeCursor<'a, T: Core, G: Grammar> {
Start(NodeRef<'a, T, G>),
End(NodeRef<'a, T, G>),
}
impl<'a, T: Core, G: Grammar> NodeRef<'a, T, G> {
pub fn from(tree: &'a SyntaxTree<T, G>, idx: NodeIdx) -> Self {
NodeRef { tree, idx }
}
pub fn tree(&self) -> &'a SyntaxTree<T, G> {
self.tree
}
pub fn index(&self) -> NodeIdx {
self.idx
}
pub fn syntax(&self) -> &'a SyntaxNode<T, G> {
self.tree.node(self.idx)
}
pub fn token(&self) -> TokenRef<'a, G::Lex> {
match self.syntax().trunk() {
SyntaxTrunk::Leaf(t) => self.tree.tokens.reference(t.cursor()),
_ => panic!("a node must be a leaf to link to a token"),
}
}
pub fn len(&self) -> usize {
self.tree.children(self.idx).count()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn children(&self) -> impl Iterator<Item = NodeRef<'a, T, G>> + 'a {
self.tree
.children(self.idx)
.map(|id| NodeRef::from(self.tree, id))
}
pub fn reverse_children(&self) -> impl Iterator<Item = NodeRef<'a, T, G>> + 'a {
self.tree
.reverse_children(self.idx)
.map(|id| NodeRef::from(self.tree, id))
}
pub fn first(&self) -> NodeRef<'a, T, G> {
self.children().next().unwrap()
}
pub fn last(&self) -> NodeRef<'a, T, G> {
self.reverse_children().next().unwrap()
}
pub fn nth(&self, n: usize) -> NodeRef<'a, T, G> {
let Some(node) = self.children().nth(n) else {
panic!("expected node at {n}")
};
node
}
pub fn ancestors(&self) -> impl Iterator<Item = NodeRef<'a, T, G>> + 'a {
self.tree
.ancestors(self.idx)
.map(|id| NodeRef::from(self.tree, id))
}
pub fn descendants(&self) -> impl Iterator<Item = NodeRef<'a, T, G>> + 'a {
self.tree
.descendants(self.idx)
.map(|id| NodeRef::from(self.tree, id))
}
pub fn traverse(&self) -> impl Iterator<Item = NodeCursor<'a, T, G>> + 'a {
self.tree.traverse(self.idx).map(|edge| match edge {
NodeEdge::Start(id) => NodeCursor::Start(NodeRef::from(self.tree, id)),
NodeEdge::End(id) => NodeCursor::End(NodeRef::from(self.tree, id)),
})
}
/// Returns the span of text from first to last token, if any.
pub fn span(&self) -> Option<Span> {
if let (Some(start), Some(end)) = (self.start(), self.end()) {
let s = start.span();
let e = end.span();
Some(Span::new(s.locator().clone(), s.start()..e.end()))
} else {
None
}
}
/// Returns a reference to the first token, if any.
pub fn start(&self) -> Option<TokenRef<'a, G::Lex>> {
match self.syntax().trunk() {
SyntaxTrunk::Leaf(t) => Some(self.tree.tokens.reference(t.cursor())),
_ => self.children().find_map(|c| c.start()),
}
}
/// Returns a reference to the last token, if any.
pub fn end(&self) -> Option<TokenRef<'a, G::Lex>> {
match self.syntax().trunk() {
SyntaxTrunk::Leaf(t) => Some(self.tree.tokens.reference(t.cursor())),
_ => self.reverse_children().find_map(|c| c.end()),
}
}
pub fn detach(&self) -> SyntaxTree<T, G> {
self.tree.detach(self.idx)
}
pub fn as_str(&self) -> &'a str {
self.token().value().as_str(self.tree)
}
/// Update the given digest with node reference information.
pub fn digest<D: Digest>(&self, digest: &mut D) {
let (index, generation) = generational_arena::Index::from(self.idx).into_raw_parts();
digest.update(self.tree.locator().url().as_str());
digest.update(index.to_be_bytes());
digest.update(generation.to_be_bytes());
}
}
impl<'a, T: Core, G: Grammar> Debug for NodeRef<'a, T, G> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.syntax().trunk())?;
if self.syntax().has_core() {
write!(f, " ({:?})", self.syntax().core_ref())?;
}
if !self.is_empty() {
write!(f, " -> ")?;
f.debug_list().entries(self.children()).finish()?;
}
Ok(())
}
}
/// An abstract type over a concrete syntax node.
pub trait AbstractSyntaxNode<'a, T: Core, G: Grammar>
where
Self: Sized,
{
fn cast(node: NodeRef<'a, T, G>) -> Option<Self>;
fn node(&self) -> NodeRef<'a, T, G>;
}
#[macro_export]
macro_rules! syntax_nodes {
( $grammar:ident, $( $node:ident ),+ ) => {
#[allow(dead_code)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum SyntaxKind {
$( $node ),+
}
$(
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
pub struct $node<'a, T: Core>(NodeRef<'a, T, $grammar>);
#[allow(dead_code)]
impl<'a, T: Core> AbstractSyntaxNode<'a, T, $grammar> for $node<'a, T>
{
fn cast(node: NodeRef<'a, T, $grammar>) -> Option<Self> {
match node.syntax().trunk() {
SyntaxTrunk::Tree(SyntaxKind::$node) => Some($node(node)),
_ => None,
}
}
fn node(&self) -> NodeRef<'a, T, $grammar> {
self.0
}
}
)+
}
}
#[macro_export]
macro_rules! terminal_node {
( $grammar:ident, $node:ident, $(|)? $( $pattern:pat_param )|+ $( if $guard:expr )? $(,)? ) => {
#[allow(dead_code)]
#[derive(Debug)]
pub struct $node<'a, T: Core>(NodeRef<'a, T, $grammar>);
#[allow(dead_code)]
impl<'a, T: Core> AbstractSyntaxNode<'a, T, $grammar> for $node<'a, T> {
fn cast(node: NodeRef<'a, T, $grammar>) -> Option<Self> {
match node.syntax().trunk() {
SyntaxTrunk::Leaf(t) if matches!(t.kind(), $( $pattern )|+ $( if $guard )?) => Some(Self(node)),
_ => None,
}
}
fn node(&self) -> NodeRef<'a, T, $grammar> {
self.0
}
}
};
}
#[derive(Debug, Clone)]
pub struct ParserError(&'static str, Span);
impl ParserError {
pub fn new(error: &'static str, span: Span) -> Self {
ParserError(error, span)
}
pub fn span(&self) -> Span {
self.1.clone()
}
}
impl Display for ParserError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(self.0)
}
}
impl std::error::Error for ParserError {}
/// A successful production from a parser function.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParserMatch<G: Grammar> {
Token(TokenAlias<G::Lex>),
Syntax(G::Kind),
Node(NodeIdx),
}
/// A result from a parser function.
pub type ParserResult<G> = std::result::Result<(Cursor, ParserMatch<G>), ParserError>;
/// A syntax analysis context.
pub struct Context<T: Core, G: Grammar> {
tree: SyntaxTree<T, G>,
cache: HashMap<(Cursor, G::Tag), ParserResult<G>>,
hits: Cell<usize>,
reads: Cell<usize>,
no_cache: bool,
}
impl<T: Core, G: Grammar> Context<T, G> {
pub fn new(tokens: TokenList<G::Lex>) -> Self {
Context {
tree: SyntaxTree::new(tokens),
cache: Default::default(),
hits: Cell::new(0),
reads: Cell::new(0),
no_cache: false,
}
}
pub fn without_cache(mut self) -> Self {
self.no_cache = true;
self
}
pub fn head(&self) -> Cursor {
self.skip_trivia(self.tree.tokens.head())
}
pub fn span(&self, s: Cursor) -> Span {
if s.is_valid() {
self.tree.tokens.token_span(s).1
} else {
let end = self.tree.tokens.end();
Span::new(self.tree.tokens.locator().clone(), end..end + 1)
}
}
pub fn tree(self) -> SyntaxTree<T, G> {
self.tree
}
pub fn cache(&mut self, p: G::Tag, s: Cursor, r: ParserResult<G>) {
if !self.no_cache {
self.cache.insert((s, p), r);
}
}
pub fn lookup(&mut self, p: G::Tag, s: Cursor) -> Option<ParserResult<G>> {
if self.no_cache {
return None;
}
let hit = self.cache.get(&(s, p)).cloned();
if hit.is_some() {
self.hits.set(self.hits.get() + 1);
}
hit
}
pub fn compose(&mut self, kind: G::Kind, children: &[ParserMatch<G>]) -> ParserMatch<G> {
if children.is_empty() {
return ParserMatch::Syntax(kind);
}
let node = SyntaxNode::new(SyntaxTrunk::Tree(kind));
let parent = self.tree.new_node(node);
for child in children {
let n = match *child {
ParserMatch::Token(t) => self.tree.new_node(SyntaxNode::new(SyntaxTrunk::Leaf(t))),
ParserMatch::Syntax(k) => self.tree.new_node(SyntaxNode::new(SyntaxTrunk::Tree(k))),
ParserMatch::Node(n) => n,
};
self.tree.append(parent, n)
}
ParserMatch::Node(parent)
}
pub fn count(&self) -> usize {
self.tree.count()
}
fn skip_trivia(&self, mut s: Cursor) -> Cursor {
while s.is_valid() && G::Lex::is_trivia(self.tree.tokens.kind(s)) {
s = self.tree.tokens.advance(s);
}
s
}
fn peek(&self, s: Cursor) -> Option<TokenAlias<G::Lex>> {
if s.is_valid() {
Some(self.tree.tokens.alias(s))
} else {
None
}
}
fn pop(&self, s: Cursor) -> Option<(Cursor, TokenAlias<G::Lex>)> {
self.reads.set(self.reads.get() + 1);
self.peek(s)
.map(|t| (self.skip_trivia(self.tree.tokens.advance(s)), t))
}
}
impl<T: Core, G: Grammar> Debug for Context<T, G> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Context")
.field("arena_size", &self.count())
.field("input_length", &self.tree.tokens.len())
.field("input_reads", &self.reads.get())
.field("cache_hits", &self.hits.get())
.field("cache_size", &self.cache.len())
.finish()
}
}
/// The type of parser production functions.
pub type ParserFn<T, G> = for<'a> fn(&'a mut Context<T, G>, Cursor) -> ParserResult<G>;
pub fn memoize<T: Core, G: Grammar>(
t: G::Tag,
c: &mut Context<T, G>,
s: Cursor,
p: ParserFn<T, G>,
) -> ParserResult<G> {
if let Some(r) = c.lookup(t, s) {
r
} else {
let r = p(c, s);
c.cache(t, s, r.clone());
r
}
}
pub fn repeat<T: Core, G: Grammar>(
c: &mut Context<T, G>,
mut s: Cursor,
ns: &mut Vec<ParserMatch<G>>,
ps: &[ParserFn<T, G>],
) -> Cursor {
'outer: loop {
for p in ps {
if let Ok((s1, n)) = p(c, s) {
ns.push(n);
s = s1;
} else {
break 'outer;
}
}
}
s
}
pub fn intersperse<T: Core, G: Grammar, P, I>(
c: &mut Context<T, G>,
s: Cursor,
ns: &mut Vec<ParserMatch<G>>,
parser: P,
infix: I,
) -> std::result::Result<Cursor, ParserError>
where
P: Fn(&mut Context<T, G>, Cursor) -> ParserResult<G>,
I: Fn(&mut Context<T, G>, Cursor) -> ParserResult<G>,
{
let (s, n) = parser(c, s)?;
ns.push(n);
let mut sl = s;
loop {
let s = sl;
if let Ok((s, n0, n1)) =
infix(c, s).and_then(|(s, n0)| parser(c, s).map(|(s, n1)| (s, n0, n1)))
{
ns.push(n0);
ns.push(n1);
sl = s;
} else {
break Ok(s);
}
}
}
pub fn parse_token_with<T: Core, G: Grammar, F>(
c: &mut Context<T, G>,
s: Cursor,
pred: F,
) -> ParserResult<G>
where
F: Fn(&<G::Lex as Lexeme>::Kind) -> bool,
{
match c.pop(s) {
Some((s, t)) if pred(&t.kind()) => Ok((s, ParserMatch::Token(t))),
_ => Err(ParserError::new(
"unexpected token or end of input",
c.span(s),
)),
}
}
pub fn parse_token<T: Core, G: Grammar>(
c: &mut Context<T, G>,
s: Cursor,
kind: <G::Lex as Lexeme>::Kind,
) -> ParserResult<G> {
parse_token_with(c, s, |k| *k == kind)
}