From d9103e01b467634d61baa3b131aeb93fca9e20a5 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Mon, 4 Jun 2012 13:53:13 +0200 Subject: moves positions to scala.reflect.internal.util This is the first step of factoring out scala-reflect.jar. --- src/compiler/scala/reflect/internal/Kinds.scala | 2 +- .../scala/reflect/internal/Positions.scala | 4 +- .../scala/reflect/internal/SymbolTable.scala | 1 - src/compiler/scala/reflect/internal/Types.scala | 2 +- .../scala/reflect/internal/util/Position.scala | 277 ++++++++++++++++++++ .../scala/reflect/internal/util/SourceFile.scala | 161 ++++++++++++ .../scala/reflect/internal/util/StringOps.scala | 99 ++++++++ .../scala/reflect/internal/util/TableDef.scala | 94 +++++++ .../scala/reflect/internal/util/WeakHashSet.scala | 61 +++++ .../makro/runtime/AbortMacroException.scala | 2 +- .../scala/reflect/runtime/JavaUniverse.scala | 2 +- .../scala/tools/nsc/CompilationUnits.scala | 3 +- src/compiler/scala/tools/nsc/CompileServer.scala | 2 +- src/compiler/scala/tools/nsc/CompileSocket.scala | 2 +- src/compiler/scala/tools/nsc/Driver.scala | 2 +- src/compiler/scala/tools/nsc/Global.scala | 3 +- src/compiler/scala/tools/nsc/Main.scala | 2 +- src/compiler/scala/tools/nsc/Phases.scala | 2 +- src/compiler/scala/tools/nsc/ScalaDoc.scala | 2 +- src/compiler/scala/tools/nsc/ast/DocComments.scala | 2 +- src/compiler/scala/tools/nsc/ast/Positions.scala | 4 +- .../scala/tools/nsc/ast/parser/MarkupParsers.scala | 3 +- .../scala/tools/nsc/ast/parser/Parsers.scala | 5 +- .../scala/tools/nsc/ast/parser/Scanners.scala | 3 +- .../tools/nsc/ast/parser/SymbolicXMLBuilder.scala | 2 +- .../tools/nsc/backend/icode/BasicBlocks.scala | 2 +- .../scala/tools/nsc/backend/icode/Members.scala | 2 +- .../scala/tools/nsc/backend/icode/Opcodes.scala | 2 +- .../scala/tools/nsc/backend/icode/Printers.scala | 2 +- .../scala/tools/nsc/backend/jvm/GenJVM.scala | 2 +- .../scala/tools/nsc/backend/opt/Inliners.scala | 4 +- .../nsc/dependencies/DependencyAnalysis.scala | 2 +- src/compiler/scala/tools/nsc/doc/DocFactory.scala | 2 +- src/compiler/scala/tools/nsc/doc/DocParser.scala | 2 +- .../scala/tools/nsc/doc/model/TreeFactory.scala | 2 +- .../nsc/doc/model/comment/CommentFactory.scala | 2 +- .../scala/tools/nsc/interactive/BuildManager.scala | 2 +- .../tools/nsc/interactive/CompilerControl.scala | 3 +- .../scala/tools/nsc/interactive/ContextTrees.scala | 2 +- .../scala/tools/nsc/interactive/Global.scala | 3 +- .../nsc/interactive/InteractiveReporter.scala | 2 +- .../scala/tools/nsc/interactive/Picklers.scala | 6 +- .../scala/tools/nsc/interactive/REPL.scala | 4 +- .../tools/nsc/interactive/RangePositions.scala | 3 +- .../nsc/interactive/RefinedBuildManager.scala | 3 +- .../nsc/interactive/RichCompilationUnits.scala | 2 +- .../tools/nsc/interactive/ScratchPadMaker.scala | 2 +- .../tools/nsc/interactive/SimpleBuildManager.scala | 2 +- .../nsc/interactive/tests/InteractiveTest.scala | 4 +- .../scala/tools/nsc/interactive/tests/Tester.scala | 2 +- .../nsc/interactive/tests/core/AskCommand.scala | 4 +- .../nsc/interactive/tests/core/CoreTestDefs.scala | 2 +- .../PresentationCompilerRequestsWorkingMode.scala | 4 +- .../tests/core/PresentationCompilerTestDef.scala | 2 +- .../interactive/tests/core/SourcesCollector.scala | 2 +- .../nsc/interactive/tests/core/TestResources.scala | 2 +- .../scala/tools/nsc/interpreter/ExprTyper.scala | 2 +- .../scala/tools/nsc/interpreter/ILoopInit.scala | 2 +- .../scala/tools/nsc/interpreter/IMain.scala | 3 +- .../scala/tools/nsc/interpreter/Power.scala | 2 +- .../scala/tools/nsc/interpreter/TypeStrings.scala | 2 +- src/compiler/scala/tools/nsc/io/AbstractFile.scala | 2 + src/compiler/scala/tools/nsc/io/Directory.scala | 3 + src/compiler/scala/tools/nsc/io/File.scala | 2 + .../scala/tools/nsc/io/NoAbstractFile.scala | 2 + src/compiler/scala/tools/nsc/io/Path.scala | 5 +- src/compiler/scala/tools/nsc/io/SourceReader.scala | 2 +- src/compiler/scala/tools/nsc/io/VirtualFile.scala | 2 + src/compiler/scala/tools/nsc/io/ZipArchive.scala | 2 + .../scala/tools/nsc/javac/JavaParsers.scala | 2 +- .../scala/tools/nsc/javac/JavaScanners.scala | 3 +- .../tools/nsc/matching/ParallelMatching.scala | 2 +- src/compiler/scala/tools/nsc/plugins/Plugin.scala | 5 +- .../tools/nsc/reporters/AbstractReporter.scala | 2 +- .../tools/nsc/reporters/ConsoleReporter.scala | 3 +- .../scala/tools/nsc/reporters/Reporter.scala | 9 +- .../scala/tools/nsc/reporters/StoreReporter.scala | 2 +- .../tools/nsc/scratchpad/SourceInserter.scala | 2 +- .../scala/tools/nsc/settings/MutableSettings.scala | 7 +- .../scala/tools/nsc/symtab/BrowsingLoaders.scala | 2 +- .../scala/tools/nsc/symtab/clr/CLRTypes.scala | 2 +- .../tools/nsc/transform/SpecializeTypes.scala | 4 +- .../tools/nsc/typechecker/ContextErrors.scala | 2 +- .../tools/nsc/typechecker/MethodSynthesis.scala | 2 +- .../tools/nsc/typechecker/NamesDefaults.scala | 2 +- .../scala/tools/nsc/typechecker/Typers.scala | 2 +- src/compiler/scala/tools/nsc/util/ClassPath.scala | 2 +- .../scala/tools/nsc/util/Exceptional.scala | 2 +- .../scala/tools/nsc/util/FreshNameCreator.scala | 4 +- src/compiler/scala/tools/nsc/util/Position.scala | 278 --------------------- src/compiler/scala/tools/nsc/util/SourceFile.scala | 162 ------------ src/compiler/scala/tools/nsc/util/TableDef.scala | 95 ------- .../scala/tools/nsc/util/WeakHashSet.scala | 61 ----- src/compiler/scala/tools/nsc/util/package.scala | 53 ++++ src/compiler/scala/tools/reflect/FrontEnds.scala | 2 +- .../scala/tools/reflect/ToolBoxFactory.scala | 6 +- src/compiler/scala/tools/util/StringOps.scala | 100 -------- 97 files changed, 874 insertions(+), 797 deletions(-) create mode 100644 src/compiler/scala/reflect/internal/util/Position.scala create mode 100644 src/compiler/scala/reflect/internal/util/SourceFile.scala create mode 100644 src/compiler/scala/reflect/internal/util/StringOps.scala create mode 100644 src/compiler/scala/reflect/internal/util/TableDef.scala create mode 100644 src/compiler/scala/reflect/internal/util/WeakHashSet.scala delete mode 100644 src/compiler/scala/tools/nsc/util/Position.scala delete mode 100644 src/compiler/scala/tools/nsc/util/SourceFile.scala delete mode 100644 src/compiler/scala/tools/nsc/util/TableDef.scala delete mode 100644 src/compiler/scala/tools/nsc/util/WeakHashSet.scala delete mode 100644 src/compiler/scala/tools/util/StringOps.scala (limited to 'src') diff --git a/src/compiler/scala/reflect/internal/Kinds.scala b/src/compiler/scala/reflect/internal/Kinds.scala index eca63c7c15..b736a9192f 100644 --- a/src/compiler/scala/reflect/internal/Kinds.scala +++ b/src/compiler/scala/reflect/internal/Kinds.scala @@ -7,7 +7,7 @@ package scala.reflect package internal import scala.collection.{ mutable, immutable } -import scala.tools.util.StringOps.{ countAsString, countElementsAsString } +import scala.reflect.internal.util.StringOps.{ countAsString, countElementsAsString } trait Kinds { self: SymbolTable => diff --git a/src/compiler/scala/reflect/internal/Positions.scala b/src/compiler/scala/reflect/internal/Positions.scala index bbd8880e35..6ae9b40fcb 100644 --- a/src/compiler/scala/reflect/internal/Positions.scala +++ b/src/compiler/scala/reflect/internal/Positions.scala @@ -3,8 +3,8 @@ package internal trait Positions extends api.Positions { self: SymbolTable => - type Position = scala.tools.nsc.util.Position - val NoPosition = scala.tools.nsc.util.NoPosition + type Position = scala.reflect.internal.util.Position + val NoPosition = scala.reflect.internal.util.NoPosition implicit val PositionTag = ClassTag[Position](classOf[Position]) /** A position that wraps a set of trees. diff --git a/src/compiler/scala/reflect/internal/SymbolTable.scala b/src/compiler/scala/reflect/internal/SymbolTable.scala index cf6db7d438..cadd76b1ba 100644 --- a/src/compiler/scala/reflect/internal/SymbolTable.scala +++ b/src/compiler/scala/reflect/internal/SymbolTable.scala @@ -8,7 +8,6 @@ package internal import scala.collection.{ mutable, immutable } import util._ -import scala.tools.nsc.util.WeakHashSet abstract class SymbolTable extends makro.Universe with Collections diff --git a/src/compiler/scala/reflect/internal/Types.scala b/src/compiler/scala/reflect/internal/Types.scala index ada998bf9e..23921d73cc 100644 --- a/src/compiler/scala/reflect/internal/Types.scala +++ b/src/compiler/scala/reflect/internal/Types.scala @@ -5995,7 +5995,7 @@ trait Types extends api.Types { self: SymbolTable => // Lubs and Glbs --------------------------------------------------------- private def printLubMatrix(btsMap: Map[Type, List[Type]], depth: Int) { - import scala.tools.nsc.util.TableDef + import util.TableDef import TableDef.Column def str(tp: Type) = { if (tp == NoType) "" diff --git a/src/compiler/scala/reflect/internal/util/Position.scala b/src/compiler/scala/reflect/internal/util/Position.scala new file mode 100644 index 0000000000..3c251b3b31 --- /dev/null +++ b/src/compiler/scala/reflect/internal/util/Position.scala @@ -0,0 +1,277 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2011 LAMP/EPFL + * @author Martin Odersky + * + */ + +package scala.reflect.internal.util + +import reflect.ClassTag +import reflect.base.Attachments +import reflect.api.PositionApi + +object Position { + val tabInc = 8 + + /** Prints the message with the given position indication. */ + def formatMessage(posIn: Position, msg: String, shortenFile: Boolean): String = { + val pos = ( + if (posIn eq null) NoPosition + else if (posIn.isDefined) posIn.inUltimateSource(posIn.source) + else posIn + ) + def file = pos.source.file + def prefix = if (shortenFile) file.name else file.path + + pos match { + case FakePos(fmsg) => fmsg+" "+msg + case NoPosition => msg + case _ => + List( + "%s:%s: %s".format(prefix, pos.line, msg), + pos.lineContent.stripLineEnd, + " " * (pos.column - 1) + "^" + ) mkString "\n" + } + } +} + +abstract class Position extends PositionApi { self => + + type Pos = Position + + def pos: Position = this + + def withPos(newPos: Position): Attachments { type Pos = self.Pos } = newPos + + /** Java file corresponding to the source file of this position. + */ + // necessary for conformance with scala.reflect.api.Position + def fileInfo: java.io.File = source.file.file + + /** Contents of the source file that contains this position. + */ + // necessary for conformance with scala.reflect.api.Position + def fileContent: Array[Char] = source.content + + /** An optional value containing the source file referred to by this position, or + * None if not defined. + */ + def source: SourceFile = throw new UnsupportedOperationException("Position.source") + + /** Is this position neither a NoPosition nor a FakePosition? + * If isDefined is true, offset and source are both defined. + */ + def isDefined: Boolean = false + + /** Is this position a transparent position? */ + def isTransparent: Boolean = false + + /** Is this position a range position? */ + def isRange: Boolean = false + + /** Is this position a non-transparent range position? */ + def isOpaqueRange: Boolean = false + + /** if opaque range, make this position transparent */ + def makeTransparent: Position = this + + /** The start of the position's range, error if not a range position */ + def start: Int = throw new UnsupportedOperationException("Position.start") + + /** The start of the position's range, or point if not a range position */ + def startOrPoint: Int = point + + /** The point (where the ^ is) of the position */ + def point: Int = throw new UnsupportedOperationException("Position.point") + + /** The point (where the ^ is) of the position, or else `default` if undefined */ + def pointOrElse(default: Int): Int = default + + /** The end of the position's range, error if not a range position */ + def end: Int = throw new UnsupportedOperationException("Position.end") + + /** The end of the position's range, or point if not a range position */ + def endOrPoint: Int = point + + @deprecated("use point instead", "2.9.0") + def offset: Option[Int] = if (isDefined) Some(point) else None + + /** The same position with a different start value (if a range) */ + def withStart(off: Int): Position = this + + /** The same position with a different end value (if a range) */ + def withEnd(off: Int): Position = this + + /** The same position with a different point value (if a range or offset) */ + def withPoint(off: Int): Position = this + + /** The same position with a different source value, and its values shifted by given offset */ + def withSource(source: SourceFile, shift: Int): Position = this + + /** If this is a range, the union with the other range, with the point of this position. + * Otherwise, this position + */ + def union(pos: Position): Position = this + + /** If this is a range position, the offset position of its start. + * Otherwise the position itself + */ + def focusStart: Position = this + + /** If this is a range position, the offset position of its point. + * Otherwise the position itself + */ + def focus: Position = this + + /** If this is a range position, the offset position of its end. + * Otherwise the position itself + */ + def focusEnd: Position = this + + /** Does this position include the given position `pos`. + * This holds if `this` is a range position and its range [start..end] + * is the same or covers the range of the given position, which may or may not be a range position. + */ + def includes(pos: Position): Boolean = false + + /** Does this position properly include the given position `pos` ("properly" meaning their + * ranges are not the same)? + */ + def properlyIncludes(pos: Position): Boolean = + includes(pos) && (start < pos.startOrPoint || pos.endOrPoint < end) + + /** Does this position precede that position? + * This holds if both positions are defined and the end point of this position + * is not larger than the start point of the given position. + */ + def precedes(pos: Position): Boolean = + isDefined && pos.isDefined && endOrPoint <= pos.startOrPoint + + /** Does this position properly precede the given position `pos` ("properly" meaning their ranges + * do not share a common point). + */ + def properlyPrecedes(pos: Position): Boolean = + isDefined && pos.isDefined && endOrPoint < pos.startOrPoint + + /** Does this position overlap with that position? + * This holds if both positions are ranges and there is an interval of + * non-zero length that is shared by both position ranges. + */ + def overlaps(pos: Position): Boolean = + isRange && pos.isRange && + ((pos.start < end && start < pos.end) || (start < pos.end && pos.start < end)) + + /** Does this position cover the same range as that position? + * Holds only if both position are ranges + */ + def sameRange(pos: Position): Boolean = + isRange && pos.isRange && start == pos.start && end == pos.end + + def line: Int = throw new UnsupportedOperationException("Position.line") + + def column: Int = throw new UnsupportedOperationException("Position.column") + + /** Convert this to a position around `point` that spans a single source line */ + def toSingleLine: Position = this + + def lineContent: String = + if (isDefined) source.lineToString(line - 1) + else "NO_LINE" + + /** Map this position to a position in an original source + * file. If the SourceFile is a normal SourceFile, simply + * return this. + */ + def inUltimateSource(source : SourceFile): Position = + if (source == null) this else source.positionInUltimateSource(this) + + def dbgString: String = toString + def safeLine: Int = try line catch { case _: UnsupportedOperationException => -1 } + + def show: String = "["+toString+"]" +} + +case object NoPosition extends Position { + override def dbgString = toString +} + +case class FakePos(msg: String) extends Position { + override def toString = msg +} + +class OffsetPosition(override val source: SourceFile, override val point: Int) extends Position { + override def isDefined = true + override def pointOrElse(default: Int): Int = point + override def withPoint(off: Int) = new OffsetPosition(source, off) + override def withSource(source: SourceFile, shift: Int) = new OffsetPosition(source, point + shift) + + override def line: Int = source.offsetToLine(point) + 1 + + override def column: Int = { + var idx = source.lineToOffset(source.offsetToLine(point)) + var col = 0 + while (idx != point) { + col += (if (source.content(idx) == '\t') Position.tabInc - col % Position.tabInc else 1) + idx += 1 + } + col + 1 + } + + override def union(pos: Position) = if (pos.isRange) pos else this + + override def equals(that : Any) = that match { + case that : OffsetPosition => point == that.point && source.file == that.source.file + case that => false + } + override def hashCode = point * 37 + source.file.hashCode + + override def toString = { + val pointmsg = if (point > source.length) "out-of-bounds-" else "offset=" + "source-%s,line-%s,%s%s".format(source.file.canonicalPath, line, pointmsg, point) + } + override def show = "["+point+"]" +} + +/** new for position ranges */ +class RangePosition(source: SourceFile, override val start: Int, point: Int, override val end: Int) +extends OffsetPosition(source, point) { + if (start > end) assert(false, "bad position: "+show) + override def isRange: Boolean = true + override def isOpaqueRange: Boolean = true + override def startOrPoint: Int = start + override def endOrPoint: Int = end + override def withStart(off: Int) = new RangePosition(source, off, point, end) + override def withEnd(off: Int) = new RangePosition(source, start, point, off) + override def withPoint(off: Int) = new RangePosition(source, start, off, end) + override def withSource(source: SourceFile, shift: Int) = new RangePosition(source, start + shift, point + shift, end + shift) + override def focusStart = new OffsetPosition(source, start) + override def focus = { + if (focusCache eq NoPosition) focusCache = new OffsetPosition(source, point) + focusCache + } + override def focusEnd = new OffsetPosition(source, end) + override def makeTransparent = new TransparentPosition(source, start, point, end) + override def includes(pos: Position) = pos.isDefined && start <= pos.startOrPoint && pos.endOrPoint <= end + override def union(pos: Position): Position = + if (pos.isRange) new RangePosition(source, start min pos.start, point, end max pos.end) else this + + override def toSingleLine: Position = source match { + case bs: BatchSourceFile + if end > 0 && bs.offsetToLine(start) < bs.offsetToLine(end - 1) => + val pointLine = bs.offsetToLine(point) + new RangePosition(source, bs.lineToOffset(pointLine), point, bs.lineToOffset(pointLine + 1)) + case _ => this + } + + override def toString = "RangePosition("+source.file.canonicalPath+", "+start+", "+point+", "+end+")" + override def show = "["+start+":"+end+"]" + private var focusCache: Position = NoPosition +} + +class TransparentPosition(source: SourceFile, start: Int, point: Int, end: Int) extends RangePosition(source, start, point, end) { + override def isOpaqueRange: Boolean = false + override def isTransparent = true + override def makeTransparent = this + override def show = "<"+start+":"+end+">" +} \ No newline at end of file diff --git a/src/compiler/scala/reflect/internal/util/SourceFile.scala b/src/compiler/scala/reflect/internal/util/SourceFile.scala new file mode 100644 index 0000000000..7c80ddd37d --- /dev/null +++ b/src/compiler/scala/reflect/internal/util/SourceFile.scala @@ -0,0 +1,161 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2011 LAMP/EPFL + * @author Martin Odersky + */ + + +package scala.reflect.internal.util + +import scala.tools.nsc.io.{ AbstractFile, VirtualFile } +import scala.collection.mutable.ArrayBuffer +import annotation.tailrec +import java.util.regex.Pattern +import java.io.IOException +import scala.reflect.internal.Chars._ + +/** abstract base class of a source file used in the compiler */ +abstract class SourceFile { + def content : Array[Char] // normalized, must end in SU + def file : AbstractFile + def isLineBreak(idx : Int) : Boolean + def isSelfContained: Boolean + def length : Int + def position(offset: Int) : Position = { + assert(offset < length, file + ": " + offset + " >= " + length) + new OffsetPosition(this, offset) + } + def position(line: Int, column: Int) : Position = new OffsetPosition(this, lineToOffset(line) + column) + + def offsetToLine(offset: Int): Int + def lineToOffset(index : Int): Int + + /** Map a position to a position in the underlying source file. + * For regular source files, simply return the argument. + */ + def positionInUltimateSource(position: Position) = position + override def toString() = file.name + def dbg(offset: Int) = (new OffsetPosition(this, offset)).dbgString + def path = file.path + + def beginsWith(offset: Int, text: String): Boolean = + (content drop offset) startsWith text + + def lineToString(index: Int): String = + content drop lineToOffset(index) takeWhile (c => !isLineBreakChar(c.toChar)) mkString + + @tailrec + final def skipWhitespace(offset: Int): Int = + if (content(offset).isWhitespace) skipWhitespace(offset + 1) else offset + + def identifier(pos: Position): Option[String] = None +} + +/** An object representing a missing source file. + */ +object NoSourceFile extends SourceFile { + def content = Array() + def file = NoFile + def isLineBreak(idx: Int) = false + def isSelfContained = true + def length = -1 + def offsetToLine(offset: Int) = -1 + def lineToOffset(index : Int) = -1 + override def toString = "" +} + +object NoFile extends VirtualFile("", "") + +object ScriptSourceFile { + /** Length of the script header from the given content, if there is one. + * The header begins with "#!" or "::#!" and ends with a line starting + * with "!#" or "::!#". + */ + def headerLength(cs: Array[Char]): Int = { + val headerPattern = Pattern.compile("""((?m)^(::)?!#.*|^.*/env .*)(\r|\n|\r\n)""") + val headerStarts = List("#!", "::#!") + + if (headerStarts exists (cs startsWith _)) { + val matcher = headerPattern matcher cs.mkString + if (matcher.find) matcher.end + else throw new IOException("script file does not close its header with !# or ::!#") + } + else 0 + } + def stripHeader(cs: Array[Char]): Array[Char] = cs drop headerLength(cs) + + def apply(file: AbstractFile, content: Array[Char]) = { + val underlying = new BatchSourceFile(file, content) + val headerLen = headerLength(content) + val stripped = new ScriptSourceFile(underlying, content drop headerLen, headerLen) + + stripped + } +} +import ScriptSourceFile._ + +class ScriptSourceFile(underlying: BatchSourceFile, content: Array[Char], override val start: Int) extends BatchSourceFile(underlying.file, content) { + override def isSelfContained = false + + override def positionInUltimateSource(pos: Position) = + if (!pos.isDefined) super.positionInUltimateSource(pos) + else new OffsetPosition(underlying, pos.point + start) +} + +/** a file whose contents do not change over time */ +class BatchSourceFile(val file : AbstractFile, val content: Array[Char]) extends SourceFile { + + def this(_file: AbstractFile) = this(_file, _file.toCharArray) + def this(sourceName: String, cs: Seq[Char]) = this(new VirtualFile(sourceName), cs.toArray) + def this(file: AbstractFile, cs: Seq[Char]) = this(file, cs.toArray) + + override def equals(that : Any) = that match { + case that : BatchSourceFile => file.path == that.file.path && start == that.start + case _ => false + } + override def hashCode = file.path.## + start.## + val length = content.length + def start = 0 + def isSelfContained = true + + override def identifier(pos: Position) = + if (pos.isDefined && pos.source == this && pos.point != -1) { + def isOK(c: Char) = isIdentifierPart(c) || isOperatorPart(c) + Some(new String(content drop pos.point takeWhile isOK)) + } else { + super.identifier(pos) + } + + def isLineBreak(idx: Int) = + if (idx >= length) false else { + val ch = content(idx) + // don't identify the CR in CR LF as a line break, since LF will do. + if (ch == CR) (idx + 1 == length) || (content(idx + 1) != LF) + else isLineBreakChar(ch) + } + + def calculateLineIndices(cs: Array[Char]) = { + val buf = new ArrayBuffer[Int] + buf += 0 + for (i <- 0 until cs.length) if (isLineBreak(i)) buf += i + 1 + buf += cs.length // sentinel, so that findLine below works smoother + buf.toArray + } + private lazy val lineIndices: Array[Int] = calculateLineIndices(content) + + def lineToOffset(index : Int): Int = lineIndices(index) + + private var lastLine = 0 + + /** Convert offset to line in this source file + * Lines are numbered from 0 + */ + def offsetToLine(offset: Int): Int = { + val lines = lineIndices + def findLine(lo: Int, hi: Int, mid: Int): Int = + if (offset < lines(mid)) findLine(lo, mid - 1, (lo + mid - 1) / 2) + else if (offset >= lines(mid + 1)) findLine(mid + 1, hi, (mid + 1 + hi) / 2) + else mid + lastLine = findLine(0, lines.length, lastLine) + lastLine + } +} diff --git a/src/compiler/scala/reflect/internal/util/StringOps.scala b/src/compiler/scala/reflect/internal/util/StringOps.scala new file mode 100644 index 0000000000..281ade8134 --- /dev/null +++ b/src/compiler/scala/reflect/internal/util/StringOps.scala @@ -0,0 +1,99 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + +package scala.reflect.internal.util + +/** This object provides utility methods to extract elements + * from Strings. + * + * @author Martin Odersky + * @version 1.0 + */ +trait StringOps { + def onull(s: String) = if (s == null) "" else s + def oempty(xs: String*) = xs filterNot (x => x == null || x == "") + def ojoin(xs: String*): String = oempty(xs: _*) mkString " " + def ojoin(xs: Seq[String], sep: String): String = oempty(xs: _*) mkString sep + def ojoinOr(xs: Seq[String], sep: String, orElse: String) = { + val ys = oempty(xs: _*) + if (ys.isEmpty) orElse else ys mkString sep + } + def trimTrailingSpace(s: String) = { + if (s.length == 0 || !s.charAt(s.length - 1).isWhitespace) s + else { + var idx = s.length - 1 + while (idx >= 0 && s.charAt(idx).isWhitespace) + idx -= 1 + + s.substring(0, idx + 1) + } + } + + def decompose(str: String, sep: Char): List[String] = { + def ws(start: Int): List[String] = + if (start == str.length) List() + else if (str.charAt(start) == sep) ws(start + 1) + else { + val end = str.indexOf(sep, start) + if (end < 0) List(str.substring(start)) + else str.substring(start, end) :: ws(end + 1) + } + ws(0) + } + + def words(str: String): List[String] = decompose(str, ' ') + + def stripPrefixOpt(str: String, prefix: String): Option[String] = + if (str startsWith prefix) Some(str drop prefix.length) + else None + + def stripSuffixOpt(str: String, suffix: String): Option[String] = + if (str endsWith suffix) Some(str dropRight suffix.length) + else None + + def splitWhere(str: String, f: Char => Boolean, doDropIndex: Boolean = false): Option[(String, String)] = + splitAt(str, str indexWhere f, doDropIndex) + + def splitAt(str: String, idx: Int, doDropIndex: Boolean = false): Option[(String, String)] = + if (idx == -1) None + else Some((str take idx, str drop (if (doDropIndex) idx + 1 else idx))) + + /** Returns a string meaning "n elements". + * + * @param n ... + * @param elements ... + * @return ... + */ + def countElementsAsString(n: Int, elements: String): String = + n match { + case 0 => "no " + elements + "s" + case 1 => "one " + elements + case 2 => "two " + elements + "s" + case 3 => "three " + elements + "s" + case 4 => "four " + elements + "s" + case _ => "" + n + " " + elements + "s" + } + + /** Turns a count into a friendly English description if n<=4. + * + * @param n ... + * @return ... + */ + def countAsString(n: Int): String = + n match { + case 0 => "none" + case 1 => "one" + case 2 => "two" + case 3 => "three" + case 4 => "four" + case _ => "" + n + } +} + +object StringOps extends StringOps { } diff --git a/src/compiler/scala/reflect/internal/util/TableDef.scala b/src/compiler/scala/reflect/internal/util/TableDef.scala new file mode 100644 index 0000000000..d692a6d8f5 --- /dev/null +++ b/src/compiler/scala/reflect/internal/util/TableDef.scala @@ -0,0 +1,94 @@ +package scala.reflect.internal.util + +import TableDef._ + +/** A class for representing tabular data in a way that preserves + * its inner beauty. See Exceptional for an example usage. + * One creates an instance of TableDef by defining the columns of + * the table, then uses that to create an instance of Table by + * passing in a sequence of rows. + */ +class TableDef[T](_cols: Column[T]*) { + /** These operators are about all there is to it. + * + * ~ appends a column to the table + * >> creates a right-justified column and appends it + * << creates a left-justified column and appends it + * >+ specifies a string to separate the previous column from the next. + * if none is specified, a space is used. + */ + def ~(next: Column[T]) = retThis(cols :+= next) + def >>(pair: (String, T => Any)) = this ~ Column(pair._1, pair._2, false) + def <<(pair: (String, T => Any)) = this ~ Column(pair._1, pair._2, true) + def >+(sep: String) = retThis(separators += ((cols.size - 1, sep))) + + /** Below this point should all be considered private/internal. + */ + private var cols: List[Column[T]] = _cols.toList + private var separators: Map[Int, String] = Map() + + def defaultSep(index: Int) = if (index > (cols.size - 2)) "" else " " + def sepAfter(i: Int): String = separators.getOrElse(i, defaultSep(i)) + def sepWidths = cols.indices map (i => sepAfter(i).length) + + def columns = cols + def colNames = cols map (_.name) + def colFunctions = cols map (_.f) + def colApply(el: T) = colFunctions map (f => f(el)) + def retThis(body: => Unit): this.type = { body ; this } + + class Table(val rows: Seq[T]) extends Seq[T] { + def iterator = rows.iterator + def apply(index: Int) = rows(index) + def length = rows.length + + def maxColWidth(col: Column[T]) = col.name +: (rows map col.f) map (_.toString.length) max + def specs = cols map (_ formatSpec rows) + + val colWidths = cols map maxColWidth + val rowFormat = mkFormatString(sepAfter) + val headFormat = mkFormatString(i => " " * sepWidths(i)) + val argLists = rows map colApply + + val headers = List( + headFormat.format(colNames: _*), + (colWidths, sepWidths).zipped map ((w1, w2) => "-" * w1 + " " * w2) mkString + ) + + def mkFormatString(sepf: Int => String): String = + specs.zipWithIndex map { case (c, i) => c + sepf(i) } mkString + + def pp(): Unit = allToSeq foreach println + + def toFormattedSeq = argLists map (xs => rowFormat.format(xs: _*)) + def allToSeq = headers ++ toFormattedSeq + + override def toString = allToSeq mkString "\n" + } + + def formatterFor(rows: Seq[T]): T => String = { + val formatStr = new Table(rows).rowFormat + + x => formatStr.format(colApply(x) : _*) + } + + def table(rows: Seq[T]) = new Table(rows) + + override def toString = cols.mkString("TableDef(", ", ", ")") +} + +object TableDef { + case class Column[-T](name: String, f: T => Any, left: Boolean) { + def maxWidth(elems: Seq[T]): Int = name +: (elems map f) map (_.toString.length) max + def formatSpec(elems: Seq[T]): String = { + val justify = if (left) "-" else "" + "%" + justify + maxWidth(elems) + "s" + } + override def toString = { + val justify = if (left) "<<" else ">>" + justify + "(" + name + ")" + } + } + + def apply[T](cols: Column[T]*) = new TableDef[T](cols: _*) +} diff --git a/src/compiler/scala/reflect/internal/util/WeakHashSet.scala b/src/compiler/scala/reflect/internal/util/WeakHashSet.scala new file mode 100644 index 0000000000..9882aad5e5 --- /dev/null +++ b/src/compiler/scala/reflect/internal/util/WeakHashSet.scala @@ -0,0 +1,61 @@ +package scala.reflect.internal.util + +import scala.collection.mutable +import scala.collection.mutable.ArrayBuffer +import scala.collection.mutable.Builder +import scala.collection.mutable.SetBuilder +import scala.collection.generic.Clearable +import scala.runtime.AbstractFunction1 + +/** A bare-bones implementation of a mutable `Set` that uses weak references + * to hold the elements. + * + * This implementation offers only add/remove/test operations, + * therefore it does not fulfill the contract of Scala collection sets. + */ +class WeakHashSet[T <: AnyRef] extends AbstractFunction1[T, Boolean] with Clearable { + private val underlying = mutable.HashSet[WeakReferenceWithEquals[T]]() + + /** Add the given element to this set. */ + def +=(elem: T): this.type = { + underlying += new WeakReferenceWithEquals(elem) + this + } + + /** Remove the given element from this set. */ + def -=(elem: T): this.type = { + underlying -= new WeakReferenceWithEquals(elem) + this + } + + /** Does the given element belong to this set? */ + def contains(elem: T): Boolean = + underlying.contains(new WeakReferenceWithEquals(elem)) + + /** Does the given element belong to this set? */ + def apply(elem: T): Boolean = contains(elem) + + /** Return the number of elements in this set, including reclaimed elements. */ + def size = underlying.size + + /** Remove all elements in this set. */ + def clear() = underlying.clear() +} + +/** A WeakReference implementation that implements equals and hashCode by + * delegating to the referent. + */ +class WeakReferenceWithEquals[T <: AnyRef](ref: T) { + def get(): T = underlying.get() + + override val hashCode = ref.hashCode + + override def equals(other: Any): Boolean = other match { + case wf: WeakReferenceWithEquals[_] => + underlying.get() == wf.get() + case _ => + false + } + + private val underlying = new java.lang.ref.WeakReference(ref) +} diff --git a/src/compiler/scala/reflect/makro/runtime/AbortMacroException.scala b/src/compiler/scala/reflect/makro/runtime/AbortMacroException.scala index d0d40d6f42..a68910859d 100644 --- a/src/compiler/scala/reflect/makro/runtime/AbortMacroException.scala +++ b/src/compiler/scala/reflect/makro/runtime/AbortMacroException.scala @@ -1,6 +1,6 @@ package scala.reflect.makro package runtime -import scala.tools.nsc.util.Position +import scala.reflect.internal.util.Position class AbortMacroException(val pos: Position, val msg: String) extends Throwable(msg) diff --git a/src/compiler/scala/reflect/runtime/JavaUniverse.scala b/src/compiler/scala/reflect/runtime/JavaUniverse.scala index 9dcf8786c5..d4a83b960d 100644 --- a/src/compiler/scala/reflect/runtime/JavaUniverse.scala +++ b/src/compiler/scala/reflect/runtime/JavaUniverse.scala @@ -14,7 +14,7 @@ class JavaUniverse extends internal.SymbolTable with ReflectSetup with runtime.S type TreeGen = internal.TreeGen - override type Position = scala.tools.nsc.util.Position + override type Position = scala.reflect.internal.util.Position override val gen = new TreeGen { val global: self.type = self } diff --git a/src/compiler/scala/tools/nsc/CompilationUnits.scala b/src/compiler/scala/tools/nsc/CompilationUnits.scala index 2f1e15168a..54b114bf82 100644 --- a/src/compiler/scala/tools/nsc/CompilationUnits.scala +++ b/src/compiler/scala/tools/nsc/CompilationUnits.scala @@ -5,7 +5,8 @@ package scala.tools.nsc -import util.{ FreshNameCreator, Position, NoPosition, BatchSourceFile, SourceFile, NoSourceFile } +import util.FreshNameCreator +import scala.reflect.internal.util.{ Position, NoPosition, BatchSourceFile, SourceFile, NoSourceFile } import scala.collection.mutable import scala.collection.mutable.{ LinkedHashSet, ListBuffer } diff --git a/src/compiler/scala/tools/nsc/CompileServer.scala b/src/compiler/scala/tools/nsc/CompileServer.scala index 6393ade146..fd59319c14 100644 --- a/src/compiler/scala/tools/nsc/CompileServer.scala +++ b/src/compiler/scala/tools/nsc/CompileServer.scala @@ -7,7 +7,7 @@ package scala.tools.nsc import java.io.{ BufferedOutputStream, FileOutputStream, PrintStream } import scala.tools.nsc.reporters.{Reporter, ConsoleReporter} -import scala.tools.nsc.util.FakePos //Position +import scala.reflect.internal.util.FakePos //Position import scala.tools.util.SocketServer import settings.FscSettings diff --git a/src/compiler/scala/tools/nsc/CompileSocket.scala b/src/compiler/scala/tools/nsc/CompileSocket.scala index a4a8e1fd11..7d2abd42fd 100644 --- a/src/compiler/scala/tools/nsc/CompileSocket.scala +++ b/src/compiler/scala/tools/nsc/CompileSocket.scala @@ -13,7 +13,7 @@ import java.security.SecureRandom import io.{ File, Path, Directory, Socket } import scala.util.control.Exception.catching import scala.tools.util.CompileOutputCommon -import scala.tools.util.StringOps.splitWhere +import scala.reflect.internal.util.StringOps.splitWhere import scala.sys.process._ trait HasCompileSocket { diff --git a/src/compiler/scala/tools/nsc/Driver.scala b/src/compiler/scala/tools/nsc/Driver.scala index 0c52954a0b..0051c3bdec 100644 --- a/src/compiler/scala/tools/nsc/Driver.scala +++ b/src/compiler/scala/tools/nsc/Driver.scala @@ -2,7 +2,7 @@ package scala.tools.nsc import scala.tools.nsc.reporters.{Reporter, ConsoleReporter} import Properties.{ versionString, copyrightString } -import scala.tools.nsc.util.{ BatchSourceFile, FakePos } +import scala.reflect.internal.util.{ BatchSourceFile, FakePos } abstract class Driver { diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index 89298d3d2c..787c9c7f57 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -12,7 +12,8 @@ import scala.tools.util.PathResolver import scala.collection.{ mutable, immutable } import io.{ SourceReader, AbstractFile, Path } import reporters.{ Reporter, ConsoleReporter } -import util.{ NoPosition, Exceptional, ClassPath, MergedClassPath, SourceFile, NoSourceFile, Statistics, StatisticsInfo, BatchSourceFile, ScriptSourceFile, ScalaClassLoader, returning } +import util.{ Exceptional, ClassPath, MergedClassPath, Statistics, StatisticsInfo, ScalaClassLoader, returning } +import scala.reflect.internal.util.{ NoPosition, SourceFile, NoSourceFile, BatchSourceFile, ScriptSourceFile } import scala.reflect.internal.pickling.{ PickleBuffer, PickleFormat } import settings.{ AestheticSettings } import symtab.{ Flags, SymbolTable, SymbolLoaders, SymbolTrackers } diff --git a/src/compiler/scala/tools/nsc/Main.scala b/src/compiler/scala/tools/nsc/Main.scala index 4763fab45c..19c872b6d3 100644 --- a/src/compiler/scala/tools/nsc/Main.scala +++ b/src/compiler/scala/tools/nsc/Main.scala @@ -11,7 +11,7 @@ import File.pathSeparator import scala.tools.nsc.interactive.{ RefinedBuildManager, SimpleBuildManager } import scala.tools.nsc.io.AbstractFile import scala.tools.nsc.reporters.{Reporter, ConsoleReporter} -import scala.tools.nsc.util.{ BatchSourceFile, FakePos } //{Position} +import scala.reflect.internal.util.{ BatchSourceFile, FakePos } //{Position} import Properties.{ versionString, copyrightString, residentPromptString, msilLibPath } /** The main class for NSC, a compiler for the programming diff --git a/src/compiler/scala/tools/nsc/Phases.scala b/src/compiler/scala/tools/nsc/Phases.scala index aa0ea1bdd8..896fcb3ca1 100644 --- a/src/compiler/scala/tools/nsc/Phases.scala +++ b/src/compiler/scala/tools/nsc/Phases.scala @@ -6,7 +6,7 @@ package scala.tools.nsc import symtab.Flags -import util.TableDef +import reflect.internal.util.TableDef import language.postfixOps object Phases { diff --git a/src/compiler/scala/tools/nsc/ScalaDoc.scala b/src/compiler/scala/tools/nsc/ScalaDoc.scala index 4fa2cc71e5..5a4b4172c6 100644 --- a/src/compiler/scala/tools/nsc/ScalaDoc.scala +++ b/src/compiler/scala/tools/nsc/ScalaDoc.scala @@ -9,7 +9,7 @@ package scala.tools.nsc import java.io.File.pathSeparator import scala.tools.nsc.doc.DocFactory import scala.tools.nsc.reporters.ConsoleReporter -import scala.tools.nsc.util.FakePos +import scala.reflect.internal.util.FakePos import Properties.msilLibPath /** The main class for scaladoc, a front-end for the Scala compiler diff --git a/src/compiler/scala/tools/nsc/ast/DocComments.scala b/src/compiler/scala/tools/nsc/ast/DocComments.scala index a01756b35b..316faba6e2 100755 --- a/src/compiler/scala/tools/nsc/ast/DocComments.scala +++ b/src/compiler/scala/tools/nsc/ast/DocComments.scala @@ -8,7 +8,7 @@ package ast import symtab._ import reporters._ -import util.{Position, NoPosition} +import scala.reflect.internal.util.{Position, NoPosition} import util.DocStrings._ import scala.reflect.internal.Chars._ import scala.collection.mutable diff --git a/src/compiler/scala/tools/nsc/ast/Positions.scala b/src/compiler/scala/tools/nsc/ast/Positions.scala index 83a67cfbe3..74d1f8ab4b 100644 --- a/src/compiler/scala/tools/nsc/ast/Positions.scala +++ b/src/compiler/scala/tools/nsc/ast/Positions.scala @@ -1,7 +1,7 @@ package scala.tools.nsc package ast -import scala.tools.nsc.util.{ SourceFile, Position, OffsetPosition, NoPosition } +import scala.reflect.internal.util.{ SourceFile, Position, OffsetPosition, NoPosition } trait Positions extends scala.reflect.internal.Positions { self: Global => @@ -14,7 +14,7 @@ trait Positions extends scala.reflect.internal.Positions { // [Eugene] disabling this for now. imo it doesn't justify pollution of the public API // override def _checkSetAnnotation(tree: Tree, annot: TreeAnnotation): Unit = { // if (tree.pos != NoPosition && tree.pos != annot.pos) debugwarn("Overwriting annotation "+ tree.annotation +" of tree "+ tree +" with annotation "+ annot) - // // if ((tree.annotation.isInstanceOf[scala.tools.nsc.util.Position] || !annot.isInstanceOf[scala.tools.nsc.util.Position]) && tree.isInstanceOf[Block]) + // // if ((tree.annotation.isInstanceOf[scala.reflect.internal.util.Position] || !annot.isInstanceOf[scala.reflect.internal.util.Position]) && tree.isInstanceOf[Block]) // // println("Updating block from "+ tree.annotation +" to "+ annot) // } diff --git a/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala b/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala index f702f44338..12b39d9e9c 100755 --- a/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala @@ -9,7 +9,8 @@ package ast.parser import scala.collection.mutable import mutable.{ Buffer, ArrayBuffer, ListBuffer } import scala.util.control.ControlThrowable -import scala.tools.nsc.util.{SourceFile,CharArrayReader} +import scala.tools.nsc.util.CharArrayReader +import scala.reflect.internal.util.SourceFile import scala.xml.{ Text, TextBuffer } import scala.xml.parsing.MarkupParserCommon import scala.xml.Utility.{ isNameStart, isNameChar, isSpace } diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index 688da4fd04..fd154fe796 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -10,10 +10,11 @@ package scala.tools.nsc package ast.parser import scala.collection.mutable.{ListBuffer, StringBuilder} -import util.{ SourceFile, OffsetPosition, FreshNameCreator } import scala.reflect.internal.{ ModifierFlags => Flags } -import Tokens._ import scala.reflect.internal.Chars.{ isScalaLetter } +import scala.reflect.internal.util.{ SourceFile, OffsetPosition } +import Tokens._ +import util.FreshNameCreator /** Historical note: JavaParsers started life as a direct copy of Parsers * but at a time when that Parsers had been replaced by a different one. diff --git a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala index 1835f645b8..6ba273b8ea 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala @@ -5,7 +5,8 @@ package scala.tools.nsc package ast.parser -import scala.tools.nsc.util._ +import scala.tools.nsc.util.CharArrayReader +import scala.reflect.internal.util._ import scala.reflect.internal.Chars._ import Tokens._ import scala.annotation.switch diff --git a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala index b0204c5971..a4b45482de 100755 --- a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala @@ -10,7 +10,7 @@ import scala.collection.{ mutable, immutable } import xml.{ EntityRef, Text } import xml.XML.{ xmlns } import symtab.Flags.MUTABLE -import scala.tools.util.StringOps.splitWhere +import scala.reflect.internal.util.StringOps.splitWhere import language.implicitConversions /** This class builds instance of `Tree` that represent XML. diff --git a/src/compiler/scala/tools/nsc/backend/icode/BasicBlocks.scala b/src/compiler/scala/tools/nsc/backend/icode/BasicBlocks.scala index b8ecaf1b43..86533c713e 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/BasicBlocks.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/BasicBlocks.scala @@ -9,7 +9,7 @@ package icode import scala.collection.{ mutable, immutable } import mutable.{ ListBuffer, ArrayBuffer } -import util.{ Position, NoPosition } +import scala.reflect.internal.util.{ Position, NoPosition } import backend.icode.analysis.ProgramPoint import language.postfixOps diff --git a/src/compiler/scala/tools/nsc/backend/icode/Members.scala b/src/compiler/scala/tools/nsc/backend/icode/Members.scala index efb4e7a199..00f4a9d262 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/Members.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/Members.scala @@ -9,7 +9,7 @@ package icode import java.io.PrintWriter import scala.collection.{ mutable, immutable } -import util.{ SourceFile, NoSourceFile } +import scala.reflect.internal.util.{ SourceFile, NoSourceFile } import symtab.Flags.{ DEFERRED } trait ReferenceEquality { diff --git a/src/compiler/scala/tools/nsc/backend/icode/Opcodes.scala b/src/compiler/scala/tools/nsc/backend/icode/Opcodes.scala index 3179fc5c56..5f495c8456 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/Opcodes.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/Opcodes.scala @@ -10,7 +10,7 @@ package backend package icode import scala.tools.nsc.ast._ -import scala.tools.nsc.util.{Position,NoPosition} +import scala.reflect.internal.util.{Position,NoPosition} /* A pattern match diff --git a/src/compiler/scala/tools/nsc/backend/icode/Printers.scala b/src/compiler/scala/tools/nsc/backend/icode/Printers.scala index 4ea253d29d..d34d93f147 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/Printers.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/Printers.scala @@ -9,7 +9,7 @@ package icode import java.io.PrintWriter import scala.tools.nsc.symtab.Flags -import scala.tools.nsc.util.Position +import scala.reflect.internal.util.Position trait Printers { self: ICodes => import global._ diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala index 747e7ef989..21260d399c 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala @@ -11,7 +11,7 @@ import java.nio.ByteBuffer import scala.collection.{ mutable, immutable } import scala.reflect.internal.pickling.{ PickleFormat, PickleBuffer } import scala.tools.nsc.symtab._ -import scala.tools.nsc.util.{ SourceFile, NoSourceFile } +import scala.reflect.internal.util.{ SourceFile, NoSourceFile } import scala.reflect.internal.ClassfileConstants._ import ch.epfl.lamp.fjbg._ import JAccessFlags._ diff --git a/src/compiler/scala/tools/nsc/backend/opt/Inliners.scala b/src/compiler/scala/tools/nsc/backend/opt/Inliners.scala index 08e059419a..f332e8cfdd 100644 --- a/src/compiler/scala/tools/nsc/backend/opt/Inliners.scala +++ b/src/compiler/scala/tools/nsc/backend/opt/Inliners.scala @@ -9,7 +9,7 @@ package backend.opt import scala.collection.mutable import scala.tools.nsc.symtab._ -import scala.tools.nsc.util.NoSourceFile +import scala.reflect.internal.util.NoSourceFile /** * @author Iulian Dragos @@ -100,7 +100,7 @@ abstract class Inliners extends SubComponent { } def isBottomType(sym: Symbol) = sym == NullClass || sym == NothingClass - def posToStr(pos: util.Position) = if (pos.isDefined) pos.point.toString else "" + def posToStr(pos: scala.reflect.internal.util.Position) = if (pos.isDefined) pos.point.toString else "" /** Is the given class a closure? */ def isClosureClass(cls: Symbol): Boolean = diff --git a/src/compiler/scala/tools/nsc/dependencies/DependencyAnalysis.scala b/src/compiler/scala/tools/nsc/dependencies/DependencyAnalysis.scala index 02be916f59..317cc28298 100644 --- a/src/compiler/scala/tools/nsc/dependencies/DependencyAnalysis.scala +++ b/src/compiler/scala/tools/nsc/dependencies/DependencyAnalysis.scala @@ -1,11 +1,11 @@ package scala.tools.nsc package dependencies -import util.SourceFile import io.Path import collection._ import symtab.Flags import scala.tools.nsc.io.AbstractFile +import scala.reflect.internal.util.SourceFile trait DependencyAnalysis extends SubComponent with Files { import global._ diff --git a/src/compiler/scala/tools/nsc/doc/DocFactory.scala b/src/compiler/scala/tools/nsc/doc/DocFactory.scala index 76a8b87ba7..e2e1ddf065 100644 --- a/src/compiler/scala/tools/nsc/doc/DocFactory.scala +++ b/src/compiler/scala/tools/nsc/doc/DocFactory.scala @@ -8,7 +8,7 @@ package doc import scala.util.control.ControlThrowable import reporters.Reporter -import util.{ NoPosition, BatchSourceFile} +import scala.reflect.internal.util.{ NoPosition, BatchSourceFile} import io.{ File, Directory } import DocParser.Parsed diff --git a/src/compiler/scala/tools/nsc/doc/DocParser.scala b/src/compiler/scala/tools/nsc/doc/DocParser.scala index 2bd80f31da..119a2c06e9 100644 --- a/src/compiler/scala/tools/nsc/doc/DocParser.scala +++ b/src/compiler/scala/tools/nsc/doc/DocParser.scala @@ -8,7 +8,7 @@ package nsc package doc import reporters._ -import util._ +import scala.reflect.internal.util._ import interactive.RangePositions import DocParser.Parsed diff --git a/src/compiler/scala/tools/nsc/doc/model/TreeFactory.scala b/src/compiler/scala/tools/nsc/doc/model/TreeFactory.scala index f948d53c8b..fe586c4996 100755 --- a/src/compiler/scala/tools/nsc/doc/model/TreeFactory.scala +++ b/src/compiler/scala/tools/nsc/doc/model/TreeFactory.scala @@ -3,7 +3,7 @@ package doc package model import scala.collection._ -import util.{RangePosition, OffsetPosition, SourceFile} +import scala.reflect.internal.util.{RangePosition, OffsetPosition, SourceFile} /** The goal of this trait is , using makeTree, * to browse a tree to diff --git a/src/compiler/scala/tools/nsc/doc/model/comment/CommentFactory.scala b/src/compiler/scala/tools/nsc/doc/model/comment/CommentFactory.scala index e6bc76f676..996223b9f9 100644 --- a/src/compiler/scala/tools/nsc/doc/model/comment/CommentFactory.scala +++ b/src/compiler/scala/tools/nsc/doc/model/comment/CommentFactory.scala @@ -12,7 +12,7 @@ import reporters.Reporter import scala.collection._ import scala.util.matching.Regex import scala.annotation.switch -import util.{NoPosition, Position} +import scala.reflect.internal.util.{NoPosition, Position} import language.postfixOps /** The comment parser transforms raw comment strings into `Comment` objects. diff --git a/src/compiler/scala/tools/nsc/interactive/BuildManager.scala b/src/compiler/scala/tools/nsc/interactive/BuildManager.scala index 0f89236861..7d36d17a18 100644 --- a/src/compiler/scala/tools/nsc/interactive/BuildManager.scala +++ b/src/compiler/scala/tools/nsc/interactive/BuildManager.scala @@ -9,7 +9,7 @@ package interactive import scala.collection._ import scala.tools.nsc.reporters.{Reporter, ConsoleReporter} -import util.FakePos +import scala.reflect.internal.util.FakePos import dependencies._ import io.AbstractFile diff --git a/src/compiler/scala/tools/nsc/interactive/CompilerControl.scala b/src/compiler/scala/tools/nsc/interactive/CompilerControl.scala index 1b91b06942..801b4ad22b 100644 --- a/src/compiler/scala/tools/nsc/interactive/CompilerControl.scala +++ b/src/compiler/scala/tools/nsc/interactive/CompilerControl.scala @@ -7,11 +7,12 @@ package interactive import scala.util.control.ControlThrowable import scala.tools.nsc.io.AbstractFile -import scala.tools.nsc.util.{SourceFile, Position, WorkScheduler} import scala.tools.nsc.symtab._ import scala.tools.nsc.ast._ import scala.tools.nsc.util.FailedInterrupt import scala.tools.nsc.util.EmptyAction +import scala.tools.nsc.util.WorkScheduler +import scala.reflect.internal.util.{SourceFile, Position} /** Interface of interactive compiler to a client such as an IDE * The model the presentation compiler consists of the following parts: diff --git a/src/compiler/scala/tools/nsc/interactive/ContextTrees.scala b/src/compiler/scala/tools/nsc/interactive/ContextTrees.scala index fc48d4819c..80d2796801 100644 --- a/src/compiler/scala/tools/nsc/interactive/ContextTrees.scala +++ b/src/compiler/scala/tools/nsc/interactive/ContextTrees.scala @@ -6,7 +6,7 @@ package scala.tools.nsc package interactive import collection.mutable.ArrayBuffer -import util.Position +import scala.reflect.internal.util.Position trait ContextTrees { self: Global => diff --git a/src/compiler/scala/tools/nsc/interactive/Global.scala b/src/compiler/scala/tools/nsc/interactive/Global.scala index edf0108e58..82ce59d075 100644 --- a/src/compiler/scala/tools/nsc/interactive/Global.scala +++ b/src/compiler/scala/tools/nsc/interactive/Global.scala @@ -11,7 +11,8 @@ import mutable.{LinkedHashMap, SynchronizedMap, HashSet, SynchronizedSet} import scala.concurrent.SyncVar import scala.util.control.ControlThrowable import scala.tools.nsc.io.{ AbstractFile, LogReplay, Logger, NullLogger, Replayer } -import scala.tools.nsc.util.{ SourceFile, BatchSourceFile, Position, RangePosition, NoPosition, WorkScheduler, MultiHashMap } +import scala.tools.nsc.util.{ WorkScheduler, MultiHashMap } +import scala.reflect.internal.util.{ SourceFile, BatchSourceFile, Position, RangePosition, NoPosition } import scala.tools.nsc.reporters._ import scala.tools.nsc.symtab._ import scala.tools.nsc.ast._ diff --git a/src/compiler/scala/tools/nsc/interactive/InteractiveReporter.scala b/src/compiler/scala/tools/nsc/interactive/InteractiveReporter.scala index 397e83a362..df8fc3b21e 100644 --- a/src/compiler/scala/tools/nsc/interactive/InteractiveReporter.scala +++ b/src/compiler/scala/tools/nsc/interactive/InteractiveReporter.scala @@ -6,7 +6,7 @@ package scala.tools.nsc package interactive import collection.mutable.ArrayBuffer -import util.Position +import scala.reflect.internal.util.Position import reporters.Reporter case class Problem(pos: Position, msg: String, severityLevel: Int) diff --git a/src/compiler/scala/tools/nsc/interactive/Picklers.scala b/src/compiler/scala/tools/nsc/interactive/Picklers.scala index 7c635c6d65..bf920191c8 100644 --- a/src/compiler/scala/tools/nsc/interactive/Picklers.scala +++ b/src/compiler/scala/tools/nsc/interactive/Picklers.scala @@ -5,10 +5,12 @@ package scala.tools.nsc package interactive -import util.{SourceFile, BatchSourceFile, InterruptReq} +import util.InterruptReq +import scala.reflect.internal.util.{SourceFile, BatchSourceFile} import io.{AbstractFile, PlainFile} -import util.{Position, RangePosition, NoPosition, OffsetPosition, TransparentPosition, EmptyAction} +import util.EmptyAction +import scala.reflect.internal.util.{Position, RangePosition, NoPosition, OffsetPosition, TransparentPosition} import io.{Pickler, CondPickler} import io.Pickler._ import collection.mutable diff --git a/src/compiler/scala/tools/nsc/interactive/REPL.scala b/src/compiler/scala/tools/nsc/interactive/REPL.scala index 1d78cc6e1c..1dcc979255 100644 --- a/src/compiler/scala/tools/nsc/interactive/REPL.scala +++ b/src/compiler/scala/tools/nsc/interactive/REPL.scala @@ -6,7 +6,7 @@ package scala.tools.nsc package interactive import scala.concurrent.SyncVar -import scala.tools.nsc.util._ +import scala.reflect.internal.util._ import scala.tools.nsc.symtab._ import scala.tools.nsc.ast._ import scala.tools.nsc.reporters._ @@ -141,7 +141,7 @@ object REPL { */ def compileInstrumented(iSourceName: String, arguments: List[String]): Option[AbstractFile] = { println("compiling "+iSourceName) - val command = new CompilerCommand(iSourceName :: arguments, reporter.error(scala.tools.nsc.util.NoPosition, _)) + val command = new CompilerCommand(iSourceName :: arguments, reporter.error(scala.reflect.internal.util.NoPosition, _)) val virtualDirectoryOpt = if (arguments contains "-d") None diff --git a/src/compiler/scala/tools/nsc/interactive/RangePositions.scala b/src/compiler/scala/tools/nsc/interactive/RangePositions.scala index c3eb06f2ff..06828f3a3a 100644 --- a/src/compiler/scala/tools/nsc/interactive/RangePositions.scala +++ b/src/compiler/scala/tools/nsc/interactive/RangePositions.scala @@ -7,7 +7,8 @@ package interactive import ast.Trees import ast.Positions -import scala.tools.nsc.util.{SourceFile, Position, RangePosition, NoPosition, WorkScheduler} +import scala.reflect.internal.util.{SourceFile, Position, RangePosition, NoPosition} +import scala.tools.nsc.util.WorkScheduler import scala.collection.mutable.ListBuffer /** Handling range positions diff --git a/src/compiler/scala/tools/nsc/interactive/RefinedBuildManager.scala b/src/compiler/scala/tools/nsc/interactive/RefinedBuildManager.scala index bad181eb76..57f0835edd 100644 --- a/src/compiler/scala/tools/nsc/interactive/RefinedBuildManager.scala +++ b/src/compiler/scala/tools/nsc/interactive/RefinedBuildManager.scala @@ -12,7 +12,8 @@ import scala.util.control.Breaks._ import scala.tools.nsc.symtab.Flags import dependencies._ -import util.{FakePos, ClassPath} +import scala.reflect.internal.util.FakePos +import util.ClassPath import io.AbstractFile import scala.tools.util.PathResolver diff --git a/src/compiler/scala/tools/nsc/interactive/RichCompilationUnits.scala b/src/compiler/scala/tools/nsc/interactive/RichCompilationUnits.scala index 9ef7d33549..9b2fee5f1f 100644 --- a/src/compiler/scala/tools/nsc/interactive/RichCompilationUnits.scala +++ b/src/compiler/scala/tools/nsc/interactive/RichCompilationUnits.scala @@ -5,7 +5,7 @@ package scala.tools.nsc package interactive -import scala.tools.nsc.util.{SourceFile, Position, NoPosition} +import scala.reflect.internal.util.{SourceFile, Position, NoPosition} import collection.mutable.ArrayBuffer trait RichCompilationUnits { self: Global => diff --git a/src/compiler/scala/tools/nsc/interactive/ScratchPadMaker.scala b/src/compiler/scala/tools/nsc/interactive/ScratchPadMaker.scala index e2dcc48709..a3f6726b44 100644 --- a/src/compiler/scala/tools/nsc/interactive/ScratchPadMaker.scala +++ b/src/compiler/scala/tools/nsc/interactive/ScratchPadMaker.scala @@ -1,7 +1,7 @@ package scala.tools.nsc package interactive -import util.{SourceFile, BatchSourceFile, RangePosition} +import scala.reflect.internal.util.{SourceFile, BatchSourceFile, RangePosition} import collection.mutable.ArrayBuffer import reflect.internal.Chars.isLineBreakChar diff --git a/src/compiler/scala/tools/nsc/interactive/SimpleBuildManager.scala b/src/compiler/scala/tools/nsc/interactive/SimpleBuildManager.scala index dd06e7dafa..6c9c7249e8 100644 --- a/src/compiler/scala/tools/nsc/interactive/SimpleBuildManager.scala +++ b/src/compiler/scala/tools/nsc/interactive/SimpleBuildManager.scala @@ -10,7 +10,7 @@ import scala.collection._ import scala.tools.nsc.reporters.{Reporter, ConsoleReporter} import dependencies._ -import util.FakePos +import scala.reflect.internal.util.FakePos import io.AbstractFile /** A simple build manager, using the default scalac dependency tracker. diff --git a/src/compiler/scala/tools/nsc/interactive/tests/InteractiveTest.scala b/src/compiler/scala/tools/nsc/interactive/tests/InteractiveTest.scala index 9dc2a8de10..f622f11ffd 100644 --- a/src/compiler/scala/tools/nsc/interactive/tests/InteractiveTest.scala +++ b/src/compiler/scala/tools/nsc/interactive/tests/InteractiveTest.scala @@ -12,8 +12,8 @@ import java.io.File.pathSeparatorChar import java.io.File.separatorChar import scala.annotation.migration -import scala.tools.nsc.util.Position -import scala.tools.nsc.util.SourceFile +import scala.reflect.internal.util.Position +import scala.reflect.internal.util.SourceFile import collection.mutable.ListBuffer diff --git a/src/compiler/scala/tools/nsc/interactive/tests/Tester.scala b/src/compiler/scala/tools/nsc/interactive/tests/Tester.scala index 034db218ae..55198e3617 100644 --- a/src/compiler/scala/tools/nsc/interactive/tests/Tester.scala +++ b/src/compiler/scala/tools/nsc/interactive/tests/Tester.scala @@ -6,7 +6,7 @@ package scala.tools.nsc package interactive package tests -import util._ +import scala.reflect.internal.util._ import reporters._ import io.AbstractFile import collection.mutable.ArrayBuffer diff --git a/src/compiler/scala/tools/nsc/interactive/tests/core/AskCommand.scala b/src/compiler/scala/tools/nsc/interactive/tests/core/AskCommand.scala index 657ef23eed..0cf32a8d8a 100644 --- a/src/compiler/scala/tools/nsc/interactive/tests/core/AskCommand.scala +++ b/src/compiler/scala/tools/nsc/interactive/tests/core/AskCommand.scala @@ -7,8 +7,8 @@ package interactive package tests.core import scala.tools.nsc.interactive.Response -import scala.tools.nsc.util.Position -import scala.tools.nsc.util.SourceFile +import scala.reflect.internal.util.Position +import scala.reflect.internal.util.SourceFile /** * A trait for defining commands that can be queried to the diff --git a/src/compiler/scala/tools/nsc/interactive/tests/core/CoreTestDefs.scala b/src/compiler/scala/tools/nsc/interactive/tests/core/CoreTestDefs.scala index d6102734ab..c8e6b6ccce 100644 --- a/src/compiler/scala/tools/nsc/interactive/tests/core/CoreTestDefs.scala +++ b/src/compiler/scala/tools/nsc/interactive/tests/core/CoreTestDefs.scala @@ -2,7 +2,7 @@ package scala.tools.nsc package interactive package tests.core -import scala.tools.nsc.util.Position +import scala.reflect.internal.util.Position import scala.tools.nsc.interactive.tests.core._ /** Set of core test definitions that are executed for each test run. */ diff --git a/src/compiler/scala/tools/nsc/interactive/tests/core/PresentationCompilerRequestsWorkingMode.scala b/src/compiler/scala/tools/nsc/interactive/tests/core/PresentationCompilerRequestsWorkingMode.scala index d2baaf32c6..18a8eb5fc3 100644 --- a/src/compiler/scala/tools/nsc/interactive/tests/core/PresentationCompilerRequestsWorkingMode.scala +++ b/src/compiler/scala/tools/nsc/interactive/tests/core/PresentationCompilerRequestsWorkingMode.scala @@ -2,8 +2,8 @@ package scala.tools.nsc package interactive package tests.core -import scala.tools.nsc.util.Position -import scala.tools.nsc.util.SourceFile +import scala.reflect.internal.util.Position +import scala.reflect.internal.util.SourceFile trait PresentationCompilerRequestsWorkingMode extends TestResources { diff --git a/src/compiler/scala/tools/nsc/interactive/tests/core/PresentationCompilerTestDef.scala b/src/compiler/scala/tools/nsc/interactive/tests/core/PresentationCompilerTestDef.scala index 8b8be697cc..9cf2aa4fe4 100644 --- a/src/compiler/scala/tools/nsc/interactive/tests/core/PresentationCompilerTestDef.scala +++ b/src/compiler/scala/tools/nsc/interactive/tests/core/PresentationCompilerTestDef.scala @@ -1,7 +1,7 @@ package scala.tools.nsc.interactive.tests.core import scala.tools.nsc.interactive.Global -import scala.tools.nsc.util.Position +import scala.reflect.internal.util.Position trait PresentationCompilerTestDef { diff --git a/src/compiler/scala/tools/nsc/interactive/tests/core/SourcesCollector.scala b/src/compiler/scala/tools/nsc/interactive/tests/core/SourcesCollector.scala index 21e90fe57f..e80b741a8d 100644 --- a/src/compiler/scala/tools/nsc/interactive/tests/core/SourcesCollector.scala +++ b/src/compiler/scala/tools/nsc/interactive/tests/core/SourcesCollector.scala @@ -1,6 +1,6 @@ package scala.tools.nsc.interactive.tests.core -import scala.tools.nsc.util.{SourceFile,BatchSourceFile} +import scala.reflect.internal.util.{SourceFile,BatchSourceFile} import scala.tools.nsc.io.{AbstractFile,Path} private[tests] object SourcesCollector { diff --git a/src/compiler/scala/tools/nsc/interactive/tests/core/TestResources.scala b/src/compiler/scala/tools/nsc/interactive/tests/core/TestResources.scala index 106d1dacb3..887c3cf29b 100644 --- a/src/compiler/scala/tools/nsc/interactive/tests/core/TestResources.scala +++ b/src/compiler/scala/tools/nsc/interactive/tests/core/TestResources.scala @@ -1,7 +1,7 @@ package scala.tools.nsc.interactive.tests.core import scala.tools.nsc.io.Path -import scala.tools.nsc.util.SourceFile +import scala.reflect.internal.util.SourceFile /** Resources used by the test. */ private[tests] trait TestResources extends TestSettings { diff --git a/src/compiler/scala/tools/nsc/interpreter/ExprTyper.scala b/src/compiler/scala/tools/nsc/interpreter/ExprTyper.scala index 79b429e26a..f2438dcc20 100644 --- a/src/compiler/scala/tools/nsc/interpreter/ExprTyper.scala +++ b/src/compiler/scala/tools/nsc/interpreter/ExprTyper.scala @@ -6,7 +6,7 @@ package scala.tools.nsc package interpreter -import util.BatchSourceFile +import scala.reflect.internal.util.BatchSourceFile import scala.tools.nsc.ast.parser.Tokens.EOF trait ExprTyper { diff --git a/src/compiler/scala/tools/nsc/interpreter/ILoopInit.scala b/src/compiler/scala/tools/nsc/interpreter/ILoopInit.scala index 5bc2ee1278..37e4dfaea4 100644 --- a/src/compiler/scala/tools/nsc/interpreter/ILoopInit.scala +++ b/src/compiler/scala/tools/nsc/interpreter/ILoopInit.scala @@ -6,7 +6,7 @@ package scala.tools.nsc package interpreter -import util.Position +import scala.reflect.internal.util.Position import scala.util.control.Exception.ignoring /** diff --git a/src/compiler/scala/tools/nsc/interpreter/IMain.scala b/src/compiler/scala/tools/nsc/interpreter/IMain.scala index d19a83ef8d..2a7adbe781 100644 --- a/src/compiler/scala/tools/nsc/interpreter/IMain.scala +++ b/src/compiler/scala/tools/nsc/interpreter/IMain.scala @@ -7,7 +7,8 @@ package scala.tools.nsc package interpreter import Predef.{ println => _, _ } -import util.{ Set => _, _ } +import util.stringFromWriter +import scala.reflect.internal.util._ import java.net.URL import scala.sys.BooleanProp import io.VirtualDirectory diff --git a/src/compiler/scala/tools/nsc/interpreter/Power.scala b/src/compiler/scala/tools/nsc/interpreter/Power.scala index 1d3c5560fd..57d7cef726 100644 --- a/src/compiler/scala/tools/nsc/interpreter/Power.scala +++ b/src/compiler/scala/tools/nsc/interpreter/Power.scala @@ -8,7 +8,7 @@ package interpreter import scala.collection.{ mutable, immutable } import scala.util.matching.Regex -import scala.tools.nsc.util.{ BatchSourceFile } +import scala.reflect.internal.util.{ BatchSourceFile } import session.{ History } import scala.io.Codec import java.net.{ URL, MalformedURLException } diff --git a/src/compiler/scala/tools/nsc/interpreter/TypeStrings.scala b/src/compiler/scala/tools/nsc/interpreter/TypeStrings.scala index faf80a39a7..56b9c7011c 100644 --- a/src/compiler/scala/tools/nsc/interpreter/TypeStrings.scala +++ b/src/compiler/scala/tools/nsc/interpreter/TypeStrings.scala @@ -13,7 +13,7 @@ import NameTransformer._ import scala.reflect.runtime.{universe => ru} import scala.reflect.{ClassTag, classTag} import typechecker.DestructureTypes -import scala.tools.util.StringOps.ojoin +import scala.reflect.internal.util.StringOps.ojoin import language.implicitConversions /** A more principled system for turning types into strings. diff --git a/src/compiler/scala/tools/nsc/io/AbstractFile.scala b/src/compiler/scala/tools/nsc/io/AbstractFile.scala index 08b9df2fa8..3faaeaeaec 100644 --- a/src/compiler/scala/tools/nsc/io/AbstractFile.scala +++ b/src/compiler/scala/tools/nsc/io/AbstractFile.scala @@ -8,6 +8,8 @@ package scala.tools.nsc package io import java.io.{ FileOutputStream, IOException, InputStream, OutputStream, BufferedOutputStream } +// [scala-reflect.jar migration note] uncomment when creating scala-reflect.jar +// import java.io.{ File => JFile } import java.net.URL import scala.collection.mutable.ArrayBuffer diff --git a/src/compiler/scala/tools/nsc/io/Directory.scala b/src/compiler/scala/tools/nsc/io/Directory.scala index b4ceba682a..0eecd9a6e2 100644 --- a/src/compiler/scala/tools/nsc/io/Directory.scala +++ b/src/compiler/scala/tools/nsc/io/Directory.scala @@ -9,6 +9,9 @@ package scala.tools.nsc package io +// [scala-reflect.jar migration note] uncomment when creating scala-reflect.jar +// import java.io.{ File => JFile } + object Directory { import scala.util.Properties.{ tmpDir, userHome, userDir } diff --git a/src/compiler/scala/tools/nsc/io/File.scala b/src/compiler/scala/tools/nsc/io/File.scala index 06cb20e4ac..4cea8439b1 100644 --- a/src/compiler/scala/tools/nsc/io/File.scala +++ b/src/compiler/scala/tools/nsc/io/File.scala @@ -13,6 +13,8 @@ package io import java.io.{ FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, BufferedInputStream, BufferedOutputStream, IOException, PrintStream, PrintWriter, Closeable => JCloseable } +// [scala-reflect.jar migration note] uncomment when creating scala-reflect.jar +// import java.io.{ File => JFile } import java.nio.channels.{ Channel, FileChannel } import scala.io.Codec import language.{reflectiveCalls, implicitConversions} diff --git a/src/compiler/scala/tools/nsc/io/NoAbstractFile.scala b/src/compiler/scala/tools/nsc/io/NoAbstractFile.scala index 36cf42d7ec..e468356722 100644 --- a/src/compiler/scala/tools/nsc/io/NoAbstractFile.scala +++ b/src/compiler/scala/tools/nsc/io/NoAbstractFile.scala @@ -7,6 +7,8 @@ package scala.tools.nsc package io import java.io.InputStream +// [scala-reflect.jar migration note] uncomment when creating scala-reflect.jar +// import java.io.{ File => JFile } /** A distinguished object so you can avoid both null * and Option. diff --git a/src/compiler/scala/tools/nsc/io/Path.scala b/src/compiler/scala/tools/nsc/io/Path.scala index b8cf15bfcf..1db015e2f5 100644 --- a/src/compiler/scala/tools/nsc/io/Path.scala +++ b/src/compiler/scala/tools/nsc/io/Path.scala @@ -9,6 +9,8 @@ package io import java.io.{ FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, BufferedInputStream, BufferedOutputStream, RandomAccessFile } +// [scala-reflect.jar migration note] uncomment when creating scala-reflect.jar +// import java.io.{ File => JFile } import java.net.{ URI, URL } import scala.util.Random.alphanumeric import language.implicitConversions @@ -42,7 +44,8 @@ object Path { if (i < 0) "" else name.substring(i + 1).toLowerCase } - def isJarOrZip(f: Path, examineFile: Boolean = true) = Jar.isJarOrZip(f, examineFile) + // [Eugene++] I hope that noone relied on this method +// def isJarOrZip(f: Path, examineFile: Boolean = true) = Jar.isJarOrZip(f, examineFile) // not certain these won't be problematic, but looks good so far implicit def string2path(s: String): Path = apply(s) diff --git a/src/compiler/scala/tools/nsc/io/SourceReader.scala b/src/compiler/scala/tools/nsc/io/SourceReader.scala index 324c5e4111..2e146becdf 100644 --- a/src/compiler/scala/tools/nsc/io/SourceReader.scala +++ b/src/compiler/scala/tools/nsc/io/SourceReader.scala @@ -28,7 +28,7 @@ class SourceReader(decoder: CharsetDecoder, reporter: Reporter) { private var chars: CharBuffer = CharBuffer.allocate(0x4000) private def reportEncodingError(filename:String) = { - reporter.error(util.NoPosition, + reporter.error(scala.reflect.internal.util.NoPosition, "IO error while decoding "+filename+" with "+decoder.charset()+"\n"+ "Please try specifying another one using the -encoding option") } diff --git a/src/compiler/scala/tools/nsc/io/VirtualFile.scala b/src/compiler/scala/tools/nsc/io/VirtualFile.scala index 450d86b599..48826ed191 100644 --- a/src/compiler/scala/tools/nsc/io/VirtualFile.scala +++ b/src/compiler/scala/tools/nsc/io/VirtualFile.scala @@ -8,6 +8,8 @@ package scala.tools.nsc package io import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, InputStream, OutputStream } +// [scala-reflect.jar migration note] uncomment when creating scala-reflect.jar +// import java.io.{ File => JFile } /** This class implements an in-memory file. * diff --git a/src/compiler/scala/tools/nsc/io/ZipArchive.scala b/src/compiler/scala/tools/nsc/io/ZipArchive.scala index 01f3619ba7..e61a9ba0da 100644 --- a/src/compiler/scala/tools/nsc/io/ZipArchive.scala +++ b/src/compiler/scala/tools/nsc/io/ZipArchive.scala @@ -8,6 +8,8 @@ package io import java.net.URL import java.io.{ IOException, InputStream, ByteArrayInputStream } +// [scala-reflect.jar migration note] uncomment when creating scala-reflect.jar +// import java.io.{ File => JFile } import java.util.zip.{ ZipEntry, ZipFile, ZipInputStream } import scala.collection.{ immutable, mutable } import annotation.tailrec diff --git a/src/compiler/scala/tools/nsc/javac/JavaParsers.scala b/src/compiler/scala/tools/nsc/javac/JavaParsers.scala index f71e067366..e2203e07b3 100644 --- a/src/compiler/scala/tools/nsc/javac/JavaParsers.scala +++ b/src/compiler/scala/tools/nsc/javac/JavaParsers.scala @@ -8,7 +8,7 @@ package scala.tools.nsc package javac -import scala.tools.nsc.util.OffsetPosition +import scala.reflect.internal.util.OffsetPosition import scala.collection.mutable.ListBuffer import symtab.Flags import JavaTokens._ diff --git a/src/compiler/scala/tools/nsc/javac/JavaScanners.scala b/src/compiler/scala/tools/nsc/javac/JavaScanners.scala index d47756e757..73b5a752b4 100644 --- a/src/compiler/scala/tools/nsc/javac/JavaScanners.scala +++ b/src/compiler/scala/tools/nsc/javac/JavaScanners.scala @@ -6,7 +6,8 @@ package scala.tools.nsc package javac -import scala.tools.nsc.util._ +import scala.tools.nsc.util.JavaCharArrayReader +import scala.reflect.internal.util._ import scala.reflect.internal.Chars._ import JavaTokens._ import scala.annotation.switch diff --git a/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala b/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala index 7346d9c59f..0d1356a6b0 100644 --- a/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala +++ b/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala @@ -9,7 +9,7 @@ package matching import PartialFunction._ import scala.collection.{ mutable } -import util.Position +import scala.reflect.internal.util.Position import transform.ExplicitOuter import symtab.Flags import mutable.ListBuffer diff --git a/src/compiler/scala/tools/nsc/plugins/Plugin.scala b/src/compiler/scala/tools/nsc/plugins/Plugin.scala index 48c4a9b5b3..7f9df2235a 100644 --- a/src/compiler/scala/tools/nsc/plugins/Plugin.scala +++ b/src/compiler/scala/tools/nsc/plugins/Plugin.scala @@ -6,7 +6,7 @@ package scala.tools.nsc package plugins -import io.{ File, Path } +import io.{ File, Path, Jar } import java.net.URLClassLoader import java.util.jar.JarFile import java.util.zip.ZipException @@ -132,7 +132,8 @@ object Plugin { val alljars = (jars ::: (for { dir <- dirs if dir.isDirectory entry <- dir.toDirectory.files.toList sortBy (_.name) - if Path.isJarOrZip(entry) +// was: if Path.isJarOrZip(entry) + if Jar.isJarOrZip(entry) pdesc <- loadDescription(entry) if !(ignoring contains pdesc.name) } yield entry)).distinct diff --git a/src/compiler/scala/tools/nsc/reporters/AbstractReporter.scala b/src/compiler/scala/tools/nsc/reporters/AbstractReporter.scala index ab8fe23909..cb85e49d55 100644 --- a/src/compiler/scala/tools/nsc/reporters/AbstractReporter.scala +++ b/src/compiler/scala/tools/nsc/reporters/AbstractReporter.scala @@ -8,7 +8,7 @@ package reporters import scala.collection.mutable import scala.tools.nsc.Settings -import scala.tools.nsc.util.Position +import scala.reflect.internal.util.Position /** * This reporter implements filtering. diff --git a/src/compiler/scala/tools/nsc/reporters/ConsoleReporter.scala b/src/compiler/scala/tools/nsc/reporters/ConsoleReporter.scala index 956c43c35a..dff900383e 100644 --- a/src/compiler/scala/tools/nsc/reporters/ConsoleReporter.scala +++ b/src/compiler/scala/tools/nsc/reporters/ConsoleReporter.scala @@ -7,8 +7,7 @@ package scala.tools.nsc package reporters import java.io.{ BufferedReader, IOException, PrintWriter } -import util._ -import scala.tools.util.StringOps +import scala.reflect.internal.util._ /** * This class implements a Reporter that displays messages on a text diff --git a/src/compiler/scala/tools/nsc/reporters/Reporter.scala b/src/compiler/scala/tools/nsc/reporters/Reporter.scala index 309fc5733f..303a477491 100644 --- a/src/compiler/scala/tools/nsc/reporters/Reporter.scala +++ b/src/compiler/scala/tools/nsc/reporters/Reporter.scala @@ -6,9 +6,8 @@ package scala.tools.nsc package reporters -import scala.tools.nsc.util._ -import scala.tools.util.StringOps -import StringOps._ +import scala.reflect.internal.util._ +import scala.reflect.internal.util.StringOps._ /** * This interface provides methods to issue information, warning and @@ -78,8 +77,8 @@ abstract class Reporter { } // sbt compat - @deprecated("Moved to scala.tools.util.StringOps", "2.10.0") + @deprecated("Moved to scala.reflect.internal.util.StringOps", "2.10.0") def countElementsAsString(n: Int, elements: String): String = StringOps.countElementsAsString(n, elements) - @deprecated("Moved to scala.tools.util.StringOps", "2.10.0") + @deprecated("Moved to scala.reflect.internal.util.StringOps", "2.10.0") def countAsString(n: Int): String = StringOps.countAsString(n) } diff --git a/src/compiler/scala/tools/nsc/reporters/StoreReporter.scala b/src/compiler/scala/tools/nsc/reporters/StoreReporter.scala index 12edd9bf31..5c3f7b8965 100644 --- a/src/compiler/scala/tools/nsc/reporters/StoreReporter.scala +++ b/src/compiler/scala/tools/nsc/reporters/StoreReporter.scala @@ -7,7 +7,7 @@ package scala.tools.nsc package reporters import scala.collection.mutable -import scala.tools.nsc.util.Position +import scala.reflect.internal.util.Position /** * This class implements a Reporter that displays messages on a text diff --git a/src/compiler/scala/tools/nsc/scratchpad/SourceInserter.scala b/src/compiler/scala/tools/nsc/scratchpad/SourceInserter.scala index 311aa6e324..42a35dc642 100644 --- a/src/compiler/scala/tools/nsc/scratchpad/SourceInserter.scala +++ b/src/compiler/scala/tools/nsc/scratchpad/SourceInserter.scala @@ -2,7 +2,7 @@ package scala.tools.nsc package scratchpad import java.io.Writer -import util.SourceFile +import scala.reflect.internal.util.SourceFile import reflect.internal.Chars._ diff --git a/src/compiler/scala/tools/nsc/settings/MutableSettings.scala b/src/compiler/scala/tools/nsc/settings/MutableSettings.scala index c0d9a677e8..fc833e2c26 100644 --- a/src/compiler/scala/tools/nsc/settings/MutableSettings.scala +++ b/src/compiler/scala/tools/nsc/settings/MutableSettings.scala @@ -8,8 +8,8 @@ package scala.tools package nsc package settings -import io.{ AbstractFile, Path, PlainFile, VirtualDirectory } -import scala.tools.util.StringOps +import io.{ AbstractFile, Jar, Path, PlainFile, VirtualDirectory } +import scala.reflect.internal.util.StringOps import scala.collection.mutable.ListBuffer import scala.io.Source import scala.reflect.{ ClassTag, classTag } @@ -255,7 +255,8 @@ class MutableSettings(val errorFn: String => Unit) private def checkDir(dir: AbstractFile, name: String, allowJar: Boolean = false): AbstractFile = ( if (dir != null && dir.isDirectory) dir - else if (allowJar && dir == null && Path.isJarOrZip(name, false)) +// was: else if (allowJar && dir == null && Path.isJarOrZip(name, false)) + else if (allowJar && dir == null && Jar.isJarOrZip(name, false)) new PlainFile(Path(name)) else throw new FatalError(name + " does not exist or is not a directory") diff --git a/src/compiler/scala/tools/nsc/symtab/BrowsingLoaders.scala b/src/compiler/scala/tools/nsc/symtab/BrowsingLoaders.scala index 5f7deb87bd..25d835790e 100644 --- a/src/compiler/scala/tools/nsc/symtab/BrowsingLoaders.scala +++ b/src/compiler/scala/tools/nsc/symtab/BrowsingLoaders.scala @@ -6,7 +6,7 @@ package scala.tools.nsc package symtab -import scala.tools.nsc.util.BatchSourceFile +import scala.reflect.internal.util.BatchSourceFile import scala.tools.nsc.io.AbstractFile /** A subclass of SymbolLoaders that implements browsing behavior. diff --git a/src/compiler/scala/tools/nsc/symtab/clr/CLRTypes.scala b/src/compiler/scala/tools/nsc/symtab/clr/CLRTypes.scala index 7be0fcb146..d3b6c9238d 100644 --- a/src/compiler/scala/tools/nsc/symtab/clr/CLRTypes.scala +++ b/src/compiler/scala/tools/nsc/symtab/clr/CLRTypes.scala @@ -12,7 +12,7 @@ import java.util.{Comparator, StringTokenizer} import scala.util.Sorting import ch.epfl.lamp.compiler.msil._ import scala.collection.{ mutable, immutable } -import scala.tools.nsc.util.{Position, NoPosition} +import scala.reflect.internal.util.{Position, NoPosition} /** * Collects all types from all reference assemblies. diff --git a/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala b/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala index 4504eaad38..ae6ec8511c 100644 --- a/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala +++ b/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala @@ -1695,7 +1695,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers { } } - private def forwardCall(pos: util.Position, receiver: Tree, paramss: List[List[ValDef]]): Tree = { + private def forwardCall(pos: scala.reflect.internal.util.Position, receiver: Tree, paramss: List[List[ValDef]]): Tree = { val argss = mmap(paramss)(x => Ident(x.symbol)) def mkApply(fun: Tree, args: List[Tree]) = Apply(fun, args) atPos(pos) { (receiver /: argss) (mkApply) } @@ -1722,7 +1722,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers { * } * }} */ - private def forwardCtorCall(pos: util.Position, receiver: Tree, paramss: List[List[ValDef]], clazz: Symbol): Tree = { + private def forwardCtorCall(pos: scala.reflect.internal.util.Position, receiver: Tree, paramss: List[List[ValDef]], clazz: Symbol): Tree = { /** A constructor parameter `f` initializes a specialized field * iff: diff --git a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala index affa9cd63b..94e7dc0d65 100644 --- a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala +++ b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala @@ -7,7 +7,7 @@ package scala.tools.nsc package typechecker import scala.collection.{ mutable, immutable } -import scala.tools.util.StringOps.{ countElementsAsString, countAsString } +import scala.reflect.internal.util.StringOps.{ countElementsAsString, countAsString } import symtab.Flags.{ PRIVATE, PROTECTED } trait ContextErrors { diff --git a/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala b/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala index 9d6b4a589c..465b1007cb 100644 --- a/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala +++ b/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala @@ -7,7 +7,7 @@ package typechecker import symtab.Flags._ import scala.collection.{ mutable, immutable } -import scala.tools.util.StringOps.{ ojoin } +import scala.reflect.internal.util.StringOps.{ ojoin } import scala.reflect.ClassTag import scala.reflect.runtime.{ universe => ru } import language.higherKinds diff --git a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala index 85b2039765..f64c4a4ce1 100644 --- a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala +++ b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala @@ -377,7 +377,7 @@ trait NamesDefaults { self: Analyzer => */ def addDefaults(givenArgs: List[Tree], qual: Option[Tree], targs: List[Tree], previousArgss: List[List[Tree]], params: List[Symbol], - pos: util.Position, context: Context): (List[Tree], List[Symbol]) = { + pos: scala.reflect.internal.util.Position, context: Context): (List[Tree], List[Symbol]) = { if (givenArgs.length < params.length) { val (missing, positional) = missingParams(givenArgs, params) if (missing forall (_.hasDefault)) { diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index f46d161da5..a7c69a4e39 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -13,7 +13,7 @@ package scala.tools.nsc package typechecker import scala.collection.mutable -import scala.tools.nsc.util.BatchSourceFile +import scala.reflect.internal.util.BatchSourceFile import mutable.ListBuffer import symtab.Flags._ import util.Statistics diff --git a/src/compiler/scala/tools/nsc/util/ClassPath.scala b/src/compiler/scala/tools/nsc/util/ClassPath.scala index 0636fcf581..9755fb6c60 100644 --- a/src/compiler/scala/tools/nsc/util/ClassPath.scala +++ b/src/compiler/scala/tools/nsc/util/ClassPath.scala @@ -10,7 +10,7 @@ package util import java.net.URL import scala.collection.{ mutable, immutable } import io.{ File, Directory, Path, Jar, AbstractFile } -import scala.tools.util.StringOps.splitWhere +import scala.reflect.internal.util.StringOps.splitWhere import scala.reflect.ClassTag import Jar.isJarOrZip import File.pathSeparator diff --git a/src/compiler/scala/tools/nsc/util/Exceptional.scala b/src/compiler/scala/tools/nsc/util/Exceptional.scala index 7c093b7866..90d032518a 100644 --- a/src/compiler/scala/tools/nsc/util/Exceptional.scala +++ b/src/compiler/scala/tools/nsc/util/Exceptional.scala @@ -3,7 +3,7 @@ package util import java.util.concurrent.ExecutionException import java.lang.reflect.{ InvocationTargetException, UndeclaredThrowableException } -import scala.tools.util.StringOps._ +import scala.reflect.internal.util.StringOps._ import language.implicitConversions object Exceptional { diff --git a/src/compiler/scala/tools/nsc/util/FreshNameCreator.scala b/src/compiler/scala/tools/nsc/util/FreshNameCreator.scala index 88fc6718a2..16bf748414 100644 --- a/src/compiler/scala/tools/nsc/util/FreshNameCreator.scala +++ b/src/compiler/scala/tools/nsc/util/FreshNameCreator.scala @@ -16,9 +16,9 @@ trait FreshNameCreator { def newName(prefix: String): String @deprecated("use newName(prefix)", "2.9.0") - def newName(pos: util.Position, prefix: String): String = newName(prefix) + def newName(pos: scala.reflect.internal.util.Position, prefix: String): String = newName(prefix) @deprecated("use newName()", "2.9.0") - def newName(pos: util.Position): String = newName() + def newName(pos: scala.reflect.internal.util.Position): String = newName() } object FreshNameCreator { diff --git a/src/compiler/scala/tools/nsc/util/Position.scala b/src/compiler/scala/tools/nsc/util/Position.scala deleted file mode 100644 index d07468e728..0000000000 --- a/src/compiler/scala/tools/nsc/util/Position.scala +++ /dev/null @@ -1,278 +0,0 @@ -/* NSC -- new Scala compiler - * Copyright 2005-2011 LAMP/EPFL - * @author Martin Odersky - * - */ - -package scala.tools.nsc -package util - -import reflect.ClassTag -import reflect.base.Attachments -import reflect.api.PositionApi - -object Position { - val tabInc = 8 - - /** Prints the message with the given position indication. */ - def formatMessage(posIn: Position, msg: String, shortenFile: Boolean): String = { - val pos = ( - if (posIn eq null) NoPosition - else if (posIn.isDefined) posIn.inUltimateSource(posIn.source) - else posIn - ) - def file = pos.source.file - def prefix = if (shortenFile) file.name else file.path - - pos match { - case FakePos(fmsg) => fmsg+" "+msg - case NoPosition => msg - case _ => - List( - "%s:%s: %s".format(prefix, pos.line, msg), - pos.lineContent.stripLineEnd, - " " * (pos.column - 1) + "^" - ) mkString "\n" - } - } -} - -abstract class Position extends PositionApi { self => - - type Pos = Position - - def pos: Position = this - - def withPos(newPos: Position): Attachments { type Pos = self.Pos } = newPos - - /** Java file corresponding to the source file of this position. - */ - // necessary for conformance with scala.reflect.api.Position - def fileInfo: java.io.File = source.file.file - - /** Contents of the source file that contains this position. - */ - // necessary for conformance with scala.reflect.api.Position - def fileContent: Array[Char] = source.content - - /** An optional value containing the source file referred to by this position, or - * None if not defined. - */ - def source: SourceFile = throw new UnsupportedOperationException("Position.source") - - /** Is this position neither a NoPosition nor a FakePosition? - * If isDefined is true, offset and source are both defined. - */ - def isDefined: Boolean = false - - /** Is this position a transparent position? */ - def isTransparent: Boolean = false - - /** Is this position a range position? */ - def isRange: Boolean = false - - /** Is this position a non-transparent range position? */ - def isOpaqueRange: Boolean = false - - /** if opaque range, make this position transparent */ - def makeTransparent: Position = this - - /** The start of the position's range, error if not a range position */ - def start: Int = throw new UnsupportedOperationException("Position.start") - - /** The start of the position's range, or point if not a range position */ - def startOrPoint: Int = point - - /** The point (where the ^ is) of the position */ - def point: Int = throw new UnsupportedOperationException("Position.point") - - /** The point (where the ^ is) of the position, or else `default` if undefined */ - def pointOrElse(default: Int): Int = default - - /** The end of the position's range, error if not a range position */ - def end: Int = throw new UnsupportedOperationException("Position.end") - - /** The end of the position's range, or point if not a range position */ - def endOrPoint: Int = point - - @deprecated("use point instead", "2.9.0") - def offset: Option[Int] = if (isDefined) Some(point) else None - - /** The same position with a different start value (if a range) */ - def withStart(off: Int): Position = this - - /** The same position with a different end value (if a range) */ - def withEnd(off: Int): Position = this - - /** The same position with a different point value (if a range or offset) */ - def withPoint(off: Int): Position = this - - /** The same position with a different source value, and its values shifted by given offset */ - def withSource(source: SourceFile, shift: Int): Position = this - - /** If this is a range, the union with the other range, with the point of this position. - * Otherwise, this position - */ - def union(pos: Position): Position = this - - /** If this is a range position, the offset position of its start. - * Otherwise the position itself - */ - def focusStart: Position = this - - /** If this is a range position, the offset position of its point. - * Otherwise the position itself - */ - def focus: Position = this - - /** If this is a range position, the offset position of its end. - * Otherwise the position itself - */ - def focusEnd: Position = this - - /** Does this position include the given position `pos`. - * This holds if `this` is a range position and its range [start..end] - * is the same or covers the range of the given position, which may or may not be a range position. - */ - def includes(pos: Position): Boolean = false - - /** Does this position properly include the given position `pos` ("properly" meaning their - * ranges are not the same)? - */ - def properlyIncludes(pos: Position): Boolean = - includes(pos) && (start < pos.startOrPoint || pos.endOrPoint < end) - - /** Does this position precede that position? - * This holds if both positions are defined and the end point of this position - * is not larger than the start point of the given position. - */ - def precedes(pos: Position): Boolean = - isDefined && pos.isDefined && endOrPoint <= pos.startOrPoint - - /** Does this position properly precede the given position `pos` ("properly" meaning their ranges - * do not share a common point). - */ - def properlyPrecedes(pos: Position): Boolean = - isDefined && pos.isDefined && endOrPoint < pos.startOrPoint - - /** Does this position overlap with that position? - * This holds if both positions are ranges and there is an interval of - * non-zero length that is shared by both position ranges. - */ - def overlaps(pos: Position): Boolean = - isRange && pos.isRange && - ((pos.start < end && start < pos.end) || (start < pos.end && pos.start < end)) - - /** Does this position cover the same range as that position? - * Holds only if both position are ranges - */ - def sameRange(pos: Position): Boolean = - isRange && pos.isRange && start == pos.start && end == pos.end - - def line: Int = throw new UnsupportedOperationException("Position.line") - - def column: Int = throw new UnsupportedOperationException("Position.column") - - /** Convert this to a position around `point` that spans a single source line */ - def toSingleLine: Position = this - - def lineContent: String = - if (isDefined) source.lineToString(line - 1) - else "NO_LINE" - - /** Map this position to a position in an original source - * file. If the SourceFile is a normal SourceFile, simply - * return this. - */ - def inUltimateSource(source : SourceFile): Position = - if (source == null) this else source.positionInUltimateSource(this) - - def dbgString: String = toString - def safeLine: Int = try line catch { case _: UnsupportedOperationException => -1 } - - def show: String = "["+toString+"]" -} - -case object NoPosition extends Position { - override def dbgString = toString -} - -case class FakePos(msg: String) extends Position { - override def toString = msg -} - -class OffsetPosition(override val source: SourceFile, override val point: Int) extends Position { - override def isDefined = true - override def pointOrElse(default: Int): Int = point - override def withPoint(off: Int) = new OffsetPosition(source, off) - override def withSource(source: SourceFile, shift: Int) = new OffsetPosition(source, point + shift) - - override def line: Int = source.offsetToLine(point) + 1 - - override def column: Int = { - var idx = source.lineToOffset(source.offsetToLine(point)) - var col = 0 - while (idx != point) { - col += (if (source.content(idx) == '\t') Position.tabInc - col % Position.tabInc else 1) - idx += 1 - } - col + 1 - } - - override def union(pos: Position) = if (pos.isRange) pos else this - - override def equals(that : Any) = that match { - case that : OffsetPosition => point == that.point && source.file == that.source.file - case that => false - } - override def hashCode = point * 37 + source.file.hashCode - - override def toString = { - val pointmsg = if (point > source.length) "out-of-bounds-" else "offset=" - "source-%s,line-%s,%s%s".format(source.file.canonicalPath, line, pointmsg, point) - } - override def show = "["+point+"]" -} - -/** new for position ranges */ -class RangePosition(source: SourceFile, override val start: Int, point: Int, override val end: Int) -extends OffsetPosition(source, point) { - if (start > end) assert(false, "bad position: "+show) - override def isRange: Boolean = true - override def isOpaqueRange: Boolean = true - override def startOrPoint: Int = start - override def endOrPoint: Int = end - override def withStart(off: Int) = new RangePosition(source, off, point, end) - override def withEnd(off: Int) = new RangePosition(source, start, point, off) - override def withPoint(off: Int) = new RangePosition(source, start, off, end) - override def withSource(source: SourceFile, shift: Int) = new RangePosition(source, start + shift, point + shift, end + shift) - override def focusStart = new OffsetPosition(source, start) - override def focus = { - if (focusCache eq NoPosition) focusCache = new OffsetPosition(source, point) - focusCache - } - override def focusEnd = new OffsetPosition(source, end) - override def makeTransparent = new TransparentPosition(source, start, point, end) - override def includes(pos: Position) = pos.isDefined && start <= pos.startOrPoint && pos.endOrPoint <= end - override def union(pos: Position): Position = - if (pos.isRange) new RangePosition(source, start min pos.start, point, end max pos.end) else this - - override def toSingleLine: Position = source match { - case bs: BatchSourceFile - if end > 0 && bs.offsetToLine(start) < bs.offsetToLine(end - 1) => - val pointLine = bs.offsetToLine(point) - new RangePosition(source, bs.lineToOffset(pointLine), point, bs.lineToOffset(pointLine + 1)) - case _ => this - } - - override def toString = "RangePosition("+source.file.canonicalPath+", "+start+", "+point+", "+end+")" - override def show = "["+start+":"+end+"]" - private var focusCache: Position = NoPosition -} - -class TransparentPosition(source: SourceFile, start: Int, point: Int, end: Int) extends RangePosition(source, start, point, end) { - override def isOpaqueRange: Boolean = false - override def isTransparent = true - override def makeTransparent = this - override def show = "<"+start+":"+end+">" -} \ No newline at end of file diff --git a/src/compiler/scala/tools/nsc/util/SourceFile.scala b/src/compiler/scala/tools/nsc/util/SourceFile.scala deleted file mode 100644 index e1ae96da8c..0000000000 --- a/src/compiler/scala/tools/nsc/util/SourceFile.scala +++ /dev/null @@ -1,162 +0,0 @@ -/* NSC -- new Scala compiler - * Copyright 2005-2011 LAMP/EPFL - * @author Martin Odersky - */ - - -package scala.tools.nsc -package util - -import io.{ AbstractFile, VirtualFile } -import scala.collection.mutable.ArrayBuffer -import annotation.tailrec -import java.util.regex.Pattern -import java.io.IOException -import scala.reflect.internal.Chars._ - -/** abstract base class of a source file used in the compiler */ -abstract class SourceFile { - def content : Array[Char] // normalized, must end in SU - def file : AbstractFile - def isLineBreak(idx : Int) : Boolean - def isSelfContained: Boolean - def length : Int - def position(offset: Int) : Position = { - assert(offset < length, file + ": " + offset + " >= " + length) - new OffsetPosition(this, offset) - } - def position(line: Int, column: Int) : Position = new OffsetPosition(this, lineToOffset(line) + column) - - def offsetToLine(offset: Int): Int - def lineToOffset(index : Int): Int - - /** Map a position to a position in the underlying source file. - * For regular source files, simply return the argument. - */ - def positionInUltimateSource(position: Position) = position - override def toString() = file.name - def dbg(offset: Int) = (new OffsetPosition(this, offset)).dbgString - def path = file.path - - def beginsWith(offset: Int, text: String): Boolean = - (content drop offset) startsWith text - - def lineToString(index: Int): String = - content drop lineToOffset(index) takeWhile (c => !isLineBreakChar(c.toChar)) mkString - - @tailrec - final def skipWhitespace(offset: Int): Int = - if (content(offset).isWhitespace) skipWhitespace(offset + 1) else offset - - def identifier(pos: Position): Option[String] = None -} - -/** An object representing a missing source file. - */ -object NoSourceFile extends SourceFile { - def content = Array() - def file = NoFile - def isLineBreak(idx: Int) = false - def isSelfContained = true - def length = -1 - def offsetToLine(offset: Int) = -1 - def lineToOffset(index : Int) = -1 - override def toString = "" -} - -object NoFile extends VirtualFile("", "") - -object ScriptSourceFile { - /** Length of the script header from the given content, if there is one. - * The header begins with "#!" or "::#!" and ends with a line starting - * with "!#" or "::!#". - */ - def headerLength(cs: Array[Char]): Int = { - val headerPattern = Pattern.compile("""((?m)^(::)?!#.*|^.*/env .*)(\r|\n|\r\n)""") - val headerStarts = List("#!", "::#!") - - if (headerStarts exists (cs startsWith _)) { - val matcher = headerPattern matcher cs.mkString - if (matcher.find) matcher.end - else throw new IOException("script file does not close its header with !# or ::!#") - } - else 0 - } - def stripHeader(cs: Array[Char]): Array[Char] = cs drop headerLength(cs) - - def apply(file: AbstractFile, content: Array[Char]) = { - val underlying = new BatchSourceFile(file, content) - val headerLen = headerLength(content) - val stripped = new ScriptSourceFile(underlying, content drop headerLen, headerLen) - - stripped - } -} -import ScriptSourceFile._ - -class ScriptSourceFile(underlying: BatchSourceFile, content: Array[Char], override val start: Int) extends BatchSourceFile(underlying.file, content) { - override def isSelfContained = false - - override def positionInUltimateSource(pos: Position) = - if (!pos.isDefined) super.positionInUltimateSource(pos) - else new OffsetPosition(underlying, pos.point + start) -} - -/** a file whose contents do not change over time */ -class BatchSourceFile(val file : AbstractFile, val content: Array[Char]) extends SourceFile { - - def this(_file: AbstractFile) = this(_file, _file.toCharArray) - def this(sourceName: String, cs: Seq[Char]) = this(new VirtualFile(sourceName), cs.toArray) - def this(file: AbstractFile, cs: Seq[Char]) = this(file, cs.toArray) - - override def equals(that : Any) = that match { - case that : BatchSourceFile => file.path == that.file.path && start == that.start - case _ => false - } - override def hashCode = file.path.## + start.## - val length = content.length - def start = 0 - def isSelfContained = true - - override def identifier(pos: Position) = - if (pos.isDefined && pos.source == this && pos.point != -1) { - def isOK(c: Char) = isIdentifierPart(c) || isOperatorPart(c) - Some(new String(content drop pos.point takeWhile isOK)) - } else { - super.identifier(pos) - } - - def isLineBreak(idx: Int) = - if (idx >= length) false else { - val ch = content(idx) - // don't identify the CR in CR LF as a line break, since LF will do. - if (ch == CR) (idx + 1 == length) || (content(idx + 1) != LF) - else isLineBreakChar(ch) - } - - def calculateLineIndices(cs: Array[Char]) = { - val buf = new ArrayBuffer[Int] - buf += 0 - for (i <- 0 until cs.length) if (isLineBreak(i)) buf += i + 1 - buf += cs.length // sentinel, so that findLine below works smoother - buf.toArray - } - private lazy val lineIndices: Array[Int] = calculateLineIndices(content) - - def lineToOffset(index : Int): Int = lineIndices(index) - - private var lastLine = 0 - - /** Convert offset to line in this source file - * Lines are numbered from 0 - */ - def offsetToLine(offset: Int): Int = { - val lines = lineIndices - def findLine(lo: Int, hi: Int, mid: Int): Int = - if (offset < lines(mid)) findLine(lo, mid - 1, (lo + mid - 1) / 2) - else if (offset >= lines(mid + 1)) findLine(mid + 1, hi, (mid + 1 + hi) / 2) - else mid - lastLine = findLine(0, lines.length, lastLine) - lastLine - } -} diff --git a/src/compiler/scala/tools/nsc/util/TableDef.scala b/src/compiler/scala/tools/nsc/util/TableDef.scala deleted file mode 100644 index 10c63eeee2..0000000000 --- a/src/compiler/scala/tools/nsc/util/TableDef.scala +++ /dev/null @@ -1,95 +0,0 @@ -package scala.tools.nsc -package util - -import TableDef._ - -/** A class for representing tabular data in a way that preserves - * its inner beauty. See Exceptional for an example usage. - * One creates an instance of TableDef by defining the columns of - * the table, then uses that to create an instance of Table by - * passing in a sequence of rows. - */ -class TableDef[T](_cols: Column[T]*) { - /** These operators are about all there is to it. - * - * ~ appends a column to the table - * >> creates a right-justified column and appends it - * << creates a left-justified column and appends it - * >+ specifies a string to separate the previous column from the next. - * if none is specified, a space is used. - */ - def ~(next: Column[T]) = retThis(cols :+= next) - def >>(pair: (String, T => Any)) = this ~ Column(pair._1, pair._2, false) - def <<(pair: (String, T => Any)) = this ~ Column(pair._1, pair._2, true) - def >+(sep: String) = retThis(separators += ((cols.size - 1, sep))) - - /** Below this point should all be considered private/internal. - */ - private var cols: List[Column[T]] = _cols.toList - private var separators: Map[Int, String] = Map() - - def defaultSep(index: Int) = if (index > (cols.size - 2)) "" else " " - def sepAfter(i: Int): String = separators.getOrElse(i, defaultSep(i)) - def sepWidths = cols.indices map (i => sepAfter(i).length) - - def columns = cols - def colNames = cols map (_.name) - def colFunctions = cols map (_.f) - def colApply(el: T) = colFunctions map (f => f(el)) - def retThis(body: => Unit): this.type = { body ; this } - - class Table(val rows: Seq[T]) extends Seq[T] { - def iterator = rows.iterator - def apply(index: Int) = rows(index) - def length = rows.length - - def maxColWidth(col: Column[T]) = col.name +: (rows map col.f) map (_.toString.length) max - def specs = cols map (_ formatSpec rows) - - val colWidths = cols map maxColWidth - val rowFormat = mkFormatString(sepAfter) - val headFormat = mkFormatString(i => " " * sepWidths(i)) - val argLists = rows map colApply - - val headers = List( - headFormat.format(colNames: _*), - (colWidths, sepWidths).zipped map ((w1, w2) => "-" * w1 + " " * w2) mkString - ) - - def mkFormatString(sepf: Int => String): String = - specs.zipWithIndex map { case (c, i) => c + sepf(i) } mkString - - def pp(): Unit = allToSeq foreach println - - def toFormattedSeq = argLists map (xs => rowFormat.format(xs: _*)) - def allToSeq = headers ++ toFormattedSeq - - override def toString = allToSeq mkString "\n" - } - - def formatterFor(rows: Seq[T]): T => String = { - val formatStr = new Table(rows).rowFormat - - x => formatStr.format(colApply(x) : _*) - } - - def table(rows: Seq[T]) = new Table(rows) - - override def toString = cols.mkString("TableDef(", ", ", ")") -} - -object TableDef { - case class Column[-T](name: String, f: T => Any, left: Boolean) { - def maxWidth(elems: Seq[T]): Int = name +: (elems map f) map (_.toString.length) max - def formatSpec(elems: Seq[T]): String = { - val justify = if (left) "-" else "" - "%" + justify + maxWidth(elems) + "s" - } - override def toString = { - val justify = if (left) "<<" else ">>" - justify + "(" + name + ")" - } - } - - def apply[T](cols: Column[T]*) = new TableDef[T](cols: _*) -} diff --git a/src/compiler/scala/tools/nsc/util/WeakHashSet.scala b/src/compiler/scala/tools/nsc/util/WeakHashSet.scala deleted file mode 100644 index 5bbb766e21..0000000000 --- a/src/compiler/scala/tools/nsc/util/WeakHashSet.scala +++ /dev/null @@ -1,61 +0,0 @@ -package scala.tools.nsc.util - -import scala.collection.mutable -import scala.collection.mutable.ArrayBuffer -import scala.collection.mutable.Builder -import scala.collection.mutable.SetBuilder -import scala.collection.generic.Clearable -import scala.runtime.AbstractFunction1 - -/** A bare-bones implementation of a mutable `Set` that uses weak references - * to hold the elements. - * - * This implementation offers only add/remove/test operations, - * therefore it does not fulfill the contract of Scala collection sets. - */ -class WeakHashSet[T <: AnyRef] extends AbstractFunction1[T, Boolean] with Clearable { - private val underlying = mutable.HashSet[WeakReferenceWithEquals[T]]() - - /** Add the given element to this set. */ - def +=(elem: T): this.type = { - underlying += new WeakReferenceWithEquals(elem) - this - } - - /** Remove the given element from this set. */ - def -=(elem: T): this.type = { - underlying -= new WeakReferenceWithEquals(elem) - this - } - - /** Does the given element belong to this set? */ - def contains(elem: T): Boolean = - underlying.contains(new WeakReferenceWithEquals(elem)) - - /** Does the given element belong to this set? */ - def apply(elem: T): Boolean = contains(elem) - - /** Return the number of elements in this set, including reclaimed elements. */ - def size = underlying.size - - /** Remove all elements in this set. */ - def clear() = underlying.clear() -} - -/** A WeakReference implementation that implements equals and hashCode by - * delegating to the referent. - */ -class WeakReferenceWithEquals[T <: AnyRef](ref: T) { - def get(): T = underlying.get() - - override val hashCode = ref.hashCode - - override def equals(other: Any): Boolean = other match { - case wf: WeakReferenceWithEquals[_] => - underlying.get() == wf.get() - case _ => - false - } - - private val underlying = new java.lang.ref.WeakReference(ref) -} diff --git a/src/compiler/scala/tools/nsc/util/package.scala b/src/compiler/scala/tools/nsc/util/package.scala index 1336cca3c5..2229b8d997 100644 --- a/src/compiler/scala/tools/nsc/util/package.scala +++ b/src/compiler/scala/tools/nsc/util/package.scala @@ -86,4 +86,57 @@ package object util { lazy val trace = new SimpleTracer(System.out) lazy val errtrace = new SimpleTracer(System.err) + @deprecated("Moved to scala.reflect.internal.util.StringOps", "2.10.0") + val StringOps = scala.reflect.internal.util.StringOps + + @deprecated("Moved to scala.reflect.internal.util.StringOps", "2.10.0") + type StringOps = scala.reflect.internal.util.StringOps + + @deprecated("Moved to scala.reflect.internal.util.TableDef", "2.10.0") + val TableDef = scala.reflect.internal.util.TableDef + + @deprecated("Moved to scala.reflect.internal.util.TableDef", "2.10.0") + type TableDef[T] = scala.reflect.internal.util.TableDef[T] + + @deprecated("scala.reflect.internal.util.WeakHashSet", "2.10.0") + type WeakHashSet[T <: AnyRef] = scala.reflect.internal.util.WeakHashSet[T] + + @deprecated("Moved to scala.reflect.internal.util.Position", "2.10.0") + val Position = scala.reflect.internal.util.Position + + @deprecated("Moved to scala.reflect.internal.util.Position", "2.10.0") + type Position = scala.reflect.internal.util.Position + + @deprecated("Moved to scala.reflect.internal.util.NoPosition", "2.10.0") + val NoPosition = scala.reflect.internal.util.NoPosition + + @deprecated("Moved to scala.reflect.internal.util.FakePos", "2.10.0") + val FakePos = scala.reflect.internal.util.FakePos + + @deprecated("Moved to scala.reflect.internal.util.FakePos", "2.10.0") + type FakePos = scala.reflect.internal.util.FakePos + + @deprecated("Moved to scala.reflect.internal.util.OffsetPosition", "2.10.0") + type OffsetPosition = scala.reflect.internal.util.OffsetPosition + + @deprecated("Moved to scala.reflect.internal.util.RangePosition", "2.10.0") + type RangePosition = scala.reflect.internal.util.RangePosition + + @deprecated("Moved to scala.reflect.internal.util.SourceFile", "2.10.0") + type SourceFile = scala.reflect.internal.util.SourceFile + + @deprecated("Moved to scala.reflect.internal.util.NoSourceFile", "2.10.0") + val NoSourceFile = scala.reflect.internal.util.NoSourceFile + + @deprecated("Moved to scala.reflect.internal.util.NoFile", "2.10.0") + val NoFile = scala.reflect.internal.util.NoFile + + @deprecated("Moved to scala.reflect.internal.util.ScriptSourceFile", "2.10.0") + val ScriptSourceFile = scala.reflect.internal.util.ScriptSourceFile + + @deprecated("Moved to scala.reflect.internal.util.ScriptSourceFile", "2.10.0") + type ScriptSourceFile = scala.reflect.internal.util.ScriptSourceFile + + @deprecated("Moved to scala.reflect.internal.util.BatchSourceFile", "2.10.0") + type BatchSourceFile = scala.reflect.internal.util.BatchSourceFile } diff --git a/src/compiler/scala/tools/reflect/FrontEnds.scala b/src/compiler/scala/tools/reflect/FrontEnds.scala index 0e4dbd406a..d8f07fb2e5 100644 --- a/src/compiler/scala/tools/reflect/FrontEnds.scala +++ b/src/compiler/scala/tools/reflect/FrontEnds.scala @@ -7,7 +7,7 @@ import scala.reflect.ClassTag trait FrontEnds extends scala.reflect.api.FrontEnds { - type Position = scala.tools.nsc.util.Position + type Position = scala.reflect.internal.util.Position def mkConsoleFrontEnd(minSeverity: Int = 1): FrontEnd = { val settings = new Settings() diff --git a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala index 741f1b268f..7cf515425d 100644 --- a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala +++ b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala @@ -8,9 +8,9 @@ import scala.tools.nsc.Global import scala.tools.nsc.typechecker.Modes import scala.tools.nsc.io.VirtualDirectory import scala.tools.nsc.interpreter.AbstractFileClassLoader -import scala.tools.nsc.util.{FreshNameCreator, BatchSourceFile} +import scala.tools.nsc.util.FreshNameCreator import scala.reflect.internal.Flags -import scala.tools.nsc.util.{NoSourceFile, NoFile} +import scala.reflect.internal.util.{BatchSourceFile, NoSourceFile, NoFile} import java.lang.{Class => jClass} import scala.compat.Platform.EOL import scala.reflect.NameTransformer @@ -289,7 +289,7 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => lazy val compiler: ToolBoxGlobal = { try { - val errorFn: String => Unit = msg => frontEnd.log(scala.tools.nsc.util.NoPosition, msg, frontEnd.ERROR) + val errorFn: String => Unit = msg => frontEnd.log(scala.reflect.internal.util.NoPosition, msg, frontEnd.ERROR) val command = new CompilerCommand(arguments.toList, errorFn) command.settings.outputDirs setSingleOutput virtualDirectory val instance = new ToolBoxGlobal(command.settings, new FrontEndToReporterProxy(frontEnd) { val settings = command.settings }) diff --git a/src/compiler/scala/tools/util/StringOps.scala b/src/compiler/scala/tools/util/StringOps.scala deleted file mode 100644 index 725e0afb79..0000000000 --- a/src/compiler/scala/tools/util/StringOps.scala +++ /dev/null @@ -1,100 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2002-2011, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - - -package scala.tools -package util - -/** This object provides utility methods to extract elements - * from Strings. - * - * @author Martin Odersky - * @version 1.0 - */ -trait StringOps { - def onull(s: String) = if (s == null) "" else s - def oempty(xs: String*) = xs filterNot (x => x == null || x == "") - def ojoin(xs: String*): String = oempty(xs: _*) mkString " " - def ojoin(xs: Seq[String], sep: String): String = oempty(xs: _*) mkString sep - def ojoinOr(xs: Seq[String], sep: String, orElse: String) = { - val ys = oempty(xs: _*) - if (ys.isEmpty) orElse else ys mkString sep - } - def trimTrailingSpace(s: String) = { - if (s.length == 0 || !s.charAt(s.length - 1).isWhitespace) s - else { - var idx = s.length - 1 - while (idx >= 0 && s.charAt(idx).isWhitespace) - idx -= 1 - - s.substring(0, idx + 1) - } - } - - def decompose(str: String, sep: Char): List[String] = { - def ws(start: Int): List[String] = - if (start == str.length) List() - else if (str.charAt(start) == sep) ws(start + 1) - else { - val end = str.indexOf(sep, start) - if (end < 0) List(str.substring(start)) - else str.substring(start, end) :: ws(end + 1) - } - ws(0) - } - - def words(str: String): List[String] = decompose(str, ' ') - - def stripPrefixOpt(str: String, prefix: String): Option[String] = - if (str startsWith prefix) Some(str drop prefix.length) - else None - - def stripSuffixOpt(str: String, suffix: String): Option[String] = - if (str endsWith suffix) Some(str dropRight suffix.length) - else None - - def splitWhere(str: String, f: Char => Boolean, doDropIndex: Boolean = false): Option[(String, String)] = - splitAt(str, str indexWhere f, doDropIndex) - - def splitAt(str: String, idx: Int, doDropIndex: Boolean = false): Option[(String, String)] = - if (idx == -1) None - else Some((str take idx, str drop (if (doDropIndex) idx + 1 else idx))) - - /** Returns a string meaning "n elements". - * - * @param n ... - * @param elements ... - * @return ... - */ - def countElementsAsString(n: Int, elements: String): String = - n match { - case 0 => "no " + elements + "s" - case 1 => "one " + elements - case 2 => "two " + elements + "s" - case 3 => "three " + elements + "s" - case 4 => "four " + elements + "s" - case _ => "" + n + " " + elements + "s" - } - - /** Turns a count into a friendly English description if n<=4. - * - * @param n ... - * @return ... - */ - def countAsString(n: Int): String = - n match { - case 0 => "none" - case 1 => "one" - case 2 => "two" - case 3 => "three" - case 4 => "four" - case _ => "" + n - } -} - -object StringOps extends StringOps { } -- cgit v1.2.3