Class: Kwartz::PresentationLogicParser

Inherits:
Object
  • Object
show all
Includes:
Assertion, CharacterType
Defined in:
lib/kwartz/parser.rb

Overview

.[abstract] parser class for presentation logic

Direct Known Subclasses

CssStyleParser, RubyStyleParser

Constant Summary collapse

PLOGIC_KEYWORDS =
table
ESCAPE_FLAG_TABLE =
table
@@class_table =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertion

assert

Methods included from CharacterType

#is_alpha, #is_digit, #is_identchar, #is_whitespace

Constructor Details

#initialize(properties = {}) ⇒ PresentationLogicParser

Returns a new instance of PresentationLogicParser.



65
66
67
# File 'lib/kwartz/parser.rb', line 65

def initialize(properties={})
  @properties = properties
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



88
89
90
# File 'lib/kwartz/parser.rb', line 88

def column
  @column
end

#errorObject (readonly)

Returns the value of attribute error.



88
89
90
# File 'lib/kwartz/parser.rb', line 88

def error
  @error
end

#linenumObject (readonly)

Returns the value of attribute linenum.



88
89
90
# File 'lib/kwartz/parser.rb', line 88

def linenum
  @linenum
end

#posObject (readonly)

Returns the value of attribute pos.



88
89
90
# File 'lib/kwartz/parser.rb', line 88

def pos
  @pos
end

#tokenObject (readonly)

Returns the value of attribute token.



88
89
90
# File 'lib/kwartz/parser.rb', line 88

def token
  @token
end

#valueObject (readonly)

Returns the value of attribute value.



88
89
90
# File 'lib/kwartz/parser.rb', line 88

def value
  @value
end

Class Method Details

.get_class(css) ⇒ Object



376
377
378
# File 'lib/kwartz/parser.rb', line 376

def self.get_class(css)
  return @@class_table[css]
end

.register_class(css, klass) ⇒ Object



371
372
373
# File 'lib/kwartz/parser.rb', line 371

def self.register_class(css, klass)
  @@class_table[css] = klass
end

Instance Method Details

#_parse_blockObject



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/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

#escape?(value) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/kwartz/parser.rb', line 113

def escape?(value)
  return ESCAPE_FLAG_TABLE[value]
end

#getchObject

scanner



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/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

#keywords(keyword) ⇒ Object

.[abstract] detect parser-specific keywords

return symbol if keyword is token, else return nil



225
226
227
# File 'lib/kwartz/parser.rb', line 225

def keywords(keyword)
  not_implemented
end

#parse(input, filename = '') ⇒ Object

.[abstract] parse input string and return list of ElementRuleset



343
344
345
# File 'lib/kwartz/parser.rb', line 343

def parse(input, filename='')
  not_implemented
end

#parse_error(message, linenum = @linenum, column = @column) ⇒ Object



337
338
339
# File 'lib/kwartz/parser.rb', line 337

def parse_error(message, linenum=@linenum, column=@column)
  return ParseError.new(message, @filename, linenum, column)
end

#scanObject

scan token



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
286
287
288
289
290
# File 'lib/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

#scan_block(skip_open_curly = false) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/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

#scan_hookObject

called from scan(), return false when not hooked



217
218
219
# File 'lib/kwartz/parser.rb', line 217

def scan_hook
  #not_implemented
end

#scan_identObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/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

#scan_lineObject



323
324
325
326
327
328
329
330
331
# File 'lib/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

#scan_stringObject



178
179
180
181
182
183
184
185
186
# File 'lib/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

#scan_string_dquotedObject



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
# File 'lib/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

#scan_string_quotedObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/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