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
use std::{fs, path::Path};
use anyhow::anyhow;
use gazebo::prelude::*;
use lalrpop_util as lu;
use crate::{
codemap::{CodeMap, FileSpan, Pos, Span},
errors::Diagnostic,
syntax::{
ast::{AstModule, AstStmt, Stmt},
dialect::Dialect,
grammar::StarlarkParser,
lexer::{Lexer, Token},
},
};
fn one_of(expected: &[String]) -> String {
let mut result = String::new();
for (i, e) in expected.iter().enumerate() {
let sep = match i {
0 => "one of",
_ if i < expected.len() - 1 => ",",
_ => " or",
};
result.push_str(&format!("{} {}", sep, e));
}
result
}
pub(crate) fn parse_error_add_span(
err: lu::ParseError<usize, Token, anyhow::Error>,
len: usize,
codemap: &CodeMap,
) -> anyhow::Error {
if let lu::ParseError::User { error } = err {
return error;
}
let message = match &err {
lu::ParseError::InvalidToken { .. } => "Parse error: invalid token".to_owned(),
lu::ParseError::UnrecognizedToken {
token: (_x, t, ..),
expected,
} => format!(
"Parse error: unexpected {} here, expected {}",
t,
one_of(expected)
),
lu::ParseError::ExtraToken { token: (_x, t, ..) } => {
format!("Parse error: extraneous token {}", t)
}
lu::ParseError::UnrecognizedEOF { .. } => "Parse error: unexpected end of file".to_owned(),
lu::ParseError::User { .. } => unreachable!(),
};
let span = match &err {
lu::ParseError::InvalidToken { location } => {
Span::new(Pos::new(*location as u32), Pos::new(*location as u32))
}
lu::ParseError::UnrecognizedToken {
token: (x, .., y), ..
} => Span::new(Pos::new(*x as u32), Pos::new(*y as u32)),
lu::ParseError::UnrecognizedEOF { .. } => {
Span::new(Pos::new(len as u32), Pos::new(len as u32))
}
lu::ParseError::ExtraToken { token: (x, .., y) } => {
Span::new(Pos::new(*x as u32), Pos::new(*y as u32))
}
lu::ParseError::User { .. } => unreachable!(),
};
Diagnostic::new(anyhow!(message), span, codemap)
}
impl AstModule {
fn create(
codemap: CodeMap,
statement: AstStmt,
dialect: &Dialect,
) -> anyhow::Result<AstModule> {
Stmt::validate(&codemap, &statement, dialect)?;
Ok(AstModule {
codemap,
statement,
dialect: dialect.clone(),
})
}
pub fn parse_file(path: &Path, dialect: &Dialect) -> anyhow::Result<Self> {
let content = fs::read_to_string(path)?;
Self::parse(&path.to_string_lossy(), content, dialect)
}
pub fn parse(filename: &str, content: String, dialect: &Dialect) -> anyhow::Result<Self> {
let codemap = CodeMap::new(filename.to_owned(), content);
let lexer = Lexer::new(codemap.source(), dialect, codemap.dupe());
match StarlarkParser::new().parse(&codemap, dialect, lexer) {
Ok(v) => Ok(AstModule::create(codemap, v, dialect)?),
Err(p) => Err(parse_error_add_span(p, codemap.source().len(), &codemap)),
}
}
pub fn loads(&self) -> Vec<&str> {
fn f<'a>(ast: &'a AstStmt, vec: &mut Vec<&'a str>) {
match &ast.node {
Stmt::Load(load) => vec.push(&load.module.node),
Stmt::Statements(stmts) => {
for s in stmts {
f(s, vec);
}
}
_ => {}
}
}
let mut loads = Vec::new();
f(&self.statement, &mut loads);
loads
}
pub(crate) fn file_span(&self, x: Span) -> FileSpan {
self.codemap.file_span(x)
}
}