我正在尝试更新遵循以下规范的ANTLR语法
https://github.com/facebook/graphql/pull/327/files
在逻辑上,它定义为
StringValue ::
- `"` StringCharacter* `"`
- `"""` MultiLineStringCharacter* `"""`
StringCharacter ::
- SourceCharacter but not `"` or \ or LineTerminator
- \u EscapedUnicode
- \ EscapedCharacter
MultiLineStringCharacter ::
- SourceCharacter but not `"""` or `\"""`
- `\"""`
(不是上面的逻辑 - 不是ANTLR语法)
我在ANTRL 4中尝试了以下内容,但它不会识别三重引号字符串中的超过1个字符
string : triplequotedstring | StringValue ;
triplequotedstring: '"""' triplequotedstringpart? '"""';
triplequotedstringpart : EscapedTripleQuote* | SourceCharacter*;
EscapedTripleQuote : '\\"""';
SourceCharacter :[\u0009\u000A\u000D\u0020-\uFFFF];
StringValue: '"' (~(["\\\n\r\u2028\u2029])|EscapedChar)* '"';
根据这些规则,它将识别"""""""'但是只要我添加更多字符就会失败
例如:'""" abc"""'不解析和ANTLR的IntelliJ插件说
line 1:14 extraneous input 'abc' expecting {'"""', '\\"""', SourceCharacter}
如何在ANTLR中使用' \"""''逸出?
答案 0 :(得分:0)
你的一些削弱规则应该是lexer规则。 SourceCharacter
应该是fragment
。
此外,您可能需要EscapedTripleQuote* | SourceCharacter*
而不是( EscapedTripleQuote | SourceCharacter )*
。第一个匹配aaa...
或bbb...
,而您可能需要匹配aababbba...
尝试这样的事情:
string
: Triplequotedstring
| StringValue
;
Triplequotedstring
: '"""' TriplequotedstringPart*? '"""'
;
StringValue
: '"' ( ~["\\\n\r\u2028\u2029] | EscapedChar )* '"'
;
// Fragments never become a token of their own: they are only used inside other lexer rules
fragment TriplequotedstringPart : EscapedTripleQuote | SourceCharacter;
fragment EscapedTripleQuote : '\\"""';
fragment SourceCharacter :[\u0009\u000A\u000D\u0020-\uFFFF];