Class: PHPRB::Compiler

Inherits:
Object show all
Defined in:
lib/web/phprb.rb

Defined Under Namespace

Classes: Buffer, PercentLine, Scanner, SimpleScanner, TrimScanner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trim_mode) ⇒ Compiler

Returns a new instance of Compiler.



380
381
382
383
384
385
# File 'lib/web/phprb.rb', line 380

def initialize(trim_mode)
  @percent, @trim_mode = prepare_trim_mode(trim_mode)
  @put_cmd = 'print'
  @pre_cmd = []
  @post_cmd = []
end

Instance Attribute Details

#percentObject (readonly)

Returns the value of attribute percent.



386
387
388
# File 'lib/web/phprb.rb', line 386

def percent
  @percent
end

#post_cmdObject

Returns the value of attribute post_cmd.



387
388
389
# File 'lib/web/phprb.rb', line 387

def post_cmd
  @post_cmd
end

#pre_cmdObject

Returns the value of attribute pre_cmd.



387
388
389
# File 'lib/web/phprb.rb', line 387

def pre_cmd
  @pre_cmd
end

#put_cmdObject

Returns the value of attribute put_cmd.



387
388
389
# File 'lib/web/phprb.rb', line 387

def put_cmd
  @put_cmd
end

#trim_modeObject (readonly)

Returns the value of attribute trim_mode.



386
387
388
# File 'lib/web/phprb.rb', line 386

def trim_mode
  @trim_mode
end

Instance Method Details

#compile(s) ⇒ Object



288
289
290
291
292
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/web/phprb.rb', line 288

def compile(s)
  out = Buffer.new(self)

  content = ''
  scanner = make_scanner(s)
  scanner.scan do |token|
	if scanner.stag.nil?
	  case token
      when PercentLine
 out.push("#{@put_cmd} #{content.dump}") if content.size > 0
 content = ''
        out.push(token.to_s)
        out.cr
	  when :cr
 out.cr
	  when '<?', '<?=', '<?#', '<%', '<%=', '<%#'
 scanner.stag = token
 out.push("#{@put_cmd} #{content.dump}") if content.size > 0
 content = ''
	  when "\n"
 content << "\n"
 out.push("#{@put_cmd} #{content.dump}")
 out.cr
 content = ''
	  when '<%%'
 content << '<%'
	  when '<??'
 content << '<?'
	  else
 content << token
	  end
	else
	  case token
	  when '?>', '%>'
 case scanner.stag
 when '<?', '<%'
   if content[-1] == ?\n
		content.chop!
		out.push(content)
		out.cr
   else
		out.push(content)
   end
 when '<?=', '<%='
   out.push("#{@put_cmd}((#{content}).to_s)")
 when '<?#', '<%#'
   # out.push("# #{content.dump}")
 end
 scanner.stag = nil
 content = ''
	  when '%%>'
 content << '%>'
	  when '??>'
 content << '?>'
	  else
 content << token
	  end
	end
  end
  out.push("#{@put_cmd} #{content.dump}") if content.size > 0
  out.close
  out.script
end

#make_scanner(src) ⇒ Object



376
377
378
# File 'lib/web/phprb.rb', line 376

def make_scanner(src)
  Scanner.make_scanner(src, @trim_mode, @percent)
end

#prepare_trim_mode(mode) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/web/phprb.rb', line 352

def prepare_trim_mode(mode)
  case mode
  when 1
	return [false, '>']
  when 2
	return [false, '<>']
  when 0
	return [false, nil]
  when String
	perc = mode.include?('?')
	if mode.include?('-')
	  return [perc, '-']
	elsif mode.include?('<>')
	  return [perc, '<>']
	elsif mode.include?('>')
	  return [perc, '>']
	else
	  [perc, nil]
	end
  else
	return [false, nil]
  end
end