[antlr-interest] Antlr 3.0b6 - problem with '!' operator ? Or did I miss something...

Jim Idle jimi at intersystems.com
Fri Feb 9 12:13:15 PST 2007


duration returns [String value] :
    ts=timestamp
    {$value = $ts.value;}
    ;
timestamp returns [String value] :
    t=INT 'secs'
	{ $value=$t.text}
    ;

Or:

duration 
returns [String value]
scope { String time }
: timestamp
	{$value = $duration::time;}
;
timestamp 
: t=INT 'secs' { $duration::time = $t.text; }
;

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of David CROSSON
Sent: Thursday, February 08, 2007 7:10 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Antlr 3.0b6 - problem with '!' operator ? Or did I miss something...

Hi,

I can't achieve to make the operator '!' working in order to change text returned by a rule.
In the following test grammar and test unit, I'm using the string "32 secs" as input and I expect
to get the string "32" but this not the case I got "32 secs".

-----------------------------------
grammar Debug;
options {
    filter=false;
    output=AST;
}
@header {
  package logparser.parsers;
}
@lexer::header {
  package logparser.parsers;
}
duration returns [String value] :
    ts=timestamp
    {$duration.value = $ts.text;}
    ;
timestamp :
    INT 'secs'!
    ;
INT :
    ('0'..'9')+
    ;
WS :
    ' '+ {$channel=HIDDEN;}
    ;

-----------------------------------
And the test unit :

package logparser.test;

import junit.framework.*;
import logparser.parsers.DebugLexer;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import logparser.parsers.DebugParser;

public class DebugTest extends TestCase {
    public DebugTest(String testName) {super(testName);}
    protected void setUp() throws Exception {}
    protected void tearDown() throws Exception {}
    
    public void testDebug() throws Exception {
        CharStream in = new ANTLRStringStream("32 secs");
        DebugLexer lexer = new DebugLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        DebugParser parser = new DebugParser(tokens);
        String value = parser.duration().value;
        assertTrue("ERR returned value : '" + value+"'","32".equals(value));
    }
}
-----------------------------------
Thanks for all,
David Crosson.





More information about the antlr-interest mailing list