play.twirl.parser

TwirlParser

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?
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 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
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new TwirlParser(shouldParseInclusiveDot: Boolean)

Type Members

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

  2. case class Input() extends Product with Serializable

  3. sealed abstract class ParseResult extends AnyRef

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

Value Members

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

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

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

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. def accept(str: String): Unit

    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.

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

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

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

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

    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

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

    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

  10. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  11. def block(): Block

  12. def blockArgs(): PosString

  13. def brackets(): String

  14. def caseExpression(): TemplateTree

  15. def chainedMethods(): Simple

  16. def check(str: String): Boolean

    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.

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

    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.

  18. def clone(): AnyRef

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

  20. def elseCall(): Simple

  21. final def eq(arg0: AnyRef): Boolean

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

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

  24. def expression(): Display

  25. def expressionPart(): ScalaExpPart

  26. def extraImports(): Seq[Simple]

  27. def finalize(): Unit

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

  29. final def getClass(): Class[_]

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

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

  32. def importExpression(): Simple

  33. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  34. def localDef(): Def

  35. def matchExpOrSafeExpOrExpr(): Display

  36. def methodCall(): String

  37. def mixed(): ListBuffer[TemplateTree]

  38. def mkRegressionStatisticsString(): Unit

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

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

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

    Definition Classes
    AnyRef
  42. def parentheses(): String

  43. def parse(source: String): ParseResult

  44. def plain(): Plain

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

    Set the source position of a Positional

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

    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

  48. def scalaBlock(): Simple

  49. def scalaBlockChained(): Block

  50. def scalaBlockDisplayed(): Display

  51. def setSource(source: String): Unit

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

    Match zero or more parser

  53. val shouldParseInclusiveDot: Boolean

  54. def squareBrackets(): String

  55. def startArgs(): String

  56. def stringLiteral(quote: String, escape: String): String

    Match a string literal, allowing for escaped quotes.

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

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

    Definition Classes
    AnyRef
  58. def template(): Template

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

  60. def templateDeclaration(): (PosString, PosString)

  61. def toString(): String

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

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

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

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

  66. def whitespaceNoBreak(): String

Inherited from AnyRef

Inherited from Any

Ungrouped