summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interpreter/Parsed.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-02-01 22:35:12 +0000
committerPaul Phillips <paulp@improving.org>2010-02-01 22:35:12 +0000
commitb80125cb3fa7ab12b5921ee930c04e9d95384861 (patch)
tree2e3c09c8c62a7e13facef213d01e0d72009e432b /src/compiler/scala/tools/nsc/interpreter/Parsed.scala
parent3282ac260cebe12a2d0dcb6bb7c0e479e0e20c6c (diff)
downloadscala-b80125cb3fa7ab12b5921ee930c04e9d95384861.tar.gz
scala-b80125cb3fa7ab12b5921ee930c04e9d95384861.tar.bz2
scala-b80125cb3fa7ab12b5921ee930c04e9d95384861.zip
Quite a lot more work on completion.
completion is now avilable, with some caveats. Review by community.
Diffstat (limited to 'src/compiler/scala/tools/nsc/interpreter/Parsed.scala')
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/Parsed.scala71
1 files changed, 52 insertions, 19 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/Parsed.scala b/src/compiler/scala/tools/nsc/interpreter/Parsed.scala
index 885893ee53..05cb2641cd 100644
--- a/src/compiler/scala/tools/nsc/interpreter/Parsed.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/Parsed.scala
@@ -6,26 +6,59 @@
package scala.tools.nsc
package interpreter
+import jline.ArgumentCompletor.{ ArgumentDelimiter, ArgumentList }
+
/** One instance of a command buffer.
*/
-class Parsed(_buf: String) {
- val buffer = if (_buf == null) "" else _buf
- val segments = (buffer split '.').toList filterNot (_ == "")
- lazy val hd :: tl = segments
- def stub = firstDot + hd + "."
- def remainder = buffer stripPrefix stub
- def unqualifiedPart = segments.last
-
- def isEmpty = segments.size == 0
- def isUnqualified = segments.size == 1
- def isQualified = segments.size > 1
-
- def isFirstCharDot = buffer startsWith "."
- def isLastCharDot = buffer endsWith "."
- def firstDot = if (isFirstCharDot) "." else ""
- def lastDot = if (isLastCharDot) "." else ""
-
- // sneakily, that is 0 when there is no dot, which is what we want
- def position = (buffer lastIndexOf '.') + 1
+class Parsed private (
+ val buffer: String,
+ val cursor: Int,
+ val delimited: Char => Boolean
+) extends Delimited {
+ def isEmpty = buffer == ""
+ def isUnqualified = args.size == 1
+ def isQualified = args.size > 1
+ def isAtStart = cursor <= 0
+
+ def args = toArgs(buffer take cursor).toList
+ def bufferHead = args.head
+ def headLength = bufferHead.length + 1
+ def bufferTail = new Parsed(buffer drop headLength, cursor - headLength, delimited)
+
+ def prev = new Parsed(buffer, cursor - 1, delimited)
+ def next = new Parsed(buffer, cursor + 1, delimited)
+ def currentChar = buffer(cursor)
+ def currentArg = args.last
+ def position =
+ if (isEmpty) 0
+ else if (isLastDelimiter) cursor
+ else cursor - currentArg.length
+
+ def isFirstDelimiter = !isEmpty && isDelimiterChar(buffer.head)
+ def isLastDelimiter = !isEmpty && isDelimiterChar(buffer.last)
+ def firstIfDelimiter = if (isFirstDelimiter) buffer.head.toString else ""
+ def lastIfDelimiter = if (isLastDelimiter) buffer.last.toString else ""
+
+ def isQuoted = false // TODO
+ def isEscaped = !isAtStart && isEscapeChar(currentChar) && !isEscapeChar(prev.currentChar)
+ def isDelimiter = !isQuoted && !isEscaped && isDelimiterChar(currentChar)
+
+ def asJlineArgumentList =
+ if (isEmpty) new ArgumentList(Array[String](), 0, 0, cursor)
+ else new ArgumentList(args.toArray, args.size - 1, currentArg.length, cursor)
+
+ override def toString = "Parsed(%s / %d)".format(buffer, cursor)
}
+object Parsed {
+ def onull(s: String) = if (s == null) "" else s
+ def apply(s: String): Parsed = apply(onull(s), onull(s).length)
+ def apply(s: String, cursor: Int): Parsed = apply(onull(s), cursor, "(){},`; \t" contains _)
+ def apply(s: String, cursor: Int, delimited: Char => Boolean): Parsed =
+ new Parsed(onull(s), cursor, delimited)
+
+ def dotted(s: String): Parsed = dotted(onull(s), onull(s).length)
+ def dotted(s: String, cursor: Int): Parsed = new Parsed(onull(s), cursor, _ == '.')
+
+ def undelimited(s: String, cursor: Int): Parsed = new Parsed(onull(s), cursor, _ => false)
+}