using scala parser combinators have parsed text input , created of own types along way. result prints fine. need go through output, presume nested structure includes types created. how go this?
how call parser:
gmparser1.parseitem(i_inputhard_2) match { case gmparser1.success(res, _) => println(">" + res + "< of type: " + res.getclass.getsimplename) case x => println("could not parse input string:" + x) } edit msgresponse(o2 flow|off) is:
>(msgresponse~(o2 flow~off))< of type: $tilde and wthrresponse(id(tube 25,carbon monoxide)|0.20) is:
>(wthrresponse~idwithvalue(id(tube 25,carbon monoxide),0.20))< of type: $tilde just give context question here of input parsing. want get at id:
trait keeper case class id(leftcontents:string,rightcontents:string) extends keeper and here id being created:
def id = "id(" ~> idcontents <~ ")" ^^ { contents => id(contents._1,contents._2) } and here whole of parser:
object gmparser1 extends regexparsers { override def skipwhitespace = false def number = regex(new regex("[-+]?(\\d*[.])?\\d+")) def idcontents = text ~ ("," ~> text) def id = "id(" ~> idcontents <~ ")" ^^ { contents => id(contents._1,contents._2) } def text = """[a-za-z0-9* ]+""".r def wholeword = """[a-za-z]+""".r def idbracketcontents = id ~ ( "|" ~> number ) ^^ { contents => idwithvalue(contents._1,contents._2) } def nonidbracketcontents = text ~ ( "|" ~> text ) def bracketcontents = idbracketcontents | nonidbracketcontents def outerbrackets = "(" ~> bracketcontents <~ ")" def target = wholeword ~ outerbrackets def parseitem(str: string): parseresult[any] = parse(target, str) trait keeper case class id(leftcontents:string,rightcontents:string) extends keeper case class idwithvalue(leftcontents:id,numbercontents:string) extends keeper }
the parser created ~ operator produces value of ~ case class. @ contents, can pattern match on over other case class (keeping in mind name symbolic, it's used infix).
so can replace case gmparser1.success(res, _) => ... case gmparser1.success(functionname ~ argument) => ... @ function name , argument (or whatever semantics of wholeword , bracketcontents in wholeword "(" bracketcontents ")" are). can use nested pattern @ individual parts of argument.
you (and should) use ^^ pattern matching in rules create more meaningful ast structure doesn't contain ~. useful distinguish nonidbracketcontents result bracketcontents result example.
Comments
Post a Comment