class Mustache::Parser

The Parser is responsible for taking a string template and converting it into an array of tokens and, really, expressions. It raises SyntaxError if there is anything it doesn't understand and knows which sigil corresponds to which tag type.

For example, given this template:

Hi {{thing}}!

Run through the Parser we'll get these tokens:

[:multi,
  [:static, "Hi "],
  [:mustache, :etag, "thing"],
  [:static, "!\n"]]

You can see the array of tokens for any template with the mustache(1) command line tool:

$ mustache --tokens test.mustache
[:multi, [:static, "Hi "], [:mustache, :etag, "thing"], [:static, "!\n"]]

Constants

ALLOWED_CONTENT

The content allowed in a tag name.

ANY_CONTENT

These types of tags allow any content, the rest only allow ALLOWED_CONTENT.

SKIP_WHITESPACE

After these types of tags, all whitespace until the end of the line will be skipped if they are the first (and only) non-whitespace content on the line.

VALID_TYPES

The sigil types which are valid after an opening `{{`

Attributes

ctag[W]
otag[W]
result[R]
scanner[R]

Public Class Methods

add_type(*types, &block) click to toggle source

Add a supported sigil type (with optional aliases) to the Parser.

Requires a block, which will be sent the following parameters:

  • content - The raw content of the tag

  • fetch- A mustache context fetch expression for the content

  • padding - Indentation whitespace from the currently-parsed line

  • pre_match_position - Location of the scanner before a match was made

The provided block will be evaluated against the current instance of Parser, and may append to the Parser's @result as needed.

# File lib/mustache/parser.rb, line 65
def self.add_type(*types, &block)
  types = types.map(&:to_s)
  type, *aliases = types
  method_name = "scan_tag_#{type}".to_sym
  define_method(method_name, &block)
  aliases.each { |a| alias_method "scan_tag_#{a}", method_name }
  types.each { |t| VALID_TYPES << t unless VALID_TYPES.include?(t) }
  @valid_types = nil
end
new(options = {}) click to toggle source

Accepts an options hash which does nothing but may be used in the future.

# File lib/mustache/parser.rb, line 92
def initialize(options = {})
  @options = {}
end
valid_types() click to toggle source
# File lib/mustache/parser.rb, line 50
def self.valid_types
  @valid_types ||= Regexp.new(VALID_TYPES.map { |t| Regexp.escape(t) }.join('|') )
end

Public Instance Methods

compile(template) click to toggle source

Given a string template, returns an array of tokens.

# File lib/mustache/parser.rb, line 107
def compile(template)
  if template.respond_to?(:encoding)
    @encoding = template.encoding
    template = template.dup.force_encoding("BINARY")
  else
    @encoding = nil
  end

  # Keeps information about opened sections.
  @sections = []
  @result = [:multi]
  @scanner = StringScanner.new(template)

  # Scan until the end of the template.
  until @scanner.eos?
    scan_tags || scan_text
  end

  if !@sections.empty?
    # We have parsed the whole file, but there's still opened sections.
    type, pos, result = @sections.pop
    error "Unclosed section #{type.inspect}", pos
  end

  @result
end
ctag() click to toggle source

The closing tag delimiter. This too may be changed at runtime.

# File lib/mustache/parser.rb, line 102
def ctag
  @ctag ||= '}}'
end
error(message, pos = position) click to toggle source

Raises a SyntaxError. The message should be the name of the error - other details such as line number and position are handled for you.

# File lib/mustache/parser.rb, line 266
def error(message, pos = position)
  raise SyntaxError.new(message, pos)
end
offset() click to toggle source
# File lib/mustache/parser.rb, line 240
def offset
  position[0, 2]
end
otag() click to toggle source

The opening tag delimiter. This may be changed at runtime.

# File lib/mustache/parser.rb, line 97
def otag
  @otag ||= '{{'
end
position() click to toggle source

Returns [lineno, column, line]

# File lib/mustache/parser.rb, line 245
def position
  # The rest of the current line
  rest = @scanner.check_until(/\n|\Z/).to_s.chomp

  # What we have parsed so far
  parsed = @scanner.string[0...@scanner.pos]

  lines = parsed.split("\n")

  [ lines.size, lines.last.size - 1, lines.last + rest ]
end
regexp(thing) click to toggle source

Used to quickly convert a string into a regular expression usable by the string scanner.

# File lib/mustache/parser.rb, line 259
def regexp(thing)
  /#{Regexp.escape(thing)}/
end
scan_tags() click to toggle source

Find {{mustaches}} and add them to the @result array.

# File lib/mustache/parser.rb, line 135
def scan_tags
  # Scan until we hit an opening delimiter.
  start_of_line = @scanner.beginning_of_line?
  pre_match_position = @scanner.pos
  last_index = @result.length

  return unless x = @scanner.scan(/([ \t]*)?#{Regexp.escape(otag)}/)
  padding = @scanner[1] || ''

  # Don't touch the preceding whitespace unless we're matching the start
  # of a new line.
  unless start_of_line
    @result << [:static, padding] unless padding.empty?
    pre_match_position += padding.length
    padding = ''
  end

  # Since {{= rewrites ctag, we store the ctag which should be used
  # when parsing this specific tag.
  current_ctag = self.ctag
  type = @scanner.scan(self.class.valid_types)
  @scanner.skip(/\s*/)

  # ANY_CONTENT tags allow any character inside of them, while
  # other tags (such as variables) are more strict.
  content = if ANY_CONTENT.include?(type)
    r = /\s*#{regexp(type)}?#{regexp(current_ctag)}/
    scan_until_exclusive(r)
  else
    @scanner.scan(ALLOWED_CONTENT)
  end

  # We found {{ but we can't figure out what's going on inside.
  error "Illegal content in tag" if content.empty?

  fetch = [:mustache, :fetch, content.split('.')]
  prev = @result

  # Based on the sigil, do what needs to be done.
  if type
    # Method#call proves much faster than using send
    method("scan_tag_#{type}").
      call(content, fetch, padding, pre_match_position)
  else
    @result << [:mustache, :etag, fetch, offset]
  end
  # The closing } in unescaped tags is just a hack for
  # aesthetics.
  type = "}" if type == "{"

  # Skip whitespace and any balancing sigils after the content
  # inside this tag.
  @scanner.skip(/\s+/)
  @scanner.skip(regexp(type)) if type

  # Try to find the closing tag.
  unless close = @scanner.scan(regexp(current_ctag))
    error "Unclosed tag"
  end

  # If this tag was the only non-whitespace content on this line, strip
  # the remaining whitespace.  If not, but we've been hanging on to padding
  # from the beginning of the line, re-insert the padding as static text.
  if start_of_line && !@scanner.eos?
    if @scanner.peek(2) =~ /\r?\n/ && SKIP_WHITESPACE.include?(type)
      @scanner.skip(/\r?\n/)
    else
      prev.insert(last_index, [:static, padding]) unless padding.empty?
    end
  end

  # Store off the current scanner position now that we've closed the tag
  # and consumed any irrelevant whitespace.
  @sections.last[1] << @scanner.pos unless @sections.empty?

  return unless @result == [:multi]
end
scan_text() click to toggle source

Try to find static text, e.g. raw HTML with no {{mustaches}}.

# File lib/mustache/parser.rb, line 214
def scan_text
  text = scan_until_exclusive(/(^[ \t]*)?#{Regexp.escape(otag)}/)

  if text.nil?
    # Couldn't find any otag, which means the rest is just static text.
    text = @scanner.rest
    # Mark as done.
    @scanner.terminate
  end

  text.force_encoding(@encoding) if @encoding

  @result << [:static, text] unless text.empty?
end
scan_until_exclusive(regexp) click to toggle source

Scans the string until the pattern is matched. Returns the substring excluding the end of the match, advancing the scan pointer to that location. If there is no match, nil is returned.

# File lib/mustache/parser.rb, line 232
def scan_until_exclusive(regexp)
  pos = @scanner.pos
  if @scanner.scan_until(regexp)
    @scanner.pos -= @scanner.matched.size
    @scanner.pre_match[pos..-1]
  end
end