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?
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
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(blockArgsAllowed: Boolean): 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

    Parse a comment.

  20. def elseCall(): Simple

  21. def elseIfCall(): Simple

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

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

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

  25. def expression(): Display

  26. def expressionPart(blockArgsAllowed: Boolean): ScalaExpPart

  27. def extraImports(): Seq[Simple]

  28. def finalize(): Unit

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

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

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

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

  33. def importExpression(): Simple

  34. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  35. def lastComment(): Comment

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

  36. def localDef(): Def

  37. def matchExpOrSafeExpOrExpr(): Display

  38. def methodCall(): String

  39. def mixed(): ListBuffer[TemplateTree]

  40. def mkRegressionStatisticsString(): Unit

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

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

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

    Definition Classes
    AnyRef
  44. def parentheses(): String

  45. def parse(source: String): ParseResult

  46. def plain(): Plain

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

    Set the source position of a Positional

  48. 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.

  49. def safeExpression(): Display

  50. def scalaBlock(): Simple

  51. def scalaBlockChained(): Block

  52. def scalaBlockDisplayed(): Display

  53. def setSource(source: String): Unit

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

    Match zero or more parser

  55. val shouldParseInclusiveDot: Boolean

  56. def squareBrackets(): String

  57. 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.

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

    Definition Classes
    AnyRef
  59. def template(): Template

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

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

  62. def toString(): String

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

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

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

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

  67. def whitespaceNoBreak(): String

Inherited from AnyRef

Inherited from Any

Ungrouped