在 ANTLR4 语法中转义两个字符转义序列的最佳方法是什么?
what is the best way to escape two character escape sequences in an ANTLR4 grammar?
我正在尝试为类似于 mustache 的模板语言编写 ANTLR4 解析器。这使用散布在普通文本文件中的 {{...}}
标签。如果模板需要在 OPEN_TAG {{
旁边包含并发出 {
,lexer/parser 可能会出现问题。我相信应该有一种方法可以这样编写解析器:
This is a left brace {{{tag logic}} and here are two left braces {{{tag logic}}{
转换为
This is a left brace { and here are two left braces {{
或者:
- 我怎样才能告诉词法分析器只匹配 OPEN_TAG 到
{{
然后
除 {
之外的任何内容,将前导 {
吸收到前一个 TEXT
模式?
- 是否有 "better" 方法为
{{
提供转义序列?
谢谢!
使用模式处理词法分析器中的标签。
LBrace : '{' ;
RBrace : '}' ;
TOpenTag : '{{' -> pushMode(tagLogic) ;
mode tagLogic ;
TLBrace : '{' -> type(LBrace) ;
TRBrace : '}' -> type(RBrace) ;
TCloseTag : '}}' -> popMode ;
TLogic : [a-zA-Z0-9]+ ;
TWs : [ \t\r\n]+ -> skip ;
我正在尝试为类似于 mustache 的模板语言编写 ANTLR4 解析器。这使用散布在普通文本文件中的 {{...}}
标签。如果模板需要在 OPEN_TAG {{
旁边包含并发出 {
,lexer/parser 可能会出现问题。我相信应该有一种方法可以这样编写解析器:
This is a left brace {{{tag logic}} and here are two left braces {{{tag logic}}{
转换为
This is a left brace { and here are two left braces {{
或者:
- 我怎样才能告诉词法分析器只匹配 OPEN_TAG 到
{{
然后 除{
之外的任何内容,将前导{
吸收到前一个 TEXT 模式? - 是否有 "better" 方法为
{{
提供转义序列?
谢谢!
使用模式处理词法分析器中的标签。
LBrace : '{' ;
RBrace : '}' ;
TOpenTag : '{{' -> pushMode(tagLogic) ;
mode tagLogic ;
TLBrace : '{' -> type(LBrace) ;
TRBrace : '}' -> type(RBrace) ;
TCloseTag : '}}' -> popMode ;
TLogic : [a-zA-Z0-9]+ ;
TWs : [ \t\r\n]+ -> skip ;