1use crate::{
2 span::Span,
3 token::{Token, TokenKind},
4};
5
6#[derive(Debug, PartialEq, Clone)]
7pub struct Syntax<'src> {
8 pub span: Span,
9 pub rules: Vec<SyntaxRule<'src>>,
10}
11
12#[derive(Debug, PartialEq, Clone)]
13pub struct SyntaxRule<'src> {
14 pub span: Span,
15 pub name: &'src str,
16 pub definitions: Vec<SingleDefinition<'src>>,
17}
18
19#[derive(Debug, PartialEq, Clone)]
20pub struct SingleDefinition<'src> {
21 pub span: Span,
22 pub terms: Vec<SyntacticTerm<'src>>,
23}
24
25#[derive(Debug, PartialEq, Clone)]
26pub struct SyntacticTerm<'src> {
27 pub span: Span,
28 pub factor: SyntacticFactor<'src>,
29 pub exception: Option<SyntacticException<'src>>,
30}
31
32pub type SyntacticException<'src> = SyntacticFactor<'src>;
33
34#[derive(Debug, PartialEq, Clone)]
35pub struct SyntacticFactor<'src> {
36 pub span: Span,
37 pub repetition: Option<usize>,
38 pub primary: SyntacticPrimary<'src>,
39}
40
41#[derive(Debug, PartialEq, Clone)]
42pub struct SyntacticPrimary<'src> {
43 pub span: Span,
44 pub kind: SyntacticPrimaryKind<'src>,
45}
46
47#[derive(Debug, PartialEq, Clone)]
48pub enum SyntacticPrimaryKind<'src> {
49 OptionalSequence(Vec<SingleDefinition<'src>>),
50 RepeatedSequence(Vec<SingleDefinition<'src>>),
51 GroupedSequence(Vec<SingleDefinition<'src>>),
52 MetaIdentifier(&'src str),
53 TerminalString(&'src str),
54 SpecialSequence(&'src str),
55 EmptySequence,
56}
57
58#[derive(Debug, PartialEq, Eq, Clone)]
59pub struct Comment<'src> {
60 pub span: Span,
61 pub text: &'src str,
62}
63
64impl<'src> TryFrom<Token<'src>> for Comment<'src> {
65 type Error = &'static str;
66
67 fn try_from(value: Token<'src>) -> Result<Self, Self::Error> {
68 match value.kind {
69 TokenKind::Comment(text) => Ok(Comment {
70 span: value.span,
71 text,
72 }),
73 _ => Err("Comment node can only be constructed from Comment TokenKind"),
74 }
75 }
76}