[antlr-interest] Another Comments question

Tom Davies tomdavies at optushome.com.au
Sun Jul 27 17:53:27 PDT 2003


I'm using the hidden tokens approach, keeping my own List of tokens, and
recording in my parser which ranges of tokens should have their comments
before a particular tree node.

So my parser rule looks like:
program
  : pSection c1:"CL"! c2:COLON!
    (specSection)? (argSection)? (commonSection)? (localSection)?
    (clDeclSection)* (procDeclSection)*
    (statement)* c3:"END"! c4:"CL"! c5:SEP!
  { ## = #([CL,"CL"], ##);
    Context.get().setBeforeComments(##,c1,c2);
    Context.get().setAfterComments(##,c3,c5);
  }
  ;

And the corresponding tree parser rule looks like:
program returns [
  UnitSpec u = null;
  String args="", common="", local="", stats="", psectionComments = ""
  ] 
  : #(c:CL (psectionComments = psection) (args=argSection)?
(common=commonSection)? (local=localSection)? stats=statementList)
  { u = new UnitSpec(
              args,
	      common,
	      local,
	      stats,
	      Context.get().getSubroutines(),
	      psectionComments + Context.get().commentsBefore(c),
	      Context.get().commentsAfter(c)
	);
  }
  ;

(Context is just the place I keep my symbol table and all the functions
which create my target code)

I also have a rule:
pStatement: ("CL"^ | "DEF"^ | "DD"^ | "ZCL"^ | "SCL"^ | "XCL"^)
	EQUALS! IDENTIFIER (FILESUFFIX)? (COMMA! "NEW")? c:SEP!
	;

I'd like to say:
pStatement: a:("CL"^ | "DEF"^ | "DD"^ | "ZCL"^ | "SCL"^ | "XCL"^)
	EQUALS! IDENTIFIER (FILESUFFIX)? (COMMA! "NEW")? b:SEP!
{ Context.get().setBeforeComments(##,a,b); }
	;

but the construct a:(...) is not allowed. Any good alternative? Can I
say ##.getToken() or an equivalent, to get 'a'?

Thanks,
  Tom

-- 
Tom Davies <tomdavies at optushome.com.au>


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list