Class: Kwaff::Parser
- Inherits:
-
Object
- Object
- Kwaff::Parser
- Defined in:
- lib/kwaff/parser.rb
Overview
parse Kwaff string and create Kwaff::Document.
ex.
kwaff_str = File.open('file.kwaff') { |f| f.read }
parser = Kwaff::Parser.new(kwaff_str)
kwaff_doc = parser.parse_document()
translator = Kwaff::XmlTranslator.new()
xml_str = translator.translate(kwaff_doc)
if you want to get REXML::Document instead of Kwaff::Document, use Kwaff::Rexml::Parser instead of Kwaff::Parser.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#newline ⇒ Object
readonly
Returns the value of attribute newline.
Instance Method Summary collapse
-
#create_comment(str) ⇒ Object
factory method.
-
#create_document(headers, root) ⇒ Object
factory method.
-
#create_element(tag, children, attrs) ⇒ Object
factory method.
-
#create_text(str, flag_escape = true) ⇒ Object
factory method.
-
#initialize(str, toppings = {}) ⇒ Parser
constructor
A new instance of Parser.
- #parse_cdata(level = 0) ⇒ Object
- #parse_comment(level = 0) ⇒ Object
- #parse_document(str = nil) ⇒ Object
- #parse_element(level = 0) ⇒ Object
- #parse_node_list(level = 0) ⇒ Object
- #parse_text(level = 0) ⇒ Object
- #reset(str) ⇒ Object
- #set_node_option(node, name, value) ⇒ Object
-
#spclen(str) ⇒ Object
space length, with tab expanding.
Constructor Details
#initialize(str, toppings = {}) ⇒ Parser
Returns a new instance of Parser.
42 43 44 45 46 47 |
# File 'lib/kwaff/parser.rb', line 42 def initialize(str, toppings={}) reset(str) @toppings = toppings @_s_cache = {} # for spclen() @indent_width = toppings[:indent_width] || 2 end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
48 49 50 |
# File 'lib/kwaff/parser.rb', line 48 def index @index end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
48 49 50 |
# File 'lib/kwaff/parser.rb', line 48 def lines @lines end |
#newline ⇒ Object (readonly)
Returns the value of attribute newline.
48 49 50 |
# File 'lib/kwaff/parser.rb', line 48 def newline @newline end |
Instance Method Details
#create_comment(str) ⇒ Object
factory method
94 95 96 |
# File 'lib/kwaff/parser.rb', line 94 def create_comment(str) return Comment.new(str) end |
#create_document(headers, root) ⇒ Object
factory method
84 85 86 |
# File 'lib/kwaff/parser.rb', line 84 def create_document(headers, root) return Document.new(headers, root) end |
#create_element(tag, children, attrs) ⇒ Object
factory method
89 90 91 |
# File 'lib/kwaff/parser.rb', line 89 def create_element(tag, children, attrs) return Element.new(tag, children, attrs) end |
#create_text(str, flag_escape = true) ⇒ Object
factory method
99 100 101 |
# File 'lib/kwaff/parser.rb', line 99 def create_text(str, flag_escape=true) return Text.new(str, flag_escape) end |
#parse_cdata(level = 0) ⇒ Object
303 304 305 |
# File 'lib/kwaff/parser.rb', line 303 def parse_cdata(level=0) return _parse_textline(/^([ \t]*)\~ ?(.*)$/, false, level) # *pattern* end |
#parse_comment(level = 0) ⇒ Object
288 289 290 291 292 293 294 295 296 |
# File 'lib/kwaff/parser.rb', line 288 def parse_comment(level=0) line = _current_line() line =~ /^([ \t]*)[\#\!][ \t]*(.*)$/ # *pattern* Kwaff::assert() unless level == spclen($1) str = $2 comment = create_comment(str) line = _read_line() return comment end |
#parse_document(str = nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/kwaff/parser.rb', line 109 def parse_document(str=nil) reset(str) if str headers = OrderedHash.new line = _current_line() while line if line =~ /^\?[ \t]*([-:\w]+)[ \t]*[:=][ \t]*(.*)/ ## *pattern* headers[$1.intern] = $2.strip elsif line =~ /^\s*$/ # ignore empty line elsif line =~ /^\s*\/\// ## *pattern* # ignore comment line else break end line = _read_line() end if headers[:doctype] =~ /\A@[-:\w]+\z/ nickname = headers[:doctype] doctype = DocType.get(nickname) unless doctype raise ParseError.new("doctype `#{headers[:doctype]}' not registered.", self) end headers[:doctype] = doctype end root_elem = parse_element() doc = create_document(headers, root_elem) set_node_option(doc, :empty_lines, @empty_lines) return doc end |
#parse_element(level = 0) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/kwaff/parser.rb', line 211 def parse_element(level=0) ## tag and text line = _current_line() line =~ /^([ \t]*)\*[ \t]*([-:\w]+)([ \t]*=[ \t]*(.*))?/ ## *pattern* Kwaff::assert() unless level == spclen($1) tag = $2 || '' value = $4 tag.strip! if tag.empty? raise ParseError.new("tag is missing.", self) end flag_escape = true if value =~ /^<<<?('?\w+'?)$/ terminator = $1 if terminator =~ /\A'(.*)'\z/ terminator = $1 flag_escape = false end value = _parse_heredoc(terminator) end ## text text = value ? create_text(value, flag_escape) : nil ## attrs attrs = OrderedHash.new line = _read_line() while line if line =~ /^\s*\/\/$/ # line comment ## *pattern* # nothing #elsif line =~ /^\s*$/ # empty line # @empty_lines += 1 elsif line =~ /^\s*\-/ # attribute ## *pattern* unless line =~ /\A([ \t]*)\-[ \t]+([-:\w]+)[ \t]*=[ \t]*(.*)/ ## *pattern* raise ParseError.new("invalid attribute format.", self) end attr_level = spclen($1) attr_name = $2 attr_value = $3 unless attr_level == level + @indent_width ## *indent* raise ParseError.new("attr indent width is not #{@indent_width}.", self) end if attr_value =~ /^<<([_A-Z]+)$/ terminator = $1 attr_value = _parse_heredoc(terminator) end attrs[attr_name] = attr_value else break end line = _read_line() end ## empty lines @empty_lines = 0 while line && line =~ /\A\s*\z/ @empty_lines += 1 line = _read_line() end ## children if line && line =~ /^(\s*)[*\#!.\~]/ && spclen($1) > level ## *pattern* #children = parse_node_list(spclen($1)) unless spclen($1) == level + @indent_width ## *indent* raise ParseError.new("indent width is not #{@indent_width}.", self) end children = parse_node_list(level+@indent_width) else children = [] end children.unshift(text) if text ## element return create_element(tag, children, attrs) end |
#parse_node_list(level = 0) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/kwaff/parser.rb', line 140 def parse_node_list(level=0) list = [] str = nil line = _current_line() while line if line =~ /^(\s*)([*\#!.\~])/ # node ## *pattern* curr_level = spclen($1) ch = $2 if str list << create_text(str) str = nil end if curr_level == level # sibling node empty_lines = @empty_lines || 0 @empty_lines = 0 case ch when '*' ## *pattern* node = parse_element(level) when '#', '!' ## *pattern* node = parse_comment(level) when '.' ## *pattern* node = parse_text(level) when '~' ## *pattern* node = parse_cdata(level) else raise ParseError.new("internal error", self) end set_node_option(node, :empty_lines, empty_lines) list << node line = _current_line() next elsif curr_level < level # parent node break else # child node raise ParseError.new("illegal indentation.", self) # or internal error end elsif line =~ /^\s*$/ # empty lines @empty_lines ||= 0 @empty_lines += 1 line = _read_line() elsif line =~ /^\s*\/\// # line-comment # *pattern* line = _read_line() else #raise ParseError.new("invalid line.", self) str ||= '' str << line line = _read_line() end end list << create_text(str) if str return list end |
#parse_text(level = 0) ⇒ Object
299 300 301 |
# File 'lib/kwaff/parser.rb', line 299 def parse_text(level=0) return _parse_textline(/^([ \t]*)\. ?(.*)$/, true, level) # *pattern* end |
#reset(str) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/kwaff/parser.rb', line 51 def reset(str) @str = str idx = str.index(?\n) @newline = (idx && str[idx-1] == ?\r) ? "\r\n" : "\n" @lines = str.split(@newline, -1) @lines.pop() # remove the last line @index = 0 @limit = @lines.length end |
#set_node_option(node, name, value) ⇒ Object
104 105 106 |
# File 'lib/kwaff/parser.rb', line 104 def set_node_option(node, name, value) node.empty_lines = value if name == :empty_lines end |
#spclen(str) ⇒ Object
space length, with tab expanding
76 77 78 79 80 |
# File 'lib/kwaff/parser.rb', line 76 def spclen(str) return 0 unless str len = (@_s_cache[str] ||= (str.gsub(/([^\t]{8})|([^\t]*)\t/n){[$+].pack("A8")}).length) return len end |