CStyle 注释的 TextX 语法规则

问题描述 投票:0回答:1

我正在尝试创建一个 textx 语法,其中包含像 c/c++ 源文件一样具有单行注释的功能。例如

//this is a comment 

这是我为了测试这个而开发的语法。最终“条目”将不仅仅包含评论。

Ledger:entries+=Entry;

Entry: Comment;

Comment:/\/\/.*$/;

但是当我尝试解析以下文件时

//comment 1
//comment 2

我收到此错误

textx.exceptions.TextXSyntaxError: file.txt:3:1: Expected Comment => 'omment 2 *'

这是解析器的调试输出

*** PARSING MODEL ***
>> Matching rule Model=Sequence at position 0 => *//comment
   >> Matching rule Ledger=Sequence in Model at position 0 => *//comment
      >> Matching rule __asgn_oneormore=OneOrMore[entries] in Ledger at position 0 => *//comment
         ?? Try match rule Comment=RegExMatch(\/\/.*$) in __asgn_oneormore at position 0 => *//comment
         ?? Try match rule Comment=RegExMatch(\/\/.*$) in __asgn_oneormore at position 0 => *//comment
' at 0 => '*//comment 1*'ent 1
         ?? Try match rule Comment=RegExMatch(\/\/.*$) in __asgn_oneormore at position 13 => omment 1 *//comment
' at 13 => 'omment 1 *//comment 2*'
         ?? Try match rule Comment=RegExMatch(\/\/.*$) in __asgn_oneormore at position 26 => omment 2 *
         -- NoMatch at 26
         -- NoMatch at 26
      <<- Not matched rule __asgn_oneormore=OneOrMore[entries] in __asgn_oneormore at position 0 => *//comment
   <<- Not matched rule Ledger=Sequence in Ledger at position 0 => *//comment
<<- Not matched rule Model=Sequence in Model at position 0 => *//comment

如果我在输入规则中添加另一个选项,例如

Ledger:entries+=Entry;

Entry: Comment | FLOAT;

Comment:/\/\/.*$/;

并将该条目添加到文件中,例如

//comment 1
//comment 2
7

然后我不会抛出异常,但注释不会被解析(它们不会出现在我的模型中)。解析器调试输出如下。

>> Matching rule Model=Sequence at position 0 => *//comment
   >> Matching rule Ledger=Sequence in Model at position 0 => *//comment
      >> Matching rule __asgn_oneormore=OneOrMore[entries] in Ledger at position 0 => *//comment
         >> Matching rule Entry=OrderedChoice in __asgn_oneormore at position 0 => *//comment
            ?? Try match rule Comment=RegExMatch(\/\/.*$) in Entry at position 0 => *//comment
            ?? Try match rule Comment=RegExMatch(\/\/.*$) in Entry at position 0 => *//comment
' at 0 => '*//comment 1*'omment 1
            ?? Try match rule Comment=RegExMatch(\/\/.*$) in Entry at position 13 => omment 1 *//comment
' at 13 => 'omment 1 *//comment 2*'
            ?? Try match rule Comment=RegExMatch(\/\/.*$) in Entry at position 26 => omment 2 *7
            -- NoMatch at 26
            -- NoMatch at 26
            ?? Try match rule FLOAT=RegExMatch([+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?(?<=[\w\.])(?![\w\.])) in Entry at position 0 => *//comment
            ++ Match '7' at 26 => 'omment 2 *7*'
         <<+ Matched rule Entry=OrderedChoice in Entry at position 27 => mment 2 7*
         >> Matching rule Entry=OrderedChoice in __asgn_oneormore at position 27 => mment 2 7*
            ?? Try match rule Comment=RegExMatch(\/\/.*$) in Entry at position 27 => mment 2 7*
            ?? Try match rule Comment=RegExMatch(\/\/.*$) in Entry at position 27 => mment 2 7*
            -- NoMatch at 27
            -- NoMatch at 27
            ?? Try match rule FLOAT=RegExMatch([+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?(?<=[\w\.])(?![\w\.])) in Entry at position 27 => mment 2 7*
            -- NoMatch at 27
         <<- Not matched rule Entry=OrderedChoice in Entry at position 27 => mment 2 7*
      <<+ Matched rule __asgn_oneormore=OneOrMore[entries] in __asgn_oneormore at position 27 => mment 2 7*
   <<+ Matched rule Ledger=Sequence in Ledger at position 27 => mment 2 7*
   ?? Try match rule EOF in Model at position 27 => mment 2 7*
<<+ Matched rule Model=Sequence in Model at position 27 => mment 2 7*

任何人都可以帮我将 C 风格注释添加到我的语法中并最终出现在模型中吗? 谢谢!

comments dsl textx
1个回答
0
投票

在 textX 中尚不支持在模型中保留注释(和空格)(请参阅此问题)。不幸的是,唯一的选择是使用常规规则捕获注释,这可能会因注释处理作业而使您的语法变得混乱。您可以通过限制注释在模型中出现的位置来使维护稍微容易一些,但同样,这不是一个理想的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.