-
Notifications
You must be signed in to change notification settings - Fork 224
/
ScaladocParser.scala
264 lines (227 loc) · 9.62 KB
/
ScaladocParser.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package scala.meta.internal.parsers
import java.nio.CharBuffer
import java.util.regex.Pattern
import scala.meta.internal.Scaladoc
import scala.meta.internal.fastparse
import scala.meta.internal.fastparse._
import scala.meta.internal.fastparse.NoWhitespace._
/**
* Represents a scaladoc line.
*/
object ScaladocParser {
import Scaladoc._
private val numberOfSupportedHeadingLevels = 6
private def hspace[_: P] = CharIn("\t\r ")
private def hspacesMin[_: P](min: Int) = P(CharsWhileIn("\t\r ", min))
private def hspaces0[_: P] = hspacesMin(0)
private def hspaces1[_: P] = hspacesMin(1)
private def plusMinus[_: P] = CharsWhileIn("+\\-")
private def hspacesMinWithLen[_: P](min: Int): P[Int] =
(Index ~ hspacesMin(min) ~ Index).map { case (b, e) => e - b }
private def nl[_: P]: P0 = P("\n")
private def startOrNl[_: P] = nl | Start
private def paraEnd[_: P] = nl.rep(exactly = 2)
private def space[_: P] = CharIn("\t\r \n")
private def spacesMin[_: P](min: Int) = CharsWhileIn("\t\r \n", min)
private def spaces1[_: P] = spacesMin(1)
private def nlHspaces0[_: P] = nl.? ~ hspaces0
private def nlHspaces1[_: P] = space ~ hspaces0
private def leadHspaces0[_: P] = startOrNl ~ hspaces0
private def punctParser[_: P] = CharsWhileIn(".,:!?;)", 0)
private def labelParser[_: P]: P[Unit] = (!space ~ AnyChar).rep(1)
private def wordParser[_: P]: P[Word] = P(labelParser.!.map(Word.apply))
private def listPrefix[_: P] = "-" | CharIn("1aiI") ~ "."
private def escape[_: P] = P("\\")
private def tableSep[_: P] = P("|")
private def tableSpaceSep[_: P] = P(hspaces0 ~ tableSep)
private def codePrefix[_: P] = P("{{{")
private def codeSuffix[_: P] = P(hspaces0 ~ "}}}")
private def linkPrefix[_: P] = P("[[" ~ hspaces0)
private def linkSuffix[_: P] = P(hspaces0 ~ "]]")
private def codeLineParser[_: P]: P[String] = P {
def codeLineEnd = P(nl | codeSuffix)
(!codeLineEnd ~ AnyChar).rep.!
}
private def codeExprParser[_: P]: P[CodeExpr] = P {
def pattern = codePrefix ~ hspaces0 ~ codeLineParser ~ codeSuffix ~ punctParser.!
pattern.map { case (x, y) => CodeExpr(x.trim, y) }
}
private def codeBlockParser[_: P]: P[CodeBlock] = P {
def code = codeLineParser.rep(1, sep = nl)
def pattern = leadHspaces0 ~ codePrefix ~ nl ~ code ~ codeSuffix
pattern.map { x => CodeBlock(if (x.last.nonEmpty) x.toSeq else x.view.dropRight(1).toSeq) }
}
/*
* Markdown fenced code blocks
* https://spec.commonmark.org/0.29/#fenced-code-blocks
*/
private def mdCodeBlockIndent[_: P] = hspace.rep(max = 3)
private def mdCodeBlockFence[_: P] = "`".rep(3) | "~".rep(3)
private def mdCodeBlockParser[_: P]: P[MdCodeBlock] = P {
(startOrNl ~ mdCodeBlockIndent.! ~ mdCodeBlockFence.!).flatMap { case (indent, fence) =>
def lineEnd = hspaces0 ~ nl
def info = hspaces0 ~ labelParser.!.rep(0, sep = hspaces0) ~ lineEnd
info.flatMap { infoSeq =>
def codeEnd = mdCodeBlockIndent ~ fence.substring(0, 1).rep(fence.length)
def line = hspace.rep(max = indent.length) ~ (!lineEnd ~ AnyChar).rep.!
def lines = !codeEnd ~ line.rep(1, sep = lineEnd ~ !codeEnd)
def block = (lines ~ lineEnd).? ~ codeEnd ~ hspaces0 ~ &(nl | End)
block.map(x => MdCodeBlock(infoSeq, x.getOrElse(Nil), fence))
}
}
}
private def headingParser[_: P]: P[Heading] = P {
def delimParser = leadHspaces0 ~ CharsWhileIn("=", 1).!
// heading delimiter
delimParser.flatMap { delim =>
val level = delim.length
if (level > numberOfSupportedHeadingLevels) Fail
else {
def title = (!delim ~ AnyChar).rep(1)
// Heading description and delimiter
(title.! ~ delim ~ &(nl)).map(x => Heading(level, x.trim))
}
}
}
private def linkParser[_: P]: P[Link] = P {
def end = space | linkSuffix
def anchor = P((!end ~ AnyChar).rep(1).!.rep(1, sep = spaces1))
def pattern = linkPrefix ~ (anchor ~ linkSuffix ~ labelParser.?.!)
pattern.map { case (x, y) => new Link(x, y) }
}
private def nextPartParser[_: P]: P[Unit] = P {
def mdCodeBlockPrefix = mdCodeBlockIndent ~ mdCodeBlockFence
def anotherBeg = P(CharIn("@=") | (codePrefix ~ nl) | listPrefix | tableSep | "+-" | nl)
nl ~/ (nl | mdCodeBlockPrefix | hspaces0 ~/ anotherBeg)
}
private def textParser[_: P]: P[Text] = P {
def end = P(End | nextPartParser)
def part: P[TextPart] = P(!paraEnd ~ (codeExprParser | linkParser | wordParser))
def sep = P(!end ~ nlHspaces0)
def text = hspaces0 ~ part.rep(1, sep = sep)
text.map(x => Text(x.toSeq))
}
private def leadTextParser[_: P]: P[Text] = P((space | Start) ~ hspaces0 ~ textParser)
private def tagLabelParser[_: P]: P[Word] = P(!nextPartParser ~ nlHspaces1 ~ wordParser)
private def tagDescParser[_: P]: P[Option[Text]] = P {
hspaces0 ~ (textParser | !nextPartParser ~ nl ~ textParser).?
}
private def tagParser[_: P]: P[Tag] = P {
leadHspaces0 ~ ("@" ~ labelParser).!.flatMap { tag =>
val tagType = TagType.getTag(tag)
(tagType.hasLabel, tagType.optDesc) match {
case (false, false) => Pass(Tag(tagType))
case (false, true) => tagDescParser.map(x => Tag(tagType, desc = x))
case (true, false) => tagLabelParser.map(x => Tag(tagType, label = Some(x)))
case (true, true) =>
(tagLabelParser ~ tagDescParser).map { case (label, desc) =>
Tag(tagType, Some(label), desc)
}
}
}
}
private def listBlockParser[_: P](minIndent: Int = 1): P[ListBlock] = P {
def listParser = (hspacesMinWithLen(minIndent) ~ listPrefix.! ~ hspaces1).flatMap {
case (indent, prefix) =>
def sep = (nl ~ hspacesMinWithLen(indent) ~ prefix ~ hspaces1).flatMap { x =>
if (x != indent) Fail else Pass
}
(textParser ~ listBlockParser(indent + 1).?)
.map { case (desc, list) => ListItem(desc, list) }
.rep(1, sep = sep)
.map(x => ListBlock(prefix, x.toSeq))
}
startOrNl ~ listParser
}
private def tableParser[_: P]: P[Table] = P {
def toRow(x: Iterable[String]): Table.Row = Table.Row(x.toSeq)
def toAlign(x: String): Option[Table.Align] = {
def isEnd(y: Char) = y match {
case ':' => Some(true)
case '-' => Some(false)
case _ => None
}
for {
head <- x.headOption
isLeft <- isEnd(head)
isRight <- isEnd(x.last)
// covers "not found" (-1) and found at the end (x.length - 1)
if 0 == (1 + x.indexWhere(_ != '-', 1)) % x.length
} yield {
if (!isRight) Table.Left
else if (!isLeft) Table.Right
else Table.Center
}
}
def cellChar = escape ~ AnyChar | !(nl | escape | tableSep) ~ AnyChar
def row = (cellChar.rep.! ~ tableSpaceSep).rep(1)
// non-standard but frequently used delimiter line, e.g.: +-----+-------+
def delimLine = hspaces0 ~ plusMinus
def sep = nl ~ (delimLine ~ nl).rep ~ tableSpaceSep
// according to spec, the table must contain at least two rows
def table = row.rep(2, sep = sep).map { x =>
// we'll trim the header later; we might need it if align is missing
val rest = x.tail.map(_.map(_.trim))
val alignRow = rest.head
val align = alignRow.flatMap(toAlign).toSeq
if (align.length != alignRow.length) {
// determine alignment from the header row
val head = x.head
val headBuilder = Seq.newBuilder[String]
val alignBuilder = Seq.newBuilder[Table.Align]
headBuilder.sizeHint(head.length)
alignBuilder.sizeHint(head.length)
head.foreach { cell =>
val padLeft = cell.indexWhere(_ > ' ')
if (padLeft < 0) {
headBuilder += ""
alignBuilder += Table.Left
} else {
val idxRight = cell.lastIndexWhere(_ > ' ') + 1
headBuilder += cell.substring(padLeft, idxRight)
alignBuilder += {
val padRight = cell.length - idxRight
if (padLeft < math.max(padRight - 1, 2)) Table.Left
else if (padRight < math.max(padLeft - 1, 2)) Table.Right
else Table.Center
}
}
}
Table(toRow(headBuilder.result()), alignBuilder.result(), rest.toSeq.map(toRow))
} else
Table(toRow(x.head.map(_.trim)), align, rest.tail.toSeq.map(toRow))
}
startOrNl ~ (delimLine ~ nl).? ~ tableSpaceSep ~ table ~ (nl ~ delimLine).?
}
private def termParser[_: P] =
listBlockParser() |
mdCodeBlockParser |
codeBlockParser |
headingParser |
tagParser |
tableParser |
leadTextParser // keep at the end, this is the fallback
private def notTermEnd[_: P] = P(!(End | paraEnd))
private def termsParser[_: P] = P(notTermEnd ~ termParser)
private def paraParser[_: P] = P((termsParser.rep(1)).map(x => Scaladoc.Paragraph(x.toSeq)))
private def paraSep[_: P] = P((nl ~ &(nl)).rep(1))
private def docParser[_: P] = P(paraParser.rep(sep = paraSep).map(x => Scaladoc(x.toSeq)))
/** Contains all scaladoc parsers */
private def parser[_: P]: P[Scaladoc] = P {
paraSep.? ~ docParser ~ spacesMin(0) ~ End
}
private val scaladocDelim = Pattern.compile("[ \t]*(?:$|\n[ \t]*\\**)")
/** Parses a scaladoc comment */
def parse(comment: String): Option[Scaladoc] = {
val isScaladoc = comment.length >= 5 && comment.startsWith("/**") && comment.endsWith("*/")
if (!isScaladoc) None
else {
val content = CharBuffer.wrap(comment, 3, comment.length - 2)
val text = scaladocDelim.matcher(content).replaceAll("\n")
fastparse.parse(text, parser(_)) match {
case p: Parsed.Success[Scaladoc] => Some(p.value)
case _ => None
}
}
}
}