Codegen/Rust: allow breaking up schema file · github/codeql@a5e3fbf · GitHub
Skip to content

Commit a5e3fbf

Browse files
author
Paolo Tranquilli
committed
Codegen/Rust: allow breaking up schema file
1 parent 3c09f70 commit a5e3fbf

8 files changed

Lines changed: 74 additions & 64 deletions

File tree

‎misc/codegen/codegen.py‎

Lines changed: 4 additions & 2 deletions

‎misc/codegen/loaders/schemaloader.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" schema loader """
2+
import sys
23

34
import inflection
45
import typing
@@ -140,6 +141,8 @@ def load(m: types.ModuleType) -> schema.Schema:
140141
continue
141142
if name.startswith("__"):
142143
continue
144+
if isinstance(data, types.ModuleType):
145+
continue
143146
cls = _get_class(data)
144147
if classes and not cls.bases:
145148
raise schema.Error(
@@ -160,7 +163,10 @@ def load(m: types.ModuleType) -> schema.Schema:
160163

161164

162165
def load_file(path: pathlib.Path) -> schema.Schema:
163-
spec = importlib.util.spec_from_file_location("schema", path)
164-
module = importlib.util.module_from_spec(spec)
165-
spec.loader.exec_module(module)
166+
assert path.suffix in ("", ".py")
167+
sys.path.insert(0, str(path.parent))
168+
try:
169+
module = importlib.import_module(path.with_suffix("").name)
170+
finally:
171+
sys.path.remove(str(path.parent))
166172
return load(module)

‎rust/BUILD.bazel‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package(default_visibility = ["//rust:__subpackages__"])
99

1010
filegroup(
1111
name = "schema",
12-
srcs = ["schema.py"],
12+
srcs = glob(["schema/*.py"]),
1313
)
1414

1515
filegroup(

‎rust/codegen.conf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# configuration file for Rust code generation default options
2+
--schema=schema
23
--generate=dbscheme,rusttest,ql,rust
34
--dbscheme=ql/lib/rust.dbscheme
45
--ql-output=ql/lib/codeql/rust/elements/internal/generated

‎rust/ql/lib/rust.dbscheme‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ locatable_locations(
117117
);
118118

119119

120-
// from schema.py
120+
// from schema
121121

122122
@element =
123123
@locatable

‎rust/schema/__init__.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Schema description
3+
4+
This file should be kept simple:
5+
* no flow control
6+
* no aliases
7+
* only class definitions with annotations and `include` calls
8+
9+
For how documentation of generated QL code works, please read `misc/codegen/schema_documentation.md`.
10+
"""
11+
12+
from .prelude import *
13+
from .ast import *
14+
15+
include("../shared/tree-sitter-extractor/src/generator/prefix.dbscheme")
16+
include("prefix.dbscheme")

rust/schema.py renamed to rust/schema/ast.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,4 @@
1-
"""
2-
Schema description
3-
4-
This file should be kept simple:
5-
* no flow control
6-
* no aliases
7-
* only class definitions with annotations and `include` calls
8-
9-
For how documentation of generated QL code works, please read `misc/codegen/schema_documentation.md`.
10-
"""
11-
12-
from misc.codegen.lib.schemadefs import *
13-
14-
include("../shared/tree-sitter-extractor/src/generator/prefix.dbscheme")
15-
include("prefix.dbscheme")
16-
17-
18-
@qltest.skip
19-
class Element:
20-
pass
21-
22-
23-
@qltest.skip
24-
class Locatable(Element):
25-
pass
26-
27-
28-
@qltest.skip
29-
class AstNode(Locatable):
30-
pass
31-
32-
33-
@qltest.skip
34-
class Unextracted(Element):
35-
"""
36-
The base class marking everything that was not properly extracted for some reason, such as:
37-
* syntax errors
38-
* insufficient context information
39-
* yet unimplemented parts of the extractor
40-
"""
41-
pass
42-
43-
44-
@qltest.skip
45-
class Missing(Unextracted):
46-
"""
47-
The base class marking errors during parsing or resolution.
48-
"""
49-
50-
51-
@qltest.skip
52-
class Unimplemented(Unextracted):
53-
"""
54-
The base class for unimplemented nodes. This is used to mark nodes that are not yet extracted.
55-
"""
56-
pass
57-
1+
from .prelude import *
582

593
class AssocItem(AstNode):
604
pass

‎rust/schema/prelude.py‎

Lines changed: 41 additions & 0 deletions

0 commit comments

Comments
 (0)