kaiseki 1.0.6 → 1.0.7
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/lib/kaiseki.rb +2 -1
- data/lib/parse_error.rb +1 -1
- data/lib/parseable.rb +4 -0
- data/lib/parser_custom.rb +26 -0
- data/lib/parser_package.rb +4 -0
- data/lib/parser_repeat.rb +1 -0
- data/lib/predicate_and.rb +4 -0
- data/lib/predicate_not.rb +4 -0
- data/lib/predicate_skip.rb +4 -0
- data/lib/rule.rb +8 -0
- data/lib/stream.rb +24 -16
- metadata +4 -3
data/lib/kaiseki.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Kaiseki
|
2
|
-
VERSION = '1.0.
|
2
|
+
VERSION = '1.0.7'
|
3
3
|
file_path = File.dirname __FILE__
|
4
4
|
|
5
5
|
#load basic kaiseki classes
|
@@ -30,6 +30,7 @@ module Kaiseki
|
|
30
30
|
require file_path + '/parser_repeat'
|
31
31
|
|
32
32
|
require file_path + '/parser_package'
|
33
|
+
require file_path + '/parser_custom'
|
33
34
|
|
34
35
|
#load predicates
|
35
36
|
require file_path + '/predicate_and'
|
data/lib/parse_error.rb
CHANGED
data/lib/parseable.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Kaiseki
|
2
|
+
attr_reader :name
|
3
|
+
|
4
|
+
class CustomParser < BasicParser
|
5
|
+
def initialize name = nil, is_predicate = false, &block
|
6
|
+
super block
|
7
|
+
@name = name
|
8
|
+
@is_predicate = is_predicate
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse! stream, options = {}
|
12
|
+
stream.must_be Stream
|
13
|
+
stream.lock do
|
14
|
+
@expected.call stream, options
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def predicate?
|
19
|
+
@is_predicate
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
@name || @expected.inspect
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/parser_package.rb
CHANGED
data/lib/parser_repeat.rb
CHANGED
data/lib/predicate_and.rb
CHANGED
data/lib/predicate_not.rb
CHANGED
data/lib/predicate_skip.rb
CHANGED
data/lib/rule.rb
CHANGED
@@ -18,6 +18,14 @@ module Kaiseki
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
def custom &block
|
22
|
+
if @parseable
|
23
|
+
raise "parseable for rule #{@name.inspect} already defined"
|
24
|
+
else
|
25
|
+
@parseable = CustomParser.new &block
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
21
29
|
def override options
|
22
30
|
if @parseable
|
23
31
|
@parseable = @parseable.override options
|
data/lib/stream.rb
CHANGED
@@ -4,13 +4,13 @@ module Kaiseki
|
|
4
4
|
|
5
5
|
def initialize string
|
6
6
|
if string.is_a? File
|
7
|
-
@string =
|
8
|
-
string.each_char {|n| @string << n }
|
7
|
+
@string = string.to_a.join
|
9
8
|
else
|
10
9
|
@string = string.to_s
|
11
10
|
end
|
12
11
|
@pos = 0
|
13
|
-
|
12
|
+
@newlines = []
|
13
|
+
index_lines
|
14
14
|
end
|
15
15
|
|
16
16
|
def getc
|
@@ -32,14 +32,6 @@ module Kaiseki
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
def look len = 10
|
36
|
-
if @string.length - @pos > len
|
37
|
-
"\"#{@string[@pos, len - 3] + '...'}\""
|
38
|
-
else
|
39
|
-
"\"#{@string[@pos..-1]}\""
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
35
|
def goto pos
|
44
36
|
if pos < 0
|
45
37
|
@pos = 0
|
@@ -79,20 +71,36 @@ module Kaiseki
|
|
79
71
|
alias :size :length
|
80
72
|
|
81
73
|
def line
|
82
|
-
|
74
|
+
bsearch @pos, 0, @newlines.length
|
83
75
|
end
|
84
76
|
|
85
77
|
def column
|
86
|
-
|
78
|
+
@pos - @newlines[self.line][0]
|
87
79
|
end
|
88
80
|
|
89
81
|
private
|
90
82
|
def index_lines
|
91
|
-
|
83
|
+
@newlines = []
|
84
|
+
start = 0
|
85
|
+
@string.length.times do |i|
|
86
|
+
if @string[i] == "\n"
|
87
|
+
@newlines << [start, i]
|
88
|
+
start = i + 1
|
89
|
+
end
|
90
|
+
end
|
91
|
+
@newlines << [start, @string.length]
|
92
92
|
end
|
93
93
|
|
94
|
-
def bsearch
|
95
|
-
|
94
|
+
def bsearch value, low, high
|
95
|
+
return nil if high < low
|
96
|
+
mid = low + (high - low) / 2
|
97
|
+
if value < @newlines[mid][0]
|
98
|
+
bsearch value, low, mid - 1
|
99
|
+
elsif value > @newlines[mid][1]
|
100
|
+
bsearch value, mid + 1, high
|
101
|
+
else
|
102
|
+
mid
|
103
|
+
end
|
96
104
|
end
|
97
105
|
end
|
98
106
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 1.0.
|
8
|
+
- 7
|
9
|
+
version: 1.0.7
|
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:
|
17
|
+
date: 2011-01-08 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- lib/grammar_stub.rb
|
53
53
|
- lib/parser_sequence.rb
|
54
54
|
- lib/mod_symbol.rb
|
55
|
+
- lib/parser_custom.rb
|
55
56
|
- lib/parser_eof.rb
|
56
57
|
- lib/parse_result.rb
|
57
58
|
- lib/parser_symbol.rb
|