kdl 1.0.0.rc1 → 1.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1da5c7ecdbb21b75f621fbb6876fd46270c525311d9b34443ebbda8e715f6b0d
4
- data.tar.gz: c8317287f0573c91b256d28ff902e3a38bd3275b009016ce0d41c631e7321303
3
+ metadata.gz: 0ad96fca4491347ec286e3a6ccdaa2c48027ac6e51e4541ea4583d858885192c
4
+ data.tar.gz: '0863341cc116e0d95b663be4cf7becd89173773d20db4e80a0575f224f9590f1'
5
5
  SHA512:
6
- metadata.gz: 549b4b676a95b83146647aca4dbbccf44463fbaa5cc0192dc7e0e1e8977e194bdc505c29edde3c71f5f225a22eb23ea2ba484d4a26c24cdbae152015dde99b77
7
- data.tar.gz: 13520a974cb5e28658bebd7b09fa79bb13e40cf2c12f50e0a51408fef74ab756e314f7e6758a357b0ef82afdda3cfa7604197184dad45607296ffb7469507df3
6
+ metadata.gz: 1371e648e0b27a19664fd58c4d6b8cfa136727b20235fd845735d1b6ac4eb1eea8b978634fc58c86e15af8d2ee2641e0afbf7b798d678fb19dd607123d734d7c
7
+ data.tar.gz: 54d709fda2686dec0dd3d3fa9b11fd3e90c23651d12fb94136efd37b5f67b2fe533a0c540fdbea0597708ee7d14fd609b1d2ad9d6cc4e1fb297438ad9f641c22
@@ -17,7 +17,7 @@ jobs:
17
17
  test:
18
18
  strategy:
19
19
  matrix:
20
- ruby: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0, head]
20
+ ruby: [2.4, 2.5, 2.6, 2.7, 3.0, head]
21
21
 
22
22
  runs-on: ubuntu-latest
23
23
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # KDL
2
2
 
3
- [![Actions Status](https://github.com/jellymann/kdl-rb/workflows/Ruby/badge.svg)](https://github.com/jellymann/kdl-rb/actions)
3
+ [![Actions Status](https://github.com/danini-the-panini/kdl-rb/workflows/Ruby/badge.svg)](https://github.com/danini-the-panini/kdl-rb/actions)
4
4
 
5
5
  This is a Ruby implementation of the [KDL Document Language](https://kdl.dev)
6
6
 
@@ -28,6 +28,20 @@ require 'kdl'
28
28
  KDL.parse_document(a_string) #=> KDL::Document
29
29
  ```
30
30
 
31
+ You can optionally provide your own type annotation handlers:
32
+
33
+ ```ruby
34
+ KDL.parse_document(a_string, type_parsers: {
35
+ 'foo' => -> (value, type) {
36
+ Foo.new(value.value, type: type)
37
+ }
38
+ })
39
+ ```
40
+
41
+ The `foo` proc will be called with instances of Value or Node with the type annotation `(foo)`.
42
+
43
+ Parsers are expected to have a `call` method that takes the Value or Node, and the type annotation itself, as arguments, and is expected to return either an instance of Value or Node (depending on the input type) or `nil` to return the original value as is. Take a look at [the built in parsers](lib/kdl/types) as a reference.
44
+
31
45
  ## Development
32
46
 
33
47
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -36,7 +50,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
36
50
 
37
51
  ## Contributing
38
52
 
39
- Bug reports and pull requests are welcome on GitHub at https://github.com/jellymann/kdl-rb.
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/danini-the-panini/kdl-rb.
40
54
 
41
55
 
42
56
  ## License
data/kdl.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.description = %q{Ruby implementation of the KDL Document Language Spec}
11
11
  spec.homepage = "https://kdl.dev"
12
12
  spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
16
16
  spec.metadata["source_code_uri"] = "https://github.com/danini-the-panini/kdl-rb"
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.require_paths = ["lib"]
26
26
 
27
27
  spec.add_development_dependency 'racc', '~> 1.5'
28
+ spec.add_dependency 'simpleidn', '~> 0.2.1'
28
29
  end
data/lib/kdl/kdl.tab.rb CHANGED
@@ -8,8 +8,14 @@ require 'racc/parser.rb'
8
8
  module KDL
9
9
  class Parser < Racc::Parser
10
10
 
11
- module_eval(<<'...end kdl.yy/module_eval...', 'kdl.yy', 68)
12
- def parse(str)
11
+ module_eval(<<'...end kdl.yy/module_eval...', 'kdl.yy', 69)
12
+
13
+ def parse(str, options = {})
14
+ if options.fetch(:parse_types, true)
15
+ @type_parsers = ::KDL::Types::MAPPING.merge(options.fetch(:type_parsers, {}))
16
+ else
17
+ @type_parsers = {}
18
+ end
13
19
  @tokenizer = ::KDL::Tokenizer.new(str)
14
20
  do_parse
15
21
  end
@@ -23,82 +29,82 @@ module_eval(<<'...end kdl.yy/module_eval...', 'kdl.yy', 68)
23
29
  ##### State transition tables begin ###
24
30
 
25
31
  racc_action_table = [
26
- -2, 10, 18, 19, 20, 18, 19, 20, 6, 7,
27
- 6, 7, 44, 24, 17, 37, 8, 17, 8, 16,
28
- 57, 30, 16, 18, 46, 47, 51, 52, 55, 56,
29
- 54, 44, 37, -42, 61, 17, 18, 19, 20, 37,
30
- 42, -28, -29, 18, 19, 20, 63, 73, 17, 18,
31
- 19, 20, nil, 16, 74, 17, 18, 19, 20, nil,
32
- 16, 17, 18, 46, 47, 51, 52, 55, 56, 54,
33
- nil, nil, 57, nil, 17, 65, 66, 51, 52, 55,
34
- 56, 54, 28, 7, nil, nil, 17, 6, 7, 33,
35
- 8, 30, 6, 7, 33, 8, 6, 7, nil, 33,
32
+ -2, 10, 19, 20, 21, 19, 20, 21, 6, 7,
33
+ 6, 7, 45, 25, 18, 38, 8, 18, 8, 17,
34
+ 58, 32, 17, 19, 47, 48, 52, 53, 56, 57,
35
+ 55, 45, 38, -43, 62, 18, 19, 20, 21, 38,
36
+ 43, -29, -30, 19, 20, 21, 64, 74, 18, 19,
37
+ 20, 21, nil, 17, 75, 18, 19, 20, 21, nil,
38
+ 17, 18, 19, 47, 48, 52, 53, 56, 57, 55,
39
+ nil, nil, 58, nil, 18, 66, 67, 52, 53, 56,
40
+ 57, 55, 30, 7, nil, nil, 18, 6, 7, 35,
41
+ 8, 32, 6, 7, 35, 8, 6, 7, nil, 35,
36
42
  8, 6, 7, nil, 8, 6, 7, 6, 7, 8,
37
- 6, 7, nil, 8, nil, 8, nil, nil, 8, 65,
38
- 66, 51, 52, 55, 56, 54, 18, 19, 20 ]
43
+ 6, 7, nil, 8, nil, 8, nil, nil, 8, 66,
44
+ 67, 52, 53, 56, 57, 55, 19, 20, 21 ]
39
45
 
40
46
  racc_action_check = [
41
47
  3, 1, 2, 2, 2, 5, 5, 5, 0, 0,
42
- 3, 3, 44, 10, 2, 16, 0, 5, 3, 2,
43
- 29, 44, 5, 28, 28, 28, 28, 28, 28, 28,
44
- 28, 28, 30, 28, 38, 28, 67, 67, 67, 42,
45
- 28, 46, 47, 68, 68, 68, 48, 67, 67, 36,
46
- 36, 36, nil, 67, 68, 68, 15, 15, 15, nil,
47
- 68, 36, 62, 62, 62, 62, 62, 62, 62, 62,
48
- nil, nil, 62, nil, 62, 63, 63, 63, 63, 63,
49
- 63, 63, 13, 13, nil, nil, 63, 26, 26, 13,
50
- 13, 13, 27, 27, 26, 26, 31, 31, nil, 27,
51
- 27, 33, 33, nil, 31, 57, 57, 59, 59, 33,
52
- 69, 69, nil, 57, nil, 59, nil, nil, 69, 50,
53
- 50, 50, 50, 50, 50, 50, 17, 17, 17 ]
48
+ 3, 3, 45, 10, 2, 17, 0, 5, 3, 2,
49
+ 31, 45, 5, 30, 30, 30, 30, 30, 30, 30,
50
+ 30, 30, 32, 30, 39, 30, 68, 68, 68, 43,
51
+ 30, 47, 48, 69, 69, 69, 49, 68, 68, 37,
52
+ 37, 37, nil, 68, 69, 69, 14, 14, 14, nil,
53
+ 69, 37, 63, 63, 63, 63, 63, 63, 63, 63,
54
+ nil, nil, 63, nil, 63, 64, 64, 64, 64, 64,
55
+ 64, 64, 15, 15, nil, nil, 64, 28, 28, 15,
56
+ 15, 15, 29, 29, 28, 28, 33, 33, nil, 29,
57
+ 29, 35, 35, nil, 33, 58, 58, 60, 60, 35,
58
+ 70, 70, nil, 58, nil, 60, nil, nil, 70, 51,
59
+ 51, 51, 51, 51, 51, 51, 18, 18, 18 ]
54
60
 
55
61
  racc_action_pointer = [
56
62
  -2, 1, 0, 0, nil, 3, nil, nil, nil, nil,
57
- 13, nil, nil, 72, nil, 54, 5, 124, nil, nil,
58
- nil, nil, nil, nil, nil, nil, 77, 82, 21, 8,
59
- 22, 86, nil, 91, nil, nil, 47, nil, 19, nil,
60
- nil, nil, 29, nil, 2, nil, 25, 26, 30, nil,
61
- 116, nil, nil, nil, nil, nil, nil, 95, nil, 97,
62
- nil, nil, 60, 72, nil, nil, nil, 34, 41, 100,
63
- nil, nil, nil, nil, nil ]
63
+ 13, nil, nil, nil, 54, 72, nil, 5, 124, nil,
64
+ nil, nil, nil, nil, nil, nil, nil, nil, 77, 82,
65
+ 21, 8, 22, 86, nil, 91, nil, 47, nil, 19,
66
+ nil, nil, nil, 29, nil, 2, nil, 25, 26, 30,
67
+ nil, 116, nil, nil, nil, nil, nil, nil, 95, nil,
68
+ 97, nil, nil, 60, 72, nil, nil, nil, 34, 41,
69
+ 100, nil, nil, nil, nil, nil ]
64
70
 
65
71
  racc_action_default = [
66
- -50, -51, -1, -49, -3, -51, -43, -44, -45, -46,
67
- -51, -6, -7, -50, -11, -51, -50, -51, -27, -28,
68
- -29, -47, -4, -5, 75, -8, -51, -51, -43, -51,
69
- -50, -22, -23, -24, -41, -12, -51, -42, -51, -9,
70
- -10, -13, -50, -15, -51, -21, -33, -34, -51, -31,
71
- -51, -35, -36, -37, -38, -39, -40, -50, -20, -25,
72
- -17, -26, -51, -51, -32, -33, -34, -51, -51, -49,
73
- -14, -16, -30, -19, -18 ]
72
+ -51, -52, -1, -50, -3, -52, -44, -45, -46, -47,
73
+ -52, -6, -7, -8, -52, -51, -13, -51, -52, -28,
74
+ -29, -30, -48, -4, -5, 76, -9, -10, -52, -52,
75
+ -44, -52, -51, -23, -24, -25, -42, -52, -43, -52,
76
+ -11, -12, -14, -51, -16, -52, -22, -34, -35, -52,
77
+ -32, -52, -36, -37, -38, -39, -40, -41, -51, -21,
78
+ -26, -18, -27, -52, -52, -33, -34, -35, -52, -52,
79
+ -50, -15, -17, -31, -20, -19 ]
74
80
 
75
81
  racc_goto_table = [
76
- 21, 22, 23, 2, 3, 4, 50, 35, 5, 38,
77
- 41, 25, 43, 36, 1, 26, 27, 64, nil, nil,
78
- 48, nil, nil, nil, 39, 40, nil, nil, 21, nil,
79
- nil, nil, 60, nil, nil, nil, nil, 59, nil, 62,
80
- 50, 50, nil, nil, 70, 72, 71, nil, nil, nil,
81
- nil, nil, nil, nil, 48, nil, 21, nil, nil, nil,
82
- 68, 69, 4, 22, 23, 67, 21 ]
82
+ 22, 23, 24, 2, 3, 4, 39, 51, 5, 42,
83
+ 37, 44, 27, 1, 26, 28, 29, 65, 49, nil,
84
+ nil, nil, nil, nil, nil, 40, 41, nil, nil, nil,
85
+ 22, nil, nil, 61, nil, nil, 63, nil, nil, 60,
86
+ 51, 51, 71, 73, 72, nil, nil, nil, nil, nil,
87
+ nil, 49, nil, nil, nil, nil, nil, 22, nil, nil,
88
+ nil, 69, 70, 4, 23, 24, 68, 22 ]
83
89
 
84
90
  racc_goto_check = [
85
- 20, 6, 7, 2, 3, 4, 13, 12, 5, 12,
86
- 14, 9, 16, 15, 1, 10, 11, 18, nil, nil,
87
- 12, nil, nil, nil, 9, 9, nil, nil, 20, nil,
88
- nil, nil, 6, nil, nil, nil, nil, 3, nil, 15,
89
- 13, 13, nil, nil, 14, 14, 16, nil, nil, nil,
90
- nil, nil, nil, nil, 12, nil, 20, nil, nil, nil,
91
- 2, 3, 4, 6, 7, 5, 20 ]
91
+ 21, 6, 7, 2, 3, 4, 14, 9, 5, 15,
92
+ 16, 17, 11, 1, 8, 12, 13, 19, 14, nil,
93
+ nil, nil, nil, nil, nil, 11, 11, nil, nil, nil,
94
+ 21, nil, nil, 6, nil, nil, 16, nil, nil, 3,
95
+ 9, 9, 15, 15, 17, nil, nil, nil, nil, nil,
96
+ nil, 14, nil, nil, nil, nil, nil, 21, nil, nil,
97
+ nil, 2, 3, 4, 6, 7, 5, 21 ]
92
98
 
93
99
  racc_goto_pointer = [
94
- nil, 14, 3, 4, 5, 8, -4, -3, nil, -2,
95
- 2, 3, -8, -22, -18, -3, -16, nil, -33, nil,
96
- -3 ]
100
+ nil, 13, 3, 4, 5, 8, -4, -3, 0, -23,
101
+ nil, -3, 0, 1, -12, -21, -7, -19, nil, -34,
102
+ nil, -3 ]
97
103
 
98
104
  racc_goto_default = [
99
- nil, nil, nil, 31, 34, nil, 11, 12, 13, nil,
100
- 58, 45, 14, 15, nil, 29, nil, 32, 49, 53,
101
- 9 ]
105
+ nil, nil, nil, 33, 36, nil, 11, 12, 13, 14,
106
+ 15, nil, 59, 46, 16, nil, 31, nil, 34, 50,
107
+ 54, 9 ]
102
108
 
103
109
  racc_reduce_table = [
104
110
  0, 0, :racc_error,
@@ -109,53 +115,54 @@ racc_reduce_table = [
109
115
  2, 22, :_reduce_5,
110
116
  2, 22, :_reduce_6,
111
117
  2, 22, :_reduce_7,
112
- 2, 26, :_reduce_8,
113
- 3, 26, :_reduce_9,
114
- 3, 26, :_reduce_10,
115
- 1, 28, :_reduce_11,
116
- 2, 28, :_reduce_12,
117
- 3, 28, :_reduce_13,
118
- 5, 28, :_reduce_14,
119
- 3, 28, :_reduce_15,
120
- 5, 28, :_reduce_16,
118
+ 1, 26, :_reduce_8,
119
+ 2, 26, :_reduce_9,
120
+ 2, 28, :_reduce_10,
121
+ 3, 28, :_reduce_11,
122
+ 3, 28, :_reduce_12,
123
+ 1, 30, :_reduce_13,
124
+ 3, 30, :_reduce_14,
125
+ 5, 30, :_reduce_15,
126
+ 3, 30, :_reduce_16,
127
+ 5, 30, :_reduce_17,
121
128
  3, 27, :_reduce_none,
122
- 4, 30, :_reduce_18,
123
- 4, 30, :_reduce_19,
124
- 2, 31, :_reduce_none,
125
- 2, 31, :_reduce_none,
126
- 1, 29, :_reduce_none,
127
- 1, 29, :_reduce_none,
128
- 1, 37, :_reduce_none,
129
- 2, 37, :_reduce_none,
130
- 3, 33, :_reduce_26,
131
- 1, 32, :_reduce_27,
132
- 1, 32, :_reduce_28,
133
- 1, 32, :_reduce_29,
134
- 3, 36, :_reduce_30,
135
- 1, 34, :_reduce_none,
136
- 2, 34, :_reduce_32,
137
- 1, 38, :_reduce_33,
138
- 1, 38, :_reduce_34,
139
- 1, 38, :_reduce_35,
140
- 1, 38, :_reduce_36,
141
- 1, 38, :_reduce_37,
142
- 1, 38, :_reduce_38,
143
- 1, 39, :_reduce_39,
144
- 1, 39, :_reduce_40,
145
- 1, 35, :_reduce_none,
129
+ 4, 32, :_reduce_19,
130
+ 4, 32, :_reduce_20,
131
+ 2, 33, :_reduce_none,
132
+ 2, 33, :_reduce_none,
133
+ 1, 31, :_reduce_none,
134
+ 1, 31, :_reduce_none,
135
+ 1, 38, :_reduce_none,
136
+ 2, 38, :_reduce_none,
137
+ 3, 29, :_reduce_27,
138
+ 1, 34, :_reduce_28,
139
+ 1, 34, :_reduce_29,
140
+ 1, 34, :_reduce_30,
141
+ 3, 37, :_reduce_31,
146
142
  1, 35, :_reduce_none,
147
- 1, 40, :_reduce_none,
148
- 1, 40, :_reduce_none,
149
- 1, 40, :_reduce_none,
143
+ 2, 35, :_reduce_33,
144
+ 1, 39, :_reduce_34,
145
+ 1, 39, :_reduce_35,
146
+ 1, 39, :_reduce_36,
147
+ 1, 39, :_reduce_37,
148
+ 1, 39, :_reduce_38,
149
+ 1, 39, :_reduce_39,
150
+ 1, 40, :_reduce_40,
151
+ 1, 40, :_reduce_41,
152
+ 1, 36, :_reduce_none,
153
+ 1, 36, :_reduce_none,
154
+ 1, 41, :_reduce_none,
155
+ 1, 41, :_reduce_none,
156
+ 1, 41, :_reduce_none,
150
157
  1, 23, :_reduce_none,
151
158
  2, 23, :_reduce_none,
152
159
  1, 25, :_reduce_none,
153
160
  1, 25, :_reduce_none,
154
- 0, 24, :_reduce_50 ]
161
+ 0, 24, :_reduce_51 ]
155
162
 
156
- racc_reduce_n = 51
163
+ racc_reduce_n = 52
157
164
 
158
- racc_shift_n = 75
165
+ racc_shift_n = 76
159
166
 
160
167
  racc_token_table = {
161
168
  false => 0,
@@ -228,12 +235,13 @@ Racc_token_to_s_table = [
228
235
  "linespace_star",
229
236
  "node",
230
237
  "empty_node",
238
+ "untyped_node",
239
+ "type",
231
240
  "node_decl",
232
241
  "node_term",
233
242
  "node_children",
234
243
  "empty_children",
235
244
  "identifier",
236
- "type",
237
245
  "value",
238
246
  "ws_star",
239
247
  "property",
@@ -292,13 +300,13 @@ module_eval(<<'.,.,', 'kdl.yy', 20)
292
300
 
293
301
  module_eval(<<'.,.,', 'kdl.yy', 21)
294
302
  def _reduce_8(val, _values)
295
- val[0].tap { |x| x.children = nil }
303
+ val[0]
296
304
  end
297
305
  .,.,
298
306
 
299
307
  module_eval(<<'.,.,', 'kdl.yy', 22)
300
308
  def _reduce_9(val, _values)
301
- val[0].tap { |x| x.children = val[1] }
309
+ val[1].as_type(val[0], @type_parsers.fetch(val[0], nil))
302
310
  end
303
311
  .,.,
304
312
 
@@ -310,56 +318,60 @@ module_eval(<<'.,.,', 'kdl.yy', 23)
310
318
 
311
319
  module_eval(<<'.,.,', 'kdl.yy', 24)
312
320
  def _reduce_11(val, _values)
313
- KDL::Node.new(val[0])
321
+ val[0].tap { |x| x.children = val[1] }
314
322
  end
315
323
  .,.,
316
324
 
317
325
  module_eval(<<'.,.,', 'kdl.yy', 25)
318
326
  def _reduce_12(val, _values)
319
- KDL::Node.new(val[1], type: val[0])
327
+ val[0].tap { |x| x.children = nil }
320
328
  end
321
329
  .,.,
322
330
 
323
331
  module_eval(<<'.,.,', 'kdl.yy', 26)
324
332
  def _reduce_13(val, _values)
325
- val[0].tap { |x| x.arguments << val[2] }
333
+ KDL::Node.new(val[0])
326
334
  end
327
335
  .,.,
328
336
 
329
337
  module_eval(<<'.,.,', 'kdl.yy', 27)
330
338
  def _reduce_14(val, _values)
331
- val[0]
339
+ val[0].tap { |x| x.arguments << val[2] }
332
340
  end
333
341
  .,.,
334
342
 
335
343
  module_eval(<<'.,.,', 'kdl.yy', 28)
336
344
  def _reduce_15(val, _values)
337
- val[0].tap { |x| x.properties[val[2][0]] = val[2][1] }
345
+ val[0]
338
346
  end
339
347
  .,.,
340
348
 
341
349
  module_eval(<<'.,.,', 'kdl.yy', 29)
342
350
  def _reduce_16(val, _values)
351
+ val[0].tap { |x| x.properties[val[2][0]] = val[2][1] }
352
+ end
353
+ .,.,
354
+
355
+ module_eval(<<'.,.,', 'kdl.yy', 30)
356
+ def _reduce_17(val, _values)
343
357
  val[0]
344
358
  end
345
359
  .,.,
346
360
 
347
- # reduce 17 omitted
361
+ # reduce 18 omitted
348
362
 
349
- module_eval(<<'.,.,', 'kdl.yy', 31)
350
- def _reduce_18(val, _values)
363
+ module_eval(<<'.,.,', 'kdl.yy', 32)
364
+ def _reduce_19(val, _values)
351
365
  val[2]
352
366
  end
353
367
  .,.,
354
368
 
355
- module_eval(<<'.,.,', 'kdl.yy', 32)
356
- def _reduce_19(val, _values)
369
+ module_eval(<<'.,.,', 'kdl.yy', 33)
370
+ def _reduce_20(val, _values)
357
371
  []
358
372
  end
359
373
  .,.,
360
374
 
361
- # reduce 20 omitted
362
-
363
375
  # reduce 21 omitted
364
376
 
365
377
  # reduce 22 omitted
@@ -370,15 +382,11 @@ module_eval(<<'.,.,', 'kdl.yy', 32)
370
382
 
371
383
  # reduce 25 omitted
372
384
 
373
- module_eval(<<'.,.,', 'kdl.yy', 38)
374
- def _reduce_26(val, _values)
375
- val[1]
376
- end
377
- .,.,
385
+ # reduce 26 omitted
378
386
 
379
- module_eval(<<'.,.,', 'kdl.yy', 40)
387
+ module_eval(<<'.,.,', 'kdl.yy', 39)
380
388
  def _reduce_27(val, _values)
381
- val[0].value
389
+ val[1]
382
390
  end
383
391
  .,.,
384
392
 
@@ -394,23 +402,23 @@ module_eval(<<'.,.,', 'kdl.yy', 42)
394
402
  end
395
403
  .,.,
396
404
 
397
- module_eval(<<'.,.,', 'kdl.yy', 44)
405
+ module_eval(<<'.,.,', 'kdl.yy', 43)
398
406
  def _reduce_30(val, _values)
399
- [val[0], val[2]]
407
+ val[0].value
400
408
  end
401
409
  .,.,
402
410
 
403
- # reduce 31 omitted
404
-
405
- module_eval(<<'.,.,', 'kdl.yy', 47)
406
- def _reduce_32(val, _values)
407
- val[1].as_type(val[0])
411
+ module_eval(<<'.,.,', 'kdl.yy', 45)
412
+ def _reduce_31(val, _values)
413
+ [val[0], val[2]]
408
414
  end
409
415
  .,.,
410
416
 
411
- module_eval(<<'.,.,', 'kdl.yy', 49)
417
+ # reduce 32 omitted
418
+
419
+ module_eval(<<'.,.,', 'kdl.yy', 48)
412
420
  def _reduce_33(val, _values)
413
- KDL::Value::String.new(val[0].value)
421
+ val[1].as_type(val[0], @type_parsers.fetch(val[0], nil))
414
422
  end
415
423
  .,.,
416
424
 
@@ -422,41 +430,45 @@ module_eval(<<'.,.,', 'kdl.yy', 50)
422
430
 
423
431
  module_eval(<<'.,.,', 'kdl.yy', 51)
424
432
  def _reduce_35(val, _values)
425
- KDL::Value::Int.new(val[0].value)
433
+ KDL::Value::String.new(val[0].value)
426
434
  end
427
435
  .,.,
428
436
 
429
437
  module_eval(<<'.,.,', 'kdl.yy', 52)
430
438
  def _reduce_36(val, _values)
431
- KDL::Value::Float.new(val[0].value, format: val[0].meta[:format])
439
+ KDL::Value::Int.new(val[0].value)
432
440
  end
433
441
  .,.,
434
442
 
435
443
  module_eval(<<'.,.,', 'kdl.yy', 53)
436
444
  def _reduce_37(val, _values)
437
- KDL::Value::Boolean.new(val[0])
445
+ KDL::Value::Float.new(val[0].value, format: val[0].meta[:format])
438
446
  end
439
447
  .,.,
440
448
 
441
449
  module_eval(<<'.,.,', 'kdl.yy', 54)
442
450
  def _reduce_38(val, _values)
443
- KDL::Value::Null
451
+ KDL::Value::Boolean.new(val[0])
444
452
  end
445
453
  .,.,
446
454
 
447
- module_eval(<<'.,.,', 'kdl.yy', 56)
455
+ module_eval(<<'.,.,', 'kdl.yy', 55)
448
456
  def _reduce_39(val, _values)
449
- true
457
+ KDL::Value::Null
450
458
  end
451
459
  .,.,
452
460
 
453
461
  module_eval(<<'.,.,', 'kdl.yy', 57)
454
462
  def _reduce_40(val, _values)
455
- false
463
+ true
456
464
  end
457
465
  .,.,
458
466
 
459
- # reduce 41 omitted
467
+ module_eval(<<'.,.,', 'kdl.yy', 58)
468
+ def _reduce_41(val, _values)
469
+ false
470
+ end
471
+ .,.,
460
472
 
461
473
  # reduce 42 omitted
462
474
 
@@ -474,8 +486,10 @@ module_eval(<<'.,.,', 'kdl.yy', 57)
474
486
 
475
487
  # reduce 49 omitted
476
488
 
477
- module_eval(<<'.,.,', 'kdl.yy', 64)
478
- def _reduce_50(val, _values)
489
+ # reduce 50 omitted
490
+
491
+ module_eval(<<'.,.,', 'kdl.yy', 65)
492
+ def _reduce_51(val, _values)
479
493
  nil
480
494
  end
481
495
  .,.,
data/lib/kdl/kdl.yy CHANGED
@@ -14,23 +14,24 @@ rule
14
14
  document : nodes { KDL::Document.new(val[0]) }
15
15
  | linespaces { KDL::Document.new([])}
16
16
 
17
- nodes : none { [] }
18
- | linespace_star node { [val[1]] }
19
- | linespace_star empty_node { [] }
20
- | nodes node { [*val[0], val[1]] }
21
- | nodes empty_node { val[0] }
22
- node : node_decl node_term { val[0].tap { |x| x.children = nil } }
23
- | node_decl node_children node_term { val[0].tap { |x| x.children = val[1] } }
24
- | node_decl empty_children node_term { val[0].tap { |x| x.children = nil } }
25
- node_decl : identifier { KDL::Node.new(val[0]) }
26
- | type identifier { KDL::Node.new(val[1], type: val[0]) }
27
- | node_decl WS value { val[0].tap { |x| x.arguments << val[2] } }
28
- | node_decl WS SLASHDASH ws_star value { val[0] }
29
- | node_decl WS property { val[0].tap { |x| x.properties[val[2][0]] = val[2][1] } }
30
- | node_decl WS SLASHDASH ws_star property { val[0] }
31
- empty_node: SLASHDASH ws_star node
32
- node_children: ws_star LBRACE nodes RBRACE { val[2] }
33
- | ws_star LBRACE linespace_star RBRACE { [] }
17
+ nodes : none { [] }
18
+ | linespace_star node { [val[1]] }
19
+ | linespace_star empty_node { [] }
20
+ | nodes node { [*val[0], val[1]] }
21
+ | nodes empty_node { val[0] }
22
+ node : untyped_node { val[0] }
23
+ | type untyped_node { val[1].as_type(val[0], @type_parsers.fetch(val[0], nil)) }
24
+ untyped_node : node_decl node_term { val[0].tap { |x| x.children = nil } }
25
+ | node_decl node_children node_term { val[0].tap { |x| x.children = val[1] } }
26
+ | node_decl empty_children node_term { val[0].tap { |x| x.children = nil } }
27
+ node_decl : identifier { KDL::Node.new(val[0]) }
28
+ | node_decl WS value { val[0].tap { |x| x.arguments << val[2] } }
29
+ | node_decl WS SLASHDASH ws_star value { val[0] }
30
+ | node_decl WS property { val[0].tap { |x| x.properties[val[2][0]] = val[2][1] } }
31
+ | node_decl WS SLASHDASH ws_star property { val[0] }
32
+ empty_node : SLASHDASH ws_star node
33
+ node_children : ws_star LBRACE nodes RBRACE { val[2] }
34
+ | ws_star LBRACE linespace_star RBRACE { [] }
34
35
  empty_children: SLASHDASH node_children
35
36
  | WS empty_children
36
37
  node_term: linespaces | semicolon_term
@@ -45,7 +46,7 @@ rule
45
46
  property: identifier EQUALS value { [val[0], val[2]] }
46
47
 
47
48
  value : untyped_value
48
- | type untyped_value { val[1].as_type(val[0]) }
49
+ | type untyped_value { val[1].as_type(val[0], @type_parsers.fetch(val[0], nil)) }
49
50
 
50
51
  untyped_value : STRING { KDL::Value::String.new(val[0].value) }
51
52
  | RAWSTRING { KDL::Value::String.new(val[0].value) }
@@ -65,7 +66,13 @@ rule
65
66
  none: { nil }
66
67
 
67
68
  ---- inner
68
- def parse(str)
69
+
70
+ def parse(str, options = {})
71
+ if options.fetch(:parse_types, true)
72
+ @type_parsers = ::KDL::Types::MAPPING.merge(options.fetch(:type_parsers, {}))
73
+ else
74
+ @type_parsers = {}
75
+ end
69
76
  @tokenizer = ::KDL::Tokenizer.new(str)
70
77
  do_parse
71
78
  end
data/lib/kdl/node.rb CHANGED
@@ -38,6 +38,23 @@ module KDL
38
38
  children == other.children
39
39
  end
40
40
 
41
+ def as_type(type, parser = nil)
42
+ if parser.nil?
43
+ @type = type
44
+ self
45
+ else
46
+ result = parser.call(self, type)
47
+
48
+ return self.as_type(type) if result.nil?
49
+
50
+ unless result.is_a?(::KDL::Node)
51
+ raise ArgumentError, "expected parser to return an instance of ::KDL::Node, got `#{result.class}'"
52
+ end
53
+
54
+ result
55
+ end
56
+ end
57
+
41
58
  private
42
59
 
43
60
  def id_to_s(id)