Class: PHPRB::Compiler::TrimScanner

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

Constant Summary collapse

TrimSplitRegexp =
/(<[?%][?%])|([?%][?%]>)|(<[?%]=)|(<[?%]#)|(<[?%])|([?%]>\n)|([?%]>)|(\n)/
ExplicitTrimRegexp =
/(^[ \t]*<[?%]-)|(-[?%]>\n?\z)|(<[?%]-)|(-[?%]>)|(<[?%][?%])|([?%][?%]>)|(<[?%]=)|(<[?%]#)|(<[?%])|([?%]>)|(\n)/
PHPRB_STAG =
%w(<?= <?# <? <%= <%# <%)

Constants inherited from Scanner

Scanner::SplitRegexp

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Scanner

default_scanner=, make_scanner, regist_scanner

Constructor Details

#initialize(src, trim_mode, percent) ⇒ TrimScanner

Returns a new instance of TrimScanner.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/web/phprb.rb', line 56

def initialize(src, trim_mode, percent)
	super
	@trim_mode = trim_mode
	@percent = percent
	if @trim_mode == '>'
	  @scan_line = self.method(:trim_line1)
	elsif @trim_mode == '<>'
	  @scan_line = self.method(:trim_line2)
	elsif @trim_mode == '-'
	  @scan_line = self.method(:explicit_trim_line)
	else
	  @scan_line = self.method(:scan_line)
	end
end

Instance Attribute Details

#stagObject

Returns the value of attribute stag.



70
71
72
# File 'lib/web/phprb.rb', line 70

def stag
  @stag
end

Instance Method Details

#explicit_trim_line(line) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/web/phprb.rb', line 138

def explicit_trim_line(line)
	line.split(ExplicitTrimRegexp).each do |token|
	  next if token.empty?
	  if @stag.nil? && /[ \t]*<[?%]-/ =~ token
	    yield('<' + $1)
	  elsif @stag && /-[?%]>\n/ =~ token
	    yield($1 + '>')
	    yield(:cr)
	  elsif @stag && token =~ /^-[?%]>$/
	    yield($1 + '>')
	  else
	    yield(token)
	  end
	end
end

#is_erb_stag?(s) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/web/phprb.rb', line 155

def is_erb_stag?(s)
	PHPRB_STAG.member?(s)
end

#percent_line(line, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/web/phprb.rb', line 86

def percent_line(line, &block)
	if @stag || line[0] != ?%
	  return @scan_line.call(line, &block)
	end

	line[0] = ''
	if line[0] == ?%
	  @scan_line.call(line, &block)
	else
    yield(PercentLine.new(line.chomp))
	end
end

#scan(&block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/web/phprb.rb', line 72

def scan(&block)
	@stag = nil
	if @percent
	  @src.each do |line|
	    percent_line(line, &block)
	  end
	else
	  @src.each do |line|
	    @scan_line.call(line, &block)
	  end
	end
	nil
end

#scan_line(line) ⇒ Object



99
100
101
102
103
104
# File 'lib/web/phprb.rb', line 99

def scan_line(line)
	line.split(SplitRegexp).each do |token|
	  next if token.empty?
	  yield(token)
	end
end

#trim_line1(line) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/web/phprb.rb', line 106

def trim_line1(line)
	line.split(TrimSplitRegexp).each do |token|
	  next if token.empty?
	  if token =~ /^([?%])>\n$/
	    yield($1 + '>')
	    yield(:cr)
	    break
	  end
	  yield(token)
	end
end

#trim_line2(line) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/web/phprb.rb', line 118

def trim_line2(line)
	head = nil
	line.split(TrimSplitRegexp).each do |token|
	  next if token.empty?
	  head = token unless head
	  if token == /^([?$])>\n$/
	    yield($1 + '>')
	    if  is_erb_stag?(head)
 yield(:cr)
	    else
 yield("\n")
	    end
	    break
	  end

	  yield(token)
	end
end