aboutsummaryrefslogtreecommitdiff
path: root/xtask/src
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src')
-rw-r--r--xtask/src/ast_src.rs6
-rw-r--r--xtask/src/codegen/gen_syntax.rs11
-rw-r--r--xtask/src/metrics.rs132
3 files changed, 22 insertions, 127 deletions
diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs
index 1386fc4e7..adc191254 100644
--- a/xtask/src/ast_src.rs
+++ b/xtask/src/ast_src.rs
@@ -113,12 +113,12 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc {
113 "TUPLE_TYPE", 113 "TUPLE_TYPE",
114 "NEVER_TYPE", 114 "NEVER_TYPE",
115 "PATH_TYPE", 115 "PATH_TYPE",
116 "POINTER_TYPE", 116 "PTR_TYPE",
117 "ARRAY_TYPE", 117 "ARRAY_TYPE",
118 "SLICE_TYPE", 118 "SLICE_TYPE",
119 "REFERENCE_TYPE", 119 "REF_TYPE",
120 "INFER_TYPE", 120 "INFER_TYPE",
121 "FN_POINTER_TYPE", 121 "FN_PTR_TYPE",
122 "FOR_TYPE", 122 "FOR_TYPE",
123 "IMPL_TRAIT_TYPE", 123 "IMPL_TRAIT_TYPE",
124 "DYN_TRAIT_TYPE", 124 "DYN_TRAIT_TYPE",
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index 4602ff1d7..cafad8070 100644
--- a/xtask/src/codegen/gen_syntax.rs
+++ b/xtask/src/codegen/gen_syntax.rs
@@ -10,7 +10,7 @@ use std::{
10 10
11use proc_macro2::{Punct, Spacing}; 11use proc_macro2::{Punct, Spacing};
12use quote::{format_ident, quote}; 12use quote::{format_ident, quote};
13use ungrammar::{Grammar, Rule}; 13use ungrammar::{rust_grammar, Grammar, Rule};
14 14
15use crate::{ 15use crate::{
16 ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC}, 16 ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC},
@@ -19,9 +19,7 @@ use crate::{
19}; 19};
20 20
21pub fn generate_syntax(mode: Mode) -> Result<()> { 21pub fn generate_syntax(mode: Mode) -> Result<()> {
22 let grammar = include_str!("rust.ungram") 22 let grammar = rust_grammar();
23 .parse::<Grammar>()
24 .unwrap_or_else(|err| panic!("\n \x1b[91merror\x1b[0m: {}\n", err));
25 let ast = lower(&grammar); 23 let ast = lower(&grammar);
26 24
27 let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS); 25 let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS);
@@ -538,6 +536,7 @@ fn lower_enum(grammar: &Grammar, rule: &Rule) -> Option<Vec<String>> {
538 for alternative in alternatives { 536 for alternative in alternatives {
539 match alternative { 537 match alternative {
540 Rule::Node(it) => variants.push(grammar[*it].name.clone()), 538 Rule::Node(it) => variants.push(grammar[*it].name.clone()),
539 Rule::Token(it) if grammar[*it].name == ";" => (),
541 _ => return None, 540 _ => return None,
542 } 541 }
543 } 542 }
@@ -591,8 +590,8 @@ fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, label: Option<&String>, r
591 | "index" 590 | "index"
592 | "base" 591 | "base"
593 | "value" 592 | "value"
594 | "target_type" 593 | "trait"
595 | "target_trait" 594 | "self_ty"
596 ); 595 );
597 if manually_implemented { 596 if manually_implemented {
598 return; 597 return;
diff --git a/xtask/src/metrics.rs b/xtask/src/metrics.rs
index 9ac3fa51d..4bade2c7e 100644
--- a/xtask/src/metrics.rs
+++ b/xtask/src/metrics.rs
@@ -1,7 +1,6 @@
1use std::{ 1use std::{
2 collections::BTreeMap, 2 collections::BTreeMap,
3 env, 3 env,
4 fmt::{self, Write as _},
5 io::Write as _, 4 io::Write as _,
6 path::Path, 5 path::Path,
7 time::{Instant, SystemTime, UNIX_EPOCH}, 6 time::{Instant, SystemTime, UNIX_EPOCH},
@@ -127,40 +126,21 @@ impl Metrics {
127 self.metrics.insert(name.into(), (value, unit)); 126 self.metrics.insert(name.into(), (value, unit));
128 } 127 }
129 128
130 fn json(&self) -> Json { 129 fn json(&self) -> String {
131 let mut json = Json::default(); 130 let mut buf = String::new();
132 self.to_json(&mut json); 131 self.to_json(write_json::object(&mut buf));
133 json 132 buf
134 } 133 }
135 fn to_json(&self, json: &mut Json) {
136 json.begin_object();
137 {
138 json.field("host");
139 self.host.to_json(json);
140
141 json.field("timestamp");
142 let timestamp = self.timestamp.duration_since(UNIX_EPOCH).unwrap();
143 json.number(timestamp.as_secs() as f64);
144 134
145 json.field("revision"); 135 fn to_json(&self, mut obj: write_json::Object<'_>) {
146 json.string(&self.revision); 136 self.host.to_json(obj.object("host"));
147 137 let timestamp = self.timestamp.duration_since(UNIX_EPOCH).unwrap();
148 json.field("metrics"); 138 obj.number("timestamp", timestamp.as_secs() as f64);
149 json.begin_object(); 139 obj.string("revision", &self.revision);
150 { 140 let mut metrics = obj.object("metrics");
151 for (k, (value, unit)) in &self.metrics { 141 for (k, (value, unit)) in &self.metrics {
152 json.field(k); 142 metrics.array(k).number(*value as f64).string(unit);
153 json.begin_array();
154 {
155 json.number(*value as f64);
156 json.string(unit);
157 }
158 json.end_array();
159 }
160 }
161 json.end_object()
162 } 143 }
163 json.end_object();
164 } 144 }
165} 145}
166 146
@@ -189,91 +169,7 @@ impl Host {
189 Ok(line[field.len()..].trim().to_string()) 169 Ok(line[field.len()..].trim().to_string())
190 } 170 }
191 } 171 }
192 fn to_json(&self, json: &mut Json) { 172 fn to_json(&self, mut obj: write_json::Object<'_>) {
193 json.begin_object(); 173 obj.string("os", &self.os).string("cpu", &self.cpu).string("mem", &self.mem);
194 {
195 json.field("os");
196 json.string(&self.os);
197
198 json.field("cpu");
199 json.string(&self.cpu);
200
201 json.field("mem");
202 json.string(&self.mem);
203 }
204 json.end_object();
205 }
206}
207
208struct State {
209 obj: bool,
210 first: bool,
211}
212
213#[derive(Default)]
214struct Json {
215 stack: Vec<State>,
216 buf: String,
217}
218
219impl Json {
220 fn begin_object(&mut self) {
221 self.stack.push(State { obj: true, first: true });
222 self.buf.push('{');
223 }
224 fn end_object(&mut self) {
225 self.stack.pop();
226 self.buf.push('}')
227 }
228 fn begin_array(&mut self) {
229 self.stack.push(State { obj: false, first: true });
230 self.buf.push('[');
231 }
232 fn end_array(&mut self) {
233 self.stack.pop();
234 self.buf.push(']')
235 }
236 fn field(&mut self, name: &str) {
237 self.object_comma();
238 self.string_token(name);
239 self.buf.push(':');
240 }
241 fn string(&mut self, value: &str) {
242 self.array_comma();
243 self.string_token(value);
244 }
245 fn string_token(&mut self, value: &str) {
246 self.buf.push('"');
247 self.buf.extend(value.escape_default());
248 self.buf.push('"');
249 }
250 fn number(&mut self, value: f64) {
251 self.array_comma();
252 write!(self.buf, "{}", value).unwrap();
253 }
254
255 fn array_comma(&mut self) {
256 let state = self.stack.last_mut().unwrap();
257 if state.obj {
258 return;
259 }
260 if !state.first {
261 self.buf.push(',');
262 }
263 state.first = false;
264 }
265
266 fn object_comma(&mut self) {
267 let state = self.stack.last_mut().unwrap();
268 if !state.first {
269 self.buf.push(',');
270 }
271 state.first = false;
272 }
273}
274
275impl fmt::Display for Json {
276 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
277 write!(f, "{}", self.buf)
278 } 174 }
279} 175}