[antlr-interest] "replace: range invalid" error when rewriting an imaginary token/empty block

Michael Glaesemann grzm at seespotcode.net
Sun Aug 5 16:12:23 PDT 2012


Hi!

I'm writing a code annotator and am having issues annotating empty blocks.
I'm just starting with ANTLR, and have searched both TDAR and Google, but
haven't found a solution to my problem. I'm using AntlrWorks 1.4.3.

What I want to do:

# source
begin statement end

# annotated source indicating position of the block
begin /* line 1 pos 0 */ statement end

# source
begin statement statement end

# annotated source
begin /* line 1 pos 0 */ statement statement end

I have the above working. However, it fails with empty blocks:

# source
begin end

#error
java.lang.IllegalArgumentException: replace: range invalid: 2..0(size=4)

Here are my combined and tree grammars:

grammar EmptyBlock;
options {
  output=AST;
  ASTLabelType=CommonTree;
}

tokens {
	BLOCK;
  SLIST;
}

@header { package mtblock; }
@lexer::header { package mtblock; }

block
	: BEGIN stats END -> ^(BLOCK BEGIN stats END)
	;
	
stats
	: stat* -> ^(SLIST stat*)
	;

stat
 	: STAT
 	;

STAT
	: 'statement'
	;

BEGIN 
	: 'begin'
	;

END
	: 'end'
	;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;


tree grammar Annotate;
options {
  tokenVocab=EmptyBlock;
  ASTLabelType=CommonTree;
  output=template;
  rewrite=true;
}
@header { package mtblock; }

block
scope {
  int line;
  int pos;
}
	: ^(BLOCK {$block::line=$BLOCK.line; $block::pos=$BLOCK.pos;}
	    BEGIN  stats END)
	;

stats
	: ^(SLIST stat*)
  	-> template(l={$block::line},p={$block::pos},s={$text})
  		"/* line <l> pos <p> */ <s>"
	;

stat
	: STAT
	;


I found one post to the mailing list that I thought was similar:
<http://www.antlr.org/pipermail/antlr-interest/2009-September/035969.html>

When I tried what was suggested (see below), I got the same error as before.

I think it has to do with the fact that the SLIST token doesn't have any stream
information associated with it. It has no indexes in the stream, so I'm getting
the range error. However, if that's the case I'm at a loss as to how to add
appropriate index information to the SLIST token.

Thanks for any assistance.

Michael Glaesemann
grzm seespotcode net





grammar EmptyBlock;
options {
  output=AST;
  ASTLabelType=CommonTree;
}

tokens {
	BLOCK;
  SLIST;
}

@header { package mtblock; }
@lexer::header { package mtblock; }

block
	: BEGIN block_body END -> ^(BLOCK BEGIN block_body END)
	;
	
block_body
  :	 stats? -> ^(SLIST stats?)
  ;

stats
	: stat+
	;

stat
 	: STAT
 	;

STAT
	: 'statement'
	;

BEGIN 
	: 'begin'
	;

END
	: 'end'
	;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;


tree grammar Annotate;
options {
  tokenVocab=EmptyBlock;
  ASTLabelType=CommonTree;
  output=template;
  rewrite=true;
}
@header { package mtblock; }

block
scope {
  int line;
  int pos;
}
	: ^(BLOCK {$block::line=$BLOCK.line; $block::pos=$BLOCK.pos;}
	    BEGIN block_body END)
	;

block_body
	: ^(SLIST stats)
  	-> template(l={$block::line},p={$block::pos},s={$text})
  		"/* line <l> pos <p> */ <s>"
	| SLIST
  	-> template(l={$block::line},p={$block::pos})
  		"/* line <l> pos <p> */"
	;

stats
	: stat+
	;

stat
	: STAT
	;



More information about the antlr-interest mailing list