diff options
author | Li Haoyi <haoyi@dropbox.com> | 2014-11-16 19:11:32 -0800 |
---|---|---|
committer | Li Haoyi <haoyi@dropbox.com> | 2014-11-16 19:11:32 -0800 |
commit | db544916874accf7d2a90f6f6d95a30cf88588c6 (patch) | |
tree | 46f6da4af1d372ba2913f84ab84ed5628e3645e9 /scalatexApi/src/main | |
parent | 50ecd448fbd32b15c78076abf305b1ac05503be3 (diff) | |
download | hands-on-scala-js-db544916874accf7d2a90f6f6d95a30cf88588c6.tar.gz hands-on-scala-js-db544916874accf7d2a90f6f6d95a30cf88588c6.tar.bz2 hands-on-scala-js-db544916874accf7d2a90f6f6d95a30cf88588c6.zip |
Added more file-tests, renamed to scalaparser
Diffstat (limited to 'scalatexApi/src/main')
-rw-r--r-- | scalatexApi/src/main/scala/scalaparser/ScalaSyntax.scala (renamed from scalatexApi/src/main/scala/torimatomeru/ScalaSyntax.scala) | 38 | ||||
-rw-r--r-- | scalatexApi/src/main/scala/scalaparser/syntax/Basic.scala (renamed from scalatexApi/src/main/scala/torimatomeru/syntax/Basic.scala) | 2 | ||||
-rw-r--r-- | scalatexApi/src/main/scala/scalaparser/syntax/Identifiers.scala (renamed from scalatexApi/src/main/scala/torimatomeru/syntax/Identifiers.scala) | 2 | ||||
-rw-r--r-- | scalatexApi/src/main/scala/scalaparser/syntax/Literals.scala (renamed from scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala) | 16 | ||||
-rw-r--r-- | scalatexApi/src/main/scala/scalatex/stages/Parser.scala | 2 |
5 files changed, 28 insertions, 32 deletions
diff --git a/scalatexApi/src/main/scala/torimatomeru/ScalaSyntax.scala b/scalatexApi/src/main/scala/scalaparser/ScalaSyntax.scala index 7538ec6..0e183a1 100644 --- a/scalatexApi/src/main/scala/torimatomeru/ScalaSyntax.scala +++ b/scalatexApi/src/main/scala/scalaparser/ScalaSyntax.scala @@ -1,9 +1,25 @@ -package torimatomeru +package scalaparser import acyclic.file import language.implicitConversions import syntax._ import org.parboiled2._ +/** + * Parser for Scala syntax. + * + * The `G` parameter that gets passed in to each rule stands for + * "Greedy", and determines whether or not that rule is to consume + * newlines after the last terminal in that rule. We need to pass it + * everywhere so it can go all the way to the last terminal deep + * inside the parse tree, which can then decide whether or not to + * consume whitespace. + * + * The vast majority of terminals will consume newlines; only rules + * which occur in {} blocks won't have their terminals consume newlines. + * That's why the parser does terminals-consume-newlines-by-default, + * and leaves it up to the dev to thread the `G` variable where-ever + * we want the opposite behavior. + */ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identifiers with Literals { // Aliases for common things. These things are used in almost every parser // in the file, so it makes sense to keep them short. @@ -54,23 +70,12 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif */ def pr(s: String) = rule { run(println(s"LOGGING $cursor: $s")) } - ////////////////////////////////////////////////// - // Override rules from dependencies - // in order to handle white spaces - // Note: when you add your AST, make sure to - // only capture super.rule and not the whitespace - ////////////////////////////////////////////////// - def Id(G: B = t) = rule { Identifiers.Id ~ W(G) } def VarId(G: B = t) = rule { Identifiers.VarId ~ W(G) } def Literal(G: B = t) = rule { Literals.Literal ~ W(G) } def Semi = rule { Basic.Semi ~ WL } def Newline = rule { Basic.Newline ~ WL } - /////////////////////////////////////////// - // Qualifiers and Ids - /////////////////////////////////////////// - def QualId(G: B = t) = rule { oneOrMore(Id(false)) separatedBy '.' ~ W(G) } def Ids = rule { oneOrMore(Id()) separatedBy ',' } @@ -85,10 +90,6 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif def ClassQualifier = rule { '[' ~ Id() ~ ']' } - /////////////////////////////////////////// - // Types and more Types - /////////////////////////////////////////// - def Type: R0 = rule { FunctionArgTypes ~ "=>" ~ Type | InfixType ~ optional(ExistentialClause) } def FunctionArgTypes = rule { InfixType | '(' ~ optional(oneOrMore(ParamType) separatedBy ',') ~ ')' } @@ -122,10 +123,6 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif def ParamType = rule { "=>" ~ Type | Type ~ "*" | Type } - ///////////////////////////////////////////////// - // Declarations, Expressions and Pattern Matching - ///////////////////////////////////////////////// - def Expr(G: B = t): R0 = rule { (Bindings | optional("implicit") ~ Id() | "_") ~ "=>" ~ Expr(G) | Expr1(G) } def Expr1(G: B = t): R0 = rule { IfCFlow(G) | @@ -254,7 +251,6 @@ class ScalaSyntax(val input: ParserInput) extends Parser with Basic with Identif def Import(G: B = t): R0 = rule { "import" ~ oneOrMore(ImportExpr(G)).separatedBy(',') } - //ImportExpr is slightly changed wrt spec because StableId always consumes all the Ids possible, so there is no need to one at the end def ImportExpr(G: B = t): R0 = rule { StableId(G) ~ optional('.' ~ (StrW("_", G) | ImportSelectors(G))) } def ImportSelectors(G: B = t): R0 = rule { '{' ~ zeroOrMore(ImportSelector ~ ',') ~ (ImportSelector | '_') ~ StrW("}", G) } def ImportSelector: R0 = rule { Id() ~ optional("=>" ~ (Id() | '_')) } diff --git a/scalatexApi/src/main/scala/torimatomeru/syntax/Basic.scala b/scalatexApi/src/main/scala/scalaparser/syntax/Basic.scala index 18cff91..bd142a1 100644 --- a/scalatexApi/src/main/scala/torimatomeru/syntax/Basic.scala +++ b/scalatexApi/src/main/scala/scalaparser/syntax/Basic.scala @@ -1,4 +1,4 @@ -package torimatomeru +package scalaparser package syntax import acyclic.file import org.parboiled2._ diff --git a/scalatexApi/src/main/scala/torimatomeru/syntax/Identifiers.scala b/scalatexApi/src/main/scala/scalaparser/syntax/Identifiers.scala index 4be1d4e..e985f92 100644 --- a/scalatexApi/src/main/scala/torimatomeru/syntax/Identifiers.scala +++ b/scalatexApi/src/main/scala/scalaparser/syntax/Identifiers.scala @@ -1,4 +1,4 @@ -package torimatomeru +package scalaparser package syntax import acyclic.file import org.parboiled2._ diff --git a/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala b/scalatexApi/src/main/scala/scalaparser/syntax/Literals.scala index b199c30..11eb943 100644 --- a/scalatexApi/src/main/scala/torimatomeru/syntax/Literals.scala +++ b/scalatexApi/src/main/scala/scalaparser/syntax/Literals.scala @@ -1,4 +1,4 @@ -package torimatomeru +package scalaparser package syntax import acyclic.file import org.parboiled2._ @@ -27,11 +27,11 @@ trait Literals { self: Parser with Basic with Identifiers => def Literal = rule { (capture(optional("-")) ~ (FloatingPointLiteral | IntegerLiteral) ~> ((sign: String, number) => sign + number)) | - BooleanLiteral | - CharacterLiteral | - StringLiteral | - SymbolLiteral | - capture("null") + BooleanLiteral | + CharacterLiteral | + StringLiteral | + SymbolLiteral | + capture("null") } @@ -43,8 +43,8 @@ trait Literals { self: Parser with Basic with Identifiers => def MultiLineChars = rule { zeroOrMore(optional('"') ~ optional('"') ~ noneOf("\"")) } def StringLiteral = rule { - ("\"\"\"" ~ capture(MultiLineChars) ~ capture("\"\"\"" ~ zeroOrMore('"')) ~> ((multilineChars: String, quotes) => multilineChars + quotes.dropRight(3))) | - ('"' ~ capture(zeroOrMore("\\\"" | noneOf("\n\""))) ~ '"') + (optional(Identifiers.Id) ~ "\"\"\"" ~ capture(MultiLineChars) ~ capture("\"\"\"" ~ zeroOrMore('"')) ~> ((multilineChars: String, quotes) => multilineChars + quotes.dropRight(3))) | + (optional(Identifiers.Id) ~ '"' ~ capture(zeroOrMore("\\\"" | noneOf("\n\""))) ~ '"') } def isPrintableChar(c: Char): Boolean = { diff --git a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala index 117806d..403cb72 100644 --- a/scalatexApi/src/main/scala/scalatex/stages/Parser.scala +++ b/scalatexApi/src/main/scala/scalatex/stages/Parser.scala @@ -2,7 +2,7 @@ package scalatex package stages import acyclic.file import org.parboiled2._ -import torimatomeru.ScalaSyntax +import scalaparser.ScalaSyntax /** * Parses the input text into a roughly-structured AST. This AST |