I don't know if this is opaque or not, but it does what you want (I'll explain
more below):
from lepl import *
def with_line(node):
def wrapper(results, stream_out, **kargs):
return node(results, ('lineno', s_delta(stream_out)[1]))
return wrapper
class Greeting(Node): pass
newline = ~Literal('\n')
space = ~Space()
padding = (space | newline)[:]
line = padding & (Literal('howdy:') ** with_line(Greeting)) & padding
ast = line.parse(' \n \n \n howdy: \n \n\n ')
print(ast[0])
The output is:
Greeting
+- ['howdy:']
`- lineno 4
This is for the latest version of Lepl btw (5).
The reason that works is:
1 - I'm creating the node exactly where we match the greeting (in your
original code you were creating the node after discaring more padding, so the
line number would have been wrong).
2 - I'm using "**" rather than ">" which calls a function with various named
arguments (see
http://www.acooke.org/lepl/operators.html#operators-that-apply-functions-to-results)
- in this case the important arguments are "results" (the string matched) and
"stream_out) which is the input stream after matching.
3 - With that information, we can get the location from the stream after
matching. That's done with s_delta which returns (offset, lineno and char)
see
http://www.acooke.org/lepl/api/lepl.stream.core-pysrc.html#StreamHelper.delta
4 - Finally, we stick that in the node, along with the result.
Cheers,
Andrew
> --
> You received this message because you are subscribed to the Google Groups "lepl" group.
> To post to this group, send email to le...@googlegroups.com.
> To unsubscribe from this group, send email to lepl+uns...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/lepl?hl=en.
>
i can't remember the exact names off the top of my head, but hopefully that's
obvious...?
cheers,
andrew