From 95fe19545d60ae9aa79d2c8d5665c7f3b0cbdb1a Mon Sep 17 00:00:00 2001 From: Den Shabalin Date: Fri, 6 Sep 2013 18:28:44 +0200 Subject: 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 --- test/files/run/macro-parse-position-malformed.check | 1 + .../macro-parse-position-malformed/Impls_Macros_1.scala | 17 +++++++++++++++++ .../run/macro-parse-position-malformed/Test_2.scala | 3 +++ test/files/run/macro-parse-position.check | 5 +++++ .../files/run/macro-parse-position/Impls_Macros_1.scala | 12 ++++++++++++ test/files/run/macro-parse-position/Test_2.scala | 3 +++ test/files/run/reify_copypaste1.scala | 2 +- 7 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/files/run/macro-parse-position-malformed.check create mode 100644 test/files/run/macro-parse-position-malformed/Impls_Macros_1.scala create mode 100644 test/files/run/macro-parse-position-malformed/Test_2.scala create mode 100644 test/files/run/macro-parse-position.check create mode 100644 test/files/run/macro-parse-position/Impls_Macros_1.scala create mode 100644 test/files/run/macro-parse-position/Test_2.scala (limited to 'test') 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-,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-,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) -- cgit v1.2.3