Skip to main content

rdf_parsers/ntriples/
parser.rs

1use crate::TokenTrait;
2pub type SyntaxNode = rowan::SyntaxNode<Lang>;
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, logos :: Logos)]
4# [logos (subpattern HEX = r#"([0-9]|[A-F]|[a-f])"#)]
5# [logos (subpattern UCHAR = r#"(\\u(?&HEX)(?&HEX)(?&HEX)(?&HEX)|\\U(?&HEX)(?&HEX)(?&HEX)(?&HEX)(?&HEX)(?&HEX)(?&HEX)(?&HEX))"#)]
6# [logos (subpattern IRIREF = r#"<(([^\x{00}-\x{20}<>"{}|^`\\]|(?&UCHAR)))*>"#)]
7# [logos (subpattern PN_CHARS_BASE = r#"([A-Z]|[a-z]|[\x{00C0}-\x{00D6}]|[\x{00D8}-\x{00F6}]|[\x{00F8}-\x{02FF}]|[\x{0370}-\x{037D}]|[\x{037F}-\x{1FFF}]|[\x{200C}-\x{200D}]|[\x{2070}-\x{218F}]|[\x{2C00}-\x{2FEF}]|[\x{3001}-\x{D7FF}]|[\x{F900}-\x{FDCF}]|[\x{FDF0}-\x{FFFD}]|[\x{10000}-\x{EFFFF}])"#)]
8# [logos (subpattern PN_CHARS_U = r#"((?&PN_CHARS_BASE)|_|:)"#)]
9# [logos (subpattern PN_CHARS = r#"((?&PN_CHARS_U)|-|[0-9]|\u00B7|[\x{0300}-\x{036F}]|[\x{203F}-\x{2040}])"#)]
10# [logos (subpattern BLANK_NODE_LABEL = r#"_:((?&PN_CHARS_U)|[0-9])((((?&PN_CHARS)|\.))*(?&PN_CHARS))?"#)]
11# [logos (subpattern LANGTAG = r#"@([a-zA-Z])+(-([a-zA-Z0-9])+)*"#)]
12# [logos (subpattern ECHAR = r#"\\[tbnrf"'\\]"#)]
13# [logos (subpattern STRING_LITERAL_QUOTE = r#""(([^\x{22}\x{5C}\x{A}\x{D}]|(?&ECHAR)|(?&UCHAR)))*""#)]
14#[repr(u16)]
15pub enum SyntaxKind {
16    Eof = 0,
17    #[regex(r"[ \t\n\r]+")]
18    WhiteSpace,
19    #[regex(r"#[^\n]*", allow_greedy = true)]
20    Comment,
21    #[doc = r" producings"]
22    Literal,
23    NtriplesDoc,
24    Object,
25    Predicate,
26    Subject,
27    Triple,
28    #[doc = r" terminals"]
29    #[token(".")]
30    Stop,
31    #[token("^^")]
32    Datatype,
33    #[regex("(?&BLANK_NODE_LABEL)")]
34    BlankNodeLabel,
35    #[regex("(?&IRIREF)")]
36    Iriref,
37    #[regex("(?&LANGTAG)")]
38    Langtag,
39    #[regex("(?&STRING_LITERAL_QUOTE)")]
40    StringLiteralQuote,
41    Error,
42    ROOT,
43}
44impl From<SyntaxKind> for rowan::SyntaxKind {
45    fn from(kind: SyntaxKind) -> Self {
46        Self(kind as u16)
47    }
48}
49impl From<rowan::SyntaxKind> for SyntaxKind {
50    fn from(value: rowan::SyntaxKind) -> Self {
51        assert!(value.0 <= SyntaxKind::ROOT as u16);
52        unsafe { std::mem::transmute::<u16, SyntaxKind>(value.0) }
53    }
54}
55#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
56pub enum Lang {}
57impl rowan::Language for Lang {
58    type Kind = SyntaxKind;
59    fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
60        assert!(raw.0 <= SyntaxKind::ROOT as u16);
61        unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
62    }
63    fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
64        kind.into()
65    }
66}
67mod definitions {
68    use super::*;
69    #[derive(Debug, Clone, Copy)]
70    pub struct Rule {
71        pub kind: SyntaxKind,
72        pub state: usize,
73    }
74    impl Rule {
75        pub fn new(kind: SyntaxKind) -> Self {
76            match kind {
77                SyntaxKind::NtriplesDoc => Rule {
78                    kind,
79                    state: 1usize,
80                },
81                SyntaxKind::Triple => Rule {
82                    kind,
83                    state: 4usize,
84                },
85                SyntaxKind::Subject => Rule {
86                    kind,
87                    state: 1usize,
88                },
89                SyntaxKind::Predicate => Rule {
90                    kind,
91                    state: 1usize,
92                },
93                SyntaxKind::Object => Rule {
94                    kind,
95                    state: 1usize,
96                },
97                SyntaxKind::Literal => Rule {
98                    kind,
99                    state: 6usize,
100                },
101                SyntaxKind::Stop => Rule { kind, state: 0 },
102                SyntaxKind::Datatype => Rule { kind, state: 0 },
103                SyntaxKind::BlankNodeLabel => Rule { kind, state: 0 },
104                SyntaxKind::Iriref => Rule { kind, state: 0 },
105                SyntaxKind::Langtag => Rule { kind, state: 0 },
106                SyntaxKind::StringLiteralQuote => Rule { kind, state: 0 },
107                _ => panic!("Unknown rule kind {:?}", kind),
108            }
109        }
110    }
111    pub fn first_tokens(kind: SyntaxKind) -> &'static [SyntaxKind] {
112        match kind {
113            SyntaxKind::Literal => &[],
114            SyntaxKind::NtriplesDoc => &[],
115            SyntaxKind::Object => &[],
116            SyntaxKind::Predicate => &[],
117            SyntaxKind::Subject => &[],
118            SyntaxKind::Triple => &[],
119            SyntaxKind::Stop => &[SyntaxKind::Stop],
120            SyntaxKind::Datatype => &[SyntaxKind::Datatype],
121            SyntaxKind::BlankNodeLabel => &[SyntaxKind::BlankNodeLabel],
122            SyntaxKind::Iriref => &[SyntaxKind::Iriref],
123            SyntaxKind::Langtag => &[SyntaxKind::Langtag],
124            SyntaxKind::StringLiteralQuote => &[SyntaxKind::StringLiteralQuote],
125            _ => &[],
126        }
127    }
128    #[doc = r" Returns the minimum error cost that `kind` must incur when `tok`"]
129    #[doc = r" is the current token.  0 means the token is reachable (or the rule"]
130    #[doc = r" is nullable); positive means the rule cannot make progress without"]
131    #[doc = r" at least that much error cost."]
132    pub fn min_error_for_token(kind: SyntaxKind, tok: SyntaxKind) -> isize {
133        match kind {
134            SyntaxKind::Literal => match tok {
135                SyntaxKind::Datatype
136                | SyntaxKind::Iriref
137                | SyntaxKind::Langtag
138                | SyntaxKind::StringLiteralQuote => 0,
139                _ => kind.max_error_value(),
140            },
141            SyntaxKind::Object => match tok {
142                SyntaxKind::BlankNodeLabel
143                | SyntaxKind::Datatype
144                | SyntaxKind::Iriref
145                | SyntaxKind::Langtag
146                | SyntaxKind::StringLiteralQuote => 0,
147                _ => kind.max_error_value(),
148            },
149            SyntaxKind::Predicate => match tok {
150                SyntaxKind::Iriref => 0,
151                _ => kind.max_error_value(),
152            },
153            SyntaxKind::Subject => match tok {
154                SyntaxKind::BlankNodeLabel | SyntaxKind::Iriref => 0,
155                _ => kind.max_error_value(),
156            },
157            SyntaxKind::Triple => match tok {
158                SyntaxKind::BlankNodeLabel
159                | SyntaxKind::Datatype
160                | SyntaxKind::Iriref
161                | SyntaxKind::Langtag
162                | SyntaxKind::Stop
163                | SyntaxKind::StringLiteralQuote => 0,
164                _ => kind.max_error_value(),
165            },
166            SyntaxKind::Stop => match tok {
167                SyntaxKind::Stop => 0,
168                _ => kind.max_error_value(),
169            },
170            SyntaxKind::Datatype => match tok {
171                SyntaxKind::Datatype => 0,
172                _ => kind.max_error_value(),
173            },
174            SyntaxKind::BlankNodeLabel => match tok {
175                SyntaxKind::BlankNodeLabel => 0,
176                _ => kind.max_error_value(),
177            },
178            SyntaxKind::Iriref => match tok {
179                SyntaxKind::Iriref => 0,
180                _ => kind.max_error_value(),
181            },
182            SyntaxKind::Langtag => match tok {
183                SyntaxKind::Langtag => 0,
184                _ => kind.max_error_value(),
185            },
186            SyntaxKind::StringLiteralQuote => match tok {
187                SyntaxKind::StringLiteralQuote => 0,
188                _ => kind.max_error_value(),
189            },
190            _ => 0,
191        }
192    }
193    #[doc = r" Precomputed dist(q, a): minimum insertion cost to reach a point"]
194    #[doc = r" where terminal `terminal` can be matched, starting from parser"]
195    #[doc = r" state `(kind, state)`.  Returns 0 as the conservative default"]
196    #[doc = r" (admissible — parent context might accept the terminal after a pop)."]
197    pub fn state_dist(kind: SyntaxKind, state: usize, terminal: SyntaxKind) -> isize {
198        match (kind, state, terminal) {
199            (SyntaxKind::Literal, 2usize, _) => match terminal {
200                SyntaxKind::Langtag | SyntaxKind::Datatype => 0,
201                _ => 1isize,
202            },
203            (SyntaxKind::Literal, 3usize, _) => match terminal {
204                SyntaxKind::Iriref => 0,
205                _ => 1isize,
206            },
207            (SyntaxKind::Literal, 4usize, _) => match terminal {
208                SyntaxKind::BlankNodeLabel => 2isize,
209                SyntaxKind::Iriref => 1isize,
210                SyntaxKind::Langtag => 2isize,
211                SyntaxKind::StringLiteralQuote => 2isize,
212                SyntaxKind::Stop => 2isize,
213                _ => 0,
214            },
215            (SyntaxKind::Literal, 5usize, _) => match terminal {
216                SyntaxKind::Langtag => 0,
217                _ => 1isize,
218            },
219            (SyntaxKind::Literal, 6usize, _) => match terminal {
220                SyntaxKind::StringLiteralQuote => 0,
221                _ => 1isize,
222            },
223            (SyntaxKind::NtriplesDoc, 2usize, _) => match terminal {
224                SyntaxKind::BlankNodeLabel | SyntaxKind::Iriref => 0,
225                _ => 1isize,
226            },
227            (SyntaxKind::Object, 1usize, _) => match terminal {
228                SyntaxKind::Langtag => 1isize,
229                SyntaxKind::Datatype => 1isize,
230                SyntaxKind::Stop => 1isize,
231                _ => 0,
232            },
233            (SyntaxKind::Object, 2usize, _) => match terminal {
234                SyntaxKind::Iriref => 0,
235                _ => 1isize,
236            },
237            (SyntaxKind::Object, 3usize, _) => match terminal {
238                SyntaxKind::BlankNodeLabel => 0,
239                _ => 1isize,
240            },
241            (SyntaxKind::Object, 4usize, _) => match terminal {
242                SyntaxKind::StringLiteralQuote => 0,
243                _ => 1isize,
244            },
245            (SyntaxKind::Predicate, 1usize, _) => match terminal {
246                SyntaxKind::Iriref => 0,
247                _ => 1isize,
248            },
249            (SyntaxKind::Subject, 1usize, _) => match terminal {
250                SyntaxKind::BlankNodeLabel | SyntaxKind::Iriref => 0,
251                _ => 1isize,
252            },
253            (SyntaxKind::Subject, 2usize, _) => match terminal {
254                SyntaxKind::Iriref => 0,
255                _ => 1isize,
256            },
257            (SyntaxKind::Subject, 3usize, _) => match terminal {
258                SyntaxKind::BlankNodeLabel => 0,
259                _ => 1isize,
260            },
261            (SyntaxKind::Triple, 1usize, _) => match terminal {
262                SyntaxKind::Stop => 0,
263                _ => 8isize,
264            },
265            (SyntaxKind::Triple, 2usize, _) => match terminal {
266                SyntaxKind::Langtag => 1isize,
267                SyntaxKind::Datatype => 1isize,
268                SyntaxKind::Stop => 1isize,
269                _ => 0,
270            },
271            (SyntaxKind::Triple, 3usize, _) => match terminal {
272                SyntaxKind::Iriref => 0,
273                _ => 1isize,
274            },
275            (SyntaxKind::Triple, 4usize, _) => match terminal {
276                SyntaxKind::BlankNodeLabel | SyntaxKind::Iriref => 0,
277                _ => 1isize,
278            },
279            _ => 0,
280        }
281    }
282    impl crate::a_star::ParserTrait for Rule {
283        type Kind = SyntaxKind;
284        fn step(
285            &self,
286            element: &crate::a_star::Element<Self>,
287            state: &mut crate::a_star::AStar<Self>,
288        ) {
289            match (self.kind, self.state) {
290                (SyntaxKind::NtriplesDoc, 0usize) => {
291                    if let Some(parent) = element.pop() {
292                        state.add_element(parent);
293                    }
294                }
295                (SyntaxKind::NtriplesDoc, 1usize) => {
296                    state.add_element(element.pop_push(Rule {
297                        kind: self.kind,
298                        state: 2usize,
299                    }));
300                    state.add_element(element.pop_push(Rule {
301                        kind: self.kind,
302                        state: 0usize,
303                    }));
304                }
305                (SyntaxKind::NtriplesDoc, 2usize) => {
306                    state.add_element_checked(
307                        element
308                            .pop_push(Rule {
309                                kind: self.kind,
310                                state: 1usize,
311                            })
312                            .push(Rule {
313                                kind: SyntaxKind::Triple,
314                                state: 4usize,
315                            }),
316                        SyntaxKind::Triple,
317                    );
318                }
319                (SyntaxKind::Triple, 0usize) => {
320                    if let Some(parent) = element.pop() {
321                        state.add_element(parent);
322                    }
323                }
324                (SyntaxKind::Triple, 1usize) => {
325                    let (matched, fb) = state.expect_as_inline(element, SyntaxKind::Stop);
326                    state.add_element(matched.pop_push(Rule {
327                        kind: self.kind,
328                        state: 0usize,
329                    }));
330                    if let Some(fb) = fb {
331                        state.add_element(fb.pop_push(Rule {
332                            kind: self.kind,
333                            state: 0usize,
334                        }));
335                    }
336                }
337                (SyntaxKind::Triple, 2usize) => {
338                    state.add_element_checked(
339                        element
340                            .pop_push(Rule {
341                                kind: self.kind,
342                                state: 1usize,
343                            })
344                            .push(Rule {
345                                kind: SyntaxKind::Object,
346                                state: 1usize,
347                            }),
348                        SyntaxKind::Object,
349                    );
350                }
351                (SyntaxKind::Triple, 3usize) => {
352                    state.add_element_checked(
353                        element
354                            .pop_push(Rule {
355                                kind: self.kind,
356                                state: 2usize,
357                            })
358                            .push(Rule {
359                                kind: SyntaxKind::Predicate,
360                                state: 1usize,
361                            }),
362                        SyntaxKind::Predicate,
363                    );
364                }
365                (SyntaxKind::Triple, 4usize) => {
366                    state.add_element_checked(
367                        element
368                            .pop_push(Rule {
369                                kind: self.kind,
370                                state: 3usize,
371                            })
372                            .push(Rule {
373                                kind: SyntaxKind::Subject,
374                                state: 1usize,
375                            }),
376                        SyntaxKind::Subject,
377                    );
378                }
379                (SyntaxKind::Subject, 0usize) => {
380                    if let Some(parent) = element.pop() {
381                        state.add_element(parent);
382                    }
383                }
384                (SyntaxKind::Subject, 1usize) => {
385                    state.add_element_checked(
386                        element.pop_push(Rule {
387                            kind: self.kind,
388                            state: 2usize,
389                        }),
390                        SyntaxKind::Iriref,
391                    );
392                    state.add_element_checked(
393                        element.pop_push(Rule {
394                            kind: self.kind,
395                            state: 3usize,
396                        }),
397                        SyntaxKind::BlankNodeLabel,
398                    );
399                }
400                (SyntaxKind::Subject, 2usize) => {
401                    let (matched, fb) = state.expect_as_inline(element, SyntaxKind::Iriref);
402                    state.add_element(matched.pop_push(Rule {
403                        kind: self.kind,
404                        state: 0usize,
405                    }));
406                    if let Some(fb) = fb {
407                        state.add_element(fb.pop_push(Rule {
408                            kind: self.kind,
409                            state: 0usize,
410                        }));
411                    }
412                }
413                (SyntaxKind::Subject, 3usize) => {
414                    let (matched, fb) = state.expect_as_inline(element, SyntaxKind::BlankNodeLabel);
415                    state.add_element(matched.pop_push(Rule {
416                        kind: self.kind,
417                        state: 0usize,
418                    }));
419                    if let Some(fb) = fb {
420                        state.add_element(fb.pop_push(Rule {
421                            kind: self.kind,
422                            state: 0usize,
423                        }));
424                    }
425                }
426                (SyntaxKind::Predicate, 0usize) => {
427                    if let Some(parent) = element.pop() {
428                        state.add_element(parent);
429                    }
430                }
431                (SyntaxKind::Predicate, 1usize) => {
432                    let (matched, fb) = state.expect_as_inline(element, SyntaxKind::Iriref);
433                    state.add_element(matched.pop_push(Rule {
434                        kind: self.kind,
435                        state: 0usize,
436                    }));
437                    if let Some(fb) = fb {
438                        state.add_element(fb.pop_push(Rule {
439                            kind: self.kind,
440                            state: 0usize,
441                        }));
442                    }
443                }
444                (SyntaxKind::Object, 0usize) => {
445                    if let Some(parent) = element.pop() {
446                        state.add_element(parent);
447                    }
448                }
449                (SyntaxKind::Object, 1usize) => {
450                    state.add_element_checked(
451                        element.pop_push(Rule {
452                            kind: self.kind,
453                            state: 2usize,
454                        }),
455                        SyntaxKind::Iriref,
456                    );
457                    state.add_element_checked(
458                        element.pop_push(Rule {
459                            kind: self.kind,
460                            state: 3usize,
461                        }),
462                        SyntaxKind::BlankNodeLabel,
463                    );
464                    state.add_element(element.pop_push(Rule {
465                        kind: self.kind,
466                        state: 4usize,
467                    }));
468                }
469                (SyntaxKind::Object, 2usize) => {
470                    let (matched, fb) = state.expect_as_inline(element, SyntaxKind::Iriref);
471                    state.add_element(matched.pop_push(Rule {
472                        kind: self.kind,
473                        state: 0usize,
474                    }));
475                    if let Some(fb) = fb {
476                        state.add_element(fb.pop_push(Rule {
477                            kind: self.kind,
478                            state: 0usize,
479                        }));
480                    }
481                }
482                (SyntaxKind::Object, 3usize) => {
483                    let (matched, fb) = state.expect_as_inline(element, SyntaxKind::BlankNodeLabel);
484                    state.add_element(matched.pop_push(Rule {
485                        kind: self.kind,
486                        state: 0usize,
487                    }));
488                    if let Some(fb) = fb {
489                        state.add_element(fb.pop_push(Rule {
490                            kind: self.kind,
491                            state: 0usize,
492                        }));
493                    }
494                }
495                (SyntaxKind::Object, 4usize) => {
496                    state.add_element_checked(
497                        element
498                            .pop_push(Rule {
499                                kind: self.kind,
500                                state: 0usize,
501                            })
502                            .push(Rule {
503                                kind: SyntaxKind::Literal,
504                                state: 6usize,
505                            }),
506                        SyntaxKind::Literal,
507                    );
508                }
509                (SyntaxKind::Literal, 0usize) => {
510                    if let Some(parent) = element.pop() {
511                        state.add_element(parent);
512                    }
513                }
514                (SyntaxKind::Literal, 1usize) => {
515                    state.add_element(element.pop_push(Rule {
516                        kind: self.kind,
517                        state: 0usize,
518                    }));
519                    state.add_element(element.pop_push(Rule {
520                        kind: self.kind,
521                        state: 2usize,
522                    }));
523                }
524                (SyntaxKind::Literal, 2usize) => {
525                    state.add_element(element.pop_push(Rule {
526                        kind: self.kind,
527                        state: 4usize,
528                    }));
529                    state.add_element_checked(
530                        element.pop_push(Rule {
531                            kind: self.kind,
532                            state: 5usize,
533                        }),
534                        SyntaxKind::Langtag,
535                    );
536                }
537                (SyntaxKind::Literal, 3usize) => {
538                    let (matched, fb) = state.expect_as_inline(element, SyntaxKind::Iriref);
539                    state.add_element(matched.pop_push(Rule {
540                        kind: self.kind,
541                        state: 0usize,
542                    }));
543                    if let Some(fb) = fb {
544                        state.add_element(fb.pop_push(Rule {
545                            kind: self.kind,
546                            state: 0usize,
547                        }));
548                    }
549                }
550                (SyntaxKind::Literal, 4usize) => {
551                    let (matched, fb) = state.expect_as_inline(element, SyntaxKind::Datatype);
552                    state.add_element(matched.pop_push(Rule {
553                        kind: self.kind,
554                        state: 3usize,
555                    }));
556                    if let Some(fb) = fb {
557                        state.add_element(fb.pop_push(Rule {
558                            kind: self.kind,
559                            state: 3usize,
560                        }));
561                    }
562                }
563                (SyntaxKind::Literal, 5usize) => {
564                    let (matched, fb) = state.expect_as_inline(element, SyntaxKind::Langtag);
565                    state.add_element(matched.pop_push(Rule {
566                        kind: self.kind,
567                        state: 0usize,
568                    }));
569                    if let Some(fb) = fb {
570                        state.add_element(fb.pop_push(Rule {
571                            kind: self.kind,
572                            state: 0usize,
573                        }));
574                    }
575                }
576                (SyntaxKind::Literal, 6usize) => {
577                    let (matched, fb) =
578                        state.expect_as_inline(element, SyntaxKind::StringLiteralQuote);
579                    state.add_element(matched.pop_push(Rule {
580                        kind: self.kind,
581                        state: 1usize,
582                    }));
583                    if let Some(fb) = fb {
584                        state.add_element(fb.pop_push(Rule {
585                            kind: self.kind,
586                            state: 1usize,
587                        }));
588                    }
589                }
590                (SyntaxKind::Stop, _) => {
591                    let added = state.expect_as(element, SyntaxKind::Stop);
592                    if let Some(parent) = added.pop() {
593                        state.add_element(parent);
594                    }
595                }
596                (SyntaxKind::Datatype, _) => {
597                    let added = state.expect_as(element, SyntaxKind::Datatype);
598                    if let Some(parent) = added.pop() {
599                        state.add_element(parent);
600                    }
601                }
602                (SyntaxKind::BlankNodeLabel, _) => {
603                    let added = state.expect_as(element, SyntaxKind::BlankNodeLabel);
604                    if let Some(parent) = added.pop() {
605                        state.add_element(parent);
606                    }
607                }
608                (SyntaxKind::Iriref, _) => {
609                    let added = state.expect_as(element, SyntaxKind::Iriref);
610                    if let Some(parent) = added.pop() {
611                        state.add_element(parent);
612                    }
613                }
614                (SyntaxKind::Langtag, _) => {
615                    let added = state.expect_as(element, SyntaxKind::Langtag);
616                    if let Some(parent) = added.pop() {
617                        state.add_element(parent);
618                    }
619                }
620                (SyntaxKind::StringLiteralQuote, _) => {
621                    let added = state.expect_as(element, SyntaxKind::StringLiteralQuote);
622                    if let Some(parent) = added.pop() {
623                        state.add_element(parent);
624                    }
625                }
626                _ => panic!("Aaaah unexpected state {:?}", self),
627            }
628        }
629        fn at(&self) -> usize {
630            self.state
631        }
632        fn element_kind(&self) -> SyntaxKind {
633            self.kind
634        }
635        fn state_dist(&self, terminal: &SyntaxKind) -> isize {
636            state_dist(self.kind, self.state, *terminal)
637        }
638    }
639}
640pub use definitions::*;
641impl TokenTrait for SyntaxKind {
642    const ERROR: Self = SyntaxKind::Error;
643    const ROOT: Self = SyntaxKind::ROOT;
644    fn branch(&self) -> u32 {
645        *self as u32
646    }
647    fn skips(&self) -> bool {
648        match self {
649            SyntaxKind::WhiteSpace => true,
650            SyntaxKind::Error => true,
651            SyntaxKind::Comment => true,
652            _ => false,
653        }
654    }
655    fn starting_tokens(&self) -> &'static [SyntaxKind] {
656        &[]
657    }
658    fn min_error_for_token(&self, tok: &SyntaxKind) -> isize {
659        min_error_for_token(*self, *tok)
660    }
661    fn ending_tokens(&self) -> &'static [SyntaxKind] {
662        &[]
663    }
664    fn max_error_value(&self) -> isize {
665        match self {
666            SyntaxKind::Stop => 8isize,
667            _ => 1isize,
668        }
669    }
670    fn bracket_delta(&self) -> i8 {
671        match self {
672            _ => 0,
673        }
674    }
675    fn min_completion_cost(&self) -> isize {
676        match self {
677            SyntaxKind::Triple => 11isize,
678            _ => Self::max_error_value(self),
679        }
680    }
681    fn deletion_cost(&self) -> isize {
682        match self {
683            _ => Self::max_error_value(self) * 5,
684        }
685    }
686}
687pub mod format {
688    use super::{SyntaxKind, SyntaxNode};
689    use crate::format::Doc;
690    use crate::TokenTrait;
691    use rowan::NodeOrToken;
692    #[derive(Default)]
693    struct Hints {
694        space: bool,
695        line: bool,
696        hardline: bool,
697        blankline: bool,
698        indent: bool,
699        dedent: bool,
700    }
701    impl Hints {
702        fn to_docs(&self) -> Vec<Doc> {
703            let mut v = Vec::new();
704            if self.indent {
705                v.push(Doc::nil());
706            }
707            if self.dedent {
708                v.push(Doc::nil());
709            }
710            if self.blankline {
711                v.push(Doc::HardLine);
712                v.push(Doc::HardLine);
713            } else if self.hardline {
714                v.push(Doc::HardLine);
715            } else if self.line {
716                v.push(Doc::Line);
717            } else if self.space {
718                v.push(Doc::text(" "));
719            }
720            v
721        }
722    }
723    fn format_hints(parent: SyntaxKind, token: SyntaxKind) -> (Hints, Hints) {
724        match (parent, token) {
725            (_, SyntaxKind::Stop) => (
726                Hints {
727                    space: false,
728                    line: false,
729                    hardline: false,
730                    blankline: false,
731                    indent: false,
732                    dedent: false,
733                },
734                Hints {
735                    space: false,
736                    line: false,
737                    hardline: true,
738                    blankline: false,
739                    indent: false,
740                    dedent: false,
741                },
742            ),
743            _ => (Hints::default(), Hints::default()),
744        }
745    }
746    fn is_group(kind: SyntaxKind) -> bool {
747        false
748    }
749    pub fn to_doc(node: &SyntaxNode) -> Doc {
750        let mut parts: Vec<Vec<Doc>> = vec![vec![]];
751        for child in node.children_with_tokens() {
752            match child {
753                NodeOrToken::Token(t) => {
754                    if t.kind() == SyntaxKind::WhiteSpace {
755                        continue;
756                    }
757                    if t.kind() == SyntaxKind::Comment {
758                        parts
759                            .last_mut()
760                            .unwrap()
761                            .push(Doc::text(t.text().to_string()));
762                        continue;
763                    }
764                    let (before, after) = format_hints(node.kind(), t.kind());
765                    if before.dedent && parts.len() > 1 {
766                        let nested = parts.pop().unwrap_or_default();
767                        let indent_doc = Doc::nest(2, Doc::concat(nested));
768                        parts.last_mut().unwrap().push(indent_doc);
769                    }
770                    parts.last_mut().unwrap().extend(
771                        before
772                            .to_docs()
773                            .into_iter()
774                            .filter(|d| !matches!(d, Doc::Nil)),
775                    );
776                    parts
777                        .last_mut()
778                        .unwrap()
779                        .push(Doc::text(t.text().to_string()));
780                    let after_line: Vec<Doc> = after
781                        .to_docs()
782                        .into_iter()
783                        .filter(|d| !matches!(d, Doc::Nil))
784                        .collect();
785                    if after.indent {
786                        parts.push(after_line);
787                    } else {
788                        parts.last_mut().unwrap().extend(after_line);
789                    }
790                }
791                NodeOrToken::Node(n) => {
792                    if n.kind() == SyntaxKind::ERROR {
793                        parts
794                            .last_mut()
795                            .unwrap()
796                            .push(Doc::text(n.text().to_string()));
797                        continue;
798                    }
799                    let is_terminal_wrapper = {
800                        let mut it = n.children_with_tokens().filter(|c| match c {
801                            NodeOrToken::Token(t) => t.kind() != SyntaxKind::WhiteSpace,
802                            NodeOrToken::Node(_) => true,
803                        });
804                        matches!(it.next(), Some(NodeOrToken::Token(_))) && it.next().is_none()
805                    };
806                    if is_terminal_wrapper {
807                        let token_text = n.text().to_string();
808                        let (before, after) = format_hints(node.kind(), n.kind());
809                        if before.dedent && parts.len() > 1 {
810                            let nested = parts.pop().unwrap_or_default();
811                            let indent_doc = Doc::nest(2, Doc::concat(nested));
812                            parts.last_mut().unwrap().push(indent_doc);
813                        }
814                        parts.last_mut().unwrap().extend(
815                            before
816                                .to_docs()
817                                .into_iter()
818                                .filter(|d| !matches!(d, Doc::Nil)),
819                        );
820                        parts.last_mut().unwrap().push(Doc::text(token_text));
821                        let after_line: Vec<Doc> = after
822                            .to_docs()
823                            .into_iter()
824                            .filter(|d| !matches!(d, Doc::Nil))
825                            .collect();
826                        if after.indent {
827                            parts.push(after_line);
828                        } else {
829                            parts.last_mut().unwrap().extend(after_line);
830                        }
831                    } else {
832                        let (before, after) = format_hints(node.kind(), n.kind());
833                        if before.dedent && parts.len() > 1 {
834                            let nested = parts.pop().unwrap_or_default();
835                            let indent_doc = Doc::nest(2, Doc::concat(nested));
836                            parts.last_mut().unwrap().push(indent_doc);
837                        }
838                        parts.last_mut().unwrap().extend(
839                            before
840                                .to_docs()
841                                .into_iter()
842                                .filter(|d| !matches!(d, Doc::Nil)),
843                        );
844                        let child_doc = to_doc(&n);
845                        let child_doc = if is_group(n.kind()) {
846                            Doc::group(child_doc)
847                        } else {
848                            child_doc
849                        };
850                        parts.last_mut().unwrap().push(child_doc);
851                        let after_line: Vec<Doc> = after
852                            .to_docs()
853                            .into_iter()
854                            .filter(|d| !matches!(d, Doc::Nil))
855                            .collect();
856                        if after.indent {
857                            parts.push(after_line);
858                        } else {
859                            parts.last_mut().unwrap().extend(after_line);
860                        }
861                    }
862                }
863            }
864        }
865        while parts.len() > 1 {
866            let nested = parts.pop().unwrap();
867            parts
868                .last_mut()
869                .unwrap()
870                .push(Doc::nest(2, Doc::concat(nested)));
871        }
872        Doc::concat(parts.pop().unwrap_or_default())
873    }
874    pub fn format(node: &SyntaxNode, width: usize) -> String {
875        let doc = to_doc(node);
876        let s = crate::format::render(&doc, width);
877        let s = s.trim_start_matches('\n').trim_end_matches('\n');
878        format!("{s}\n")
879    }
880}