Skip to content

Commit 3ba4964

Browse files
committed
restructured naming for comment descriptions, closing #168
1 parent c13b6c6 commit 3ba4964

File tree

5 files changed

+57
-58
lines changed

5 files changed

+57
-58
lines changed

docs/tutorial/interlude-1-haskell.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ object lexer {
105105
escapeSequences = text.EscapeDesc.haskell,
106106
),
107107
SpaceDesc.plain.copy(
108-
commentStart = "{-",
109-
commentEnd = "-}",
110-
commentLine = "--",
111-
nestedComments = true,
108+
lineCommentStart = "--",
109+
multiLineCommentStart = "{-",
110+
multiLineCommentEnd = "-}",
111+
multiLineNestedComments = true,
112112
space = Basic(c => c == ' ' || c == '\t'),
113113
)
114114
)

parsley/shared/src/main/scala/parsley/internal/machine/instructions/TokenInstrs.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ private [internal] final class TokenComment private (
7575
endOfSingleComment: Iterable[ExpectDesc],
7676
) extends CommentLexer {
7777
def this(desc: SpaceDesc, errConfig: ErrorConfig) = {
78-
this(desc.commentStart, desc.commentEnd, desc.commentLine, desc.nestedComments, desc.commentLineAllowsEOF,
79-
errConfig.labelSpaceEndOfMultiComment.asExpectItems(desc.commentEnd),
78+
this(desc.multiLineCommentStart, desc.multiLineCommentEnd, desc.lineCommentStart, desc.multiLineNestedComments, desc.lineCommentAllowsEOF,
79+
errConfig.labelSpaceEndOfMultiComment.asExpectItems(desc.multiLineCommentEnd),
8080
errConfig.labelSpaceEndOfLineComment.asExpectDescs("end of line"))
8181
}
8282
private [this] final val openingSize = Math.max(start.codePointCount(0, start.length), line.codePointCount(0, line.length))
@@ -173,8 +173,8 @@ private [internal] final class TokenWhiteSpace private (
173173
protected [this] val endOfMultiComment: Iterable[ExpectItem],
174174
protected [this] val endOfSingleComment: Iterable[ExpectDesc]) extends WhiteSpaceLike {
175175
def this(ws: Char => Boolean, desc: SpaceDesc, errConfig: ErrorConfig) = {
176-
this(ws, desc.commentStart, desc.commentEnd, desc.commentLine, desc.nestedComments, desc.commentLineAllowsEOF,
177-
errConfig.labelSpaceEndOfMultiComment.asExpectItems(desc.commentEnd),
176+
this(ws, desc.multiLineCommentStart, desc.multiLineCommentEnd, desc.lineCommentStart, desc.multiLineNestedComments, desc.lineCommentAllowsEOF,
177+
errConfig.labelSpaceEndOfMultiComment.asExpectItems(desc.multiLineCommentEnd),
178178
errConfig.labelSpaceEndOfLineComment.asExpectDescs("end of line"))
179179
}
180180
override def spaces(ctx: Context): Unit = {
@@ -196,8 +196,8 @@ private [internal] final class TokenSkipComments private (
196196
protected [this] val endOfMultiComment: Iterable[ExpectItem],
197197
protected [this] val endOfSingleComment: Iterable[ExpectDesc]) extends WhiteSpaceLike {
198198
def this(desc: SpaceDesc, errConfig: ErrorConfig) = {
199-
this(desc.commentStart, desc.commentEnd, desc.commentLine, desc.nestedComments, desc.commentLineAllowsEOF,
200-
errConfig.labelSpaceEndOfMultiComment.asExpectItems(desc.commentEnd),
199+
this(desc.multiLineCommentStart, desc.multiLineCommentEnd, desc.lineCommentStart, desc.multiLineNestedComments, desc.lineCommentAllowsEOF,
200+
errConfig.labelSpaceEndOfMultiComment.asExpectItems(desc.multiLineCommentEnd),
201201
errConfig.labelSpaceEndOfLineComment.asExpectDescs("end of line"))
202202
}
203203
override def spaces(ctx: Context): Unit = ()

parsley/shared/src/main/scala/parsley/token/descriptions/SpaceDesc.scala

+25-26
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,29 @@ import parsley.token.predicate.{CharPredicate, Unicode}
99

1010
/** This class describes how whitespace should be handled lexically.
1111
*
12-
* @param commentStart how do multi-line comments start? (empty for no multi-line comments)
13-
* @param commentEnd how do multi-line comments end? (empty for no multi-line comments)
14-
* @param commentLine how do single-line comments start? (empty for no single-line comments)
15-
* @param commentLineAllowsEOF can a single-line comment be terminated by the end-of-file, or '''must''' it ends with a newline
16-
* @param nestedComments can multi-line comments be nested within each other?
12+
* @param lineCommentStart how do single-line comments start? (empty for no single-line comments)
13+
* @param lineCommentAllowsEOF can a single-line comment be terminated by the end-of-file, or '''must''' it ends with a newline
14+
* @param multiLineCommentStart how do multi-line comments start? (empty for no multi-line comments)
15+
* @param multiLineCommentEnd how do multi-line comments end? (empty for no multi-line comments)
16+
* @param multiLineNestedComments can multi-line comments be nested within each other?
1717
* @param space what characters serve as whitespace within the language?
1818
* @param whitespaceIsContextDependent can the definition of whitespace change depending on context? (in Python, say, newlines are valid whitespace
1919
* within parentheses, but are significant outside of them)
2020
* @since 4.0.0
2121
*/
22-
final case class SpaceDesc (commentStart: String,
23-
commentEnd: String,
24-
commentLine: String,
25-
commentLineAllowsEOF: Boolean,
26-
nestedComments: Boolean,
22+
final case class SpaceDesc (lineCommentStart: String,
23+
lineCommentAllowsEOF: Boolean,
24+
multiLineCommentStart: String,
25+
multiLineCommentEnd: String,
26+
multiLineNestedComments: Boolean,
2727
space: CharPredicate,
2828
whitespaceIsContextDependent: Boolean) {
29-
private [token] lazy val supportsComments = {
30-
require(commentStart.nonEmpty == commentEnd.nonEmpty, "multi-line comments must describe both start and end")
31-
val on = commentStart.nonEmpty || commentLine.nonEmpty
32-
require(commentStart.isEmpty || commentLine.isEmpty || !commentLine.startsWith(commentStart),
33-
"multi-line comments which are a valid prefix of a single-line comment are not allowed as this causes ambiguity in the parser")
34-
on
35-
}
29+
require(multiLineCommentStart.nonEmpty == multiLineCommentEnd.nonEmpty, "multi-line comments must describe both start and end")
30+
require(multiLineCommentStart.isEmpty || lineCommentStart.isEmpty || !lineCommentStart.startsWith(multiLineCommentStart),
31+
"multi-line comments which are a valid prefix of a single-line comment are not allowed as this causes ambiguity in the parser")
32+
require(!multiLineCommentStart.contains('\n') && !multiLineCommentEnd.contains('\n') && !lineCommentStart.contains('\n'),
33+
"comment descriptions cannot include newlines")
34+
private [token] lazy val supportsComments = multiLineCommentStart.nonEmpty || lineCommentStart.nonEmpty
3635
}
3736

3837
/** This object contains any default configurations describing whitespace.
@@ -42,23 +41,23 @@ object SpaceDesc {
4241
/** The plain definition of space, with no comments, no nested comments, and any unicode space character.
4342
*
4443
* {{{
45-
* commentStart = ""
44+
* multiLineCommentStart = ""
4645
* commentEnd = ""
47-
* commentLine = ""
48-
* commentLineAllowsEOF = true
49-
* nestedComments = false
46+
* lineComment = ""
47+
* lineCommentAllowsEOF = true
48+
* multiLineNestedComments = false
5049
* space = Unicode(Character.isWhitespace)
5150
* whitespaceIsContextDependent = false
5251
* }}}
5352
*
5453
* @since 4.0.0
5554
*/
5655
val plain = SpaceDesc(
57-
commentStart = "",
58-
commentEnd = "",
59-
commentLine = "",
60-
commentLineAllowsEOF = true,
61-
nestedComments = false,
56+
multiLineCommentStart = "",
57+
multiLineCommentEnd = "",
58+
lineCommentStart = "",
59+
lineCommentAllowsEOF = true,
60+
multiLineNestedComments = false,
6261
space = Unicode(Character.isWhitespace),
6362
whitespaceIsContextDependent = false
6463
)

parsley/shared/src/test/scala/parsley/token/SpaceTests.scala

+16-16
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class SpaceTests extends ParsleyTest {
3939
"/**/ a" -> None,
4040
)
4141

42-
val basicLine = basicNoComments.copy(commentLine = "--")
43-
val unicodeLine = unicodeNoComments.copy(commentLine = "--")
42+
val basicLine = basicNoComments.copy(lineCommentStart = "--")
43+
val unicodeLine = unicodeNoComments.copy(lineCommentStart = "--")
4444

4545
it should "parse spaces and line comments when defined" in {
4646
cases(makeSpace(basicLine).whiteSpace *> string("a")) (
@@ -59,8 +59,8 @@ class SpaceTests extends ParsleyTest {
5959
)
6060
}
6161

62-
val basicMulti = basicNoComments.copy(commentStart = "/*", commentEnd = "*/")
63-
val unicodeMulti = unicodeNoComments.copy(commentStart = "/*", commentEnd = "*/")
62+
val basicMulti = basicNoComments.copy(multiLineCommentStart = "/*", multiLineCommentEnd = "*/")
63+
val unicodeMulti = unicodeNoComments.copy(multiLineCommentStart = "/*", multiLineCommentEnd = "*/")
6464

6565
it should "parse spaces and multi-line comment when defined" in {
6666
cases(makeSpace(basicMulti).whiteSpace *> string("a")) (
@@ -79,8 +79,8 @@ class SpaceTests extends ParsleyTest {
7979
)
8080
}
8181

82-
val basicMixed = basicNoComments.copy(commentLine = "#", commentStart = "##", commentEnd = "##")
83-
val unicodeMixed = unicodeNoComments.copy(commentLine = "#", commentStart = "##", commentEnd = "##")
82+
val basicMixed = basicNoComments.copy(lineCommentStart = "#", multiLineCommentStart = "##", multiLineCommentEnd = "##")
83+
val unicodeMixed = unicodeNoComments.copy(lineCommentStart = "#", multiLineCommentStart = "##", multiLineCommentEnd = "##")
8484

8585
it should "parse spaces and mixed comments when defined" in {
8686
cases(makeSpace(basicMixed).whiteSpace *> string("a")) (
@@ -111,10 +111,10 @@ class SpaceTests extends ParsleyTest {
111111
unicode.whiteSpace shouldBe unicode.skipComments
112112
}
113113

114-
val basicLineEOF = basicLine.copy(commentLineAllowsEOF = true)
115-
val basicLineNoEOF = basicLine.copy(commentLineAllowsEOF = false)
116-
val basicMixedEOF = basicMixed.copy(commentLineAllowsEOF = true)
117-
val basicMixedNoEOF = basicMixed.copy(commentLineAllowsEOF = false)
114+
val basicLineEOF = basicLine.copy(lineCommentAllowsEOF = true)
115+
val basicLineNoEOF = basicLine.copy(lineCommentAllowsEOF = false)
116+
val basicMixedEOF = basicMixed.copy(lineCommentAllowsEOF = true)
117+
val basicMixedNoEOF = basicMixed.copy(lineCommentAllowsEOF = false)
118118

119119
it should "allow for line comments to end in EOF" in {
120120
cases(makeSpace(basicLineEOF).whiteSpace)("--hello world" -> Some(()))
@@ -126,9 +126,9 @@ class SpaceTests extends ParsleyTest {
126126
cases(makeSpace(basicMixedNoEOF).whiteSpace)("--hello world" -> None)
127127
}
128128

129-
val basicMultiNested = basicMulti.copy(nestedComments = true)
129+
val basicMultiNested = basicMulti.copy(multiLineNestedComments = true)
130130
// having the same start and end makes this... weird
131-
val basicMixedNested = basicMixed.copy(nestedComments = true, commentStart = "#-", commentEnd = "-#")
131+
val basicMixedNested = basicMixed.copy(multiLineNestedComments = true, multiLineCommentStart = "#-", multiLineCommentEnd = "-#")
132132

133133
it should "parse nested comments when applicable" in {
134134
cases(makeSpace(basicMultiNested).whiteSpace) (
@@ -143,8 +143,8 @@ class SpaceTests extends ParsleyTest {
143143
)
144144
}
145145

146-
val basicMultiNonNested = basicMulti.copy(nestedComments = false)
147-
val basicMixedNonNested = basicMulti.copy(nestedComments = false)
146+
val basicMultiNonNested = basicMulti.copy(multiLineNestedComments = false)
147+
val basicMixedNonNested = basicMulti.copy(multiLineNestedComments = false)
148148

149149
it should "not parse nested comments when applicable" in {
150150
cases(makeSpace(basicMultiNonNested).whiteSpace) (
@@ -220,8 +220,8 @@ class SpaceTests extends ParsleyTest {
220220
}
221221

222222
it should "not aggressively eat everything" in {
223-
val lexer1 = makeSpace(basicCommentsOnly.copy(commentStart = "", commentEnd = ""))
224-
val lexer2 = makeSpace(basicCommentsOnly.copy(commentLine = ""))
223+
val lexer1 = makeSpace(basicCommentsOnly.copy(multiLineCommentStart = "", multiLineCommentEnd = ""))
224+
val lexer2 = makeSpace(basicCommentsOnly.copy(lineCommentStart = ""))
225225
val lexer3 = makeSpace(unicodeCommentsOnly)
226226
(lexer1.skipComments *> char('a')).parse("a") shouldBe a [Success[_]]
227227
(lexer2.skipComments *> char('a')).parse("a") shouldBe a [Success[_]]

parsley/shared/src/test/scala/parsley/token/TokeniserTests.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ class TokeniserTests extends ParsleyTest {
2727
caseSensitive = true),
2828
desc.numeric.NumericDesc.plain,
2929
desc.text.TextDesc.plain,
30-
desc.SpaceDesc(commentStart = "/*",
31-
commentEnd = "*/",
32-
commentLine = "//",
33-
commentLineAllowsEOF = true,
34-
nestedComments = true,
30+
desc.SpaceDesc(multiLineCommentStart = "/*",
31+
multiLineCommentEnd = "*/",
32+
lineCommentStart = "//",
33+
lineCommentAllowsEOF = true,
34+
multiLineNestedComments = true,
3535
space = token.predicate.Basic(_.isWhitespace),
3636
whitespaceIsContextDependent = false))
3737
val scala_ =
3838
scala.copy(
39-
spaceDesc = scala.spaceDesc.copy(nestedComments = false)
39+
spaceDesc = scala.spaceDesc.copy(multiLineNestedComments = false)
4040
)
4141
val tokeniser = new token.Lexer(scala)
4242
val tokeniser_ = new token.Lexer(scala_)

0 commit comments

Comments
 (0)