erikrose / parsimonious

The fastest pure-Python PEG parser I can muster
MIT License
1.8k stars 126 forks source link

Arguable confusion/incorrect info the README re: AST producing #152

Open pfalcon opened 5 years ago

pfalcon commented 5 years ago

From README:

Next, let's parse something and get an abstract syntax tree:

print grammar.parse('((bold stuff))') <Node called "bold_text" matching "((bold stuff))"> <Node called "bold_open" matching "(("> <RegexNode called "text" matching "bold stuff"> <Node called "bold_close" matching "))">

May I suggest that was produced here is not an abstract syntax tree, but a concrete syntax tree. An AST would be something like:

<Node called "bold_text" matching "((bold stuff))">
    <RegexNode called "text" matching "bold stuff">

Whereas parsimonious' output contains every syntactic/punctuational detail like those "((", which are cruft from user's perspective.

Indeed, as the following ini-parsing example shows, it takes an additional step to convert parsimonious' CST to user-useful AST, using node visitors and statements like:

        key, _, value, *_ = node.children
        return key.text, value.text