Class

play.twirl.parser

TwirlParser

Related Doc: package parser

Permalink

class TwirlParser extends AnyRef

TwirlParser is a recursive descent parser for a modified grammar of the Play2 template language as loosely defined here and more rigorously defined by the original template parser, play.templates.ScalaTemplateCompiler.TemplateParser. TwirlParser is meant to be a near drop in replacement for play.templates.ScalaTemplateCompiler.TemplateParser.

The original grammar, as reversed-engineered from play.templates.ScalaTemplateCompiler.TemplateParser, is defined as follows:

parser : comment? whitespace? ('@' parentheses+)? templateContent
templateContent : (importExpression | localDef | template | mixed)*
templateDeclaration : '@' identifier squareBrackets? parentheses*
localDef : templateDeclaration (' ' | '\t')* '=' (' ' | '\t') scalaBlock
template : templateDeclaration (' ' | '\t')* '=' (' ' | '\t') '{' templateContent '}'
mixed : (comment | scalaBlockDisplayed | caseExpression | matchExpression | forExpression | safeExpression | plain | expression) | ('{' mixed* '}')
scalaBlockDisplayed : scalaBlock
scalaBlockChained : scalaBlock
scalaBlock : '@' brackets
importExpression : '@' 'import ' .* '\r'? '\n'
caseExpression : whitespace? 'case' .+ '=>' block whitespace?
forExpression : '@' "for" parentheses block
matchExpression : '@' (simpleExpr | complexExpr) whitespaceNoBreak 'match' block
simpleExpr : methodCall expressionPart*
complexExpr : parentheses
safeExpression : '@' parentheses
elseCall : whitespaceNoBreak "else" whitespaceNoBreak?
chainedMethods : ('.' methodCall)+
expressionPart : chainedMethods | block | (whitespaceNoBreak scalaBlockChained) | elseCall | parentheses
expression : '@' methodCall expressionPart*
methodCall : identifier squareBrackets? parentheses?
blockArgs : [^'=>' '\n']* '=>'
block : whitespaceNoBreak '{' blockArgs? mixed* '}'
brackets : '{' (brackets | [^'}'])* '}'
comment : '@*' [^'*@']* '*@'
parentheses : '(' (parentheses | [^')'])* ')'
squareBrackets : '[' (squareBrackets | [^']'])* ']'
plain : ('@@' | ([^'@'] [^'{' '}']))+
whitespaceNoBreak : [' ' '\t']+
identifier : javaIdentStart javaIdentPart* // see java docs for what these two rules mean

TwirlParser implements a slightly modified version of the above grammar that removes some back-tracking within the 'mixed' non-terminal. It is defined as follows:

parser : comment? whitespace? ('@' parentheses+)? templateContent
templateContent : (importExpression | localDef | template | mixed)*
templateDeclaration : '@' identifier squareBrackets? parentheses*
localDef : templateDeclaration (' ' | '\t')* '=' (' ' | '\t') scalaBlock
template : templateDeclaration (' ' | '\t')* '=' (' ' | '\t') '{' templateContent '}'
mixed : (comment | scalaBlockDisplayed | forExpression | matchExpOrSafeExpOrExpr | caseExpression | plain) | ('{' mixed* '}')
matchExpOrSafeExpOrExpr : (expression | safeExpression) (whitespaceNoBreak 'match' block)?
scalaBlockDisplayed : scalaBlock
scalaBlockChained : scalaBlock
scalaBlock : '@' brackets
importExpression : '@' 'import ' .* '\r'? '\n'
caseExpression : (whitespace? 'case' .+ '=>' block whitespace?) | whitespace
forExpression : '@' "for" parentheses block
simpleExpr : methodCall expressionPart*
complexExpr : parentheses
safeExpression : '@' parentheses
elseCall : whitespaceNoBreak? "else" whitespaceNoBreak?
elseIfCall : whitespaceNoBreak? "else if" parentheses  whitespaceNoBreak?
chainedMethods : ('.' methodCall)+
expressionPart : chainedMethods | block | (whitespaceNoBreak scalaBlockChained) | elseIfCall | elseCall | parentheses
expression : '@' methodCall expressionPart*
methodCall : identifier squareBrackets? parentheses?
blockArgs : [^'=>' '\n']* '=>'
block : whitespaceNoBreak? '{' blockArgs? mixed* '}'
brackets : '{' (brackets | [^'}'])* '}'
comment : '@*' [^'*@']* '*@'
parentheses : '(' (parentheses | [^')'])* ')'
squareBrackets : '[' (squareBrackets | [^']'])* ']'
plain : ('@@' | '@}' | ([^'@'] [^'{' '}']))+
whitespaceNoBreak : [' ' '\t']+
identifier : javaIdentStart javaIdentPart* // see java docs for what these two rules mean

TwirlParser can detect several type of parse errors and provides line information. In all cases, the parser will continue parsing the best it can after encountering an error. The following errors are what can be detected:

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TwirlParser
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new TwirlParser(shouldParseInclusiveDot: Boolean)

    Permalink

Type Members

  1. case class Error(template: Template, input: Input, errors: List[PosString]) extends ParseResult with Product with Serializable

    Permalink
  2. case class Input() extends Product with Serializable

    Permalink
  3. sealed abstract class ParseResult extends AnyRef

    Permalink
  4. case class Success(template: Template, input: Input) extends ParseResult with Product with Serializable

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def accept(str: String): Unit

    Permalink

    Try to match str and advance str.length characters.

    Try to match str and advance str.length characters.

    Reports an error if the input does not match str or if str.length goes past the EOF.

  5. def any(length: Int = 1): String

    Permalink

    Consume/Advance length characters, and return the consumed characters.

    Consume/Advance length characters, and return the consumed characters. Returns "" if at EOF.

  6. def anyUntil(f: (Char) ⇒ Boolean, inclusive: Boolean): String

    Permalink

    Consume characters until f returns false on the peek of input.

    Consume characters until f returns false on the peek of input.

    inclusive

    - should the stopped character be included in the consumed characters?

    returns

    the consumed characters

  7. def anyUntil(stop: String, inclusive: Boolean): String

    Permalink

    Consume characters until input matches stop

    Consume characters until input matches stop

    inclusive

    - should stop be included in the consumed characters?

    returns

    the consumed characters

  8. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  9. def block(blockArgsAllowed: Boolean): Block

    Permalink
  10. def blockArgs(): PosString

    Permalink
  11. def brackets(): String

    Permalink
  12. def caseExpression(): TemplateTree

    Permalink
  13. def chainedMethods(): Simple

    Permalink
  14. def check(str: String): Boolean

    Permalink

    Does the current input match str? If so, advance str.length.

    Does the current input match str? If so, advance str.length.

    Will not advance if str.length surpasses EOF

    returns

    true if advancing, false otherwise.

  15. def check(f: (Char) ⇒ Boolean): Boolean

    Permalink

    Does f applied to the current peek return true or false? If true, advance one character.

    Does f applied to the current peek return true or false? If true, advance one character.

    Will not advance if at EOF.

    returns

    true if advancing, false otherwise.

  16. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  17. def comment(): Comment

    Permalink

    Parse a comment.

  18. def elseCall(): Simple

    Permalink
  19. def elseIfCall(): Simple

    Permalink
  20. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  21. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  22. def error(message: String, offset: Int = input.offset()): Unit

    Permalink
  23. def expression(): Display

    Permalink
  24. def expressionPart(blockArgsAllowed: Boolean): ScalaExpPart

    Permalink
  25. def extraImports(): Seq[Simple]

    Permalink
  26. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  27. def forExpression(): Display

    Permalink
  28. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  29. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  30. def identifier(): String

    Permalink
  31. def importExpression(): Simple

    Permalink
  32. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  33. def lastComment(): Comment

    Permalink

    Parses comments and/or whitespace, ignoring both until the last comment is reached, and returning that (if found)

  34. def localDef(): Def

    Permalink
  35. def matchExpOrSafeExpOrExpr(): Display

    Permalink
  36. def methodCall(): String

    Permalink
  37. def mixed(): ListBuffer[TemplateTree]

    Permalink
  38. def mkRegressionStatisticsString(): Unit

    Permalink
  39. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  40. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  41. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  42. def parentheses(): String

    Permalink
  43. def parse(source: String): ParseResult

    Permalink
  44. def plain(): Plain

    Permalink
  45. def position[T <: Positional](positional: T, offset: Int): T

    Permalink

    Set the source position of a Positional

  46. def recursiveTag(prefix: String, suffix: String, allowStringLiterals: Boolean = false): String

    Permalink

    Recursively match pairs of prefixes and suffixes and return the consumed characters

    Recursively match pairs of prefixes and suffixes and return the consumed characters

    Terminates at EOF.

  47. def safeExpression(): Display

    Permalink
  48. def scalaBlock(): Simple

    Permalink
  49. def scalaBlockChained(): Block

    Permalink
  50. def scalaBlockDisplayed(): Display

    Permalink
  51. def setSource(source: String): Unit

    Permalink
  52. def several[T, BufferType <: Buffer[T]](parser: () ⇒ T, provided: BufferType = null)(implicit manifest: Manifest[BufferType]): BufferType

    Permalink

    Match zero or more parser

  53. val shouldParseInclusiveDot: Boolean

    Permalink
  54. def squareBrackets(): String

    Permalink
  55. def stringLiteral(quote: String, escape: String): String

    Permalink

    Match a string literal, allowing for escaped quotes.

    Match a string literal, allowing for escaped quotes. Terminates at EOF.

  56. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  57. def template(): Template

    Permalink
  58. def templateContent(): (Seq[Simple], Seq[Def], Seq[Template], Seq[TemplateTree])

    Permalink
  59. def templateDeclaration(): (PosString, PosString)

    Permalink
  60. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  61. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  62. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  63. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  64. def whitespace(): String

    Permalink
  65. def whitespaceNoBreak(): String

    Permalink

Inherited from AnyRef

Inherited from Any

Ungrouped