Subject: Re: [Stratego] Count newlines
From: [email-masked] (Jurgen J. Vinju)
Date: 18 Apr 04 10:24:22 GMT

> I want to be able to relate parts of my
> parsed structure (tree) to the original
> file.
>
> I would like to count newlines (currently
> just skipped as LAYOUT), and then store
> the value of this counter when important
> bits of the grammar are parsed.

Hi guys,

I don't know if this is relevant, but at CWI we do it like this:

1. parse a file using sglr
2. annotate the parse tree with position information using:
   $ addPosInfo -p <filename> -i <parsetree> -o <parsetree>

You then have an AsFix parse tree that has ATerm annotations on every node
explaining exactly where the node starts and ends in terms of lines and
columns (Un*x style).

3. If needed, then implode the tree using implodePT which will preserve
   the posinfo annotations. I don't know if implode-asfix has the same
   behavior. implodePT might have a switch to turn preservation of
   annotations on or off.

The result is an ATerm that contains pointers to the original source text.
The annotation is labelled by "pos-info", which, if I remember correctly,
you can address using Stratego.

> On Wed, Apr 14, 2004 at 10:11:06AM +0100, Bill James Ellis wrote:
> I've found it more useful to count characters.
> With a character count I use emacs to move forward
> that number of characters, and I'm at the exact point
> in the file where the parse gets stuck.  I use character
> counts in my hand-written parsers.  I don't know how to
> do the like in Stratego.  Line numbers are, I believe,
> a relic of the days when people debugged programms using
> huge stacks of printed listings because CRTs weren't
> available yet.

You are right, still addPosinfo supports line/column addressing. The next
release of SDF2 will also provide character offsets in the annotations next
to the line/column numbers. This seems redundant, but without recomputing
all indexes in a parse tree you can't easily map one to the other; in an
imploded tree it is impossible without re-parsing the source text.

Some editors still not offer offset-based addressing of parts of a text ;-(
Also, Windows vs Unix differences (\r characters) tend to spoil character
counts. So that's why line/column must be supported by addPosinfo in the
future.

Have fun!

Jurgen
-- 
http://www.cwi.nl/~jurgenv

_______________________________________________
stratego mailing list
[email-masked]
https://mail.cs.uu.nl/mailman/listinfo/stratego