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
use {
    std::{
        rc::Rc,
        fmt,
        ops::Deref,
        ops::DerefMut,
    },
    crate::{
        makepad_math::Vec4,
        makepad_live_tokenizer::{
            LiveId,
            Delim,
            FullToken
        },
        live_ptr::{LiveFileId},
        span::TextSpan
    }
};

#[derive(Clone, Debug, PartialEq)]
pub struct TokenWithSpan {
    pub span: TextSpan,
    pub token: LiveToken,
}

impl Deref for TokenWithSpan {
    type Target = LiveToken;
    fn deref(&self) -> &Self::Target {&self.token}
}

impl DerefMut for TokenWithSpan {
    fn deref_mut(&mut self) -> &mut Self::Target {&mut self.token}
}

impl fmt::Display for TokenWithSpan {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} ", self.token)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum LiveToken {
    Punct(LiveId),
    Ident(LiveId),
    
    Open(Delim),
    Close(Delim),
    
    String (Rc<String>),
    Bool(bool),
    Int(i64),
    Float(f64),
    Color(u32),
    
    Eof,
}

impl LiveToken {

    pub fn as_float(&self) -> Option<f64> {
        match self {
            Self::Float(v) => Some(*v),
            Self::Int(v) => Some(*v as f64),
            _ => None
        }
    }
    pub fn as_vec4(&self) -> Option<Vec4> {
        match self {
            Self::Color(c) => Some(Vec4::from_u32(*c)),
            _ => None
        }
    }    
    
    pub fn is_open(&self) -> bool {
        matches!(self, LiveToken::Open(_))
    }
    
    pub fn is_close(&self) -> bool {
        matches!(self, LiveToken::Close(_))
    }
    
    pub fn is_open_delim(&self, delim: Delim) -> bool {
        match self {
            LiveToken::Open(d) => *d == delim,
            _ => false
        }
    }
    
    pub fn is_close_delim(&self, delim: Delim) -> bool {
        match self {
            LiveToken::Close(d) => *d == delim,
            _ => false
        }
    }
    
    pub fn is_int(&self) -> bool {
        matches!(self, LiveToken::Int(_))
    }
    
    
    pub fn is_float(&self) -> bool {
        matches!(self, LiveToken::Float(_))
    }
    
        
    pub fn is_color(&self) -> bool {
        matches!(self, LiveToken::Color(_))
    }

    pub fn is_bool(&self) -> bool {
        matches!(self, LiveToken::Bool(_))
    }

    pub fn is_parsed_number(&self) -> bool {
        matches!(self, LiveToken::Int(_) | LiveToken::Float(_))
    }
    
    pub fn is_value_type(&self) -> bool {
        matches!(self, LiveToken::Color(_) | LiveToken::Bool(_) | LiveToken::Int(_) | LiveToken::Float(_))
    }
        
    pub fn is_ident(&self) -> bool {
        matches!(self, LiveToken::Ident(_))
    }
    
    pub fn is_punct(&self) -> bool {
        matches!(self, LiveToken::Punct(_))
    }
    
    pub fn is_punct_id(&self, id: LiveId) -> bool {
        match self {
            LiveToken::Punct(v) => *v == id,
            _ => false
        }
    }
    
    pub fn is_parse_equal(&self, other: &LiveToken) -> bool {
        match self {
            LiveToken::String(p) => if let LiveToken::String(o) = other {*p == *o}else {false},
            LiveToken::Punct(p) => if let LiveToken::Punct(o) = other {*p == *o}else {false},
            LiveToken::Ident(p) => if let LiveToken::Ident(o) = other {*p == *o}else {false},
            LiveToken::Open(p) => if let LiveToken::Open(o) = other {*p == *o}else {false},
            LiveToken::Close(p) => if let LiveToken::Close(o) = other {*p == *o}else {false},
            LiveToken::Bool(_) => matches!(other, LiveToken::Bool(_)),
            LiveToken::Int(_) => if let LiveToken::Int(_) = other {true} else { matches!(other, LiveToken::Float(_)) },
            LiveToken::Float(_) => if let LiveToken::Float(_) = other {true} else { matches!(other, LiveToken::Int(_)) },
            LiveToken::Color(_) => matches!(other, LiveToken::Color(_)),
            LiveToken::Eof => matches!(other, LiveToken::Eof),
        }
    }
    
    pub fn from_full_token(full_token: &FullToken) -> Option<Self> {
        match full_token {
            FullToken::String(s) => Some(LiveToken::String(s.clone())),
            FullToken::Punct(p) => Some(LiveToken::Punct(*p)),
            FullToken::Ident(p) => Some(LiveToken::Ident(*p)),
            FullToken::Open(p) => Some(LiveToken::Open(*p)),
            FullToken::Close(p) => Some(LiveToken::Close(*p)),
            FullToken::Bool(p) => Some(LiveToken::Bool(*p)),
            FullToken::Int(p) => Some(LiveToken::Int(*p)),
            FullToken::Float(p) => Some(LiveToken::Float(*p)),
            FullToken::Color(p) => Some(LiveToken::Color(*p)),
            _ => None
        }
    }
}

impl fmt::Display for LiveToken {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Eof => write!(f, "<eof>"),
            Self::String(v) => write!(f, "{}", v),
            Self::Punct(id) => write!(f, "{}", id),
            Self::Ident(id) => write!(f, "{}", id),
            Self::Open(Delim::Paren) => write!(f, "("),
            Self::Open(Delim::Brace) => write!(f, "{{"),
            Self::Open(Delim::Bracket) => write!(f, "["),
            Self::Close(Delim::Paren) => write!(f, ")"),
            Self::Close(Delim::Brace) => write!(f, "}}"),
            Self::Close(Delim::Bracket) => write!(f, "]"),
            Self::Bool(lit) => write!(f, "{}", lit),
            Self::Int(lit) => write!(f, "{}", lit),
            Self::Float(lit) => write!(f, "{}", lit),
            Self::Color(lit) => write!(f, "#{:x}", lit),
        }
    }
}

impl LiveTokenId {
    pub fn new(file_id: LiveFileId, token: usize) -> Self {
        let file_id = file_id.to_index();
        if file_id > 0x3fe || token > 0x3ffff {
            panic!();
        }
        LiveTokenId(
            (((file_id as u32 + 1) & 0x3ff) << 18) | ((token as u32) & 0x3ffff)
        )
    }
    
    pub fn is_empty(&self) -> bool {
        ((self.0 >> 18) & 0x3ff) == 0
    }
    
    pub fn token_index(&self) -> usize {
        (self.0 & 0x3ffff) as usize
    }
    
    pub fn file_id(&self) -> Option<LiveFileId> {
        let id = (self.0 >> 18) & 0x3ff;
        if id == 0{
            None
        }
        else{
            Some(LiveFileId((id - 1) as u16))
        }
    }
    
    pub fn to_bits(&self) -> u32 {self.0}
    pub fn from_bits(v: u32) -> Option<Self> {
        if (v & 0xf000_0000) != 0 {
            panic!();
        }
        if ((v >> 18) & 0x3ff) == 0 {
            None
        } else {
            Some(Self(v))
        }
    }
}

#[derive(Clone, Default, Copy, Eq, Ord, Hash, PartialOrd, PartialEq)]
pub struct LiveTokenId(u32);

impl fmt::Debug for LiveTokenId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "TokenId(token_index:{}, file_id:{})", self.token_index(), self.file_id().unwrap_or(LiveFileId::new(0)).to_index())
    }
}