我将以下 EmojiLexer.g4 与 ANTLR 4.7.2 一起使用
lexer grammar EmojiLexer;
String
: '"' StringChar* '"'
;
fragment StringChar
: ~["\r\n]
;
Punctuation
: ( '['
| ':'
| ']'
| ';'
| '@'
)
;
fragment IdentifierNonDigit
: [a-zA-Z_$\u0080-\uffff]
;
Identifier
: IdentifierNonDigit+
;
Whitespace
: [ \t]+
-> skip
;
Newline
: ( '\r' '\n'?
| '\n'
)
-> skip
;
并使用以下 Java 代码来对字符串进行词法分析:
public class EmojiTest {
public static void main(String[] args) {
final String string = "[foo bar:bazz];\n"
+ "\n"
+ "@\"emojis break it: \uD83D\uDE31\";\n"
+ "\n"
+ "[foo bar:bazz];\n";
final CharStream charStream = CharStreams.fromString(string);
final EmojiLexer lexer = new EmojiLexer(charStream);
while (true) {
final Token token = lexer.nextToken();
final int type = token.getType();
if (type < 0) {
break;
}
final int startIndex = token.getStartIndex();
final int stopIndex = token.getStopIndex() + 1;
System.out.println(startIndex + "-" + stopIndex + ": " + type + ": " + escape(string.substring(startIndex, stopIndex)));
}
}
private static String escape(String s) {
final StringBuilder buffer = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
final char chr = s.charAt(i);
if (chr >= 0x20 && chr < 0x7f) {
buffer.append(chr);
}
else {
buffer.append("\\u");
final String hex = Integer.toHexString(chr);
for (int j = hex.length(); j < 4; j++) {
buffer.append('0');
}
buffer.append(hex);
}
}
return buffer.toString();
}
}
但不知何故,表情符号双字符😱混淆了 ANTLR,因为结果是:
0-1: 2: [
1-4: 3: foo
5-8: 3: bar
8-9: 2: :
9-13: 3: bazz
13-14: 2: ]
14-15: 2: ;
17-18: 2: @
18-38: 1: "emojis break it: \ud83d\ude31
38-39: 2: "
41-42: 2: \u000a
42-45: 3: [fo
46-49: 3: ba
49-50: 2: r
50-54: 3: :baz
54-55: 2: z
55-56: 2: ]
这是我的代码或 ANTLR 中的错误吗?
最佳答案
您使用代理对来指定表情符号。我不确定这是否仍然受支持。而是使用完整的 UTF-32 规范:\u{1f631}
表示 😱
如果您想捕获所有表情符号,而不仅仅是这一个,您甚至可以使用:
EMOJI : [\p{Emoji}];
它使用表情符号的 Unicode 字符类。
另请参阅:https://github.com/antlr/antlr4/blob/master/doc/unicode.md#unicode-code-points-in-lexer-grammars
通过用 UTF-32 字符交换代理项,我得到了以下标记列表:
[@0,0:0='[',<2>,1:0]
[@1,1:3='foo',<3>,1:1]
[@2,5:7='bar',<3>,1:5]
[@3,8:8=':',<2>,1:8]
[@4,9:12='bazz',<3>,1:9]
[@5,13:13=']',<2>,1:13]
[@6,14:14=';',<2>,1:14]
[@7,17:17='@',<2>,3:0]
[@8,18:37='"emojis break it: 😱"',<1>,3:1]
[@9,38:38=';',<2>,3:21]
[@10,41:41='[',<2>,5:0]
[@11,42:44='foo',<3>,5:1]
[@12,46:48='bar',<3>,5:5]
[@13,49:49=':',<2>,5:8]
[@14,50:53='bazz',<3>,5:9]
[@15,54:54=']',<2>,5:13]
[@16,55:55=';',<2>,5:14]
[@17,57:56='<EOF>',<-1>,6:0]
这是我使用的输入:
[foo bar:bazz];
@"emojis break it: 😱";
[foo bar:bazz];
我必须承认我使用 ANLTR4 扩展对其进行了测试,该扩展使用 Typescript 而不是 Java,但我相信这并不重要。
关于java - ANTLR:处理表情符号字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62538445/