phisql 1.2.0__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.
Files changed (48) hide show
  1. phisql-1.2.0/LICENSE +190 -0
  2. phisql-1.2.0/MANIFEST.in +3 -0
  3. phisql-1.2.0/PKG-INFO +210 -0
  4. phisql-1.2.0/README.md +177 -0
  5. phisql-1.2.0/phisql/__init__.py +58 -0
  6. phisql-1.2.0/phisql/__main__.py +21 -0
  7. phisql-1.2.0/phisql/_data/schema/1.2.0/schema.json +2042 -0
  8. phisql-1.2.0/phisql/_data/spec/v1.0/catalog/entity-types.yaml +200 -0
  9. phisql-1.2.0/phisql/_data/spec/v1.0/catalog/findings.yaml +107 -0
  10. phisql-1.2.0/phisql/_data/spec/v1.0/catalog/keywords.yaml +94 -0
  11. phisql-1.2.0/phisql/_data/spec/v1.0/catalog/policy.yaml +58 -0
  12. phisql-1.2.0/phisql/_data/spec/v1.0/catalog/predicates.yaml +44 -0
  13. phisql-1.2.0/phisql/_data/spec/v1.0/catalog/sources.yaml +75 -0
  14. phisql-1.2.0/phisql/_data/spec/v1.0/catalog/strategies.yaml +155 -0
  15. phisql-1.2.0/phisql/_data/spec/v1.0/catalog/validators.yaml +105 -0
  16. phisql-1.2.0/phisql/_generated/PhiSQLLexer.py +485 -0
  17. phisql-1.2.0/phisql/_generated/PhiSQLListener.py +498 -0
  18. phisql-1.2.0/phisql/_generated/PhiSQLParser.py +4302 -0
  19. phisql-1.2.0/phisql/_generated/PhiSQLVisitor.py +283 -0
  20. phisql-1.2.0/phisql/_generated/__init__.py +23 -0
  21. phisql-1.2.0/phisql/_paths.py +80 -0
  22. phisql-1.2.0/phisql/ast.py +220 -0
  23. phisql-1.2.0/phisql/catalog.py +146 -0
  24. phisql-1.2.0/phisql/cli.py +94 -0
  25. phisql-1.2.0/phisql/compiler.py +580 -0
  26. phisql-1.2.0/phisql/errors.py +26 -0
  27. phisql-1.2.0/phisql/parser.py +300 -0
  28. phisql-1.2.0/phisql/phisql.py +34 -0
  29. phisql-1.2.0/phisql/policy_schema.py +86 -0
  30. phisql-1.2.0/phisql.egg-info/PKG-INFO +210 -0
  31. phisql-1.2.0/phisql.egg-info/SOURCES.txt +46 -0
  32. phisql-1.2.0/phisql.egg-info/dependency_links.txt +1 -0
  33. phisql-1.2.0/phisql.egg-info/entry_points.txt +2 -0
  34. phisql-1.2.0/phisql.egg-info/requires.txt +6 -0
  35. phisql-1.2.0/phisql.egg-info/top_level.txt +1 -0
  36. phisql-1.2.0/pyproject.toml +78 -0
  37. phisql-1.2.0/setup.cfg +4 -0
  38. phisql-1.2.0/setup.py +111 -0
  39. phisql-1.2.0/tests/test_cli.py +77 -0
  40. phisql-1.2.0/tests/test_compile_error.py +111 -0
  41. phisql-1.2.0/tests/test_compiler.py +46 -0
  42. phisql-1.2.0/tests/test_examples_complete.py +73 -0
  43. phisql-1.2.0/tests/test_examples_parse.py +28 -0
  44. phisql-1.2.0/tests/test_examples_schema.py +86 -0
  45. phisql-1.2.0/tests/test_grammar_features.py +183 -0
  46. phisql-1.2.0/tests/test_parse_error.py +94 -0
  47. phisql-1.2.0/tests/test_policy_naming.py +89 -0
  48. phisql-1.2.0/tests/test_policy_schema.py +71 -0
phisql-1.2.0/LICENSE ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for describing the origin of the Work and
141
+ reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Support. While redistributing the Work or
166
+ Derivative Works thereof, You may choose to offer, and charge a
167
+ fee for, acceptance of support, warranty, indemnity, or other
168
+ liability obligations and/or rights consistent with this License.
169
+ However, in accepting such obligations, You may act only on Your
170
+ own behalf and on Your sole responsibility, not on behalf of any
171
+ other Contributor, and only if You agree to indemnify, defend,
172
+ and hold each Contributor harmless for any liability incurred by,
173
+ or claims asserted against, such Contributor by reason of your
174
+ accepting any such warranty or support.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Philterd, LLC.
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
@@ -0,0 +1,3 @@
1
+ # Include the spec data copied into the package at build time (see setup.py)
2
+ # so it is present in source distributions as well as wheels.
3
+ recursive-include phisql/_data *.yaml *.json
phisql-1.2.0/PKG-INFO ADDED
@@ -0,0 +1,210 @@
1
+ Metadata-Version: 2.4
2
+ Name: phisql
3
+ Version: 1.2.0
4
+ Summary: Python reference parser and compiler for the PhiSQL specification.
5
+ Author-email: "Philterd, LLC" <support@philterd.ai>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/philterd/phisql
8
+ Project-URL: Source, https://github.com/philterd/phisql
9
+ Project-URL: Issues, https://github.com/philterd/phisql/issues
10
+ Project-URL: Documentation, https://philterd.github.io/phisql/
11
+ Keywords: phisql,pii,redaction,deidentification,phileas,privacy
12
+ Classifier: Development Status :: 5 - Production/Stable
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Security
22
+ Classifier: Topic :: Software Development :: Compilers
23
+ Classifier: Topic :: Text Processing :: Linguistic
24
+ Requires-Python: >=3.9
25
+ Description-Content-Type: text/markdown
26
+ License-File: LICENSE
27
+ Requires-Dist: PyYAML>=5.4
28
+ Requires-Dist: antlr4-python3-runtime==4.13.2
29
+ Provides-Extra: test
30
+ Requires-Dist: pytest>=7.0; extra == "test"
31
+ Requires-Dist: jsonschema>=4.18.0; extra == "test"
32
+ Dynamic: license-file
33
+
34
+ # PhiSQL Reference Implementation (Python)
35
+
36
+ The Python reference parser and compiler for the [PhiSQL specification](https://github.com/philterd/phisql/tree/main/spec).
37
+ It is a sibling of the [Java reference implementation](https://github.com/philterd/phisql/tree/main/reference/java) and produces the
38
+ same Phileas JSON output for the same input.
39
+
40
+ The lexer and parser are generated from
41
+ [`spec/v1.0/grammar/PhiSQL.g4`](https://github.com/philterd/phisql/blob/main/spec/v1.0/grammar/PhiSQL.g4)
42
+ with ANTLR (the same grammar the Java reference generates from). The generated
43
+ sources are committed under [`phisql/_generated/`](phisql/_generated/) so that
44
+ installing and testing this package stays pure-Python, with no Java needed to
45
+ use it. The parser walks its parse tree into the AST the compiler consumes
46
+ (`phisql/parser.py`).
47
+
48
+ The grammar in the spec remains the single source of truth.
49
+ [`scripts/generate_parser.sh`](scripts/generate_parser.sh) regenerates the
50
+ parser from it, and CI regenerates and runs `git diff --exit-code` over
51
+ `phisql/_generated/`, so any drift between the grammar and the committed parser
52
+ fails the build. (Regenerating needs a JDK to run the ANTLR tool; using and
53
+ testing the package does not.)
54
+
55
+ The compiler is driven by the catalog YAML files under
56
+ [`spec/v1.0/catalog/`](https://github.com/philterd/phisql/tree/main/spec/v1.0/catalog) — the same files the Java
57
+ reference, the spec validator, and the conformance suite use. There is no copy
58
+ of the catalog or grammar inside this directory; both are read from the spec.
59
+
60
+ ## Requirements
61
+
62
+ - Python 3.9+
63
+ - [PyYAML](https://pyyaml.org/) (declared as a dependency)
64
+ - [antlr4-python3-runtime](https://pypi.org/project/antlr4-python3-runtime/) (declared as a dependency; loads the generated parser)
65
+
66
+ A JDK is required only to *regenerate* the parser (see below), not to install,
67
+ use, or test the package.
68
+
69
+ ## Install
70
+
71
+ ```sh
72
+ cd reference/python
73
+ pip install -e ".[test]"
74
+ ```
75
+
76
+ This must be run from a checkout of the repository, because the catalog and
77
+ schema are read from `spec/` and `schema/`. The files are located by walking up
78
+ from the package to find the repository root; set `PHISQL_SPEC_ROOT` to point at
79
+ a checkout's root if you run the package from elsewhere.
80
+
81
+ ## Test
82
+
83
+ ```sh
84
+ cd reference/python
85
+ pytest
86
+ ```
87
+
88
+ The suite mirrors the Java reference's tests:
89
+
90
+ 1. `test_examples_parse.py` parses every `.phisql` file under
91
+ `../../spec/v1.0/examples/` and asserts it produces no syntax errors.
92
+ 2. `test_compiler.py` compiles every redaction example and asserts the output
93
+ equals the sibling `.json` file (compared as parsed JSON).
94
+
95
+ These two are the load-bearing assertions that the implementation stays in sync
96
+ with the spec: any grammar change that breaks an example, or any new example
97
+ the parser or compiler can't handle, fails the build.
98
+
99
+ ## Regenerating the parser
100
+
101
+ The lexer/parser/visitor under `phisql/_generated/` are generated from
102
+ `spec/v1.0/grammar/PhiSQL.g4`. After changing the grammar, regenerate them:
103
+
104
+ ```sh
105
+ cd reference/python
106
+ ./scripts/generate_parser.sh
107
+ ```
108
+
109
+ The script downloads the pinned ANTLR tool jar to a gitignored cache on first
110
+ run (override with `ANTLR_JAR=/path/to/antlr-complete.jar`) and needs a JDK on
111
+ `PATH`. Commit the regenerated files. CI runs the same script and fails if the
112
+ committed output differs, so the grammar stays the single source of truth.
113
+
114
+ ## Usage
115
+
116
+ ### Parse
117
+
118
+ ```python
119
+ from phisql import parse
120
+
121
+ document = parse("POLICY ssn_only; REDACT SSN WITH MASK;")
122
+ # document is an AST (see phisql/ast.py). Walk it directly, or use the compiler.
123
+ ```
124
+
125
+ ### Compile to Phileas JSON
126
+
127
+ ```python
128
+ from phisql import Compiler
129
+
130
+ result = Compiler().compile(
131
+ "POLICY hipaa_safe_harbor;\n"
132
+ "DEIDENTIFY SSN AS REDACT, DATE AS TRUNCATE, EMAIL_ADDRESS AS MASK;"
133
+ )
134
+
135
+ print(result.policy_name()) # "hipaa_safe_harbor"
136
+ print(result.to_json_string()) # Phileas JSON policy
137
+ ```
138
+
139
+ ### Compile from a file
140
+
141
+ ```python
142
+ from phisql import Compiler
143
+
144
+ result = Compiler().compile_file("policies/hipaa-safe-harbor.phisql")
145
+ result.policy_name() # "hipaa-safe-harbor" (from the filename basename)
146
+ ```
147
+
148
+ The policy name comes from the filename basename. A `POLICY` declaration inside
149
+ the file is optional; when present, its name must match the basename after
150
+ hyphen/underscore normalization (so `hipaa-safe-harbor.phisql` may declare
151
+ `POLICY hipaa_safe_harbor`). The compiler raises a `CompileException` on
152
+ mismatch. This rule is defined in
153
+ [`spec/v1.0/catalog/policy.yaml`](https://github.com/philterd/phisql/blob/main/spec/v1.0/catalog/policy.yaml).
154
+
155
+ ### Command line
156
+
157
+ ```sh
158
+ python -m phisql path/to/policy.phisql # or: phisql path/to/policy.phisql
159
+ ```
160
+
161
+ It writes the compiled Phileas JSON to stdout. Exit codes form the adapter
162
+ contract the conformance runner relies on: `0` success, `2` parse error, `3`
163
+ compile error, `64` usage error, `1` other I/O error.
164
+
165
+ ### Retrieve the policy schema
166
+
167
+ An application that depends on `phisql` can read the canonical redaction policy
168
+ JSON Schema straight from the library — no network fetch, no separate checkout —
169
+ exactly as the Java reference exposes it through `ai.philterd.phisql.PolicySchema`:
170
+
171
+ ```python
172
+ from phisql import PolicySchema
173
+
174
+ PolicySchema.get_supported_schema_version() # "1.0.0"
175
+ PolicySchema.get_schema() # the schema as a JSON string
176
+ PolicySchema.get_schema_dict() # the schema parsed into a dict
177
+ ```
178
+
179
+ The schema (and the catalog the compiler uses) are copied into the package at
180
+ build time — see [How the spec data is bundled](#how-the-spec-data-is-bundled) —
181
+ so these work from an installed wheel regardless of where it came from.
182
+
183
+ ## How the spec data is bundled
184
+
185
+ The compiler is driven by the catalog YAML and the policy schema, both of which
186
+ live in the repository root (`spec/` and `schema/`). The build copies them into
187
+ the package at `phisql/_data/` — the Python analogue of the Maven
188
+ `copy-resources` steps that pack the same files into the Java JAR. This is done
189
+ by `setup.py` at build time, so:
190
+
191
+ - an **installed wheel** (or a Git/path install) is self-contained;
192
+ - a **source checkout** falls back to reading `spec/`/`schema/` directly, so the
193
+ tests run without a build step;
194
+ - set `PHISQL_SPEC_ROOT` to a repository root to override the lookup explicitly.
195
+
196
+ The repository remains the single source of truth; `phisql/_data/` is a
197
+ gitignored build artifact, like the Java `target/` resources.
198
+
199
+ ## Scope
200
+
201
+ Like the Java reference, this compiler targets the redaction subset of PhiSQL
202
+ (REDACT, DEIDENTIFY, IGNORE, DEFINE IDENTIFIER, DEFINE DICTIONARY, DEFINE
203
+ SECTION, DETECT PHEYE, and the CONFIGURE forms) and emits Phileas JSON.
204
+ Discovery statements (FIND PII, DISCOVER ENTITIES, SCAN, SELECT FROM findings)
205
+ parse successfully but are not yet compiled; they target a separate
206
+ discovery-query schema.
207
+
208
+ ## License
209
+
210
+ Apache License, Version 2.0. See [LICENSE](https://github.com/philterd/phisql/blob/main/LICENSE).
phisql-1.2.0/README.md ADDED
@@ -0,0 +1,177 @@
1
+ # PhiSQL Reference Implementation (Python)
2
+
3
+ The Python reference parser and compiler for the [PhiSQL specification](https://github.com/philterd/phisql/tree/main/spec).
4
+ It is a sibling of the [Java reference implementation](https://github.com/philterd/phisql/tree/main/reference/java) and produces the
5
+ same Phileas JSON output for the same input.
6
+
7
+ The lexer and parser are generated from
8
+ [`spec/v1.0/grammar/PhiSQL.g4`](https://github.com/philterd/phisql/blob/main/spec/v1.0/grammar/PhiSQL.g4)
9
+ with ANTLR (the same grammar the Java reference generates from). The generated
10
+ sources are committed under [`phisql/_generated/`](phisql/_generated/) so that
11
+ installing and testing this package stays pure-Python, with no Java needed to
12
+ use it. The parser walks its parse tree into the AST the compiler consumes
13
+ (`phisql/parser.py`).
14
+
15
+ The grammar in the spec remains the single source of truth.
16
+ [`scripts/generate_parser.sh`](scripts/generate_parser.sh) regenerates the
17
+ parser from it, and CI regenerates and runs `git diff --exit-code` over
18
+ `phisql/_generated/`, so any drift between the grammar and the committed parser
19
+ fails the build. (Regenerating needs a JDK to run the ANTLR tool; using and
20
+ testing the package does not.)
21
+
22
+ The compiler is driven by the catalog YAML files under
23
+ [`spec/v1.0/catalog/`](https://github.com/philterd/phisql/tree/main/spec/v1.0/catalog) — the same files the Java
24
+ reference, the spec validator, and the conformance suite use. There is no copy
25
+ of the catalog or grammar inside this directory; both are read from the spec.
26
+
27
+ ## Requirements
28
+
29
+ - Python 3.9+
30
+ - [PyYAML](https://pyyaml.org/) (declared as a dependency)
31
+ - [antlr4-python3-runtime](https://pypi.org/project/antlr4-python3-runtime/) (declared as a dependency; loads the generated parser)
32
+
33
+ A JDK is required only to *regenerate* the parser (see below), not to install,
34
+ use, or test the package.
35
+
36
+ ## Install
37
+
38
+ ```sh
39
+ cd reference/python
40
+ pip install -e ".[test]"
41
+ ```
42
+
43
+ This must be run from a checkout of the repository, because the catalog and
44
+ schema are read from `spec/` and `schema/`. The files are located by walking up
45
+ from the package to find the repository root; set `PHISQL_SPEC_ROOT` to point at
46
+ a checkout's root if you run the package from elsewhere.
47
+
48
+ ## Test
49
+
50
+ ```sh
51
+ cd reference/python
52
+ pytest
53
+ ```
54
+
55
+ The suite mirrors the Java reference's tests:
56
+
57
+ 1. `test_examples_parse.py` parses every `.phisql` file under
58
+ `../../spec/v1.0/examples/` and asserts it produces no syntax errors.
59
+ 2. `test_compiler.py` compiles every redaction example and asserts the output
60
+ equals the sibling `.json` file (compared as parsed JSON).
61
+
62
+ These two are the load-bearing assertions that the implementation stays in sync
63
+ with the spec: any grammar change that breaks an example, or any new example
64
+ the parser or compiler can't handle, fails the build.
65
+
66
+ ## Regenerating the parser
67
+
68
+ The lexer/parser/visitor under `phisql/_generated/` are generated from
69
+ `spec/v1.0/grammar/PhiSQL.g4`. After changing the grammar, regenerate them:
70
+
71
+ ```sh
72
+ cd reference/python
73
+ ./scripts/generate_parser.sh
74
+ ```
75
+
76
+ The script downloads the pinned ANTLR tool jar to a gitignored cache on first
77
+ run (override with `ANTLR_JAR=/path/to/antlr-complete.jar`) and needs a JDK on
78
+ `PATH`. Commit the regenerated files. CI runs the same script and fails if the
79
+ committed output differs, so the grammar stays the single source of truth.
80
+
81
+ ## Usage
82
+
83
+ ### Parse
84
+
85
+ ```python
86
+ from phisql import parse
87
+
88
+ document = parse("POLICY ssn_only; REDACT SSN WITH MASK;")
89
+ # document is an AST (see phisql/ast.py). Walk it directly, or use the compiler.
90
+ ```
91
+
92
+ ### Compile to Phileas JSON
93
+
94
+ ```python
95
+ from phisql import Compiler
96
+
97
+ result = Compiler().compile(
98
+ "POLICY hipaa_safe_harbor;\n"
99
+ "DEIDENTIFY SSN AS REDACT, DATE AS TRUNCATE, EMAIL_ADDRESS AS MASK;"
100
+ )
101
+
102
+ print(result.policy_name()) # "hipaa_safe_harbor"
103
+ print(result.to_json_string()) # Phileas JSON policy
104
+ ```
105
+
106
+ ### Compile from a file
107
+
108
+ ```python
109
+ from phisql import Compiler
110
+
111
+ result = Compiler().compile_file("policies/hipaa-safe-harbor.phisql")
112
+ result.policy_name() # "hipaa-safe-harbor" (from the filename basename)
113
+ ```
114
+
115
+ The policy name comes from the filename basename. A `POLICY` declaration inside
116
+ the file is optional; when present, its name must match the basename after
117
+ hyphen/underscore normalization (so `hipaa-safe-harbor.phisql` may declare
118
+ `POLICY hipaa_safe_harbor`). The compiler raises a `CompileException` on
119
+ mismatch. This rule is defined in
120
+ [`spec/v1.0/catalog/policy.yaml`](https://github.com/philterd/phisql/blob/main/spec/v1.0/catalog/policy.yaml).
121
+
122
+ ### Command line
123
+
124
+ ```sh
125
+ python -m phisql path/to/policy.phisql # or: phisql path/to/policy.phisql
126
+ ```
127
+
128
+ It writes the compiled Phileas JSON to stdout. Exit codes form the adapter
129
+ contract the conformance runner relies on: `0` success, `2` parse error, `3`
130
+ compile error, `64` usage error, `1` other I/O error.
131
+
132
+ ### Retrieve the policy schema
133
+
134
+ An application that depends on `phisql` can read the canonical redaction policy
135
+ JSON Schema straight from the library — no network fetch, no separate checkout —
136
+ exactly as the Java reference exposes it through `ai.philterd.phisql.PolicySchema`:
137
+
138
+ ```python
139
+ from phisql import PolicySchema
140
+
141
+ PolicySchema.get_supported_schema_version() # "1.0.0"
142
+ PolicySchema.get_schema() # the schema as a JSON string
143
+ PolicySchema.get_schema_dict() # the schema parsed into a dict
144
+ ```
145
+
146
+ The schema (and the catalog the compiler uses) are copied into the package at
147
+ build time — see [How the spec data is bundled](#how-the-spec-data-is-bundled) —
148
+ so these work from an installed wheel regardless of where it came from.
149
+
150
+ ## How the spec data is bundled
151
+
152
+ The compiler is driven by the catalog YAML and the policy schema, both of which
153
+ live in the repository root (`spec/` and `schema/`). The build copies them into
154
+ the package at `phisql/_data/` — the Python analogue of the Maven
155
+ `copy-resources` steps that pack the same files into the Java JAR. This is done
156
+ by `setup.py` at build time, so:
157
+
158
+ - an **installed wheel** (or a Git/path install) is self-contained;
159
+ - a **source checkout** falls back to reading `spec/`/`schema/` directly, so the
160
+ tests run without a build step;
161
+ - set `PHISQL_SPEC_ROOT` to a repository root to override the lookup explicitly.
162
+
163
+ The repository remains the single source of truth; `phisql/_data/` is a
164
+ gitignored build artifact, like the Java `target/` resources.
165
+
166
+ ## Scope
167
+
168
+ Like the Java reference, this compiler targets the redaction subset of PhiSQL
169
+ (REDACT, DEIDENTIFY, IGNORE, DEFINE IDENTIFIER, DEFINE DICTIONARY, DEFINE
170
+ SECTION, DETECT PHEYE, and the CONFIGURE forms) and emits Phileas JSON.
171
+ Discovery statements (FIND PII, DISCOVER ENTITIES, SCAN, SELECT FROM findings)
172
+ parse successfully but are not yet compiled; they target a separate
173
+ discovery-query schema.
174
+
175
+ ## License
176
+
177
+ Apache License, Version 2.0. See [LICENSE](https://github.com/philterd/phisql/blob/main/LICENSE).
@@ -0,0 +1,58 @@
1
+ # Copyright 2026 Philterd, LLC.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """
15
+ PhiSQL reference implementation (Python).
16
+
17
+ The parser is generated from ``spec/v1.0/grammar/PhiSQL.g4`` with ANTLR (see
18
+ ``phisql._generated``) and the compiler is driven by the catalog files under
19
+ ``spec/v1.0/catalog/``. This package is a sibling of the Java reference
20
+ implementation and produces the same Phileas JSON output.
21
+
22
+ Quick start::
23
+
24
+ from phisql import Compiler
25
+
26
+ result = Compiler().compile("POLICY ssn_only; REDACT SSN WITH MASK;")
27
+ print(result.policy_name()) # "ssn_only"
28
+ print(result.to_json_string()) # Phileas JSON policy
29
+ """
30
+
31
+ from .catalog import Catalog
32
+ from .compiler import Compiler, CompileResult
33
+ from .errors import CompileException, ParseException
34
+ from .phisql import parse
35
+ from .policy_schema import (
36
+ SUPPORTED_SCHEMA_VERSION,
37
+ PolicySchema,
38
+ get_schema,
39
+ get_schema_dict,
40
+ get_supported_schema_version,
41
+ )
42
+
43
+ __version__ = "1.2.0"
44
+
45
+ __all__ = [
46
+ "Catalog",
47
+ "Compiler",
48
+ "CompileResult",
49
+ "CompileException",
50
+ "ParseException",
51
+ "parse",
52
+ "PolicySchema",
53
+ "get_schema",
54
+ "get_schema_dict",
55
+ "get_supported_schema_version",
56
+ "SUPPORTED_SCHEMA_VERSION",
57
+ "__version__",
58
+ ]
@@ -0,0 +1,21 @@
1
+ # Copyright 2026 Philterd, LLC.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Enables ``python -m phisql <file.phisql>``."""
15
+
16
+ import sys
17
+
18
+ from .cli import main
19
+
20
+ if __name__ == "__main__":
21
+ sys.exit(main())