i can´t see my error here .. this rule parse some stuff ok but the last two samples not. Could somebody please give me a hint ..
Goal is a parser than can identify member property access and member function calls. Also chained in some way
a()
a(para)
x.a()
x.a(para)
x.a(para).g(para).j()
x.y
x.y.z
x.y.z() <---fail
y.z.z(para) <--- fail
lvalue =
iter_pos >> name[_val = _1]
>> *(lit('(') > paralistopt > lit(')') >> iter_pos)[_val = construct<common_node>(type_cmd_fnc_call, LOCATION_NODE_ITER(_val, _2), key_this, construct<common_node>(_val), key_parameter, construct<std::vector<common_node> >(_1))]
>> *(lit('.') >> name_pure >> lit('(') > paralistopt > lit(')') >> iter_pos)[_val = construct<common_node>(type_cmd_fnc_call, LOCATION_NODE_ITER(_val, _3), key_this, construct<common_node>(_val), key_callname, construct<std::wstring>(_1), key_parameter, construct<std::vector<common_node> >(_2))]
>> *(lit('.') >> name_pure >> iter_pos)[_val = construct<common_node>(type_cmd_dot_call, LOCATION_NODE_ITER(_val, _2), key_this, construct<common_node>(_val), key_propname, construct<std::wstring>(_1))]
;
thank you Markus
You provide very little information to go at. Let me humor you with my entry into this guessing game:
Let's assume you want to parse a simple "language" that merely allows member expressions and function invocations, but chained.
Now, your grammar says nothing about the parameters (though it's clear the param list can be empty), so let me go the next mile and assume that you want to accept the same kind of expressions there (so
foo(a)
is okay, but alsobar(foo(a))
orbar(b.foo(a))
).Since you accept chaining of function calls, it appears that functions are first-class objects (and functions can return functions), so
foo(a)(b, c, d)
should be accepted as well.You didn't mention it, but parameters often include literals (
sqrt(9)
comes to mind, orprintln("hello world")
).Other items:
iter_pos
(ab)use it seems you're interested in tracking the original source location inside the resulting AST.1. Define An AST
We should keep it simple as ever:
2. A Matching Grammar
In this skeleton most rules benefit from automatic attribute propagation. The one that doesn't is
expression
:So, let's create some helpers for the semantic actions.
Define two simple construction functions:
Then use them:
That's all. We're ready to roll!
3. DEMO
Live On Coliru
I created a test bed looking like this:
Incredible as it may appear, this already parses all the test cases and prints:
4. Too Good To Be True?
You're right. I cheated. I didn't show you this code required to debug print the parsed AST:
It's only debug printing, as string literals aren't correctly roundtripped. But it's only 10 lines of code, that's a bonus.
5. The Full Monty: Source Locations
This had your interest, so let's show it working. Let's add a simple loop to print all locations of identifiers:
Of course, this begs the question, what are
showpos
andall_identifiers
?As for the identifier extraction:
That's a tree visitor that harvests all identifiers recursively, inserting them into the back of a container.
Live On Coliru
Where output looks like (excerpt):
Try changing
to