Antlr setText不按我预期的方式工作

时间:2011-10-06 00:34:51

标签: antlr antlr3

我需要将标识符转换为beanutil字符串,以便从对象中检索项目。字符串转换的标识符如下所示:

name                                        ==> name
attribute.name                              ==> attributes(name)[0].value
attribute.name[2]                           ==> attributes(name)[2].value
address.attribute.postalcode                ==> contactDetails.addresses[0].attributes(postalcode)[0].value
address[2].attribute.postalcode             ==> contactDetails.addresses[2].attributes(postalcode)[0].value
address[2].attribute.postalcode[3]          ==> contactDetails.addresses[2].attributes(postalcode)[3].value

现在我决定使用antlr这样做,因为我觉得它可能和使用一组'if'语句一样快。随意告诉我,我错了。

现在,我已经使用antlr进行了部分工作,但是一旦我开始执行'address',setText部分似乎就停止了属性。

我是以正确的方式做到这一点还是有更好的方法来使用antlr来获得我想要的结果?

grammar AttributeParser;

parse returns [ String result ]
:  Address EOF { $result = $Address.text; }
|  Attribute EOF { $result = $Attribute.text; }
|  Varname EOF { $result = $Varname.text; }
;

Address
: 'address' (Arraypos)* '.' Attribute { setText("contactDetails.addresses" + ($Arraypos == null ? "[0]" : $Arraypos.text ) + "." + $Attribute.text); }
; 

Attribute
:  'attribute.' Varname (Arraypos)* { setText("attributes(" + $Varname.text + ")" + ($Arraypos == null ? "[0]" : $Arraypos.text ) + ".value"); }
;

Arraypos
: '[' Number+ ']'
;


Varname  
:  ('a'..'z'|'A'..'Z')+
;

Number
: '0'..'9'+
;

Spaces
:  (' ' | '\t' | '\r' | '\n')+ { setText(" "); }
;

下面是两个单元测试,第一个返回我期望的,第二个没有。

@Test
public void testSimpleAttributeWithArrayRef() throws Exception {

    String source = "attribute.name[2]";
    ANTLRStringStream in = new ANTLRStringStream(source);
    AttributeParserLexer lexer = new AttributeParserLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    AttributeParserParser parser = new AttributeParserParser(tokens);
    String result = parser.parse();
    assertEquals("attributes(name)[2].value", result);       
}

@Test
public void testAddress() throws Exception {

    String source = "address.attribute.postalcode";
    ANTLRStringStream in = new ANTLRStringStream(source);
    AttributeParserLexer lexer = new AttributeParserLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    AttributeParserParser parser = new AttributeParserParser(tokens);
    String result = parser.parse();
    System.out.println("Result: " + result);
    assertEquals("contactDetails.addresses[0].attributes(postalcode)[0].value", result);       
}

1 个答案:

答案 0 :(得分:1)

不,您无法执行(Arraypos)*,然后按以下方式引用内容:$Arraypos.text

我不会更改标记的内部文本,而是创建一些解析器规则并让它们返回相应的文本。

一个小小的演示:

grammar AttributeParser;

parse returns [String s]
  :  input EOF {$s = $input.s;}
  ;

input returns [String s]
  :  address   {$s = $address.s;}
  |  attribute {$s = $attribute.s;}
  |  Varname   {$s = $Varname.text;}
  ;

address returns [String s]
  :  Address arrayPos '.' attribute
     {$s = "contactDetails.addresses" + $arrayPos.s + "." + $attribute.s;}
  ;

attribute returns [String s]
  :  Attribute '.' Varname arrayPos 
     {$s = "attributes(" + $Varname.text + ")" + $arrayPos.s + ".value" ;}
  ;

arrayPos returns [String s]
  :  Arraypos      {$s = $Arraypos.text;}
  |  /* nothing */ {$s = "[0]";}
  ;

Attribute : 'attribute';
Address   : 'address';
Arraypos  : '[' '0'..'9'+ ']';
Varname   : ('a'..'z' | 'A'..'Z')+;

可以用以下方法测试:

import org.antlr.runtime.*;

public class Main {
  public static void main(String[] args) throws Exception {
    String[][] tests = {
      {"name",                                "name"},
      {"attribute.name",                      "attributes(name)[0].value"},
      {"attribute.name[2]",                   "attributes(name)[2].value"},
      {"address.attribute.postalcode",        "contactDetails.addresses[0].attributes(postalcode)[0].value"},
      {"address[2].attribute.postalcode",     "contactDetails.addresses[2].attributes(postalcode)[0].value"},
      {"address[2].attribute.postalcode[3]",  "contactDetails.addresses[2].attributes(postalcode)[3].value"}
    };
    for(String[] test : tests) {
      String input = test[0];
      String expected = test[1];
      AttributeParserLexer lexer = new AttributeParserLexer(new ANTLRStringStream(input));
      AttributeParserParser parser = new AttributeParserParser(new CommonTokenStream(lexer));
      String output = parser.parse();
      if(!output.equals(expected)) {
        throw new RuntimeException(output + " != " + expected);
      }
      System.out.printf("in  = %s\nout = %s\n\n", input, output, expected);
    }
  }
}

要运行演示,请执行以下操作:

java -cp antlr-3.3.jar org.antlr.Tool AttributeParser.g
javac -cp antlr-3.3.jar *.java
java -cp .:antlr-3.3.jar Main

将打印以下内容到控制台:

in  = name
out = name

in  = attribute.name
out = attributes(name)[0].value

in  = attribute.name[2]
out = attributes(name)[2].value

in  = address.attribute.postalcode
out = contactDetails.addresses[0].attributes(postalcode)[0].value

in  = address[2].attribute.postalcode
out = contactDetails.addresses[2].attributes(postalcode)[0].value

in  = address[2].attribute.postalcode[3]
out = contactDetails.addresses[2].attributes(postalcode)[3].value

修改

请注意,您还可以让解析器规则返回的不仅仅是这样的一个对象:

bar
  :  foo {System.out.println($foo.text + ", " + $foo.number);}
  ;

foo returns [String text, int number]
  :  'FOO' {$text = "a"; $number = 1;}
  |  'foo' {$text = "b"; $number = 2;}
  ;