citrus 1.0.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 +86 -0
- data/Rakefile +67 -0
- data/citrus.gemspec +29 -0
- data/examples/calc.citrus +103 -0
- data/examples/calc.rb +95 -0
- data/examples/calc_sugar.rb +94 -0
- data/lib/citrus.rb +904 -0
- data/lib/citrus/debug.rb +37 -0
- data/lib/citrus/peg.rb +375 -0
- data/lib/citrus/sugar.rb +25 -0
- data/test/alias_test.rb +66 -0
- data/test/and_predicate_test.rb +27 -0
- data/test/calc_peg_test.rb +6 -0
- data/test/calc_sugar_test.rb +6 -0
- data/test/calc_test.rb +6 -0
- data/test/choice_test.rb +62 -0
- data/test/expression_test.rb +29 -0
- data/test/fixed_width_test.rb +37 -0
- data/test/grammar_test.rb +129 -0
- data/test/helper.rb +143 -0
- data/test/label_test.rb +26 -0
- data/test/match_test.rb +76 -0
- data/test/not_predicate_test.rb +27 -0
- data/test/peg_test.rb +663 -0
- data/test/repeat_test.rb +93 -0
- data/test/rule_test.rb +49 -0
- data/test/sequence_test.rb +53 -0
- data/test/super_test.rb +66 -0
- metadata +133 -0
data/test/repeat_test.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class RepeatTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_terminal?
|
6
|
+
rule = Repeat.new
|
7
|
+
assert_equal(false, rule.terminal?)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_match_zero_or_one
|
11
|
+
rule = Repeat.new(0, 1, 'a')
|
12
|
+
|
13
|
+
match = rule.match(input(''))
|
14
|
+
assert(match)
|
15
|
+
assert_equal('', match.text)
|
16
|
+
assert_equal(0, match.length)
|
17
|
+
|
18
|
+
match = rule.match(input('a'))
|
19
|
+
assert(match)
|
20
|
+
assert_equal('a', match.text)
|
21
|
+
assert_equal(1, match.length)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_match_one_or_more
|
25
|
+
rule = Repeat.new(1, Infinity, 'a')
|
26
|
+
|
27
|
+
match = rule.match(input(''))
|
28
|
+
assert_equal(nil, match)
|
29
|
+
|
30
|
+
match = rule.match(input('a'))
|
31
|
+
assert(match)
|
32
|
+
assert_equal('a', match.text)
|
33
|
+
assert_equal(1, match.length)
|
34
|
+
|
35
|
+
match = rule.match(input('a' * 200))
|
36
|
+
assert(match)
|
37
|
+
assert_equal('a' * 200, match.text)
|
38
|
+
assert_equal(200, match.length)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_match_one
|
42
|
+
rule = Repeat.new(1, 1, 'a')
|
43
|
+
|
44
|
+
match = rule.match(input(''))
|
45
|
+
assert_equal(nil, match)
|
46
|
+
|
47
|
+
match = rule.match(input('a'))
|
48
|
+
assert(match)
|
49
|
+
assert_equal('a', match.text)
|
50
|
+
assert_equal(1, match.length)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_operator
|
54
|
+
rule = Repeat.new(1, 2, '')
|
55
|
+
assert_equal('1*2', rule.operator)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_operator_asterisk
|
59
|
+
rule = Repeat.new(0, Infinity, '')
|
60
|
+
assert_equal('*', rule.operator)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_operator_question_mark
|
64
|
+
rule = Repeat.new(0, 1, '')
|
65
|
+
assert_equal('?', rule.operator)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_operator_plus
|
69
|
+
rule = Repeat.new(1, Infinity, '')
|
70
|
+
assert_equal('+', rule.operator)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_to_s
|
74
|
+
rule = Repeat.new(1, 2, /a/)
|
75
|
+
assert_equal('/a/1*2', rule.to_s)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_to_s_asterisk
|
79
|
+
rule = Repeat.new(0, Infinity, 'a')
|
80
|
+
assert_equal('"a"*', rule.to_s)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_to_s_question_mark
|
84
|
+
rule = Repeat.new(0, 1, 'a')
|
85
|
+
assert_equal('"a"?', rule.to_s)
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_to_s_plus
|
89
|
+
rule = Repeat.new(1, Infinity, 'a')
|
90
|
+
assert_equal('"a"+', rule.to_s)
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
data/test/rule_test.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class RuleTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
module MatchModule
|
6
|
+
def a_test; end
|
7
|
+
end
|
8
|
+
|
9
|
+
NumericProc = Proc.new {
|
10
|
+
def to_i
|
11
|
+
text.to_i
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_f
|
15
|
+
text.to_f
|
16
|
+
end
|
17
|
+
}
|
18
|
+
|
19
|
+
NumericModule = Module.new(&NumericProc)
|
20
|
+
|
21
|
+
def test_match_module
|
22
|
+
rule = EqualRule.new('a')
|
23
|
+
rule.ext = MatchModule
|
24
|
+
match = rule.match(input('a'))
|
25
|
+
assert(match)
|
26
|
+
assert_kind_of(MatchModule, match)
|
27
|
+
assert_respond_to(match, :a_test)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_numeric_proc
|
31
|
+
rule = EqualRule.new(1)
|
32
|
+
rule.ext = NumericProc
|
33
|
+
match = rule.match(input('1'))
|
34
|
+
assert(match)
|
35
|
+
assert_equal(1, match.to_i)
|
36
|
+
assert_instance_of(Float, match.to_f)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_numeric_module
|
40
|
+
rule = EqualRule.new(1)
|
41
|
+
rule.ext = NumericModule
|
42
|
+
match = rule.match(input('1'))
|
43
|
+
assert(match)
|
44
|
+
assert_kind_of(NumericModule, match)
|
45
|
+
assert_equal(1, match.to_i)
|
46
|
+
assert_instance_of(Float, match.to_f)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class SequenceTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_terminal?
|
6
|
+
rule = Sequence.new
|
7
|
+
assert_equal(false, rule.terminal?)
|
8
|
+
end
|
9
|
+
|
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.text)
|
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.text)
|
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.text)
|
38
|
+
assert_equal(3, match.length)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_to_s
|
42
|
+
rule = Sequence.new(%w<a b>)
|
43
|
+
assert_equal('"a" "b"', rule.to_s)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_to_s_embed
|
47
|
+
rule1 = Sequence.new(%w<a b>)
|
48
|
+
rule2 = Sequence.new(%w<c d>)
|
49
|
+
rule = Sequence.new([rule1, rule2])
|
50
|
+
assert_equal('("a" "b") ("c" "d")', rule.to_s)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/test/super_test.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
Citrus.load(File.dirname(__FILE__) + '/_files/super')
|
3
|
+
|
4
|
+
class SuperTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_terminal?
|
7
|
+
rule = Super.new
|
8
|
+
assert_equal(false, rule.terminal?)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_match
|
12
|
+
grammar1 = Grammar.new {
|
13
|
+
rule :a, 'a'
|
14
|
+
}
|
15
|
+
|
16
|
+
grammar2 = Grammar.new {
|
17
|
+
include grammar1
|
18
|
+
rule :a, any('b', sup)
|
19
|
+
}
|
20
|
+
|
21
|
+
match = grammar2.parse('b')
|
22
|
+
assert(match)
|
23
|
+
assert('b', match.text)
|
24
|
+
assert(1, match.length)
|
25
|
+
|
26
|
+
match = grammar2.parse('a')
|
27
|
+
assert(match)
|
28
|
+
assert('a', match.text)
|
29
|
+
assert(1, match.length)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_peg
|
33
|
+
match = SuperTwo.parse('2')
|
34
|
+
assert(match)
|
35
|
+
|
36
|
+
match = SuperTwo.parse('1')
|
37
|
+
assert(match)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_nested
|
41
|
+
grammar1 = Grammar.new {
|
42
|
+
rule :a, 'a'
|
43
|
+
rule :b, 'b'
|
44
|
+
}
|
45
|
+
|
46
|
+
grammar2 = Grammar.new {
|
47
|
+
include grammar1
|
48
|
+
rule :a, any(sup, :b)
|
49
|
+
rule :b, sup
|
50
|
+
}
|
51
|
+
|
52
|
+
match = grammar2.parse('a')
|
53
|
+
assert(match)
|
54
|
+
assert_equal(:a, match.first.name)
|
55
|
+
|
56
|
+
match = grammar2.parse('b')
|
57
|
+
assert(match)
|
58
|
+
assert_equal(:b, match.first.name)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_to_s
|
62
|
+
rule = Super.new
|
63
|
+
assert_equal('super', rule.to_s)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: citrus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Michael Jackson
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-13 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: builder
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: rake
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :development
|
43
|
+
version_requirements: *id002
|
44
|
+
description: Parsing Expressions for Ruby
|
45
|
+
email: mjijackson@gmail.com
|
46
|
+
executables: []
|
47
|
+
|
48
|
+
extensions: []
|
49
|
+
|
50
|
+
extra_rdoc_files:
|
51
|
+
- README
|
52
|
+
files:
|
53
|
+
- lib/citrus/debug.rb
|
54
|
+
- lib/citrus/peg.rb
|
55
|
+
- lib/citrus/sugar.rb
|
56
|
+
- lib/citrus.rb
|
57
|
+
- examples/calc.citrus
|
58
|
+
- examples/calc.rb
|
59
|
+
- examples/calc_sugar.rb
|
60
|
+
- test/alias_test.rb
|
61
|
+
- test/and_predicate_test.rb
|
62
|
+
- test/calc_peg_test.rb
|
63
|
+
- test/calc_sugar_test.rb
|
64
|
+
- test/calc_test.rb
|
65
|
+
- test/choice_test.rb
|
66
|
+
- test/expression_test.rb
|
67
|
+
- test/fixed_width_test.rb
|
68
|
+
- test/grammar_test.rb
|
69
|
+
- test/helper.rb
|
70
|
+
- test/label_test.rb
|
71
|
+
- test/match_test.rb
|
72
|
+
- test/not_predicate_test.rb
|
73
|
+
- test/peg_test.rb
|
74
|
+
- test/repeat_test.rb
|
75
|
+
- test/rule_test.rb
|
76
|
+
- test/sequence_test.rb
|
77
|
+
- test/super_test.rb
|
78
|
+
- citrus.gemspec
|
79
|
+
- Rakefile
|
80
|
+
- README
|
81
|
+
has_rdoc: true
|
82
|
+
homepage: http://github.com/mjijackson/citrus
|
83
|
+
licenses: []
|
84
|
+
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options:
|
87
|
+
- --line-numbers
|
88
|
+
- --inline-source
|
89
|
+
- --title
|
90
|
+
- Citrus
|
91
|
+
- --main
|
92
|
+
- Citrus
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
requirements: []
|
110
|
+
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 1.3.6
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: Parsing Expressions for Ruby
|
116
|
+
test_files:
|
117
|
+
- test/alias_test.rb
|
118
|
+
- test/and_predicate_test.rb
|
119
|
+
- test/calc_peg_test.rb
|
120
|
+
- test/calc_sugar_test.rb
|
121
|
+
- test/calc_test.rb
|
122
|
+
- test/choice_test.rb
|
123
|
+
- test/expression_test.rb
|
124
|
+
- test/fixed_width_test.rb
|
125
|
+
- test/grammar_test.rb
|
126
|
+
- test/label_test.rb
|
127
|
+
- test/match_test.rb
|
128
|
+
- test/not_predicate_test.rb
|
129
|
+
- test/peg_test.rb
|
130
|
+
- test/repeat_test.rb
|
131
|
+
- test/rule_test.rb
|
132
|
+
- test/sequence_test.rb
|
133
|
+
- test/super_test.rb
|