|
1 | 1 | /* NEST (New Scala Test) |
2 | | - * Copyright 2007-2013 LAMP/EPFL |
| 2 | + * Copyright 2007-2018 LAMP/EPFL |
3 | 3 | * @author Paul Phillips |
4 | 4 | */ |
5 | | - |
6 | | -package scala.tools |
7 | | -package cmd |
| 5 | +package scala.tools.cmd |
8 | 6 |
|
9 | 7 | import scala.annotation.tailrec |
10 | 8 |
|
11 | | -/** A simple (overly so) command line parser. |
12 | | - * !!! This needs a thorough test suite to make sure quoting is |
13 | | - * done correctly and portably. |
| 9 | +/** A simple enough command line parser. |
14 | 10 | */ |
15 | 11 | object CommandLineParser { |
16 | | - // splits a string into a quoted prefix and the rest of the string, |
17 | | - // taking escaping into account (using \) |
18 | | - // `"abc"def` will match as `DoubleQuoted(abc, def)` |
19 | | - private class QuotedExtractor(quote: Char) { |
20 | | - def unapply(in: String): Option[(String, String)] = { |
21 | | - val del = quote.toString |
22 | | - if (in startsWith del) { |
23 | | - var escaped = false |
24 | | - val (quoted, next) = (in substring 1) span { |
25 | | - case `quote` if !escaped => false |
26 | | - case '\\' if !escaped => escaped = true; true |
27 | | - case _ => escaped = false; true |
28 | | - } |
29 | | - // the only way to get out of the above loop is with an empty next or !escaped |
30 | | - // require(next.isEmpty || !escaped) |
31 | | - if (next startsWith del) Some((quoted, next substring 1)) |
32 | | - else None |
33 | | - } else None |
| 12 | + private final val DQ = '"' |
| 13 | + private final val SQ = '\'' |
| 14 | + |
| 15 | + /** Split the line into tokens separated by whitespace or quotes. |
| 16 | + * |
| 17 | + * @return either an error message or reverse list of tokens |
| 18 | + */ |
| 19 | + private def tokens(in: String) = { |
| 20 | + import Character.isWhitespace |
| 21 | + import java.lang.{StringBuilder => Builder} |
| 22 | + import collection.mutable.ArrayBuffer |
| 23 | + |
| 24 | + var accum: List[String] = Nil |
| 25 | + var pos = 0 |
| 26 | + var start = 0 |
| 27 | + val qpos = new ArrayBuffer[Int](16) // positions of paired quotes |
| 28 | + |
| 29 | + def cur: Int = if (done) -1 else in.charAt(pos) |
| 30 | + def bump() = pos += 1 |
| 31 | + def done = pos >= in.length |
| 32 | + |
| 33 | + def skipToQuote(q: Int) = { |
| 34 | + var escaped = false |
| 35 | + def terminal = in.charAt(pos) match { |
| 36 | + case _ if escaped => escaped = false ; false |
| 37 | + case '\\' => escaped = true ; false |
| 38 | + case `q` => true |
| 39 | + case _ => false |
| 40 | + } |
| 41 | + while (!done && !terminal) pos += 1 |
| 42 | + !done |
34 | 43 | } |
35 | | - } |
36 | | - private object DoubleQuoted extends QuotedExtractor('"') |
37 | | - private object SingleQuoted extends QuotedExtractor('\'') |
38 | | - object Word { |
39 | | - private val regex = """(\S+)""".r |
40 | | - def unapply(s: String): Option[(String, String)] = { |
41 | | - regex.findPrefixOf(s) match { |
42 | | - case Some(prefix) => Some(prefix, s.substring(prefix.length)) |
43 | | - case None => None |
| 44 | + def skipToDelim(): Boolean = |
| 45 | + cur match { |
| 46 | + case q @ (DQ | SQ) => { qpos.append(pos); bump(); skipToQuote(q) } && { qpos.append(pos); bump(); skipToDelim() } |
| 47 | + case -1 => true |
| 48 | + case c if isWhitespace(c) => true |
| 49 | + case _ => bump(); skipToDelim() |
| 50 | + } |
| 51 | + def skipWhitespace() = while (isWhitespace(cur)) pos += 1 |
| 52 | + def copyText() = { |
| 53 | + val buf = new Builder |
| 54 | + var p = start |
| 55 | + var i = 0 |
| 56 | + while (p < pos) { |
| 57 | + if (i >= qpos.size) { |
| 58 | + buf.append(in, p, pos) |
| 59 | + p = pos |
| 60 | + } else if (p == qpos(i)) { |
| 61 | + buf.append(in, qpos(i)+1, qpos(i+1)) |
| 62 | + p = qpos(i+1)+1 |
| 63 | + i += 2 |
| 64 | + } else { |
| 65 | + buf.append(in, p, qpos(i)) |
| 66 | + p = qpos(i) |
| 67 | + } |
44 | 68 | } |
| 69 | + buf.toString |
45 | 70 | } |
46 | | - } |
47 | | - |
48 | | - // parse `in` for an argument, return it and the remainder of the input (or an error message) |
49 | | - // (argument may be in single/double quotes, taking escaping into account, quotes are stripped) |
50 | | - private def argument(in: String): Either[String, (String, String)] = in match { |
51 | | - case DoubleQuoted(arg, rest) => Right((arg, rest)) |
52 | | - case SingleQuoted(arg, rest) => Right((arg, rest)) |
53 | | - case Word(arg, rest) => Right((arg, rest)) |
54 | | - case _ => Left(s"Illegal argument: $in") |
55 | | - } |
| 71 | + def text() = { |
| 72 | + val res = |
| 73 | + if (qpos.isEmpty) in.substring(start, pos) |
| 74 | + else if (qpos(0) == start && qpos(1) == pos) in.substring(start+1, pos-1) |
| 75 | + else copyText() |
| 76 | + qpos.clear() |
| 77 | + res |
| 78 | + } |
| 79 | + def badquote = Left("Unmatched quote") |
56 | 80 |
|
57 | | - // parse a list of whitespace-separated arguments (ignoring whitespace in quoted arguments) |
58 | | - @tailrec private def commandLine(in: String, accum: List[String] = Nil): Either[String, (List[String], String)] = { |
59 | | - val trimmed = in.trim |
60 | | - if (trimmed.isEmpty) Right((accum.reverse, "")) |
61 | | - else argument(trimmed) match { |
62 | | - case Right((arg, next)) => |
63 | | - val leadingWhitespaceLen = next.prefixLength(Character.isWhitespace) |
64 | | - val rest = next.substring(leadingWhitespaceLen) |
65 | | - if (leadingWhitespaceLen == 0 && rest.nonEmpty) |
66 | | - Left("Arguments should be separated by whitespace.") // TODO: can this happen? |
67 | | - else |
68 | | - commandLine(rest, arg :: accum) |
69 | | - case Left(msg) => Left(msg) |
| 81 | + @tailrec def loop(): Either[String, List[String]] = { |
| 82 | + skipWhitespace() |
| 83 | + start = pos |
| 84 | + if (done) Right(accum) |
| 85 | + else if (!skipToDelim()) badquote |
| 86 | + else { |
| 87 | + accum = text() :: accum |
| 88 | + loop() |
| 89 | + } |
70 | 90 | } |
| 91 | + loop() |
71 | 92 | } |
72 | 93 |
|
73 | 94 | class ParseException(msg: String) extends RuntimeException(msg) |
74 | 95 |
|
75 | | - def tokenize(line: String): List[String] = tokenize(line, x => throw new ParseException(x)) |
76 | | - def tokenize(line: String, errorFn: String => Unit): List[String] = { |
77 | | - commandLine(line) match { |
78 | | - case Right((args, _)) => args |
79 | | - case Left(msg) => errorFn(msg) ; Nil |
| 96 | + def tokenize(line: String, errorFn: String => Unit): List[String] = |
| 97 | + tokens(line) match { |
| 98 | + case Right(args) => args.reverse |
| 99 | + case Left(msg) => errorFn(msg) ; Nil |
80 | 100 | } |
81 | | - } |
| 101 | + |
| 102 | + def tokenize(line: String): List[String] = tokenize(line, x => throw new ParseException(x)) |
82 | 103 | } |
0 commit comments