tr_lang/token/
tokens.rs

1use serde::{Deserialize, Serialize};
2
3pub mod tokentypes {
4    use serde::{Deserialize, Serialize};
5
6    #[derive(Debug, Clone, Serialize, Deserialize)]
7    pub enum ParserTokenType {
8        Yazı { val: String },
9        Artı,
10        Eksi,
11        ArtıArtı,
12        EksiEksi,
13        Çarpı,
14        Bölü,
15        Modulo,
16        De,
17        Büyüktür,
18        Küçüktür,
19        BüyükEşittir,
20        KüçükEşittir,
21        Eşittir,
22        EşitDeğildir,
23        Değildir,
24        Kopya,
25        Takas,
26        Döndür,
27        Üst,
28        Ve,
29        Veya,
30        Girdi,
31        İkiNoktaNokta,
32        İkiNokta,
33        İken(Option<usize>),
34        Sayı { val: f64 },
35        Bool { val: bool },
36        İse(Option<usize>),
37        Yoksa(Option<usize>),
38        Son { tp: usize },
39        Identifier { id: String },
40        İşlev { sonloc: Option<usize> },
41        İşlevSonlandır { tp: Vec<usize> },
42        Koy,
43        Ver { tp: Option<usize> },
44        At,
45        Tipinde,
46        ParenL,
47        Hiç,
48        Blok,
49        BlokSonlandır,
50        InScopeParentL,
51        InScopeParentR,
52        EOF,
53    }
54
55    #[derive(Debug, Clone)]
56    pub enum LexerTokenType {
57        ParenL,
58        ParenR,
59        Comma,
60        İşlev,
61        Yazı,
62        Sayı,
63        De,
64        İken,
65        İse,
66        Yoksa,
67        Identifier,
68        Son,
69        Kopya,
70        Doğru,
71        Yanlış,
72        Artı,
73        ArtıArtı,
74        Eksi,
75        EksiEksi,
76        Çarpı,
77        Bölü,
78        Modulo,
79        Büyüktür,
80        Küçüktür,
81        İkiNoktaNokta,
82        İkiNokta,
83        BüyükEşittir,
84        KüçükEşittir,
85        Eşittir,
86        EşitDeğildir,
87        Değildir,
88        // Replaced with `İse` `Yoksa` and `Son`
89        // `İse` `Yoksa` ve `Son` ile değiştirildi
90        /*
91        Sor,
92        SorYoksa,
93        SorSon,
94        */
95        Takas,
96        Üst,
97        Döndür,
98        Ve,
99        Veya,
100        Girdi,
101        Koy,
102        At,
103        Ver,
104        Yükle,
105        Tipinde,
106        Hiç,
107        Blok,
108        InScopeParentL,
109        InScopeParentR,
110        EOF,
111    }
112}
113
114#[derive(Debug, Clone)]
115pub enum Precedence {
116    Precedence(usize),
117    Reserved,
118    ParenL,
119    ParenR,
120    Comma,
121    None,
122}
123
124#[derive(Debug, Clone)]
125pub struct LexerToken {
126    pub typ: tokentypes::LexerTokenType,
127    pub lexeme: String,
128    pub line: usize,
129    pub col: usize,
130    pub file: String,
131    pub precedence: Precedence,
132}
133
134impl LexerToken {
135    pub fn new(
136        typ: tokentypes::LexerTokenType,
137        lexeme: String,
138        line: usize,
139        col: usize,
140        file: String,
141        precedence: Precedence,
142    ) -> Self {
143        Self {
144            typ,
145            lexeme,
146            line,
147            col,
148            file,
149            precedence,
150        }
151    }
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize)]
155pub struct ParserToken {
156    pub typ: tokentypes::ParserTokenType,
157    pub line: usize,
158    pub col: usize,
159    pub file: String,
160}
161
162impl ParserToken {
163    pub fn new(typ: tokentypes::ParserTokenType, line: usize, col: usize, file: String) -> Self {
164        Self {
165            typ,
166            line,
167            col,
168            file,
169        }
170    }
171
172    pub fn repr(&self) -> String {
173        use tokentypes::ParserTokenType as TokTyp;
174        match self.typ {
175            TokTyp::Artı => "+".to_string(),
176            TokTyp::ArtıArtı => "++".to_string(),
177            TokTyp::At => "at".to_string(),
178            TokTyp::Bool { val } => match val {
179                true => "doğru".to_string(),
180                false => "yanlış".to_string(),
181            },
182            TokTyp::Bölü => "/".to_string(),
183            TokTyp::BüyükEşittir => ">=".to_string(),
184            TokTyp::Büyüktür => ">".to_string(),
185            TokTyp::De => "de".to_string(),
186            TokTyp::Değildir => "!".to_string(),
187            TokTyp::Döndür => "dön".to_string(),
188            TokTyp::Eksi => "-".to_string(),
189            TokTyp::EksiEksi => "--".to_string(),
190            TokTyp::EOF => "EOF".to_string(),
191            TokTyp::EşitDeğildir => "!=".to_string(),
192            TokTyp::Eşittir => "=".to_string(),
193            TokTyp::Girdi => "girdi".to_string(),
194            TokTyp::Identifier { ref id } => id.clone(),
195            TokTyp::İken(_) => "iken".to_string(),
196            TokTyp::İkiNoktaNokta => ":.".to_string(),
197            TokTyp::İse(_) => "ise".to_string(),
198            TokTyp::İşlev { .. } => "işlev".to_string(),
199            TokTyp::İşlevSonlandır { .. } => "son".to_string(),
200            TokTyp::Kopya => "kpy".to_string(),
201            TokTyp::Koy => "->".to_string(),
202            TokTyp::KüçükEşittir => "<=".to_string(),
203            TokTyp::Küçüktür => "<".to_string(),
204            TokTyp::Modulo => "%".to_string(),
205            TokTyp::Sayı { val } => match val.fract() == 0.0 {
206                true => format!("{:.0?}", val),
207                false => format!("{:?}", val),
208            },
209            TokTyp::Son { .. } => "son".to_string(),
210            TokTyp::Takas => "tks".to_string(),
211            TokTyp::Tipinde => "@".to_string(),
212            TokTyp::Ve => "ve".to_string(),
213            TokTyp::Veya => "veya".to_string(),
214            TokTyp::Yazı { ref val } => format!("{:?}", val),
215            TokTyp::Yoksa(_) => "yoksa".to_string(),
216            TokTyp::Çarpı => "*".to_string(),
217            TokTyp::Üst => "üst".to_string(),
218            TokTyp::Ver { .. } => "ver".to_string(),
219            TokTyp::ParenL => "(".to_string(),
220            TokTyp::Hiç => "hiç".to_string(),
221            TokTyp::Blok => "blok".to_string(),
222            TokTyp::BlokSonlandır => "son".to_string(),
223            TokTyp::İkiNokta => ":".to_string(),
224            TokTyp::InScopeParentL => "(".to_string(),
225            TokTyp::InScopeParentR => ")".to_string(),
226        }
227    }
228}