2016-04-27 5 views
2

私は、独自の区切り文字(具体的には、オラクルの引用符で区切られた文字列)を定義する文字列リテラル読むことができる字句解析ルールを作成します:文字を参照するレクサールールを書くには?

!は、区切り文字として機能しますが、理論的にはでき
q'!My string which can contain 'single quotes'!' 

をどんなキャラクターでもあります。

これは、特定の言語ターゲットに依存することなく、レクサールールで行うことはできますか?

答えて

3

特定の言語ターゲットに依存することなく、レクサールールでこれを実行できますか?

いいえ、ターゲット依存コードはそのようなことに必要です。

ちょうどあなた、または他の誰かがこのQを読んだ場合、Aは、これはターゲット・コードを使用して行うことができるか疑問に思っている&、ここでの迅速なデモだ:

クラスでテストすることができ
lexer grammar TLexer; 

@members { 
    boolean ahead(String text) { 
    for (int i = 0; i < text.length(); i++) { 
     if (_input.LA(i + 1) != text.charAt(i)) { 
     return false; 
     } 
    } 
    return true; 
    } 
} 

TEXT 
: [nN]? (['] ([']['] | ~['])* ['] 
     | [qQ] ['] QUOTED_TEXT ['] 
     ) 
; 

// Skip everything other than TEXT tokens 
OTHER 
: . -> skip 
; 

fragment QUOTED_TEXT 
: '[' ({!ahead("]'")}?      .)* ']' 
| '{' ({!ahead("}'")}?      .)* '}' 
| '<' ({!ahead(">'")}?      .)* '>' 
| '(' ({!ahead(")'")}?      .)* ')' 
| . ({!ahead(getText().charAt(0) + "'")}? .)* . 
; 

input: `foo q'!My string which can contain 'single quotes'!' bar` 
    token: -> q'!My string which can contain 'single quotes'!' 

input: `foo q'(My string which can contain 'single quotes')' bar` 
    token: -> q'(My string which can contain 'single quotes')' 

input: `foo 'My string which can contain ''single quotes' bar` 
    token: -> 'My string which can contain ''single quotes' 

.を:印刷します

public class Main { 

    static void test(String input) { 
     TLexer lexer = new TLexer(new ANTLRInputStream(input)); 
     CommonTokenStream tokenStream = new CommonTokenStream(lexer); 
     tokenStream.fill(); 

     System.out.printf("input: `%s`\n", input); 

     for (Token token : tokenStream.getTokens()) { 
      if (token.getType() != TLexer.EOF) { 
       System.out.printf(" token: -> %s\n", token.getText()); 
      } 
     } 

     System.out.println(); 
    } 

    public static void main(String[] args) throws Exception { 
     test("foo q'!My string which can contain 'single quotes'!' bar"); 
     test("foo q'(My string which can contain 'single quotes')' bar"); 
     test("foo 'My string which can contain ''single quotes' bar"); 
    } 
} 

代替

| . ({!ahead(getText().charAt(0) + "'")}? .)* . 

に少しも寛容であるかもしれないが、それは否定、または通常の文字セットとそれを交換することによって微調整することができます。

関連する問題