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
Public Class Methods
Source
# 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
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.
Source
# File lib/mustache/parser.rb, line 91 def initialize(options = {}) @options = options @option_inline_partials_at_compile_time = options[:inline_partials_at_compile_time] if @option_inline_partials_at_compile_time @partial_resolver = options[:partial_resolver] raise ArgumentError.new "Missing or invalid partial_resolver" unless @partial_resolver.respond_to? :call end # Initialize default tags self.otag ||= '{{' self.ctag ||= '}}' end
Accepts an options hash which does nothing but may be used in the future.
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
Source
# File lib/mustache/parser.rb, line 119 def compile(template) @encoding = nil if template.respond_to?(:encoding) @encoding = template.encoding template = template.dup.force_encoding("BINARY") 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 unless @sections.empty? # We have parsed the whole file, but there's still opened sections. type, pos, _ = @sections.pop error "Unclosed section #{type.inspect}", pos end @result end
Given a string template, returns an array of tokens.
Source
# File lib/mustache/parser.rb, line 113 def ctag=(value) @ctag_regex = regexp value @ctag = value end
The closing tag delimiter. This too may be changed at runtime.
Source
# File lib/mustache/parser.rb, line 105 def otag=(value) regex = regexp value @otag_regex = /([ \t]*)?#{regex}/ @otag_not_regex = /(^[ \t]*)?#{regex}/ @otag = value end
The opening tag delimiter. This may be changed at runtime.
Private Instance Methods
Source
# File lib/mustache/parser.rb, line 159 def dispatch_based_on_type type, content, fetch, padding, pre_match_position send("scan_tag_#{type}", content, fetch, padding, pre_match_position) end
Source
# File lib/mustache/parser.rb, line 285 def error(message, pos = position) raise SyntaxError.new(message, pos) 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.
Source
# File lib/mustache/parser.rb, line 163 def find_closing_tag scanner, current_ctag_regex error "Unclosed tag" unless scanner.scan(current_ctag_regex) end
Source
# File lib/mustache/parser.rb, line 264 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
Returns [lineno, column, line]
Source
# File lib/mustache/parser.rb, line 278 def regexp(thing) Regexp.new Regexp.escape(thing) if thing end
Used to quickly convert a string into a regular expression usable by the string scanner.
Source
# File lib/mustache/parser.rb, line 301 def scan_tag_ content, fetch, padding, pre_match_position @result << [:mustache, :etag, fetch, offset] end
This function handles the cases where the scanned tag does not have a type.
Source
# File lib/mustache/parser.rb, line 306 def scan_tag_block content, fetch, padding, pre_match_position block = [:multi] @result << [:mustache, :section, fetch, offset, block] @sections << [content, position, @result] @result = block end
Source
# File lib/mustache/parser.rb, line 324 def scan_tag_close content, fetch, padding, pre_match_position section, pos, result = @sections.pop if section.nil? error "Closing unopened #{content.inspect}" end raw = @scanner.pre_match[pos[3]...pre_match_position] + padding (@result = result).last << raw << [self.otag, self.ctag] if section != content error "Unclosed section #{section.inspect}", pos end end
Source
# File lib/mustache/parser.rb, line 340 def scan_tag_comment content, fetch, padding, pre_match_position end
Source
# File lib/mustache/parser.rb, line 345 def scan_tag_delimiter content, fetch, padding, pre_match_position self.otag, self.ctag = content.split(' ', 2) end
Source
# File lib/mustache/parser.rb, line 315 def scan_tag_inverted content, fetch, padding, pre_match_position block = [:multi] @result << [:mustache, :inverted_section, fetch, offset, block] @sections << [content, position, @result] @result = block end
Source
# File lib/mustache/parser.rb, line 351 def scan_tag_open_partial content, fetch, padding, pre_match_position @result << if @option_inline_partials_at_compile_time partial = @partial_resolver.call content partial.gsub!(/^/, padding) unless padding.empty? self.class.new(@options).compile partial else [:mustache, :partial, content, offset, padding] end end
Source
# File lib/mustache/parser.rb, line 364 def scan_tag_unescaped content, fetch, padding, pre_match_position @result << [:mustache, :utag, fetch, offset] end
Source
# File lib/mustache/parser.rb, line 233 def scan_text text = scan_until_exclusive @otag_not_regex 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
Try to find static text, e.g. raw HTML with no {{mustaches}}.
Source
# File lib/mustache/parser.rb, line 251 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
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.