strling 3.0.0 → 4.0.1
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.
- checksums.yaml +4 -4
- data/LICENSE +201 -0
- data/README.md +31 -67
- data/lib/strling/native_client.rb +231 -0
- data/lib/strling/requests.rb +81 -0
- data/lib/strling/stdlib_generated.rb +32 -0
- data/lib/strling.rb +37 -18
- metadata +17 -18
- data/lib/strling/core/compiler.rb +0 -96
- data/lib/strling/core/errors.rb +0 -174
- data/lib/strling/core/hint_engine.rb +0 -43
- data/lib/strling/core/ir.rb +0 -348
- data/lib/strling/core/nodes.rb +0 -445
- data/lib/strling/core/parser.rb +0 -579
- data/lib/strling/core/validator.rb +0 -101
- data/lib/strling/emitters/pcre2.rb +0 -246
- data/lib/strling/ir.rb +0 -201
- data/lib/strling/nodes.rb +0 -102
- data/lib/strling/simply.rb +0 -90
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 92e0cfe31ef33c7a25939675e900f19312bc4d5f1a6b8017065ed7a3e8d98b8e
|
|
4
|
+
data.tar.gz: 0d5b51771a99a333248f9416b82dd62e7b11b4365df1a51c6117b9ae309adf77
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b68089138194b9e8147f4fe1cf54ef1587b66cef69396e0b4f12437e2e782923067b377cda6bc54c9e79d649b238d0aee1a4cbc3aee8f30a89c8f53ac4b5a9d5
|
|
7
|
+
data.tar.gz: bdb847c32832ce6752a1a0ac510e009cbd8ae3517e483b6dab7f967b7097ad304de9a9d584eeb0336086d613d5a21448bd5c352a325e5ee4f98dca76e8708466
|
data/LICENSE
ADDED
|
@@ -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 [yyyy] [name of copyright owner]
|
|
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.
|
data/README.md
CHANGED
|
@@ -1,88 +1,52 @@
|
|
|
1
|
-
# STRling
|
|
1
|
+
# STRling Ruby adapter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
STRling 4.0.1 is not publicly published. Public installation commands describe
|
|
4
|
+
the proposed separately authorized release. Candidate tests use staged artifacts.
|
|
5
|
+
See the [support matrix](../../docs/support-matrix.md) for exact supported scopes.
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
<tr>
|
|
7
|
-
<td style="padding: 10px;"><img src="https://raw.githubusercontent.com/strling-lang/.github/refs/heads/main/strling_silver_bell.png" alt="STRling Logo" width="100" /></td>
|
|
8
|
-
<td style="padding: 10px;">
|
|
9
|
-
<strong>The Universal Regular Expression Compiler.</strong><br><br>
|
|
10
|
-
STRling is a next-generation production-grade syntax designed to make Regex readable, maintainable, and robust. It abstracts the cryptic nature of raw regex strings into a clean, object-oriented, and strictly typed interface that compiles to standard PCRE2 (or native) patterns.
|
|
11
|
-
</td>
|
|
12
|
-
</tr>
|
|
13
|
-
</table>
|
|
7
|
+
This package is a thin Ruby projection of the canonical STRling compiler. It does not contain a parser, semantic model, validator, target planner, or regex emitter. The caller supplies an absolute path to a `strling.c-abi` version 1 library; Ruby's standard `Fiddle` interface carries strict JSON requests and canonical results across that boundary.
|
|
14
8
|
|
|
15
|
-
|
|
9
|
+
The adapter is a provisional Preview candidate during the Fourth Edition migration. This is not a publication or permanent support-tier promise.
|
|
16
10
|
|
|
17
|
-
|
|
18
|
-
gem install strling
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Or add to your Gemfile:
|
|
11
|
+
## Requirements
|
|
22
12
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
```
|
|
13
|
+
- Ruby `>= 3.0, < 4.0`
|
|
14
|
+
- a compatible native STRling library chosen by the application
|
|
26
15
|
|
|
27
|
-
##
|
|
16
|
+
## Use
|
|
28
17
|
|
|
29
|
-
|
|
18
|
+
Pass the authenticated absolute native-library path as the first program argument.
|
|
19
|
+
The example requires an installed package and its declared dependencies.
|
|
30
20
|
|
|
31
21
|
```ruby
|
|
32
22
|
require 'strling'
|
|
33
23
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
s = Strling::Simply
|
|
37
|
-
|
|
38
|
-
phone_pattern = s.merge(
|
|
39
|
-
s.start, # Start of line
|
|
40
|
-
s.capture(s.digit.times(3)), # 3 digits (area code)
|
|
41
|
-
s.may(s.any_of('-', '.', ' ')), # Optional separator
|
|
42
|
-
s.capture(s.digit.times(3)), # 3 digits (exchange)
|
|
43
|
-
s.may(s.any_of('-', '.', ' ')), # Optional separator
|
|
44
|
-
s.capture(s.digit.times(4)), # 4 digits (line number)
|
|
45
|
-
s.end # End of line
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
# Compile to regex string and test
|
|
49
|
-
regex = Regexp.new(phone_pattern.to_s)
|
|
50
|
-
puts regex.match?('555-123-4567') # true
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
> **Note:** This compiles to the optimized regex: `^(\d{3})[-. ]?(\d{3})[-. ]?(\d{4})$`
|
|
54
|
-
|
|
55
|
-
## 🚀 Why STRling?
|
|
56
|
-
|
|
57
|
-
Regular Expressions are powerful but notorious for being "write-only" code. STRling solves this by treating Regex as **Software**, not a string.
|
|
24
|
+
client = Strling.load_native(ARGV.fetch(0))
|
|
25
|
+
result = client.compile(Strling.source_compile_request('semantic strling 1.0; case sensitive; pattern text "hello";'))
|
|
58
26
|
|
|
59
|
-
|
|
60
|
-
- **🛡️ Type Safety:** Catch syntax errors, invalid ranges, and incompatible flags at **compile time** inside your IDE, not at runtime when your app crashes.
|
|
61
|
-
- **🧠 IntelliSense & Autocomplete:** Stop memorizing cryptic codes like `(?<=...)`. Use fluent, self-documenting methods like `Simply.look_behind(...)` with full IDE discovery.
|
|
62
|
-
- **📖 Readability First:** Code is read far more often than it is written. STRling patterns describe _intent_, making them understandable to junior developers and future maintainers instantly.
|
|
63
|
-
- **🌍 Polyglot Engine:** One mental model, 17 languages. Whether you are writing Rust, Python, or TypeScript, the syntax and behavior remain identical.
|
|
27
|
+
raise 'canonical compile failed' unless result['outcome'] == 'succeeded'
|
|
64
28
|
|
|
65
|
-
|
|
29
|
+
step = Strling.email('root')
|
|
30
|
+
builder = Strling.simply_builder_request([step], 'root')
|
|
31
|
+
simply_result = client.simply_compile(builder)
|
|
66
32
|
|
|
67
|
-
|
|
33
|
+
raise 'Simply compile failed' unless simply_result['status'] == 'success' &&
|
|
34
|
+
simply_result['compile_result']['outcome'] == 'succeeded'
|
|
35
|
+
client.close
|
|
36
|
+
```
|
|
68
37
|
|
|
69
|
-
|
|
70
|
-
- Converts the human-readable STRling syntax into a structured tree.
|
|
71
|
-
2. **Compile**: `AST -> IR` (Intermediate Representation)
|
|
72
|
-
- Transforms the AST into a target-agnostic intermediate representation, optimizing structures like literal sequences.
|
|
73
|
-
3. **Emit**: `IR -> Target Regex`
|
|
74
|
-
- Generates the final, optimized regex string for the specific target engine (e.g., PCRE2, JS, Python `re`).
|
|
38
|
+
The five generated standard-library helpers are lexical-shape recipes. They do not claim that accepted text is semantically valid.
|
|
75
39
|
|
|
76
|
-
|
|
40
|
+
Loading is explicit and fails closed: relative or missing paths, ABI mismatch, missing symbols, invalid UTF-8/JSON, duplicate JSON properties, oversized messages, release failures, and use after close are errors. The package never searches ambient library names and has no subprocess, socket, download, or local-compiler fallback.
|
|
77
41
|
|
|
78
|
-
|
|
79
|
-
- [**Project Hub**](https://github.com/strling-lang/strling/blob/main/README.md): The main STRling repository.
|
|
80
|
-
- [**Specification**](https://github.com/strling-lang/strling/tree/main/spec): The core grammar and semantic specifications.
|
|
42
|
+
See the [canonical interop contract](../../spec/interop/1.0/README.md) and the [migration record](../../docs/migration/dynamic-language-adapter-migration.md).
|
|
81
43
|
|
|
82
|
-
##
|
|
44
|
+
## Proposed public installation
|
|
83
45
|
|
|
84
|
-
|
|
46
|
+
After separately authorized publication, the exact proposed coordinate is:
|
|
85
47
|
|
|
86
|
-
|
|
48
|
+
```bash
|
|
49
|
+
gem install strling --version 4.0.1
|
|
50
|
+
```
|
|
87
51
|
|
|
88
|
-
|
|
52
|
+
Use only the runtime, OS, CPU and native/WASM loading scope declared above and in the support matrix.
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fiddle'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'pathname'
|
|
6
|
+
require 'thread'
|
|
7
|
+
|
|
8
|
+
module Strling
|
|
9
|
+
# Reentrant native client over caller-selected strling.c-abi v1.
|
|
10
|
+
class NativeClient
|
|
11
|
+
STATUS_RESPONSE_WRITTEN = 0
|
|
12
|
+
private_constant :STATUS_RESPONSE_WRITTEN
|
|
13
|
+
|
|
14
|
+
def self.load(library_path)
|
|
15
|
+
path = Pathname.new(String(library_path))
|
|
16
|
+
raise NativeAdapterError.new(:native_load, 'native STRling library path must be absolute') unless path.absolute?
|
|
17
|
+
|
|
18
|
+
normalized = path.cleanpath
|
|
19
|
+
unless normalized.file?
|
|
20
|
+
raise NativeAdapterError.new(:native_load, "native STRling library not found: #{normalized}")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
new(normalized.to_s)
|
|
24
|
+
rescue NativeAdapterError
|
|
25
|
+
raise
|
|
26
|
+
rescue StandardError => e
|
|
27
|
+
raise NativeAdapterError.new(:native_load, "invalid native STRling library path: #{e.message}")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
attr_reader :library_path
|
|
31
|
+
|
|
32
|
+
def initialize(library_path)
|
|
33
|
+
@library_path = library_path.freeze
|
|
34
|
+
@mutex = Mutex.new
|
|
35
|
+
@condition = ConditionVariable.new
|
|
36
|
+
@active_calls = 0
|
|
37
|
+
@closed = false
|
|
38
|
+
load_functions
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def closed?
|
|
42
|
+
@mutex.synchronize { @closed }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def execute(request)
|
|
46
|
+
encoded = JSON.generate(request, allow_nan: false).encode(Encoding::UTF_8)
|
|
47
|
+
raise NativeAdapterError.new(:transport, 'interop request is not valid UTF-8') unless encoded.valid_encoding?
|
|
48
|
+
if encoded.bytesize > MAX_INTEROP_REQUEST_BYTES
|
|
49
|
+
raise NativeAdapterError.new(:transport, "interop request exceeds #{MAX_INTEROP_REQUEST_BYTES} bytes")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
begin_call
|
|
53
|
+
begin
|
|
54
|
+
execute_bytes(encoded)
|
|
55
|
+
ensure
|
|
56
|
+
end_call
|
|
57
|
+
end
|
|
58
|
+
rescue JSON::GeneratorError, EncodingError, TypeError => e
|
|
59
|
+
raise NativeAdapterError.new(:transport, "interop request is not strict JSON: #{e.message}")
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def describe
|
|
63
|
+
completed_result(envelope('describe', {}))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def compile(compile_request, target_profile: nil)
|
|
67
|
+
payload = { 'compile_request' => compile_request }
|
|
68
|
+
payload['target_profile'] = target_profile unless target_profile.nil?
|
|
69
|
+
completed_result(envelope('compile', payload))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def inspect_target_profile(target_profile)
|
|
73
|
+
completed_result(envelope('target_profile.inspect', 'target_profile' => target_profile))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def simply_compile(builder_request, target_profile: nil)
|
|
77
|
+
payload = { 'builder_request' => builder_request }
|
|
78
|
+
payload['target_profile'] = target_profile unless target_profile.nil?
|
|
79
|
+
completed_result(envelope('simply.compile', payload))
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def close
|
|
83
|
+
handle = @mutex.synchronize do
|
|
84
|
+
return nil if @closed
|
|
85
|
+
|
|
86
|
+
@closed = true
|
|
87
|
+
@condition.wait(@mutex) until @active_calls.zero?
|
|
88
|
+
selected = @handle
|
|
89
|
+
@handle = nil
|
|
90
|
+
selected
|
|
91
|
+
end
|
|
92
|
+
handle.close if handle&.respond_to?(:close)
|
|
93
|
+
nil
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
private
|
|
97
|
+
|
|
98
|
+
def load_functions
|
|
99
|
+
# Fiddle does not expose RTLD_LOCAL on every supported Ruby even though
|
|
100
|
+
# local visibility is the native loader default when RTLD_GLOBAL is absent.
|
|
101
|
+
@handle = Fiddle::Handle.new(@library_path, Fiddle::RTLD_NOW)
|
|
102
|
+
@abi = function('strling_interop_abi_version_v1', [], Fiddle::TYPE_UINT)
|
|
103
|
+
@execute = function(
|
|
104
|
+
'strling_interop_execute_v1',
|
|
105
|
+
[Fiddle::TYPE_VOIDP, Fiddle::TYPE_SIZE_T, Fiddle::TYPE_VOIDP],
|
|
106
|
+
Fiddle::TYPE_UINT
|
|
107
|
+
)
|
|
108
|
+
@free = function(
|
|
109
|
+
'strling_interop_owned_bytes_free_v1',
|
|
110
|
+
[Fiddle::TYPE_VOIDP],
|
|
111
|
+
Fiddle::TYPE_UINT
|
|
112
|
+
)
|
|
113
|
+
actual = @abi.call
|
|
114
|
+
return if actual == NATIVE_ABI_VERSION
|
|
115
|
+
|
|
116
|
+
@handle.close
|
|
117
|
+
@handle = nil
|
|
118
|
+
raise NativeAdapterError.new(:native_abi, "expected strling.c-abi #{NATIVE_ABI_VERSION}", status: actual)
|
|
119
|
+
rescue Fiddle::DLError => e
|
|
120
|
+
@handle&.close
|
|
121
|
+
@handle = nil
|
|
122
|
+
raise NativeAdapterError.new(:native_load, "cannot load native STRling library: #{e.message}")
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def function(symbol, arguments, result)
|
|
126
|
+
Fiddle::Function.new(@handle[symbol], arguments, result)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def begin_call
|
|
130
|
+
@mutex.synchronize do
|
|
131
|
+
raise NativeAdapterError.new(:closed_client, 'native STRling client is closed') if @closed
|
|
132
|
+
|
|
133
|
+
@active_calls += 1
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def end_call
|
|
138
|
+
@mutex.synchronize do
|
|
139
|
+
@active_calls -= 1
|
|
140
|
+
@condition.broadcast if @active_calls.zero?
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def execute_bytes(encoded)
|
|
145
|
+
input = Fiddle::Pointer.malloc(encoded.bytesize, Fiddle::RUBY_FREE)
|
|
146
|
+
input[0, encoded.bytesize] = encoded unless encoded.empty?
|
|
147
|
+
descriptor_size = Fiddle::SIZEOF_VOIDP + Fiddle::SIZEOF_SIZE_T
|
|
148
|
+
output = Fiddle::Pointer.malloc(descriptor_size, Fiddle::RUBY_FREE)
|
|
149
|
+
output[0, descriptor_size] = "\0" * descriptor_size
|
|
150
|
+
primary_error = nil
|
|
151
|
+
begin
|
|
152
|
+
status = @execute.call(encoded.empty? ? 0 : input, encoded.bytesize, output)
|
|
153
|
+
unless status == STATUS_RESPONSE_WRITTEN
|
|
154
|
+
raise NativeAdapterError.new(:native_abi, 'native STRling execution failed', status: status)
|
|
155
|
+
end
|
|
156
|
+
address = output[0, Fiddle::SIZEOF_VOIDP].unpack1(pointer_pack)
|
|
157
|
+
length = output[Fiddle::SIZEOF_VOIDP, Fiddle::SIZEOF_SIZE_T].unpack1(size_pack)
|
|
158
|
+
if address.zero? || length.zero? || length > MAX_INTEROP_RESPONSE_BYTES
|
|
159
|
+
raise NativeAdapterError.new(:transport, 'native STRling returned an invalid or oversized response')
|
|
160
|
+
end
|
|
161
|
+
raw = Fiddle::Pointer.new(address)[0, length]
|
|
162
|
+
decode_response(raw)
|
|
163
|
+
rescue StandardError => e
|
|
164
|
+
primary_error = e
|
|
165
|
+
raise
|
|
166
|
+
ensure
|
|
167
|
+
release_status = @free.call(output)
|
|
168
|
+
if primary_error.nil? && release_status != STATUS_RESPONSE_WRITTEN
|
|
169
|
+
raise NativeAdapterError.new(:native_abi, 'native STRling response release failed', status: release_status)
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def pointer_pack
|
|
175
|
+
Fiddle::SIZEOF_VOIDP == 8 ? 'Q' : 'L'
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def size_pack
|
|
179
|
+
Fiddle::SIZEOF_SIZE_T == 8 ? 'Q' : 'L'
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
class StrictObject < Hash
|
|
183
|
+
def []=(key, value)
|
|
184
|
+
raise JSON::ParserError, "duplicate property #{key.inspect}" if key?(key)
|
|
185
|
+
|
|
186
|
+
super
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
private_constant :StrictObject
|
|
190
|
+
|
|
191
|
+
def decode_response(raw)
|
|
192
|
+
text = raw.dup.force_encoding(Encoding::UTF_8)
|
|
193
|
+
raise NativeAdapterError.new(:transport, 'native STRling response is not strict UTF-8') unless text.valid_encoding?
|
|
194
|
+
|
|
195
|
+
value = JSON.parse(text, object_class: StrictObject, create_additions: false)
|
|
196
|
+
unless value.is_a?(Hash) && value['interop_protocol_version'] == INTEROP_PROTOCOL_VERSION
|
|
197
|
+
raise NativeAdapterError.new(:transport, 'interop response has an unsupported version')
|
|
198
|
+
end
|
|
199
|
+
status = value['status']
|
|
200
|
+
unless %w[completed error].include?(status)
|
|
201
|
+
raise NativeAdapterError.new(:transport, 'interop response has an unsupported status')
|
|
202
|
+
end
|
|
203
|
+
if status == 'completed'
|
|
204
|
+
raise NativeAdapterError.new(:transport, 'completed interop response has no result') unless value.key?('result')
|
|
205
|
+
else
|
|
206
|
+
error = value['error']
|
|
207
|
+
unless error.is_a?(Hash) && error['code'].is_a?(String) && error['path'].is_a?(String)
|
|
208
|
+
raise NativeAdapterError.new(:transport, 'failed interop response has no stable code and path')
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
value
|
|
212
|
+
rescue JSON::ParserError => e
|
|
213
|
+
raise NativeAdapterError.new(:transport, "native STRling response is not strict JSON: #{e.message}")
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def envelope(operation, payload)
|
|
217
|
+
{
|
|
218
|
+
'interop_protocol_version' => INTEROP_PROTOCOL_VERSION,
|
|
219
|
+
'operation' => operation,
|
|
220
|
+
'payload' => payload
|
|
221
|
+
}
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def completed_result(request)
|
|
225
|
+
response = execute(request)
|
|
226
|
+
raise InteropProtocolError, response if response['status'] == 'error'
|
|
227
|
+
|
|
228
|
+
response['result']
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Strling
|
|
4
|
+
module_function
|
|
5
|
+
|
|
6
|
+
def source_compile_request(source, options = {})
|
|
7
|
+
prepared = options.transform_keys(&:to_sym)
|
|
8
|
+
specification = prepared.fetch(:specification_version, '1.0-draft.1').to_s
|
|
9
|
+
frontend = prepared.fetch(:frontend_id, 'strling.semantic').to_s
|
|
10
|
+
default_media_type = frontend == 'strling.regex-compat' ? 'text/x-strling-regex-compat' : 'text/x-strling-semantic'
|
|
11
|
+
request = {
|
|
12
|
+
'contract_version' => '1.0.0',
|
|
13
|
+
'specification_version' => specification,
|
|
14
|
+
'input' => {
|
|
15
|
+
'kind' => 'source',
|
|
16
|
+
'document' => {
|
|
17
|
+
'contract_version' => '1.0.0',
|
|
18
|
+
'source_id' => prepared.fetch(:source_id, 'src:ruby.adapter').to_s,
|
|
19
|
+
'specification_version' => specification,
|
|
20
|
+
'frontend' => {
|
|
21
|
+
'id' => frontend,
|
|
22
|
+
'dialect_version' => prepared.fetch(:frontend_version, '1.0.0').to_s
|
|
23
|
+
},
|
|
24
|
+
'content' => {
|
|
25
|
+
'kind' => 'inline',
|
|
26
|
+
'encoding' => 'utf-8',
|
|
27
|
+
'media_type' => prepared.fetch(:media_type, default_media_type).to_s,
|
|
28
|
+
'text' => String(source)
|
|
29
|
+
},
|
|
30
|
+
'provenance' => { 'kind' => 'authored' }
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
'requested_outputs' => prepared.fetch(:requested_outputs, %w[semantic analysis]),
|
|
34
|
+
'compiler_options' => prepared.fetch(
|
|
35
|
+
:compiler_options,
|
|
36
|
+
{
|
|
37
|
+
'partial_semantics' => 'forbid',
|
|
38
|
+
'diagnostic_policy' => { 'minimum_severity' => 'hint' }
|
|
39
|
+
}
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
target = prepared[:target_profile_reference]
|
|
43
|
+
request['target_profile'] = target unless target.nil?
|
|
44
|
+
request
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def stdlib_helper(step_id, helper_id, parameters = {})
|
|
48
|
+
{
|
|
49
|
+
'step_id' => String(step_id),
|
|
50
|
+
'operation' => 'stdlib_helper',
|
|
51
|
+
'arguments' => {
|
|
52
|
+
'helper_id' => String(helper_id),
|
|
53
|
+
'parameters' => parameters
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def simply_builder_request(steps, root_step_id, identity_namespace: 'ruby.adapter')
|
|
59
|
+
{
|
|
60
|
+
'protocol_version' => '1.1.0',
|
|
61
|
+
'contract_version' => '1.0.0',
|
|
62
|
+
'specification_version' => '1.0-draft.1',
|
|
63
|
+
'identity_namespace' => identity_namespace,
|
|
64
|
+
'semantic_options' => {
|
|
65
|
+
'case_matching' => 'sensitive',
|
|
66
|
+
'text_model' => 'unicode_scalar_values',
|
|
67
|
+
'builtin_character_domain' => 'unicode',
|
|
68
|
+
'wildcard_line_terminators' => 'exclude'
|
|
69
|
+
},
|
|
70
|
+
'steps' => steps,
|
|
71
|
+
'root_step_id' => String(root_step_id),
|
|
72
|
+
'compile' => {
|
|
73
|
+
'requested_outputs' => %w[semantic analysis],
|
|
74
|
+
'compiler_options' => {
|
|
75
|
+
'partial_semantics' => 'forbid',
|
|
76
|
+
'diagnostic_policy' => { 'minimum_severity' => 'hint' }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
end
|
|
81
|
+
end
|