summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDen Shabalin <den.shabalin@gmail.com>2013-09-06 18:28:44 +0200
committerDen Shabalin <den.shabalin@gmail.com>2013-09-12 12:43:23 +0200
commit95fe19545d60ae9aa79d2c8d5665c7f3b0cbdb1a (patch)
tree7848cd767ac44c0e6e5790553d29afac9439a030
parent5607bd137d8a22c6933e3692a4a1626928acf67f (diff)
downloadscala-95fe19545d60ae9aa79d2c8d5665c7f3b0cbdb1a.tar.gz
scala-95fe19545d60ae9aa79d2c8d5665c7f3b0cbdb1a.tar.bz2
scala-95fe19545d60ae9aa79d2c8d5665c7f3b0cbdb1a.zip
SI-6489 parsing in macros should provide proper positions
1. macro parsing doesn't use toolbox any more but calls parser directly 2. in order for this to work parser has to be refactored to limit usage of currentUnit and rewire it into parser's local unit method which might use currentUnit for some parsers but will user proper unit for UnitParser 3. similar change has to be done to make compilation unit's reporter overridable
-rw-r--r--src/compiler/scala/reflect/macros/contexts/Parsers.scala28
-rw-r--r--src/compiler/scala/tools/nsc/CompilationUnits.scala5
-rw-r--r--src/compiler/scala/tools/nsc/Global.scala24
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/Parsers.scala28
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala11
-rw-r--r--src/compiler/scala/tools/nsc/reporters/StoreReporter.scala2
-rw-r--r--src/compiler/scala/tools/reflect/ToolBoxFactory.scala10
-rw-r--r--src/compiler/scala/tools/reflect/quasiquotes/Parsers.scala10
-rw-r--r--src/reflect/scala/reflect/internal/TreeGen.scala6
-rw-r--r--test/files/run/macro-parse-position-malformed.check1
-rw-r--r--test/files/run/macro-parse-position-malformed/Impls_Macros_1.scala17
-rw-r--r--test/files/run/macro-parse-position-malformed/Test_2.scala3
-rw-r--r--test/files/run/macro-parse-position.check5
-rw-r--r--test/files/run/macro-parse-position/Impls_Macros_1.scala12
-rw-r--r--test/files/run/macro-parse-position/Test_2.scala3
-rw-r--r--test/files/run/reify_copypaste1.scala2
16 files changed, 113 insertions, 54 deletions
diff --git a/src/compiler/scala/reflect/macros/contexts/Parsers.scala b/src/compiler/scala/reflect/macros/contexts/Parsers.scala
index 3dab02beba..ae6488b5a8 100644
--- a/src/compiler/scala/reflect/macros/contexts/Parsers.scala
+++ b/src/compiler/scala/reflect/macros/contexts/Parsers.scala
@@ -1,24 +1,20 @@
package scala.reflect.macros
package contexts
-import scala.language.existentials
-import scala.tools.reflect.ToolBox
-import scala.tools.reflect.ToolBoxError
+import scala.tools.nsc.reporters.StoreReporter
trait Parsers {
self: Context =>
+ import global._
- def parse(code: String): Tree =
- // todo. provide decent implementation
- // see `Typers.typedUseCase` for details
- try {
- import scala.reflect.runtime.{universe => ru}
- val parsed = ru.rootMirror.mkToolBox().parse(code)
- val importer = universe.mkImporter(ru)
- importer.importTree(parsed)
- } catch {
- case ToolBoxError(msg, cause) =>
- // todo. provide a position
- throw new ParseException(universe.NoPosition, msg)
+ def parse(code: String) = {
+ val sreporter = new StoreReporter()
+ val unit = new CompilationUnit(newSourceFile(code, "<macro>")) { override def reporter = sreporter }
+ val parser = newUnitParser(unit)
+ val tree = gen.mkTreeOrBlock(parser.parseStats())
+ sreporter.infos.foreach {
+ case sreporter.Info(pos, msg, sreporter.ERROR) => throw ParseException(pos, msg)
}
-}
+ tree
+ }
+} \ No newline at end of file
diff --git a/src/compiler/scala/tools/nsc/CompilationUnits.scala b/src/compiler/scala/tools/nsc/CompilationUnits.scala
index f7437e4e6c..1de5c1f626 100644
--- a/src/compiler/scala/tools/nsc/CompilationUnits.scala
+++ b/src/compiler/scala/tools/nsc/CompilationUnits.scala
@@ -9,8 +9,9 @@ import util.FreshNameCreator
import scala.reflect.internal.util.{ SourceFile, NoSourceFile }
import scala.collection.mutable
import scala.collection.mutable.{ LinkedHashSet, ListBuffer }
+import scala.tools.nsc.reporters.Reporter
-trait CompilationUnits { self: Global =>
+trait CompilationUnits { global: Global =>
/** An object representing a missing compilation unit.
*/
@@ -119,6 +120,8 @@ trait CompilationUnits { self: Global =>
*/
val icode: LinkedHashSet[icodes.IClass] = new LinkedHashSet
+ def reporter = global.reporter
+
def echo(pos: Position, msg: String) =
reporter.echo(pos, msg)
diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala
index 0e3b2993c7..8b9ef2178b 100644
--- a/src/compiler/scala/tools/nsc/Global.scala
+++ b/src/compiler/scala/tools/nsc/Global.scala
@@ -110,7 +110,10 @@ class Global(var currentSettings: Settings, var reporter: Reporter)
}
/** A spare instance of TreeBuilder left for backwards compatibility. */
- lazy val treeBuilder: TreeBuilder { val global: Global.this.type } = new syntaxAnalyzer.ParserTreeBuilder
+ lazy val treeBuilder: TreeBuilder { val global: Global.this.type } = new UnitTreeBuilder {
+ val global: Global.this.type = Global.this;
+ val unit = currentUnit
+ }
/** Fold constants */
object constfold extends {
@@ -1139,11 +1142,20 @@ class Global(var currentSettings: Settings, var reporter: Reporter)
warning("there were %d %s warning(s); re-run with %s for details".format(warnings.size, what, option.name))
}
- def newCompilationUnit(code: String) = new CompilationUnit(newSourceFile(code))
- def newSourceFile(code: String) = new BatchSourceFile("<console>", code)
- def newUnitScanner(unit: CompilationUnit): UnitScanner = new UnitScanner(unit)
- def newUnitParser(unit: CompilationUnit): UnitParser = new UnitParser(unit)
- def newUnitParser(code: String): UnitParser = newUnitParser(newCompilationUnit(code))
+ def newSourceFile(code: String, filename: String = "<console>") =
+ new BatchSourceFile(filename, code)
+
+ def newCompilationUnit(code: String, filename: String = "<console>") =
+ new CompilationUnit(newSourceFile(code, filename))
+
+ def newUnitScanner(unit: CompilationUnit): UnitScanner =
+ new UnitScanner(unit)
+
+ def newUnitParser(unit: CompilationUnit): UnitParser =
+ new UnitParser(unit)
+
+ def newUnitParser(code: String, filename: String = "<console>"): UnitParser =
+ newUnitParser(newCompilationUnit(code, filename))
/** A Run is a single execution of the compiler on a sets of units
*/
diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
index 52aa11cb40..f3b842b170 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
@@ -28,20 +28,14 @@ import util.FreshNameCreator
*/
trait ParsersCommon extends ScannersCommon { self =>
val global : Global
- import global._
+ // the use of currentUnit in the parser should be avoided as it might
+ // cause unexpected behaviour when you work with two units at the
+ // same time; use Parser.unit instead
+ import global.{currentUnit => _, _}
def newLiteral(const: Any) = Literal(Constant(const))
def literalUnit = newLiteral(())
- class ParserTreeBuilder extends TreeBuilder {
- val global: self.global.type = self.global
- def freshName(prefix: String): Name = freshTermName(prefix)
- def freshTermName(prefix: String): TermName = currentUnit.freshTermName(prefix)
- def freshTypeName(prefix: String): TypeName = currentUnit.freshTypeName(prefix)
- def o2p(offset: Int): Position = new OffsetPosition(currentUnit.source, offset)
- def r2p(start: Int, mid: Int, end: Int): Position = rangePos(currentUnit.source, start, mid, end)
- }
-
/** This is now an abstract class, only to work around the optimizer:
* methods in traits are never inlined.
*/
@@ -172,6 +166,7 @@ self =>
private val globalFresh = new FreshNameCreator.Default
+ def unit = global.currentUnit
def freshName(prefix: String): Name = freshTermName(prefix)
def freshTermName(prefix: String): TermName = newTermName(globalFresh.newName(prefix))
def freshTypeName(prefix: String): TypeName = newTypeName(globalFresh.newName(prefix))
@@ -196,7 +191,7 @@ self =>
* that we don't have the xml library on the compilation classpath.
*/
private[this] lazy val xmlp = {
- currentUnit.encounteredXml(o2p(in.offset))
+ unit.encounteredXml(o2p(in.offset))
new MarkupParser(this, preserveWS = true)
}
@@ -225,7 +220,7 @@ self =>
override def templateBody(isPre: Boolean) = skipBraces((emptyValDef, EmptyTree.asList))
}
- class UnitParser(val unit: global.CompilationUnit, patches: List[BracePatch]) extends SourceFileParser(unit.source) {
+ class UnitParser(override val unit: global.CompilationUnit, patches: List[BracePatch]) extends SourceFileParser(unit.source) { uself =>
def this(unit: global.CompilationUnit) = this(unit, Nil)
override def newScanner() = new UnitScanner(unit, patches)
@@ -298,9 +293,10 @@ self =>
import nme.raw
- abstract class Parser extends ParserCommon {
+ abstract class Parser extends ParserCommon { parser =>
val in: Scanner
+ def unit: CompilationUnit
def freshName(prefix: String): Name
def freshTermName(prefix: String): TermName
def freshTypeName(prefix: String): TypeName
@@ -310,8 +306,12 @@ self =>
/** whether a non-continuable syntax error has been seen */
private var lastErrorOffset : Int = -1
+ class ParserTreeBuilder extends UnitTreeBuilder {
+ val global: self.global.type = self.global
+ def unit = parser.unit
+ }
val treeBuilder = new ParserTreeBuilder
- import treeBuilder.{global => _, _}
+ import treeBuilder.{global => _, unit => _, _}
/** The types of the context bounds of type parameters of the surrounding class
*/
diff --git a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala
index 28e3217449..d622031e9d 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala
@@ -8,6 +8,7 @@ package ast.parser
import symtab.Flags._
import scala.collection.mutable.ListBuffer
+import scala.reflect.internal.util.OffsetPosition
/** Methods for building trees, used in the parser. All the trees
* returned by this class must be untyped.
@@ -534,3 +535,13 @@ abstract class TreeBuilder {
}
}
}
+
+abstract class UnitTreeBuilder extends TreeBuilder {
+ import global._
+ def unit: CompilationUnit
+ def freshName(prefix: String): Name = freshTermName(prefix)
+ def freshTermName(prefix: String): TermName = unit.freshTermName(prefix)
+ def freshTypeName(prefix: String): TypeName = unit.freshTypeName(prefix)
+ def o2p(offset: Int): Position = new OffsetPosition(unit.source, offset)
+ def r2p(start: Int, mid: Int, end: Int): Position = rangePos(unit.source, start, mid, end)
+}
diff --git a/src/compiler/scala/tools/nsc/reporters/StoreReporter.scala b/src/compiler/scala/tools/nsc/reporters/StoreReporter.scala
index 34e2a8a96a..04c5bdf824 100644
--- a/src/compiler/scala/tools/nsc/reporters/StoreReporter.scala
+++ b/src/compiler/scala/tools/nsc/reporters/StoreReporter.scala
@@ -14,7 +14,7 @@ import scala.reflect.internal.util.Position
* console.
*/
class StoreReporter extends Reporter {
- class Info(val pos: Position, val msg: String, val severity: Severity) {
+ case class Info(pos: Position, msg: String, severity: Severity) {
override def toString() = "pos: " + pos + " " + msg + " " + severity
}
val infos = new mutable.LinkedHashSet[Info]
diff --git a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala
index 57ebe1b30d..ffbca7014a 100644
--- a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala
+++ b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala
@@ -273,15 +273,9 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf =>
def parse(code: String): Tree = {
reporter.reset()
- val file = new BatchSourceFile("<toolbox>", code)
- val unit = new CompilationUnit(file)
- val parsed = newUnitParser(unit).parseStats()
+ val tree = gen.mkTreeOrBlock(newUnitParser(code, "<toolbox>").parseStats())
throwIfErrors()
- parsed match {
- case Nil => EmptyTree
- case expr :: Nil => expr
- case stats :+ expr => Block(stats, expr)
- }
+ tree
}
def showAttributed(artifact: Any, printTypes: Boolean = true, printIds: Boolean = true, printKinds: Boolean = false): String = {
diff --git a/src/compiler/scala/tools/reflect/quasiquotes/Parsers.scala b/src/compiler/scala/tools/reflect/quasiquotes/Parsers.scala
index 19888fa8d2..5a1a25cfa1 100644
--- a/src/compiler/scala/tools/reflect/quasiquotes/Parsers.scala
+++ b/src/compiler/scala/tools/reflect/quasiquotes/Parsers.scala
@@ -49,7 +49,7 @@ trait Parsers { self: Quasiquotes =>
def entryPoint: QuasiquoteParser => Tree
- class QuasiquoteParser(source0: SourceFile) extends SourceFileParser(source0) {
+ class QuasiquoteParser(source0: SourceFile) extends SourceFileParser(source0) { parser =>
def isHole: Boolean = isIdent && isHole(in.name)
def isHole(name: Name): Boolean = holeMap.contains(name)
@@ -73,7 +73,7 @@ trait Parsers { self: Quasiquotes =>
override def makeFunctionTypeTree(argtpes: List[Tree], restpe: Tree): Tree =
AppliedTypeTree(Ident(tpnme.QUASIQUOTE_FUNCTION), argtpes :+ restpe)
}
- import treeBuilder.{global => _, _}
+ import treeBuilder.{global => _, unit => _, _}
// q"def foo($x)"
override def allowTypelessParams = true
@@ -144,11 +144,7 @@ trait Parsers { self: Quasiquotes =>
}
object TermParser extends Parser {
- def entryPoint = _.templateStats() match {
- case Nil => EmptyTree
- case tree :: Nil => tree
- case stats :+ tree => Block(stats, tree)
- }
+ def entryPoint = { parser => gen.mkTreeOrBlock(parser.templateStats()) }
}
object TypeParser extends Parser {
diff --git a/src/reflect/scala/reflect/internal/TreeGen.scala b/src/reflect/scala/reflect/internal/TreeGen.scala
index 07fa6fb317..26adf20c52 100644
--- a/src/reflect/scala/reflect/internal/TreeGen.scala
+++ b/src/reflect/scala/reflect/internal/TreeGen.scala
@@ -437,4 +437,10 @@ abstract class TreeGen extends macros.TreeBuilder {
else if (!stats.last.isTerm) Block(stats, Literal(Constant(())))
else if (stats.length == 1) stats.head
else Block(stats.init, stats.last)
+
+ def mkTreeOrBlock(stats: List[Tree]) = stats match {
+ case Nil => EmptyTree
+ case head :: Nil => head
+ case _ => gen.mkBlock(stats)
+ }
}
diff --git a/test/files/run/macro-parse-position-malformed.check b/test/files/run/macro-parse-position-malformed.check
new file mode 100644
index 0000000000..00f0bc5b62
--- /dev/null
+++ b/test/files/run/macro-parse-position-malformed.check
@@ -0,0 +1 @@
+failed with 'source-<macro>,line-1,offset=7' position and '')' expected but eof found.' message
diff --git a/test/files/run/macro-parse-position-malformed/Impls_Macros_1.scala b/test/files/run/macro-parse-position-malformed/Impls_Macros_1.scala
new file mode 100644
index 0000000000..2417eb6897
--- /dev/null
+++ b/test/files/run/macro-parse-position-malformed/Impls_Macros_1.scala
@@ -0,0 +1,17 @@
+import scala.language.experimental.macros
+import scala.reflect.macros.{Context => Ctx, ParseException}
+
+object Macros {
+ def impl(c: Ctx)() = {
+ import c.universe._
+ val out = try {
+ c.parse("foo(bar")
+ "didn't fail"
+ } catch {
+ case e: ParseException =>
+ s"failed with '${e.pos}' position and '${e.msg}' message"
+ }
+ c.Expr[String](Literal(Constant(out)))
+ }
+ def foo(): String = macro impl
+} \ No newline at end of file
diff --git a/test/files/run/macro-parse-position-malformed/Test_2.scala b/test/files/run/macro-parse-position-malformed/Test_2.scala
new file mode 100644
index 0000000000..cff569bd81
--- /dev/null
+++ b/test/files/run/macro-parse-position-malformed/Test_2.scala
@@ -0,0 +1,3 @@
+object Test extends App {
+ println(Macros.foo)
+}
diff --git a/test/files/run/macro-parse-position.check b/test/files/run/macro-parse-position.check
new file mode 100644
index 0000000000..3da0320696
--- /dev/null
+++ b/test/files/run/macro-parse-position.check
@@ -0,0 +1,5 @@
+false
+source-<macro>,line-1,offset=4
+8
+foo bar
+
diff --git a/test/files/run/macro-parse-position/Impls_Macros_1.scala b/test/files/run/macro-parse-position/Impls_Macros_1.scala
new file mode 100644
index 0000000000..b6f1ebbcd5
--- /dev/null
+++ b/test/files/run/macro-parse-position/Impls_Macros_1.scala
@@ -0,0 +1,12 @@
+import scala.language.experimental.macros
+import scala.reflect.macros.{Context => Ctx}
+
+object Macros {
+ def impl(c: Ctx)() = {
+ import c.universe._
+ val t = c.parse("foo bar")
+ val out = s"${t.pos == NoPosition}\n${t.pos}\n${t.pos.source.content.length}\n${new String(t.pos.source.content)}"
+ c.Expr[String](Literal(Constant(out)))
+ }
+ def foo(): String = macro impl
+} \ No newline at end of file
diff --git a/test/files/run/macro-parse-position/Test_2.scala b/test/files/run/macro-parse-position/Test_2.scala
new file mode 100644
index 0000000000..cff569bd81
--- /dev/null
+++ b/test/files/run/macro-parse-position/Test_2.scala
@@ -0,0 +1,3 @@
+object Test extends App {
+ println(Macros.foo)
+}
diff --git a/test/files/run/reify_copypaste1.scala b/test/files/run/reify_copypaste1.scala
index b2eef28026..cf813182ae 100644
--- a/test/files/run/reify_copypaste1.scala
+++ b/test/files/run/reify_copypaste1.scala
@@ -12,7 +12,7 @@ object Test extends App {
val reify = Select(Select(Select(Select(Ident(ScalaPackage), TermName("reflect")), TermName("runtime")), TermName("universe")), TermName("reify"))
val reifee = Block(List(ValDef(Modifiers(LAZY), TermName("x"), TypeTree(), Apply(Ident(ListModule), List(Literal(Constant(1)), Literal(Constant(2)))))), Ident(TermName("x")))
toolBox.eval(Apply(reify, List(reifee)))
- val Block(List(tpeCopypaste), exprCopypaste @ ModuleDef(_, _, Template(_, _, (_ :: stats) :+ expr))) = toolBox.parse(output.toString())
+ val Block(List(tpeCopypaste, exprCopypaste @ ModuleDef(_, _, Template(_, _, (_ :: stats) :+ expr))), Literal(Constant(()))) = toolBox.parse(output.toString())
output.reset()
toolBox.eval(Block(stats, expr))
stdout.println(output.toString)