bnfc icon indicating copy to clipboard operation
bnfc copied to clipboard

Access to AST nodes' positions in the input file

Open mantkiew opened this issue 12 years ago • 5 comments

In order to be able to provide better error messages to the users, we would like to say

Error E occured on line 25, col 55

However, currently the positions of AST nodes in the input file are not accessible from AST. How could we access the nodes' positons? How do AST node positions relate to token positions e.g.,

position token PIdent (letter (letter|digit|’_’|’\’’)*) ;

mantkiew avatar May 10 '13 18:05 mantkiew

I don't think that you can access the position of any node excepts token defined with the position keyword.

It could maybe be added as an option...

gdetrez avatar May 15 '13 18:05 gdetrez

That would be valuable.

rpglover64 avatar Jun 12 '13 17:06 rpglover64

What we have to do currently, to address the issue is to introduce a bunch of internal constructions but these pollute our code badly....

in the original .cf file you'd have

Module.         Module ::= [Declaration] ;

but we add

Module . Module ::= [Declaration];
internal PosModule . Module ::= Span [Declaration];

where the Span is a section of the input file, i.e., ( (x1, y1), (x2, y2) ):

Pos . Pos ::= Integer Integer ;
internal PosPos . Pos ::= Span Integer Integer ;
Span . Span ::= Pos Pos ;
internal PosSpan . Span ::= Span Pos Pos ;

The nasty consequence is that in all our functions which work with AST nodes, we always have to pattern match on the original constructors (Module) and the new ones with positions (PosModule).

It is a nasty hack that adds up a lot programming and maintenance cost.

mantkiew avatar Jun 12 '13 20:06 mantkiew

What we could have is a function which computes a span from the position-tokens within it.

On Wed, Jun 12, 2013 at 10:59 PM, Michal Antkiewicz < [email protected]> wrote:

What we have to do currently, to address the issue is to introduce a bunch of internal constructions but these pollute our code badly....

in the original .cf file you'd have

Module. Module ::= [Declaration] ;

but we add

Module . Module ::= [Declaration]; internal PosModule . Module ::= Span [Declaration];

where the Span is a section of the input file, i.e., ( (x1, y1), (x2, y2) ):

Pos . Pos ::= Integer Integer ; internal PosPos . Pos ::= Span Integer Integer ; Span . Span ::= Pos Pos ; internal PosSpan . Span ::= Span Pos Pos ;

The nasty consequence is that in all our functions which work with AST nodes, we always have to pattern match on the original constructors ( Module) and the new ones with positions (PosModule).

It is a nasty hack that adds up a lot programming and maintenance cost.

— Reply to this email directly or view it on GitHubhttps://github.com/BNFC/bnfc/issues/28#issuecomment-19355141 .

jyp avatar Jun 13 '13 06:06 jyp

We have forked bnfc and added some support (https://github.com/juodaspaulius/bnfc/). If there's interest we could guard these extension using a new command line parameter (e.g., --positions) and contribute back.

mantkiew avatar Jul 09 '14 16:07 mantkiew