sqlfluff-templater-sqlsource 0.2.0b3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlfluff_templater_sqlsource-0.2.0b3/.gitignore +5 -0
- sqlfluff_templater_sqlsource-0.2.0b3/LICENSE +19 -0
- sqlfluff_templater_sqlsource-0.2.0b3/PKG-INFO +38 -0
- sqlfluff_templater_sqlsource-0.2.0b3/README.md +28 -0
- sqlfluff_templater_sqlsource-0.2.0b3/pyproject.toml +41 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/__init__.py +11 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/dialect_marker.py +74 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/dialects.py +120 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/errors.py +64 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/identifiers.py +69 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/lexer.py +261 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/list_uses.py +161 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/marker_scope.py +159 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/markers.py +154 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/plan.py +329 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/quoting.py +236 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/render.py +119 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/rules.py +217 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/settings.py +321 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/stand_ins.py +101 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/templater.py +111 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/text.py +94 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/text_builder.py +188 -0
- sqlfluff_templater_sqlsource-0.2.0b3/sqlfluff_templater_sqlsource/tokens.py +251 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 Michael Crawford
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: sqlfluff-templater-sqlsource
|
|
3
|
+
Version: 0.2.0b3
|
|
4
|
+
Summary: A sqlfluff templater for SqlSource queries
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Requires-Dist: sqlfluff<5,>=4.5
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# sqlfluff-templater-sqlsource
|
|
12
|
+
|
|
13
|
+
A [sqlfluff](https://sqlfluff.com) templater for [SqlSource](https://github.com/mbcrawfo/SqlSource) queries. It lets
|
|
14
|
+
sqlfluff lint and format the `.sql` files of a SqlSource project, with their `{{...}}` tokens and `-- name:` headers
|
|
15
|
+
left as they are.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
pip install sqlfluff sqlfluff-templater-sqlsource==<version>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Replace `<version>` with the version of your `SqlSource` package, as
|
|
24
|
+
[the guide](https://mbcrawfo.github.io/SqlSource/guides/sqlfluff.html#set-up-sqlfluff) says.
|
|
25
|
+
|
|
26
|
+
## Use
|
|
27
|
+
|
|
28
|
+
Select the templater and your database's sqlfluff dialect in the `.sqlfluff` file of the folder you run sqlfluff
|
|
29
|
+
from:
|
|
30
|
+
|
|
31
|
+
```ini
|
|
32
|
+
[sqlfluff]
|
|
33
|
+
templater = sqlsource
|
|
34
|
+
dialect = postgres
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The guide at <https://mbcrawfo.github.io/SqlSource/guides/sqlfluff.html> describes what sqlfluff then checks and how
|
|
38
|
+
to configure it.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# sqlfluff-templater-sqlsource
|
|
2
|
+
|
|
3
|
+
A [sqlfluff](https://sqlfluff.com) templater for [SqlSource](https://github.com/mbcrawfo/SqlSource) queries. It lets
|
|
4
|
+
sqlfluff lint and format the `.sql` files of a SqlSource project, with their `{{...}}` tokens and `-- name:` headers
|
|
5
|
+
left as they are.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pip install sqlfluff sqlfluff-templater-sqlsource==<version>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Replace `<version>` with the version of your `SqlSource` package, as
|
|
14
|
+
[the guide](https://mbcrawfo.github.io/SqlSource/guides/sqlfluff.html#set-up-sqlfluff) says.
|
|
15
|
+
|
|
16
|
+
## Use
|
|
17
|
+
|
|
18
|
+
Select the templater and your database's sqlfluff dialect in the `.sqlfluff` file of the folder you run sqlfluff
|
|
19
|
+
from:
|
|
20
|
+
|
|
21
|
+
```ini
|
|
22
|
+
[sqlfluff]
|
|
23
|
+
templater = sqlsource
|
|
24
|
+
dialect = postgres
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The guide at <https://mbcrawfo.github.io/SqlSource/guides/sqlfluff.html> describes what sqlfluff then checks and how
|
|
28
|
+
to configure it.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
# Not 1.26 or older, which write `License: MIT` and leave out `License-File` for the two keys below.
|
|
3
|
+
requires = ["hatchling>=1.27"]
|
|
4
|
+
build-backend = "hatchling.build"
|
|
5
|
+
|
|
6
|
+
[project]
|
|
7
|
+
name = "sqlfluff-templater-sqlsource"
|
|
8
|
+
# Never edit this: CI sets the real version when it publishes.
|
|
9
|
+
version = "0.2.0b3"
|
|
10
|
+
description = "A sqlfluff templater for SqlSource queries"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
license = "MIT"
|
|
13
|
+
# A copy of the repository's LICENSE, which a test keeps equal to it, since hatchling packs only files of this folder.
|
|
14
|
+
license-files = ["LICENSE"]
|
|
15
|
+
requires-python = ">=3.10"
|
|
16
|
+
# Not 4.4 or older: through 4.4.0 fix moves an inline `-- token:` comment onto a line of its own under LT05, which makes
|
|
17
|
+
# it a marker, or skips the patch around it. 4.5.0 is the first release whose fix keeps every token, default and marker.
|
|
18
|
+
dependencies = ["sqlfluff>=4.5,<5"]
|
|
19
|
+
|
|
20
|
+
[project.entry-points.sqlfluff]
|
|
21
|
+
sqlfluff_templater_sqlsource = "sqlfluff_templater_sqlsource"
|
|
22
|
+
|
|
23
|
+
[dependency-groups]
|
|
24
|
+
dev = ["pytest==9.2.0", "ruff==0.17.5"]
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.wheel]
|
|
27
|
+
packages = ["sqlfluff_templater_sqlsource"]
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.sdist]
|
|
30
|
+
only-include = ["sqlfluff_templater_sqlsource", "README.md", "LICENSE", "pyproject.toml"]
|
|
31
|
+
|
|
32
|
+
[tool.pytest.ini_options]
|
|
33
|
+
testpaths = ["tests"]
|
|
34
|
+
pythonpath = ["tests"]
|
|
35
|
+
|
|
36
|
+
[tool.ruff]
|
|
37
|
+
target-version = "py310"
|
|
38
|
+
line-length = 120
|
|
39
|
+
|
|
40
|
+
[tool.ruff.lint]
|
|
41
|
+
select = ["E", "F", "W", "I", "UP", "B", "SIM", "RUF"]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Registers the SqlSource templater with sqlfluff."""
|
|
2
|
+
|
|
3
|
+
from sqlfluff.core.plugin import hookimpl
|
|
4
|
+
from sqlfluff.core.templaters import RawTemplater
|
|
5
|
+
|
|
6
|
+
from sqlfluff_templater_sqlsource.templater import SqlSourceTemplater
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@hookimpl
|
|
10
|
+
def get_templaters() -> list[type[RawTemplater]]:
|
|
11
|
+
return [SqlSourceTemplater]
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""The `-- dialect:` marker: putting a file's into effect before the text after it is lexed.
|
|
2
|
+
|
|
3
|
+
Mirrors `SqlDialectMarker.cs` in `src/SqlSource/Parsing/`, and the place `SqlFileParser.InvalidDialect` gives the
|
|
4
|
+
error of a `-- dialect:` marker whose value is not a dialect.
|
|
5
|
+
|
|
6
|
+
A file's dialect is set in its header: the comments that come before its first SQL and before its first `-- name:`
|
|
7
|
+
marker. The header is read under the dialect the lexer starts with, and the marker applies from the line after it.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
|
|
12
|
+
from sqlfluff_templater_sqlsource.dialects import DialectChoice, parse_dialect_name
|
|
13
|
+
from sqlfluff_templater_sqlsource.errors import INVALID_MARKER_VALUE, ParseError
|
|
14
|
+
from sqlfluff_templater_sqlsource.lexer import Lexer
|
|
15
|
+
from sqlfluff_templater_sqlsource.markers import Marker, MarkerKind, place_of, read_marker
|
|
16
|
+
from sqlfluff_templater_sqlsource.rules import DialectRules
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class DialectHeader:
|
|
21
|
+
"""What the comments at the start of a file set.
|
|
22
|
+
|
|
23
|
+
`end` is the offset where the header ends: a `-- dialect:` marker that starts at or after it is misplaced.
|
|
24
|
+
`named` is the dialect that the marker put into effect names, or None when none did. `invalid` is the error of
|
|
25
|
+
the first `-- dialect:` marker of the header whose value is not a dialect, or None.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
end: int
|
|
29
|
+
named: DialectChoice | None
|
|
30
|
+
invalid: ParseError | None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def apply_dialect_marker(lexer: Lexer, text: str) -> int:
|
|
34
|
+
"""Reads the comments at the start of `text` from `lexer`, switches it to the dialect that the first valid
|
|
35
|
+
`-- dialect:` marker among them names, and returns the offset where the header ends."""
|
|
36
|
+
return read_dialect_header(lexer, text).end
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def read_dialect_header(lexer: Lexer, text: str) -> DialectHeader:
|
|
40
|
+
"""As `apply_dialect_marker`, and gives the dialect named and the first invalid marker too."""
|
|
41
|
+
named: DialectChoice | None = None
|
|
42
|
+
invalid: ParseError | None = None
|
|
43
|
+
while (comment := lexer.try_read_leading_comment()) is not None:
|
|
44
|
+
marker = read_marker(text, comment)
|
|
45
|
+
if marker is None:
|
|
46
|
+
continue
|
|
47
|
+
|
|
48
|
+
if marker.kind == MarkerKind.NAME:
|
|
49
|
+
return DialectHeader(marker.start, named, invalid)
|
|
50
|
+
|
|
51
|
+
if marker.kind != MarkerKind.DIALECT:
|
|
52
|
+
continue
|
|
53
|
+
|
|
54
|
+
dialect = try_read(text, marker)
|
|
55
|
+
if dialect is None:
|
|
56
|
+
if invalid is None:
|
|
57
|
+
invalid = invalid_dialect(marker)
|
|
58
|
+
elif named is None:
|
|
59
|
+
lexer.rules = DialectRules.for_choice(dialect)
|
|
60
|
+
named = dialect
|
|
61
|
+
|
|
62
|
+
return DialectHeader(lexer.position, named, invalid)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def try_read(text: str, marker: Marker) -> DialectChoice | None:
|
|
66
|
+
"""The dialect that a `-- dialect:` marker's value names, or None for a value that is empty or is not a dialect
|
|
67
|
+
with its options."""
|
|
68
|
+
return parse_dialect_name(text[marker.value_start : marker.value_end])
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def invalid_dialect(marker: Marker) -> ParseError:
|
|
72
|
+
"""The error of a `-- dialect:` marker whose value is not a dialect: at the value, or at the marker when it has
|
|
73
|
+
none."""
|
|
74
|
+
return ParseError(INVALID_MARKER_VALUE, *place_of(marker))
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""The dialects SqlSource reads, their options and their names.
|
|
2
|
+
|
|
3
|
+
Mirrors `SqlDialect.cs`, `SqlDialectOptions.cs`, `SqlDialectChoice.cs` and `SqlDialectName.cs` in
|
|
4
|
+
`src/SqlSource/Parsing/`.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import enum
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from typing import TypeVar
|
|
10
|
+
|
|
11
|
+
from sqlfluff_templater_sqlsource.text import equals_ascii_ignoring_case, trim
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Dialect(enum.Enum):
|
|
15
|
+
"""The database whose rules decide where comments, strings and quoted identifiers start and end, valued by the
|
|
16
|
+
names the C# enum gives them."""
|
|
17
|
+
|
|
18
|
+
ANSI = "Ansi"
|
|
19
|
+
SQL_SERVER = "SqlServer"
|
|
20
|
+
POSTGRESQL = "PostgreSql"
|
|
21
|
+
MYSQL = "MySql"
|
|
22
|
+
MARIADB = "MariaDb"
|
|
23
|
+
SQLITE = "Sqlite"
|
|
24
|
+
ORACLE = "Oracle"
|
|
25
|
+
COCKROACHDB = "CockroachDb"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class DialectOptions(enum.Flag):
|
|
29
|
+
"""Settings of a database server that change one rule of its dialect. Only MySQL and MariaDB have any. The
|
|
30
|
+
values are those of the C# enum."""
|
|
31
|
+
|
|
32
|
+
NONE = 0
|
|
33
|
+
ANSI_QUOTES = 1
|
|
34
|
+
NO_BACKSLASH_ESCAPES = 2
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True)
|
|
38
|
+
class DialectChoice:
|
|
39
|
+
"""What a dialect setting names: a dialect, and the options of that dialect that are on."""
|
|
40
|
+
|
|
41
|
+
dialect: Dialect
|
|
42
|
+
options: DialectOptions = DialectOptions.NONE
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# Each name and alias, with its dialect: the first name of a dialect is its canonical one.
|
|
46
|
+
NAMES: tuple[tuple[str, Dialect], ...] = (
|
|
47
|
+
("ansi", Dialect.ANSI),
|
|
48
|
+
("mssql", Dialect.SQL_SERVER),
|
|
49
|
+
("sqlserver", Dialect.SQL_SERVER),
|
|
50
|
+
("tsql", Dialect.SQL_SERVER),
|
|
51
|
+
("postgres", Dialect.POSTGRESQL),
|
|
52
|
+
("postgresql", Dialect.POSTGRESQL),
|
|
53
|
+
("cockroachdb", Dialect.COCKROACHDB),
|
|
54
|
+
("cockroach", Dialect.COCKROACHDB),
|
|
55
|
+
("mysql", Dialect.MYSQL),
|
|
56
|
+
("mariadb", Dialect.MARIADB),
|
|
57
|
+
("sqlite", Dialect.SQLITE),
|
|
58
|
+
("oracle", Dialect.ORACLE),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# Each name of an option, with the option. An option is also accepted as the server spells its SQL mode.
|
|
62
|
+
OPTIONS: tuple[tuple[str, DialectOptions], ...] = (
|
|
63
|
+
("ansi-quotes", DialectOptions.ANSI_QUOTES),
|
|
64
|
+
("ansi_quotes", DialectOptions.ANSI_QUOTES),
|
|
65
|
+
("no-backslash-escapes", DialectOptions.NO_BACKSLASH_ESCAPES),
|
|
66
|
+
("no_backslash_escapes", DialectOptions.NO_BACKSLASH_ESCAPES),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def canonical_name(dialect: Dialect) -> str:
|
|
71
|
+
"""The name that stands for `dialect` wherever one name is needed: its first in the list."""
|
|
72
|
+
return next(name for name, candidate in NAMES if candidate == dialect)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def option_names(options: DialectOptions) -> list[str]:
|
|
76
|
+
"""The name of each option that is on, the first the list gives it, in the order of the list."""
|
|
77
|
+
names = []
|
|
78
|
+
named = DialectOptions.NONE
|
|
79
|
+
for name, option in OPTIONS:
|
|
80
|
+
if option & options and not option & named:
|
|
81
|
+
names.append(name)
|
|
82
|
+
named |= option
|
|
83
|
+
return names
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def options_of(dialect: Dialect) -> DialectOptions:
|
|
87
|
+
"""The options that `dialect` takes; none for a dialect that takes none."""
|
|
88
|
+
if dialect in (Dialect.MYSQL, Dialect.MARIADB):
|
|
89
|
+
return DialectOptions.ANSI_QUOTES | DialectOptions.NO_BACKSLASH_ESCAPES
|
|
90
|
+
return DialectOptions.NONE
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def parse_dialect_name(value: str) -> DialectChoice | None:
|
|
94
|
+
"""Reads a name or an alias and the options after it, ignoring case and the white space around each part.
|
|
95
|
+
|
|
96
|
+
None for anything else: a name that is not a dialect, an option that does not exist or that the dialect does not
|
|
97
|
+
have, and an empty part. Case is ignored as .NET's ordinal comparison ignores it, so only ASCII letters fold.
|
|
98
|
+
"""
|
|
99
|
+
dialect, *parts = value.split(",")
|
|
100
|
+
found = _find(dialect, NAMES)
|
|
101
|
+
if found is None:
|
|
102
|
+
return None
|
|
103
|
+
|
|
104
|
+
allowed = options_of(found)
|
|
105
|
+
options = DialectOptions.NONE
|
|
106
|
+
for part in parts:
|
|
107
|
+
option = _find(part, OPTIONS)
|
|
108
|
+
if option is None or not option & allowed:
|
|
109
|
+
return None
|
|
110
|
+
options |= option
|
|
111
|
+
return DialectChoice(found, options)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
_T = TypeVar("_T")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _find(part: str, names: tuple[tuple[str, _T], ...]) -> _T | None:
|
|
118
|
+
start, end = trim(part, 0, len(part))
|
|
119
|
+
name = part[start:end]
|
|
120
|
+
return next((value for candidate, value in names if equals_ascii_ignoring_case(name, candidate)), None)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""An error of a file: one that stops its templating plan, or one of the others that the port meets.
|
|
2
|
+
|
|
3
|
+
Mirrors `SqlParseError.cs` in `src/SqlSource/Parsing/`, with the kind given as the code of the diagnostic that
|
|
4
|
+
`SqlDiagnostics.ForParseError` reports for it, and without the arguments of its message.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
|
|
9
|
+
# A quoted region is not closed: SqlParseErrorKind.UnterminatedQuote.
|
|
10
|
+
UNTERMINATED_QUOTE = "SQLSRC101"
|
|
11
|
+
|
|
12
|
+
# A block comment or hint is not closed: SqlParseErrorKind.UnterminatedBlockComment.
|
|
13
|
+
UNTERMINATED_BLOCK_COMMENT = "SQLSRC102"
|
|
14
|
+
|
|
15
|
+
# A marker's value is not valid, as the value of a -- dialect: marker that is not a dialect:
|
|
16
|
+
# SqlParseErrorKind.InvalidMarkerValue.
|
|
17
|
+
INVALID_MARKER_VALUE = "SQLSRC111"
|
|
18
|
+
|
|
19
|
+
# A comment hides the closing braces of a token's default: SqlParseErrorKind.TokenCrossesComment.
|
|
20
|
+
TOKEN_CROSSES_COMMENT = "SQLSRC120"
|
|
21
|
+
|
|
22
|
+
# The errors below never stop a plan: a file with one has a best-effort plan.
|
|
23
|
+
|
|
24
|
+
# A -- name: marker names no usable C# name: SqlParseErrorKind.InvalidName.
|
|
25
|
+
INVALID_NAME = "SQLSRC103"
|
|
26
|
+
|
|
27
|
+
# Two queries of a file have one name: SqlParseErrorKind.DuplicateName.
|
|
28
|
+
DUPLICATE_NAME = "SQLSRC104"
|
|
29
|
+
|
|
30
|
+
# A file without -- name: markers has a name that is no usable C# name: SqlParseErrorKind.InvalidFileName.
|
|
31
|
+
INVALID_FILE_NAME = "SQLSRC105"
|
|
32
|
+
|
|
33
|
+
# SQL stands before a file's first -- name: marker: SqlParseErrorKind.SqlBeforeFirstName.
|
|
34
|
+
SQL_BEFORE_FIRST_NAME = "SQLSRC106"
|
|
35
|
+
|
|
36
|
+
# A -- summary: marker stands before a file's first -- name: marker: SqlParseErrorKind.SummaryBeforeFirstName.
|
|
37
|
+
SUMMARY_BEFORE_FIRST_NAME = "SQLSRC107"
|
|
38
|
+
|
|
39
|
+
# A marker comes after the last SQL of its block: SqlParseErrorKind.MarkerAtEndOfBlock.
|
|
40
|
+
MARKER_AT_END_OF_BLOCK = "SQLSRC108"
|
|
41
|
+
|
|
42
|
+
# Two settings for one thing differ, as two defaults of a token do: SqlParseErrorKind.ConflictingSettings.
|
|
43
|
+
CONFLICTING_SETTINGS = "SQLSRC112"
|
|
44
|
+
|
|
45
|
+
# A block holds no SQL: SqlParseErrorKind.EmptyBlock.
|
|
46
|
+
EMPTY_BLOCK = "SQLSRC113"
|
|
47
|
+
|
|
48
|
+
# A token is named by a reserved C# keyword: SqlParseErrorKind.ReservedTokenName.
|
|
49
|
+
RESERVED_TOKEN_NAME = "SQLSRC114"
|
|
50
|
+
|
|
51
|
+
# A -- dialect: marker stands after the file's header: SqlParseErrorKind.MisplacedDialect.
|
|
52
|
+
MISPLACED_DIALECT = "SQLSRC115"
|
|
53
|
+
|
|
54
|
+
# A marker stands where its scope does not allow it: SqlParseErrorKind.MarkerNotAllowedHere.
|
|
55
|
+
MARKER_NOT_ALLOWED_HERE = "SQLSRC116"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True)
|
|
59
|
+
class ParseError:
|
|
60
|
+
"""A problem found in a file: its diagnostic's code, and where it is, as offsets into the file's text."""
|
|
61
|
+
|
|
62
|
+
code: str
|
|
63
|
+
start: int
|
|
64
|
+
end: int
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""The C# identifier rules that a token's name and a query's name meet.
|
|
2
|
+
|
|
3
|
+
Mirrors `SqlIdentifier.cs` in `src/SqlSource/`, and the methods of Roslyn's `SyntaxFacts` that it and the token
|
|
4
|
+
scanner call: `IsIdentifierStartCharacter`, `IsIdentifierPartCharacter`, `IsValidIdentifier` and `GetKeywordKind`, as
|
|
5
|
+
Microsoft.CodeAnalysis.CSharp 4.14.0 has them.
|
|
6
|
+
|
|
7
|
+
Roslyn tests UTF-16 code units, as `text.py` explains, so a code point outside the BMP is a surrogate here: no part of
|
|
8
|
+
an identifier.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from sqlfluff_templater_sqlsource.text import category
|
|
12
|
+
|
|
13
|
+
# The categories of a letter to Roslyn: a letter number counts.
|
|
14
|
+
_LETTER_CATEGORIES = frozenset(("Lu", "Ll", "Lt", "Lm", "Lo", "Nl"))
|
|
15
|
+
|
|
16
|
+
# What else may follow the first character: a decimal digit, a connector, a combining mark and a format character.
|
|
17
|
+
_PART_CATEGORIES = _LETTER_CATEGORIES | {"Nd", "Pc", "Mn", "Mc", "Cf"}
|
|
18
|
+
|
|
19
|
+
# The words that SyntaxFacts.GetKeywordKind gives a kind: the reserved keywords, and Roslyn's four undocumented ones.
|
|
20
|
+
# A contextual keyword such as `where` is none.
|
|
21
|
+
# fmt: off
|
|
22
|
+
_RESERVED_KEYWORDS = frozenset((
|
|
23
|
+
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const",
|
|
24
|
+
"continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern",
|
|
25
|
+
"false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface",
|
|
26
|
+
"internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", "params",
|
|
27
|
+
"private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof",
|
|
28
|
+
"stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong",
|
|
29
|
+
"unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while", "__arglist", "__makeref",
|
|
30
|
+
"__reftype", "__refvalue",
|
|
31
|
+
))
|
|
32
|
+
# fmt: on
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def is_identifier_start_character(value: str) -> bool:
|
|
36
|
+
"""As `SyntaxFacts.IsIdentifierStartCharacter`: an ASCII letter, `_`, or a letter above U+007F."""
|
|
37
|
+
if value.isascii():
|
|
38
|
+
return value.isalpha() or value == "_"
|
|
39
|
+
return category(value) in _LETTER_CATEGORIES
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def is_identifier_part_character(value: str) -> bool:
|
|
43
|
+
"""As `SyntaxFacts.IsIdentifierPartCharacter`: an ASCII letter or digit, `_`, or above U+007F a letter, a decimal
|
|
44
|
+
digit, a connector, a combining mark or a format character."""
|
|
45
|
+
if value.isascii():
|
|
46
|
+
return value.isalnum() or value == "_"
|
|
47
|
+
return category(value) in _PART_CATEGORIES
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def is_valid(value: str) -> bool:
|
|
51
|
+
"""True when `value` has the form of an identifier. Keywords pass. A format character such as a zero-width space
|
|
52
|
+
does not: C# allows one in an identifier and then ignores it, so two names that differ only by one would collide
|
|
53
|
+
in generated code."""
|
|
54
|
+
return (
|
|
55
|
+
value != ""
|
|
56
|
+
and is_identifier_start_character(value[0])
|
|
57
|
+
and all(is_identifier_part_character(character) for character in value[1:])
|
|
58
|
+
and not any(category(character) == "Cf" for character in value)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def is_reserved_keyword(value: str) -> bool:
|
|
63
|
+
"""True for a reserved keyword such as `class`. A contextual keyword such as `where` is not one."""
|
|
64
|
+
return value in _RESERVED_KEYWORDS
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def is_usable_name(value: str) -> bool:
|
|
68
|
+
"""True when `value` can be the name of a generated member or parameter."""
|
|
69
|
+
return is_valid(value) and not is_reserved_keyword(value)
|