特定の言語ターゲットに依存することなく、レクサールールでこれを実行できますか?
いいえ、ターゲット依存コードはそのようなことに必要です。
ちょうどあなた、または他の誰かがこの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) + "'")}? .)* .
に少しも寛容であるかもしれないが、それは否定、または通常の文字セットとそれを交換することによって微調整することができます。