diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-23 16:57:47 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-23 16:57:47 +0100 |
commit | 2197205885f43441f14861f34449426295397dd9 (patch) | |
tree | 3af21aaefe1efdabafeb5702959e1094504813e7 /xtask/src/codegen.rs | |
parent | edf4d8e555c6847fb9e6e61d727c4def11789bfc (diff) | |
parent | 6048d294009f0f58593747e0870aa174e29a32af (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.rs | 46 |
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 | |||
8 | mod gen_syntax; | ||
9 | mod gen_parser_tests; | ||
10 | |||
11 | use std::{fs, path::Path}; | ||
12 | |||
13 | use crate::Result; | ||
14 | |||
15 | pub use self::{gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax}; | ||
16 | |||
17 | pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; | ||
18 | const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar"; | ||
19 | const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok"; | ||
20 | const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err"; | ||
21 | |||
22 | pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; | ||
23 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; | ||
24 | |||
25 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
26 | pub enum Mode { | ||
27 | Overwrite, | ||
28 | Verify, | ||
29 | } | ||
30 | |||
31 | /// A helper to update file on disk if it has changed. | ||
32 | /// With verify = false, | ||
33 | pub 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 | } | ||