Advanced control of recursive parser in scala
- by Jeriho
val uninterestingthings = ".".r
val parser = "(?ui)(regexvalue)".r | (uninterestingthings~>parser)
This recursive parser will try to parse "(?ui)(regexvalue)".r until the end of input. Is in scala a way to prohibit parsing when some defined number of characters were consumed by "uninterestingthings" ?
UPD: I have one poor solution:
object NonRecursiveParser extends RegexParsers with PackratParsers{
var max = -1
val maxInput2Consume = 25
def uninteresting:Regex ={
if(max<maxInput2Consume){
max+=1
("."+"{0,"+max.toString+"}").r
}else{
throw new Exception("I am tired")
}
}
lazy val value = "itt".r
def parser:Parser[Any] = (uninteresting~>value)|parser
def parseQuery(input:String) = {
try{
parse(parser, input)
}catch{
case e:Exception =>
}
}
}
Disadvantages:
- not all members are lazy vals so PackratParser will have some time penalty
- constructing regexps on every "uninteresting" method call - time penalty
- using exception to control program - code style and time penalty