chore(cli): apply clippy fixes

This commit is contained in:
Amaan Qureshi 2024-02-04 01:30:33 -05:00
parent 1fb16a72ac
commit 04ff704bca
No known key found for this signature in database
GPG key ID: E67890ADC4227273
49 changed files with 1094 additions and 1277 deletions

View file

@ -154,7 +154,7 @@ impl Generator {
self.symbol_map = HashMap::new();
for symbol in self.parse_table.symbols.iter() {
for symbol in &self.parse_table.symbols {
let mut mapping = symbol;
// There can be multiple symbols in the grammar that have the same name and kind,
@ -201,7 +201,7 @@ impl Generator {
for production_info in &self.parse_table.production_infos {
// Build a list of all field names
for field_name in production_info.field_map.keys() {
if let Err(i) = self.field_names.binary_search(&field_name) {
if let Err(i) = self.field_names.binary_search(field_name) {
self.field_names.insert(i, field_name.clone());
}
}
@ -209,13 +209,14 @@ impl Generator {
for alias in &production_info.alias_sequence {
// Generate a mapping from aliases to C identifiers.
if let Some(alias) = &alias {
let existing_symbol = self.parse_table.symbols.iter().cloned().find(|symbol| {
if let Some(default_alias) = self.default_aliases.get(symbol) {
default_alias == alias
} else {
let (name, kind) = self.metadata_for_symbol(*symbol);
name == alias.value && kind == alias.kind()
}
let existing_symbol = self.parse_table.symbols.iter().copied().find(|symbol| {
self.default_aliases.get(symbol).map_or_else(
|| {
let (name, kind) = self.metadata_for_symbol(*symbol);
name == alias.value && kind == alias.kind()
},
|default_alias| default_alias == alias,
)
});
// Some aliases match an existing symbol in the grammar.
@ -316,7 +317,7 @@ impl Generator {
"#define SYMBOL_COUNT {}",
self.parse_table.symbols.len()
);
add_line!(self, "#define ALIAS_COUNT {}", self.unique_aliases.len(),);
add_line!(self, "#define ALIAS_COUNT {}", self.unique_aliases.len());
add_line!(self, "#define TOKEN_COUNT {}", token_count);
add_line!(
self,
@ -342,7 +343,7 @@ impl Generator {
indent!(self);
self.symbol_order.insert(Symbol::end(), 0);
let mut i = 1;
for symbol in self.parse_table.symbols.iter() {
for symbol in &self.parse_table.symbols {
if *symbol != Symbol::end() {
self.symbol_order.insert(*symbol, i);
add_line!(self, "{} = {},", self.symbol_ids[&symbol], i);
@ -361,12 +362,13 @@ impl Generator {
fn add_symbol_names_list(&mut self) {
add_line!(self, "static const char * const ts_symbol_names[] = {{");
indent!(self);
for symbol in self.parse_table.symbols.iter() {
for symbol in &self.parse_table.symbols {
let name = self.sanitize_string(
self.default_aliases
.get(symbol)
.map(|alias| alias.value.as_str())
.unwrap_or(self.metadata_for_symbol(*symbol).0),
.map_or(self.metadata_for_symbol(*symbol).0, |alias| {
alias.value.as_str()
}),
);
add_line!(self, "[{}] = \"{}\",", self.symbol_ids[&symbol], name);
}
@ -527,15 +529,13 @@ impl Generator {
if let Some(alias) = &step.alias {
if step.symbol.is_non_terminal()
&& Some(alias) != self.default_aliases.get(&step.symbol)
&& self.symbol_ids.contains_key(&step.symbol)
{
if self.symbol_ids.contains_key(&step.symbol) {
if let Some(alias_id) = self.alias_ids.get(&alias) {
let alias_ids = alias_ids_by_symbol
.entry(step.symbol)
.or_insert(Vec::new());
if let Err(i) = alias_ids.binary_search(&alias_id) {
alias_ids.insert(i, alias_id);
}
if let Some(alias_id) = self.alias_ids.get(alias) {
let alias_ids =
alias_ids_by_symbol.entry(step.symbol).or_insert(Vec::new());
if let Err(i) = alias_ids.binary_search(&alias_id) {
alias_ids.insert(i, alias_id);
}
}
}
@ -555,11 +555,11 @@ impl Generator {
for (symbol, alias_ids) in alias_ids_by_symbol {
let symbol_id = &self.symbol_ids[symbol];
let public_symbol_id = &self.symbol_ids[&self.symbol_map[&symbol]];
add_line!(self, "{}, {},", symbol_id, 1 + alias_ids.len());
add_line!(self, "{symbol_id}, {},", 1 + alias_ids.len());
indent!(self);
add_line!(self, "{},", public_symbol_id);
add_line!(self, "{public_symbol_id},");
for alias_id in alias_ids {
add_line!(self, "{},", alias_id);
add_line!(self, "{alias_id},");
}
dedent!(self);
}
@ -585,7 +585,7 @@ impl Generator {
let primary_state = first_state_for_each_core_id
.entry(state.core_id)
.or_insert(idx);
add_line!(self, "[{}] = {},", idx, primary_state);
add_line!(self, "[{idx}] = {primary_state},");
}
dedent!(self);
add_line!(self, "}};");
@ -603,7 +603,9 @@ impl Generator {
let mut field_map_ids = Vec::new();
for production_info in &self.parse_table.production_infos {
if !production_info.field_map.is_empty() {
if production_info.field_map.is_empty() {
field_map_ids.push((0, 0));
} else {
let mut flat_field_map = Vec::new();
for (field_name, locations) in &production_info.field_map {
for location in locations {
@ -618,8 +620,6 @@ impl Generator {
),
flat_field_map.len(),
));
} else {
field_map_ids.push((0, 0));
}
}
@ -632,10 +632,7 @@ impl Generator {
if length > 0 {
add_line!(
self,
"[{}] = {{.index = {}, .length = {}}},",
production_id,
row_id,
length
"[{production_id}] = {{.index = {row_id}, .length = {length}}},",
);
}
}
@ -649,7 +646,7 @@ impl Generator {
);
indent!(self);
for (row_index, field_pairs) in flat_field_maps.into_iter().skip(1) {
add_line!(self, "[{}] =", row_index);
add_line!(self, "[{row_index}] =");
indent!(self);
for (field_name, location) in field_pairs {
add_whitespace!(self);
@ -697,7 +694,7 @@ impl Generator {
ruled_out_chars.extend(chars.iter());
} else {
ranges = chars.clone().negate().simplify_ignoring(&ruled_out_chars);
ranges.insert(0, '\0'..'\0')
ranges.insert(0, '\0'..'\0');
}
// Record any large character sets so that they can be extracted
@ -738,7 +735,7 @@ impl Generator {
.collect();
// Generate a helper function for each large character set.
let mut sorted_large_char_sets: Vec<_> = large_character_sets.iter().map(|e| e).collect();
let mut sorted_large_char_sets = large_character_sets.iter().collect::<Vec<_>>();
sorted_large_char_sets.sort_unstable_by_key(|info| (info.symbol, info.index));
for info in sorted_large_char_sets {
add_line!(
@ -760,8 +757,7 @@ impl Generator {
add_line!(
self,
"static bool {}(TSLexer *lexer, TSStateId state) {{",
name
"static bool {name}(TSLexer *lexer, TSStateId state) {{",
);
indent!(self);
@ -771,7 +767,7 @@ impl Generator {
indent!(self);
for (i, state) in lex_table.states.into_iter().enumerate() {
add_line!(self, "case {}:", i);
add_line!(self, "case {i}:");
indent!(self);
self.add_lex_state(state, &state_transition_summaries[i], &large_character_sets);
dedent!(self);
@ -810,14 +806,14 @@ impl Generator {
}
i += 1;
}
return None;
None
}
fn add_lex_state(
&mut self,
state: LexState,
transition_info: &Vec<TransitionSummary>,
large_character_sets: &Vec<LargeCharacterSetInfo>,
transition_info: &[TransitionSummary],
large_character_sets: &[LargeCharacterSetInfo],
) {
if let Some(accept_action) = state.accept_action {
add_line!(self, "ACCEPT_TOKEN({});", self.symbol_ids[&accept_action]);
@ -852,7 +848,7 @@ impl Generator {
// Otherwise, generate code to compare the lookahead character
// with all of the character ranges.
if transition.ranges.len() > 0 {
if !transition.ranges.is_empty() {
add!(self, "if (");
self.add_character_range_conditions(&transition.ranges, transition.is_included, 2);
add!(self, ") ");
@ -878,26 +874,20 @@ impl Generator {
for (i, range) in ranges.iter().enumerate() {
if is_included {
if i > 0 {
add!(self, " ||{}", line_break);
add!(self, " ||{line_break}");
}
if range.start == '\0' {
add!(self, "!eof && ");
}
if range.end == range.start {
if range.start == '\0' {
add!(self, "!eof && ");
}
add!(self, "lookahead == ");
self.add_character(range.start);
} else if range.end as u32 == range.start as u32 + 1 {
if range.start == '\0' {
add!(self, "!eof && ");
}
add!(self, "lookahead == ");
self.add_character(range.start);
add!(self, " ||{}lookahead == ", line_break);
add!(self, " ||{line_break}lookahead == ");
self.add_character(range.end);
} else {
if range.start == '\0' {
add!(self, "!eof && ");
}
add!(self, "(");
self.add_character(range.start);
add!(self, " <= lookahead && lookahead <= ");
@ -906,7 +896,7 @@ impl Generator {
}
} else {
if i > 0 {
add!(self, " &&{}", line_break);
add!(self, " &&{line_break}");
}
if range.end == range.start {
add!(self, "lookahead != ");
@ -914,19 +904,17 @@ impl Generator {
} else if range.end as u32 == range.start as u32 + 1 {
add!(self, "lookahead != ");
self.add_character(range.start);
add!(self, " &&{}lookahead != ", line_break);
add!(self, " &&{line_break}lookahead != ");
self.add_character(range.end);
} else if range.start != '\0' {
add!(self, "(lookahead < ");
self.add_character(range.start);
add!(self, " || ");
self.add_character(range.end);
add!(self, " < lookahead)");
} else {
if range.start != '\0' {
add!(self, "(lookahead < ");
self.add_character(range.start);
add!(self, " || ");
self.add_character(range.end);
add!(self, " < lookahead)");
} else {
add!(self, "lookahead > ");
self.add_character(range.end);
}
add!(self, "lookahead > ");
self.add_character(range.end);
}
}
}
@ -955,7 +943,7 @@ impl Generator {
add!(self, "(");
}
add!(self, "c {} ", op);
add!(self, "c {op} ");
self.add_character(*value);
if !simple {
@ -1008,17 +996,16 @@ impl Generator {
indent!(self);
for (i, state) in self.parse_table.states.iter().enumerate() {
if state.is_end_of_non_terminal_extra() {
add_line!(self, "[{}] = {{(TSStateId)(-1)}},", i,);
add_line!(self, "[{i}] = {{(TSStateId)(-1)}},");
} else if state.external_lex_state_id > 0 {
add_line!(
self,
"[{}] = {{.lex_state = {}, .external_lex_state = {}}},",
i,
"[{i}] = {{.lex_state = {}, .external_lex_state = {}}},",
state.lex_state_id,
state.external_lex_state_id
);
} else {
add_line!(self, "[{}] = {{.lex_state = {}}},", i, state.lex_state_id);
add_line!(self, "[{i}] = {{.lex_state = {}}},", state.lex_state_id);
}
}
dedent!(self);
@ -1052,11 +1039,11 @@ impl Generator {
let token = &self.syntax_grammar.external_tokens[i];
let id_token = token
.corresponding_internal_token
.unwrap_or(Symbol::external(i));
.unwrap_or_else(|| Symbol::external(i));
add_line!(
self,
"[{}] = {},",
self.external_token_id(&token),
self.external_token_id(token),
self.symbol_ids[&id_token],
);
}
@ -1151,12 +1138,7 @@ impl Generator {
&mut parse_table_entries,
&mut next_parse_action_list_index,
);
add_line!(
self,
"[{}] = ACTIONS({}),",
self.symbol_ids[symbol],
entry_id
);
add_line!(self, "[{}] = ACTIONS({entry_id}),", self.symbol_ids[symbol]);
}
dedent!(self);
add_line!(self, "}},");
@ -1212,14 +1194,14 @@ impl Generator {
(symbols.len(), *kind, *value, symbols[0])
});
add_line!(self, "[{}] = {},", index, values_with_symbols.len());
add_line!(self, "[{index}] = {},", values_with_symbols.len());
indent!(self);
for ((value, kind), symbols) in values_with_symbols.iter_mut() {
for ((value, kind), symbols) in &mut values_with_symbols {
if *kind == SymbolType::NonTerminal {
add_line!(self, "STATE({}), {},", value, symbols.len());
add_line!(self, "STATE({value}), {},", symbols.len());
} else {
add_line!(self, "ACTIONS({}), {},", value, symbols.len());
add_line!(self, "ACTIONS({value}), {},", symbols.len());
}
symbols.sort_unstable();
@ -1250,8 +1232,7 @@ impl Generator {
for i in self.large_state_count..self.parse_table.states.len() {
add_line!(
self,
"[SMALL_STATE({})] = {},",
i,
"[SMALL_STATE({i})] = {},",
small_state_indices[i - self.large_state_count]
);
}
@ -1260,10 +1241,10 @@ impl Generator {
add_line!(self, "");
}
let mut parse_table_entries: Vec<_> = parse_table_entries
let mut parse_table_entries = parse_table_entries
.into_iter()
.map(|(entry, i)| (i, entry))
.collect();
.collect::<Vec<_>>();
parse_table_entries.sort_by_key(|(index, _)| *index);
self.add_parse_action_list(parse_table_entries);
}
@ -1277,8 +1258,7 @@ impl Generator {
for (i, entry) in parse_table_entries {
add!(
self,
" [{}] = {{.entry = {{.count = {}, .reusable = {}}}}},",
i,
" [{i}] = {{.entry = {{.count = {}, .reusable = {}}}}},",
entry.actions.len(),
entry.reusable
);
@ -1293,9 +1273,9 @@ impl Generator {
is_repetition,
} => {
if is_repetition {
add!(self, "SHIFT_REPEAT({})", state);
add!(self, "SHIFT_REPEAT({state})");
} else {
add!(self, "SHIFT({})", state);
add!(self, "SHIFT({state})");
}
}
ParseAction::Reduce {
@ -1305,17 +1285,17 @@ impl Generator {
production_id,
..
} => {
add!(self, "REDUCE({}, {}", self.symbol_ids[&symbol], child_count);
add!(self, "REDUCE({}, {child_count}", self.symbol_ids[&symbol]);
if dynamic_precedence != 0 {
add!(self, ", .dynamic_precedence = {}", dynamic_precedence);
add!(self, ", .dynamic_precedence = {dynamic_precedence}");
}
if production_id != 0 {
add!(self, ", .production_id = {}", production_id);
add!(self, ", .production_id = {production_id}");
}
add!(self, ")");
}
}
add!(self, ",")
add!(self, ",");
}
add!(self, "\n");
}
@ -1326,29 +1306,26 @@ impl Generator {
fn add_parser_export(&mut self) {
let language_function_name = format!("tree_sitter_{}", self.language_name);
let external_scanner_name = format!("{}_external_scanner", language_function_name);
let external_scanner_name = format!("{language_function_name}_external_scanner");
add_line!(self, "#ifdef __cplusplus");
add_line!(self, r#"extern "C" {{"#);
add_line!(self, "#endif");
if !self.syntax_grammar.external_tokens.is_empty() {
add_line!(self, "void *{}_create(void);", external_scanner_name);
add_line!(self, "void {}_destroy(void *);", external_scanner_name);
add_line!(self, "void *{external_scanner_name}_create(void);");
add_line!(self, "void {external_scanner_name}_destroy(void *);");
add_line!(
self,
"bool {}_scan(void *, TSLexer *, const bool *);",
external_scanner_name
"bool {external_scanner_name}_scan(void *, TSLexer *, const bool *);",
);
add_line!(
self,
"unsigned {}_serialize(void *, char *);",
external_scanner_name
"unsigned {external_scanner_name}_serialize(void *, char *);",
);
add_line!(
self,
"void {}_deserialize(void *, const char *, unsigned);",
external_scanner_name
"void {external_scanner_name}_deserialize(void *, const char *, unsigned);",
);
add_line!(self, "");
}
@ -1360,8 +1337,7 @@ impl Generator {
add_line!(
self,
"extern const TSLanguage *{}(void) {{",
language_function_name
"extern const TSLanguage *{language_function_name}(void) {{",
);
indent!(self);
add_line!(self, "static const TSLanguage language = {{");
@ -1421,11 +1397,11 @@ impl Generator {
indent!(self);
add_line!(self, "&ts_external_scanner_states[0][0],");
add_line!(self, "ts_external_scanner_symbol_map,");
add_line!(self, "{}_create,", external_scanner_name);
add_line!(self, "{}_destroy,", external_scanner_name);
add_line!(self, "{}_scan,", external_scanner_name);
add_line!(self, "{}_serialize,", external_scanner_name);
add_line!(self, "{}_deserialize,", external_scanner_name);
add_line!(self, "{external_scanner_name}_create,");
add_line!(self, "{external_scanner_name}_destroy,");
add_line!(self, "{external_scanner_name}_scan,");
add_line!(self, "{external_scanner_name}_serialize,");
add_line!(self, "{external_scanner_name}_deserialize,");
dedent!(self);
add_line!(self, "}},");
}
@ -1511,8 +1487,8 @@ impl Generator {
self.symbol_ids.insert(symbol, id);
}
fn field_id(&self, field_name: &String) -> String {
format!("field_{}", field_name)
fn field_id(&self, field_name: &str) -> String {
format!("field_{field_name}")
}
fn metadata_for_symbol(&self, symbol: Symbol) -> (&str, VariableType) {
@ -1618,7 +1594,7 @@ impl Generator {
'0'..='9' | 'a'..='z' | 'A'..='Z' | '_' => unreachable!(),
' ' => break 'special_chars,
};
if !result.is_empty() && !result.ends_with("_") {
if !result.is_empty() && !result.ends_with('_') {
result.push('_');
}
result += replacement;
@ -1664,9 +1640,9 @@ impl Generator {
'\r' => add!(self, "'\\r'"),
_ => {
if c == ' ' || c.is_ascii_graphic() {
add!(self, "'{}'", c)
add!(self, "'{c}'");
} else {
add!(self, "{}", c as u32)
add!(self, "{}", c as u32);
}
}
}
@ -1691,7 +1667,8 @@ impl Generator {
/// * `abi_version` - The language ABI version that should be generated. Usually
/// you want Tree-sitter's current version, but right after making an ABI
/// change, it may be useful to generate code with the previous ABI.
pub(crate) fn render_c_code(
#[allow(clippy::too_many_arguments)]
pub fn render_c_code(
name: &str,
parse_table: ParseTable,
main_lex_table: LexTable,
@ -1702,12 +1679,10 @@ pub(crate) fn render_c_code(
default_aliases: AliasMap,
abi_version: usize,
) -> String {
if !(ABI_VERSION_MIN..=ABI_VERSION_MAX).contains(&abi_version) {
panic!(
"This version of Tree-sitter can only generate parsers with ABI version {} - {}, not {}",
ABI_VERSION_MIN, ABI_VERSION_MAX, abi_version
);
}
assert!(
(ABI_VERSION_MIN..=ABI_VERSION_MAX).contains(&abi_version),
"This version of Tree-sitter can only generate parsers with ABI version {ABI_VERSION_MIN} - {ABI_VERSION_MAX}, not {abi_version}",
);
Generator {
buffer: String::new(),