Line/column numbers for surface syntax?

37 views
Skip to first unread message

Jonathan Schuster

unread,
Aug 24, 2016, 11:48:22 AM8/24/16
to nanopass-framework
Is there a way to get syntactic position information like line and column number from the input program to Nanopass? I'd like to add some line-based annotations to my program for analysis purposes.

Seems like the answer is likely no, since I think it just works on s-expressions, but I thought I'd check.

-Jon

Leif Andersen

unread,
Aug 24, 2016, 2:22:31 PM8/24/16
to Jonathan Schuster, nanopass-framework
You are correct that there is no mechanism built into Nanopass to do this. But this is a great question, and there are several ways you can store source locations.

The first is just to store the source locations inside of the nanopass expression itself.

The other way is to store all of your identifiers as structs rather than symbols. That way you can store source locations attached to identifiers.

(struct my-identifier (name srcloc))

Then you just put my-identifier in the terminals section of your language.

This merits a proper write up and I'll make a quick blog post describing it tonight.


~Leif Andersen

--
You received this message because you are subscribed to the Google Groups "nanopass-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nanopass-framework+unsub...@googlegroups.com.
To post to this group, send email to nanopass-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nanopass-framework/CAKUgKwxn1c5XZJt7-uH7s%2BeNrk8y9gBDjV8%2BziHkwErz1YcauA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Jens Axel Søgaard

unread,
Aug 24, 2016, 2:44:24 PM8/24/16
to Leif Andersen, Jonathan Schuster, nanopass-framework
As Leif writes it is up to your parser to produce something to store in the Nanopass structures.
In Urlang the input to the parser is a syntax object. The parser then uses syntax-parse to
examine the original syntax and produce a program represented as Nanopass structures.

I chose to represent identifiers are represented as Racket identifiers (i.e. syntax-objects
representing a symbol), so the source location of an identifier is always available.

This makes it possible to let DrRacket highlight syntax errors such as "unbound identifier"
correctly (use raise-syntax-error to report errors) . 
Also: since macro expansion takes place in the parser, and the parser has the
original syntax object, macros can also report errors with full source locations.

I opted not to store the source location for forms after parsing. 

https://github.com/soegaard/urlang/blob/master/urlang/main.rkt#L495

/Jens Axel


2016-08-24 20:21 GMT+02:00 Leif Andersen <le...@leifandersen.net>:
You are correct that there is no mechanism built into Nanopass to do this. But this is a great question, and there are several ways you can store source locations.

The first is just to store the source locations inside of the nanopass expression itself.

The other way is to store all of your identifiers as structs rather than symbols. That way you can store source locations attached to identifiers.

(struct my-identifier (name srcloc))

Then you just put my-identifier in the terminals section of your language.

This merits a proper write up and I'll make a quick blog post describing it tonight.


~Leif Andersen

On Wed, Aug 24, 2016 at 11:47 AM, Jonathan Schuster <schu...@ccs.neu.edu> wrote:
Is there a way to get syntactic position information like line and column number from the input program to Nanopass? I'd like to add some line-based annotations to my program for analysis purposes.

Seems like the answer is likely no, since I think it just works on s-expressions, but I thought I'd check.

-Jon

--
You received this message because you are subscribed to the Google Groups "nanopass-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nanopass-framework+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "nanopass-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nanopass-framework+unsub...@googlegroups.com.
To post to this group, send email to nanopass-framework@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
--
Jens Axel Søgaard

Reply all
Reply to author
Forward
0 new messages