Skip to content

Commit 9c6bb2b

Browse files
committed
Shared: Generate final tree-sitter classes
1 parent da3d0cf commit 9c6bb2b

3 files changed

Lines changed: 116 additions & 47 deletions

File tree

shared/tree-sitter-extractor/src/generator/mod.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,20 @@ pub fn generate(
120120
)));
121121
dbscheme::write(&mut dbscheme_writer, &dbscheme_tail)?;
122122

123-
let mut body = vec![
124-
ql::TopLevel::Class(ql_gen::create_ast_node_class(
125-
&ast_node_name,
126-
&node_location_table_name,
127-
&node_parent_table_name,
128-
)),
129-
ql::TopLevel::Class(ql_gen::create_token_class(&token_name, &tokeninfo_name)),
130-
];
123+
let mut body = vec![];
124+
125+
for c in ql_gen::create_ast_node_class(
126+
&ast_node_name,
127+
&node_location_table_name,
128+
&node_parent_table_name,
129+
) {
130+
body.push(ql::TopLevel::Class(c));
131+
}
132+
133+
for c in ql_gen::create_token_class(&token_name, &tokeninfo_name) {
134+
body.push(ql::TopLevel::Class(c));
135+
}
136+
131137
if has_trivia_tokens {
132138
body.push(ql::TopLevel::Class(ql_gen::create_trivia_token_class(
133139
&trivia_token_name,

shared/tree-sitter-extractor/src/generator/ql.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,29 @@ pub struct Class<'a> {
4040
pub qldoc: Option<String>,
4141
pub name: &'a str,
4242
pub is_abstract: bool,
43+
pub is_final: bool,
44+
pub is_private: bool,
4345
pub supertypes: BTreeSet<Type<'a>>,
4446
pub characteristic_predicate: Option<Expression<'a>>,
4547
pub predicates: Vec<Predicate<'a>>,
48+
pub alias: Option<String>,
4649
}
4750

4851
impl fmt::Display for Class<'_> {
4952
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5053
if let Some(qldoc) = &self.qldoc {
5154
write!(f, "/** {qldoc} */")?;
5255
}
56+
if self.is_final {
57+
write!(f, "final ")?;
58+
}
59+
if self.is_private {
60+
write!(f, "private ")?;
61+
}
62+
if let Some(alias) = &self.alias {
63+
write!(f, "class {} = {alias} ;", &self.name)?;
64+
return Ok(());
65+
}
5366
if self.is_abstract {
5467
write!(f, "abstract ")?;
5568
}

shared/tree-sitter-extractor/src/generator/ql_gen.rs

Lines changed: 89 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn create_ast_node_class<'a>(
88
ast_node: &'a str,
99
node_location_table: &'a str,
1010
node_parent_table: &'a str,
11-
) -> ql::Class<'a> {
11+
) -> [ql::Class<'a>; 2] {
1212
// Default implementation of `toString` calls `this.getAPrimaryQlClass()`
1313
let to_string = ql::Predicate {
1414
qldoc: Some(String::from(
@@ -132,25 +132,41 @@ pub fn create_ast_node_class<'a>(
132132
),
133133
overlay: None,
134134
};
135-
ql::Class {
136-
qldoc: Some(String::from("The base class for all AST nodes")),
137-
name: "AstNode",
138-
is_abstract: false,
139-
supertypes: vec![ql::Type::At(ast_node)].into_iter().collect(),
140-
characteristic_predicate: None,
141-
predicates: vec![
142-
to_string,
143-
get_location,
144-
get_parent,
145-
get_parent_index,
146-
get_a_field_or_child,
147-
get_a_primary_ql_class,
148-
get_primary_ql_classes,
149-
],
150-
}
135+
[
136+
ql::Class {
137+
qldoc: Some(String::from("The base class for all AST nodes")),
138+
name: "AstNodeImpl",
139+
is_abstract: false,
140+
is_final: false,
141+
is_private: true,
142+
alias: None,
143+
supertypes: vec![ql::Type::At(ast_node)].into_iter().collect(),
144+
characteristic_predicate: None,
145+
predicates: vec![
146+
to_string,
147+
get_location,
148+
get_parent,
149+
get_parent_index,
150+
get_a_field_or_child,
151+
get_a_primary_ql_class,
152+
get_primary_ql_classes,
153+
],
154+
},
155+
ql::Class {
156+
qldoc: None,
157+
name: "AstNode",
158+
is_abstract: false,
159+
is_final: true,
160+
is_private: false,
161+
alias: Some("AstNodeImpl".to_string()),
162+
supertypes: vec![].into_iter().collect(),
163+
characteristic_predicate: None,
164+
predicates: vec![],
165+
},
166+
]
151167
}
152168

153-
pub fn create_token_class<'a>(token_type: &'a str, tokeninfo: &'a str) -> ql::Class<'a> {
169+
pub fn create_token_class<'a>(token_type: &'a str, tokeninfo: &'a str) -> [ql::Class<'a>; 2] {
154170
let tokeninfo_arity = 3; // id, kind, value
155171
let get_value = ql::Predicate {
156172
qldoc: Some(String::from("Gets the value of this token.")),
@@ -183,20 +199,36 @@ pub fn create_token_class<'a>(token_type: &'a str, tokeninfo: &'a str) -> ql::Cl
183199
),
184200
overlay: None,
185201
};
186-
ql::Class {
187-
qldoc: Some(String::from("A token.")),
188-
name: "Token",
189-
is_abstract: false,
190-
supertypes: vec![ql::Type::At(token_type), ql::Type::Normal("AstNode")]
191-
.into_iter()
192-
.collect(),
193-
characteristic_predicate: None,
194-
predicates: vec![
195-
get_value,
196-
to_string,
197-
create_get_a_primary_ql_class("Token", false),
198-
],
199-
}
202+
[
203+
ql::Class {
204+
qldoc: Some(String::from("A token.")),
205+
name: "TokenImpl",
206+
is_abstract: false,
207+
is_final: false,
208+
is_private: true,
209+
alias: None,
210+
supertypes: vec![ql::Type::At(token_type), ql::Type::Normal("AstNodeImpl")]
211+
.into_iter()
212+
.collect(),
213+
characteristic_predicate: None,
214+
predicates: vec![
215+
get_value,
216+
to_string,
217+
create_get_a_primary_ql_class("Token", false),
218+
],
219+
},
220+
ql::Class {
221+
qldoc: None,
222+
name: "Token",
223+
is_abstract: false,
224+
is_final: true,
225+
is_private: false,
226+
alias: Some("TokenImpl".to_string()),
227+
supertypes: vec![].into_iter().collect(),
228+
characteristic_predicate: None,
229+
predicates: vec![],
230+
},
231+
]
200232
}
201233

202234
/// Creates the `TriviaToken` class. Trivia tokens (e.g. comments) are
@@ -251,9 +283,15 @@ pub fn create_trivia_token_class<'a>(
251283
)),
252284
name: "TriviaToken",
253285
is_abstract: false,
254-
supertypes: vec![ql::Type::At(trivia_token_type), ql::Type::Normal("AstNode")]
255-
.into_iter()
256-
.collect(),
286+
is_final: true,
287+
is_private: false,
288+
alias: None,
289+
supertypes: vec![
290+
ql::Type::At(trivia_token_type),
291+
ql::Type::Normal("AstNodeImpl"),
292+
]
293+
.into_iter()
294+
.collect(),
257295
characteristic_predicate: None,
258296
predicates: vec![
259297
get_value,
@@ -271,7 +309,10 @@ pub fn create_reserved_word_class(db_name: &str) -> ql::Class<'_> {
271309
qldoc: Some(String::from("A reserved word.")),
272310
name: class_name,
273311
is_abstract: false,
274-
supertypes: vec![ql::Type::At(db_name), ql::Type::Normal("Token")]
312+
is_final: true,
313+
is_private: false,
314+
alias: None,
315+
supertypes: vec![ql::Type::At(db_name), ql::Type::Normal("TokenImpl")]
275316
.into_iter()
276317
.collect(),
277318
characteristic_predicate: None,
@@ -775,11 +816,14 @@ pub fn convert_nodes(nodes: &node_types::NodeTypeMap) -> Vec<ql::TopLevel<'_>> {
775816
create_get_a_primary_ql_class(&node.ql_class_name, true);
776817
let mut supertypes: BTreeSet<ql::Type> = BTreeSet::new();
777818
supertypes.insert(ql::Type::At(&node.dbscheme_name));
778-
supertypes.insert(ql::Type::Normal("Token"));
819+
supertypes.insert(ql::Type::Normal("TokenImpl"));
779820
classes.push(ql::TopLevel::Class(ql::Class {
780821
qldoc: Some(format!("A class representing `{}` tokens.", type_name.kind)),
781822
name: &node.ql_class_name,
782823
is_abstract: false,
824+
is_final: true,
825+
is_private: false,
826+
alias: None,
783827
supertypes,
784828
characteristic_predicate: None,
785829
predicates: vec![get_a_primary_ql_class],
@@ -793,9 +837,12 @@ pub fn convert_nodes(nodes: &node_types::NodeTypeMap) -> Vec<ql::TopLevel<'_>> {
793837
qldoc: None,
794838
name: &node.ql_class_name,
795839
is_abstract: false,
840+
is_final: true,
841+
is_private: false,
842+
alias: None,
796843
supertypes: vec![
797844
ql::Type::At(&node.dbscheme_name),
798-
ql::Type::Normal("AstNode"),
845+
ql::Type::Normal("AstNodeImpl"),
799846
]
800847
.into_iter()
801848
.collect(),
@@ -824,9 +871,12 @@ pub fn convert_nodes(nodes: &node_types::NodeTypeMap) -> Vec<ql::TopLevel<'_>> {
824871
qldoc: Some(format!("A class representing `{}` nodes.", type_name.kind)),
825872
name: main_class_name,
826873
is_abstract: false,
874+
is_final: true,
875+
is_private: false,
876+
alias: None,
827877
supertypes: vec![
828878
ql::Type::At(&node.dbscheme_name),
829-
ql::Type::Normal("AstNode"),
879+
ql::Type::Normal("AstNodeImpl"),
830880
]
831881
.into_iter()
832882
.collect(),

0 commit comments

Comments
 (0)