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.
- data/README +106 -49
- data/benchmark/after.dat +192 -0
- data/benchmark/before.dat +192 -0
- data/citrus.gemspec +0 -1
- data/doc/extras.markdown +16 -0
- data/doc/syntax.markdown +76 -29
- data/doc/testing.markdown +12 -20
- data/examples/calc.citrus +12 -11
- data/examples/calc.rb +12 -11
- data/lib/citrus.rb +416 -253
- data/lib/citrus/file.rb +66 -33
- data/test/_files/super.citrus +1 -1
- data/test/_files/super2.citrus +13 -0
- data/test/alias_test.rb +18 -34
- data/test/and_predicate_test.rb +15 -10
- data/test/but_predicate_test.rb +22 -17
- data/test/calc_file_test.rb +1 -1
- data/test/choice_test.rb +12 -37
- data/test/{rule_test.rb → extension_test.rb} +17 -16
- data/test/file_test.rb +350 -244
- data/test/grammar_test.rb +5 -11
- data/test/helper.rb +1 -17
- data/test/input_test.rb +172 -2
- data/test/label_test.rb +0 -10
- data/test/match_test.rb +91 -35
- data/test/multibyte_test.rb +4 -4
- data/test/not_predicate_test.rb +15 -10
- data/test/parse_error_test.rb +1 -3
- data/test/repeat_test.rb +59 -32
- data/test/sequence_test.rb +19 -31
- data/test/string_terminal_test.rb +55 -0
- data/test/super_test.rb +31 -31
- data/test/terminal_test.rb +12 -37
- metadata +13 -23
- data/lib/citrus/debug.rb +0 -69
- data/test/debug_test.rb +0 -23
data/test/not_predicate_test.rb
CHANGED
@@ -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
|
11
|
-
rule = NotPredicate.new('
|
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
|
-
|
14
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
data/test/parse_error_test.rb
CHANGED
@@ -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
|
11
|
-
|
9
|
+
def test_exec_zero_or_one
|
10
|
+
abc = Rule.new('abc')
|
11
|
+
rule = Repeat.new(abc, 0, 1)
|
12
12
|
|
13
|
-
|
14
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
25
|
-
|
23
|
+
def test_exec_zero_or_more
|
24
|
+
abc = Rule.new('abc')
|
25
|
+
rule = Repeat.new(abc, 0, Infinity)
|
26
26
|
|
27
|
-
|
28
|
-
assert_equal(
|
27
|
+
events = rule.exec(Input.new(''))
|
28
|
+
assert_equal([rule.id, CLOSE, 0], events)
|
29
29
|
|
30
|
-
|
31
|
-
|
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
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
42
|
-
|
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
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
data/test/sequence_test.rb
CHANGED
@@ -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
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
assert_equal(
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
9
|
+
def test_exec
|
10
|
+
ghi = Rule.new('ghi')
|
12
11
|
grammar1 = Grammar.new {
|
13
|
-
rule :a, '
|
12
|
+
rule :a, 'abc'
|
14
13
|
}
|
15
|
-
|
16
14
|
grammar2 = Grammar.new {
|
17
15
|
include grammar1
|
18
|
-
rule :a, any(
|
16
|
+
rule :a, any(ghi, sup)
|
19
17
|
}
|
18
|
+
rule_2a = grammar2.rule(:a)
|
19
|
+
rule_1a = grammar1.rule(:a)
|
20
20
|
|
21
|
-
|
22
|
-
|
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
|
-
|
27
|
-
|
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
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
41
|
+
def test_exec_aliased
|
41
42
|
grammar1 = Grammar.new {
|
42
|
-
rule :a, '
|
43
|
-
rule :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
|
-
|
53
|
-
|
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
|
-
|
57
|
-
|
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
|
data/test/terminal_test.rb
CHANGED
@@ -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
|
9
|
+
def test_exec
|
11
10
|
rule = Terminal.new(/\d+/)
|
12
|
-
|
13
|
-
|
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
|
15
|
+
def test_exec_long
|
19
16
|
rule = Terminal.new(/\d+/)
|
20
|
-
|
21
|
-
assert_equal(
|
17
|
+
events = rule.exec(Input.new('123 456'))
|
18
|
+
assert_equal([rule.id, CLOSE, 3], events)
|
22
19
|
end
|
23
20
|
|
24
|
-
def
|
21
|
+
def test_exec_miss
|
25
22
|
rule = Terminal.new(/\d+/)
|
26
|
-
|
27
|
-
|
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
|
44
|
-
rule = Terminal.new(
|
45
|
-
|
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
|