I have been beating my head against a wall, trying to figure out how to get this to work. The initial parser worked great, but when I try to get a Map out of it, it is only giving the initial number value, but not he value blocks.
The format is "float: [label float: [optinallabel float:] ...]" like:
412285.556: [Label 0.0:[Label1 1.0:][Label2 2.0:]
The parser to list is:
object ParseList extends JavaTokenParsers {
def sep : Parser[String] = ":"
def string : Parser[Any] = """[\.a-zA-Z0-9]+""".r
def num: Parser[Any] = floatingPointNumber <~ sep
def valueBlock: Parser[Any] = "["~>rep(valueBlock)<~"]" | string ~ floatingPointNumber <~ sep
def expr: Parser[Any] = num ~ rep(valueBlock )
def apply(in: String) = parseAll(expr,in)
}
Testing gives:
scala> ParseList("""412285.556: """)
res150: ParseList.ParseResult[Any] = [1.13] parsed: (412285.556~List())
scala> ParseList("""412285.556: [Label 1.0:]""")
res151: ParseList.ParseResult[Any] = [1.25] parsed: (412285.556~List(List((Label~1.0))))
scala> ParseList("""412285.556: [Label 0.0:[Label1 1.0:][Label2 2.0:]]""")
res152: ParseList.ParseResult[Any] = [1.51] parsed: (412285.556~List(List((Label~0.0), List((Label1~1.0)), List((Label2~2.0)))))
When I try to make it a Map, if only returns the number, but calls the member routine. See the debug output.
The Map parser:
object ParseMap extends JavaTokenParsers {
// Seperator
def sep : Parser[String] = ":"
// string
def string : Parser[String] = """[a-zA-Z][a-zA-Z0-9]+""".r
// Block within [] with label value: and option additional blocks
def valueBlock: Parser[(String,Any)] =
member <~ rep(obj)
// Member - value pair within a block
def member: Parser[(String, Any)] =
string ~ floatingPointNumber <~ sep ^^
{ case s ~ n => (s, n); println("In Member s=" +s+" n="+n); (s, n)}
def obj: Parser[Map[String,Any]] =
"["~> rep(valueBlock) <~"]" ^^ {Map() ++ _}
// Initial number value of the data
def num: Parser[(String, Any)] =
floatingPointNumber <~ sep ~ rep(obj) ^^
{ case floatingPointNumber => ("Num", floatingPointNumber) }
// order of operations
def value: Parser[Any] = (
num
| obj
| member
| floatingPointNumber
| string
)
def apply(in: String) = parseAll(value,in)
}
The testing gives:
scala> ParseMap("""412285.556: """)
res154: ParseMap.ParseResult[Any] = [1.13] parsed: (Num,412285.556)
scala> ParseMap("""412285.556: [Label 1.0:]""")
In Member s=Label n=1.0
res155: ParseMap.ParseResult[Any] = [1.25] parsed: (Num,412285.556)
scala> ParseMap("""412285.556: [Label 0.0:[Label1 1.0:][Label2 2.0:]]""")
In Member s=Label n=0.0
In Member s=Label1 n=1.0
In Member s=Label2 n=2.0
res156: ParseMap.ParseResult[Any] = [1.51] parsed: (Num,412285.556)
All my attempts to get a single Map out of it, has failed. Any help would be greatly appreciated.