boseiju/lexer/
tokens.rs

1pub mod intermediates;
2
3use crate::ability_tree::object;
4use crate::ability_tree::terminals;
5use crate::ability_tree::time;
6use crate::ability_tree::zone;
7use crate::lexer::span::Span;
8
9pub trait IntoToken: Sized {
10    fn try_from_span(span: &crate::lexer::Span) -> Option<Self>;
11}
12
13#[derive(idris_derive::Idris)]
14#[idris(repr = usize)]
15#[derive(serde::Serialize, serde::Deserialize)]
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub enum Token {
18    AbilityWord(terminals::AbilityWord),
19    ActionKeyword(intermediates::ActionKeyword),
20    AmbiguousToken(intermediates::AmbiguousToken),
21    AnyNumberOfClause { clauses: intermediates::AnyNumberOfClause },
22    BackwardDuration(time::BackwardDuration),
23    CardActions(intermediates::CardActions),
24    Choice(intermediates::Choice),
25    ChoiceReference(intermediates::ChoiceReference),
26    Color(terminals::Color),
27    ControlFlow(intermediates::ControlFlow),
28    CountSpecifier(intermediates::CountSpecifier),
29    Counter(terminals::Counter),
30    DamageKind(intermediates::DamageKind),
31    EnglishKeyword(intermediates::EnglishKeyword),
32    ForwardDuration(time::ForwardDuration),
33    GlobalZone(intermediates::GlobalZone),
34    InAdditionToPayingItsOtherCost(intermediates::InAdditionToPayingItsOtherCost),
35    Instant(time::Instant),
36    KeywordAbility(intermediates::KeywordAbility),
37    KeywordAction(terminals::KeywordAction),
38    Mana { mana: terminals::Mana },
39    NamedToken(terminals::NamedToken),
40    NonKind(intermediates::NonKind),
41    NotOfAKind { not: intermediates::NotOfAKind },
42    Number(intermediates::Number),
43    NumberOfTimes(intermediates::NumberOfTimes),
44    NumberOperation(intermediates::NumberOperation),
45    ObjectKind(object::ObjectKind),
46    Order(terminals::Order),
47    OwnableZone(zone::OwnableZone),
48    OwnerSpecifier(terminals::OwnerSpecifier),
49    PermanentProperty(terminals::CardProperty),
50    PermanentState(terminals::CardState),
51    Phase(terminals::Phase),
52    PlayerAction(intermediates::PlayerAction),
53    PlayerProperties(intermediates::PlayerProperties),
54    PlayerSpecifier(intermediates::PlayerSpecifier),
55    PowerToughnessModElements(intermediates::PowerToughnessModElements),
56    PowerToughness { pt: terminals::PowerToughness },
57    SagaChapterNumber { chapter: terminals::SagaChapterNumber },
58    SelfReferencing { reference: object::SelfReferencingObject },
59    SpellProperty(terminals::SpellProperty),
60    Step(terminals::Step),
61    TapUntapCost(intermediates::TapUntapCost),
62    UnderControl(intermediates::UnderControl),
63    VhyToSortLater(intermediates::VhyToSortLater),
64    WinLoseClause(intermediates::WinLoseClause),
65}
66
67impl Token {
68    pub fn try_from_span(span: Span) -> Option<Token> {
69        if let Some(kind) = intermediates::AmbiguousToken::try_from_span(&span) {
70            Some(Self::AmbiguousToken(kind))
71        } else if let Some(kind) = terminals::Counter::try_from_span(&span) {
72            Some(Self::Counter(kind))
73        } else if let Some(kind) = intermediates::CountSpecifier::try_from_span(&span) {
74            Some(Self::CountSpecifier(kind))
75        } else if let Some(kind) = terminals::OwnerSpecifier::try_from_span(&span) {
76            Some(Self::OwnerSpecifier(kind))
77        } else if let Some(kind) = terminals::Order::try_from_span(&span) {
78            Some(Self::Order(kind))
79        } else if let Some(kind) = intermediates::CardActions::try_from_span(&span) {
80            Some(Self::CardActions(kind))
81        } else if let Some(kind) = intermediates::PlayerSpecifier::try_from_span(&span) {
82            Some(Self::PlayerSpecifier(kind))
83        } else if let Some(kind) = terminals::CardState::try_from_span(&span) {
84            Some(Self::PermanentState(kind))
85        } else if let Some(kind) = terminals::CardProperty::try_from_span(&span) {
86            Some(Self::PermanentProperty(kind))
87        } else if let Some(kind) = terminals::SpellProperty::try_from_span(&span) {
88            Some(Self::SpellProperty(kind))
89        } else if let Some(kind) = terminals::Phase::try_from_span(&span) {
90            Some(Self::Phase(kind))
91        } else if let Some(kind) = terminals::Step::try_from_span(&span) {
92            Some(Self::Step(kind))
93        } else if let Some(pt) = terminals::PowerToughness::try_from_span(&span) {
94            Some(Self::PowerToughness { pt })
95        } else if let Some(kind) = intermediates::PowerToughnessModElements::try_from_span(&span) {
96            Some(Self::PowerToughnessModElements(kind))
97        } else if let Some(chapter) = terminals::SagaChapterNumber::try_from_span(&span) {
98            Some(Self::SagaChapterNumber { chapter })
99        } else if let Some(kind) = intermediates::InAdditionToPayingItsOtherCost::try_from_span(&span) {
100            Some(Self::InAdditionToPayingItsOtherCost(kind))
101        } else if let Some(kind) = crate::ability_tree::time::Instant::try_from_span(&span) {
102            Some(Self::Instant(kind))
103        } else if let Some(kind) = crate::ability_tree::time::ForwardDuration::try_from_span(&span) {
104            Some(Self::ForwardDuration(kind))
105        } else if let Some(kind) = crate::ability_tree::time::BackwardDuration::try_from_span(&span) {
106            Some(Self::BackwardDuration(kind))
107        } else if let Some(kind) = terminals::NamedToken::try_from_span(&span) {
108            Some(Self::NamedToken(kind))
109        } else if let Some(kind) = zone::OwnableZone::try_from_span(&span) {
110            Some(Self::OwnableZone(kind))
111        } else if let Some(kind) = terminals::Color::try_from_span(&span) {
112            Some(Self::Color(kind))
113        } else if let Some(kind) = terminals::AbilityWord::try_from_span(&span) {
114            Some(Self::AbilityWord(kind))
115        } else if let Some(kind) = intermediates::KeywordAbility::try_from_span(&span) {
116            Some(Self::KeywordAbility(kind))
117        } else if let Some(kind) = terminals::KeywordAction::try_from_span(&span) {
118            Some(Self::KeywordAction(kind))
119        } else if let Some(mana) = terminals::Mana::try_from_span(&span) {
120            Some(Self::Mana { mana })
121        } else if let Some(kind) = object::ObjectKind::try_from_span(&span) {
122            Some(Self::ObjectKind(kind))
123        } else if let Some(kind) = intermediates::ControlFlow::try_from_span(&span) {
124            Some(Self::ControlFlow(kind))
125        } else if let Some(kind) = intermediates::TapUntapCost::try_from_span(&span) {
126            Some(Self::TapUntapCost(kind))
127        } else if let Some(kind) = intermediates::EnglishKeyword::try_from_span(&span) {
128            Some(Self::EnglishKeyword(kind))
129        } else if let Some(reference) = object::SelfReferencingObject::try_from_span(&span) {
130            Some(Self::SelfReferencing { reference })
131        } else if let Some(kind) = intermediates::Number::try_from_span(&span) {
132            Some(Self::Number(kind))
133        } else if let Some(not) = intermediates::NotOfAKind::try_from_span(&span) {
134            Some(Self::NotOfAKind { not })
135        } else if let Some(kind) = intermediates::ActionKeyword::try_from_span(&span) {
136            Some(Self::ActionKeyword(kind))
137        } else if let Some(kind) = intermediates::DamageKind::try_from_span(&span) {
138            Some(Self::DamageKind(kind))
139        } else if let Some(kind) = intermediates::PlayerAction::try_from_span(&span) {
140            Some(Self::PlayerAction(kind))
141        } else if let Some(kind) = intermediates::NonKind::try_from_span(&span) {
142            Some(Self::NonKind(kind))
143        } else if let Some(kind) = intermediates::UnderControl::try_from_span(&span) {
144            Some(Self::UnderControl(kind))
145        } else if let Some(kind) = intermediates::PlayerProperties::try_from_span(&span) {
146            Some(Self::PlayerProperties(kind))
147        } else if let Some(kind) = intermediates::NumberOfTimes::try_from_span(&span) {
148            Some(Self::NumberOfTimes(kind))
149        } else if let Some(kind) = intermediates::NumberOperation::try_from_span(&span) {
150            Some(Self::NumberOperation(kind))
151        } else if let Some(kind) = intermediates::ChoiceReference::try_from_span(&span) {
152            Some(Self::ChoiceReference(kind))
153        } else if let Some(kind) = intermediates::Choice::try_from_span(&span) {
154            Some(Self::Choice(kind))
155        } else if let Some(clauses) = intermediates::AnyNumberOfClause::try_from_span(&span) {
156            Some(Self::AnyNumberOfClause { clauses })
157        } else if let Some(kind) = intermediates::WinLoseClause::try_from_span(&span) {
158            Some(Self::WinLoseClause(kind))
159        } else if let Some(kind) = intermediates::GlobalZone::try_from_span(&span) {
160            Some(Self::GlobalZone(kind))
161        } else if let Some(kind) = intermediates::VhyToSortLater::try_from_span(&span) {
162            Some(Self::VhyToSortLater(kind))
163        } else {
164            None
165        }
166    }
167
168    #[cfg(feature = "spanned_tree")]
169    pub fn span(&self) -> crate::ability_tree::span::TreeSpan {
170        use crate::ability_tree::AbilityTreeNode;
171        match self {
172            Self::AbilityWord(child) => child.span,
173            Self::ActionKeyword(child) => child.span(),
174            Self::AmbiguousToken(child) => child.span(),
175            Self::AnyNumberOfClause { clauses } => clauses.span,
176            Self::BackwardDuration(child) => child.node_span(),
177            Self::CardActions(child) => child.span(),
178            Self::Choice(child) => child.span(),
179            Self::ChoiceReference(child) => child.span(),
180            Self::Color(child) => child.span,
181            Self::ControlFlow(child) => child.span(),
182            Self::CountSpecifier(child) => child.span(),
183            Self::Counter(child) => child.span,
184            Self::DamageKind(child) => child.span(),
185            Self::EnglishKeyword(child) => child.span(),
186            Self::ForwardDuration(child) => child.node_span(),
187            Self::GlobalZone(child) => child.span(),
188            Self::InAdditionToPayingItsOtherCost(child) => child.span,
189            Self::Instant(child) => child.node_span(),
190            Self::KeywordAbility(child) => child.span,
191            Self::KeywordAction(child) => child.span,
192            Self::Mana { mana } => mana.node_span(),
193            Self::NamedToken(child) => child.node_span(),
194            Self::NonKind(child) => child.span(),
195            Self::NotOfAKind { not } => not.span,
196            Self::Number(child) => child.span(),
197            Self::NumberOfTimes(child) => child.span(),
198            Self::NumberOperation(child) => child.span(),
199            Self::ObjectKind(child) => child.node_span(),
200            Self::Order(child) => child.node_span(),
201            Self::OwnableZone(child) => child.node_span(),
202            Self::OwnerSpecifier(child) => child.node_span(),
203            Self::PermanentProperty(child) => child.span(),
204            Self::PermanentState(child) => child.span(),
205            Self::Phase(child) => child.span(),
206            Self::PlayerAction(child) => child.span(),
207            Self::PlayerProperties(child) => child.span(),
208            Self::PlayerSpecifier(child) => child.span(),
209            Self::PowerToughnessModElements(child) => child.span(),
210            Self::PowerToughness { pt } => pt.span,
211            Self::SagaChapterNumber { chapter } => chapter.span,
212            Self::SelfReferencing { reference } => reference.span,
213            Self::SpellProperty(child) => child.span(),
214            Self::Step(child) => child.span(),
215            Self::TapUntapCost(child) => child.span(),
216            Self::UnderControl(child) => child.span(),
217            Self::VhyToSortLater(child) => child.span(),
218            Self::WinLoseClause(child) => child.span(),
219        }
220    }
221}