kumi-parser 0.0.2 → 0.0.3
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/.rspec +3 -0
- data/LICENSE +21 -0
- data/README.md +76 -0
- data/Rakefile +10 -0
- data/examples/debug_text_parser.rb +41 -0
- data/examples/debug_transform_rule.rb +26 -0
- data/examples/text_parser_comprehensive_test.rb +333 -0
- data/examples/text_parser_test_with_comments.rb +146 -0
- data/kumi-parser.gemspec +45 -0
- data/lib/kumi/parser/analyzer_diagnostic_converter.rb +84 -0
- data/lib/kumi/parser/error_extractor.rb +89 -0
- data/lib/kumi/parser/syntax_validator.rb +43 -0
- data/lib/kumi/parser/text_parser/api.rb +60 -0
- data/lib/kumi/parser/text_parser/editor_diagnostic.rb +102 -0
- data/lib/kumi/parser/text_parser/grammar.rb +214 -0
- data/lib/kumi/parser/text_parser/parser.rb +168 -0
- data/lib/kumi/parser/text_parser/transform.rb +170 -0
- data/lib/kumi/parser/text_parser.rb +53 -0
- data/lib/kumi/parser/version.rb +7 -0
- data/lib/kumi/parser.rb +8 -0
- data/lib/kumi-parser.rb +18 -0
- data/test_basic.rb +44 -0
- metadata +24 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14750f2db79c5e3125f477a55452f97c53a919dfd14f6731ebef89169b6a25a3
|
4
|
+
data.tar.gz: 2c617dd3c673d580c0ea4dcee97d0a82cc34d69e8d23aefd7e6cc53c9e288fcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe96cecd1682fde3006a890413e82aa2d8963400a160b5a808d4cb841bfe702c72624ab4b3abb9abc3829745e4355fcecb7f096300c5b1f13e2b8cdcd2475f34
|
7
|
+
data.tar.gz: ab9576fa7828bedcd731567a37e1b977691cc5aa4a87ba2b2c1faf3a83299eabe466c4a62165548cef4178dd3648f012167378581b70a58d0e2435206fce5d5f
|
data/.rspec
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Kumi Team
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Kumi::Parser
|
2
|
+
|
3
|
+
Text parser for [Kumi](https://github.com/amuta/kumi). Allows Kumi schemas to be written as plain text with syntax validation and editor integration.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'kumi-parser'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install kumi-parser
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Basic Parsing
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'kumi/parser'
|
27
|
+
|
28
|
+
schema_text = <<~SCHEMA
|
29
|
+
schema do
|
30
|
+
input do
|
31
|
+
integer :age, domain: 18..65
|
32
|
+
string :status, domain: %w[active inactive]
|
33
|
+
end
|
34
|
+
|
35
|
+
trait :adult, input.age >= 18
|
36
|
+
value :bonus, 100
|
37
|
+
end
|
38
|
+
SCHEMA
|
39
|
+
|
40
|
+
# Parse and get AST
|
41
|
+
ast = Kumi::Parser::TextParser.parse(schema_text)
|
42
|
+
|
43
|
+
# Validate syntax
|
44
|
+
diagnostics = Kumi::Parser::TextParser.validate(schema_text)
|
45
|
+
puts "Valid!" if diagnostics.empty?
|
46
|
+
```
|
47
|
+
|
48
|
+
### Editor Integration
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
# Get diagnostics for Monaco Editor
|
52
|
+
monaco_diagnostics = Kumi::Parser::TextParser.diagnostics_for_monaco(schema_text)
|
53
|
+
|
54
|
+
# Get diagnostics for CodeMirror
|
55
|
+
codemirror_diagnostics = Kumi::Parser::TextParser.diagnostics_for_codemirror(schema_text)
|
56
|
+
|
57
|
+
# Get diagnostics as JSON
|
58
|
+
json_diagnostics = Kumi::Parser::TextParser.diagnostics_as_json(schema_text)
|
59
|
+
```
|
60
|
+
|
61
|
+
## API Reference
|
62
|
+
|
63
|
+
- `parse(text)` - Parse schema text and return AST
|
64
|
+
- `validate(text)` - Validate syntax and return diagnostics
|
65
|
+
- `valid?(text)` - Quick validation check (returns boolean)
|
66
|
+
- `diagnostics_for_monaco(text)` - Get Monaco Editor format diagnostics
|
67
|
+
- `diagnostics_for_codemirror(text)` - Get CodeMirror format diagnostics
|
68
|
+
- `diagnostics_as_json(text)` - Get JSON format diagnostics
|
69
|
+
|
70
|
+
## Development
|
71
|
+
|
72
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
MIT License
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Debug the text parser transform
|
2
|
+
|
3
|
+
require_relative 'lib/kumi/text_parser'
|
4
|
+
|
5
|
+
schema_text = <<~SCHEMA
|
6
|
+
schema do
|
7
|
+
input do
|
8
|
+
integer :age
|
9
|
+
end
|
10
|
+
#{' '}
|
11
|
+
trait :adult, input.age >= 18
|
12
|
+
value :bonus, 100
|
13
|
+
end
|
14
|
+
SCHEMA
|
15
|
+
|
16
|
+
puts 'Debugging text parser...'
|
17
|
+
|
18
|
+
begin
|
19
|
+
# Test just the grammar parsing first
|
20
|
+
grammar = Kumi::TextParser::Grammar.new
|
21
|
+
parse_tree = grammar.parse(schema_text)
|
22
|
+
|
23
|
+
puts 'Raw parse tree:'
|
24
|
+
puts parse_tree.inspect
|
25
|
+
puts
|
26
|
+
|
27
|
+
# Now test the transform
|
28
|
+
transform = Kumi::TextParser::Transform.new
|
29
|
+
ast = transform.apply(parse_tree)
|
30
|
+
|
31
|
+
puts 'Transformed AST:'
|
32
|
+
puts ast.inspect
|
33
|
+
puts
|
34
|
+
|
35
|
+
puts 'AST structure:'
|
36
|
+
puts "- Values: #{ast.attributes.count} - #{ast.attributes.map(&:name)}"
|
37
|
+
puts "- Traits: #{ast.traits.count} - #{ast.traits.map(&:name)}"
|
38
|
+
rescue StandardError => e
|
39
|
+
puts "Error: #{e.message}"
|
40
|
+
puts e.backtrace.first(5)
|
41
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Debug specific transform rule
|
2
|
+
|
3
|
+
require_relative 'lib/kumi/text_parser'
|
4
|
+
|
5
|
+
# Test just the trait parsing
|
6
|
+
trait_text = 'trait :adult, input.age >= 18'
|
7
|
+
|
8
|
+
grammar = Kumi::TextParser::Grammar.new
|
9
|
+
transform = Kumi::TextParser::Transform.new
|
10
|
+
|
11
|
+
begin
|
12
|
+
# Parse just the trait declaration
|
13
|
+
parse_result = grammar.trait_declaration.parse(trait_text)
|
14
|
+
puts 'Trait parse result:'
|
15
|
+
puts parse_result.inspect
|
16
|
+
puts
|
17
|
+
|
18
|
+
# Try to transform it
|
19
|
+
transformed = transform.apply(parse_result)
|
20
|
+
puts 'Transformed result:'
|
21
|
+
puts transformed.inspect
|
22
|
+
puts "Class: #{transformed.class}"
|
23
|
+
rescue StandardError => e
|
24
|
+
puts "Error: #{e.message}"
|
25
|
+
puts e.backtrace.first(5)
|
26
|
+
end
|
@@ -0,0 +1,333 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../lib/kumi'
|
4
|
+
|
5
|
+
# Comprehensive test of all text parser supported features
|
6
|
+
# This example tests what the text parser ACTUALLY supports
|
7
|
+
# (not what the Ruby DSL supports)
|
8
|
+
|
9
|
+
module TextParserComprehensiveTest
|
10
|
+
extend Kumi::Schema
|
11
|
+
|
12
|
+
schema do
|
13
|
+
input do
|
14
|
+
# Basic type declarations
|
15
|
+
integer :age, domain: 18..65
|
16
|
+
float :score, domain: 0.0..100.0
|
17
|
+
string :status, domain: %w[active inactive suspended]
|
18
|
+
boolean :verified
|
19
|
+
any :metadata
|
20
|
+
|
21
|
+
# Nested array declarations
|
22
|
+
array :items do
|
23
|
+
string :name
|
24
|
+
float :price
|
25
|
+
integer :quantity
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# ============================================================
|
30
|
+
# ARITHMETIC OPERATIONS - All supported operators
|
31
|
+
# ============================================================
|
32
|
+
|
33
|
+
# Addition and subtraction
|
34
|
+
value :total_price, input.items.price + input.items.quantity
|
35
|
+
value :price_diff, input.items.price - 10.0
|
36
|
+
|
37
|
+
# Multiplication and division
|
38
|
+
value :scaled_score, input.score * 1.5
|
39
|
+
value :average_score, input.score / 2
|
40
|
+
|
41
|
+
# Modulo
|
42
|
+
value :remainder, input.age % 10
|
43
|
+
|
44
|
+
# Complex arithmetic with parentheses
|
45
|
+
value :complex_calc, (input.score * 2.5) + (input.age / 2) - 10
|
46
|
+
|
47
|
+
# ============================================================
|
48
|
+
# COMPARISON OPERATIONS - All comparison operators
|
49
|
+
# ============================================================
|
50
|
+
|
51
|
+
trait :adult, input.age >= 18
|
52
|
+
trait :senior, input.age > 60
|
53
|
+
trait :young_adult, input.age <= 25
|
54
|
+
trait :is_teen, input.age < 20
|
55
|
+
trait :perfect_score, input.score == 100.0
|
56
|
+
trait :not_perfect, input.score != 100.0
|
57
|
+
|
58
|
+
# String comparisons
|
59
|
+
trait :is_active, input.status == 'active'
|
60
|
+
trait :not_suspended, input.status != 'suspended'
|
61
|
+
|
62
|
+
# Boolean comparisons
|
63
|
+
trait :is_verified, input.verified == true
|
64
|
+
trait :not_verified, input.verified == false
|
65
|
+
|
66
|
+
# ============================================================
|
67
|
+
# LOGICAL OPERATIONS - AND and OR
|
68
|
+
# ============================================================
|
69
|
+
|
70
|
+
# AND operations using &
|
71
|
+
trait :eligible, (input.age >= 18) & (input.verified == true) & (input.status == 'active')
|
72
|
+
trait :premium_user, is_verified & is_active & (input.score > 80.0)
|
73
|
+
|
74
|
+
# OR operations using |
|
75
|
+
trait :needs_attention, (input.status == 'suspended') | (input.verified == false)
|
76
|
+
trait :special_case, senior | (input.score >= 95.0)
|
77
|
+
|
78
|
+
# Complex logical expressions
|
79
|
+
trait :complex_logic, (adult & is_verified) | (senior & is_active)
|
80
|
+
|
81
|
+
# ============================================================
|
82
|
+
# FUNCTION CALLS - fn(:name, args) syntax
|
83
|
+
# ============================================================
|
84
|
+
|
85
|
+
# Math functions
|
86
|
+
value :absolute_diff, fn(:abs, fn(:subtract, input.score, 50.0))
|
87
|
+
value :rounded_score, fn(:round, input.score)
|
88
|
+
value :clamped_score, fn(:clamp, input.score, 20.0, 80.0)
|
89
|
+
|
90
|
+
# String functions (note: no method syntax like .length)
|
91
|
+
value :name_length, fn(:string_length, input.status)
|
92
|
+
value :uppercase_status, fn(:upcase, input.status)
|
93
|
+
value :lowercase_status, fn(:downcase, input.status)
|
94
|
+
|
95
|
+
# Collection/aggregation functions on arrays
|
96
|
+
value :total_items, fn(:sum, input.items.quantity)
|
97
|
+
value :item_count, fn(:size, input.items)
|
98
|
+
value :max_price, fn(:max, input.items.price)
|
99
|
+
value :min_price, fn(:min, input.items.price)
|
100
|
+
|
101
|
+
# ============================================================
|
102
|
+
# REFERENCES - Both bare identifiers and ref() NOT supported in text parser
|
103
|
+
# ============================================================
|
104
|
+
|
105
|
+
# Using previously defined values/traits (bare identifiers)
|
106
|
+
trait :super_eligible, eligible & premium_user
|
107
|
+
value :bonus_points, fn(:multiply, total_price, 0.1)
|
108
|
+
|
109
|
+
# ============================================================
|
110
|
+
# CASCADE EXPRESSIONS - on/base syntax
|
111
|
+
# ============================================================
|
112
|
+
|
113
|
+
value :user_tier do
|
114
|
+
on premium_user, 'premium'
|
115
|
+
on eligible, 'standard'
|
116
|
+
on is_verified, 'basic'
|
117
|
+
base 'guest'
|
118
|
+
end
|
119
|
+
|
120
|
+
value :discount_rate do
|
121
|
+
on senior, 0.25
|
122
|
+
on premium_user, 0.15
|
123
|
+
on is_active, 0.05
|
124
|
+
base 0.0
|
125
|
+
end
|
126
|
+
|
127
|
+
# Cascades using complex conditions
|
128
|
+
value :risk_level do
|
129
|
+
on complex_logic, 'low'
|
130
|
+
on needs_attention, 'high'
|
131
|
+
on is_active, 'medium'
|
132
|
+
base 'unknown'
|
133
|
+
end
|
134
|
+
|
135
|
+
# ============================================================
|
136
|
+
# NESTED INPUT REFERENCES - Deep field access
|
137
|
+
# ============================================================
|
138
|
+
|
139
|
+
# Direct nested field access (parsed as multi-part input reference)
|
140
|
+
value :first_item_name, input.items.name
|
141
|
+
value :all_prices, input.items.price
|
142
|
+
|
143
|
+
# Operations on nested fields (broadcasting)
|
144
|
+
value :discounted_prices, input.items.price * 0.9
|
145
|
+
|
146
|
+
# ============================================================
|
147
|
+
# LITERALS - All supported literal types
|
148
|
+
# ============================================================
|
149
|
+
|
150
|
+
# Number literals (integers and floats)
|
151
|
+
value :constant_int, 42
|
152
|
+
value :constant_float, 3.14159
|
153
|
+
|
154
|
+
# String literals (double quotes only)
|
155
|
+
value :greeting, 'Hello, World!'
|
156
|
+
|
157
|
+
# Boolean literals
|
158
|
+
value :always_true, true
|
159
|
+
value :always_false, false
|
160
|
+
|
161
|
+
# ============================================================
|
162
|
+
# EDGE CASES AND LIMITATIONS
|
163
|
+
# ============================================================
|
164
|
+
|
165
|
+
# Complex parenthesized expressions
|
166
|
+
value :nested_parens, ((input.age + 10) * 2) / (input.score - 20)
|
167
|
+
|
168
|
+
# Multiple operators in sequence
|
169
|
+
value :operator_chain, input.age + 10 - 5 + 20 - 3
|
170
|
+
|
171
|
+
# Nested function calls
|
172
|
+
value :nested_functions, fn(:round, fn(:multiply, fn(:add, input.score, 10), 1.5))
|
173
|
+
|
174
|
+
# ============================================================
|
175
|
+
# NOT SUPPORTED (would fail in text parser)
|
176
|
+
# ============================================================
|
177
|
+
|
178
|
+
# These are commented out as they would cause parse errors:
|
179
|
+
|
180
|
+
# Array literals in expressions
|
181
|
+
# value :array_literal, [1, 2, 3, 4, 5]
|
182
|
+
# value :max_of_array, fn(:max, [input.age, 30, 50])
|
183
|
+
|
184
|
+
# Method-style function calls
|
185
|
+
# value :method_style, fn.add(input.age, 10)
|
186
|
+
|
187
|
+
# Sugar method calls on fields
|
188
|
+
# trait :long_status, input.status.length > 5
|
189
|
+
# value :name_chars, input.status.size
|
190
|
+
|
191
|
+
# Ternary/conditional expressions
|
192
|
+
# value :conditional, eligible ? 100 : 0
|
193
|
+
|
194
|
+
# Hash literals
|
195
|
+
# value :options, { min: 0, max: 100 }
|
196
|
+
|
197
|
+
# ref() syntax
|
198
|
+
# trait :ref_example, ref(:eligible) & ref(:premium_user)
|
199
|
+
|
200
|
+
# Symbol literals (except in fn() calls)
|
201
|
+
# value :symbol_value, :active
|
202
|
+
|
203
|
+
# Power operator
|
204
|
+
# value :squared, input.age ** 2
|
205
|
+
|
206
|
+
# Comments within the DSL
|
207
|
+
# value :test, 42 # this would fail
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Test the schema with the text parser
|
212
|
+
if __FILE__ == $0
|
213
|
+
require_relative '../lib/kumi/text_parser'
|
214
|
+
|
215
|
+
# Create a clean schema without comments for text parser testing
|
216
|
+
schema_text = <<~SCHEMA
|
217
|
+
schema do
|
218
|
+
input do
|
219
|
+
integer :age, domain: 18..65
|
220
|
+
float :score, domain: 0.0..100.0
|
221
|
+
string :status, domain: %w[active inactive suspended]
|
222
|
+
boolean :verified
|
223
|
+
any :metadata
|
224
|
+
#{' '}
|
225
|
+
array :items do
|
226
|
+
string :name
|
227
|
+
float :price
|
228
|
+
integer :quantity
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
value :total_price, input.items.price + input.items.quantity
|
233
|
+
value :price_diff, input.items.price - 10.0
|
234
|
+
value :scaled_score, input.score * 1.5
|
235
|
+
value :average_score, input.score / 2
|
236
|
+
value :remainder, input.age % 10
|
237
|
+
value :complex_calc, (input.score * 2.5) + (input.age / 2) - 10
|
238
|
+
#{' '}
|
239
|
+
trait :adult, input.age >= 18
|
240
|
+
trait :senior, input.age > 60
|
241
|
+
trait :young_adult, input.age <= 25
|
242
|
+
trait :is_teen, input.age < 20
|
243
|
+
trait :perfect_score, input.score == 100.0
|
244
|
+
trait :not_perfect, input.score != 100.0
|
245
|
+
trait :is_active, input.status == "active"
|
246
|
+
trait :not_suspended, input.status != "suspended"
|
247
|
+
trait :is_verified, input.verified == true
|
248
|
+
trait :not_verified, input.verified == false
|
249
|
+
#{' '}
|
250
|
+
trait :eligible, (input.age >= 18) & (input.verified == true) & (input.status == "active")
|
251
|
+
trait :premium_user, is_verified & is_active & (input.score > 80.0)
|
252
|
+
trait :needs_attention, (input.status == "suspended") | (input.verified == false)
|
253
|
+
trait :special_case, senior | (input.score >= 95.0)
|
254
|
+
trait :complex_logic, (adult & is_verified) | (senior & is_active)
|
255
|
+
#{' '}
|
256
|
+
value :absolute_diff, fn(:abs, fn(:subtract, input.score, 50.0))
|
257
|
+
value :rounded_score, fn(:round, input.score)
|
258
|
+
value :clamped_score, fn(:clamp, input.score, 20.0, 80.0)
|
259
|
+
value :name_length, fn(:string_length, input.status)
|
260
|
+
value :uppercase_status, fn(:upcase, input.status)
|
261
|
+
value :lowercase_status, fn(:downcase, input.status)
|
262
|
+
value :total_items, fn(:sum, input.items.quantity)
|
263
|
+
value :item_count, fn(:size, input.items)
|
264
|
+
value :max_price, fn(:max, input.items.price)
|
265
|
+
value :min_price, fn(:min, input.items.price)
|
266
|
+
#{' '}
|
267
|
+
trait :super_eligible, eligible & premium_user
|
268
|
+
value :bonus_points, fn(:multiply, total_price, 0.1)
|
269
|
+
#{' '}
|
270
|
+
value :user_tier do
|
271
|
+
on premium_user, "premium"
|
272
|
+
on eligible, "standard"
|
273
|
+
on is_verified, "basic"
|
274
|
+
base "guest"
|
275
|
+
end
|
276
|
+
#{' '}
|
277
|
+
value :discount_rate do
|
278
|
+
on senior, 0.25
|
279
|
+
on premium_user, 0.15
|
280
|
+
on is_active, 0.05
|
281
|
+
base 0.0
|
282
|
+
end
|
283
|
+
#{' '}
|
284
|
+
value :risk_level do
|
285
|
+
on complex_logic, "low"
|
286
|
+
on needs_attention, "high"
|
287
|
+
on is_active, "medium"
|
288
|
+
base "unknown"
|
289
|
+
end
|
290
|
+
#{' '}
|
291
|
+
value :first_item_name, input.items.name
|
292
|
+
value :all_prices, input.items.price
|
293
|
+
value :discounted_prices, input.items.price * 0.9
|
294
|
+
#{' '}
|
295
|
+
value :constant_int, 42
|
296
|
+
value :constant_float, 3.14159
|
297
|
+
value :greeting, "Hello, World!"
|
298
|
+
value :always_true, true
|
299
|
+
value :always_false, false
|
300
|
+
#{' '}
|
301
|
+
value :nested_parens, ((input.age + 10) * 2) / (input.score - 20)
|
302
|
+
value :operator_chain, input.age + 10 - 5 + 20 - 3
|
303
|
+
value :nested_functions, fn(:round, fn(:multiply, fn(:add, input.score, 10), 1.5))
|
304
|
+
end
|
305
|
+
SCHEMA
|
306
|
+
|
307
|
+
puts 'Testing text parser with comprehensive DSL...'
|
308
|
+
puts '=' * 60
|
309
|
+
|
310
|
+
begin
|
311
|
+
# Validate the schema
|
312
|
+
diagnostics = Kumi::TextParser.validate(schema_text)
|
313
|
+
|
314
|
+
if diagnostics.empty?
|
315
|
+
puts '✅ Schema is valid!'
|
316
|
+
|
317
|
+
# Parse and show some info
|
318
|
+
ast = Kumi::TextParser.parse(schema_text)
|
319
|
+
puts "\nParsed successfully!"
|
320
|
+
puts "- Input fields: #{ast.inputs.map(&:name).join(', ')}"
|
321
|
+
puts "- Values: #{ast.attributes.count}"
|
322
|
+
puts "- Traits: #{ast.traits.count}"
|
323
|
+
else
|
324
|
+
puts '❌ Schema has errors:'
|
325
|
+
diagnostics.to_a.each do |diagnostic|
|
326
|
+
puts " Line #{diagnostic.line}, Column #{diagnostic.column}: #{diagnostic.message}"
|
327
|
+
end
|
328
|
+
end
|
329
|
+
rescue StandardError => e
|
330
|
+
puts "❌ Parser error: #{e.message}"
|
331
|
+
puts e.backtrace.first(5)
|
332
|
+
end
|
333
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../lib/kumi/text_parser'
|
4
|
+
|
5
|
+
# Test schema with comments and verify AST structure
|
6
|
+
schema_text = <<~SCHEMA
|
7
|
+
schema do
|
8
|
+
# Input section with type declarations
|
9
|
+
input do
|
10
|
+
integer :age, domain: 18..65 # User's age
|
11
|
+
float :score, domain: 0.0..100.0 # Test score
|
12
|
+
string :status, domain: %w[active inactive] # User status
|
13
|
+
boolean :verified # Verification status
|
14
|
+
#{' '}
|
15
|
+
# Nested array example
|
16
|
+
array :items do
|
17
|
+
string :name # Item name
|
18
|
+
float :price # Item price
|
19
|
+
integer :quantity # Item quantity
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Basic arithmetic operations
|
24
|
+
value :total_price, input.items.price + input.items.quantity
|
25
|
+
value :scaled_score, input.score * 1.5
|
26
|
+
#{' '}
|
27
|
+
# Trait definitions with comparisons
|
28
|
+
trait :adult, input.age >= 18 # Is adult
|
29
|
+
trait :high_scorer, input.score > 80.0 # High score
|
30
|
+
trait :is_active, input.status == "active" # Active user
|
31
|
+
#{' '}
|
32
|
+
# Complex logical operations
|
33
|
+
trait :eligible, adult & is_active & (input.verified == true)
|
34
|
+
#{' '}
|
35
|
+
# Function calls
|
36
|
+
value :rounded_score, fn(:round, input.score)
|
37
|
+
value :item_count, fn(:size, input.items)
|
38
|
+
#{' '}
|
39
|
+
# Cascade expressions
|
40
|
+
value :user_level do
|
41
|
+
on high_scorer, "premium" # Premium users
|
42
|
+
on eligible, "standard" # Standard users#{' '}
|
43
|
+
base "basic" # Default level
|
44
|
+
end
|
45
|
+
end
|
46
|
+
SCHEMA
|
47
|
+
|
48
|
+
puts 'Testing text parser with comments...'
|
49
|
+
puts '=' * 50
|
50
|
+
|
51
|
+
begin
|
52
|
+
# Test parsing
|
53
|
+
diagnostics = Kumi::TextParser.validate(schema_text)
|
54
|
+
|
55
|
+
if diagnostics.empty?
|
56
|
+
puts '✅ Schema parsed successfully!'
|
57
|
+
|
58
|
+
# Parse and examine AST structure
|
59
|
+
ast = Kumi::TextParser.parse(schema_text)
|
60
|
+
|
61
|
+
puts "\n📊 AST Structure:"
|
62
|
+
puts "- Root type: #{ast.class.name}"
|
63
|
+
puts "- Input fields: #{ast.inputs.count}"
|
64
|
+
ast.inputs.each_with_index do |input, i|
|
65
|
+
puts " #{i + 1}. #{input.name} (#{input.type})"
|
66
|
+
next unless input.children && input.children.any?
|
67
|
+
|
68
|
+
input.children.each do |child|
|
69
|
+
puts " - #{child.name} (#{child.type})"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
puts "- Value declarations: #{ast.attributes.count}"
|
74
|
+
ast.attributes.each_with_index do |value, i|
|
75
|
+
puts " #{i + 1}. #{value.name}"
|
76
|
+
end
|
77
|
+
|
78
|
+
puts "- Trait declarations: #{ast.traits.count}"
|
79
|
+
ast.traits.each_with_index do |trait, i|
|
80
|
+
puts " #{i + 1}. #{trait.name}"
|
81
|
+
end
|
82
|
+
|
83
|
+
# Test with actual Ruby DSL to verify it works end-to-end
|
84
|
+
puts "\n🧪 Testing with full Kumi analysis..."
|
85
|
+
begin
|
86
|
+
analyzer = Kumi::Analyzer.new
|
87
|
+
analysis_result = analyzer.analyze(ast)
|
88
|
+
|
89
|
+
if analysis_result.errors.any?
|
90
|
+
puts '❌ Analysis errors:'
|
91
|
+
analysis_result.errors.each do |error|
|
92
|
+
if error.respond_to?(:message)
|
93
|
+
puts " - #{error.message}"
|
94
|
+
elsif error.is_a?(Array)
|
95
|
+
puts " - #{error[1]}"
|
96
|
+
else
|
97
|
+
puts " - #{error}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
else
|
101
|
+
puts '✅ Schema analysis successful!'
|
102
|
+
|
103
|
+
# Try to create a compiled schema
|
104
|
+
compiler = Kumi::Compiler.new
|
105
|
+
compiled = compiler.compile(ast, analysis_result)
|
106
|
+
puts '✅ Schema compilation successful!'
|
107
|
+
|
108
|
+
# Test execution with sample data
|
109
|
+
test_data = {
|
110
|
+
age: 25,
|
111
|
+
score: 85.5,
|
112
|
+
status: 'active',
|
113
|
+
verified: true,
|
114
|
+
items: [
|
115
|
+
{ name: 'Item 1', price: 10.0, quantity: 2 },
|
116
|
+
{ name: 'Item 2', price: 15.0, quantity: 1 }
|
117
|
+
]
|
118
|
+
}
|
119
|
+
|
120
|
+
runner = Kumi::Runner.new(compiled, test_data)
|
121
|
+
|
122
|
+
# Test a few key calculations
|
123
|
+
puts "\n🎯 Test Results:"
|
124
|
+
puts "- total_price: #{runner.fetch(:total_price)}"
|
125
|
+
puts "- scaled_score: #{runner.fetch(:scaled_score)}"
|
126
|
+
puts "- adult: #{runner.fetch(:adult)}"
|
127
|
+
puts "- eligible: #{runner.fetch(:eligible)}"
|
128
|
+
puts "- user_level: #{runner.fetch(:user_level)}"
|
129
|
+
puts "- item_count: #{runner.fetch(:item_count)}"
|
130
|
+
|
131
|
+
end
|
132
|
+
rescue StandardError => e
|
133
|
+
puts "❌ Analysis/compilation error: #{e.message}"
|
134
|
+
puts e.backtrace.first(3)
|
135
|
+
end
|
136
|
+
|
137
|
+
else
|
138
|
+
puts '❌ Schema has parse errors:'
|
139
|
+
diagnostics.to_a.each do |diagnostic|
|
140
|
+
puts " Line #{diagnostic.line}, Column #{diagnostic.column}: #{diagnostic.message}"
|
141
|
+
end
|
142
|
+
end
|
143
|
+
rescue StandardError => e
|
144
|
+
puts "❌ Unexpected error: #{e.message}"
|
145
|
+
puts e.backtrace.first(5)
|
146
|
+
end
|