Does anyone have a solution to remove comments from text?
I have not has success with regular expressions nor with the parser combinator.
TIA
Does anyone have a solution to remove comments from text?
I have not has success with regular expressions nor with the parser combinator.
TIA
You could try Scalaparse as a starting point: https://www.lihaoyi.com/fastparse/#ScalaParse
Here’s the relevant part of the grammar handling comments: https://github.com/lihaoyi/fastparse/blob/master/scalaparse/src/scalaparse/syntax/Literals.scala#L49-L56
Besides, what is this for? A one time job? You could also use cpp / gcc to remove Java style comments from your source files. (e.g. cpp -P -fpreprocessed your-file.scala > output.scala
)
BTW, tackling this with a regex is futile, it is not context sensitive (think of comment characters inside of string literals).
And maybe you should say what you mean with “text”?!
@cbley I tried Fastparse but because I am using Scala 3 (no Scala 2 macros), it was not possible.
I am aware of the literals (found a reference to thees in a Gitter). I tried something similar here:
see [SOLVED] Parser combinator: removing comments
Not a one-time job. Preprocessing text before parsing in Scala.
I need to strip illegal comments from a JSON file.
I think my problem is knowing how to combine the literals. My example above fails with:
val text1 =
"""
|text1//comment1
|text2
|//comment2""".stripMargin
Which JSON parser are you using? Is it based on jackson? This would be as easy as parser.enable(ALLOW_COMMENTS)
…
ujson. I don’t think it has such a flag. 8-(
Thanks taking a look now.