Skip to content

Commit

Permalink
Parser sync with scalac
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Apr 27, 2012
1 parent 0349365 commit 179f4b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* FIX: Bug with line-per-annotation style
* Add support for String interpolation (2.10)
* Add support for macros
* Add --scalaVersion=<version> flag to command-line tool

0.1.1 (18/October/11)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class InferredSemicolonScalaParser(tokens: Array[Token]) {
accept(RBRACE)
}

def dropAnyBraces[T](body: Any) =
if (LBRACE)
inBraces(body)
else
body

def inBrackets[T](body: T) {
accept(LBRACKET)
body
Expand All @@ -61,10 +67,14 @@ class InferredSemicolonScalaParser(tokens: Array[Token]) {
}

def scriptBody() {
templateStatSeq()
templateStats()
accept(EOF)
}

private def templateStats() = {
templateStatSeq()
}

private def accept(tokenType: TokenType): Token =
if (currentTokenType == tokenType)
nextToken()
Expand Down Expand Up @@ -393,7 +403,7 @@ class InferredSemicolonScalaParser(tokens: Array[Token]) {
while (STRING_PART) {
nextToken()
if (inPattern)
pattern()
dropAnyBraces(pattern())
else if (isIdent)
ident()
else
Expand Down Expand Up @@ -1111,17 +1121,30 @@ class InferredSemicolonScalaParser(tokens: Array[Token]) {
}
} else {
ident()
typeParamClauseOpt(allowVariance = false)
paramClauses()
newLineOptWhenFollowedBy(LBRACE)
typedOpt()
if (isStatSep || RBRACE || EOF /* for our tests */ )
None
else if (LBRACE) { // TODO: check cond
blockExpr()
} else {
equalsExpr()
funDefRest()
}
}

private def funDefRest() {
typeParamClauseOpt(allowVariance = false)
paramClauses()
newLineOptWhenFollowedBy(LBRACE)
typedOpt()
if (isStatSep || RBRACE || EOF /* for our tests */ )
None
else if (LBRACE) { // TODO: check cond
blockExpr()
} else {
if (!EQUALS) {
accept(EQUALS)
throw new AssertionError("Will not reach here")
}
nextToken()
if (VARID && currentToken.text == "macro")
Some(nextToken())
else
None
expr()
}
}

Expand Down
17 changes: 14 additions & 3 deletions scalariform/src/main/scala/scalariform/parser/ScalaParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class ScalaParser(tokens: Array[Token]) {
(openToken, contents, closeToken)
}

def dropAnyBraces[T](body: Expr): Expr =
if (LBRACE) {
val (lbrace, contents, rbrace) = inBraces(body)
makeExpr(lbrace, contents, rbrace)
} else
body

def inBrackets[T](body: T): (Token, T, Token) = {
val openToken = accept(LBRACKET)
val contents = body
Expand All @@ -57,11 +64,15 @@ class ScalaParser(tokens: Array[Token]) {
}

def scriptBody(): CompilationUnit = {
val stmts = templateStatSeq()
val stmts = templateStats()
accept(EOF)
CompilationUnit(stmts)
}

private def templateStats() = {
templateStatSeq()
}

private def accept(tokenType: TokenType): Token =
if (currentTokenType == tokenType)
nextToken()
Expand Down Expand Up @@ -428,7 +439,7 @@ class ScalaParser(tokens: Array[Token]) {
val stringPart = nextToken()
val scalaSegment: Expr =
if (inPattern)
pattern()
dropAnyBraces(pattern())
else if (isIdent)
makeExpr(ident())
else
Expand Down Expand Up @@ -1383,7 +1394,7 @@ class ScalaParser(tokens: Array[Token]) {
Some(nextToken())
else
None
val expr_ = expr()
val expr_ = expr()
Some(ExprFunBody(equalsToken, macroTokenOpt, expr_))
}
FunDefOrDcl(defToken, nameToken, typeParamClauseOpt_, paramClauses_, returnTypeOpt, funBodyOpt, localDef)
Expand Down

0 comments on commit 179f4b9

Please sign in to comment.