palle-k / Covfefe

A parser for nondeterministic context free languages
https://palle-k.github.io/Covfefe/
MIT License
62 stars 8 forks source link

dot output label is not human friendly #1

Closed wildthink closed 6 years ago

wildthink commented 6 years ago

I see that you are moving forward with swift 4.1 but is there any chance you could point me in the right direction to clean up the dot output as illustrated with your parser.syntaxTree(for: "(a + b)*(-c)") example?

    node29 [label="Index(_compoundOffset: 40, _cache: Swift.String.Index._Cache.utf16)..<Index(_compoundOffset: 44, _cache: Swift.String.Index._Cache.utf16)" shape=box]

Thanks.

palle-k commented 6 years ago

The syntax tree leafs store string ranges but you can map it to store strings instead using the mapLeafs function to generate a more human readable syntax tree:

let string = "(a+b)*(-c)" 
let syntaxTree: SyntaxTree<NonTerminal, Range<String.Index>> = parser.parse(string)
let humanReadable: SyntaxTree<NonTerminal, String> = syntaxTree.mapLeafs { leaf in
    String(string[leaf])
}