-
Notifications
You must be signed in to change notification settings - Fork 44
/
CodeSpanParser.scala
104 lines (89 loc) · 3.71 KB
/
CodeSpanParser.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
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package laika.parse.code
import laika.ast.{ CategorizedCode, CodeSpan, CodeSpanSequence }
import laika.parse.Parser
import laika.parse.text.PrefixedParser
import laika.parse.builders._
import laika.parse.syntax._
import laika.parse.code.syntax._
/** A collection of code span parsers that are intended to be applied together.
*/
trait CodeSpanParser { self =>
/** The collection of parsers provided by this instance. The order of parsers
* is relevant, since they will be applied in the specified order. If any
* parser successfully produces a result, the subsequent parsers will not
* be invoked.
*/
def parsers: Seq[PrefixedParser[CategorizedCode]]
/** Merges the parsers of this collection with those of the specified other
* collection. The order of parsers is relevant, since they will be applied
* in the specified order. If any parser successfully produces a result,
* the subsequent parsers will not be invoked
*/
def ++ (other: CodeSpanParser): CodeSpanParser = new CodeSpanParser {
override def parsers = self.parsers ++ other.parsers
}
}
/** Companion with a range of convenient constructors for
* creating CodeSpanParsers instances, based on existing parser
* instances or simple specifications of start and end delimiters.
*/
object CodeSpanParser {
/** Parses a single span delimited by the specified start and end strings
* and associates it with the given code category.
*/
def apply(category: CodeCategory, start: String, end: String): CodeSpanParser = {
require(start.nonEmpty)
CodeSpanParser(category) {
(start ~ delimitedBy(end)).source
}
}
/** Parses a single text span and associates it with the given code category.
*/
def apply(category: CodeCategory)(parser: PrefixedParser[String]): CodeSpanParser =
new CodeSpanParser {
val parsers = Seq(parser.map(res => CodeSpan(res, category)))
}
/** Parses a sequence of code spans.
*/
def apply(parser: PrefixedParser[Seq[CodeSpan]]): CodeSpanParser =
new CodeSpanParser {
val parsers = Seq(parser.map(CodeSpanSequence(_)))
}
private val newLine: Parser[CodeSpan] = (atStart.as("") | "\n").asCode()
/** Parses code spans that can only start on a new line or at the start of the input.
*/
def onLineStart(parser: Parser[Seq[CodeSpan]]): CodeSpanParser = {
val combinedParser = (newLine ~ parser).concat
apply(PrefixedParser('\n')(combinedParser))
}
/** Parses a single code span that can only start on a new line or at the start of the input
* and associates it with the given code category.
*/
def onLineStart(category: CodeCategory)(parser: Parser[String]): CodeSpanParser =
onLineStart(parser.asCode(category).map(Seq(_)))
/** Lazily initializes the specified parser for recursive application.
*
* Standard constructors avoid the cost of by-name arguments, but this
* factory method can be used for the cases where recursive embedding of
* a syntax needs to occur.
*/
def recursive(parser: => CodeSpanParser): CodeSpanParser =
new CodeSpanParser {
lazy val parsers = parser.parsers
}
}