fea-rs 0.20.3

Tools for working with Adobe OpenType Feature files.
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
use std::{fmt::Write, io::Write as _};

use std::{ops::Range, sync::Arc};

use smol_str::SmolStr;

use crate::parse::{FileId, IncludeStatement};
use crate::{diagnostic::Diagnostic, GlyphMap, Level};

use self::cursor::Cursor;
use typed::AstNode as _;

mod cursor;
mod edit;
mod rewrite;
mod stack;
mod token;
pub mod typed;

use rewrite::ReparseCtx;
pub use token::Kind;

/// A node in the token tree.
///
/// A node is tagged with a `Kind`, and includes any number of child nodes or tokens.
#[derive(PartialEq, Eq, Clone, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Node {
    /// The ``Kind` of this node.
    kind: Kind,

    // NOTE: the absolute position within the tree is not known when the node
    // is created; this is updated (and correct) only when the node has been
    // accessed via a `Cursor`.
    abs_pos: u32,
    /// The length of the text spanned by this node
    text_len: u32,
    /// true if an error was encountered in this node.
    ///
    /// This is not recursive; it is only true for the direct parent of an error span.
    pub error: bool,
    //NOTE: children should not be accessed directly, but only via a cursor.
    // this ensures that their positions are updated correctly.
    children: Arc<Vec<NodeOrToken>>,
}

/// A token is a chunk of text, tagged with a `Kind`.
#[derive(Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Token {
    /// The [`Kind`] of this token
    pub kind: Kind,
    /// The absolute position in the source where this token starts
    abs_pos: u32,
    /// The token text
    pub text: SmolStr,
}

/// Either a [`Node`] or a [`Token`].
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum NodeOrToken {
    /// A node
    Node(Node),
    /// A token
    Token(Token),
}

#[derive(Clone, Debug, Default)]
pub(crate) struct TreeBuilder {
    //TODO: reuse tokens
    //token_cache: HashMap<Arc<Token>>,
    // the kind of the parent, and the index in children of the first child.
    parents: Vec<(Kind, usize)>,
    children: Vec<NodeOrToken>,
}

/// Consumes tokens during parsing, building up an AST.
pub(crate) struct AstSink<'a> {
    text: &'a str,
    file_id: FileId,
    text_pos: usize,
    builder: TreeBuilder,
    // reuseable buffer for reparsing
    reparse_buf: Vec<NodeOrToken>,
    glyph_map: Option<&'a GlyphMap>,
    errors: Vec<Diagnostic>,
    include_statement_count: usize,
    cur_node_contains_error: bool,
}

//NOTE: the inner type is option because we reuse this in the `typed` module,
//and there we want to be able to easily return an empty iterator when iter() is
//called on a token.
//
/// An iterator over the children of a node.
#[derive(Default)]
pub struct ChildIter<'a>(Option<Cursor<'a>>);

impl<'a> AstSink<'a> {
    pub fn new(text: &'a str, file_id: FileId, glyph_map: Option<&'a GlyphMap>) -> Self {
        AstSink {
            file_id,
            text,
            text_pos: 0,
            builder: TreeBuilder::default(),
            glyph_map,
            errors: Vec::new(),
            cur_node_contains_error: false,
            include_statement_count: 0,
            reparse_buf: Default::default(),
        }
    }

    pub(crate) fn token(&mut self, kind: Kind, len: usize) {
        let token_text = &self.text[self.text_pos..self.text_pos + len];
        let to_add = self.validate_token(kind, token_text);
        self.builder.push_raw(to_add);
        self.text_pos += len;
    }

    pub(crate) fn start_node(&mut self, kind: Kind) {
        self.builder.start_node(kind);
    }

    pub(crate) fn finish_node(&mut self, kind: Option<Kind>) {
        let cur_kind = kind
            .or_else(|| self.builder.parents.last().map(|x| x.0))
            .unwrap();
        let kind = self.maybe_rewrite_current_node(cur_kind).or(kind);
        self.builder.finish_node(self.cur_node_contains_error, kind);
        self.cur_node_contains_error = false;
        // if this is an include statement we store a copy.
        if self.builder.children.last().map(|n| n.kind()) == Some(Kind::IncludeNode) {
            self.include_statement_count += 1;
        }
    }

    pub(crate) fn current_node_has_error(&self) -> bool {
        self.cur_node_contains_error
    }

    pub(crate) fn error(&mut self, mut error: Diagnostic) {
        let is_hard_error = error.level == Level::Error;
        error.message.file = self.file_id;
        self.errors.push(error);
        self.cur_node_contains_error = is_hard_error;
    }

    pub fn finish(self) -> (Node, Vec<Diagnostic>, Vec<IncludeStatement>) {
        let mut node = self.builder.finish();
        node.update_positions_from_root();
        let mut includes = Vec::new();
        if self.include_statement_count > 0 {
            node.find_include_nodes(&mut includes, self.include_statement_count);
        }
        (node, self.errors, includes)
    }

    #[cfg(test)]
    pub fn errors(&self) -> &[Diagnostic] {
        &self.errors
    }

    /// called before adding a token.
    ///
    /// We can perform additional validation here. Currently it is mostly for
    /// disambiguating glyph names that might be ranges.
    fn validate_token(&mut self, kind: Kind, text: &str) -> NodeOrToken {
        if kind == Kind::GlyphNameOrRange {
            if let Some(map) = self.glyph_map {
                if map.contains(text) {
                    return Token::new(Kind::GlyphName, text.into()).into();
                }
                match try_split_range(text, map) {
                    Ok(node) => return node.into(),
                    Err(message) => {
                        let range = self.text_pos..self.text_pos + text.len();
                        self.error(Diagnostic::error(FileId::CURRENT_FILE, range, message));
                    }
                }
            }
        }
        Token::new(kind, text.into()).into()
    }

    /// Called before finishing a node.
    ///
    /// This is an opportunity for us to rewrite this node's tree, which is
    /// something that we do for rules with contextual glyphs.
    ///
    /// This lets us provide much better information about the rule to the
    /// rest of the compilation pipeline.
    ///
    /// The reason that we have to do this after the first pass is because
    /// determining whether or not a rule is contextual requires arbitrary
    /// lookahead at the parser level. Instead of writing an arbitrary lookahead
    /// parser, we instead rescan the children after parsing, grouping them
    /// into things like backtrack/context/lookahead sequences.
    fn maybe_rewrite_current_node(&mut self, cur_kind: Kind) -> Option<Kind> {
        match cur_kind {
            _ if self.cur_node_contains_error => None,
            Kind::GsubNodeNeedsRewrite => {
                Some(self.rewrite_current_node(rewrite::reparse_contextual_sub_rule))
            }
            Kind::GposNodeNeedsRewrite => {
                Some(self.rewrite_current_node(rewrite::reparse_contextual_pos_rule))
            }
            _ => None,
        }
    }

    fn rewrite_current_node(&mut self, rewrite_fn: impl FnOnce(&mut ReparseCtx) -> Kind) -> Kind {
        assert!(self.reparse_buf.is_empty());
        self.builder.move_current_children(&mut self.reparse_buf);
        // temporarily take the buffer to satisfy borrowck
        let mut buf = std::mem::take(&mut self.reparse_buf);
        let items_start_offset: usize = buf.iter().map(NodeOrToken::text_len).sum();

        let mut reparse_ctx = ReparseCtx {
            in_buf: &mut buf,
            text_pos: self.text_pos - items_start_offset,
            sink: self,
        };
        let new_kind = rewrite_fn(&mut reparse_ctx);
        // put back the buffer so we can reuse the storage next time
        assert!(
            reparse_ctx.in_buf.is_empty(),
            "rewrite finished with unhandled items"
        );
        buf.clear();
        std::mem::swap(&mut self.reparse_buf, &mut buf);
        new_kind
    }

    fn push_raw(&mut self, n: NodeOrToken) {
        self.builder.push_raw(n);
    }
}

impl Node {
    fn new(kind: Kind, children: Vec<NodeOrToken>, error: bool) -> Self {
        let text_len = children.iter().map(|x| x.text_len() as u32).sum();
        Node {
            kind,
            text_len,
            abs_pos: 0,
            children: children.into(),
            error,
        }
    }

    /// recursively compute and update the positions of each child.
    ///
    /// This should only be called on a root node; it assumes the position
    /// of the callee is `0`.
    ///
    /// This is required in order for us to correctly associate diagnostics
    /// with their locations in the source.
    pub(crate) fn update_positions_from_root(&mut self) {
        self.update_positions_recurse(0)
    }

    fn update_positions_recurse(&mut self, mut pos: usize) {
        self.abs_pos = pos as _;
        let children = Arc::make_mut(&mut self.children);

        for child in children {
            child.update_positions(pos);
            pos += child.text_len();
        }
    }

    /// Construct a new cursor for navigating the node's children
    pub(crate) fn cursor(&self) -> Cursor<'_> {
        Cursor::new(self)
    }

    /// Iterate over tokens, descending into child nodes.
    pub fn iter_tokens(&self) -> impl Iterator<Item = &Token> {
        let mut cursor = self.cursor();
        std::iter::from_fn(move || cursor.next_token())
    }

    /// Iterate over this node's direct children, without descending.
    pub fn iter_children(&self) -> ChildIter<'_> {
        ChildIter(Some(self.cursor()))
    }

    /// The `Kind` of the node
    pub fn kind(&self) -> Kind {
        self.kind
    }

    /// The total length of all tokens that are descendents of this node.
    pub fn text_len(&self) -> usize {
        self.text_len as usize
    }

    /// The range in the original source of this node.
    ///
    /// Only correct if this node is accessed via a cursor.
    pub fn range(&self) -> Range<usize> {
        let start = self.abs_pos as usize;
        start..start + (self.text_len as usize)
    }

    /// Create a new tree, replacing the provided ranges with the provided
    /// nodes.
    ///
    /// if skip_parent is true, children of inserted nodes are added directly,
    /// without their parent nodes. (we use this when resolving includes
    ///
    /// range start/end just fall on token boundaries.
    pub(crate) fn edit(&self, edits: Vec<(Range<usize>, Node)>, skip_parent: bool) -> Node {
        edit::apply_edits(self, edits, skip_parent)
    }

    fn find_include_nodes(&self, collect: &mut Vec<IncludeStatement>, num: usize) {
        for item in self.iter_children() {
            if let Some(node) = item.as_node() {
                if let Some(include) = typed::Include::cast(item) {
                    collect.push(IncludeStatement(include));
                    if collect.len() == num {
                        return;
                    }
                } else {
                    node.find_include_nodes(collect, num);
                }
            }
        }
    }

    #[doc(hidden)]
    // used in some tests for debugging
    pub fn debug_print_structure(&self, include_tokens: bool) {
        let mut cursor = self.cursor();
        while let Some(thing) = cursor.current() {
            match thing {
                NodeOrToken::Node(node) => {
                    let depth = cursor.depth();
                    let _ = writeln!(
                        std::io::stderr(),
                        "{}{} ({}..{})",
                        &crate::util::SPACES[..depth * 2],
                        node.kind,
                        cursor.pos(),
                        cursor.pos() + node.text_len()
                    );
                }
                NodeOrToken::Token(t) if include_tokens => eprint!("{}", t.as_str()),
                _ => (),
            }
            cursor.advance();
        }
    }

    #[doc(hidden)]
    pub fn simple_parse_tree(&self) -> String {
        let mut result = String::new();
        self.parse_tree_impl(0, &mut result).unwrap();
        result
    }

    fn parse_tree_impl(&self, depth: usize, buf: &mut String) -> std::fmt::Result {
        use crate::util::SPACES;
        let mut pos = self.abs_pos;
        writeln!(
            buf,
            "{}{}@[{}; {})",
            &SPACES[..depth * 2],
            self.kind,
            pos,
            pos + self.text_len
        )?;
        let depth = depth + 1;
        for child in self.iter_children() {
            match child {
                NodeOrToken::Token(Token { kind, text, .. }) => {
                    let spaces = &SPACES[..depth * 2];
                    write!(buf, "{spaces}{kind}@{pos}")?;
                    if kind.is_trivia() {
                        writeln!(buf, " \"{}\"", text.escape_debug())?;
                    } else {
                        writeln!(buf, " \"{text}\"")?;
                    }
                    pos += text.len() as u32;
                }
                NodeOrToken::Node(node) => {
                    node.parse_tree_impl(depth + 1, buf)?;
                    pos += node.text_len;
                }
            }
        }
        Ok(())
    }
}

impl<'a> Iterator for ChildIter<'a> {
    type Item = &'a NodeOrToken;

    fn next(&mut self) -> Option<Self::Item> {
        let current = self.0.as_ref()?.current();
        self.0.as_mut()?.step_over();
        current
    }
}

impl TreeBuilder {
    pub(crate) fn start_node(&mut self, kind: Kind) {
        let len = self.children.len();
        self.parents.push((kind, len));
    }

    pub(crate) fn token(&mut self, kind: Kind, text: impl Into<SmolStr>) {
        let token = Token::new(kind, text.into());
        self.push_raw(token.into());
    }

    fn push_raw(&mut self, item: NodeOrToken) {
        self.children.push(item)
    }

    /// copy the children of the currently open node into a buffer.
    ///
    /// This is only used as part of reparsing.
    fn move_current_children(&mut self, to_buf: &mut Vec<NodeOrToken>) {
        if let Some(idx) = self.parents.last().map(|(_, idx)| idx).copied() {
            to_buf.extend(self.children.drain(idx..));
        }
    }

    pub(crate) fn finish_node(&mut self, error: bool, new_kind: Option<Kind>) {
        let (kind, first_child) = self.parents.pop().unwrap();
        let kind = new_kind.unwrap_or(kind);
        let node = Node::new(kind, self.children.split_off(first_child), error);
        self.push_raw(node.into());
    }

    pub(crate) fn finish(mut self) -> Node {
        assert_eq!(self.children.len(), 1);
        self.children.pop().unwrap().into_node().unwrap()
    }
}

impl NodeOrToken {
    fn update_positions(&mut self, pos: usize) {
        match self {
            NodeOrToken::Token(t) => t.abs_pos = pos as _,
            NodeOrToken::Node(n) => n.update_positions_recurse(pos),
        }
    }

    /// `true` if this is a single token.
    pub fn is_token(&self) -> bool {
        matches!(self, NodeOrToken::Token(_))
    }

    /// If this is a single token, return that token's text
    pub fn token_text(&self) -> Option<&str> {
        self.as_token().map(Token::as_str)
    }

    /// The `Kind` of this node or token
    pub fn kind(&self) -> Kind {
        match self {
            NodeOrToken::Node(n) => n.kind,
            NodeOrToken::Token(t) => t.kind,
        }
    }

    /// `true` If this is a glyph name, a CID, or a glyph class (either inline or named)
    pub fn is_glyph_or_glyph_class(&self) -> bool {
        matches!(
            self.kind(),
            Kind::GlyphName | Kind::Cid | Kind::GlyphClass | Kind::NamedGlyphClass
        )
    }

    /// The range in the source text of this node or token.
    ///
    /// Note: this is only accurate if the token was accessed via a cursor.
    pub fn range(&self) -> Range<usize> {
        match self {
            NodeOrToken::Token(t) => t.range(),
            NodeOrToken::Node(n) => n.range(),
        }
    }

    /// The length of this token or node's text
    pub fn text_len(&self) -> usize {
        match self {
            NodeOrToken::Node(n) => n.text_len as usize,
            NodeOrToken::Token(t) => t.text.len(),
        }
    }

    /// If this is a `Node`, return it
    pub fn into_node(self) -> Option<Node> {
        match self {
            NodeOrToken::Node(node) => Some(node),
            NodeOrToken::Token(_) => None,
        }
    }

    /// If this is a `Node`, return a reference to it
    pub fn as_node(&self) -> Option<&Node> {
        match self {
            NodeOrToken::Node(node) => Some(node),
            NodeOrToken::Token(_) => None,
        }
    }

    /// IF this is a token, return a reference to it.
    pub fn as_token(&self) -> Option<&Token> {
        match self {
            NodeOrToken::Node(_) => None,
            NodeOrToken::Token(token) => Some(token),
        }
    }
}

impl From<Node> for NodeOrToken {
    fn from(src: Node) -> NodeOrToken {
        NodeOrToken::Node(src)
    }
}

impl From<Token> for NodeOrToken {
    fn from(src: Token) -> NodeOrToken {
        NodeOrToken::Token(src)
    }
}

impl Token {
    fn new(kind: Kind, text: SmolStr) -> Self {
        Token {
            kind,
            text,
            abs_pos: 0,
        }
    }

    /// The raw text for this token
    pub fn as_str(&self) -> &str {
        &self.text
    }

    /// The position of this token in its source.
    pub fn range(&self) -> Range<usize> {
        self.abs_pos as usize..self.abs_pos as usize + self.text.len()
    }
}

/// try to split a glyph containing hyphens into a glyph range.
fn try_split_range(text: &str, glyph_map: &GlyphMap) -> Result<Node, String> {
    let mut solution = None;

    // we try all possible split points
    for idx in text
        .bytes()
        .enumerate()
        .filter_map(|(idx, b)| (b == b'-').then_some(idx))
    {
        let (head, tail) = text.split_at(idx);
        if glyph_map.contains(head) && glyph_map.contains(tail.trim_start_matches('-')) {
            if let Some(prev_idx) = solution.replace(idx) {
                let (head1, tail1) = text.split_at(prev_idx);
                let (head2, tail2) = text.split_at(idx);
                let message = format!("the name '{}' contains multiple possible glyph ranges ({} to {} and {} to {}). Please insert spaces around the '-' to clarify your intent.", text, head1, tail1.trim_end_matches('-'), head2, tail2.trim_end_matches('-'));
                return Err(message);
            }
        }
    }

    // if we have a solution, generate a new node
    solution
        .map(|idx| {
            let mut builder = TreeBuilder::default();
            builder.start_node(Kind::GlyphRange);
            let (head, tail) = text.split_at(idx);
            builder.token(Kind::GlyphName, head);
            builder.token(Kind::Hyphen, "-");
            builder.token(Kind::GlyphName, tail.trim_start_matches('-'));
            builder.finish_node(false, None);
            builder.finish()
        })
        .ok_or_else(|| format!("'{text}' is neither a known glyph or a range of known glyphs",))
}

impl Node {
    fn debug_impl(&self, f: &mut std::fmt::Formatter, depth: usize) -> std::fmt::Result {
        use crate::util::SPACES;

        let ws = &SPACES[..depth * 2];
        write!(
            f,
            "\n{ws}{}:  abs {} len {} children {}",
            self.kind,
            self.abs_pos,
            self.text_len,
            self.children.len()
        )?;
        let ws = &SPACES[..(depth + 1) * 2];
        for child in self.iter_children() {
            match child {
                NodeOrToken::Token(t) => write!(f, "\n{}'{}' {}", ws, t.text, t.kind)?,
                NodeOrToken::Node(n) => n.debug_impl(f, depth + 1)?,
            }
        }
        Ok(())
    }
}

impl std::fmt::Debug for Node {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.debug_impl(f, 0)
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    static SAMPLE_FEA: &str = include_str!("../test-data/fonttools-tests/mini.fea");

    #[test]
    fn token_iter() {
        let (ast, _errs) = crate::parse::parse_string(SAMPLE_FEA);
        let reconstruct = ast
            .root()
            .iter_tokens()
            .map(Token::as_str)
            .collect::<String>();
        crate::assert_eq_str!(SAMPLE_FEA, reconstruct);
    }
}