citrus 2.1.2 → 2.2.0

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.
@@ -1,27 +1,32 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  class NotPredicateTest < Test::Unit::TestCase
4
-
5
4
  def test_terminal?
6
5
  rule = NotPredicate.new
7
6
  assert_equal(false, rule.terminal?)
8
7
  end
9
8
 
10
- def test_match
11
- rule = NotPredicate.new('a')
9
+ def test_exec
10
+ rule = NotPredicate.new('abc')
11
+ events = rule.exec(Input.new('def'))
12
+ assert_equal([rule.id, CLOSE, 0], events)
13
+ end
12
14
 
13
- match = rule.match(input('a'))
14
- assert_equal(nil, match)
15
+ def test_exec_miss
16
+ rule = NotPredicate.new('abc')
17
+ events = rule.exec(Input.new('abc'))
18
+ assert_equal([], events)
19
+ end
15
20
 
16
- match = rule.match(input('b'))
17
- assert(match)
18
- assert_equal('', match)
19
- assert_equal(0, match.length)
21
+ def test_consumption
22
+ rule = NotPredicate.new('abc')
23
+ input = Input.new('def')
24
+ events = rule.exec(input)
25
+ assert_equal(0, input.pos)
20
26
  end
21
27
 
22
28
  def test_to_s
23
29
  rule = NotPredicate.new('a')
24
30
  assert_equal('!"a"', rule.to_s)
25
31
  end
26
-
27
32
  end
@@ -1,7 +1,6 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  class ParseErrorTest < Test::Unit::TestCase
4
-
5
4
  Sentence = Grammar.new do
6
5
  include Words
7
6
 
@@ -52,5 +51,4 @@ class ParseErrorTest < Test::Unit::TestCase
52
51
  assert_equal(3, e.line_offset)
53
52
  end
54
53
  end
55
-
56
- end
54
+ end
data/test/repeat_test.rb CHANGED
@@ -1,53 +1,81 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  class RepeatTest < Test::Unit::TestCase
4
-
5
4
  def test_terminal?
6
5
  rule = Repeat.new
7
6
  assert_equal(false, rule.terminal?)
8
7
  end
9
8
 
10
- def test_match_zero_or_one
11
- rule = Repeat.new('a', 0, 1)
9
+ def test_exec_zero_or_one
10
+ abc = Rule.new('abc')
11
+ rule = Repeat.new(abc, 0, 1)
12
12
 
13
- match = rule.match(input(''))
14
- assert(match)
15
- assert_equal('', match)
16
- assert_equal(0, match.length)
13
+ events = rule.exec(Input.new(''))
14
+ assert_equal([rule.id, CLOSE, 0], events)
17
15
 
18
- match = rule.match(input('a'))
19
- assert(match)
20
- assert_equal('a', match)
21
- assert_equal(1, match.length)
16
+ events = rule.exec(Input.new('abc'))
17
+ assert_equal([rule.id, abc.id, CLOSE, 3, CLOSE, 3], events)
18
+
19
+ events = rule.exec(Input.new('abc' * 3))
20
+ assert_equal([rule.id, abc.id, CLOSE, 3, CLOSE, 3], events)
22
21
  end
23
22
 
24
- def test_match_one_or_more
25
- rule = Repeat.new('a', 1, Infinity)
23
+ def test_exec_zero_or_more
24
+ abc = Rule.new('abc')
25
+ rule = Repeat.new(abc, 0, Infinity)
26
26
 
27
- match = rule.match(input(''))
28
- assert_equal(nil, match)
27
+ events = rule.exec(Input.new(''))
28
+ assert_equal([rule.id, CLOSE, 0], events)
29
29
 
30
- match = rule.match(input('a'))
31
- assert(match)
32
- assert_equal('a', match)
33
- assert_equal(1, match.length)
30
+ events = rule.exec(Input.new('abc'))
31
+ assert_equal([rule.id, abc.id, CLOSE, 3, CLOSE, 3], events)
34
32
 
35
- match = rule.match(input('a' * 200))
36
- assert(match)
37
- assert_equal('a' * 200, match)
38
- assert_equal(200, match.length)
33
+ expected_events = [
34
+ rule.id,
35
+ abc.id, CLOSE, 3,
36
+ abc.id, CLOSE, 3,
37
+ abc.id, CLOSE, 3,
38
+ CLOSE, 9
39
+ ]
40
+
41
+ events = rule.exec(Input.new('abc' * 3))
42
+ assert_equal(expected_events, events)
39
43
  end
40
44
 
41
- def test_match_one
42
- rule = Repeat.new('a', 1, 1)
45
+ def test_exec_one_or_more
46
+ abc = Rule.new('abc')
47
+ rule = Repeat.new(abc, 1, Infinity)
48
+
49
+ events = rule.exec(Input.new(''))
50
+ assert_equal([], events)
51
+
52
+ events = rule.exec(Input.new('abc'))
53
+ assert_equal([rule.id, abc.id, CLOSE, 3, CLOSE, 3], events)
43
54
 
44
- match = rule.match(input(''))
45
- assert_equal(nil, match)
55
+ expected_events = [
56
+ rule.id,
57
+ abc.id, CLOSE, 3,
58
+ abc.id, CLOSE, 3,
59
+ abc.id, CLOSE, 3,
60
+ CLOSE, 9
61
+ ]
46
62
 
47
- match = rule.match(input('a'))
48
- assert(match)
49
- assert_equal('a', match)
50
- assert_equal(1, match.length)
63
+ events = rule.exec(Input.new('abc' * 3))
64
+ assert_equal(expected_events, events)
65
+ end
66
+
67
+ def test_exec_one
68
+ abc = Rule.new('abc')
69
+ rule = Repeat.new(abc, 1, 1)
70
+
71
+ events = rule.exec(Input.new(''))
72
+ assert_equal([], events)
73
+
74
+ events = rule.exec(Input.new('abc'))
75
+ assert_equal([rule.id, abc.id, CLOSE, 3, CLOSE, 3], events)
76
+
77
+ events = rule.exec(Input.new('abc' * 3))
78
+ assert_equal([rule.id, abc.id, CLOSE, 3, CLOSE, 3], events)
51
79
  end
52
80
 
53
81
  def test_operator
@@ -94,5 +122,4 @@ class RepeatTest < Test::Unit::TestCase
94
122
  rule = Repeat.new('a', 1, Infinity)
95
123
  assert_equal('"a"+', rule.to_s)
96
124
  end
97
-
98
125
  end
@@ -1,41 +1,30 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  class SequenceTest < Test::Unit::TestCase
4
-
5
4
  def test_terminal?
6
5
  rule = Sequence.new
7
6
  assert_equal(false, rule.terminal?)
8
7
  end
9
8
 
10
- def test_match
11
- rule = Sequence.new(%w<a b>)
12
-
13
- match = rule.match(input(''))
14
- assert_equal(nil, match)
15
-
16
- match = rule.match(input('a'))
17
- assert_equal(nil, match)
18
-
19
- match = rule.match(input('ab'))
20
- assert(match)
21
- assert_equal('ab', match)
22
- assert_equal(2, match.length)
23
- end
24
-
25
- def test_match_mixed
26
- rule = Sequence.new([/\d+/, '+', /\d+/])
27
- match = rule.match(input('1+2'))
28
- assert(match)
29
- assert_equal('1+2', match)
30
- assert_equal(3, match.length)
31
- end
32
-
33
- def test_match_embed
34
- rule = Sequence.new([/[0-9]+/, Choice.new(%w<+ ->), /\d+/])
35
- match = rule.match(input('1+2'))
36
- assert(match)
37
- assert_equal('1+2', match)
38
- assert_equal(3, match.length)
9
+ def test_exec
10
+ a = Rule.new('a')
11
+ b = Rule.new('b')
12
+ c = Rule.new('c')
13
+ rule = Sequence.new([ a, b, c ])
14
+
15
+ events = rule.exec(Input.new(''))
16
+ assert_equal([], events)
17
+
18
+ expected_events = [
19
+ rule.id,
20
+ a.id, CLOSE, 1,
21
+ b.id, CLOSE, 1,
22
+ c.id, CLOSE, 1,
23
+ CLOSE, 3
24
+ ]
25
+
26
+ events = rule.exec(Input.new('abc'))
27
+ assert_equal(expected_events, events)
39
28
  end
40
29
 
41
30
  def test_to_s
@@ -49,5 +38,4 @@ class SequenceTest < Test::Unit::TestCase
49
38
  rule = Sequence.new([rule1, rule2])
50
39
  assert_equal('("a" "b") ("c" "d")', rule.to_s)
51
40
  end
52
-
53
41
  end
@@ -0,0 +1,55 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class StringTerminalTest < Test::Unit::TestCase
4
+ def test_terminal?
5
+ rule = StringTerminal.new
6
+ assert(rule.terminal?)
7
+ end
8
+
9
+ def test_exec
10
+ rule = StringTerminal.new('abc')
11
+ events = rule.exec(Input.new('abc'))
12
+ assert_equal([rule.id, CLOSE, 3], events)
13
+ end
14
+
15
+ def test_exec_miss
16
+ rule = StringTerminal.new('abc')
17
+ events = rule.exec(Input.new('def'))
18
+ assert_equal([], events)
19
+ end
20
+
21
+ def test_exec_short
22
+ rule = StringTerminal.new('abc')
23
+ events = rule.exec(Input.new('ab'))
24
+ assert_equal([], events)
25
+ end
26
+
27
+ def test_exec_long
28
+ rule = StringTerminal.new('abc')
29
+ events = rule.exec(Input.new('abcd'))
30
+ assert_equal([rule.id, CLOSE, 3], events)
31
+ end
32
+
33
+ def test_exec_case_insensitive
34
+ rule = StringTerminal.new('abc', Regexp::IGNORECASE)
35
+
36
+ events = rule.exec(Input.new('abc'))
37
+ assert_equal([rule.id, CLOSE, 3], events)
38
+
39
+ events = rule.exec(Input.new('ABC'))
40
+ assert_equal([rule.id, CLOSE, 3], events)
41
+
42
+ events = rule.exec(Input.new('Abc'))
43
+ assert_equal([rule.id, CLOSE, 3], events)
44
+ end
45
+
46
+ def test_to_s
47
+ rule = StringTerminal.new('abc')
48
+ assert_equal('"abc"', rule.to_s)
49
+ end
50
+
51
+ def test_to_s_case_insensitive
52
+ rule = StringTerminal.new('abc', Regexp::IGNORECASE)
53
+ assert_equal('`abc`', rule.to_s)
54
+ end
55
+ end
data/test/super_test.rb CHANGED
@@ -1,66 +1,66 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
- Citrus.load(File.dirname(__FILE__) + '/_files/super')
3
2
 
4
3
  class SuperTest < Test::Unit::TestCase
5
-
6
4
  def test_terminal?
7
5
  rule = Super.new
8
6
  assert_equal(false, rule.terminal?)
9
7
  end
10
8
 
11
- def test_match
9
+ def test_exec
10
+ ghi = Rule.new('ghi')
12
11
  grammar1 = Grammar.new {
13
- rule :a, 'a'
12
+ rule :a, 'abc'
14
13
  }
15
-
16
14
  grammar2 = Grammar.new {
17
15
  include grammar1
18
- rule :a, any('b', sup)
16
+ rule :a, any(ghi, sup)
19
17
  }
18
+ rule_2a = grammar2.rule(:a)
19
+ rule_1a = grammar1.rule(:a)
20
20
 
21
- match = grammar2.parse('b')
22
- assert(match)
23
- assert_equal('b', match)
24
- assert_equal(1, match.length)
21
+ events = rule_2a.exec(Input.new('abc'))
22
+ assert_equal([rule_2a.id, rule_1a.id, CLOSE, 3, CLOSE, 3], events)
25
23
 
26
- match = grammar2.parse('a')
27
- assert(match)
28
- assert_equal('a', match)
29
- assert_equal(1, match.length)
24
+ events = rule_2a.exec(Input.new('ghi'))
25
+ assert_equal([rule_2a.id, ghi.id, CLOSE, 3, CLOSE, 3], events)
30
26
  end
31
27
 
32
- def test_peg
33
- match = SuperTwo.parse('2')
34
- assert(match)
35
-
36
- match = SuperTwo.parse('1')
37
- assert(match)
28
+ def test_exec_miss
29
+ grammar1 = Grammar.new {
30
+ rule :a, 'abc'
31
+ }
32
+ grammar2 = Grammar.new {
33
+ include grammar1
34
+ rule :a, any('def', sup)
35
+ }
36
+ rule_2a = grammar2.rule(:a)
37
+ events = rule_2a.exec(Input.new('ghi'))
38
+ assert_equal([], events)
38
39
  end
39
40
 
40
- def test_nested
41
+ def test_exec_aliased
41
42
  grammar1 = Grammar.new {
42
- rule :a, 'a'
43
- rule :b, 'b'
43
+ rule :a, 'abc'
44
+ rule :b, 'def'
44
45
  }
45
-
46
46
  grammar2 = Grammar.new {
47
47
  include grammar1
48
48
  rule :a, any(sup, :b)
49
49
  rule :b, sup
50
50
  }
51
+ rule_2a = grammar2.rule(:a)
52
+ rule_1a = grammar1.rule(:a)
53
+ rule_1b = grammar1.rule(:b)
51
54
 
52
- match = grammar2.parse('a')
53
- assert(match)
54
- assert_equal(:a, match.name)
55
+ events = rule_2a.exec(Input.new('abc'))
56
+ assert_equal([rule_2a.id, rule_1a.id, CLOSE, 3, CLOSE, 3], events)
55
57
 
56
- match = grammar2.parse('b')
57
- assert(match)
58
- assert_equal(:b, match.name)
58
+ events = rule_2a.exec(Input.new('def'))
59
+ assert_equal([rule_2a.id, rule_1b.id, CLOSE, 3, CLOSE, 3], events)
59
60
  end
60
61
 
61
62
  def test_to_s
62
63
  rule = Super.new
63
64
  assert_equal('super', rule.to_s)
64
65
  end
65
-
66
66
  end
@@ -1,56 +1,31 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  class TerminalTest < Test::Unit::TestCase
4
-
5
4
  def test_terminal?
6
5
  rule = Terminal.new
7
6
  assert(rule.terminal?)
8
7
  end
9
8
 
10
- def test_regexp_match
9
+ def test_exec
11
10
  rule = Terminal.new(/\d+/)
12
- match = rule.match(input('123 456'))
13
- assert(match)
14
- assert_equal('123', match)
15
- assert_equal(3, match.length)
11
+ events = rule.exec(Input.new('123'))
12
+ assert_equal([rule.id, CLOSE, 3], events)
16
13
  end
17
14
 
18
- def test_regexp_match_failure
15
+ def test_exec_long
19
16
  rule = Terminal.new(/\d+/)
20
- match = rule.match(input(' 456'))
21
- assert_equal(nil, match)
17
+ events = rule.exec(Input.new('123 456'))
18
+ assert_equal([rule.id, CLOSE, 3], events)
22
19
  end
23
20
 
24
- def test_regexp_to_s
21
+ def test_exec_miss
25
22
  rule = Terminal.new(/\d+/)
26
- assert_equal('/\\d+/', rule.to_s)
27
- end
28
-
29
- def test_string_match
30
- rule = Terminal.new('abc')
31
- match = rule.match(input('abc'))
32
- assert(match)
33
- assert_equal('abc', match)
34
- assert_equal(3, match.length)
35
- end
36
-
37
- def test_string_match_short
38
- rule = Terminal.new('abc')
39
- match = rule.match(input('ab'))
40
- assert_equal(nil, match)
23
+ events = rule.exec(Input.new(' 123'))
24
+ assert_equal([], events)
41
25
  end
42
26
 
43
- def test_string_match_long
44
- rule = Terminal.new('abc')
45
- match = rule.match(input('abcd'))
46
- assert(match)
47
- assert_equal('abc', match)
48
- assert_equal(3, match.length)
49
- end
50
-
51
- def test_string_to_s
52
- rule = Terminal.new('abc')
53
- assert_equal('"abc"', rule.to_s)
27
+ def test_to_s
28
+ rule = Terminal.new(/\d+/)
29
+ assert_equal('/\\d+/', rule.to_s)
54
30
  end
55
-
56
31
  end