Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree-sitter: Handle alias($.foo, $.bar) when $.bar is undefined #16412

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/extractor/tsg-python/rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# extractor. It is set to the lowest version of Rust we want to support.

[toolchain]
channel = "1.68"
channel = "1.70"
profile = "minimal"
components = [ "rustfmt" ]
26 changes: 18 additions & 8 deletions ql/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ql/rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# extractor. It is set to the lowest version of Rust we want to support.

[toolchain]
channel = "1.68"
channel = "1.70"
profile = "minimal"
components = [ "rustfmt" ]
components = [ "rustfmt" ]
2 changes: 1 addition & 1 deletion ruby/extractor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2018"
# (c.f. https://github.com/bazelbuild/rules_rust/issues/2452).
# Warning: The process takes >5min on my M1 mac, so do wait for a while.
[dependencies]
tree-sitter = "0.20"
tree-sitter = "0.22.5"
tree-sitter-embedded-template = { git = "https://github.com/tree-sitter/tree-sitter-embedded-template.git", rev = "203f7bd3c1bbfbd98fc19add4b8fcb213c059205" }
tree-sitter-ruby = { git = "https://github.com/tree-sitter/tree-sitter-ruby.git", rev = "4d9ad3f010fdc47a8433adcf9ae30c8eb8475ae7" }
clap = { version = "4.2", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion ruby/extractor/rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# extractor. It is set to the lowest version of Rust we want to support.

[toolchain]
channel = "1.68"
channel = "1.70"
profile = "minimal"
components = [ "rustfmt" ]
2 changes: 1 addition & 1 deletion shared/tree-sitter-extractor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["GitHub"]
[dependencies]
flate2 = "1.0"
globset = "0.4"
tree-sitter = "0.20"
tree-sitter = "0.22.5"
tracing = "0.1"
rayon = "1.5.0"
regex = "1.7.1"
Expand Down
2 changes: 1 addition & 1 deletion shared/tree-sitter-extractor/rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# extractor. It is set to the lowest version of Rust we want to support.

[toolchain]
channel = "1.68"
channel = "1.70"
profile = "minimal"
components = [ "clippy", "rustfmt" ]
35 changes: 23 additions & 12 deletions shared/tree-sitter-extractor/src/extractor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn location_label(

/// Extracts the source file at `path`, which is assumed to be canonicalized.
pub fn extract(
language: Language,
language: &Language,
language_prefix: &str,
schema: &NodeTypeMap,
diagnostics_writer: &mut diagnostics::LogWriter,
Expand All @@ -171,7 +171,7 @@ pub fn extract(
tracing::info!("extracting: {}", path_str);

let mut parser = Parser::new();
parser.set_language(language).unwrap();
parser.set_language(&language).unwrap();
parser.set_included_ranges(ranges).unwrap();
let tree = parser.parse(source, None).expect("Failed to parse file");
trap_writer.comment(format!("Auto-generated TRAP file for {}", path_str));
Expand Down Expand Up @@ -334,13 +334,19 @@ impl<'a> Visitor<'a> {
let (id, _, child_nodes) = self.stack.pop().expect("Vistor: empty stack");
let loc = location_for(self, node);
let loc_label = location_label(self.trap_writer, self.file_label, loc);
let table = self
.schema
.get(&TypeName {
kind: node.kind().to_owned(),
named: node.is_named(),
})
.unwrap();
let table = match self.schema.get(&TypeName {
kind: node.kind().to_owned(),
named: node.is_named(),
}) {
Some(t) => t,
None => self
.schema
.get(&TypeName {
kind: node.grammar_name().to_owned(),
named: node.is_named(),
})
.unwrap(),
};
let mut valid = true;
let parent_info = match self.stack.last_mut() {
Some(p) if !node.is_extra() => {
Expand Down Expand Up @@ -576,10 +582,15 @@ impl<'a> Visitor<'a> {
return true;
}
for other in types.iter() {
if let EntryKind::Union { members } = &self.schema.get(other).unwrap().kind {
if self.type_matches_set(tp, members) {
return true;
let blah = self.schema.get(other);
if let Some(blah2) = blah {
if let EntryKind::Union { members } = &blah2.kind {
if self.type_matches_set(tp, members) {
return true;
}
}
} else {
return true;
}
}
false
Expand Down
2 changes: 1 addition & 1 deletion shared/tree-sitter-extractor/src/extractor/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Extractor {
let lang = &self.languages[i];

crate::extractor::extract(
lang.ts_language,
&lang.ts_language,
lang.prefix,
&schemas[i],
&mut diagnostics_writer,
Expand Down
26 changes: 19 additions & 7 deletions shared/tree-sitter-extractor/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ pub fn generate(
let tokeninfo_name = format!("{}_tokeninfo", &prefix);
let reserved_word_name = format!("{}_reserved_word", &prefix);
let nodes = node_types::read_node_types_str(&prefix, language.node_types)?;
let (dbscheme_entries, mut ast_node_members, token_kinds) = convert_nodes(&nodes);
let (dbscheme_entries, mut ast_node_members, token_kinds) =
convert_nodes(&nodes, &ast_node_name);
ast_node_members.insert(&token_name);
writeln!(&mut dbscheme_writer, "/*- {} dbscheme -*/", language.name)?;
dbscheme::write(&mut dbscheme_writer, &dbscheme_entries)?;
Expand Down Expand Up @@ -112,6 +113,7 @@ fn make_field_type<'a>(
parent_name: &'a str,
field: &'a node_types::Field,
nodes: &'a node_types::NodeTypeMap,
ast_node_name: &'a str,
) -> (ql::Type<'a>, Option<dbscheme::Entry<'a>>) {
match &field.type_info {
node_types::FieldTypeInfo::Multiple {
Expand All @@ -123,7 +125,10 @@ fn make_field_type<'a>(
// type to represent them.
let members: Set<&str> = types
.iter()
.map(|t| nodes.get(t).unwrap().dbscheme_name.as_str())
.map(|t| match nodes.get(t) {
Some(node) => node.dbscheme_name.as_str(),
None => ast_node_name,
})
.collect();
(
ql::Type::At(dbscheme_union),
Expand Down Expand Up @@ -163,11 +168,13 @@ fn add_field_for_table_storage<'a>(
column_name: &'a str,
has_index: bool,
nodes: &'a node_types::NodeTypeMap,
ast_node_name: &'a str,
) -> (dbscheme::Table<'a>, Option<dbscheme::Entry<'a>>) {
let parent_name = &nodes.get(&field.parent).unwrap().dbscheme_name;
// This field can appear zero or multiple times, so put
// it in an auxiliary table.
let (field_ql_type, field_type_entry) = make_field_type(parent_name, field, nodes);
let (field_ql_type, field_type_entry) =
make_field_type(parent_name, field, nodes, ast_node_name);
let parent_column = dbscheme::Column {
unique: !has_index,
db_type: dbscheme::DbColumnType::Int,
Expand Down Expand Up @@ -212,10 +219,12 @@ fn add_field_for_column_storage<'a>(
field: &'a node_types::Field,
column_name: &'a str,
nodes: &'a node_types::NodeTypeMap,
ast_node_name: &'a str,
) -> (dbscheme::Column<'a>, Option<dbscheme::Entry<'a>>) {
// This field must appear exactly once, so we add it as
// a column to the main table for the node type.
let (field_ql_type, field_type_entry) = make_field_type(parent_name, field, nodes);
let (field_ql_type, field_type_entry) =
make_field_type(parent_name, field, nodes, ast_node_name);
(
dbscheme::Column {
unique: false,
Expand All @@ -235,9 +244,10 @@ fn add_field_for_column_storage<'a>(
/// 2. A set of names of the members of the `<lang>_ast_node` union.
/// 3. A map where the keys are the dbscheme names for token kinds, and the
/// values are their integer representations.
fn convert_nodes(
nodes: &node_types::NodeTypeMap,
) -> (Vec<dbscheme::Entry>, Set<&str>, Map<&str, usize>) {
fn convert_nodes<'a>(
nodes: &'a node_types::NodeTypeMap,
ast_node_name: &'a str,
) -> (Vec<dbscheme::Entry<'a>>, Set<&'a str>, Map<&'a str, usize>) {
let mut entries: Vec<dbscheme::Entry> = Vec::new();
let mut ast_node_members: Set<&str> = Set::new();
let token_kinds: Map<&str, usize> = nodes
Expand Down Expand Up @@ -288,6 +298,7 @@ fn convert_nodes(
field,
column_name,
nodes,
ast_node_name,
);
if let Some(field_type_entry) = field_type_entry {
entries.push(field_type_entry);
Expand All @@ -305,6 +316,7 @@ fn convert_nodes(
column_name,
*has_index,
nodes,
ast_node_name,
);
if let Some(field_type_entry) = field_type_entry {
entries.push(field_type_entry);
Expand Down