git.delta.rocks / jrsonnet / refs/commits / afe1286f67ce

difftreelog

style fix clippy warnings

Yaroslav Bolyukin2024-06-18parent: #e3e6483.patch.diff
in: master

19 files changed

modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
64 self.ctx,64 self.ctx,
65 base.as_ptr(),65 base.as_ptr(),
66 rel.as_ptr(),66 rel.as_ptr(),
67 &mut (found_here as *const _),67 &mut found_here.cast_const(),
68 &mut buf,68 &mut buf,
69 &mut buf_len,69 &mut buf_len,
70 )70 )
121 cb,121 cb,
122 ctx,122 ctx,
123 out: RefCell::new(HashMap::new()),123 out: RefCell::new(HashMap::new()),
124 })124 });
125}125}
126126
127/// # Safety127/// # Safety
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
86 let state = State::default();86 let state = State::default();
87 state.settings_mut().import_resolver = tb!(FileImportResolver::default());87 state.settings_mut().import_resolver = tb!(FileImportResolver::default());
88 state.settings_mut().context_initializer = tb!(jrsonnet_stdlib::ContextInitializer::new(88 state.settings_mut().context_initializer = tb!(jrsonnet_stdlib::ContextInitializer::new(
89 state.clone(),
90 PathResolver::new_cwd_fallback(),89 PathResolver::new_cwd_fallback(),
91 ));90 ));
92 Box::into_raw(Box::new(VM {91 Box::into_raw(Box::new(VM {
107/// Set the maximum stack depth.106/// Set the maximum stack depth.
108#[no_mangle]107#[no_mangle]
109pub extern "C" fn jsonnet_max_stack(_vm: &VM, v: c_uint) {108pub extern "C" fn jsonnet_max_stack(_vm: &VM, v: c_uint) {
110 set_stack_depth_limit(v as usize)109 set_stack_depth_limit(v as usize);
111}110}
112111
113/// Set the number of objects required before a garbage collection cycle is allowed.112/// Set the number of objects required before a garbage collection cycle is allowed.
175#[no_mangle]174#[no_mangle]
176pub extern "C" fn jsonnet_max_trace(vm: &mut VM, v: c_uint) {175pub extern "C" fn jsonnet_max_trace(vm: &mut VM, v: c_uint) {
177 if let Some(format) = vm.trace_format.as_any_mut().downcast_mut::<CompactFormat>() {176 if let Some(format) = vm.trace_format.as_any_mut().downcast_mut::<CompactFormat>() {
178 format.max_trace = v as usize177 format.max_trace = v as usize;
179 } else {178 } else {
180 panic!("max_trace is not supported by current tracing format")179 panic!("max_trace is not supported by current tracing format")
181 }180 }
182}181}
183182
184/// Evaluate a file containing Jsonnet code, return a JSON string.183/// Evaluate a file containing Jsonnet code, return a JSON string.
185///184///
186/// The returned string should be cleaned up with jsonnet_realloc.185/// The returned string should be cleaned up with `jsonnet_realloc`.
187///186///
188/// # Safety187/// # Safety
189///188///
216215
217/// Evaluate a string containing Jsonnet code, return a JSON string.216/// Evaluate a string containing Jsonnet code, return a JSON string.
218///217///
219/// The returned string should be cleaned up with jsonnet_realloc.218/// The returned string should be cleaned up with `jsonnet_realloc`.
220///219///
221/// # Safety220/// # Safety
222///221///
359 out.push(0);358 out.push(0);
360 let v = out.as_ptr();359 let v = out.as_ptr();
361 std::mem::forget(out);360 std::mem::forget(out);
362 v as *const c_char361 v.cast::<c_char>()
363}362}
364363
365/// # Safety364/// # Safety
modifiedbindings/jsonnet/src/val_extract.rsdiffbeforeafterboth
26pub extern "C" fn jsonnet_json_extract_number(_vm: &VM, v: &Val, out: &mut c_double) -> c_int {26pub extern "C" fn jsonnet_json_extract_number(_vm: &VM, v: &Val, out: &mut c_double) -> c_int {
27 match v {27 match v {
28 Val::Num(n) => {28 Val::Num(n) => {
29 *out = *n;29 *out = n.get();
30 130 1
31 }31 }
32 _ => 0,32 _ => 0,
modifiedbindings/jsonnet/src/val_make.rsdiffbeforeafterboth
6};6};
77
8use jrsonnet_evaluator::{val::ArrValue, ObjValue, Val};8use jrsonnet_evaluator::{
9 val::{ArrValue, NumValue},
10 ObjValue, Val,
11};
912
10use crate::VM;13use crate::VM;
25#[no_mangle]28#[no_mangle]
26pub extern "C" fn jsonnet_json_make_number(_vm: &VM, v: c_double) -> *mut Val {29pub extern "C" fn jsonnet_json_make_number(_vm: &VM, v: c_double) -> *mut Val {
27 Box::into_raw(Box::new(Val::Num(v)))30 Box::into_raw(Box::new(Val::Num(
31 NumValue::new(v).expect("jsonnet numbers are finite"),
32 )))
28}33}
2934
modifiedbindings/jsonnet/src/val_modify.rsdiffbeforeafterboth
12///12///
13/// # Safety13/// # Safety
14///14///
15/// `arr` should be a pointer to array value allocated by make_array, or returned by other library call15/// `arr` should be a pointer to array value allocated by `make_array`, or returned by other library call
16/// `val` should be a pointer to value allocated using this library16/// `val` should be a pointer to value allocated using this library
17#[no_mangle]17#[no_mangle]
18pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &VM, arr: &mut Val, val: &Val) {18pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &VM, arr: &mut Val, val: &Val) {
modifiedbindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth
27 .add_ext_str(27 .add_ext_str(
28 name.to_str().expect("name is not utf-8").into(),28 name.to_str().expect("name is not utf-8").into(),
29 value.to_str().expect("value is not utf-8").into(),29 value.to_str().expect("value is not utf-8").into(),
30 )30 );
31}31}
3232
33/// Binds a Jsonnet external variable to the given code.33/// Binds a Jsonnet external variable to the given code.
51 name.to_str().expect("name is not utf-8"),51 name.to_str().expect("name is not utf-8"),
52 code.to_str().expect("code is not utf-8"),52 code.to_str().expect("code is not utf-8"),
53 )53 )
54 .expect("can't parse ext code")54 .expect("can't parse ext code");
55}55}
5656
57/// Binds a top-level string argument for a top-level parameter.57/// Binds a top-level string argument for a top-level parameter.
modifiedcmds/jrsonnet-fmt/src/children.rsdiffbeforeafterboth
28 TS![, ;].contains(item.kind()),28 TS![, ;].contains(item.kind()),
29 "silently eaten token: {:?}",29 "silently eaten token: {:?}",
30 item.kind()30 item.kind()
31 )31 );
32 }32 }
33 }33 }
34 out34 out
48 if let Some(trivia) = item.as_token().cloned().and_then(Trivia::cast) {48 if let Some(trivia) = item.as_token().cloned().and_then(Trivia::cast) {
49 out.push(Ok(trivia));49 out.push(Ok(trivia));
50 } else if CustomError::can_cast(item.kind()) {50 } else if CustomError::can_cast(item.kind()) {
51 out.push(Err(item.to_string()))51 out.push(Err(item.to_string()));
52 } else {52 } else {
53 assert!(53 assert!(
54 TS![, ;].contains(item.kind()),54 TS![, ;].contains(item.kind()),
55 "silently eaten token: {:?}",55 "silently eaten token: {:?}",
56 item.kind()56 item.kind()
57 )57 );
58 }58 }
59 }59 }
60 out60 out
115 TriviaKind::Whitespace => {115 TriviaKind::Whitespace => {
116 nl_count += t.text().bytes().filter(|b| *b == b'\n').count();116 nl_count += t.text().bytes().filter(|b| *b == b'\n').count();
117 }117 }
118 TriviaKind::SingleLineHashComment => {118 TriviaKind::SingleLineHashComment | TriviaKind::SingleLineSlashComment => {
119 nl_count += 1;
120 break;
121 }
122 TriviaKind::SingleLineSlashComment => {
123 nl_count += 1;119 nl_count += 1;
124 break;120 break;
125 }121 }
163 inline_trivia: Vec::new(),159 inline_trivia: Vec::new(),
164 });160 });
165 if let Some(last_child) = last_child {161 if let Some(last_child) = last_child {
166 out.push(last_child)162 out.push(last_child);
167 }163 }
168 had_some = true;164 had_some = true;
169 started_next = false;165 started_next = false;
188 }184 }
189 had_some = true;185 had_some = true;
190 } else if CustomError::can_cast(item.kind()) {186 } else if CustomError::can_cast(item.kind()) {
191 next.push(Err(item.to_string()))187 next.push(Err(item.to_string()));
192 } else if loose {188 } else if loose {
193 if had_some {189 if had_some {
194 break;190 break;
199 TS![, ;].contains(item.kind()),195 TS![, ;].contains(item.kind()),
200 "silently eaten token: {:?}",196 "silently eaten token: {:?}",
201 item.kind()197 item.kind()
202 )198 );
203 }199 }
204 }200 }
205201
modifiedcmds/jrsonnet-fmt/src/comments.rsdiffbeforeafterboth
1use std::string::String;
2
1use dprint_core::formatting::PrintItems;3use dprint_core::formatting::PrintItems;
2use jrsonnet_rowan_parser::{nodes::TriviaKind, AstToken};4use jrsonnet_rowan_parser::{nodes::TriviaKind, AstToken};
12 EndOfItems,14 EndOfItems,
13}15}
1416
17#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
15pub fn format_comments(comments: &ChildTrivia, loc: CommentLocation, out: &mut PrintItems) {18pub fn format_comments(comments: &ChildTrivia, loc: CommentLocation, out: &mut PrintItems) {
16 for c in comments {19 for c in comments {
17 let Ok(c) = c else {20 let Ok(c) = c else {
62 }65 }
63 })66 })
64 .collect::<Vec<_>>();67 .collect::<Vec<_>>();
65 while lines.last().map(|l| l.is_empty()).unwrap_or(false) {68 while lines.last().is_some_and(String::is_empty) {
66 lines.pop();69 lines.pop();
67 }70 }
68 if lines.len() == 1 && !doc {71 if lines.len() == 1 && !doc {
69 if matches!(loc, CommentLocation::ItemInline) {72 if matches!(loc, CommentLocation::ItemInline) {
70 p!(out, str(" "));73 p!(out, str(" "));
71 }74 }
72 p!(out, str("/* ") string(lines[0].trim().to_string()) str(" */") nl)75 p!(out, str("/* ") string(lines[0].trim().to_string()) str(" */") nl);
73 } else if !lines.is_empty() {76 } else if !lines.is_empty() {
74 fn common_ws_prefix<'a>(a: &'a str, b: &str) -> &'a str {77 fn common_ws_prefix<'a>(a: &'a str, b: &str) -> &'a str {
75 let offset = a78 let offset = a
95 }98 }
96 for line in lines99 for line in lines
97 .iter_mut()100 .iter_mut()
98 .skip(if immediate_start { 1 } else { 0 })101 .skip(usize::from(immediate_start))
99 .filter(|l| !l.is_empty())102 .filter(|l| !l.is_empty())
100 {103 {
101 *line = line104 *line = line
127 }130 }
128 line = new_line.to_string();131 line = new_line.to_string();
129 }132 }
130 p!(out, string(line.to_string()) nl)133 p!(out, string(line.to_string()) nl);
131 }134 }
132 }135 }
133 if doc {136 if doc {
134 p!(out, str(" "));137 p!(out, str(" "));
135 }138 }
136 p!(out, str("*/") nl)139 p!(out, str("*/") nl);
137 }140 }
138 }141 }
139 // TODO: Keep common padding for multiple continous lines of single-line comments142 // TODO: Keep common padding for multiple continous lines of single-line comments
154 // ```157 // ```
155 TriviaKind::SingleLineHashComment => {158 TriviaKind::SingleLineHashComment => {
156 if matches!(loc, CommentLocation::ItemInline) {159 if matches!(loc, CommentLocation::ItemInline) {
157 p!(out, str(" "))160 p!(out, str(" "));
158 }161 }
159 p!(out, str("# ") string(c.text().strip_prefix('#').expect("hash comment starts with #").trim().to_string()));162 p!(out, str("# ") string(c.text().strip_prefix('#').expect("hash comment starts with #").trim().to_string()));
160 if !matches!(loc, CommentLocation::ItemInline) {163 if !matches!(loc, CommentLocation::ItemInline) {
161 p!(out, nl)164 p!(out, nl);
162 }165 }
163 }166 }
164 TriviaKind::SingleLineSlashComment => {167 TriviaKind::SingleLineSlashComment => {
165 if matches!(loc, CommentLocation::ItemInline) {168 if matches!(loc, CommentLocation::ItemInline) {
166 p!(out, str(" "))169 p!(out, str(" "));
167 }170 }
168 p!(out, str("// ") string(c.text().strip_prefix("//").expect("comment starts with //").trim().to_string()));171 p!(out, str("// ") string(c.text().strip_prefix("//").expect("comment starts with //").trim().to_string()));
169 if !matches!(loc, CommentLocation::ItemInline) {172 if !matches!(loc, CommentLocation::ItemInline) {
170 p!(out, nl)173 p!(out, nl);
171 }174 }
172 }175 }
173 // Garbage in - garbage out176 // Garbage in - garbage out
modifiedcmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth
13 condition_helpers::is_multiple_lines, condition_resolvers::true_resolver,13 condition_helpers::is_multiple_lines, condition_resolvers::true_resolver,
14 ConditionResolverContext, LineNumber, PrintItems, PrintOptions,14 ConditionResolverContext, LineNumber, PrintItems, PrintOptions,
15};15};
16use hi_doc::Formatting;
16use jrsonnet_rowan_parser::{17use jrsonnet_rowan_parser::{
17 nodes::{18 nodes::{
18 Arg, ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart,19 Arg, ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart,
155{156{
156 fn print(&self, out: &mut PrintItems) {157 fn print(&self, out: &mut PrintItems) {
157 if let Some(v) = self {158 if let Some(v) = self {
158 v.print(out)159 v.print(out);
159 } else {160 } else {
160 p!(161 p!(
161 out,162 out,
162 string(format!(163 string(format!(
163 "/*missing {}*/",164 "/*missing {}*/",
164 type_name::<P>().replace("jrsonnet_rowan_parser::generated::nodes::", "")165 type_name::<P>().replace("jrsonnet_rowan_parser::generated::nodes::", "")
165 ),)166 ),)
166 )167 );
167 }168 }
168 }169 }
169}170}
170171
171impl Printable for SyntaxToken {172impl Printable for SyntaxToken {
172 fn print(&self, out: &mut PrintItems) {173 fn print(&self, out: &mut PrintItems) {
173 p!(out, string(self.to_string()))174 p!(out, string(self.to_string()));
174 }175 }
175}176}
176177
177impl Printable for Text {178impl Printable for Text {
178 fn print(&self, out: &mut PrintItems) {179 fn print(&self, out: &mut PrintItems) {
179 p!(out, string(format!("{}", self)))180 p!(out, string(format!("{}", self)));
180 }181 }
181}182}
182impl Printable for Number {183impl Printable for Number {
183 fn print(&self, out: &mut PrintItems) {184 fn print(&self, out: &mut PrintItems) {
184 p!(out, string(format!("{}", self)))185 p!(out, string(format!("{}", self)));
185 }186 }
186}187}
187188
188impl Printable for Name {189impl Printable for Name {
189 fn print(&self, out: &mut PrintItems) {190 fn print(&self, out: &mut PrintItems) {
190 p!(out, { self.ident_lit() })191 p!(out, { self.ident_lit() });
191 }192 }
192}193}
193194
203impl Printable for Destruct {204impl Printable for Destruct {
204 fn print(&self, out: &mut PrintItems) {205 fn print(&self, out: &mut PrintItems) {
205 match self {206 match self {
206 Destruct::DestructFull(f) => {207 Self::DestructFull(f) => {
207 p!(out, { f.name() })208 p!(out, { f.name() });
208 }209 }
209 Destruct::DestructSkip(_) => p!(out, str("?")),210 Self::DestructSkip(_) => p!(out, str("?")),
210 Destruct::DestructArray(a) => {211 Self::DestructArray(a) => {
211 p!(out, str("[") >i nl);212 p!(out, str("[") >i nl);
212 for el in a.destruct_array_parts() {213 for el in a.destruct_array_parts() {
213 match el {214 match el {
214 DestructArrayPart::DestructArrayElement(e) => {215 DestructArrayPart::DestructArrayElement(e) => {
215 p!(out, {e.destruct()} str(",") nl)216 p!(out, {e.destruct()} str(",") nl);
216 }217 }
217 DestructArrayPart::DestructRest(d) => {218 DestructArrayPart::DestructRest(d) => {
218 p!(out, {d} str(",") nl)219 p!(out, {d} str(",") nl);
219 }220 }
220 }221 }
221 }222 }
222 p!(out, <i str("]"));223 p!(out, <i str("]"));
223 }224 }
224 Destruct::DestructObject(o) => {225 Self::DestructObject(o) => {
225 p!(out, str("{") >i nl);226 p!(out, str("{") >i nl);
226 for item in o.destruct_object_fields() {227 for item in o.destruct_object_fields() {
227 p!(out, { item.field() });228 p!(out, { item.field() });
228 if let Some(des) = item.destruct() {229 if let Some(des) = item.destruct() {
229 p!(out, str(": ") {des})230 p!(out, str(": ") {des});
230 }231 }
231 if let Some(def) = item.expr() {232 if let Some(def) = item.expr() {
232 p!(out, str(" = ") {def});233 p!(out, str(" = ") {def});
233 }234 }
234 p!(out, str(",") nl);235 p!(out, str(",") nl);
235 }236 }
236 if let Some(rest) = o.destruct_rest() {237 if let Some(rest) = o.destruct_rest() {
237 p!(out, {rest} nl)238 p!(out, {rest} nl);
238 }239 }
239 p!(out, <i str("}"));240 p!(out, <i str("}"));
240 }241 }
245impl Printable for FieldName {246impl Printable for FieldName {
246 fn print(&self, out: &mut PrintItems) {247 fn print(&self, out: &mut PrintItems) {
247 match self {248 match self {
248 FieldName::FieldNameFixed(f) => {249 Self::FieldNameFixed(f) => {
249 if let Some(id) = f.id() {250 if let Some(id) = f.id() {
250 p!(out, { id })251 p!(out, { id });
251 } else if let Some(str) = f.text() {252 } else if let Some(str) = f.text() {
252 p!(out, { str })253 p!(out, { str });
253 } else {254 } else {
254 p!(out, str("/*missing FieldName*/"))255 p!(out, str("/*missing FieldName*/"));
255 }256 }
256 }257 }
257 FieldName::FieldNameDynamic(d) => {258 Self::FieldNameDynamic(d) => {
258 p!(out, str("[") {d.expr()} str("]"))259 p!(out, str("[") {d.expr()} str("]"));
259 }260 }
260 }261 }
261 }262 }
262}263}
263264
264impl Printable for Visibility {265impl Printable for Visibility {
265 fn print(&self, out: &mut PrintItems) {266 fn print(&self, out: &mut PrintItems) {
266 p!(out, string(self.to_string()))267 p!(out, string(self.to_string()));
267 }268 }
268}269}
269270
270impl Printable for ObjLocal {271impl Printable for ObjLocal {
271 fn print(&self, out: &mut PrintItems) {272 fn print(&self, out: &mut PrintItems) {
272 p!(out, str("local ") {self.bind()})273 p!(out, str("local ") {self.bind()});
273 }274 }
274}275}
275276
276impl Printable for Assertion {277impl Printable for Assertion {
277 fn print(&self, out: &mut PrintItems) {278 fn print(&self, out: &mut PrintItems) {
278 p!(out, str("assert ") {self.condition()});279 p!(out, str("assert ") {self.condition()});
279 if self.colon_token().is_some() || self.message().is_some() {280 if self.colon_token().is_some() || self.message().is_some() {
280 p!(out, str(": ") {self.message()})281 p!(out, str(": ") {self.message()});
281 }282 }
282 }283 }
283}284}
288 for param in self.params() {289 for param in self.params() {
289 p!(out, { param.destruct() });290 p!(out, { param.destruct() });
290 if param.assign_token().is_some() || param.expr().is_some() {291 if param.assign_token().is_some() || param.expr().is_some() {
291 p!(out, str(" = ") {param.expr()})292 p!(out, str(" = ") {param.expr()});
292 }293 }
293 p!(out, str(",") nl)294 p!(out, str(",") nl);
294 }295 }
295 p!(out, <i str(")"));296 p!(out, <i str(")"));
296 }297 }
343 }344 }
344 p!(out, str(":"));345 p!(out, str(":"));
345 if self.end().is_some() {346 if self.end().is_some() {
346 p!(out, { self.end().map(|e| e.expr()) })347 p!(out, { self.end().map(|e| e.expr()) });
347 }348 }
348 // Keep only one : in case if we don't need step349 // Keep only one : in case if we don't need step
349 if self.step().is_some() {350 if self.step().is_some() {
357 fn print(&self, out: &mut PrintItems) {358 fn print(&self, out: &mut PrintItems) {
358 match self {359 match self {
359 Self::MemberBindStmt(b) => {360 Self::MemberBindStmt(b) => {
360 p!(out, { b.obj_local() })361 p!(out, { b.obj_local() });
361 }362 }
362 Self::MemberAssertStmt(ass) => {363 Self::MemberAssertStmt(ass) => {
363 p!(out, { ass.assertion() })364 p!(out, { ass.assertion() });
364 }365 }
365 Self::MemberFieldNormal(n) => {366 Self::MemberFieldNormal(n) => {
366 p!(out, {n.field_name()} if(n.plus_token().is_some())({n.plus_token()}) {n.visibility()} str(" ") {n.expr()})367 p!(out, {n.field_name()} if(n.plus_token().is_some())({n.plus_token()}) {n.visibility()} str(" ") {n.expr()});
367 }368 }
368 Self::MemberFieldMethod(m) => {369 Self::MemberFieldMethod(m) => {
369 p!(out, {m.field_name()} {m.params_desc()} {m.visibility()} str(" ") {m.expr()})370 p!(out, {m.field_name()} {m.params_desc()} {m.visibility()} str(" ") {m.expr()});
370 }371 }
371 }372 }
372 }373 }
375impl Printable for ObjBody {376impl Printable for ObjBody {
376 fn print(&self, out: &mut PrintItems) {377 fn print(&self, out: &mut PrintItems) {
377 match self {378 match self {
378 ObjBody::ObjBodyComp(l) => {379 Self::ObjBodyComp(l) => {
379 let (children, mut end_comments) = children_between::<Member>(380 let (children, mut end_comments) = children_between::<Member>(
380 l.syntax().clone(),381 l.syntax().clone(),
381 l.l_brace_token().map(Into::into).as_ref(),382 l.l_brace_token().map(Into::into).as_ref(),
391 );392 );
392 let trailing_for_comp = end_comments.extract_trailing();393 let trailing_for_comp = end_comments.extract_trailing();
393 p!(out, str("{") >i nl);394 p!(out, str("{") >i nl);
394 for mem in children.into_iter() {395 for mem in children {
395 if mem.should_start_with_newline {396 if mem.should_start_with_newline {
396 p!(out, nl);397 p!(out, nl);
397 }398 }
398 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);399 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);
399 p!(out, {mem.value} str(","));400 p!(out, {mem.value} str(","));
400 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);401 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);
401 p!(out, nl)402 p!(out, nl);
402 }403 }
403404
404 if end_comments.should_start_with_newline {405 if end_comments.should_start_with_newline {
417 l.r_brace_token().map(Into::into).as_ref(),418 l.r_brace_token().map(Into::into).as_ref(),
418 Some(trailing_for_comp),419 Some(trailing_for_comp),
419 );420 );
420 for mem in compspecs.into_iter() {421 for mem in compspecs {
421 if mem.should_start_with_newline {422 if mem.should_start_with_newline {
422 p!(out, nl);423 p!(out, nl);
423 }424 }
432433
433 p!(out, nl <i str("}"));434 p!(out, nl <i str("}"));
434 }435 }
435 ObjBody::ObjBodyMemberList(l) => {436 Self::ObjBodyMemberList(l) => {
436 let (children, end_comments) = children_between::<Member>(437 let (children, end_comments) = children_between::<Member>(
437 l.syntax().clone(),438 l.syntax().clone(),
438 l.l_brace_token().map(Into::into).as_ref(),439 l.l_brace_token().map(Into::into).as_ref(),
451 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);452 format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);
452 p!(out, {mem.value} str(","));453 p!(out, {mem.value} str(","));
453 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);454 format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);
454 p!(out, nl)455 p!(out, nl);
455 }456 }
456457
457 if end_comments.should_start_with_newline {458 if end_comments.should_start_with_newline {
465}466}
466impl Printable for UnaryOperator {467impl Printable for UnaryOperator {
467 fn print(&self, out: &mut PrintItems) {468 fn print(&self, out: &mut PrintItems) {
468 p!(out, string(self.text().to_string()))469 p!(out, string(self.text().to_string()));
469 }470 }
470}471}
471impl Printable for BinaryOperator {472impl Printable for BinaryOperator {
472 fn print(&self, out: &mut PrintItems) {473 fn print(&self, out: &mut PrintItems) {
473 p!(out, string(self.text().to_string()))474 p!(out, string(self.text().to_string()));
474 }475 }
475}476}
476impl Printable for Bind {477impl Printable for Bind {
477 fn print(&self, out: &mut PrintItems) {478 fn print(&self, out: &mut PrintItems) {
478 match self {479 match self {
479 Bind::BindDestruct(d) => {480 Self::BindDestruct(d) => {
480 p!(out, {d.into()} str(" = ") {d.value()})481 p!(out, {d.into()} str(" = ") {d.value()});
481 }482 }
482 Bind::BindFunction(f) => {483 Self::BindFunction(f) => {
483 p!(out, {f.name()} {f.params()} str(" = ") {f.value()})484 p!(out, {f.name()} {f.params()} str(" = ") {f.value()});
484 }485 }
485 }486 }
486 }487 }
487}488}
488impl Printable for Literal {489impl Printable for Literal {
489 fn print(&self, out: &mut PrintItems) {490 fn print(&self, out: &mut PrintItems) {
490 p!(out, string(self.syntax().to_string()))491 p!(out, string(self.syntax().to_string()));
491 }492 }
492}493}
493impl Printable for ImportKind {494impl Printable for ImportKind {
494 fn print(&self, out: &mut PrintItems) {495 fn print(&self, out: &mut PrintItems) {
495 p!(out, string(self.syntax().to_string()))496 p!(out, string(self.syntax().to_string()));
496 }497 }
497}498}
498impl Printable for ForSpec {499impl Printable for ForSpec {
499 fn print(&self, out: &mut PrintItems) {500 fn print(&self, out: &mut PrintItems) {
500 p!(out, str("for ") {self.bind()} str(" in ") {self.expr()})501 p!(out, str("for ") {self.bind()} str(" in ") {self.expr()});
501 }502 }
502}503}
503impl Printable for IfSpec {504impl Printable for IfSpec {
504 fn print(&self, out: &mut PrintItems) {505 fn print(&self, out: &mut PrintItems) {
505 p!(out, str("if ") {self.expr()})506 p!(out, str("if ") {self.expr()});
506 }507 }
507}508}
508impl Printable for CompSpec {509impl Printable for CompSpec {
509 fn print(&self, out: &mut PrintItems) {510 fn print(&self, out: &mut PrintItems) {
510 match self {511 match self {
511 CompSpec::ForSpec(f) => f.print(out),512 Self::ForSpec(f) => f.print(out),
512 CompSpec::IfSpec(i) => i.print(out),513 Self::IfSpec(i) => i.print(out),
513 }514 }
514 }515 }
515}516}
549impl Printable for Suffix {550impl Printable for Suffix {
550 fn print(&self, out: &mut PrintItems) {551 fn print(&self, out: &mut PrintItems) {
551 match self {552 match self {
552 Suffix::SuffixIndex(i) => {553 Self::SuffixIndex(i) => {
553 if i.question_mark_token().is_some() {554 if i.question_mark_token().is_some() {
554 p!(out, str("?"));555 p!(out, str("?"));
555 }556 }
556 p!(out, str(".") {i.index()});557 p!(out, str(".") {i.index()});
557 }558 }
558 Suffix::SuffixIndexExpr(e) => {559 Self::SuffixIndexExpr(e) => {
559 if e.question_mark_token().is_some() {560 if e.question_mark_token().is_some() {
560 p!(out, str(".?"));561 p!(out, str(".?"));
561 }562 }
562 p!(out, str("[") {e.index()} str("]"))563 p!(out, str("[") {e.index()} str("]"));
563 }564 }
564 Suffix::SuffixSlice(d) => {565 Self::SuffixSlice(d) => {
565 p!(out, { d.slice_desc() })566 p!(out, { d.slice_desc() });
566 }567 }
567 Suffix::SuffixApply(a) => {568 Self::SuffixApply(a) => {
568 p!(out, { a.args_desc() })569 p!(out, { a.args_desc() });
569 }570 }
570 }571 }
571 }572 }
572}573}
573impl Printable for Stmt {574impl Printable for Stmt {
574 fn print(&self, out: &mut PrintItems) {575 fn print(&self, out: &mut PrintItems) {
575 match self {576 match self {
576 Stmt::StmtLocal(l) => {577 Self::StmtLocal(l) => {
577 let (binds, end_comments) = children_between::<Bind>(578 let (binds, end_comments) = children_between::<Bind>(
578 l.syntax().clone(),579 l.syntax().clone(),
579 l.local_kw_token().map(Into::into).as_ref(),580 l.local_kw_token().map(Into::into).as_ref(),
594 format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);595 format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);
595 p!(out, {bind.value} str(","));596 p!(out, {bind.value} str(","));
596 format_comments(&bind.inline_trivia, CommentLocation::ItemInline, out);597 format_comments(&bind.inline_trivia, CommentLocation::ItemInline, out);
597 p!(out, nl)598 p!(out, nl);
598 }599 }
599 if end_comments.should_start_with_newline {600 if end_comments.should_start_with_newline {
600 p!(out, nl)601 p!(out, nl);
601 }602 }
602 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);603 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);
603 p!(out,<i);604 p!(out,<i);
604 }605 }
605 p!(out,str(";") nl);606 p!(out,str(";") nl);
606 }607 }
607 Stmt::StmtAssert(a) => {608 Self::StmtAssert(a) => {
608 p!(out, {a.assertion()} str(";") nl)609 p!(out, {a.assertion()} str(";") nl);
609 }610 }
610 }611 }
611 }612 }
614 fn print(&self, out: &mut PrintItems) {615 fn print(&self, out: &mut PrintItems) {
615 match self {616 match self {
616 Self::ExprBinary(b) => {617 Self::ExprBinary(b) => {
617 p!(out, {b.lhs_work()} str(" ") {b.binary_operator()} str(" ") {b.rhs_work()})618 p!(out, {b.lhs_work()} str(" ") {b.binary_operator()} str(" ") {b.rhs_work()});
618 }619 }
619 Self::ExprUnary(u) => p!(out, {u.unary_operator()} {u.rhs()}),620 Self::ExprUnary(u) => p!(out, {u.unary_operator()} {u.rhs()}),
620 // Self::ExprSlice(s) => {621 // Self::ExprSlice(s) => {
632 // pi633 // pi
633 // }634 // }
634 Self::ExprObjExtend(ex) => {635 Self::ExprObjExtend(ex) => {
635 p!(out, {ex.lhs_work()} str(" ") {ex.rhs_work()})636 p!(out, {ex.lhs_work()} str(" ") {ex.rhs_work()});
636 }637 }
637 Self::ExprParened(p) => {638 Self::ExprParened(p) => {
638 p!(out, str("(") {p.expr()} str(")"))639 p!(out, str("(") {p.expr()} str(")"));
639 }640 }
640 Self::ExprString(s) => p!(out, { s.text() }),641 Self::ExprString(s) => p!(out, { s.text() }),
641 Self::ExprNumber(n) => p!(out, { n.number() }),642 Self::ExprNumber(n) => p!(out, { n.number() }),
647 p!(out, <i str("]"));648 p!(out, <i str("]"));
648 }649 }
649 Self::ExprObject(obj) => {650 Self::ExprObject(obj) => {
650 p!(out, { obj.obj_body() })651 p!(out, { obj.obj_body() });
651 }652 }
652 Self::ExprArrayComp(arr) => {653 Self::ExprArrayComp(arr) => {
653 p!(out, str("[") {arr.expr()});654 p!(out, str("[") {arr.expr()});
657 p!(out, str("]"));658 p!(out, str("]"));
658 }659 }
659 Self::ExprImport(v) => {660 Self::ExprImport(v) => {
660 p!(out, {v.import_kind()} str(" ") {v.text()})661 p!(out, {v.import_kind()} str(" ") {v.text()});
661 }662 }
662 Self::ExprVar(n) => p!(out, { n.name() }),663 Self::ExprVar(n) => p!(out, { n.name() }),
663 // Self::ExprLocal(l) => {664 // Self::ExprLocal(l) => {
664 // }665 // }
665 Self::ExprIfThenElse(ite) => {666 Self::ExprIfThenElse(ite) => {
666 p!(out, str("if ") {ite.cond()} str(" then ") {ite.then().map(|t| t.expr())});667 p!(out, str("if ") {ite.cond()} str(" then ") {ite.then().map(|t| t.expr())});
667 if ite.else_kw_token().is_some() || ite.else_().is_some() {668 if ite.else_kw_token().is_some() || ite.else_().is_some() {
668 p!(out, str(" else ") {ite.else_().map(|t| t.expr())})669 p!(out, str(" else ") {ite.else_().map(|t| t.expr())});
669 }670 }
670 }671 }
671 Self::ExprFunction(f) => p!(out, str("function") {f.params_desc()} nl {f.expr()}),672 Self::ExprFunction(f) => p!(out, str("function") {f.params_desc()} nl {f.expr()}),
672 // Self::ExprAssert(a) => p!(new: {a.assertion()} str("; ") {a.expr()}),673 // Self::ExprAssert(a) => p!(new: {a.assertion()} str("; ") {a.expr()}),
673 Self::ExprError(e) => p!(out, str("error ") {e.expr()}),674 Self::ExprError(e) => p!(out, str("error ") {e.expr()}),
674 Self::ExprLiteral(l) => {675 Self::ExprLiteral(l) => {
675 p!(out, { l.literal() })676 p!(out, { l.literal() });
676 }677 }
677 }678 }
678 }679 }
696 );697 );
697 format_comments(&before, CommentLocation::AboveItem, out);698 format_comments(&before, CommentLocation::AboveItem, out);
698 p!(out, {self.expr()} nl);699 p!(out, {self.expr()} nl);
699 format_comments(&after, CommentLocation::EndOfItems, out)700 format_comments(&after, CommentLocation::EndOfItems, out);
700 }701 }
701}702}
702703
712 builder713 builder
713 .error(hi_doc::Text::single(714 .error(hi_doc::Text::single(
714 format!("{:?}", error.error).chars(),715 format!("{:?}", error.error).chars(),
715 Default::default(),716 Formatting::default(),
716 ))717 ))
717 .range(718 .range(
718 error.range.start().into()719 error.range.start().into()
751}752}
752753
753#[derive(Parser)]754#[derive(Parser)]
755#[allow(clippy::struct_excessive_bools)]
754struct Opts {756struct Opts {
755 /// Treat input as code, reformat it instead of reading file.757 /// Treat input as code, reformat it instead of reading file.
756 #[clap(long, short = 'e')]758 #[clap(long, short = 'e')]
814816
815 let mut iteration = 0;817 let mut iteration = 0;
816 let mut formatted = input.clone();818 let mut formatted = input.clone();
817 let mut tmp;819 let mut convergence_tmp;
818 // https://github.com/dprint/dprint/pull/423820 // https://github.com/dprint/dprint/pull/423
819 loop {821 loop {
820 let Some(reformatted) = format(822 let Some(reformatted) = format(
829 ) else {831 ) else {
830 return Err(Error::Parse);832 return Err(Error::Parse);
831 };833 };
832 tmp = reformatted.trim().to_owned();834 convergence_tmp = reformatted.trim().to_owned();
833 if formatted == tmp {835 if formatted == convergence_tmp {
834 break;836 break;
835 }837 }
836 formatted = tmp;838 formatted = convergence_tmp;
837 if opts.conv_limit == 0 {839 if opts.conv_limit == 0 {
838 break;840 break;
839 }841 }
840 iteration += 1;842 iteration += 1;
841 if iteration > opts.conv_limit {843 assert!(iteration <= opts.conv_limit, "formatting not converged");
842 panic!("formatting not converged");
843 }
844 }844 }
845 formatted.push('\n');845 formatted.push('\n');
846 if opts.test && formatted != input {846 if opts.test && formatted != input {
855 temp.flush()?;855 temp.flush()?;
856 temp.persist(&path)?;856 temp.persist(&path)?;
857 } else {857 } else {
858 print!("{formatted}")858 print!("{formatted}");
859 }859 }
860 Ok(())860 Ok(())
861}861}
modifiedcrates/jrsonnet-rowan-parser/src/lex.rsdiffbeforeafterboth
37 // In kinds, string blocks is parsed at least as `|||`37 // In kinds, string blocks is parsed at least as `|||`
38 lexer.bump(3);38 lexer.bump(3);
39 let res = lex_str_block(&mut lexer);39 let res = lex_str_block(&mut lexer);
40 let next = lexer.next();
40 debug_assert!(lexer.next().is_none(), "str_block is lexed");41 assert!(next.is_none(), "str_block is lexed");
41 match res {42 match res {
42 Ok(_) => {}43 Ok(()) => {}
43 Err(e) => {44 Err(e) => {
44 kind = Ok(match e {45 kind = Ok(match e {
45 StringBlockError::UnexpectedEnd => ERROR_STRING_BLOCK_UNEXPECTED_END,46 StringBlockError::UnexpectedEnd => ERROR_STRING_BLOCK_UNEXPECTED_END,
48 ERROR_STRING_BLOCK_MISSING_TERMINATION49 ERROR_STRING_BLOCK_MISSING_TERMINATION
49 }50 }
50 StringBlockError::MissingIndent => ERROR_STRING_BLOCK_MISSING_INDENT,51 StringBlockError::MissingIndent => ERROR_STRING_BLOCK_MISSING_INDENT,
51 })52 });
52 }53 }
53 }54 }
54 }55 }
modifiedcrates/jrsonnet-rowan-parser/src/marker.rsdiffbeforeafterboth
146 p: &mut Parser,146 p: &mut Parser,
147 kind: SyntaxKind,147 kind: SyntaxKind,
148 error: Option<SyntaxError>,148 error: Option<SyntaxError>,
149 ) -> CompletedMarker {149 ) -> Self {
150 let new_m = p.start();150 let new_m = p.start();
151 match &mut p.events[self.start_event_idx] {151 match &mut p.events[self.start_event_idx] {
152 Event::Start { forward_parent, .. } => {152 Event::Start { forward_parent, .. } => {
173 }173 }
174 completed174 completed
175 }175 }
176 pub fn wrap(self, p: &mut Parser, kind: SyntaxKind) -> CompletedMarker {176 pub fn wrap(self, p: &mut Parser, kind: SyntaxKind) -> Self {
177 self.wrap_raw(p, kind, None)177 self.wrap_raw(p, kind, None)
178 }178 }
179 pub fn wrap_error(self, p: &mut Parser, msg: impl AsRef<str>) -> CompletedMarker {179 pub fn wrap_error(self, p: &mut Parser, msg: impl AsRef<str>) -> Self {
180 self.wrap_raw(180 self.wrap_raw(
181 p,181 p,
182 SyntaxKind::ERROR_CUSTOM,182 SyntaxKind::ERROR_CUSTOM,
modifiedcrates/jrsonnet-rowan-parser/src/parser.rsdiffbeforeafterboth
72 .rev()72 .rev()
73 .take_while(|h| h.0 > self.entered)73 .take_while(|h| h.0 > self.entered)
74 .count();74 .count();
75 self.hints.truncate(self.hints.len() - amount)75 self.hints.truncate(self.hints.len() - amount);
76 }76 }
77 fn clear_expected_syntaxes(&mut self) {77 fn clear_expected_syntaxes(&self) {
78 self.expected_syntax_tracking_state78 self.expected_syntax_tracking_state
79 .set(ExpectedSyntax::Unnamed(TS![]));79 .set(ExpectedSyntax::Unnamed(TS![]));
80 }80 }
104 }104 }
105105
106 pub(crate) fn expect(&mut self, kind: SyntaxKind) {106 pub(crate) fn expect(&mut self, kind: SyntaxKind) {
107 self.expect_with_recovery_set(kind, TS![])107 self.expect_with_recovery_set(kind, TS![]);
108 }108 }
109109
110 pub(crate) fn expect_with_recovery_set(110 pub(crate) fn expect_with_recovery_set(
153 m153 m
154 }154 }
155 fn bump_assert(&mut self, kind: SyntaxKind) {155 fn bump_assert(&mut self, kind: SyntaxKind) {
156 assert!(self.at(kind), "expected {:?}", kind);156 assert!(self.at(kind), "expected {kind:?}");
157 self.bump_remap(self.current());157 self.bump_remap(self.current());
158 }158 }
159 fn bump(&mut self) {159 fn bump(&mut self) {
168 fn step(&self) {168 fn step(&self) {
169 use std::fmt::Write;169 use std::fmt::Write;
170 let steps = self.steps.get();170 let steps = self.steps.get();
171 if steps >= 15000000 {171 if steps >= 15_000_000 {
172 let mut out = "seems like parsing is stuck".to_owned();172 let mut out = "seems like parsing is stuck".to_owned();
173 {173 {
174 let last = 20;174 let last = 20;
175 write!(out, "\n\nLast {} events:", last).unwrap();175 write!(out, "\n\nLast {last} events:").unwrap();
176 for (i, event) in self176 for (i, event) in self
177 .events177 .events
178 .iter()178 .iter()
205 self.nth(0)205 self.nth(0)
206 }206 }
207 #[must_use]207 #[must_use]
208 pub(crate) fn expected_syntax_name(&mut self, name: &'static str) -> ExpectedSyntaxGuard {208 pub(crate) fn expected_syntax_name(&self, name: &'static str) -> ExpectedSyntaxGuard {
209 self.expected_syntax_tracking_state209 self.expected_syntax_tracking_state
210 .set(ExpectedSyntax::Named(name));210 .set(ExpectedSyntax::Named(name));
211211
212 ExpectedSyntaxGuard::new(Rc::clone(&self.expected_syntax_tracking_state))212 ExpectedSyntaxGuard::new(Rc::clone(&self.expected_syntax_tracking_state))
213 }213 }
214 pub fn at(&mut self, kind: SyntaxKind) -> bool {214 pub fn at(&self, kind: SyntaxKind) -> bool {
215 self.nth_at(0, kind)215 self.nth_at(0, kind)
216 }216 }
217 pub fn nth_at(&mut self, n: usize, kind: SyntaxKind) -> bool {217 pub fn nth_at(&self, n: usize, kind: SyntaxKind) -> bool {
218 if n == 0 {218 if n == 0 {
219 if let ExpectedSyntax::Unnamed(kinds) = self.expected_syntax_tracking_state.get() {219 if let ExpectedSyntax::Unnamed(kinds) = self.expected_syntax_tracking_state.get() {
220 let kinds = kinds.with(kind);220 let kinds = kinds.with(kind);
221 self.expected_syntax_tracking_state221 self.expected_syntax_tracking_state
222 .set(ExpectedSyntax::Unnamed(kinds))222 .set(ExpectedSyntax::Unnamed(kinds));
223 }223 }
224 }224 }
225 self.nth(n) == kind225 self.nth(n) == kind
226 }226 }
227 pub fn at_ts(&mut self, set: SyntaxKindSet) -> bool {227 pub fn at_ts(&self, set: SyntaxKindSet) -> bool {
228 if let ExpectedSyntax::Unnamed(kinds) = self.expected_syntax_tracking_state.get() {228 if let ExpectedSyntax::Unnamed(kinds) = self.expected_syntax_tracking_state.get() {
229 let kinds = kinds.union(set);229 let kinds = kinds.union(set);
230 self.expected_syntax_tracking_state230 self.expected_syntax_tracking_state
231 .set(ExpectedSyntax::Unnamed(kinds))231 .set(ExpectedSyntax::Unnamed(kinds));
232 }232 }
233 set.contains(self.current())233 set.contains(self.current())
234 }234 }
235 pub fn at_end(&mut self) -> bool {235 pub fn at_end(&self) -> bool {
236 self.at(EOF)236 self.at(EOF)
237 }237 }
238}238}
239pub(crate) struct ExpectedSyntaxGuard {239pub struct ExpectedSyntaxGuard {
240 expected_syntax_tracking_state: Rc<Cell<ExpectedSyntax>>,240 expected_syntax_tracking_state: Rc<Cell<ExpectedSyntax>>,
241}241}
242242
263impl fmt::Display for ExpectedSyntax {263impl fmt::Display for ExpectedSyntax {
264 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {264 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
265 match self {265 match self {
266 ExpectedSyntax::Named(name) => write!(f, "{name}"),266 Self::Named(name) => write!(f, "{name}"),
267 ExpectedSyntax::Unnamed(set) => write!(f, "{set}"),267 Self::Unnamed(set) => write!(f, "{set}"),
268 }268 }
269 }269 }
270}270}
298 }298 }
299 }299 }
300 match expr_binding_power(p, 0) {300 match expr_binding_power(p, 0) {
301 Ok(m) => m,301 Ok(m) | Err(m) => m,
302 Err(m) => m,
303 };302 };
304 m.complete(p, EXPR)303 m.complete(p, EXPR)
305}304}
399}398}
400fn visibility(p: &mut Parser) {399fn visibility(p: &mut Parser) {
401 if p.at_ts(TS![: :: :::]) {400 if p.at_ts(TS![: :: :::]) {
402 p.bump()401 p.bump();
403 } else {402 } else {
404 p.error_with_recovery_set(TS![=]);403 p.error_with_recovery_set(TS![=]);
405 }404 }
556 expr(p);555 expr(p);
557 let arg = m.complete(p, ARG);556 let arg = m.complete(p, ARG);
558 if started_named.get() {557 if started_named.get() {
559 unnamed_after_named.push(arg)558 unnamed_after_named.push(arg);
560 }559 }
561 }560 }
562 if comma(p) {561 if comma(p) {
566 }565 }
567 p.expect(T![')']);566 p.expect(T![')']);
568 if p.at(T![tailstrict]) {567 if p.at(T![tailstrict]) {
569 p.bump()568 p.bump();
570 }569 }
571570
572 for errored in unnamed_after_named {571 for errored in unnamed_after_named {
719 let m = p.start();718 let m = p.start();
720 p.bump_assert(T![...]);719 p.bump_assert(T![...]);
721 if p.at(IDENT) {720 if p.at(IDENT) {
722 p.bump()721 p.bump();
723 }722 }
724 m.complete(p, DESTRUCT_REST);723 m.complete(p, DESTRUCT_REST);
725}724}
modifiedcrates/jrsonnet-rowan-parser/src/precedence.rsdiffbeforeafterboth
13 Self::BitXor => (8, 9),13 Self::BitXor => (8, 9),
14 Self::BitOr => (6, 7),14 Self::BitOr => (6, 7),
15 Self::And => (4, 5),15 Self::And => (4, 5),
16 Self::NullCoaelse => (2, 3),16 Self::NullCoaelse | Self::Or => (2, 3),
17 Self::Or => (2, 3),
18 Self::ErrorNoOperator => (0, 1),17 Self::ErrorNoOperator => (0, 1),
19 }18 }
20 }19 }
23impl UnaryOperatorKind {22impl UnaryOperatorKind {
24 pub fn binding_power(&self) -> ((), u8) {23 pub fn binding_power(&self) -> ((), u8) {
25 match self {24 match self {
26 Self::Minus => ((), 20),
27 Self::Not => ((), 20),
28 Self::BitNot => ((), 20),25 Self::Minus | Self::Not | Self::BitNot => ((), 20),
29 }26 }
30 }27 }
31}28}
modifiedcrates/jrsonnet-rowan-parser/src/string_block.rsdiffbeforeafterboth
17 let _ = lex_str_block(lex);17 let _ = lex_str_block(lex);
18}18}
1919
20#[allow(clippy::too_many_lines)]
20pub fn lex_str_block(lex: &mut Lexer<SyntaxKind>) -> Result<(), StringBlockError> {21pub fn lex_str_block(lex: &mut Lexer<SyntaxKind>) -> Result<(), StringBlockError> {
21 struct Context<'a> {22 struct Context<'a> {
22 source: &'a str,23 source: &'a str,
78 };79 };
79 }80 }
8081
82 #[allow(clippy::range_plus_one)]
81 fn pos(&self) -> Range<usize> {83 fn pos(&self) -> Range<usize> {
82 if self.index == self.source.len() {84 if self.index == self.source.len() {
83 self.offset + self.index..self.offset + self.index85 self.offset + self.index..self.offset + self.index
120 let end_index = ctx122 let end_index = ctx
121 .rest()123 .rest()
122 .find("|||")124 .find("|||")
123 .map(|v| v + 3)
124 .unwrap_or_else(|| ctx.rest().len());125 .map_or_else(|| ctx.rest().len(), |v| v + 3);
125 lex.bump(ctx.index + end_index);126 lex.bump(ctx.index + end_index);
126 }127 }
127128
150 }151 }
151152
152 // Process leading blank lines before calculating string block indent153 // Process leading blank lines before calculating string block indent
153 while let Some('\n') = ctx.peek() {154 while ctx.peek() == Some('\n') {
154 ctx.next();155 ctx.next();
155 }156 }
156157
179 }180 }
180181
181 // Skip any blank lines182 // Skip any blank lines
182 while let Some('\n') = ctx.peek() {183 while ctx.peek() == Some('\n') {
183 ctx.next();184 ctx.next();
184 }185 }
185186
186 // Look at the next line187 // Look at the next line
187 num_whitespace = check_whitespace(str_block_indent, ctx.rest());188 num_whitespace = check_whitespace(str_block_indent, ctx.rest());
188 if num_whitespace == 0 {189 if num_whitespace == 0 {
189 // End of the text block190 // End of the text block
190 let mut term_indent = String::with_capacity(num_whitespace);191 // let mut term_indent = String::with_capacity(num_whitespace);
191 while let Some(' ' | '\t') = ctx.peek() {192 while let Some(' ' | '\t') = ctx.peek() {
193 // term_indent.push(
192 term_indent.push(ctx.next().unwrap());194 ctx.next().unwrap();
195 // );
193 }196 }
194197
195 if !ctx.rest().starts_with("|||") {198 if !ctx.rest().starts_with("|||") {
modifiedcrates/jrsonnet-rowan-parser/src/token_set.rsdiffbeforeafterboth
10 pub const EMPTY: Self = Self(0);10 pub const EMPTY: Self = Self(0);
11 pub const ALL: Self = Self(u128::MAX);11 pub const ALL: Self = Self(u128::MAX);
1212
13 pub const fn new(kinds: &[SyntaxKind]) -> SyntaxKindSet {13 pub const fn new(kinds: &[SyntaxKind]) -> Self {
14 let mut res = 0u128;14 let mut res = 0u128;
15 let mut i = 0;15 let mut i = 0;
16 while i < kinds.len() {16 while i < kinds.len() {
17 res |= mask(kinds[i]);17 res |= mask(kinds[i]);
18 i += 118 i += 1;
19 }19 }
20 SyntaxKindSet(res)20 Self(res)
21 }21 }
2222
23 #[must_use]
23 pub const fn union(self, other: SyntaxKindSet) -> SyntaxKindSet {24 pub const fn union(self, other: Self) -> Self {
24 SyntaxKindSet(self.0 | other.0)25 Self(self.0 | other.0)
25 }26 }
27 #[must_use]
26 pub const fn with(self, kind: SyntaxKind) -> SyntaxKindSet {28 pub const fn with(self, kind: SyntaxKind) -> Self {
27 SyntaxKindSet(self.0 | mask(kind))29 Self(self.0 | mask(kind))
28 }30 }
2931
30 pub fn contains(&self, kind: SyntaxKind) -> bool {32 pub fn contains(&self, kind: SyntaxKind) -> bool {
40 let mut variants = <Vec<SyntaxKind>>::new();42 let mut variants = <Vec<SyntaxKind>>::new();
41 for i in 0..128 {43 for i in 0..128 {
42 if v & 1 == 1 {44 if v & 1 == 1 {
43 variants.push(SyntaxKind::from_raw(i))45 variants.push(SyntaxKind::from_raw(i));
44 }46 }
45 v >>= 1;47 v >>= 1;
46 if v == 0 {48 if v == 0 {
65 let mut variants = <Vec<SyntaxKind>>::new();67 let mut variants = <Vec<SyntaxKind>>::new();
66 for i in 0..128 {68 for i in 0..128 {
67 if v & 1 == 1 {69 if v & 1 == 1 {
68 variants.push(SyntaxKind::from_raw(i))70 variants.push(SyntaxKind::from_raw(i));
69 }71 }
70 v >>= 1;72 v >>= 1;
71 if v == 0 {73 if v == 0 {
77}79}
7880
79const fn mask(kind: SyntaxKind) -> u128 {81const fn mask(kind: SyntaxKind) -> u128 {
80 if kind as u32 > 128 {82 assert!(kind as u32 <= 128, "mask for not a token kind");
81 panic!("mask for not a token kind")
82 }
83 1u128 << (kind as u128)83 1u128 << (kind as u128)
84}84}
8585
modifiedxtask/src/sourcegen/ast.rsdiffbeforeafterboth
72 pub fn is_many(&self) -> bool {72 pub fn is_many(&self) -> bool {
73 matches!(73 matches!(
74 self,74 self,
75 Field::Node {75 Self::Node {
76 cardinality: Cardinality::Many,76 cardinality: Cardinality::Many,
77 ..77 ..
78 }78 }
8181
82 pub fn token_name(&self) -> Option<String> {82 pub fn token_name(&self) -> Option<String> {
83 match self {83 match self {
84 Field::Token(token) => Some(token.clone()),84 Self::Token(token) => Some(token.clone()),
85 _ => None,85 Self::Node { .. } => None,
86 }86 }
87 }87 }
88 pub fn token_kind(&self, kinds: &KindsSrc) -> Option<TokenStream> {88 pub fn token_kind(&self, kinds: &KindsSrc) -> Option<TokenStream> {
89 match self {89 match self {
90 Field::Token(token) => Some(kinds.token(token).expect("token exists").reference()),90 Self::Token(token) => Some(kinds.token(token).expect("token exists").reference()),
91 _ => None,91 Self::Node { .. } => None,
92 }92 }
93 }93 }
94 pub fn is_token_enum(&self, grammar: &AstSrc) -> bool {94 pub fn is_token_enum(&self, grammar: &AstSrc) -> bool {
95 match self {95 match self {
96 Field::Node { ty, .. } => grammar.token_enums.iter().any(|e| &e.name == ty),96 Self::Node { ty, .. } => grammar.token_enums.iter().any(|e| &e.name == ty),
97 _ => false,97 Self::Token(_) => false,
98 }98 }
99 }99 }
100100
101 pub fn method_name(&self, kinds: &KindsSrc) -> proc_macro2::Ident {101 pub fn method_name(&self, kinds: &KindsSrc) -> proc_macro2::Ident {
102 match self {102 match self {
103 Field::Token(name) => kinds.token(name).expect("token exists").method_name(),103 Self::Token(name) => kinds.token(name).expect("token exists").method_name(),
104 Field::Node { name, .. } => {104 Self::Node { name, .. } => {
105 format_ident!("{}", name)105 format_ident!("{}", name)
106 }106 }
107 }107 }
108 }108 }
109 pub fn ty(&self) -> proc_macro2::Ident {109 pub fn ty(&self) -> proc_macro2::Ident {
110 match self {110 match self {
111 Field::Token(_) => format_ident!("SyntaxToken"),111 Self::Token(_) => format_ident!("SyntaxToken"),
112 Field::Node { ty, .. } => format_ident!("{}", ty),112 Self::Node { ty, .. } => format_ident!("{}", ty),
113 }113 }
114 }114 }
115}115}
116116
117pub fn lower(kinds: &KindsSrc, grammar: &Grammar) -> AstSrc {117pub fn lower(kinds: &KindsSrc, grammar: &Grammar) -> AstSrc {
118 let mut res = AstSrc {118 let mut res = AstSrc::default();
119 // tokens,
120 ..Default::default()
121 };
122119
123 let nodes = grammar.iter().collect::<Vec<_>>();120 let nodes = grammar.iter().collect::<Vec<_>>();
124121
135 };132 };
136 res.enums.push(enum_src);133 res.enums.push(enum_src);
137 }134 }
138 None => match lower_token_enum(grammar, rule) {135 None => {
139 Some(variants) => {136 if let Some(variants) = lower_token_enum(grammar, rule) {
140 let tokens_enum_src = AstTokenEnumSrc {137 let tokens_enum_src = AstTokenEnumSrc {
141 doc: Vec::new(),138 doc: Vec::new(),
142 name,139 name,
143 variants,140 variants,
144 };141 };
145 res.token_enums.push(tokens_enum_src);142 res.token_enums.push(tokens_enum_src);
146 }143 } else {
147 None => {
148 let mut fields = Vec::new();144 let mut fields = Vec::new();
149 lower_rule(&mut fields, grammar, None, rule, false);145 lower_rule(&mut fields, grammar, None, rule, false);
150 let mut types = HashMap::new();146 let mut types = HashMap::new();
173 fields,169 fields,
174 });170 });
175 }171 }
176 },172 }
177 }173 }
178 }174 }
179175
240 acc.push(field);236 acc.push(field);
241 }237 }
242 Rule::Token(token) => {238 Rule::Token(token) => {
243 assert!(label.is_none(), "uexpected label: {:?}", label);239 assert!(label.is_none(), "uexpected label: {label:?}");
244 let name = grammar[*token].name.clone();240 let name = grammar[*token].name.clone();
245 let field = Field::Token(name);241 let field = Field::Token(name);
246 acc.push(field);242 acc.push(field);
267 }263 }
268 Rule::Seq(rules) | Rule::Alt(rules) => {264 Rule::Seq(rules) | Rule::Alt(rules) => {
269 for rule in rules {265 for rule in rules {
270 lower_rule(acc, grammar, label, rule, in_optional)266 lower_rule(acc, grammar, label, rule, in_optional);
271 }267 }
272 }268 }
273 Rule::Opt(rule) => lower_rule(acc, grammar, label, rule, true),269 Rule::Opt(rule) => lower_rule(acc, grammar, label, rule, true),
modifiedxtask/src/sourcegen/kinds.rsdiffbeforeafterboth
41impl TokenKind {41impl TokenKind {
42 pub fn grammar_name(&self) -> &str {42 pub fn grammar_name(&self) -> &str {
43 match self {43 match self {
44 TokenKind::Keyword { code, .. } => code,44 Self::Keyword { code, .. } => code,
45 TokenKind::Literal { grammar_name, .. } => grammar_name,45 Self::Literal { grammar_name, .. }
46 TokenKind::Meta { grammar_name, .. } => grammar_name,46 | Self::Meta { grammar_name, .. }
47 TokenKind::Error { grammar_name, .. } => grammar_name,47 | Self::Error { grammar_name, .. } => grammar_name,
48 }48 }
49 }49 }
50 /// How this keyword should appear in kinds enum, screaming snake cased50 /// How this keyword should appear in kinds enum, screaming snake cased
51 pub fn name(&self) -> &str {51 pub fn name(&self) -> &str {
52 match self {52 match self {
53 TokenKind::Keyword { name, .. } => name,53 Self::Keyword { name, .. }
54 TokenKind::Literal { name, .. } => name,54 | Self::Literal { name, .. }
55 TokenKind::Meta { name, .. } => name,55 | Self::Meta { name, .. }
56 TokenKind::Error { name, .. } => name,56 | Self::Error { name, .. } => name,
57 }57 }
58 }58 }
59 pub fn expand_kind(&self) -> TokenStream {59 pub fn expand_kind(&self) -> TokenStream {
60 let name = format_ident!("{}", self.name());60 let name = format_ident!("{}", self.name());
61 let attr = match self {61 let attr = match self {
62 TokenKind::Keyword { code, .. } => quote! {#[token(#code)]},62 Self::Keyword { code, .. } => quote! {#[token(#code)]},
63 TokenKind::Literal { regex, lexer, .. } => {63 Self::Literal { regex, lexer, .. } => {
64 let lexer = lexer64 let lexer = lexer
65 .as_deref()65 .as_deref()
66 .map(TokenStream::from_str)66 .map(TokenStream::from_str)
67 .map(|r| r.expect("path is correct"));67 .map(|r| r.expect("path is correct"));
68 quote! {#[regex(#regex, #lexer)]}68 quote! {#[regex(#regex, #lexer)]}
69 }69 }
70 TokenKind::Error {70 Self::Error {
71 regex, priority, ..71 regex, priority, ..
72 } if regex.is_some() => {72 } if regex.is_some() => {
73 let priority = priority.map(|p| quote! {, priority = #p});73 let priority = priority.map(|p| quote! {, priority = #p});
82 }82 }
83 pub fn expand_t_macros(&self) -> Option<TokenStream> {83 pub fn expand_t_macros(&self) -> Option<TokenStream> {
84 match self {84 match self {
85 TokenKind::Keyword { code, name } => {85 Self::Keyword { code, name } => {
86 let code = escape_token_macro(code);86 let code = escape_token_macro(code);
87 let name = format_ident!("{name}");87 let name = format_ident!("{name}");
88 Some(quote! {88 Some(quote! {
98 /// Keywords are referenced with `T![_]` macro,98 /// Keywords are referenced with `T![_]` macro,
99 /// and literals are referenced directly by name99 /// and literals are referenced directly by name
100 pub fn reference(&self) -> TokenStream {100 pub fn reference(&self) -> TokenStream {
101 match self {
102 TokenKind::Keyword { code, .. } => {101 if let Self::Keyword { code, .. } = self {
103 let code = escape_token_macro(code);102 let code = escape_token_macro(code);
104 quote! {T![#code]}103 quote! {T![#code]}
105 }104 } else {
106 _ => {
107 let name = self.name();105 let name = self.name();
108 let ident = format_ident!("{name}");106 let ident = format_ident!("{name}");
109 quote! {#ident}107 quote! {#ident}
110 }108 }
111 }
112 }109 }
113110
114 pub fn method_name(&self) -> Ident {111 pub fn method_name(&self) -> Ident {
115 match self {112 match self {
116 TokenKind::Keyword { name, .. } => {113 Self::Keyword { name, .. } => {
117 format_ident!("{}_token", name.to_lowercase())114 format_ident!("{}_token", name.to_lowercase())
118 }115 }
119 TokenKind::Literal { name, .. } => {116 Self::Literal { name, .. } => {
120 format_ident!("{}_lit", name.to_lowercase())117 format_ident!("{}_lit", name.to_lowercase())
121 }118 }
122 TokenKind::Meta { name, .. } => format_ident!("{}_meta", name.to_lowercase()),119 Self::Meta { name, .. } => format_ident!("{}_meta", name.to_lowercase()),
123 TokenKind::Error { name, .. } => format_ident!("{}_error", name.to_lowercase()),120 Self::Error { name, .. } => format_ident!("{}_error", name.to_lowercase()),
124 }121 }
125 }122 }
126}123}
188 .is_none(),185 .is_none(),
189 "token already defined: {}",186 "token already defined: {}",
190 token.grammar_name()187 token.grammar_name()
191 )188 );
192 }189 }
193 pub fn define_node(&mut self, node: &str) {190 pub fn define_node(&mut self, node: &str) {
194 assert!(191 assert!(
195 self.defined_node_names.insert(node.to_owned()),192 self.defined_node_names.insert(node.to_owned()),
196 "node name already defined: {}",193 "node name already defined: {node}"
197 node
198 );194 );
199 self.nodes.push(node.to_string())195 self.nodes.push(node.to_string());
200 }196 }
201 pub fn token(&self, tok: &str) -> Option<&TokenKind> {197 pub fn token(&self, tok: &str) -> Option<&TokenKind> {
202 self.defined_tokens.get(tok)198 self.defined_tokens.get(tok)
modifiedxtask/src/sourcegen/mod.rsdiffbeforeafterboth
49 match special {49 match special {
50 SpecialName::Literal => panic!("literal is not defined: {name}"),50 SpecialName::Literal => panic!("literal is not defined: {name}"),
51 SpecialName::Meta => {51 SpecialName::Meta => {
52 eprintln!("implicit meta: {}", name);52 eprintln!("implicit meta: {name}");
53 kinds.define_token(TokenKind::Meta {53 kinds.define_token(TokenKind::Meta {
54 grammar_name: token.to_owned(),54 grammar_name: token.to_owned(),
55 name: format!("META_{}", name),55 name: format!("META_{name}"),
56 })56 });
57 }57 }
58 SpecialName::Error => {58 SpecialName::Error => {
59 eprintln!("implicit error: {}", name);59 eprintln!("implicit error: {name}");
60 kinds.define_token(TokenKind::Error {60 kinds.define_token(TokenKind::Error {
61 grammar_name: token.to_owned(),61 grammar_name: token.to_owned(),
62 name: format!("ERROR_{}", name),62 name: format!("ERROR_{name}"),
63 regex: None,63 regex: None,
64 priority: None,64 priority: None,
65 is_lexer_error: true,65 is_lexer_error: true,
66 })66 });
67 }67 }
68 };68 };
69 continue;69 continue;
70 };70 };
71 let name = to_upper_snake_case(token);71 let name = to_upper_snake_case(token);
72 eprintln!("implicit kw: {}", token);72 eprintln!("implicit kw: {token}");
73 kinds.define_token(TokenKind::Keyword {73 kinds.define_token(TokenKind::Keyword {
74 code: token.to_owned(),74 code: token.to_owned(),
75 name: format!("{name}_KW"),75 name: format!("{name}_KW"),
98 "/../crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs",98 "/../crates/jrsonnet-rowan-parser/src/generated/syntax_kinds.rs",
99 )),99 )),
100 &syntax_kinds,100 &syntax_kinds,
101 )?;101 );
102 ensure_file_contents(102 ensure_file_contents(
103 &PathBuf::from(concat!(103 &PathBuf::from(concat!(
104 env!("CARGO_MANIFEST_DIR"),104 env!("CARGO_MANIFEST_DIR"),
105 "/../crates/jrsonnet-rowan-parser/src/generated/nodes.rs",105 "/../crates/jrsonnet-rowan-parser/src/generated/nodes.rs",
106 )),106 )),
107 &nodes,107 &nodes,
108 )?;108 );
109 Ok(())109 Ok(())
110}110}
111111
189 reformat(&ast.to_string())189 reformat(&ast.to_string())
190}190}
191191
192#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
192fn generate_nodes(kinds: &KindsSrc, grammar: &AstSrc) -> Result<String> {193fn generate_nodes(kinds: &KindsSrc, grammar: &AstSrc) -> Result<String> {
193 let (node_defs, node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar194 let (node_defs, node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar
194 .nodes195 .nodes
524fn write_doc_comment(contents: &[String], dest: &mut String) {525fn write_doc_comment(contents: &[String], dest: &mut String) {
525 use std::fmt::Write;526 use std::fmt::Write;
526 for line in contents {527 for line in contents {
527 writeln!(dest, "///{}", line).unwrap();528 writeln!(dest, "///{line}").unwrap();
528 }529 }
529}530}
530531
modifiedxtask/src/sourcegen/util.rsdiffbeforeafterboth
1// FIXME: Replace various helper here with inflector?
2
1use std::{fs, path::Path};3use std::{fs, path::Path};
24
57
6/// Checks that the `file` has the specified `contents`. If that is not the8/// Checks that the `file` has the specified `contents`. If that is not the
7/// case, updates the file and then fails the test.9/// case, updates the file and then fails the test.
8pub fn ensure_file_contents(file: &Path, contents: &str) -> Result<()> {10pub fn ensure_file_contents(file: &Path, contents: &str) {
9 if let Ok(old_contents) = fs::read_to_string(file) {11 if let Ok(old_contents) = fs::read_to_string(file) {
10 if normalize_newlines(&old_contents) == normalize_newlines(contents) {12 if normalize_newlines(&old_contents) == normalize_newlines(contents) {
11 // File is already up to date.13 // File is already up to date.
12 return Ok(());14 return;
13 }15 }
14 }16 }
1517
18 let _ = fs::create_dir_all(parent);20 let _ = fs::create_dir_all(parent);
19 }21 }
20 fs::write(file, contents).unwrap();22 fs::write(file, contents).unwrap();
21 Ok(())
22}23}
2324
24// Eww, someone configured git to use crlf?25// Eww, someone configured git to use crlf?
25fn normalize_newlines(s: &str) -> String {26fn normalize_newlines(s: &str) -> String {
26 s.replace("\r\n", "\n")27 s.replace("\r\n", "\n")
27}28}
2829
29pub(crate) fn pluralize(s: &str) -> String {30pub fn pluralize(s: &str) -> String {
31 // FIXME: Inflector?
30 format!("{}s", s)32 format!("{s}s")
31}33}
3234
33pub fn to_upper_snake_case(s: &str) -> String {35pub fn to_upper_snake_case(s: &str) -> String {
34 let mut buf = String::with_capacity(s.len());36 let mut buf = String::with_capacity(s.len());
35 let mut prev = false;37 let mut prev = false;
36 for c in s.chars() {38 for c in s.chars() {
37 if c.is_ascii_uppercase() && prev {39 if c.is_ascii_uppercase() && prev {
38 buf.push('_')40 buf.push('_');
39 }41 }
40 prev = true;42 prev = true;
4143
48 let mut prev = false;50 let mut prev = false;
49 for c in s.chars() {51 for c in s.chars() {
50 if c.is_ascii_uppercase() && prev {52 if c.is_ascii_uppercase() && prev {
51 buf.push('_')53 buf.push('_');
52 }54 }
53 prev = true;55 prev = true;
5456