带初始化和不带初始化的变量定义树
我要解析的语言包含类似
public var a, b = 42, c;
I.e. 的 语句.g 文件看起来像这样:
statements
: (introduction | expression ';'! | ... )+
;
introduction
: head single+ -> ^(head single)+
;
single
: Name ('='^ expression)?
;
head
: modifiers* v='var' -> ^(VARIABLE[$v] modifiers*)
;
生成这样的树很容易,但大多没用(对我来说):
----------statements----------
/ | \
variable variable variable
/ \ / \ / \
'public' 'a' 'public' '=' 'public' 'c'
/ \
'b' expr
我希望将 '='
放在中间节点的顶部:
----------statements----------
/ | \
variable '=' variable
/ \ / \ / \
'public' 'a' variable expr 'public' 'c'
/ \
'public' 'b'
但是我找不到执行此操作的重写规则。
my language to parse contains statements like
public var a, b = 42, c;
I.e. the .g file looks something like:
statements
: (introduction | expression ';'! | ... )+
;
introduction
: head single+ -> ^(head single)+
;
single
: Name ('='^ expression)?
;
head
: modifiers* v='var' -> ^(VARIABLE[$v] modifiers*)
;
Generating a tree like that would be easy, but mostly useless (for me):
----------statements----------
/ | \
variable variable variable
/ \ / \ / \
'public' 'a' 'public' '=' 'public' 'c'
/ \
'b' expr
I would like to have the the '='
on top of the middle node:
----------statements----------
/ | \
variable '=' variable
/ \ / \ / \
'public' 'a' variable expr 'public' 'c'
/ \
'public' 'b'
but I can't find the rewrite rule to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以你制定规则的方式来说,这并不容易做到。
这是一种可能的方式:
它会生成以下 AST:
>
并生成:
输入:
That is not easly done with the way you've set up your rules.
Here's a way it is possible:
which produces the following AST:
for the input:
And produces:
for the input: