Class: Scripref::Parser
- Inherits:
-
StringScanner
- Object
- StringScanner
- Scripref::Parser
- Defined in:
- lib/scripref/parser.rb
Constant Summary collapse
- NUMBER_RE =
/\d+\s*/
Instance Method Summary collapse
-
#b1 ⇒ Object
try to parse first book.
-
#b2 ⇒ Object
try to parse second book.
- #book2num(str) ⇒ Object
-
#c1 ⇒ Object
try parse first chapter.
-
#c2 ⇒ Object
try to parse second chapter.
-
#cv_sep ⇒ Object
try to parse separator or chapter and verse.
-
#epsilon ⇒ Object
try to parse
end of string. -
#hyphen ⇒ Object
try to parse hyphen.
-
#initialize(*mods) ⇒ Parser
constructor
A new instance of Parser.
- #inspect ⇒ Object
-
#parse(str) ⇒ Object
Parsing a string of a scripture reference.
-
#pass_sep ⇒ Object
try to parse separator between passages.
- #push_passage ⇒ Object
-
#start ⇒ Object
start of parsing grammer.
-
#v1 ⇒ Object
try to parse first verse.
-
#v2 ⇒ Object
try to parse second verse.
-
#verse_addon ⇒ Object
try to parse addons for verses.
-
#verse_sep ⇒ Object
try to parse verse separator.
Constructor Details
#initialize(*mods) ⇒ Parser
Returns a new instance of Parser.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/scripref/parser.rb', line 11 def initialize *mods @mods = mods mods.each do |m| m.class_eval do extend ConstReader const_reader constants end extend m end end |
Instance Method Details
#b1 ⇒ Object
try to parse first book
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/scripref/parser.rb', line 37 def b1 s = scan(book_re) or return nil @text << s @b1 = @b2 = book2num(s) if check(Regexp.new(NUMBER_RE.source + cv_sep_re.source)) @c1 = @v1 = nil @c2 = @v2 = nil c1 else if Scripref.book_has_only_one_chapter?(@b1) @c1 = @c2 = 1 epsilon or (hyphen and b2) or v1 or nil else @c1 = @v1 = nil @c2 = @v2 = nil epsilon or (hyphen and b2) or c1 or nil end end end |
#b2 ⇒ Object
try to parse second book
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/scripref/parser.rb', line 107 def b2 s = scan(book_re) or return nil @text << s @b2 = book2num(s) @c2 = @v2 = nil if check(Regexp.new(NUMBER_RE.source + cv_sep_re.source)) c2 else if Scripref.book_has_only_one_chapter?(@b2) @c2 = 1 epsilon or v2 or nil else epsilon or c2 or nil end end end |
#book2num(str) ⇒ Object
227 228 229 230 231 232 233 234 235 |
# File 'lib/scripref/parser.rb', line 227 def book2num str return nil unless book_re =~str books_res.each_with_index do |re, i| if str =~ Regexp.new('^' << re.to_s << '$') num = i + 1 return num end end end |
#c1 ⇒ Object
try parse first chapter
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/scripref/parser.rb', line 59 def c1 s = scan(NUMBER_RE) or return nil @text << s @c1 = @c2 = s.to_i if cv_sep v1 or nil elsif hyphen b2 or c2 or nil elsif pass_sep b1 or c1 else epsilon or nil end end |
#c2 ⇒ Object
try to parse second chapter
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/scripref/parser.rb', line 126 def c2 s = scan(NUMBER_RE) or return nil @text << s @c2 = s.to_i if cv_sep v2 or nil else epsilon or nil end end |
#cv_sep ⇒ Object
try to parse separator or chapter and verse
170 171 172 173 174 175 176 177 |
# File 'lib/scripref/parser.rb', line 170 def cv_sep if s = scan(cv_sep_re) @text << s s else nil end end |
#epsilon ⇒ Object
try to parse end of string
161 162 163 164 165 166 167 |
# File 'lib/scripref/parser.rb', line 161 def epsilon if eos? push_passage return @result end nil end |
#hyphen ⇒ Object
try to parse hyphen
180 181 182 183 184 185 186 187 |
# File 'lib/scripref/parser.rb', line 180 def hyphen if s = scan(hyphen_re) @text << s s else nil end end |
#inspect ⇒ Object
237 238 239 |
# File 'lib/scripref/parser.rb', line 237 def inspect "#<#{self.class} #{@mods.inspect}>" end |
#parse(str) ⇒ Object
Parsing a string of a scripture reference
24 25 26 27 28 |
# File 'lib/scripref/parser.rb', line 24 def parse str self.string = str @result = [] start end |
#pass_sep ⇒ Object
try to parse separator between passages
190 191 192 193 194 195 196 197 198 |
# File 'lib/scripref/parser.rb', line 190 def pass_sep if s = scan(pass_sep_re) push_passage @result << s s else nil end end |
#push_passage ⇒ Object
221 222 223 224 225 |
# File 'lib/scripref/parser.rb', line 221 def push_passage @result << Passage.new(@text, @b1, @c1, @v1, @b2, @c2, @v2, a1: @a1, a2: @a2) @text = '' @a1 = @a2 = nil end |
#start ⇒ Object
start of parsing grammer
31 32 33 34 |
# File 'lib/scripref/parser.rb', line 31 def start @text = '' b1 or [] end |
#v1 ⇒ Object
try to parse first verse
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/scripref/parser.rb', line 76 def v1 s = scan(NUMBER_RE) or return nil @text << s @v1 = @v2 = s.to_i if addon = verse_addon case addon when :f, :ff @v2 = addon when :a, :b, :c @a1 = addon end end if hyphen b2 or ( if check(Regexp.new(NUMBER_RE.source + cv_sep_re.source)) c2 else v2 or nil end) elsif pass_sep b1 or c1 elsif verse_sep v1 else epsilon or nil end end |
#v2 ⇒ Object
try to parse second verse
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/scripref/parser.rb', line 139 def v2 s = scan(NUMBER_RE) or return nil @text << s @v2 = s.to_i if addon = verse_addon case addon when :a, :b, :c @a2 = addon end end if verse_sep v1 elsif pass_sep b1 or c1 else epsilon or nil end end |
#verse_addon ⇒ Object
try to parse addons for verses
212 213 214 215 216 217 218 219 |
# File 'lib/scripref/parser.rb', line 212 def verse_addon if s = scan(verse_addon_re) @text << s s.to_sym else nil end end |
#verse_sep ⇒ Object
try to parse verse separator
201 202 203 204 205 206 207 208 209 |
# File 'lib/scripref/parser.rb', line 201 def verse_sep if s = scan(verse_sep_re) push_passage @result << s s else nil end end |