Skip to content

Commit 1645165

Browse files
authored
Merge pull request #9884 from tausbn/ql-untangle-parsers
QL: Untangle the various parsers
2 parents 6b94cdb + 459c2a2 commit 1645165

File tree

21 files changed

+1308
-892
lines changed

21 files changed

+1308
-892
lines changed

ql/Cargo.lock

Lines changed: 108 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ql/extractor/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ edition = "2018"
99
[dependencies]
1010
flate2 = "1.0"
1111
node-types = { path = "../node-types" }
12-
tree-sitter = "0.19"
13-
tree-sitter-ql = { git = "https://github.com/erik-krogh/tree-sitter-ql.git", rev = "343cc5873e20510586ade803659ef8ce153bd603" }
12+
tree-sitter = ">= 0.20, < 0.21"
13+
tree-sitter-ql = { git = "https://github.com/tree-sitter/tree-sitter-ql.git", rev = "4b8078c7fdcce9d4ca06ce3cfec3a61e8c3f4555"}
14+
tree-sitter-ql-dbscheme = { git = "https://github.com/erik-krogh/tree-sitter-ql-dbscheme.git", rev = "63e1344353f63931e88bfbc2faa2e78e1421b213"}
15+
tree-sitter-ql-yaml = {git = "https://github.com/erik-krogh/tree-sitter-ql.git", rev = "cf704bf3671e1ae148e173464fb65a4d2bbf5f99"}
1416
clap = "2.33"
1517
tracing = "0.1"
1618
tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }

ql/extractor/src/main.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ fn main() -> std::io::Result<()> {
8585
let file_list = fs::File::open(file_list)?;
8686

8787
let language = tree_sitter_ql::language();
88+
let dbscheme = tree_sitter_ql_dbscheme::language();
89+
let yaml = tree_sitter_ql_yaml::language();
8890
let schema = node_types::read_node_types_str("ql", tree_sitter_ql::NODE_TYPES)?;
91+
let dbscheme_schema =
92+
node_types::read_node_types_str("dbscheme", tree_sitter_ql_dbscheme::NODE_TYPES)?;
93+
let yaml_schema = node_types::read_node_types_str("yaml", tree_sitter_ql_yaml::NODE_TYPES)?;
94+
8995
let lines: std::io::Result<Vec<String>> = std::io::BufReader::new(file_list).lines().collect();
9096
let lines = lines?;
9197
lines
@@ -105,15 +111,37 @@ fn main() -> std::io::Result<()> {
105111
let source = std::fs::read(&path)?;
106112
let code_ranges = vec![];
107113
let mut trap_writer = trap::Writer::new();
108-
extractor::extract(
109-
language,
110-
"ql",
111-
&schema,
112-
&mut trap_writer,
113-
&path,
114-
&source,
115-
&code_ranges,
116-
)?;
114+
if line.ends_with(".dbscheme") {
115+
extractor::extract(
116+
dbscheme,
117+
"dbscheme",
118+
&dbscheme_schema,
119+
&mut trap_writer,
120+
&path,
121+
&source,
122+
&code_ranges,
123+
)?
124+
} else if line.ends_with("qlpack.yml") {
125+
extractor::extract(
126+
yaml,
127+
"yaml",
128+
&yaml_schema,
129+
&mut trap_writer,
130+
&path,
131+
&source,
132+
&code_ranges,
133+
)?
134+
} else {
135+
extractor::extract(
136+
language,
137+
"ql",
138+
&schema,
139+
&mut trap_writer,
140+
&path,
141+
&source,
142+
&code_ranges,
143+
)?
144+
}
117145
std::fs::create_dir_all(&src_archive_file.parent().unwrap())?;
118146
std::fs::copy(&path, &src_archive_file)?;
119147
write_trap(&trap_dir, path, &trap_writer, trap_compression)

ql/generator/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ clap = "2.33"
1111
node-types = { path = "../node-types" }
1212
tracing = "0.1"
1313
tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }
14-
tree-sitter-ql = { git = "https://github.com/erik-krogh/tree-sitter-ql.git", rev = "343cc5873e20510586ade803659ef8ce153bd603" }
14+
tree-sitter-ql = { git = "https://github.com/tree-sitter/tree-sitter-ql.git", rev = "4b8078c7fdcce9d4ca06ce3cfec3a61e8c3f4555"}
15+
tree-sitter-ql-dbscheme = { git = "https://github.com/erik-krogh/tree-sitter-ql-dbscheme.git", rev = "63e1344353f63931e88bfbc2faa2e78e1421b213"}
16+
tree-sitter-ql-yaml = {git = "https://github.com/erik-krogh/tree-sitter-ql.git", rev = "cf704bf3671e1ae148e173464fb65a4d2bbf5f99"}

ql/generator/src/main.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,20 @@ fn main() -> std::io::Result<()> {
564564
let ql_library_path = matches.value_of("library").expect("missing --library");
565565
let ql_library_path = PathBuf::from(ql_library_path);
566566

567-
let languages = vec![Language {
568-
name: "QL".to_owned(),
569-
node_types: tree_sitter_ql::NODE_TYPES,
570-
}];
567+
let languages = vec![
568+
Language {
569+
name: "QL".to_owned(),
570+
node_types: tree_sitter_ql::NODE_TYPES,
571+
},
572+
Language {
573+
name: "Dbscheme".to_owned(),
574+
node_types: tree_sitter_ql_dbscheme::NODE_TYPES,
575+
},
576+
Language {
577+
name: "Yaml".to_owned(),
578+
node_types: tree_sitter_ql_yaml::NODE_TYPES,
579+
},
580+
];
571581
let mut dbscheme_writer = LineWriter::new(File::create(dbscheme_path)?);
572582
write!(
573583
dbscheme_writer,

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class AstNode extends TAstNode {
3232
node = toQL(this) and
3333
result = node.getLocation()
3434
)
35+
or
36+
result = toGenerateYaml(this).getLocation()
37+
or
38+
result = toDbscheme(this).getLocation()
3539
}
3640

3741
predicate hasLocationInfo(
@@ -111,7 +115,7 @@ class TopLevel extends TTopLevel, AstNode {
111115
ModuleMember getAMember() { result = this.getMember(_) }
112116

113117
/** Gets the `i`'th member of this top-level module. */
114-
ModuleMember getMember(int i) { toQL(result) = file.getChild(i).(QL::ModuleMember).getChild(_) }
118+
ModuleMember getMember(int i) { toQL(result) = file.getChild(i).getChild(_) }
115119

116120
/** Gets a top-level import in this module. */
117121
Import getAnImport() { result = this.getAMember() }
@@ -349,7 +353,7 @@ class Predicate extends TPredicate, AstNode, PredicateOrBuiltin, Declaration {
349353
* A relation in the database.
350354
*/
351355
class Relation extends TDBRelation, AstNode, Declaration {
352-
QL::DbTable table;
356+
Dbscheme::Table table;
353357

354358
Relation() { this = TDBRelation(table) }
355359

@@ -358,9 +362,9 @@ class Relation extends TDBRelation, AstNode, Declaration {
358362
*/
359363
override string getName() { result = table.getTableName().getChild().getValue() }
360364

361-
private QL::DbColumn getColumn(int i) {
365+
private Dbscheme::Column getColumn(int i) {
362366
result =
363-
rank[i + 1](QL::DbColumn column, int child |
367+
rank[i + 1](Dbscheme::Column column, int child |
364368
table.getChild(child) = column
365369
|
366370
column order by child
@@ -373,7 +377,7 @@ class Relation extends TDBRelation, AstNode, Declaration {
373377
/** Gets the `i`th parameter type */
374378
string getParameterType(int i) {
375379
// TODO: This is just using the name of the type, not the actual type. Checkout Type.qll
376-
result = this.getColumn(i).getColType().getChild().(QL::Token).getValue()
380+
result = this.getColumn(i).getColType().getChild().(Dbscheme::Token).getValue()
377381
}
378382

379383
/**
@@ -1171,13 +1175,27 @@ class Import extends TImport, ModuleMember, TypeRef {
11711175
string importedAs() { result = imp.getChild(1).(QL::ModuleName).getChild().getValue() }
11721176

11731177
/**
1174-
* Gets the `i`th selected name from the imported module.
1178+
* Gets the qualified name of the module selected in the import statement.
11751179
* E.g. for
11761180
* `import foo.bar::Baz::Qux`
1177-
* It is true that `getSelectionName(0) = "Baz"` and `getSelectionName(1) = "Qux"`.
1181+
* It is true that `getSelectionName() = "Baz::Qux"`.
1182+
*
1183+
* Does NOT include type arguments!
1184+
*/
1185+
string getSelectionName() { result = this.getModuleExpr().getQualifiedName() }
1186+
1187+
/**
1188+
* Gets the module expression selected in the import statement.
1189+
* E.g. for
1190+
* `import foo.Bar::Baz::Qux`
1191+
* The module expression is the `Bar::Baz::Qux` part.
11781192
*/
1179-
string getSelectionName(int i) {
1180-
result = imp.getChild(0).(QL::ImportModuleExpr).getName(i).getValue()
1193+
ModuleExpr getModuleExpr() { toQL(result) = imp.getChild(0).(QL::ImportModuleExpr).getChild() }
1194+
1195+
override AstNode getAChild(string pred) {
1196+
result = super.getAChild(pred)
1197+
or
1198+
pred = directMember("getModuleExpr") and result = this.getModuleExpr()
11811199
}
11821200

11831201
/**
@@ -1187,27 +1205,25 @@ class Import extends TImport, ModuleMember, TypeRef {
11871205
* It is true that `getQualifiedName(0) = "foo"` and `getQualifiedName(1) = "bar"`.
11881206
*/
11891207
string getQualifiedName(int i) {
1190-
result = imp.getChild(0).(QL::ImportModuleExpr).getChild().getName(i).getValue()
1208+
result = imp.getChild(0).(QL::ImportModuleExpr).getQualName(i).getValue()
11911209
}
11921210

11931211
/**
1194-
* Gets the full string specifying the module being imported.
1212+
* Gets a full string specifying the module being imported.
1213+
*
1214+
* Does NOT include type arguments!
11951215
*/
11961216
string getImportString() {
1197-
exists(string selec |
1198-
not exists(this.getSelectionName(_)) and selec = ""
1217+
exists(string qual |
1218+
not exists(this.getQualifiedName(_)) and qual = ""
11991219
or
1200-
selec =
1201-
"::" + strictconcat(int i, string q | q = this.getSelectionName(i) | q, "::" order by i)
1220+
qual = strictconcat(int i, string q | q = this.getQualifiedName(i) | q, "." order by i) + "."
12021221
|
1203-
result =
1204-
strictconcat(int i, string q | q = this.getQualifiedName(i) | q, "." order by i) + selec
1222+
result = qual + this.getSelectionName()
12051223
)
12061224
}
12071225

1208-
override Type getResolvedType() {
1209-
exists(FileOrModule mod | resolve(this, mod) | result = mod.toType())
1210-
}
1226+
override Type getResolvedType() { result = this.getModuleExpr().getResolvedType() }
12111227
}
12121228

12131229
/** A formula, such as `x = 6 and y < 5`. */
@@ -2282,6 +2298,19 @@ class ModuleExpr extends TModuleExpr, TypeRef {
22822298
SignatureExpr getArgument(int i) {
22832299
result.toQL() = me.getAFieldOrChild().(QL::ModuleInstantiation).getChild(i)
22842300
}
2301+
2302+
/**
2303+
* Gets the qualified name for this module expression, which does not include the type arguments.
2304+
*/
2305+
string getQualifiedName() {
2306+
exists(string qual |
2307+
not exists(this.getQualifier()) and qual = ""
2308+
or
2309+
qual = this.getQualifier().getQualifiedName() + "::"
2310+
|
2311+
result = qual + this.getName()
2312+
)
2313+
}
22852314
}
22862315

22872316
/** A signature expression, either a `PredicateExpr` or a `TypeExpr`. */
@@ -2426,11 +2455,13 @@ module YAML {
24262455
class YAMLNode extends TYamlNode, AstNode {
24272456
/** Holds if the predicate is a root node (has no parent) */
24282457
predicate isRoot() { not exists(this.getParent()) }
2458+
2459+
override AstNode getParent() { toGenerateYaml(result) = toGenerateYaml(this).getParent() }
24292460
}
24302461

24312462
/** A YAML comment. */
24322463
class YamlComment extends TYamlCommemt, YAMLNode {
2433-
QL::YamlComment yamlcomment;
2464+
Yaml::Comment yamlcomment;
24342465

24352466
YamlComment() { this = TYamlCommemt(yamlcomment) }
24362467

@@ -2442,23 +2473,23 @@ module YAML {
24422473

24432474
/** A YAML entry. */
24442475
class YamlEntry extends TYamlEntry, YAMLNode {
2445-
QL::YamlEntry yamle;
2476+
Yaml::Entry yamle;
24462477

24472478
YamlEntry() { this = TYamlEntry(yamle) }
24482479

24492480
/** Gets the key of this YAML entry. */
24502481
YamlKey getKey() {
2451-
exists(QL::YamlKeyvaluepair pair |
2482+
exists(Yaml::Keyvaluepair pair |
24522483
pair.getParent() = yamle and
24532484
result = TYamlKey(pair.getKey())
24542485
)
24552486
}
24562487

2457-
YamlListItem getListItem() { toQL(result).getParent() = yamle }
2488+
YamlListItem getListItem() { toGenerateYaml(result).getParent() = yamle }
24582489

24592490
/** Gets the value of this YAML entry. */
2460-
YAMLValue getValue() {
2461-
exists(QL::YamlKeyvaluepair pair |
2491+
YamlValue getValue() {
2492+
exists(Yaml::Keyvaluepair pair |
24622493
pair.getParent() = yamle and
24632494
result = TYamlValue(pair.getValue())
24642495
)
@@ -2472,15 +2503,15 @@ module YAML {
24722503

24732504
/** A YAML key. */
24742505
class YamlKey extends TYamlKey, YAMLNode {
2475-
QL::YamlKey yamlkey;
2506+
Yaml::Key yamlkey;
24762507

24772508
YamlKey() { this = TYamlKey(yamlkey) }
24782509

24792510
/**
24802511
* Gets the value of this YAML key.
24812512
*/
2482-
YAMLValue getValue() {
2483-
exists(QL::YamlKeyvaluepair pair |
2513+
YamlValue getValue() {
2514+
exists(Yaml::Keyvaluepair pair |
24842515
pair.getKey() = yamlkey and result = TYamlValue(pair.getValue())
24852516
)
24862517
}
@@ -2489,7 +2520,7 @@ module YAML {
24892520

24902521
/** Gets the value of this YAML value. */
24912522
string getNamePart(int i) {
2492-
i = 0 and result = yamlkey.getChild(0).(QL::SimpleId).getValue()
2523+
i = 0 and result = yamlkey.getChild(0).(Yaml::SimpleId).getValue()
24932524
or
24942525
exists(YamlKey child |
24952526
child = TYamlKey(yamlkey.getChild(1)) and
@@ -2511,14 +2542,14 @@ module YAML {
25112542

25122543
/** A YAML list item. */
25132544
class YamlListItem extends TYamlListitem, YAMLNode {
2514-
QL::YamlListitem yamllistitem;
2545+
Yaml::Listitem yamllistitem;
25152546

25162547
YamlListItem() { this = TYamlListitem(yamllistitem) }
25172548

25182549
/**
25192550
* Gets the value of this YAML list item.
25202551
*/
2521-
YAMLValue getValue() { result = TYamlValue(yamllistitem.getChild()) }
2552+
YamlValue getValue() { result = TYamlValue(yamllistitem.getChild()) }
25222553

25232554
override string getAPrimaryQlClass() { result = "YamlListItem" }
25242555
}
@@ -2527,12 +2558,12 @@ module YAML {
25272558
deprecated class YAMLListItem = YamlListItem;
25282559

25292560
/** A YAML value. */
2530-
class YAMLValue extends TYamlValue, YAMLNode {
2531-
QL::YamlValue yamlvalue;
2561+
class YamlValue extends TYamlValue, YAMLNode {
2562+
Yaml::Value yamlvalue;
25322563

2533-
YAMLValue() { this = TYamlValue(yamlvalue) }
2564+
YamlValue() { this = TYamlValue(yamlvalue) }
25342565

2535-
override string getAPrimaryQlClass() { result = "YAMLValue" }
2566+
override string getAPrimaryQlClass() { result = "YamlValue" }
25362567

25372568
/** Gets the value of this YAML value. */
25382569
string getValue() { result = yamlvalue.getValue() }

0 commit comments

Comments
 (0)