How to count line number?

42 views
Skip to first unread message

Li Dong

unread,
May 3, 2013, 10:12:35 AM5/3/13
to treet...@googlegroups.com
Dear all,

I would like to track the line number of parsed string. For example as "123\n456\n", there are two lines. So when parsing "123\n", I should be able inquire that this is the first line, and second line for "456\n". I wrote a new_line rule as:

rule blank
    ' '*
end

rule new_line
    [\n\r]+ blank
end

so I think I should count the line when matching the new_line. Any suggestion?

Clifford Heath

unread,
May 5, 2013, 7:44:17 AM5/5/13
to treet...@googlegroups.com
On 04/05/2013, at 12:12 AM, Li Dong <dongl...@gmail.com> wrote:
> I would like to track the line number of parsed string.

You don't need to keep track of it. Every treetop syntax node has its "interval"
which is an offset range in the input stream. Treetop also extends String with
the "line_of" method to which you can pass an offset and get back a line number.
It might not be super-efficient (Treetop only uses it for error reporting) but it works.

Clifford Heath.

> For example as "123\n456\n", there are two lines. So when parsing "123\n", I should be able inquire that this is the first line, and second line for "456\n". I wrote a new_line rule as:
>
> rule blank
> ' '*
> end
>
> rule new_line
> [\n\r]+ blank
> end
>
> so I think I should count the line when matching the new_line. Any suggestion?
>
> --
> You received this message because you are subscribed to the Google Groups "Treetop Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to treetop-dev...@googlegroups.com.
> To post to this group, send email to treet...@googlegroups.com.
> Visit this group at http://groups.google.com/group/treetop-dev?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Reply all
Reply to author
Forward
0 new messages