Class | Kwartz::PresentationLogicParser |
In: |
kwartz/parser.rb
|
Parent: | Object |
.[abstract] parser class for presentation logic
PLOGIC_KEYWORDS | = | table |
ESCAPE_FLAG_TABLE | = | table |
column | [R] | |
error | [R] | |
linenum | [R] | |
pos | [R] | |
token | [R] | |
value | [R] |
# File kwartz/parser.rb, line 361 def self.register_class(css, klass) @@class_table[css] = klass end
# File kwartz/parser.rb, line 338 def _parse_block scan() unless @token == '{''{' raise parse_error("'#{@value}': '{' expected.") end start_linenum, start_column = @linenum, @column t = scan_block(true) if t == :error if @error == :block_unclosed raise parse_error("'{': not closed by '}'.", start_linenum, start_column) else assert("@error=#{@error}") end end @value.sub!(/\A\s*\n/, '') @value.sub!(/^\s+\z/, '') return @value end
scanner
# File kwartz/parser.rb, line 119 def getch return @ch = nil if @pos >= @max_pos if @ch == ?\n @linenum += 1 @column = 0 end @pos += 1 @column += 1 @ch = @input[@pos] return @ch end
.[abstract] detect parser-specific keywords
return symbol if keyword is token, else return nil
# File kwartz/parser.rb, line 223 def keywords(keyword) not_implemented end
.[abstract] parse input string and return list of ElementRuleset
# File kwartz/parser.rb, line 333 def parse(input, filename='') not_implemented end
parser
# File kwartz/parser.rb, line 327 def parse_error(message, linenum=@linenum, column=@column) return ParseError.new(message, @filename, linenum, column) end
scan token
# File kwartz/parser.rb, line 228 def scan ## skip whitespaces c = @ch while is_whitespace(c) c = getch() end ## return nil when EOF if c == nil @value = nil return @token = nil end ## scan hook ret = scan_hook() # scan_hook() is overrided in subclass return ret if ret ## keyword or identifer if is_identchar(c) scan_ident() @token = keywords(@value) || PLOGIC_KEYWORDS[@value] || :ident return @token end ## "string" if c == ?" return scan_string_dquoted() end ## 'string' if c == ?' return scan_string_quoted() end ## '{' if c == ?{ @value = "{" getch() return @token = '{''{' end ## '}' if c == ?} @value = "}" getch() return @token = '}''}' end ## @value = c.chr @error = :invalid_char return @token = :error end
# File kwartz/parser.rb, line 283 def scan_block(skip_open_curly=false) unless skip_open_curly token = scan() unless token == ?{ @error = :block_notfound return @token = :error end end start_pos = @pos count = 1 while (c = getch()) != nil if c == ?{ count += 1 elsif c == ?} count -= 1 break if count == 0 end end unless c @error = :block_unclosed return @token = :error end assert unless c == ?} @value = @input[start_pos, @pos - start_pos] @token = :block getch() return @value end
# File kwartz/parser.rb, line 132 def scan_ident ## identifer if is_identchar(@ch) sb = @ch.chr while (c = getch()) && is_identchar(c) sb << c.chr end @value = sb return @token = :ident end return nil end
# File kwartz/parser.rb, line 313 def scan_line sb = @ch.chr while (c = getch()) != nil && c != ?\n sb << c.chr end sb.chop if sb[-1] == ?\r getch() return sb end
# File kwartz/parser.rb, line 177 def scan_string if @ch == ?' return scan_string_quoted() elsif @ch == ?" return scan_string_dquoted() else return nil end end
# File kwartz/parser.rb, line 146 def scan_string_dquoted return nil unless @ch == ?" s = '' while (c = getch()) && c != ?" if c == ?\\ c = getch() break unless c case c when ?n ; s << "\n" when ?t ; s << "\t" when ?r ; s << "\r" when ?b ; s << "\b" when ?\\ ; s << "\\" when ?" ; s << '"' else ; s << c.chr end else s << c.chr end end unless c @error = :string_unclosed return @token = :error end assert unless c == ?" getch() @value = s return @token = :string end
# File kwartz/parser.rb, line 188 def scan_string_quoted return nil unless @ch == ?' s = '' while (c = getch()) && c != ?' if c == ?\\ c = getch() break unless c case c when ?\\ ; s << "\\" when ?' ; s << "'" else ; s << "\\" << c.chr end else s << c.chr end end unless c @error = :string_unclosed return @token = :error end assert unless c == ?' getch() @value = s return @token = :string end
called from parse() and initialize parser object
# File kwartz/parser.rb, line 71 def reset(input, filename='') @input = input @filename = filename @linenum = 1 # 1 start @column = 0 # 1 start @pos = -1 # 0 start @max_pos = @input.length - 1 @token = nil @value = nil @error = nil @ch = nil getch() end