aboutsummaryrefslogtreecommitdiff
path: root/tools/src/bin/gen.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tools/src/bin/gen.rs')
-rw-r--r--tools/src/bin/gen.rs121
1 files changed, 0 insertions, 121 deletions
diff --git a/tools/src/bin/gen.rs b/tools/src/bin/gen.rs
deleted file mode 100644
index 2d3cd422d..000000000
--- a/tools/src/bin/gen.rs
+++ /dev/null
@@ -1,121 +0,0 @@
1extern crate serde;
2#[macro_use]
3extern crate serde_derive;
4
5extern crate file;
6extern crate ron;
7
8use std::path::PathBuf;
9use std::fmt::Write;
10
11fn main() {
12 let grammar = Grammar::read();
13 let text = grammar.to_syntax_kinds();
14 let target = generated_file();
15 if text != file::get_text(&target).unwrap_or_default() {
16 file::put_text(&target, &text).unwrap();
17 }
18}
19
20#[derive(Deserialize)]
21struct Grammar {
22 keywords: Vec<String>,
23 contextual_keywords: Vec<String>,
24 tokens: Vec<String>,
25 nodes: Vec<String>,
26}
27
28impl Grammar {
29 fn read() -> Grammar {
30 let text = file::get_text(&grammar_file()).unwrap();
31 ron::de::from_str(&text).unwrap()
32 }
33
34 fn to_syntax_kinds(&self) -> String {
35 let mut acc = String::new();
36 acc.push_str("#![allow(bad_style, missing_docs, unreachable_pub)]\n");
37 acc.push_str("#![cfg_attr(rustfmt, rustfmt_skip)]\n");
38 acc.push_str("//! Generated from grammar.ron\n");
39 acc.push_str("use super::SyntaxInfo;\n");
40 acc.push_str("\n");
41
42 let syntax_kinds: Vec<String> = self.tokens
43 .iter()
44 .cloned()
45 .chain(self.keywords.iter().map(|kw| kw_token(kw)))
46 .chain(self.contextual_keywords.iter().map(|kw| kw_token(kw)))
47 .chain(self.nodes.iter().cloned())
48 .collect();
49
50 // enum SyntaxKind
51 acc.push_str("/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.\n");
52 acc.push_str("#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]\n");
53 acc.push_str("pub enum SyntaxKind {\n");
54 for kind in syntax_kinds.iter() {
55 write!(acc, " {},\n", scream(kind)).unwrap();
56 }
57 acc.push_str("\n");
58 acc.push_str(" // Technical SyntaxKinds: they appear temporally during parsing,\n");
59 acc.push_str(" // but never end up in the final tree\n");
60 acc.push_str(" #[doc(hidden)]\n");
61 acc.push_str(" TOMBSTONE,\n");
62 acc.push_str(" #[doc(hidden)]\n");
63 acc.push_str(" EOF,\n");
64 acc.push_str("}\n");
65 acc.push_str("pub(crate) use self::SyntaxKind::*;\n");
66 acc.push_str("\n");
67
68 // fn info
69 acc.push_str("impl SyntaxKind {\n");
70 acc.push_str(" pub(crate) fn info(self) -> &'static SyntaxInfo {\n");
71 acc.push_str(" match self {\n");
72 for kind in syntax_kinds.iter() {
73 let sname = scream(kind);
74 write!(
75 acc,
76 " {sname} => &SyntaxInfo {{ name: \"{sname}\" }},\n",
77 sname = sname
78 ).unwrap();
79 }
80 acc.push_str("\n");
81 acc.push_str(" TOMBSTONE => &SyntaxInfo { name: \"TOMBSTONE\" },\n");
82 acc.push_str(" EOF => &SyntaxInfo { name: \"EOF\" },\n");
83 acc.push_str(" }\n");
84 acc.push_str(" }\n");
85
86 // fn from_keyword
87 acc.push_str(" pub(crate) fn from_keyword(ident: &str) -> Option<SyntaxKind> {\n");
88 acc.push_str(" match ident {\n");
89 // NB: no contextual_keywords here!
90 for kw in self.keywords.iter() {
91 write!(acc, " {:?} => Some({}),\n", kw, kw_token(kw)).unwrap();
92 }
93 acc.push_str(" _ => None,\n");
94 acc.push_str(" }\n");
95 acc.push_str(" }\n");
96 acc.push_str("}\n");
97 acc.push_str("\n");
98 acc
99 }
100}
101
102fn grammar_file() -> PathBuf {
103 base_dir().join("src/grammar.ron")
104}
105
106fn generated_file() -> PathBuf {
107 base_dir().join("src/syntax_kinds/generated.rs")
108}
109
110fn scream(word: &str) -> String {
111 word.chars().map(|c| c.to_ascii_uppercase()).collect()
112}
113
114fn kw_token(keyword: &str) -> String {
115 format!("{}_KW", scream(keyword))
116}
117
118fn base_dir() -> PathBuf {
119 let dir = env!("CARGO_MANIFEST_DIR");
120 PathBuf::from(dir).parent().unwrap().to_owned()
121}