aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-10-23 16:57:47 +0100
committerGitHub <[email protected]>2019-10-23 16:57:47 +0100
commit2197205885f43441f14861f34449426295397dd9 (patch)
tree3af21aaefe1efdabafeb5702959e1094504813e7 /xtask/src/codegen.rs
parentedf4d8e555c6847fb9e6e61d727c4def11789bfc (diff)
parent6048d294009f0f58593747e0870aa174e29a32af (diff)
Merge #2050
2050: xtask: don't depend on itertools r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'xtask/src/codegen.rs')
-rw-r--r--xtask/src/codegen.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
new file mode 100644
index 000000000..948b86719
--- /dev/null
+++ b/xtask/src/codegen.rs
@@ -0,0 +1,46 @@
1//! We use code generation heavily in rust-analyzer.
2//!
3//! Rather then doing it via proc-macros, we use old-school way of just dumping
4//! the source code.
5//!
6//! This module's submodules define specific bits that we generate.
7
8mod gen_syntax;
9mod gen_parser_tests;
10
11use std::{fs, path::Path};
12
13use crate::Result;
14
15pub use self::{gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax};
16
17pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
18const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar";
19const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok";
20const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err";
21
22pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs";
23pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs";
24
25#[derive(Debug, PartialEq, Eq, Clone, Copy)]
26pub enum Mode {
27 Overwrite,
28 Verify,
29}
30
31/// A helper to update file on disk if it has changed.
32/// With verify = false,
33pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
34 match fs::read_to_string(path) {
35 Ok(ref old_contents) if old_contents == contents => {
36 return Ok(());
37 }
38 _ => (),
39 }
40 if mode == Mode::Verify {
41 Err(format!("`{}` is not up-to-date", path.display()))?;
42 }
43 eprintln!("updating {}", path.display());
44 fs::write(path, contents)?;
45 Ok(())
46}