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 371 def self.register_class(css, klass) @@class_table[css] = klass end
# File kwartz/parser.rb, line 348 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[ \t]*\n/, '') @value.sub!(/^[ \t]+\z/, '') return @value end
scanner
# File kwartz/parser.rb, line 120 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
parser
# File kwartz/parser.rb, line 337 def parse_error(message, linenum=@linenum, column=@column) return ParseError.new(message, @filename, linenum, column) end
scan token
# File kwartz/parser.rb, line 231 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 != false ## 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 ## ',' if c == ?, @value = "," getch() return @token = ','',' end ## @value = c.chr @error = :invalid_char return @token = :error end
# File kwartz/parser.rb, line 293 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 133 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 323 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 178 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 147 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 189 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 or raise ArgumentError.new("#{self.class.name}#reset() requires string argument.") @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