kpeg 0.8.1 → 0.8.2

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.
@@ -604,6 +604,28 @@ module KPeg
604
604
  end
605
605
  end
606
606
 
607
+ class Bounds < Operator
608
+ def initialize(op)
609
+ super()
610
+ @op = op
611
+ end
612
+
613
+ attr_reader :op
614
+
615
+ def ==(obj)
616
+ case obj
617
+ when Bounds
618
+ @op == obj.op
619
+ else
620
+ super
621
+ end
622
+ end
623
+
624
+ def inspect
625
+ inspect_type "bounds", @op.inspect
626
+ end
627
+ end
628
+
607
629
  class Grammar
608
630
  def initialize
609
631
  @rules = {}
@@ -818,6 +840,10 @@ module KPeg
818
840
  def collect(op)
819
841
  Collect.new Grammar.resolve(op)
820
842
  end
843
+
844
+ def bounds(op)
845
+ Bounds.new Grammar.resolve(op)
846
+ end
821
847
  end
822
848
 
823
849
 
@@ -163,6 +163,10 @@ module KPeg
163
163
  io.print "< "
164
164
  render_op io, op.op
165
165
  io.print " >"
166
+ when Bounds
167
+ io.print "@< "
168
+ render_op io, op.op
169
+ io.print " >"
166
170
  else
167
171
  raise "Unknown op type - #{op.class}"
168
172
  end
@@ -1,3 +1,3 @@
1
1
  module KPeg
2
- VERSION = "0.8.1"
2
+ VERSION = "0.8.2"
3
3
  end
@@ -1301,6 +1301,56 @@ end
1301
1301
  assert_equal "hello", code.result
1302
1302
  end
1303
1303
 
1304
+ def test_bounds
1305
+ gram = KPeg.grammar do |g|
1306
+ g.root = g.seq(g.bounds("hello"), g.action(" bounds "))
1307
+ end
1308
+
1309
+ str = <<-STR
1310
+ require 'kpeg/compiled_parser'
1311
+
1312
+ class Test < KPeg::CompiledParser
1313
+
1314
+ # root = @< "hello" > { bounds }
1315
+ def _root
1316
+
1317
+ _save = self.pos
1318
+ while true # sequence
1319
+ _bounds_start = self.pos
1320
+ _tmp = match_string("hello")
1321
+ if _tmp
1322
+ bounds = [_bounds_start, self.pos]
1323
+ end
1324
+ unless _tmp
1325
+ self.pos = _save
1326
+ break
1327
+ end
1328
+ @result = begin; bounds ; end
1329
+ _tmp = true
1330
+ unless _tmp
1331
+ self.pos = _save
1332
+ end
1333
+ break
1334
+ end # end sequence
1335
+
1336
+ set_failed_rule :_root unless _tmp
1337
+ return _tmp
1338
+ end
1339
+
1340
+ Rules = {}
1341
+ Rules[:_root] = rule_info("root", "@< \\\"hello\\\" > { bounds }")
1342
+ end
1343
+ STR
1344
+
1345
+ cg = KPeg::CodeGenerator.new "Test", gram
1346
+
1347
+ assert_equal str, cg.output
1348
+
1349
+ code = cg.make("hello")
1350
+ assert code.parse
1351
+ assert_equal [0,5], code.result
1352
+ end
1353
+
1304
1354
  def test_parse_error
1305
1355
  gram = KPeg.grammar do |g|
1306
1356
  g.world = "world"
@@ -95,6 +95,17 @@ b(p) = x
95
95
  assert_equal "(1)", rule.op.arguments
96
96
  end
97
97
 
98
+ def test_invoke_with_double_quoted_strings
99
+ m = match "a=b(\")\")"
100
+ assert_equal "(\")\")", m.find("a").op.arguments
101
+ end
102
+
103
+ def test_invoke_with_single_quoted_strings
104
+ m = match "a=b(')')"
105
+ assert_equal "(')')", m.find("a").op.arguments
106
+ end
107
+
108
+
98
109
  def test_invoke_with_multiple_args
99
110
  assert_rule G.invoke("b", "(1,2)"), match("a=b(1,2)"), "a"
100
111
  end
@@ -348,6 +359,11 @@ Value = NUMBER:i { i }
348
359
  assert_rule G.collect(G.seq(:b, :c)), m
349
360
  end
350
361
 
362
+ def test_bounds
363
+ m = match 'a = @< b c >'
364
+ assert_rule G.bounds(G.seq(:b, :c)), m
365
+ end
366
+
351
367
  def test_comment
352
368
  m = match "a=b # this is a comment\n"
353
369
  assert_rule G.ref('b'), m
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kpeg
3
3
  version: !ruby/object:Gem::Version
4
- hash: 61
4
+ hash: 59
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 1
10
- version: 0.8.1
9
+ - 2
10
+ version: 0.8.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Evan Phoenix
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-15 00:00:00 -07:00
18
+ date: 2011-04-18 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency