|
1 |
| -package dotty.tools.dotc |
2 |
| -package config |
| 1 | +package dotty.tools.dotc.config |
3 | 2 |
|
4 | 3 | import scala.annotation.tailrec
|
| 4 | +import scala.collection.mutable.ArrayBuffer |
| 5 | +import java.lang.Character.isWhitespace |
5 | 6 |
|
6 | 7 | /** A simple enough command line parser.
|
7 | 8 | */
|
8 |
| -object CommandLineParser { |
9 |
| - private final val DQ = '"' |
10 |
| - private final val SQ = '\'' |
| 9 | +object CommandLineParser: |
| 10 | + inline private val DQ = '"' |
| 11 | + inline private val SQ = '\'' |
| 12 | + inline private val EOF = -1 |
11 | 13 |
|
12 | 14 | /** Split the line into tokens separated by whitespace or quotes.
|
13 | 15 | *
|
14 |
| - * @return either an error message or reverse list of tokens |
| 16 | + * Invoke `errorFn` with message on bad quote. |
15 | 17 | */
|
16 |
| - private def tokens(in: String) = { |
17 |
| - import Character.isWhitespace |
18 |
| - import java.lang.{StringBuilder => Builder} |
19 |
| - import collection.mutable.ArrayBuffer |
| 18 | + def tokenize(line: String, errorFn: String => Unit): List[String] = |
20 | 19 |
|
21 | 20 | var accum: List[String] = Nil
|
22 |
| - var pos = 0 |
| 21 | + |
| 22 | + var pos = 0 |
23 | 23 | var start = 0
|
24 |
| - val qpos = new ArrayBuffer[Int](16) // positions of paired quotes |
| 24 | + val qpos = new ArrayBuffer[Int](16) // positions of paired quotes |
25 | 25 |
|
26 |
| - def cur: Int = if (done) -1 else in.charAt(pos) |
27 |
| - def bump() = pos += 1 |
28 |
| - def done = pos >= in.length |
| 26 | + inline def cur = if done then EOF else line.charAt(pos): Int |
| 27 | + inline def bump() = pos += 1 |
| 28 | + inline def done = pos >= line.length |
29 | 29 |
|
30 |
| - def skipToQuote(q: Int) = { |
| 30 | + def skipToQuote(q: Int): Boolean = |
31 | 31 | var escaped = false
|
32 |
| - def terminal = in.charAt(pos) match { |
| 32 | + def terminal = cur match |
33 | 33 | case _ if escaped => escaped = false ; false
|
34 | 34 | case '\\' => escaped = true ; false
|
35 |
| - case `q` => true |
| 35 | + case `q` | EOF => true |
36 | 36 | case _ => false
|
37 |
| - } |
38 |
| - while (!done && !terminal) pos += 1 |
| 37 | + while !terminal do bump() |
39 | 38 | !done
|
40 |
| - } |
41 |
| - @tailrec |
42 |
| - def skipToDelim(): Boolean = |
43 |
| - cur match { |
44 |
| - case q @ (DQ | SQ) => { qpos += pos; bump(); skipToQuote(q) } && { qpos += pos; bump(); skipToDelim() } |
| 39 | + |
| 40 | + @tailrec def skipToDelim(): Boolean = |
| 41 | + inline def quote() = { qpos += pos ; bump() } |
| 42 | + cur match |
| 43 | + case q @ (DQ | SQ) => { quote() ; skipToQuote(q) } && { quote() ; skipToDelim() } |
45 | 44 | case -1 => true
|
46 | 45 | case c if isWhitespace(c) => true
|
47 | 46 | case _ => bump(); skipToDelim()
|
48 |
| - } |
49 |
| - def skipWhitespace() = while (isWhitespace(cur)) pos += 1 |
50 |
| - def copyText() = { |
51 |
| - val buf = new Builder |
| 47 | + |
| 48 | + def copyText(): String = |
| 49 | + val buf = new java.lang.StringBuilder |
52 | 50 | var p = start
|
53 | 51 | var i = 0
|
54 |
| - while (p < pos) { |
55 |
| - if (i >= qpos.size) { |
56 |
| - buf.append(in, p, pos) |
| 52 | + while p < pos do |
| 53 | + if i >= qpos.size then |
| 54 | + buf.append(line, p, pos) |
57 | 55 | p = pos
|
58 |
| - } else if (p == qpos(i)) { |
59 |
| - buf.append(in, qpos(i)+1, qpos(i+1)) |
| 56 | + else if p == qpos(i) then |
| 57 | + buf.append(line, qpos(i)+1, qpos(i+1)) |
60 | 58 | p = qpos(i+1)+1
|
61 | 59 | i += 2
|
62 |
| - } else { |
63 |
| - buf.append(in, p, qpos(i)) |
| 60 | + else |
| 61 | + buf.append(line, p, qpos(i)) |
64 | 62 | p = qpos(i)
|
65 |
| - } |
66 |
| - } |
67 | 63 | buf.toString
|
68 |
| - } |
69 |
| - def text() = { |
| 64 | + |
| 65 | + def text(): String = |
70 | 66 | val res =
|
71 |
| - if (qpos.isEmpty) in.substring(start, pos) |
72 |
| - else if (qpos(0) == start && qpos(1) == pos) in.substring(start+1, pos-1) |
| 67 | + if qpos.isEmpty then line.substring(start, pos) |
| 68 | + else if qpos(0) == start && qpos(1) == pos then line.substring(start+1, pos-1) |
73 | 69 | else copyText()
|
74 | 70 | qpos.clear()
|
75 | 71 | res
|
76 |
| - } |
77 |
| - def badquote = Left("Unmatched quote") |
78 | 72 |
|
79 |
| - @tailrec def loop(): Either[String, List[String]] = { |
| 73 | + inline def badquote() = errorFn(s"Unmatched quote [${qpos.last}](${line.charAt(qpos.last)})") |
| 74 | + |
| 75 | + inline def skipWhitespace() = while isWhitespace(cur) do pos += 1 |
| 76 | + |
| 77 | + @tailrec def loop(): List[String] = |
80 | 78 | skipWhitespace()
|
81 | 79 | start = pos
|
82 |
| - if (done) Right(accum) |
83 |
| - else if (!skipToDelim()) badquote |
84 |
| - else { |
| 80 | + if done then |
| 81 | + accum.reverse |
| 82 | + else if !skipToDelim() then |
| 83 | + badquote() |
| 84 | + Nil |
| 85 | + else |
85 | 86 | accum = text() :: accum
|
86 | 87 | loop()
|
87 |
| - } |
88 |
| - } |
89 |
| - loop() |
90 |
| - } |
| 88 | + end loop |
91 | 89 |
|
92 |
| - class ParseException(msg: String) extends RuntimeException(msg) |
| 90 | + loop() |
93 | 91 |
|
94 |
| - def tokenize(line: String, errorFn: String => Unit): List[String] = |
95 |
| - tokens(line) match { |
96 |
| - case Right(args) => args.reverse |
97 |
| - case Left(msg) => errorFn(msg) ; Nil |
98 |
| - } |
| 92 | + end tokenize |
99 | 93 |
|
100 | 94 | def tokenize(line: String): List[String] = tokenize(line, x => throw new ParseException(x))
|
101 |
| -} |
| 95 | + |
| 96 | + class ParseException(msg: String) extends RuntimeException(msg) |
0 commit comments