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

Replace grammar by antlr/grammars-v4's #73

Merged
merged 16 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,5 @@ Please also have a look at our documentation sections on [usage](https://pytsql.

## Credits

Inspiration drawn from [codebuff](https://github.com/antlr/codebuff/blob/master/grammars/org/antlr/codebuff/tsql.g4).
Grammar is based on [antlr4/grammars-v4](https://github.com/antlr/grammars-v4/tree/master/sql/tsql).

Original authors:
[Alex Gonopolskiy](https://github.com/agonopol)
[Kevin Klein](https://github.com/kklein)
[Encho Mishinev](https://github.com/EnchoMishinevQC)
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ dependencies:
- pyodbc
- speedy-antlr-tool==1.4.1
- antlr4-python3-runtime==4.11.1
- click
1 change: 1 addition & 0 deletions src/pytsql/grammar/TSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -6054,6 +6054,7 @@ id_
| DOUBLE_QUOTE_BLANK
| SQUARE_BRACKET_ID
| keyword
| RAW
;

simple_id
Expand Down
2 changes: 1 addition & 1 deletion src/pytsql/grammar/TSqlParser.interp

Large diffs are not rendered by default.

747 changes: 378 additions & 369 deletions src/pytsql/grammar/TSqlParser.py

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions src/pytsql/grammar/adjust_antlrs_grammar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import click
ivergara marked this conversation as resolved.
Show resolved Hide resolved
from pathlib import Path
import re

PROTECTED_RULE_ELEMENT_LABELS = [
"from",
"id",
"property",
"str",
"format",
"input",
"with",
"type",
"file"
]
CONFLICTING_NAMES_ON_WINDOWS = [
"PLATFORM"
]

# CLI rename_protected_rule_element_labels
@click.group()
ivergara marked this conversation as resolved.
Show resolved Hide resolved
def rename_protected_rule_element_labels_cli():
pass


@rename_protected_rule_element_labels_cli.command()
@click.option(
"-f", "--filepath",
type=click.Path(path_type=Path),
help="Path to a .g4 grammar file.",
)
def rename_protected_rule_element_labels(filepath: str) -> None:
"""
Some rule element labels have the same name as protected objects in C++. Hence, rename those labels.
"""
rename_policy_suffix = "_label"

with open(filepath, 'r+') as file:
grammar = file.read()
for label in PROTECTED_RULE_ELEMENT_LABELS:
grammar = re.sub(fr"(?<=[\( |]){label}(?=[ ]?\=)", f"{label}{rename_policy_suffix}", grammar)

file.seek(0, 0)
file.write(grammar)

# CLI: add_removal_of_specified_names_on_windows
@click.group()
def removal_of_specified_names_on_windows_cli():
pass

@removal_of_specified_names_on_windows_cli.command()
@click.option(
"-f", "--filepath",
type=click.Path(path_type=Path),
help="Path to an auto-generated file by `speedy-antlr-tool`",
)
def add_removal_of_specified_names_on_windows(filepath: str) -> None:
"""
Remove certain protected names for C++ compilation on Windows.

For more see: https://learn.microsoft.com/en-us/windows/win32/menurc/-undef
"""
undef_list = "\n".join([f"#undef {name}" for name in CONFLICTING_NAMES_ON_WINDOWS])

with open(filepath, "r+") as file:

content = file.read()
file.seek(0, 0)
file.write(undef_list + content)

cli = click.CommandCollection(
sources=[rename_protected_rule_element_labels_cli, removal_of_specified_names_on_windows_cli]
)

if __name__ == "__main__":
cli()
4 changes: 2 additions & 2 deletions src/pytsql/grammar/cpp_src/TSqlLexer.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

#undef PLATFORM
// Generated from TSqlLexer.g4 by ANTLR 4.11.1

#pragma once


#include "antlr4-runtime.h"
#undef PLATFORM

xhochy marked this conversation as resolved.
Show resolved Hide resolved



Expand Down
648 changes: 370 additions & 278 deletions src/pytsql/grammar/cpp_src/TSqlParser.cpp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/pytsql/grammar/cpp_src/TSqlParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19533,6 +19533,7 @@ class TSqlParser : public antlr4::Parser {
antlr4::tree::TerminalNode *DOUBLE_QUOTE_BLANK();
antlr4::tree::TerminalNode *SQUARE_BRACKET_ID();
KeywordContext *keyword();
antlr4::tree::TerminalNode *RAW();


virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
Expand Down
2 changes: 1 addition & 1 deletion src/pytsql/grammar/cpp_src/TSqlParser.interp

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/pytsql/grammar/helper_generate_parsers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ cd "$( dirname "${BASH_SOURCE[0]}" )"

antlr4="java -jar $ANTLR4_JAR_FILEPATH"

# Optionally: Download grammar
# curl -o TSqlParser.g4 https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/tsql/TSqlParser.g4
# curl -o TSqlLexer.g4 https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/tsql/TSqlLexer.g4

# Adjust grammar for C++ target
python adjust_antlrs_grammar.py rename-protected-rule-element-labels -f TSqlParser.g4

# Generate C++ target with visitor
$antlr4 -Dlanguage=Cpp -o cpp_src TSqlLexer.g4
$antlr4 -Dlanguage=Cpp -visitor -no-listener -o cpp_src TSqlParser.g4
Expand All @@ -26,3 +33,6 @@ generate(
entry_rule_names=["tsql_file"],
)
EOF

# Adjust auto-generated files for protected names on Windows
python adjust_antlrs_grammar.py add-removal-of-specified-names-on-windows --filepath cpp_src/TSqlLexer.h
11 changes: 11 additions & 0 deletions tests/unit/test_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,14 @@ def test_declare_as_defined_table_type():
DECLARE @test dbo.test_type
"""
assert len(_split(seed)) == 1


def test_token_raw_as_id():
seed = """
CREATE SCHEMA raw;
CREATE TABLE raw.tbl (
a INT
);
SELECT * FROM raw.tbl;
"""
assert len(_split(seed)) == 3