kaiseki 1.0.9 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/kaiseki.rb +3 -1
- data/lib/parseable.rb +8 -0
- data/lib/result_action.rb +23 -0
- data/lib/result_validate.rb +38 -0
- data/lib/rule.rb +19 -0
- metadata +6 -4
data/lib/kaiseki.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Kaiseki
|
2
|
-
VERSION = '1.
|
2
|
+
VERSION = '1.1.1'
|
3
3
|
file_path = File.dirname __FILE__
|
4
4
|
|
5
5
|
#load basic kaiseki classes
|
@@ -41,6 +41,8 @@ module Kaiseki
|
|
41
41
|
require file_path + '/result_merge'
|
42
42
|
require file_path + '/result_cast'
|
43
43
|
require file_path + '/result_filter'
|
44
|
+
require file_path + '/result_action'
|
45
|
+
require file_path + '/result_validate'
|
44
46
|
require file_path + '/node'
|
45
47
|
|
46
48
|
#load grammar classes
|
data/lib/parseable.rb
CHANGED
@@ -74,6 +74,14 @@ module Kaiseki
|
|
74
74
|
FilterResult.new self, node, &block
|
75
75
|
end
|
76
76
|
|
77
|
+
def action node = Node.default, &block
|
78
|
+
ActionResult.new self, node, &block
|
79
|
+
end
|
80
|
+
|
81
|
+
def validate validators
|
82
|
+
ValidateResult.new self, validators
|
83
|
+
end
|
84
|
+
|
77
85
|
def set *vars
|
78
86
|
SetVar.new self, *vars
|
79
87
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Kaiseki
|
2
|
+
class ActionResult < FilterResult
|
3
|
+
def parse! stream, options = {}
|
4
|
+
if @node.is_a? Class
|
5
|
+
node_class = @node
|
6
|
+
else
|
7
|
+
if options[:grammar]
|
8
|
+
node_class = options[:grammar].nodes[@node]
|
9
|
+
else
|
10
|
+
raise "can't use named nodes without a grammar"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
result = @expected.parse stream, options
|
14
|
+
if result.is_a? Array
|
15
|
+
node = node_class.new result, options[:global]
|
16
|
+
else
|
17
|
+
node = node_class.new [result]
|
18
|
+
end
|
19
|
+
node.eval options[:global], &@block
|
20
|
+
result
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Kaiseki
|
2
|
+
class ValidateResult < PackageParser
|
3
|
+
attr_reader :validators
|
4
|
+
|
5
|
+
def initialize expected, validators
|
6
|
+
super expected
|
7
|
+
@validators = validators
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse! stream, options = {}
|
11
|
+
result = @expected.parse stream, options
|
12
|
+
passed = true
|
13
|
+
@validators.each do |key, value|
|
14
|
+
case key
|
15
|
+
when :includes
|
16
|
+
passed = false unless value.find {|n| n === result }
|
17
|
+
when :excludes
|
18
|
+
passed = false if value.find {|n| n === result }
|
19
|
+
when :is
|
20
|
+
passed = false unless value.member? result
|
21
|
+
when :not
|
22
|
+
passed = false if value.member? result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
if passed
|
26
|
+
result
|
27
|
+
else
|
28
|
+
raise ParseError.new "`#{result}' does not pass validation", options
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def eql? other
|
33
|
+
other.is_a?(self.class) and other.expected == @expected and other.validators == @validators
|
34
|
+
end
|
35
|
+
|
36
|
+
alias :== :eql?
|
37
|
+
end
|
38
|
+
end
|
data/lib/rule.rb
CHANGED
@@ -78,6 +78,25 @@ module Kaiseki
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
+
def action node = @name, &block
|
82
|
+
if @parseable
|
83
|
+
unless @grammar.nodes.key? @name
|
84
|
+
@grammar.nodes[@name] = Node.default
|
85
|
+
end
|
86
|
+
@parseable = @parseable.action node, &block
|
87
|
+
else
|
88
|
+
raise "parseable for rule #{@name.inspect} undefined"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def validates validators
|
93
|
+
if @parseable
|
94
|
+
@parseable = @parseable.validate validators
|
95
|
+
else
|
96
|
+
raise "parseable for rule #{@name.inspect} undefined"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
81
100
|
def node args, options = {}
|
82
101
|
args.must_be Array
|
83
102
|
if @grammar.nodes.key? @name
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 1.
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 1.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- William Hamilton-Levi
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-24 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -54,8 +54,10 @@ files:
|
|
54
54
|
- lib/mod_symbol.rb
|
55
55
|
- lib/parser_custom.rb
|
56
56
|
- lib/parser_eof.rb
|
57
|
+
- lib/result_validate.rb
|
57
58
|
- lib/parse_result.rb
|
58
59
|
- lib/parser_symbol.rb
|
60
|
+
- lib/result_action.rb
|
59
61
|
- lib/mod_proc.rb
|
60
62
|
- lib/grammar.rb
|
61
63
|
- lib/parser_basic.rb
|