Typhon-Language 0.1.3__tar.gz → 0.1.4__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 (74) hide show
  1. typhon_language-0.1.4/LICENSE +201 -0
  2. {typhon_language-0.1.3/src/Typhon_Language.egg-info → typhon_language-0.1.4}/PKG-INFO +7 -5
  3. {typhon_language-0.1.3 → typhon_language-0.1.4}/pyproject.toml +7 -5
  4. typhon_language-0.1.4/src/Typhon/Driver/configs.py +14 -0
  5. typhon_language-0.1.4/src/Typhon/Driver/debugging.py +190 -0
  6. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Driver/diagnostic.py +4 -3
  7. typhon_language-0.1.4/src/Typhon/Driver/language_server.py +25 -0
  8. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Driver/run.py +1 -1
  9. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Driver/translate.py +14 -10
  10. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Driver/utils.py +39 -1
  11. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/_typhon_parser.py +2738 -2525
  12. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/parser.py +80 -53
  13. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/parser_helper.py +68 -87
  14. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/syntax_errors.py +31 -21
  15. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/token_factory_custom.py +541 -485
  16. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/tokenizer_custom.py +52 -0
  17. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/typhon_ast.py +372 -44
  18. typhon_language-0.1.4/src/Typhon/Grammar/typhon_ast_error.py +438 -0
  19. typhon_language-0.1.4/src/Typhon/LanguageServer/__init__.py +3 -0
  20. typhon_language-0.1.4/src/Typhon/LanguageServer/client/__init__.py +42 -0
  21. typhon_language-0.1.4/src/Typhon/LanguageServer/client/pyrefly.py +115 -0
  22. typhon_language-0.1.4/src/Typhon/LanguageServer/client/pyright.py +173 -0
  23. typhon_language-0.1.4/src/Typhon/LanguageServer/semantic_tokens.py +446 -0
  24. typhon_language-0.1.4/src/Typhon/LanguageServer/server.py +376 -0
  25. typhon_language-0.1.4/src/Typhon/LanguageServer/utils.py +65 -0
  26. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/SourceMap/ast_match_based_map.py +199 -152
  27. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/SourceMap/ast_matching.py +102 -87
  28. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/SourceMap/datatype.py +27 -16
  29. typhon_language-0.1.4/src/Typhon/SourceMap/defined_name_retrieve.py +145 -0
  30. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/comprehension_to_function.py +2 -5
  31. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/const_member_to_final.py +12 -7
  32. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/forbidden_statements.py +1 -0
  33. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/optional_operators_to_checked.py +14 -6
  34. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/scope_check_rename.py +44 -18
  35. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/type_abbrev_desugar.py +11 -15
  36. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/type_annotation_check_expand.py +2 -2
  37. typhon_language-0.1.4/src/Typhon/Transform/utils/imports.py +83 -0
  38. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/utils/make_class.py +18 -23
  39. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/visitor.py +25 -0
  40. typhon_language-0.1.4/src/Typhon/Typing/pyrefly.py +145 -0
  41. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Typing/pyright.py +2 -4
  42. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/__main__.py +15 -1
  43. {typhon_language-0.1.3 → typhon_language-0.1.4/src/Typhon_Language.egg-info}/PKG-INFO +7 -5
  44. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon_Language.egg-info/SOURCES.txt +12 -0
  45. typhon_language-0.1.4/src/Typhon_Language.egg-info/requires.txt +8 -0
  46. typhon_language-0.1.3/LICENSE +0 -21
  47. typhon_language-0.1.3/src/Typhon/Driver/debugging.py +0 -47
  48. typhon_language-0.1.3/src/Typhon/Transform/utils/imports.py +0 -48
  49. typhon_language-0.1.3/src/Typhon_Language.egg-info/requires.txt +0 -6
  50. {typhon_language-0.1.3 → typhon_language-0.1.4}/README.md +0 -0
  51. {typhon_language-0.1.3 → typhon_language-0.1.4}/setup.cfg +0 -0
  52. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Driver/type_check.py +0 -0
  53. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/__init__.py +0 -0
  54. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/line_break.py +0 -0
  55. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Grammar/unparse_custom.py +0 -0
  56. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/SourceMap/__init__.py +0 -0
  57. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/__init__.py +0 -0
  58. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/extended_patterns.py +0 -0
  59. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/func_literal_to_def.py +0 -0
  60. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/if_while_let.py +0 -0
  61. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/inline_statement_block_capture.py +0 -0
  62. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/insert_self_to_method.py +0 -0
  63. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/name_generator.py +0 -0
  64. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/placeholder_to_function.py +0 -0
  65. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/record_to_dataclass.py +0 -0
  66. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/transform.py +0 -0
  67. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/utils/__init__.py +0 -0
  68. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Transform/utils/jump_away.py +0 -0
  69. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Typing/__init__.py +0 -0
  70. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/Typing/result_diagnostic.py +0 -0
  71. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon/__init__.py +0 -0
  72. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon_Language.egg-info/dependency_links.txt +0 -0
  73. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon_Language.egg-info/entry_points.txt +0 -0
  74. {typhon_language-0.1.3 → typhon_language-0.1.4}/src/Typhon_Language.egg-info/top_level.txt +0 -0
@@ -0,0 +1,201 @@
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 reasonable and customary use in describing the
141
+ origin of the Work and 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 Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2025- Hirofumi Nakamura
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -1,16 +1,18 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Typhon-Language
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: Typhon programming language
5
5
  Requires-Python: >=3.12
6
6
  Description-Content-Type: text/markdown
7
7
  License-File: LICENSE
8
- Requires-Dist: basedpyright>=1.32.1
8
+ Requires-Dist: basedpyright>=1.37.1
9
9
  Requires-Dist: fire>=0.7.1
10
- Requires-Dist: intervaltree>=3.1.0
10
+ Requires-Dist: intervaltree>=3.2.1
11
11
  Requires-Dist: pegen>=0.3.0
12
- Requires-Dist: pytest>=8.4.1
13
- Requires-Dist: ruff>=0.12.5
12
+ Requires-Dist: pygls>=2.0.0
13
+ Requires-Dist: pyrefly>=0.47.0
14
+ Requires-Dist: pytest>=9.0.2
15
+ Requires-Dist: ruff>=0.14.11
14
16
  Dynamic: license-file
15
17
 
16
18
  # Typhon
@@ -1,16 +1,18 @@
1
1
  [project]
2
2
  name = "Typhon-Language"
3
- version = "0.1.3"
3
+ version = "0.1.4"
4
4
  description = "Typhon programming language"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
7
7
  dependencies = [
8
- "basedpyright>=1.32.1",
8
+ "basedpyright>=1.37.1",
9
9
  "fire>=0.7.1",
10
- "intervaltree>=3.1.0",
10
+ "intervaltree>=3.2.1",
11
11
  "pegen>=0.3.0",
12
- "pytest>=8.4.1",
13
- "ruff>=0.12.5",
12
+ "pygls>=2.0.0",
13
+ "pyrefly>=0.47.0",
14
+ "pytest>=9.0.2",
15
+ "ruff>=0.14.11",
14
16
  ]
15
17
 
16
18
  [project.scripts]
@@ -0,0 +1,14 @@
1
+ from typing import Literal
2
+
3
+ _language_backend: Literal["pyrefly", "pyright"] = "pyrefly"
4
+ # TODO: Basedpyright seems to hang on semantic tokens request
5
+ _language_backend = "pyright"
6
+
7
+
8
+ def set_language_backend(backend: Literal["pyrefly", "pyright"]):
9
+ global _language_backend
10
+ _language_backend = backend
11
+
12
+
13
+ def get_language_backend() -> Literal["pyrefly", "pyright"]:
14
+ return _language_backend
@@ -0,0 +1,190 @@
1
+ from .utils import copy_type, get_project_root
2
+ from typing import BinaryIO, override
3
+ from pathlib import Path
4
+ import logging
5
+
6
+
7
+ _debug = False
8
+ _debug_verbose = False
9
+ _debug_first_error = False
10
+ _debug_log_file: Path | None = None
11
+
12
+
13
+ def set_debug_mode(debug: bool):
14
+ global _debug
15
+ _debug = debug
16
+
17
+
18
+ def set_debug_verbose(verbose: bool):
19
+ global _debug_verbose
20
+ _debug_verbose = verbose
21
+ if verbose:
22
+ set_debug_mode(True)
23
+
24
+
25
+ def set_debug_first_error(first_error: bool):
26
+ global _debug_first_error
27
+ _debug_first_error = first_error
28
+
29
+
30
+ def set_debug_log_file(
31
+ log_file: str | None, verbose: bool = False, append: bool = False
32
+ ):
33
+ global _debug_log_file
34
+ _debug_log_file = Path(log_file) if log_file else None
35
+ if _debug_log_file is not None:
36
+ if verbose:
37
+ set_debug_verbose(True)
38
+ set_debug_mode(True)
39
+ mode = "a" if append else "w"
40
+ with _debug_log_file.open(mode) as f:
41
+ f.write("=== Typhon Debug Log ===\n")
42
+
43
+
44
+ def is_debug_mode() -> bool:
45
+ return _debug
46
+
47
+
48
+ def is_debug_verbose() -> bool:
49
+ return _debug_verbose
50
+
51
+
52
+ # Whether to stop transformation at the first error encountered.
53
+ def is_debug_first_error() -> bool:
54
+ return _debug_first_error
55
+
56
+
57
+ def get_debug_log_file() -> Path | None:
58
+ return _debug_log_file
59
+
60
+
61
+ _debug_printed: int = 0
62
+ _debug_printed_size: int = 0
63
+ _limit_reached_printed: bool = False
64
+
65
+
66
+ def _debug_limit_check(*arg) -> bool:
67
+ global _debug_printed
68
+ global _debug_printed_size
69
+ global _limit_reached_printed
70
+ if _debug_printed > 100000 or _debug_printed_size > 10000000:
71
+ if not _limit_reached_printed:
72
+ print("Debug print limit reached, stopping further debug prints.")
73
+ _limit_reached_printed = True
74
+ return True
75
+ _debug_printed += 1
76
+ _debug_printed_size += sum(len(str(a)) for a in arg)
77
+ return False
78
+
79
+
80
+ @copy_type(print)
81
+ def debug_print(*arg, **kwargs) -> None: # type: ignore
82
+ if _debug:
83
+ if _debug_limit_check(*arg):
84
+ return
85
+ if _debug_log_file is not None:
86
+ with _debug_log_file.open("a") as log_file:
87
+ print(*arg, file=log_file, **kwargs) # type: ignore
88
+ else:
89
+ print(*arg, **kwargs) # type: ignore
90
+
91
+
92
+ @copy_type(print)
93
+ def debug_verbose_print(*arg, **kwargs) -> None: # type: ignore
94
+ if _debug_verbose:
95
+ if _debug_limit_check(*arg):
96
+ return
97
+ if _debug_log_file is not None:
98
+ with _debug_log_file.open("a") as log_file:
99
+ print(*arg, file=log_file, **kwargs) # type: ignore
100
+ else:
101
+ print(*arg, **kwargs) # type: ignore
102
+
103
+
104
+ @copy_type(print)
105
+ def debug_file_write(*arg, **kwargs) -> None: # type: ignore
106
+ if _debug:
107
+ if _debug_log_file is not None:
108
+ if _debug_limit_check(*arg):
109
+ return
110
+ with _debug_log_file.open("a") as log_file:
111
+ print(*arg, file=log_file, **kwargs) # type: ignore
112
+
113
+
114
+ @copy_type(print)
115
+ def debug_file_write_verbose(*arg, **kwargs) -> None: # type: ignore
116
+ if _debug_verbose:
117
+ if _debug_log_file is not None:
118
+ if _debug_limit_check(*arg):
119
+ return
120
+ with _debug_log_file.open("a") as log_file:
121
+ print(*arg, file=log_file, **kwargs) # type: ignore
122
+
123
+
124
+ type ReadableBuffer = bytes | bytearray | memoryview
125
+
126
+
127
+ # Bypass binaryIO class stdio to log
128
+ class BinaryIOLogger(BinaryIO):
129
+ def __init__(self, pipe: BinaryIO):
130
+ self._pipe = pipe
131
+
132
+ def fileno(self) -> int:
133
+ return self._pipe.fileno()
134
+
135
+ def isatty(self) -> bool:
136
+ return self._pipe.isatty()
137
+
138
+ def write(self, data: ReadableBuffer) -> int:
139
+ # Log the data being written
140
+ if _debug_log_file is not None:
141
+ with _debug_log_file.open("a") as log_file:
142
+ log_file.write(f"Writing {data}\n")
143
+ log_file.flush()
144
+ return self._pipe.write(data)
145
+
146
+ def flush(self) -> None:
147
+ self._pipe.flush()
148
+
149
+ def read(self, n: int = -1) -> bytes:
150
+ data = self._pipe.read(n)
151
+ if _debug_log_file is not None:
152
+ with _debug_log_file.open("a") as log_file:
153
+ log_file.write(f"Read {data}\n")
154
+ log_file.flush()
155
+ return data
156
+
157
+ def close(self) -> None:
158
+ if _debug_log_file is not None:
159
+ with _debug_log_file.open("a") as log_file:
160
+ log_file.write("Closing pipe.\n")
161
+ log_file.flush()
162
+ self._pipe.close()
163
+
164
+ @override
165
+ def readline(self, limit: int = -1) -> bytes:
166
+ line = self._pipe.readline(limit)
167
+ if _debug_log_file is not None:
168
+ with _debug_log_file.open("a") as log_file:
169
+ log_file.write(f"Read line {line}\n")
170
+ log_file.flush()
171
+ return line
172
+
173
+
174
+ def debug_setup_logging(verbose: bool = True, append: bool = False) -> None:
175
+ set_debug_log_file(
176
+ str(get_project_root() / "private" / "server1.log"),
177
+ verbose=verbose,
178
+ append=append,
179
+ )
180
+ set_debug_mode(True)
181
+ set_debug_verbose(verbose)
182
+ if (log_file := get_debug_log_file()) is not None:
183
+ logging.basicConfig(
184
+ filename=log_file,
185
+ level=logging.INFO if verbose else logging.DEBUG,
186
+ filemode="a",
187
+ format="%(asctime)s %(levelname)s %(name)s %(message)s",
188
+ force=True,
189
+ )
190
+ debug_file_write("=== Typhon Debug Logging set ===\n")
@@ -9,8 +9,9 @@ def diag_error_file_position(
9
9
  rule: str | None,
10
10
  message: str,
11
11
  ):
12
+ # Range is 0-based, but line numbers displayed are 1-based
12
13
  return (
13
- f"{error_type}: {file_path}:{position.start.line}:{position.start.column}{rule if rule else ''}\n"
14
+ f"{error_type}: {file_path}:{position.start.line + 1}:{position.start.column + 1}{rule if rule else ''}\n"
14
15
  f" {message}"
15
16
  )
16
17
 
@@ -22,8 +23,8 @@ def positioned_source_code(
22
23
  result = ""
23
24
  len_in_digit = len(str(range_in_source.end.line)) + 1
24
25
  for line_num in range(range_in_source.start.line, range_in_source.end.line + 1):
25
- if line_num >= 1 and line_num <= len(source_lines):
26
- result += f" {line_num:<{len_in_digit}}| {source_lines[line_num - 1]}\n"
26
+ if line_num >= 0 and line_num < len(source_lines):
27
+ result += f" {line_num + 1:<{len_in_digit}}| {source_lines[line_num]}\n"
27
28
  if (
28
29
  range_in_source.start.line == range_in_source.end.line
29
30
  and line_num == range_in_source.start.line
@@ -0,0 +1,25 @@
1
+ from ..LanguageServer import server
2
+ from ..Driver.debugging import (
3
+ is_debug_mode,
4
+ BinaryIOLogger,
5
+ set_debug_log_file,
6
+ debug_print,
7
+ get_debug_log_file,
8
+ set_debug_verbose,
9
+ debug_setup_logging,
10
+ )
11
+ import sys
12
+ import logging
13
+
14
+
15
+ def language_server():
16
+ """
17
+ Start the Typhon Language Server.
18
+ """
19
+ server.start_io(
20
+ stdin=BinaryIOLogger(sys.stdin.buffer),
21
+ stdout=BinaryIOLogger(sys.stdout.buffer),
22
+ )
23
+
24
+ # TODO: Support TCP Mode?
25
+ # server.start_tcp("127.0.0.1", 8080)
@@ -92,7 +92,7 @@ def run_directory(source_dir: Path, capture_output: bool, *args: str) -> RunResu
92
92
  # Set up environment with modified PYTHONPATH
93
93
  orig_pythonpath = os.environ.get("PYTHONPATH", "")
94
94
  if orig_pythonpath:
95
- orig_pythonpath = f":{orig_pythonpath}"
95
+ orig_pythonpath = f"{os.pathsep}{orig_pythonpath}"
96
96
  subprocess_env = {
97
97
  **dict(os.environ),
98
98
  # Override PYTHONPATH to include temp_output_dir
@@ -5,7 +5,7 @@ from dataclasses import dataclass
5
5
  from ..Grammar.parser import parse_file
6
6
  from ..Grammar.syntax_errors import (
7
7
  diag_errors,
8
- TyphonSyntaxError,
8
+ TyphonTransformSyntaxError,
9
9
  TyphonSyntaxErrorList,
10
10
  )
11
11
  from ..Grammar.unparse_custom import unparse_custom
@@ -15,12 +15,13 @@ from .utils import (
15
15
  copy_type,
16
16
  default_output_dir,
17
17
  canonicalize_path,
18
+ mkdir_and_setup_init_py,
18
19
  )
19
20
  from ..Transform.transform import transform
20
21
  from .debugging import is_debug_mode, debug_print, is_debug_verbose
21
22
  from ..Driver.type_check import run_type_check, TypeCheckResult
22
23
  from ..SourceMap import SourceMap
23
- from ..SourceMap.ast_match_based_map import map_from_transformed_ast
24
+ from ..SourceMap.ast_match_based_map import map_from_translated_ast
24
25
 
25
26
 
26
27
  @dataclass
@@ -28,7 +29,9 @@ class TranslateResult:
28
29
  source_path_canonical: str
29
30
  output_path_canonical: str
30
31
  source_map: SourceMap | None
31
- syntax_error: SyntaxError | TyphonSyntaxError | TyphonSyntaxErrorList | None
32
+ syntax_error: (
33
+ SyntaxError | TyphonTransformSyntaxError | TyphonSyntaxErrorList | None
34
+ )
32
35
 
33
36
 
34
37
  def translate_file(source: Path, output: Path) -> TranslateResult:
@@ -36,7 +39,7 @@ def translate_file(source: Path, output: Path) -> TranslateResult:
36
39
  try:
37
40
  ast_tree = parse_file(source.as_posix(), verbose=is_debug_verbose())
38
41
  transform(ast_tree)
39
- except (SyntaxError, TyphonSyntaxError, TyphonSyntaxErrorList) as e:
42
+ except (SyntaxError, TyphonTransformSyntaxError, TyphonSyntaxErrorList) as e:
40
43
  debug_print(f"Error parsing file {source}: {e}")
41
44
  return TranslateResult(
42
45
  source_path_canonical=canonicalize_path(source),
@@ -46,8 +49,12 @@ def translate_file(source: Path, output: Path) -> TranslateResult:
46
49
  )
47
50
  translated_code = unparse_custom(ast_tree)
48
51
  source_code = source.read_text(encoding="utf-8")
49
- mapping = map_from_transformed_ast(
50
- ast_tree, ast.parse(translated_code), source_code, source.as_posix()
52
+ mapping = map_from_translated_ast(
53
+ ast_tree,
54
+ ast.parse(translated_code),
55
+ source_code,
56
+ source.as_posix(),
57
+ translated_code,
51
58
  )
52
59
  output.write_text(translated_code)
53
60
  return TranslateResult(
@@ -96,10 +103,7 @@ def translate_directory(
96
103
  output_path = module_output_dir / (source.stem + ".py")
97
104
  file_result = translate_file(source, output_path)
98
105
  result[source] = file_result
99
- if not (module_output_dir / "__init__.py").exists():
100
- (module_output_dir / "__init__.py").write_text(
101
- "# Init file for Typhon module\n", "utf-8"
102
- )
106
+ mkdir_and_setup_init_py(module_output_dir)
103
107
  for subdir in source_dir.iterdir():
104
108
  if subdir.is_dir():
105
109
  sub_output_dir = module_output_dir / subdir.name
@@ -4,6 +4,8 @@ import os
4
4
 
5
5
 
6
6
  TYPHON_EXT = ".typh"
7
+ TYPHON_TEMP_DIR = ".typhon"
8
+ TYPHON_SERVER_TEMP_DIR = ".typhon-server"
7
9
 
8
10
 
9
11
  def shorthand[T](
@@ -29,7 +31,19 @@ def shorthand[T](
29
31
 
30
32
 
31
33
  def default_output_dir(source: str) -> Path:
32
- return Path(source).parent / ".typhon"
34
+ return Path(source).parent / TYPHON_TEMP_DIR
35
+
36
+
37
+ def output_dir_for_workspace(workspace: Path) -> Path:
38
+ return workspace / TYPHON_SERVER_TEMP_DIR
39
+
40
+
41
+ def default_server_output_dir(source: str) -> Path:
42
+ return Path(source).parent / TYPHON_SERVER_TEMP_DIR
43
+
44
+
45
+ def output_dir_for_server_workspace(workspace: Path) -> Path:
46
+ return workspace / TYPHON_SERVER_TEMP_DIR
33
47
 
34
48
 
35
49
  def prepare_default_output_file(source: Path) -> Path:
@@ -39,6 +53,21 @@ def prepare_default_output_file(source: Path) -> Path:
39
53
  return output_file
40
54
 
41
55
 
56
+ def prepare_default_server_output_file(source: Path) -> Path:
57
+ temp_output_dir = default_server_output_dir(source.as_posix())
58
+ temp_output_dir.mkdir(exist_ok=True)
59
+ output_file = temp_output_dir / (source.stem + ".py")
60
+ return output_file
61
+
62
+
63
+ def mkdir_and_setup_init_py(dir: Path) -> None:
64
+ if not dir.exists():
65
+ dir.mkdir(parents=True, exist_ok=True)
66
+ init_file = dir / "__init__.py"
67
+ if not init_file.exists():
68
+ init_file.write_text("# Init file_for Typhon module.", encoding="utf-8")
69
+
70
+
42
71
  def copy_type[**P, T](
43
72
  _: Callable[P, T],
44
73
  ) -> Callable[[Callable[..., T]], Callable[P, T]]:
@@ -52,3 +81,12 @@ def copy_type[**P, T](
52
81
  # For windows, convert all to lower case and resolve.
53
82
  def canonicalize_path(path: Path) -> str:
54
83
  return Path(os.path.normcase(str(path))).resolve().as_posix()
84
+
85
+
86
+ def get_project_root() -> Path:
87
+ current = Path(__file__).resolve()
88
+ for _ in range(10):
89
+ if (current / "pyproject.toml").exists():
90
+ return current
91
+ current = current.parent
92
+ raise FileNotFoundError("Could not find project root with pyproject.toml")