|
| 1 | +package dotty.tools.dotc.semanticdb |
| 2 | + |
| 3 | +import java.lang.System.{lineSeparator => EOL} |
| 4 | +import dotty.tools.dotc.semanticdb.{Descriptor => d} |
| 5 | + |
| 6 | +class DescriptorParser(s: String) { |
| 7 | + var i = s.length |
| 8 | + def fail() = { |
| 9 | + val message = "invalid symbol format" |
| 10 | + val caret = " " * i + "^" |
| 11 | + sys.error(s"$message$EOL$s$EOL$caret") |
| 12 | + } |
| 13 | + |
| 14 | + val BOF = '\u0000' |
| 15 | + val EOF = '\u001A' |
| 16 | + var currChar = EOF |
| 17 | + def readChar(): Char = { |
| 18 | + if (i <= 0) { |
| 19 | + if (i == 0) { |
| 20 | + i -= 1 |
| 21 | + currChar = BOF |
| 22 | + currChar |
| 23 | + } else { |
| 24 | + fail() |
| 25 | + } |
| 26 | + } else { |
| 27 | + i -= 1 |
| 28 | + currChar = s(i) |
| 29 | + currChar |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + def parseValue(): String = { |
| 34 | + if (currChar == '`') { |
| 35 | + val end = i |
| 36 | + while (readChar() != '`') {} |
| 37 | + readChar() |
| 38 | + s.substring(i + 2, end) |
| 39 | + } else { |
| 40 | + val end = i + 1 |
| 41 | + if (!Character.isJavaIdentifierPart(currChar)) fail() |
| 42 | + while (Character.isJavaIdentifierPart(readChar()) && currChar != BOF) {} |
| 43 | + s.substring(i + 1, end) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + def parseDisambiguator(): String = { |
| 48 | + val end = i + 1 |
| 49 | + if (currChar != ')') fail() |
| 50 | + while (readChar() != '(') {} |
| 51 | + readChar() |
| 52 | + s.substring(i + 1, end) |
| 53 | + } |
| 54 | + |
| 55 | + def parseDescriptor(): Descriptor = { |
| 56 | + if (currChar == '.') { |
| 57 | + readChar() |
| 58 | + if (currChar == ')') { |
| 59 | + val disambiguator = parseDisambiguator() |
| 60 | + val value = parseValue() |
| 61 | + d.Method(value, disambiguator) |
| 62 | + } else { |
| 63 | + d.Term(parseValue()) |
| 64 | + } |
| 65 | + } else if (currChar == '#') { |
| 66 | + readChar() |
| 67 | + d.Type(parseValue()) |
| 68 | + } else if (currChar == '/') { |
| 69 | + readChar() |
| 70 | + d.Package(parseValue()) |
| 71 | + } else if (currChar == ')') { |
| 72 | + readChar() |
| 73 | + val value = parseValue() |
| 74 | + if (currChar != '(') fail() |
| 75 | + else readChar() |
| 76 | + d.Parameter(value) |
| 77 | + } else if (currChar == ']') { |
| 78 | + readChar() |
| 79 | + val value = parseValue() |
| 80 | + if (currChar != '[') fail() |
| 81 | + else readChar() |
| 82 | + d.TypeParameter(value) |
| 83 | + } else { |
| 84 | + fail() |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + def entryPoint(): (Descriptor, String) = { |
| 89 | + readChar() |
| 90 | + val desc = parseDescriptor() |
| 91 | + (desc, s.substring(0, i + 1)) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +object DescriptorParser { |
| 96 | + def apply(symbol: String): (Descriptor, String) = { |
| 97 | + val parser = new DescriptorParser(symbol) |
| 98 | + parser.entryPoint() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +sealed trait Descriptor { |
| 103 | + def isNone: Boolean = this == d.None |
| 104 | + def isTerm: Boolean = this.isInstanceOf[d.Term] |
| 105 | + def isMethod: Boolean = this.isInstanceOf[d.Method] |
| 106 | + def isType: Boolean = this.isInstanceOf[d.Type] |
| 107 | + def isPackage: Boolean = this.isInstanceOf[d.Package] |
| 108 | + def isParameter: Boolean = this.isInstanceOf[d.Parameter] |
| 109 | + def isTypeParameter: Boolean = this.isInstanceOf[d.TypeParameter] |
| 110 | + def value: String |
| 111 | +} |
| 112 | +object Descriptor { |
| 113 | + case object None extends Descriptor { def value: String = "" } |
| 114 | + final case class Term(value: String) extends Descriptor |
| 115 | + final case class Method(value: String, disambiguator: String) extends Descriptor |
| 116 | + final case class Type(value: String) extends Descriptor |
| 117 | + final case class Package(value: String) extends Descriptor |
| 118 | + final case class Parameter(value: String) extends Descriptor |
| 119 | + final case class TypeParameter(value: String) extends Descriptor |
| 120 | +} |
0 commit comments