| 1 |
require 'strscan' |
| 2 |
|
| 3 |
class Mustache |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class Parser |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class SyntaxError < StandardError |
| 30 |
def initialize(message, position) |
| 31 |
@message = message |
| 32 |
@lineno, @column, @line, _ = position |
| 33 |
@stripped_line = @line.strip |
| 34 |
@stripped_column = @column - (@line.size - @line.lstrip.size) |
| 35 |
end |
| 36 |
|
| 37 |
def to_s |
| 38 |
<<-EOF |
| 39 |
#{@message} |
| 40 |
Line #{@lineno} |
| 41 |
#{@stripped_line} |
| 42 |
#{' ' * @stripped_column}^ |
| 43 |
EOF |
| 44 |
end |
| 45 |
end |
| 46 |
|
| 47 |
|
| 48 |
VALID_TYPES = [ '#', '^', '/', '=', '!', '<', '>', '&', '{' ].map(&:freeze) |
| 49 |
|
| 50 |
def self.valid_types |
| 51 |
@valid_types ||= Regexp.new(VALID_TYPES.map { |t| Regexp.escape(t) }.join('|') ) |
| 52 |
end |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
def self.add_type(*types, &block) |
| 66 |
types = types.map(&:to_s) |
| 67 |
type, *aliases = types |
| 68 |
method_name = "scan_tag_#{type}".to_sym |
| 69 |
define_method(method_name, &block) |
| 70 |
aliases.each { |a| alias_method "scan_tag_#{a}", method_name } |
| 71 |
types.each { |t| VALID_TYPES << t unless VALID_TYPES.include?(t) } |
| 72 |
@valid_types = nil |
| 73 |
end |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
SKIP_WHITESPACE = [ '#', '^', '/', '<', '>', '=', '!' ].map(&:freeze) |
| 79 |
|
| 80 |
|
| 81 |
ALLOWED_CONTENT = /(\w|[?!\/.-])*/ |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
ANY_CONTENT = [ '!', '=' ].map(&:freeze) |
| 86 |
|
| 87 |
attr_reader :otag, :ctag |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
def initialize(options = {}) |
| 92 |
@options = options |
| 93 |
@option_inline_partials_at_compile_time = options[:inline_partials_at_compile_time] |
| 94 |
if @option_inline_partials_at_compile_time |
| 95 |
@partial_resolver = options[:partial_resolver] |
| 96 |
raise ArgumentError.new "Missing or invalid partial_resolver" unless @partial_resolver.respond_to? :call |
| 97 |
end |
| 98 |
|
| 99 |
|
| 100 |
self.otag ||= '{{' |
| 101 |
self.ctag ||= '}}' |
| 102 |
end |
| 103 |
|
| 104 |
|
| 105 |
def otag=(value) |
| 106 |
regex = regexp value |
| 107 |
@otag_regex = /([ \t]*)?#{regex}/ |
| 108 |
@otag_not_regex = /(^[ \t]*)?#{regex}/ |
| 109 |
@otag = value |
| 110 |
end |
| 111 |
|
| 112 |
|
| 113 |
def ctag=(value) |
| 114 |
@ctag_regex = regexp value |
| 115 |
@ctag = value |
| 116 |
end |
| 117 |
|
| 118 |
|
| 119 |
def compile(template) |
| 120 |
@encoding = nil |
| 121 |
|
| 122 |
if template.respond_to?(:encoding) |
| 123 |
@encoding = template.encoding |
| 124 |
template = template.dup.force_encoding("BINARY") |
| 125 |
end |
| 126 |
|
| 127 |
|
| 128 |
@sections = [] |
| 129 |
@result = [:multi] |
| 130 |
@scanner = StringScanner.new(template) |
| 131 |
|
| 132 |
|
| 133 |
until @scanner.eos? |
| 134 |
scan_tags || scan_text |
| 135 |
end |
| 136 |
|
| 137 |
unless @sections.empty? |
| 138 |
|
| 139 |
type, pos, _ = @sections.pop |
| 140 |
error "Unclosed section #{type.inspect}", pos |
| 141 |
end |
| 142 |
|
| 143 |
@result |
| 144 |
end |
| 145 |
|
| 146 |
|
| 147 |
private |
| 148 |
|
| 149 |
|
| 150 |
def content_tags type, current_ctag_regex |
| 151 |
if ANY_CONTENT.include?(type) |
| 152 |
r = /\s*#{regexp(type)}?#{current_ctag_regex}/ |
| 153 |
scan_until_exclusive(r) |
| 154 |
else |
| 155 |
@scanner.scan(ALLOWED_CONTENT) |
| 156 |
end |
| 157 |
end |
| 158 |
|
| 159 |
def dispatch_based_on_type type, content, fetch, padding, pre_match_position |
| 160 |
send("scan_tag_#{type}", content, fetch, padding, pre_match_position) |
| 161 |
end |
| 162 |
|
| 163 |
def find_closing_tag scanner, current_ctag_regex |
| 164 |
error "Unclosed tag" unless scanner.scan(current_ctag_regex) |
| 165 |
end |
| 166 |
|
| 167 |
|
| 168 |
def scan_tags |
| 169 |
|
| 170 |
start_of_line = @scanner.beginning_of_line? |
| 171 |
pre_match_position = @scanner.pos |
| 172 |
last_index = @result.length |
| 173 |
|
| 174 |
return unless @scanner.scan @otag_regex |
| 175 |
padding = @scanner[1] || '' |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
unless start_of_line |
| 180 |
@result << [:static, padding] unless padding.empty? |
| 181 |
pre_match_position += padding.length |
| 182 |
padding = '' |
| 183 |
end |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
current_ctag_regex = @ctag_regex |
| 188 |
type = @scanner.scan(self.class.valid_types) |
| 189 |
@scanner.skip(/\s*/) |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
content = content_tags(type, current_ctag_regex) |
| 194 |
|
| 195 |
|
| 196 |
error "Illegal content in tag" if content.empty? |
| 197 |
|
| 198 |
fetch = [:mustache, :fetch, content.split('.')] |
| 199 |
prev = @result |
| 200 |
|
| 201 |
dispatch_based_on_type(type, content, fetch, padding, pre_match_position) |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
type = "}" if type == "{" |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@scanner.skip(/\s+/) |
| 210 |
@scanner.skip(regexp(type)) if type |
| 211 |
|
| 212 |
find_closing_tag(@scanner, current_ctag_regex) |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if start_of_line && !@scanner.eos? |
| 218 |
if @scanner.peek(2) =~ /\r?\n/ && SKIP_WHITESPACE.include?(type) |
| 219 |
@scanner.skip(/\r?\n/) |
| 220 |
else |
| 221 |
prev.insert(last_index, [:static, padding]) unless padding.empty? |
| 222 |
end |
| 223 |
end |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
@sections.last[1] << @scanner.pos unless @sections.empty? |
| 228 |
|
| 229 |
return unless @result == [:multi] |
| 230 |
end |
| 231 |
|
| 232 |
|
| 233 |
def scan_text |
| 234 |
text = scan_until_exclusive @otag_not_regex |
| 235 |
|
| 236 |
if text.nil? |
| 237 |
|
| 238 |
text = @scanner.rest |
| 239 |
|
| 240 |
@scanner.terminate |
| 241 |
end |
| 242 |
|
| 243 |
text.force_encoding(@encoding) if @encoding |
| 244 |
|
| 245 |
@result << [:static, text] unless text.empty? |
| 246 |
end |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
def scan_until_exclusive(regexp) |
| 252 |
pos = @scanner.pos |
| 253 |
if @scanner.scan_until(regexp) |
| 254 |
@scanner.pos -= @scanner.matched.size |
| 255 |
@scanner.pre_match[pos..-1] |
| 256 |
end |
| 257 |
end |
| 258 |
|
| 259 |
def offset |
| 260 |
position[0, 2] |
| 261 |
end |
| 262 |
|
| 263 |
|
| 264 |
def position |
| 265 |
|
| 266 |
rest = @scanner.check_until(/\n|\Z/).to_s.chomp |
| 267 |
|
| 268 |
|
| 269 |
parsed = @scanner.string[0...@scanner.pos] |
| 270 |
|
| 271 |
lines = parsed.split("\n") |
| 272 |
|
| 273 |
[ lines.size, lines.last.size - 1, lines.last + rest ] |
| 274 |
end |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
def regexp(thing) |
| 279 |
Regexp.new Regexp.escape(thing) if thing |
| 280 |
end |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
def error(message, pos = position) |
| 286 |
raise SyntaxError.new(message, pos) |
| 287 |
end |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
def scan_tag_ content, fetch, padding, pre_match_position |
| 302 |
@result << [:mustache, :etag, fetch, offset] |
| 303 |
end |
| 304 |
|
| 305 |
|
| 306 |
def scan_tag_block content, fetch, padding, pre_match_position |
| 307 |
block = [:multi] |
| 308 |
@result << [:mustache, :section, fetch, offset, block] |
| 309 |
@sections << [content, position, @result] |
| 310 |
@result = block |
| 311 |
end |
| 312 |
alias_method :'scan_tag_#', :scan_tag_block |
| 313 |
|
| 314 |
|
| 315 |
def scan_tag_inverted content, fetch, padding, pre_match_position |
| 316 |
block = [:multi] |
| 317 |
@result << [:mustache, :inverted_section, fetch, offset, block] |
| 318 |
@sections << [content, position, @result] |
| 319 |
@result = block |
| 320 |
end |
| 321 |
alias_method :'scan_tag_^', :scan_tag_inverted |
| 322 |
|
| 323 |
|
| 324 |
def scan_tag_close content, fetch, padding, pre_match_position |
| 325 |
section, pos, result = @sections.pop |
| 326 |
if section.nil? |
| 327 |
error "Closing unopened #{content.inspect}" |
| 328 |
end |
| 329 |
|
| 330 |
raw = @scanner.pre_match[pos[3]...pre_match_position] + padding |
| 331 |
(@result = result).last << raw << [self.otag, self.ctag] |
| 332 |
|
| 333 |
if section != content |
| 334 |
error "Unclosed section #{section.inspect}", pos |
| 335 |
end |
| 336 |
end |
| 337 |
alias_method :'scan_tag_/', :scan_tag_close |
| 338 |
|
| 339 |
|
| 340 |
def scan_tag_comment content, fetch, padding, pre_match_position |
| 341 |
end |
| 342 |
alias_method :'scan_tag_!', :scan_tag_comment |
| 343 |
|
| 344 |
|
| 345 |
def scan_tag_delimiter content, fetch, padding, pre_match_position |
| 346 |
self.otag, self.ctag = content.split(' ', 2) |
| 347 |
end |
| 348 |
alias_method :'scan_tag_=', :scan_tag_delimiter |
| 349 |
|
| 350 |
|
| 351 |
def scan_tag_open_partial content, fetch, padding, pre_match_position |
| 352 |
@result << if @option_inline_partials_at_compile_time |
| 353 |
partial = @partial_resolver.call content |
| 354 |
partial.gsub!(/^/, padding) unless padding.empty? |
| 355 |
self.class.new(@options).compile partial |
| 356 |
else |
| 357 |
[:mustache, :partial, content, offset, padding] |
| 358 |
end |
| 359 |
end |
| 360 |
alias_method :'scan_tag_<', :scan_tag_open_partial |
| 361 |
alias_method :'scan_tag_>', :scan_tag_open_partial |
| 362 |
|
| 363 |
|
| 364 |
def scan_tag_unescaped content, fetch, padding, pre_match_position |
| 365 |
@result << [:mustache, :utag, fetch, offset] |
| 366 |
end |
| 367 |
alias_method :'scan_tag_{', :'scan_tag_unescaped' |
| 368 |
alias_method :'scan_tag_&', :'scan_tag_unescaped' |
| 369 |
|
| 370 |
end |
| 371 |
end |