summaryrefslogtreecommitdiff
path: root/support/texlab/src/syntax/latex/parser.rs
blob: 40693071c2c3218a081cb512af4cf4527bf51719 (plain)
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
use super::ast::*;
use std::iter::Peekable;
use std::sync::Arc;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum LatexScope {
    Root,
    Group,
    Options,
}

pub struct LatexParser<I: Iterator<Item = LatexToken>> {
    tokens: Peekable<I>,
}

impl<I: Iterator<Item = LatexToken>> LatexParser<I> {
    pub fn new(tokens: I) -> Self {
        LatexParser {
            tokens: tokens.peekable(),
        }
    }

    pub fn root(&mut self) -> LatexRoot {
        let children = self.content(LatexScope::Root);
        LatexRoot::new(children)
    }

    fn content(&mut self, scope: LatexScope) -> Vec<LatexContent> {
        let mut children = Vec::new();
        while let Some(ref token) = self.tokens.peek() {
            match token.kind {
                LatexTokenKind::Word | LatexTokenKind::BeginOptions => {
                    children.push(LatexContent::Text(self.text(scope)));
                }
                LatexTokenKind::Command => {
                    children.push(LatexContent::Command(self.command()));
                }
                LatexTokenKind::Comma => {
                    let node = LatexComma::new(self.tokens.next().unwrap());
                    children.push(LatexContent::Comma(Arc::new(node)));
                }
                LatexTokenKind::Math => {
                    children.push(LatexContent::Math(self.math()));
                }
                LatexTokenKind::BeginGroup => {
                    children.push(LatexContent::Group(self.group(LatexGroupKind::Group)));
                }
                LatexTokenKind::EndGroup => {
                    if scope == LatexScope::Root {
                        self.tokens.next();
                    } else {
                        return children;
                    }
                }
                LatexTokenKind::EndOptions => {
                    if scope == LatexScope::Options {
                        return children;
                    } else {
                        children.push(LatexContent::Text(self.text(scope)));
                    }
                }
            }
        }
        children
    }

    fn command(&mut self) -> Arc<LatexCommand> {
        let name = self.tokens.next().unwrap();

        let mut options = Vec::new();
        let mut args = Vec::new();
        while let Some(token) = self.tokens.peek() {
            match token.kind {
                LatexTokenKind::BeginGroup => {
                    args.push(self.group(LatexGroupKind::Group));
                }
                LatexTokenKind::BeginOptions => {
                    options.push(self.group(LatexGroupKind::Options));
                }
                _ => {
                    break;
                }
            }
        }
        Arc::new(LatexCommand::new(name, options, args))
    }

    fn group(&mut self, kind: LatexGroupKind) -> Arc<LatexGroup> {
        let left = self.tokens.next().unwrap();
        let scope = match kind {
            LatexGroupKind::Group => LatexScope::Group,
            LatexGroupKind::Options => LatexScope::Options,
        };
        let children = self.content(scope);
        let right_kind = match kind {
            LatexGroupKind::Group => LatexTokenKind::EndGroup,
            LatexGroupKind::Options => LatexTokenKind::EndOptions,
        };

        let right = if self.next_of_kind(right_kind) {
            self.tokens.next()
        } else {
            None
        };

        Arc::new(LatexGroup::new(left, children, right, kind))
    }

    fn text(&mut self, scope: LatexScope) -> Arc<LatexText> {
        let mut words = Vec::new();
        while let Some(ref token) = self.tokens.peek() {
            let kind = token.kind;
            let opts = kind == LatexTokenKind::EndOptions && scope != LatexScope::Options;
            if kind == LatexTokenKind::Word || kind == LatexTokenKind::BeginOptions || opts {
                words.push(self.tokens.next().unwrap());
            } else {
                break;
            }
        }
        Arc::new(LatexText::new(words))
    }

    fn math(&mut self) -> Arc<LatexMath> {
        let token = self.tokens.next().unwrap();
        Arc::new(LatexMath::new(token))
    }

    fn next_of_kind(&mut self, kind: LatexTokenKind) -> bool {
        if let Some(ref token) = self.tokens.peek() {
            token.kind == kind
        } else {
            false
        }
    }
}