Skip to content

Commit f63978c

Browse files
author
David Winslow
committed
Rewrite comment parser without regex
This is clearer and resolves a bug that I encountered while testing raster styling. (I didn't identify the bug beyond determining that it was a problem in the comment parser.)
1 parent 0512cae commit f63978c

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

geocss/src/main/scala/org/geoscript/geocss/CssParser.scala

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,17 @@ object CssParser extends RegexParsers {
4343
}
4444
}
4545

46-
private object SingleComment extends Parser[String] {
47-
val whiteSpace = """\s*""".r
48-
val comment = """/\*((?:[^/]|[^*]/)*)\*/""".r
49-
50-
override def apply(in: Reader[Char]): ParseResult[String] = {
51-
val source = in.source
52-
val start = findStart(source, in.offset)
53-
val space = source.subSequence(start, source.length)
54-
comment.findPrefixMatchOf(space) match {
55-
case Some(cmt) => Success(cmt.group(1), in.drop(start - in.offset + cmt.end))
56-
case None => Failure("nothing found", in)
57-
}
58-
}
59-
60-
private def findStart(source: CharSequence, offset: Int): Int = {
61-
val space = source.subSequence(offset, source.length)
62-
whiteSpace.findPrefixMatchOf(space) match {
63-
case Some(m) => offset + m.end
64-
case None => offset
65-
}
66-
}
46+
private val SingleComment: Parser[String] = {
47+
val whitespacepadding = rep(elem("Whitespace", _.isWhitespace))
48+
val startComment = elem('/') ~ elem('*')
49+
val endComment = elem('*') ~ elem('/')
50+
val commentChar = (elem('*') <~ not(elem('/'))) | elem("Comment content", _ != '*')
51+
for {
52+
_ <- whitespacepadding
53+
_ <- startComment
54+
body <- rep(commentChar)
55+
_ <- endComment
56+
} yield body.mkString
6757
}
6858

6959
private val ParsedComment = rep1(SingleComment) map { x => Description(x.last) }

0 commit comments

Comments
 (0)