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
//! *WIP* Parsing [`Token`](crate::latex::token::Token) iterators into more usable structures.
/*
use crate::error::Result;
use crate::token::Token;
use itertools::Itertools;
use logos::Lexer;
use std::iter::Peekable;
type Span = std::ops::Range<usize>;
pub trait TryFromTokens {
fn try_from_tokens<'source, I>(source: &'source str, iter: &mut Peekable<I>) -> Result<Self>
where
Self: Sized,
I: Iterator<Item = (Token, Span)>;
fn try_from_lexer<'source>(lexer: Lexer<'source, Token>) -> Result<Self>
where
Self: Sized,
{
let source = lexer.source();
let mut iter = lexer.spanned().peekable();
<Self as TryFromTokens>::try_from_tokens(source, &mut iter)
}
}
pub struct LaTeXDocument {
preamble: Preamble,
document: Document,
}
impl TryFromTokens for LaTeXDocument {
fn try_from_tokens<'source, I>(source: &'source str, iter: &mut Peekable<I>) -> Result<Self>
where
I: Iterator<Item = (Token, Span)>,
{
let preamble = Preamble::try_from_tokens(source, iter)?;
let document = Document::try_from_tokens(source, iter)?;
Ok(Self { preamble, document })
}
}
struct Preamble {}
impl TryFromTokens for Preamble {
fn try_from_tokens<'source, I>(source: &'source str, iter: &mut Peekable<I>) -> Result<Self>
where
I: Iterator<Item = (Token, Span)>,
{
match iter
.skip_while(|(token, _)| {
matches!(token, Token::Comment | Token::Newline | Token::TabsOrSpaces)
})
.next()
.expect("Preamble should start with a \\documentclass, but nothing was found")
{
(Token::DocumentClass, _) => (),
(token, span) => panic!(
"Preamble should start with a \\documentclass, not with {:#?}: {}",
token, &source[span]
),
}
while let Some((token, _span)) = iter.peek() {
match token {
// If \begin is found, we assumed it is \begin{document}
Token::EnvironmentBegin => return Ok(Self {}),
_ => (),
}
iter.next();
}
Ok(Self {})
}
}
struct Document {}
impl TryFromTokens for Document {
fn try_from_tokens<'source, I>(source: &'source str, iter: &mut Peekable<I>) -> Result<Self>
where
I: Iterator<Item = (Token, Span)>,
{
let mut iter = iter.skip_while(|(token, _)| {
matches!(token, Token::Comment | Token::Newline | Token::TabsOrSpaces)
});
match iter
.next_tuple()
.expect("Document should start with a \\begin{{document}}, but nothing was found")
{
(
(Token::EnvironmentBegin, _),
(Token::BraceOpen, _),
(Token::Word, word_span),
(Token::BraceClose, _),
) if &source[word_span.clone()] == "document" => (),
_ => panic!("Document should start with a \\begin{{document}}"),
}
while let Some((token, span)) = iter.next() {
match token {
Token::EnvironmentEnd => {
if let Some((token, span)) = iter
.skip_while(|(token, _)| {
matches!(token, Token::Comment | Token::Newline | Token::TabsOrSpaces)
})
.next()
{
panic!(
"Unexpected token found after \\end{{document}}: {}",
&source[span]
);
}
return Ok(Self {});
}
_ => (),
}
}
panic!("Missing \\end{{document}}");
Ok(Self {})
}
}
struct Options<'source> {
s: &'source str,
}
struct Arguments<'source> {
s: &'source str,
}
struct Command<'source> {
name: &'source str,
opts: Options<'source>,
args: Arguments<'source>,
span: Span,
}
struct Environment<'source> {
name: &'source str,
opts: Option<Options<'source>>,
args: Option<Arguments<'source>>,
span: Span,
}
*/