tex_engine 0.0.1

A modular crate for building TeX engines
Documentation
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*!
Plain TeX parses source files as a sequence of [`u8`]s, but other engines might use bigger types, e.g. XeTeX.

The [`CharType`] trait allows us to abstract over the character type, and the trait [`TeXStr`]`<Char:`[`CharType`]`>`
abstracts over the string type.
*/

use std::convert::Into;
use std::fmt::{Display, Formatter, Debug};
use std::hash::Hash;
use std::marker::PhantomData;
use std::vec::IntoIter;
use crate::tex::catcodes::{CategoryCodeScheme, STARTING_SCHEME_U8};
use crate::utils::Ptr;



/**
Plain TeX parses source files as a sequence of [`u8`]s, but other engines might use bigger types, e.g. XeTeX.
This trait allows us to abstract over the character type, by providing the relevant data needed to treat them
(essentially) like [`u8`]s.
 */
pub trait CharType:Copy+PartialEq+Eq+Hash+Display+Debug+'static+From<u8>+Default {
    /// The type of the array/vec/whatever of all possible characters. For [`u8`], this is `[A;256]`.
    type Allchars<A:Default> : AllCharsTrait<Self,A>;

    /// The maximum value of the character type. For [`u8`], this is `255`.
    const MAX:Self;

    /// Parses a character from a byte iterator. For [`u8`], this is just `iter.next()`.
    fn from_u8_iter(iter:&mut IntoIter<u8>) -> Option<Self>;
    /// Convert a `&`[`str`] into a [`TeXStr`]`<Self>`.
    fn from_str(s:&str) -> TeXStr<Self>;

    /** Whether the character is an end-of-line character.
     *
     * Should return:
     * - `Some(true)`: is end of line (e.g. `\n`).
     * - `Some(false)`: is not end of line.
     * - `None`: might be, depending on the next character - e.g. `\r`, in which case the next
     *    character should be checked to be `\n`.
     */
    fn is_eol(self) -> Option<bool>;

    /** Should return:
     * - `true`: if the pair (`self`,`next`) represents an end of line (e.g. `\r\n`).
     * - `false`: if not; in which case `self` itself is considered to be an end of line (e.g. `\r`).
     */
    fn is_eol_pair(self,next:Self) -> bool;

    /// The string "par" as a [`TeXStr`]`<Self>`.
    fn par_token() -> TeXStr<Self>;

    /// The string "relax" as a [`TeXStr`]`<Self>`.
    fn relax_token() -> TeXStr<Self>;

    /// The empty string as a [`TeXStr`]`<Self>`.
    fn empty_str() -> TeXStr<Self>;

    /// The starting category code scheme for this character type, see [`struct@STARTING_SCHEME_U8`].
    fn starting_catcode_scheme() -> CategoryCodeScheme<Self>;

    fn newline() -> Self;
    fn carriage_return() -> Self;
    fn backslash() -> Self;
    fn zeros() -> Self::Allchars<Self>;
    fn ident() -> Self::Allchars<Self>;
    fn rep_field<A:Clone+Default>(a:A) -> Self::Allchars<A>;

    /// How to display a [`TeXStr`]`<Self>`.
    fn display_str(str:&TeXStr<Self>, f: &mut Formatter<'_>) -> std::fmt::Result {
        for u in &*str.0 { write!(f,"{}",u.char_str())?; }
        Ok(())
    }
    fn char_str(&self) -> String;
    fn as_bytes(&self) -> Vec<u8>;

    fn from_i64(i:i64) -> Option<Self>;
    fn to_usize(self) -> usize;
}

thread_local! {
    /// "par" as a [`TeXStr`]`<u8>`.
    pub static PAR_U8: TeXStr<u8> = "par".into();
    /// "relax" as a [`TeXStr`]`<u8>`.
    pub static RELAX_U8: TeXStr<u8> = "relax".into();
    /// The empty string as a [`TeXStr`]`<u8>`.
    pub static EMPTY_U8: TeXStr<u8> = "".into();
}

impl CharType for u8 {
    type Allchars<A:Default> = [A;256];
    const MAX:Self=255;
    fn from_u8_iter(iter: &mut IntoIter<u8>) -> Option<Self> { iter.next() }
    fn newline() -> Self { b'\n' }
    fn carriage_return() -> Self {b'\r'}
    fn backslash() -> Self { b'\\' }
    // #[inline(always)]
    fn is_eol(self) -> Option<bool> {
        match self {
            b'\n' => Some(true),
            b'\r' => None,
            _ => Some(false)
        }
    }
    fn from_str(s: &str) -> TeXStr<Self> {
        TeXStr(Ptr::new(s.as_bytes().to_vec()))
    }
    // #[inline(always)]
    fn is_eol_pair(self, next: Self) -> bool {
        // invariant: self == \r
        next == b'\n'
    }
    fn par_token() -> TeXStr<Self> { PAR_U8.with(|p| p.clone()) }
    fn relax_token() -> TeXStr<Self> { RELAX_U8.with(|p| p.clone()) }
    fn empty_str() -> TeXStr<Self> {EMPTY_U8.with(|p| p.clone()) }
    // #[inline(always)]
    fn starting_catcode_scheme() -> CategoryCodeScheme<Self> {
        STARTING_SCHEME_U8.clone()
    }
    fn as_bytes(&self) -> Vec<u8> { vec![*self] }

    // #[inline(always)]
    fn char_str(&self) -> String {
        match *self {
            0 => "\\u0000".to_string(),
            b'\n' => "\\n".to_string(),
            b'\r' => "\\r".to_string(),
            o if is_ascii(o) => (o as char).to_string(), //f.write_char((o).into()),
            o => format!("\\u00{:X}",o)
        }
    }
    fn zeros() -> Self::Allchars<Self> {
        [0;256]
    }
    fn ident() -> Self::Allchars<Self> {
        let mut a = [0;256];
        for i in 0..256 { a[i] = i as u8; }
        a
    }
    fn rep_field<A: Clone+Default>(a: A) -> Self::Allchars<A> {
        [ // UTTERLY RIDICULOUS
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),
            a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone(),a.clone()
        ]
    }

    fn from_i64(i: i64) -> Option<Self> {
        if i == -1 {Some(255)} else if i < 0 || i > 255 { None } else { Some(i as u8) }
    }
    fn to_usize(self) -> usize { self as usize }
}

/** A trait for arrays of all possible characters. For [`u8`], this is `[A;256]`.
*/
pub trait AllCharsTrait<C:CharType,A> {
    /// Returns the value for character `u`.
    fn get(&self, u: C) -> &A;
    /// Sets the value for character `u` to `v`.
    fn set(&mut self, u: C, v:A);
    /// Replaces the value for character `u` with `v`, returning the old value.
    fn replace(&mut self, u: C, v:A) -> A;
}
impl<A> AllCharsTrait<u8,A> for [A;256] {
    //#[inline(always)]
    fn get(&self, u:u8) -> &A { &self[u as usize] }
   // #[inline(always)]
    fn set(&mut self, u:u8,v:A) { self[u as usize] = v }
    // #[inline(always)]
    fn replace(&mut self, u: u8, v: A) -> A {
        std::mem::replace(&mut self[u as usize], v)
    }
}

/** A "string" in TeX is a sequence of characters of some [`CharType`]. [`TeXStr`]
* abstracts away the character type, e.g. for control sequence names.
*/
#[derive(Clone,PartialEq,Hash,Eq)]
pub struct TeXStr<C:CharType>(Ptr<Vec<C>>);
impl<C:CharType> TeXStr<C> {
    pub fn len(&self) -> usize { self.0.len() }
    pub fn as_vec(&self) -> &Vec<C> { &self.0 }
}

//#[inline(always)]
fn is_ascii(u:u8) -> bool { 32 <= u && u <= 126 }

impl<C:CharType> Display for TeXStr<C> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        C::display_str(self, f)
    }
}
impl From<&str> for TeXStr<u8> {
    fn from(s: &str) -> Self {
        TeXStr(Ptr::new(s.as_bytes().to_vec()))
    }
}
impl From<String> for TeXStr<u8> {
    fn from(s: String) -> Self {
        TeXStr(Ptr::new(s.into_bytes()))
    }
}
impl<C:CharType> From<Vec<C>> for TeXStr<C> {
    fn from(v: Vec<C>) -> Self {
        TeXStr(Ptr::new(v))
    }
}



impl CharType for char {
    const MAX: Self = 255 as char;
    type Allchars<A: Default> = AllUnicodeChars<A>; // TODO
    fn from_i64(i: i64) -> Option<Self> { if i > 0 && i < 0x110000 { Some(char::from_u32(i as u32).unwrap()) } else { None } }
    fn to_usize(self) -> usize { self as usize }
    fn from_str(s: &str) -> TeXStr<Self> {
        TeXStr(Ptr::new(s.chars().collect()))
    }
    fn display_str(str: &TeXStr<Self>, f: &mut Formatter<'_>) -> std::fmt::Result {
        let str : String = str.0.iter().collect();
        write!(f,"{}",str)
    }

    fn backslash() -> Self { '\\' }
    fn carriage_return() -> Self { '\r' }
    fn newline() -> Self { '\n' }
    fn char_str(&self) -> String { self.to_string() }
    fn is_eol(self) -> Option<bool> {
        match self {
            '\n' => Some(true),
            '\r' => None,
            _ => Some(false)
        }
    }
    fn is_eol_pair(self, next: Self) -> bool {
        next == '\n' // self == '\r'
    }
    fn as_bytes(&self) -> Vec<u8> {
        self.to_string().as_bytes().into_iter().map(|u| *u).collect()
    }

    fn ident() -> Self::Allchars<Self> {
        todo!()
    }
    fn empty_str() -> TeXStr<Self> {
        todo!()
    }
    fn par_token() -> TeXStr<Self> {
        todo!()
    }
    fn relax_token() -> TeXStr<Self> {
        todo!()
    }
    fn starting_catcode_scheme() -> CategoryCodeScheme<Self> {
        todo!()
    }
    fn from_u8_iter(iter: &mut IntoIter<u8>) -> Option<Self> {
        todo!()
    }
    fn rep_field<A: Clone + Default>(a: A) -> Self::Allchars<A> {
        todo!()
    }
    fn zeros() -> Self::Allchars<Self> {
        todo!()
    }
}

pub struct AllUnicodeChars<A:Default>(PhantomData<A>);
impl<A:Default> AllCharsTrait<char,A> for AllUnicodeChars<A> {
    fn get(&self, u: char) -> &A {
        todo!()
    }

    fn set(&mut self, u: char, v: A) {
        todo!()
    }

    fn replace(&mut self, u: char, v: A) -> A {
        todo!()
    }
}

/*
impl CharType for char {

}

#[derive(Copy,Clone,Debug,PartialEq,Eq,Hash)]
pub struct Unicode(char);

impl Default for Unicode {
    fn default() -> Self {
        Self(0 as char)
    }
}
impl Display for Unicode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f,"{}",self.0)
    }
}

impl Into<usize> for Unicode {
    fn into(self) -> usize {
        self.0 as usize
    }
}

impl From<u8> for Unicode {
    fn from(value: u8) -> Self {
        Self(char::from_u32(value as u32).unwrap())
    }
}

impl TryFrom<i64> for Unicode {
    type Error = ();

    fn try_from(val: i64) -> Result<Self, Self::Error> {
        if val > 0 && val < 0x10FFFF && (val <= 0xD800 || val > 0xDFFF) {
            Ok(Self(char::from_u32(val as u32).unwrap()))
        } else {
            Err(())
        }
    }
}

impl Into<i64> for Unicode {
    fn into(self) -> i64 {
        self.0 as i64
    }
}

impl CharType for Unicode {
    type Allchars<A:Default> = Vec<A>;
    const MAX:Unicode = Unicode(-1 as char);
    fn display_str(str: &TeXStr<Self>, f: &mut Formatter<'_>) -> std::fmt::Result {
        let str : String = str.0.iter().map(|c| c.0).collect();
        write!(f,"{}",str)
    }
    fn from_str(s: &str) -> TeXStr<Self> {
        TeXStr(Ptr::new(s.chars().map(|c| Unicode(c)).collect()))
    }
    fn backslash() -> Self { Unicode('\\') }
    fn carriage_return() -> Self { Unicode('\r') }
    fn char_str(&self) -> String { self.0.to_string() }
    fn is_eol(self) -> Option<bool> {
        match self.0 {
            '\r' => None,
            '\n' => Some(true),
            _ => Some(false)
        }
    }
    fn is_eol_pair(self, next: Self) -> bool {
        self.0 == '\r' && next.0 == '\n'
    }
    fn par_token() -> TeXStr<Self> {
        todo!()//TeXStr(Ptr::new(vec![Unicode('p'),Unicode('a'),Unicode('r')]))
    }
    fn relax_token() -> TeXStr<Self> {
        todo!()//TeXStr(Ptr::new(vec![Unicode('r'),Unicode('e'),Unicode('l'),Unicode('a'),Unicode('x')]))
    }
    fn empty_str() -> TeXStr<Self> {
        todo!()//TeXStr(Ptr::new(vec![]))
    }
    fn ident() -> Self::Allchars<Self> {
        todo!()
    }

}
impl<A:Default> AllCharsTrait<Unicode,A> for Vec<A> {
    fn get(&self, u: Unicode) -> &A { &self[u.0 as usize] }
    fn set(&mut self, u: Unicode, v:A) { self[u.0 as usize] = v }
    fn replace(&mut self, u: Unicode, v:A) -> A { std::mem::replace(&mut self[u.0 as usize],v) }
}
 */