Class Mustache::Parser
In: lib/mustache/parser.rb
Parent: Object

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"]]

Methods

compile   ctag   error   new   otag   position   regexp   scan_tags   scan_text   scan_until_exclusive  

Classes and Modules

Class Mustache::Parser::SyntaxError

Constants

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.
ALLOWED_CONTENT = /(\w|[?!\/.-])*/   The content allowed in a tag name.
ANY_CONTENT = [ '!', '=' ]   These types of tags allow any content, the rest only allow ALLOWED_CONTENT.

Attributes

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

Public Class methods

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

Public Instance methods

Given a string template, returns an array of tokens.

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

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

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

Returns [lineno, column, line]

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

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

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

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.

[Validate]