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
use crate::codegen::generate_file_headers::write_headers;
use crate::codegen::error::CodegenError;
use crate::codegen::error::CodegenError::NoExtension;
use crate::codegen::generate_from_pairs::generate_from_pairs;
use crate::codegen::generate_misc::{generate_parser, generate_root};
use crate::codegen::generate_structs::generate_structs;
use crate::codegen::generate_trait_impls::generate_trait_impls;
use crate::codegen::FormattingFile;
use crate::config::toml::{find_config_path, read_config, ReadConfigError};
use crate::config::Config;
use crate::error::display_miette_error;
use crate::language::Language;
use crate::parser::syntax_file::{convert_syntax_file_ast, ParseError, SyntaxFile};
use crate::sources::source_file::SourceFile;
use proc_macro2::TokenStream;
use quote::quote;
use std::io::Write;
use std::path::{Path, PathBuf};
pub fn create_module_files<const N: usize>(
location: impl AsRef<Path>,
files: [&str; N],
) -> Result<[(FormattingFile, String); N], CodegenError> {
let mut location = location.as_ref().to_path_buf();
location.set_extension("");
std::fs::create_dir_all(&location)?;
println!("cargo:rerun-if-changed={:?}", location);
let mut res = files.map(|_| None);
for (index, &i) in files.iter().enumerate() {
let mut filename = location.clone();
filename.push(i);
filename.set_extension("rs");
println!("cargo:rerun-if-changed={:?}", filename);
res[index] = Some((
FormattingFile::create(&filename)?,
filename
.file_stem()
.ok_or(NoExtension)?
.to_string_lossy()
.into_owned(),
));
}
Ok(res.map(|i| i.unwrap()))
}
fn refs(inp: &mut (FormattingFile, String)) -> (&mut FormattingFile, &str) {
(&mut inp.0, &inp.1)
}
pub(crate) struct Generated {
impls: TokenStream,
structs: TokenStream,
from_pairs: TokenStream,
root: TokenStream,
parser: TokenStream,
}
fn codegen_internal(
source: SourceFile,
config: Config,
imports: &[&str],
) -> Result<Generated, CodegenError> {
let ast = SyntaxFile::try_parse(&source)?;
let serialized_parser = bincode::serialize(&ast)?;
let legacy_ast = convert_syntax_file_ast::convert(ast)?;
let mut derives = vec!["Debug"];
if config.syntax.serde {
derives.extend(["Serialize", "Deserialize"]);
}
let structs = generate_structs(&legacy_ast, &derives, config.syntax.non_exhaustive)?;
let from_pairs = generate_from_pairs(&legacy_ast, config.syntax.non_exhaustive)?;
let impls = generate_trait_impls(&legacy_ast)?;
let root = generate_root(
imports,
&derives,
config.syntax.mode.import_location(),
config.syntax.non_exhaustive,
)?;
let parser = generate_parser(&serialized_parser)?;
Ok(Generated {
impls,
structs,
from_pairs,
root,
parser,
})
}
#[doc(hidden)]
pub fn __codegen_tokenstream(
source: SourceFile,
config: Config,
debug: bool,
) -> Result<TokenStream, CodegenError> {
let Generated {
impls,
structs,
from_pairs,
root,
parser,
} = codegen_internal(source, config, &[])?;
if debug {
println!("{}", structs);
}
Ok(quote!(
mod ast {
#structs
}
mod from_pairs {
#from_pairs
}
mod ast_impls {
#impls
}
mod parser {
#parser
}
pub use ast::*;
pub use from_pairs::*;
pub use ast_impls::*;
pub use parser::*;
#root
))
}
fn unwrap<T>(r: Result<T, ReadConfigError>) -> T {
match r {
Ok(i) => i,
Err(e) => {
panic!("failed to read config: {e}")
}
}
}
pub struct Codegen {
config: Config,
}
impl Default for Codegen {
fn default() -> Self {
Self::new()
}
}
impl Codegen {
pub fn try_new() -> Result<Self, ReadConfigError> {
Self::try_with_config(find_config_path())
}
pub fn try_with_config(path: PathBuf) -> Result<Self, ReadConfigError> {
println!("cargo:rerun-if-changed={:?}", path);
Ok(Self::with_config_struct(read_config(path)?))
}
pub fn with_config_struct(config: Config) -> Self {
Self { config }
}
pub fn new() -> Self {
unwrap(Self::try_new())
}
pub fn with_config(path: PathBuf) -> Self {
unwrap(Self::try_with_config(path))
}
pub fn codegen(self) {
if let Err(e) = self.try_codegen() {
match e {
CodegenError::ParseError(ParseError::PEG(errs)) => {
for e in errs.iter().rev() {
eprintln!("{}", display_miette_error(e));
}
panic!("failed to generate ast")
}
e => panic!("failed to generate ast: {e}"),
}
}
}
pub fn try_codegen(self) -> Result<(), CodegenError> {
let mut files = create_module_files(
&self.config.syntax.destination,
[
"mod.rs",
"ast.rs",
"from_pairs.rs",
"ast_impls.rs",
"parser.rs",
],
)?;
write_headers(&mut files.iter_mut().map(refs).collect::<Vec<_>>())?;
let [ref mut f_modrs, ref mut f_ast, ref mut f_from_pairs, ref mut f_ast_trait_impls, ref mut f_serialized_parser] =
files.map(|i| i.0);
let write_serialized_ast = self.config.syntax.write_serialized_ast;
let Generated {
impls,
structs,
from_pairs,
root,
parser,
} = codegen_internal(
SourceFile::open(&self.config.syntax.definition)?,
self.config,
&["ast", "from_pairs", "ast_impls", "parser"],
)?;
write!(f_modrs, "{}", root)?;
write!(f_ast, "{}", structs)?;
write!(f_ast_trait_impls, "{}", impls)?;
write!(f_from_pairs, "{}", from_pairs)?;
if write_serialized_ast {
write!(f_serialized_parser, "{}", parser)?;
}
Ok(())
}
}