summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Haoyi <haoyi@dropbox.com>2014-11-17 01:18:55 -0800
committerLi Haoyi <haoyi@dropbox.com>2014-11-17 01:18:55 -0800
commit8c2ebc82047f0af1807dadd78562625e74478594 (patch)
tree82f7237b1a216246be90040b3d2edcd0c83ea92e
parent4fda6b8ad990762dc63fce6b56b9c97fb5d169d8 (diff)
downloadhands-on-scala-js-8c2ebc82047f0af1807dadd78562625e74478594.tar.gz
hands-on-scala-js-8c2ebc82047f0af1807dadd78562625e74478594.tar.bz2
hands-on-scala-js-8c2ebc82047f0af1807dadd78562625e74478594.zip
Operators like `=:=` work
-rw-r--r--scalatexApi/src/main/scala/scalaparser/ScalaSyntax.scala133
-rw-r--r--scalatexApi/src/test/scala/scalaparser/SyntaxTest.scala36
2 files changed, 121 insertions, 48 deletions
diff --git a/scalatexApi/src/main/scala/scalaparser/ScalaSyntax.scala b/scalatexApi/src/main/scala/scalaparser/ScalaSyntax.scala
index f88a532..9349a28 100644
--- a/scalatexApi/src/main/scala/scalaparser/ScalaSyntax.scala
+++ b/scalatexApi/src/main/scala/scalaparser/ScalaSyntax.scala
@@ -90,18 +90,24 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif
def ClassQualifier = rule { '[' ~ Id() ~ ']' }
- def Type: R0 = rule { FunctionArgTypes ~ "=>" ~ Type | InfixType ~ optional(ExistentialClause) }
- def FunctionArgTypes = rule { InfixType | '(' ~ optional(oneOrMore(ParamType) separatedBy ',') ~ ')' }
+ def Type(G: B = t): R0 = rule {
+ FunctionArgTypes ~ "=>" ~ Type(G) | InfixType(false) ~ optional(WL ~ ExistentialClause) ~ W(G)
+ }
+ def FunctionArgTypes = rule {
+ InfixType() | '(' ~ optional(oneOrMore(ParamType) separatedBy ',') ~ ')'
+ }
- def ExistentialClause = rule { "forSome" ~ '{' ~ oneOrMore(ExistentialDcl).separatedBy(Semi) }
- def ExistentialDcl = rule { "type" ~ TypeDcl | "val" ~ ValDcl }
+ def ExistentialClause = rule { "forSome" ~ '{' ~ oneOrMore(ExistentialDcl(false)).separatedBy(Semi) }
+ def ExistentialDcl(G: B = t) = rule { "type" ~ TypeDcl(G) | "val" ~ ValDcl(G) }
- def InfixType = rule { CompoundType ~ zeroOrMore(Id() ~ optional(Newline) ~ CompoundType) }
- def CompoundType = rule {
- oneOrMore(AnnotType(false)).separatedBy(WL ~ "with") ~ optional(Refinement)
+ def InfixType(G: B = t) = rule {
+ CompoundType(false) ~ zeroOrMore(WL ~ Id() ~ optional(Newline) ~ CompoundType(false)) ~ W(G)
+ }
+ def CompoundType(G: B = t) = rule {
+ oneOrMore(AnnotType(false)).separatedBy(WL ~ "with") ~ optional(Refinement(false)) ~ W(G)
}
def AnnotType(G: B = t) = rule {
- SimpleType(false) ~ zeroOrMore(WL ~ Annotation) ~ W(G)
+ SimpleType(false) ~ zeroOrMore(WL ~ Annotation(false)) ~ W(G)
}
def SimpleType(G: B = t): R0 = rule {
BasicType(false) ~
@@ -111,17 +117,19 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif
}
def BasicType(G: B = t): R0 = rule {
'(' ~ Types ~ ')' |
- Path() ~ '.' ~ "type" |
- StableId(G)
+ Path() ~ '.' ~ "type" |
+ StableId(G)
}
def TypeArgs(G: B = t) = rule { '[' ~ Types ~ StrW("]", G) }
- def Types = rule { oneOrMore(Type).separatedBy(',') }
- def Refinement = rule { optional(Newline) ~ '{' ~ oneOrMore(RefineStat).separatedBy(Semi) ~ '}' }
- def RefineStat = rule { "type" ~ TypeDef | Dcl | MATCH }
- def TypePat = rule { CompoundType }
- def Ascription(G: B = t) = rule { ":" ~ (InfixType | oneOrMore(Annotation) | "_" ~ StrW("*", G)) }
+ def Types = rule { oneOrMore(Type()).separatedBy(',') }
+ def Refinement(G: B = t) = rule {
+ optional(Newline) ~ '{' ~ oneOrMore(RefineStat).separatedBy(Semi) ~ StrW("}", G)
+ }
+ def RefineStat = rule { "type" ~ TypeDef(false) | Dcl(false) | MATCH }
+ def TypePat = rule { CompoundType() }
+ def Ascription(G: B = t) = rule { ":" ~ (InfixType(G) | oneOrMore(Annotation(G)) | "_" ~ StrW("*", G)) }
- def ParamType = rule { "=>" ~ Type | Type ~ "*" | Type }
+ def ParamType = rule { "=>" ~ Type() | Type() ~ "*" | Type() }
def Expr(G: B = t): R0 = rule { (Bindings | optional("implicit") ~ Id() | "_") ~ "=>" ~ Expr(G) | Expr1(G) }
def Expr1(G: B = t): R0 = rule {
@@ -132,7 +140,7 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif
ForCFlow(G) |
"throw" ~ Expr(G) |
"return" ~ optional(Expr(G)) |
- SimpleExpr() ~ '=' ~ Expr(G) |
+ SimpleExpr() ~ '=' ~ !Basic.OperatorChar ~ Expr(G) |
PostfixExpr(false) ~ optional("match" ~ '{' ~ CaseClauses ~ StrW("}", false) | Ascription(false)) ~ W(G)
}
def IfCFlow(G: B = t) = rule { "if" ~ '(' ~ Expr() ~ ')' ~ zeroOrMore(Newline) ~ Expr(G) ~ optional(optional(Semi) ~ "else" ~ Expr(G)) }
@@ -181,7 +189,7 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif
def BlockStat: R0 = rule {
Semi |
Import(false) |
- zeroOrMore(Annotation) ~ (optional("implicit" | "lazy") ~ Def(false) | zeroOrMore(LocalModifier) ~ TmplDef(false)) |
+ zeroOrMore(Annotation(false)) ~ (optional("implicit" | "lazy") ~ Def(false) | zeroOrMore(LocalModifier) ~ TmplDef(false)) |
Expr1(false)
}
def ResultExpr(G: B = t): R0 = rule { (Bindings | optional("implicit") ~ Id() | "_") ~ "=>" ~ Block | Expr1(t) }
@@ -195,7 +203,9 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif
oneOrMore(Pattern1).separatedBy('|')
}
def Pattern1: R0 = rule { '_' ~ ':' ~ TypePat | VarId() ~ ':' ~ TypePat | Pattern2 }
- def Pattern2: R0 = rule { VarId() ~ optional("@" ~ Pattern3) | Pattern3 }
+ def Pattern2: R0 = rule {
+ VarId() ~ "@" ~ Pattern3 | Pattern3 | VarId()
+ }
def Pattern3: R0 = rule {
SimplePattern ~ zeroOrMore(Id() ~ SimplePattern)
}
@@ -203,34 +213,47 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif
'_' |
Literal() ~ drop[String] |
'(' ~ optional(Patterns) ~ ')' |
- StableId() ~ optional('(' ~ (optional(Patterns ~ ',') ~ optional(VarId() ~ '@') ~ '_' ~ '*' | optional(Patterns)) ~ ')') |
- VarId() /*|
- XmlPattern*/
+ (
+ StableId() ~
+ optional(
+ '(' ~
+ (optional(Patterns ~ ',') ~ optional(VarId() ~ '@') ~ '_' ~ '*' | optional(Patterns)) ~
+ ')'
+ )
+ ) |
+ VarId()
}
def Patterns: R0 = rule { '_' ~ '*' | oneOrMore(Pattern).separatedBy(',') }
def TypeParamClause: R0 = rule { '[' ~ oneOrMore(VariantTypeParam).separatedBy(',') ~ ']' }
def FunTypeParamClause: R0 = rule { '[' ~ oneOrMore(TypeParam).separatedBy(',') ~ ']' }
- def VariantTypeParam: R0 = rule { zeroOrMore(Annotation) ~ optional(anyOf("+-")) ~ TypeParam }
- def TypeParam: R0 = rule { (Id() | '_') ~ optional(TypeParamClause) ~ optional(">:" ~ Type) ~ optional("<:" ~ Type) ~ zeroOrMore("<%" ~ Type) ~ zeroOrMore(':' ~ Type) }
+ def VariantTypeParam: R0 = rule { zeroOrMore(Annotation()) ~ optional(anyOf("+-")) ~ TypeParam }
+ def TypeParam: R0 = rule {
+ (Id() | '_') ~
+ optional(TypeParamClause) ~
+ optional(">:" ~ Type()) ~
+ optional("<:" ~ Type()) ~
+ zeroOrMore("<%" ~ Type()) ~
+ zeroOrMore(':' ~ Type())
+ }
def ParamClauses: R0 = rule { zeroOrMore(ParamClause) ~ optional(optional(Newline) ~ '(' ~ "implicit" ~ Params ~ ')') }
def ParamClause: R0 = rule { optional(Newline) ~ '(' ~ optional(Params) ~ ')' }
def Params: R0 = rule { zeroOrMore(Param).separatedBy(',') }
- def Param: R0 = rule { zeroOrMore(Annotation) ~ Id() ~ optional(':' ~ ParamType) ~ optional('=' ~ Expr()) }
+ def Param: R0 = rule { zeroOrMore(Annotation()) ~ Id() ~ optional(':' ~ ParamType) ~ optional('=' ~ Expr()) }
def ClassParamClauses(G: B = t): R0 = rule { zeroOrMore(ClassParamClause(G)) ~ optional(optional(Newline) ~ '(' ~ "implicit" ~ ClassParam ~ StrW(")", G)) }
def ClassParamClause(G: B = t): R0 = rule { optional(Newline) ~ '(' ~ optional(ClassParams) ~ StrW(")", G) }
def ClassParams: R0 = rule { oneOrMore(ClassParam).separatedBy(',') }
- def ClassParam: R0 = rule { zeroOrMore(Annotation) ~ optional(zeroOrMore(Modifier) ~ ("val" | "var")) ~ Id() ~ ":" ~ ParamType ~ optional("=" ~ Expr()) }
+ def ClassParam: R0 = rule { zeroOrMore(Annotation()) ~ optional(zeroOrMore(Modifier) ~ ("val" | "var")) ~ Id() ~ ":" ~ ParamType ~ optional("=" ~ Expr()) }
def Bindings: R0 = rule { '(' ~ oneOrMore(Binding).separatedBy(',') ~ ')' }
- def Binding: R0 = rule { (Id() | '_') ~ optional(':' ~ Type) }
+ def Binding: R0 = rule { (Id() | '_') ~ optional(':' ~ Type()) }
def Modifier: R0 = rule { LocalModifier | AccessModifier | "override" }
def LocalModifier: R0 = rule { "abstract" | "final" | "sealed" | "implicit" | "lazy" }
def AccessModifier: R0 = rule { ("private" | "protected") ~ optional(AccessQualifier) }
def AccessQualifier: R0 = rule { '[' ~ ("this" | Id()) ~ ']' }
- def Annotation: R0 = rule { '@' ~ SimpleType(false) ~ zeroOrMore(WL ~ ArgumentExprs()) }
+ def Annotation(G: B = t): R0 = rule { '@' ~ SimpleType(false) ~ zeroOrMore(WL ~ ArgumentExprs(false)) ~ W(G) }
def ConstrAnnotation: R0 = rule { '@' ~ SimpleType() ~ ArgumentExprs() }
def TemplateBody(G: B = t): R0 = rule {
@@ -244,12 +267,12 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif
}
def TemplateStat: R0 = rule {
Import(false) |
- zeroOrMore(Annotation ~ optional(Newline)) ~ zeroOrMore(Modifier) ~ (Def(false) | Dcl) |
+ zeroOrMore(Annotation() ~ optional(Newline)) ~ zeroOrMore(Modifier) ~ (Def(false) | Dcl(false)) |
Expr(false) |
MATCH
}
- def SelfType: R0 = rule { "this" ~ ':' ~ Type ~ "=>" | Id() ~ optional(':' ~ Type) ~ "=>" }
+ def SelfType: R0 = rule { "this" ~ ':' ~ Type() ~ "=>" | Id() ~ optional(':' ~ Type()) ~ "=>" }
def Import(G: B = t): R0 = rule { "import" ~ oneOrMore(ImportExpr(G)).separatedBy(',') }
@@ -257,27 +280,37 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif
def ImportSelectors(G: B = t): R0 = rule { '{' ~ zeroOrMore(ImportSelector ~ ',') ~ (ImportSelector | '_') ~ StrW("}", G) }
def ImportSelector: R0 = rule { Id() ~ optional("=>" ~ (Id() | '_')) }
- def Dcl: R0 = rule {
- "val" ~ ValDcl |
- "var" ~ VarDcl |
- "def" ~ FunDcl |
- "type" ~ zeroOrMore(Newline) ~ TypeDcl
+ def Dcl(G: B = t): R0 = rule {
+ "val" ~ ValDcl(G) |
+ "var" ~ VarDcl(G) |
+ "def" ~ FunDcl(G) |
+ "type" ~ zeroOrMore(Newline) ~ TypeDcl(G)
+ }
+ def ValDcl(G: B = t): R0 = rule { Ids ~ ':' ~ Type(G) }
+ def VarDcl(G: B = t): R0 = rule { Ids ~ ':' ~ Type(G) }
+ def FunDcl(G: B = t): R0 = rule { FunSig(false) ~ optional(WL ~ ':' ~ Type(G)) }
+ def FunSig(G: B = t): R0 = rule { Id() ~ optional(FunTypeParamClause) ~ ParamClauses }
+ def TypeDcl(G: B = t): R0 = rule {
+ Id(false) ~
+ optional(WL ~ TypeParamClause) ~
+ optional(WL ~ ">:" ~ Type(false)) ~
+ optional(WL ~ "<:" ~ Type(false)) ~
+ W(G)
}
- def ValDcl: R0 = rule { Ids ~ ':' ~ Type }
- def VarDcl: R0 = rule { Ids ~ ':' ~ Type }
- def FunDcl: R0 = rule { FunSig ~ optional(':' ~ Type) }
- def FunSig: R0 = rule { Id() ~ optional(FunTypeParamClause) ~ ParamClauses }
- def TypeDcl: R0 = rule { Id() ~ optional(TypeParamClause) ~ optional(">:" ~ Type) ~ optional("<:" ~ Type) }
def PatVarDef(G: B = t): R0 = rule { "val" ~ PatDef(G) | "var" ~ VarDef(G) }
- def Def(G: B = t): R0 = rule { "def" ~ FunDef(G) | "type" ~ zeroOrMore(Newline) ~ TypeDef | PatVarDef(G) | TmplDef(G) }
- def PatDef(G: B = t): R0 = rule { oneOrMore(Pattern2).separatedBy(',') ~ optional(':' ~ Type) ~ '=' ~ Expr(G) }
- def VarDef(G: B = t): R0 = rule { Ids ~ ':' ~ Type ~ '=' ~ '_' | PatDef(G) }
+ def Def(G: B = t): R0 = rule { "def" ~ FunDef(G) | "type" ~ zeroOrMore(Newline) ~ TypeDef(G) | PatVarDef(G) | TmplDef(G) }
+ def PatDef(G: B = t): R0 = rule { oneOrMore(Pattern2).separatedBy(',') ~ optional(':' ~ Type()) ~ '=' ~ Expr(G) }
+ def VarDef(G: B = t): R0 = rule { Ids ~ ':' ~ Type() ~ '=' ~ '_' | PatDef(G) }
def FunDef(G: B = t): R0 = rule {
"this" ~ ParamClause ~ ParamClauses ~ ('=' ~ ConstrExpr | optional(Newline) ~ ConstrBlock) |
- FunSig ~ (optional(':' ~ Type) ~ '=' ~ optional("macro") ~ Expr(G) | optional(Newline) ~ '{' ~ Block ~ '}')
+ FunSig() ~
+ (
+ optional(':' ~ Type()) ~ '=' ~ optional("macro") ~ Expr(G) |
+ optional(Newline) ~ '{' ~ Block ~ StrW("}", G)
+ )
}
- def TypeDef: R0 = rule { Id() ~ optional(TypeParamClause) ~ '=' ~ Type }
+ def TypeDef(G: B = t): R0 = rule { Id() ~ optional(TypeParamClause) ~ '=' ~ Type(G) }
def TmplDef(G: B = t): R0 = rule {
"trait" ~ TraitDef(G) |
@@ -325,14 +358,20 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif
'{' ~ optional(oneOrMore(EarlyDef).separatedBy(Semi)) ~ '}' ~ "with"
}
def EarlyDef: R0 = rule {
- zeroOrMore(Annotation ~ optional(Newline)) ~ zeroOrMore(Modifier) ~ PatVarDef(false)
+ zeroOrMore(Annotation() ~ optional(Newline)) ~ zeroOrMore(Modifier) ~ PatVarDef(false)
}
def ConstrExpr: R0 = rule { ConstrBlock | SelfInvocation }
def ConstrBlock: R0 = rule { '{' ~ SelfInvocation ~ zeroOrMore(Semi ~ BlockStat) ~ '}' }
def SelfInvocation: R0 = rule { "this" ~ oneOrMore(ArgumentExprs()) }
def TopStatSeq: R0 = rule { zeroOrMore(TopStat).separatedBy(Semi) }
- def TopStat: R0 = rule { Packaging | PackageObject(false) | Import(false) | zeroOrMore(Annotation ~ optional(Newline)) ~ zeroOrMore(Modifier) ~ TmplDef(false) | MATCH }
+ def TopStat: R0 = rule {
+ Packaging |
+ PackageObject(false) |
+ Import(false) |
+ zeroOrMore(Annotation(false) ~ optional(Newline)) ~ zeroOrMore(Modifier) ~ TmplDef(false) |
+ MATCH
+ }
def Packaging: R0 = rule { "package" ~ QualId() ~ '{' ~ TopStatSeq ~ '}' }
def PackageObject(G: B = t): R0 = rule { "package" ~ "object" ~ ObjectDef(G) }
def CompilationUnit: Rule1[String] = rule {
diff --git a/scalatexApi/src/test/scala/scalaparser/SyntaxTest.scala b/scalatexApi/src/test/scala/scalaparser/SyntaxTest.scala
index a7ceead..607bb55 100644
--- a/scalatexApi/src/test/scala/scalaparser/SyntaxTest.scala
+++ b/scalatexApi/src/test/scala/scalaparser/SyntaxTest.scala
@@ -268,7 +268,41 @@ object SyntaxTest extends TestSuite{
|}
""".stripMargin
)
-
+ * - check(
+ """
+ |object L{
+ | x match{
+ | case y.Y(z) => z
+ | }
+ |}
+ """.stripMargin
+ )
+ * - check(
+ """object K{
+ | val a: B {
+ | val c: D
+ | }
+ |
+ | 1
+ |}
+ """.stripMargin
+ )
+ * - check(
+ """
+ |object LOLS{
+ | def run() {}
+ |
+ | def apply() {}
+ |}
+ """.stripMargin
+ )
+ * - check(
+ """
+ |object O{
+ | a =:= b.c
+ |}
+ """.stripMargin
+ )
}
def checkFile(path: String) = check(io.Source.fromFile(path).mkString)
'file{