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"]]
The content allowed in a tag name.
These types of tags allow any content, the rest only allow ALLOWED_CONTENT.
After these types of tags, all whitespace will be skipped.
Accepts an options hash which does nothing but may be used in the future.
# File lib/mustache/parser.rb, line 62 def initialize(options = {}) @options = {} end
Given a string template, returns an array of tokens.
# File lib/mustache/parser.rb, line 77 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
The closing tag delimiter. This too may be changed at runtime.
# File lib/mustache/parser.rb, line 72 def ctag @ctag ||= '}}' end
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 226 def error(message, pos = position) raise SyntaxError.new(message, pos) end
The opening tag delimiter. This may be changed at runtime.
# File lib/mustache/parser.rb, line 67 def otag @otag ||= '{{' end
Returns [lineno, column, line]
# File lib/mustache/parser.rb, line 205 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
Used to quickly convert a string into a regular expression usable by the string scanner.
# File lib/mustache/parser.rb, line 219 def regexp(thing) /#{Regexp.escape(thing)}/ end
Try to find static text, e.g. raw HTML with no {{mustaches}}.
# File lib/mustache/parser.rb, line 178 def scan_text text = scan_until_exclusive(regexp(otag)) if text.nil? # Couldn't find any otag, which means the rest is just static text. text = @scanner.rest # Mark as done. @scanner.clear end text.force_encoding(@encoding) if @encoding @result << [:static, text] end
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 196 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