我应该如何将此语法添加到我的 html antlr4 语法中?
How should i add this syntax to my html antlr4 grammar?
首先,这是我应该做的一个例子
<p cp-for="x in t;i=index"> {{x}}, {{i}} </p>
我需要从 HTML 文本中单独解析 mustache 语法
(这意味着不能将逗号解析为 html 文本)。
这是我写的语法的一部分:
OPEN_MUSTACHE: '{{' -> pushMode (MUSTACHE_SYNTAX)
mode MUSTACHE_SYNTAX;
//there are other tokens describing the syntax but i didn't write it
CLOSE_MUSTACHE: '}}' -> popMode
因此,如果我想像示例中那样添加另一个小胡子标签,我如何告诉词法分析器向前看,而不是在它看到 MUSTACHE_CLOSE 后立即弹出?
P.S
这是我第一次在这个网站上提问;
如果我的问题不清楚,我深表歉意
编辑:我现在明白我的语法错误了,
{{ x }} , {{ i }}
中的逗号实际上是普通的 html text
So if i want to add another mustache tag like in the example, how can i tell the lexer to look ahead and not pop immediately after it sees MUSTACHE_CLOSE?
对于输入 {{#check}}, {{/check}}
,词法分析器应创建以下标记:
{{
: 打开令牌
#
: 打开标签令牌
check
: name/id 令牌
}}
: 关闭令牌
,
:普通HTML令牌
:普通HTML令牌
{{
: 打开令牌
/
: 关闭标记令牌
check
: name/id 令牌
}}
: 关闭令牌
因此,当您在 MUSTACHE_SYNTAX
模式中遇到 }}
时,您可以直接返回默认模式。
然后在你的解析器中,你做这样的事情:
parser grammar MustacheParser;
options {
tokenVocab=MustacheLexer;
}
template
: template_contents EOF
;
template_contents
: template_part*
;
template_part
: html
| mustache
;
html
: HTML+
;
mustache
: section
| ...
;
section
: '{{' '#' NAME '}}' template_contents '{{' '/' NAME '}}'
;
(当然,文字标记,如 '{{'
、'#'
等,在解析器语法中是不允许的,它只是伪代码。用你的词法分析器语法中的标记替换它们。 )
首先,这是我应该做的一个例子
<p cp-for="x in t;i=index"> {{x}}, {{i}} </p>
我需要从 HTML 文本中单独解析 mustache 语法 (这意味着不能将逗号解析为 html 文本)。
这是我写的语法的一部分:
OPEN_MUSTACHE: '{{' -> pushMode (MUSTACHE_SYNTAX)
mode MUSTACHE_SYNTAX;
//there are other tokens describing the syntax but i didn't write it
CLOSE_MUSTACHE: '}}' -> popMode
因此,如果我想像示例中那样添加另一个小胡子标签,我如何告诉词法分析器向前看,而不是在它看到 MUSTACHE_CLOSE 后立即弹出?
P.S 这是我第一次在这个网站上提问; 如果我的问题不清楚,我深表歉意
编辑:我现在明白我的语法错误了,
{{ x }} , {{ i }}
中的逗号实际上是普通的 html text
So if i want to add another mustache tag like in the example, how can i tell the lexer to look ahead and not pop immediately after it sees MUSTACHE_CLOSE?
对于输入 {{#check}}, {{/check}}
,词法分析器应创建以下标记:
{{
: 打开令牌#
: 打开标签令牌check
: name/id 令牌}}
: 关闭令牌,
:普通HTML令牌{{
: 打开令牌/
: 关闭标记令牌check
: name/id 令牌}}
: 关闭令牌
因此,当您在 MUSTACHE_SYNTAX
模式中遇到 }}
时,您可以直接返回默认模式。
然后在你的解析器中,你做这样的事情:
parser grammar MustacheParser;
options {
tokenVocab=MustacheLexer;
}
template
: template_contents EOF
;
template_contents
: template_part*
;
template_part
: html
| mustache
;
html
: HTML+
;
mustache
: section
| ...
;
section
: '{{' '#' NAME '}}' template_contents '{{' '/' NAME '}}'
;
(当然,文字标记,如 '{{'
、'#'
等,在解析器语法中是不允许的,它只是伪代码。用你的词法分析器语法中的标记替换它们。 )