ldpath 0.2.0 → 0.3.0

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
  SHA1:
3
- metadata.gz: f4ca1397937e56ffe65839c30b5e9635e1d3b8ea
4
- data.tar.gz: 49ef72928a26735e3ad93d896eca7ee509d7a276
3
+ metadata.gz: 901eaf079202867fe88880b4e6297361dcd4574a
4
+ data.tar.gz: 9d80b28a1555f00f2400d84bb4573733cc1158c3
5
5
  SHA512:
6
- metadata.gz: ee4f3b539e710feeb3fd461172feffede298fd771c78f7d025744d0dbb98b10c8300dfc71ada2dfb9cbbc7e84e71c55553faf28fef3f7404da1752a055e5e7dd
7
- data.tar.gz: 344ce5f8a2827ada956c5f1ab19c5ed675fcb6ead743f9fb0f53d567ff32ed185c649227d44187f2d00ff3a500d21e8f9ebbafd3e7adc78d1ba4ecc989cc1539
6
+ metadata.gz: d110b7f52841afd8dec645ce426a117692ae0a7c64f90585196b481c80765472ad7d148fc132da7e79fcb25a373783007a3d8a23a252c883606ba293a5c4b8db
7
+ data.tar.gz: b2fd98589dfd3dceec73c5eac70eba71696647878d54f6acd437707f3427b6cc28361b88b0e94d20325738aac2f08c65e9958f4e03a1afdfefd02a58b1da6a20
@@ -71,16 +71,21 @@ module Ldpath
71
71
  end
72
72
 
73
73
  def get(uri, context, list, idx)
74
- flatten(uri, context, list)[idx.to_i]
74
+ idx = idx.respond_to?(:to_i) ? idx.to_i : idx.to_s.to_i
75
+
76
+ flatten(uri, context, list)[idx]
75
77
  end
76
78
 
77
79
  def subList(uri, context, list, idx_start, idx_end = nil)
78
80
  arr = flatten(uri, context, list)
79
81
 
82
+ idx_start = idx_start.respond_to?(:to_i) ? idx_start.to_i : idx_start.to_s.to_i
83
+ idx_end &&= idx_end.respond_to?(:to_i) ? idx_end.to_i : idx_end.to_s.to_i
84
+
80
85
  if idx_end
81
- arr[(idx_start.to_i..(idx_end.to_i - idx_start.to_i))]
86
+ arr[(idx_start.to_i..(idx_end - idx_start))]
82
87
  else
83
- arr.drop(idx_start.to_i)
88
+ arr.drop(idx_start)
84
89
  end
85
90
  end
86
91
 
@@ -180,7 +185,7 @@ module Ldpath
180
185
  def xpath(uri, context, xpath, node)
181
186
  x = Array(xpath).flatten.first
182
187
  Array(node).flatten.compact.map do |n|
183
- Nokogiri::XML(n).xpath(x, prefixes.map { |k, v| [k, v.to_s] }).map(&:text)
188
+ Nokogiri::XML(n.to_s).xpath(x.to_s, prefixes.map { |k, v| [k, v.to_s] }).map(&:text)
184
189
  end
185
190
  end
186
191
  end
data/lib/ldpath/parser.rb CHANGED
@@ -27,36 +27,36 @@ module Ldpath
27
27
  rule(:single_line_comment) { str("#") >> (eol.absent? >> any).repeat }
28
28
 
29
29
  # simple types
30
- rule(:integer) { match("[+-]").maybe >> match("\\d+") }
31
- rule(:decimal) { match("[+-]").maybe >> match("\\d*") >> str(".") >> match("\\d+") }
30
+ rule(:integer) { match("[+-]").maybe >> match("\\d").repeat(1) }
31
+ rule(:decimal) { match("[+-]").maybe >> match("\\d").repeat >> str(".") >> match("\\d").repeat(1) }
32
32
  rule(:double) do
33
33
  match("[+-]").maybe >> (
34
- (match("\\d+") >> str('.') >> match("\\d*") >> exponent) |
35
- (str('.') >> match("\\d+") >> exponent) |
36
- (match("\\d+") >> exponent)
34
+ (match("\\d").repeat(1) >> str('.') >> match("\\d").repeat >> exponent) |
35
+ (str('.') >> match("\\d").repeat(1) >> exponent) |
36
+ (match("\\d").repeat(1) >> exponent)
37
37
  )
38
38
  end
39
39
 
40
- rule(:exponent) { match('[Ee]') >> match("[+-]").maybe >> match("\\d+") }
41
- rule(:numeric_literal) { integer | decimal | double }
42
- rule(:boolean_literal) { str('true') | str('false') }
40
+ rule(:exponent) { match('[Ee]') >> match("[+-]").maybe >> match("\\d").repeat(1) }
41
+ rule(:numeric_literal) { integer.as(:integer) | decimal.as(:decimal) | double.as(:double) }
42
+ rule(:boolean_literal) { str('true').as(:true) | str('false').as(:false) }
43
43
 
44
44
  rule(:string) { string_literal_quote | string_literal_single_quote | string_literal_long_single_quote | string_literal_long_quote }
45
45
 
46
46
  rule(:string_literal_quote) do
47
- str('"') >> (match("[^\\\"\\\\\\r\\n]") | echar | uchar).repeat.as(:literal) >> str('"')
47
+ str('"') >> (match("[^\\\"\\\\\\r\\n]") | echar | uchar).repeat.as(:string) >> str('"')
48
48
  end
49
49
 
50
50
  rule(:string_literal_single_quote) do
51
- str("'") >> (match("[^'\\\\\\r\\n]") | echar | uchar).repeat.as(:literal) >> str("'")
51
+ str("'") >> (match("[^'\\\\\\r\\n]") | echar | uchar).repeat.as(:string) >> str("'")
52
52
  end
53
53
 
54
54
  rule(:string_literal_long_quote) do
55
- str('"""') >> (str('"""').absent? >> match("[^\\\\]") | echar | uchar).repeat.as(:literal) >> str('"""')
55
+ str('"""') >> (str('"""').absent? >> match("[^\\\\]") | echar | uchar).repeat.as(:string) >> str('"""')
56
56
  end
57
57
 
58
58
  rule(:string_literal_long_single_quote) do
59
- str("'''") >> (str("'''").absent? >> match("[^\\\\]") | echar | uchar).repeat.as(:literal) >> str("'''")
59
+ str("'''") >> (str("'''").absent? >> match("[^\\\\]") | echar | uchar).repeat.as(:string) >> str("'''")
60
60
  end
61
61
 
62
62
  # operators
@@ -126,16 +126,22 @@ module Ldpath
126
126
 
127
127
  # "xyz"; 0.123e52; true
128
128
  rule(:literal) do
129
- rdf_literal | numeric_literal | boolean_literal
129
+ (
130
+ rdf_literal |
131
+ numeric_literal |
132
+ boolean_literal
133
+ ).as(:literal)
130
134
  end
131
135
 
132
136
  # "xyz"; "xyz"^^a; "xyz"@en
133
137
  rule(:rdf_literal) do
134
- string >> (literal_language_test | literal_type_test).maybe
138
+ string >> lang >> identifier.as(:lang) |
139
+ string >> type >> iri.as(:type) |
140
+ string
135
141
  end
136
142
 
137
143
  rule(:node) do
138
- iri.as(:iri) | literal.as(:literal)
144
+ iri | literal
139
145
  end
140
146
 
141
147
  # @prefix id = iri ;
@@ -252,7 +258,7 @@ module Ldpath
252
258
  loose_property_selector |
253
259
  wildcard_selector |
254
260
  reverse_property_selector |
255
- string_constant_selector |
261
+ literal_selector |
256
262
  recursive_path_selector |
257
263
  grouped_selector |
258
264
  tap_selector
@@ -316,8 +322,8 @@ module Ldpath
316
322
  inverse.as(:reverse) >> iri.as(:property)
317
323
  end
318
324
 
319
- rule(:string_constant_selector) do
320
- string
325
+ rule(:literal_selector) do
326
+ literal.as(:literal)
321
327
  end
322
328
 
323
329
  # (x)?; (x)*; (x)+; (x){3,5}
@@ -29,7 +29,23 @@ module Ldpath
29
29
  end
30
30
 
31
31
  # Core types
32
- rule(literal: simple(:literal)) { literal.to_s }
32
+ rule(true: simple(:true)) { true }
33
+ rule(false: simple(:false)) { false }
34
+ rule(integer: simple(:integer)) { integer.to_i }
35
+ rule(double: simple(:double)) { double.to_f }
36
+ rule(decimal: simple(:decimal)) { decimal.to_f }
37
+ rule(string: simple(:string), lang: simple(:lang)) { RDF::Literal.new(string, language: lang) }
38
+ rule(string: simple(:string), type: simple(:type)) { RDF::Literal.new(string, datatype: RDF::URI.new(type)) }
39
+ rule(string: simple(:string)) { string }
40
+ rule(literal: simple(:literal)) do
41
+ case literal
42
+ when RDF::Literal
43
+ literal
44
+ else
45
+ RDF::Literal(literal)
46
+ end
47
+ end
48
+
33
49
  rule(iri: simple(:iri)) { RDF::IRI.new(iri) }
34
50
 
35
51
  # Namespaces
@@ -1,3 +1,3 @@
1
1
  module Ldpath
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -154,7 +154,11 @@ describe Ldpath::Parser do
154
154
  end
155
155
 
156
156
  it "may be a numeric literal" do
157
- subject.node.parse '0'
157
+ subject.node.parse '123'
158
+ end
159
+
160
+ it "may be a decimal literal" do
161
+ subject.decimal.parse '0.123'
158
162
  end
159
163
 
160
164
  it "may be a boolean literal" do
@@ -2,13 +2,42 @@ require 'spec_helper'
2
2
  require 'pp'
3
3
  describe Ldpath::Transform do
4
4
  let(:parser) { Ldpath::Parser.new }
5
- it "should transform literals" do
6
- subject.apply(literal: "xyz")
7
- end
5
+ describe "transforms nodes" do
6
+ let(:parser) { Ldpath::Parser.new.node }
7
+ it "handles iris" do
8
+ actual = subject.apply parser.parse("<info:a>")
9
+ expect(actual).to eq RDF::URI.new("info:a")
10
+ end
11
+
12
+ it "handles strings" do
13
+ actual = subject.apply parser.parse('"xyz"')
14
+ expect(actual).to eq RDF::Literal.new("xyz")
15
+ end
8
16
 
9
- it "should transform uris" do
10
- actual = subject.apply(iri: "info:a")
11
- expect(actual).to eq RDF::URI.new("info:a")
17
+ it "handles langstrings" do
18
+ actual = subject.apply parser.parse('"xyz"@fr')
19
+ expect(actual).to eq RDF::Literal.new("xyz", language: 'fr')
20
+ end
21
+
22
+ it "handles typed literals" do
23
+ actual = subject.apply parser.parse('"xyz"^^info:x')
24
+ expect(actual).to eq RDF::Literal.new("xyz", datatype: RDF::URI.new("info:x"))
25
+ end
26
+
27
+ it "handles integers" do
28
+ actual = subject.apply parser.parse("0")
29
+ expect(actual).to eq RDF::Literal.new(0)
30
+ end
31
+
32
+ it "handles decimals" do
33
+ actual = subject.apply parser.parse("0.01")
34
+ expect(actual).to eq RDF::Literal.new(0.01)
35
+ end
36
+
37
+ it "handles doubles" do
38
+ actual = subject.apply parser.parse("1e-5")
39
+ expect(actual).to eq RDF::Literal.new(0.00001)
40
+ end
12
41
  end
13
42
 
14
43
  it "should transform prefix + localNames" do
@@ -32,7 +32,7 @@ describe "LDPath list functions" do
32
32
  let(:program) do
33
33
  Ldpath::Program.parse <<-EOF
34
34
  @prefix ex : <http://example.com/> ;
35
- list_item = fn:get(ex:list, "1") :: xsd:string ;
35
+ list_item = fn:get(ex:list, 1) :: xsd:string ;
36
36
  EOF
37
37
  end
38
38
 
@@ -45,8 +45,8 @@ describe "LDPath list functions" do
45
45
  let(:program) do
46
46
  Ldpath::Program.parse <<-EOF
47
47
  @prefix ex : <http://example.com/> ;
48
- list_items = fn:subList(ex:list, "1") :: xsd:string ;
49
- list_items_by_range = fn:subList(ex:list, "0", "1") :: xsd:string ;
48
+ list_items = fn:subList(ex:list, 1) :: xsd:string ;
49
+ list_items_by_range = fn:subList(ex:list, 0, 1) :: xsd:string ;
50
50
  EOF
51
51
  end
52
52
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ldpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Beer