|
| 1 | +package dotty.tools.dotc |
| 2 | +package config |
| 3 | + |
| 4 | +import scala.annotation.tailrec |
| 5 | +import dotty.tools.sharable |
| 6 | + |
| 7 | +/** A simple (overly so) command line parser. |
| 8 | + * !!! This needs a thorough test suite to make sure quoting is |
| 9 | + * done correctly and portably. |
| 10 | + */ |
| 11 | +object CommandLineParser { |
| 12 | + // splits a string into a quoted prefix and the rest of the string, |
| 13 | + // taking escaping into account (using \) |
| 14 | + // `"abc"def` will match as `DoubleQuoted(abc, def)` |
| 15 | + private class QuotedExtractor(quote: Char) { |
| 16 | + def unapply(in: String): Option[(String, String)] = { |
| 17 | + val del = quote.toString |
| 18 | + if (in startsWith del) { |
| 19 | + var escaped = false |
| 20 | + val (quoted, next) = (in substring 1) span { |
| 21 | + case `quote` if !escaped => false |
| 22 | + case '\\' if !escaped => escaped = true; true |
| 23 | + case _ => escaped = false; true |
| 24 | + } |
| 25 | + // the only way to get out of the above loop is with an empty next or !escaped |
| 26 | + // require(next.isEmpty || !escaped) |
| 27 | + if (next startsWith del) Some((quoted, next substring 1)) |
| 28 | + else None |
| 29 | + } else None |
| 30 | + } |
| 31 | + } |
| 32 | + private object DoubleQuoted extends QuotedExtractor('"') |
| 33 | + private object SingleQuoted extends QuotedExtractor('\'') |
| 34 | + @sharable private val Word = """(\S+)(.*)""".r |
| 35 | + |
| 36 | + // parse `in` for an argument, return it and the remainder of the input (or an error message) |
| 37 | + // (argument may be in single/double quotes, taking escaping into account, quotes are stripped) |
| 38 | + private def argument(in: String): Either[String, (String, String)] = in match { |
| 39 | + case DoubleQuoted(arg, rest) => Right((arg, rest)) |
| 40 | + case SingleQuoted(arg, rest) => Right((arg, rest)) |
| 41 | + case Word(arg, rest) => Right((arg, rest)) |
| 42 | + case _ => Left(s"Illegal argument: $in") |
| 43 | + } |
| 44 | + |
| 45 | + // parse a list of whitespace-separated arguments (ignoring whitespace in quoted arguments) |
| 46 | + @tailrec private def commandLine(in: String, accum: List[String] = Nil): Either[String, (List[String], String)] = { |
| 47 | + val trimmed = in.trim |
| 48 | + if (trimmed.isEmpty) Right((accum.reverse, "")) |
| 49 | + else argument(trimmed) match { |
| 50 | + case Right((arg, next)) => |
| 51 | + (next span Character.isWhitespace) match { |
| 52 | + case("", rest) if rest.nonEmpty => Left("Arguments should be separated by whitespace.") // TODO: can this happen? |
| 53 | + case(ws, rest) => commandLine(rest, arg :: accum) |
| 54 | + } |
| 55 | + case Left(msg) => Left(msg) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + class ParseException(msg: String) extends RuntimeException(msg) |
| 60 | + |
| 61 | + def tokenize(line: String): List[String] = tokenize(line, x => throw new ParseException(x)) |
| 62 | + def tokenize(line: String, errorFn: String => Unit): List[String] = { |
| 63 | + commandLine(line) match { |
| 64 | + case Right((args, _)) => args |
| 65 | + case Left(msg) => errorFn(msg) ; Nil |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments