ANTLR Lua长字符串语法规则 [英] ANTLR Lua long string grammar rules

查看:302
本文介绍了ANTLR Lua长字符串语法规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Lua创建ANTLR解析器.因此,我学习了Nicolai Mainero制作的语法(可在ANTLR的站点上下载,Lua 5.1语法)并开始工作.

I'm trying to create ANTLR parser for Lua. So i took grammar produced by Nicolai Mainero(available at ANTLR's site, Lua 5.1 grammar) and begin to work.

语法很好.一件事不起作用:长字符串.

Grammar is good. One thing not working: LONG STRINGS.

Lua规范规则:文字

Lua specification rule: Literal

字符串也可以使用 用长括号括起来的长格式. 我们定义了一个 级别n作为开方括号 紧接着是n个等号,然后是 另一个开口方括号.所以, 级别0的长括号是 写为[[,长括号 1级的表示为[= [,因此 在.定义了一个长括号 相似地;例如,闭幕 级别4的长括号写为 ] ====].长字符串以 打开任何级别的长括号,并 在第一个结束的长括号处结束 处于同一水平.本中的文字 方括号形式可以运行几个 行,请勿解释任何转义 序列,并忽略以下内容的长括号 任何其他级别.它们可以包含 除的右括号外的任何内容 适当的水平.适当的水平.

strings can also be defined using a long format enclosed by long brackets. We define an opening long bracket of level n as an opening square bracket followed by n equal signs followed by another opening square bracket. So, an opening long bracket of level 0 is written as [[, an opening long bracket of level 1 is written as [=[, and so on. A closing long bracket is defined similarly; for instance, a closing long bracket of level 4 is written as ]====]. A long string starts with an opening long bracket of any level and ends at the first closing long bracket of the same level. Literals in this bracketed form can run for several lines, do not interpret any escape sequences, and ignore long brackets of any other level. They can contain anything except a closing bracket of the proper level.the proper level.

我的问题与的含义接近,但是工具有所不同.

My question is close by meaning to this but tools are different.

一些关于LONGSTRING的小例子:

Some little example of LONGSTRING:

local a = [==[ Some interesting string [=[ sub string in string ]=] [hello indexes] [[And some line strings]] ]==] - its correct string. 
local f = [==[ Not interesting string ]=] - incorrect string

以下是我的LONGSTRING规则,其中没有'='符号:

Here my rule for LONGSTRING with out '=' symbol:

LONGSTRING: '[[' (~(']') | ']'(~(']')))* ']]';

有人可以帮助我吗?谢谢!

Can somebody help me? Thanks!

推荐答案

我曾经根据规范编写了Lua语法,并像这样解决了它:

I once wrote a Lua grammar according the specs and solved it like this:

grammar Lua;

// ... options ...

// ... tokens ...

@lexer::members {
    public boolean noCloseAhead(int numEqSigns) {
        if(input.LA(1) != ']') return true;
        for(int i = 2; i < numEqSigns+2; i++) {
            if(input.LA(i) != '=') return true;
        }
        return input.LA(numEqSigns+2) != ']';
    }

    public void matchClose(int numEqSigns) throws MismatchedTokenException {
        StringBuilder eqSigns = new StringBuilder();
        for(int i = 0; i < numEqSigns; i++) {
            eqSigns.append('=');
        }
        match("]"+eqSigns+"]");
    }
}

// ... parser rules ...

String
  :  '"'  (~('"'  | '\\') | EscapeSequence)* '"'
  |  '\'' (~('\'' | '\\') | EscapeSequence)* '\''
  |  LongBracket
  ;

Comment
  :  (BlockComment | LineComment) {skip();}
  ;

fragment
BlockComment
  :  '--' LongBracket 
  ;

fragment
LineComment
  :  '--' ~('\r' | '\n')* ('\r'? '\n' | EOF) 
  ;

fragment
LongBracket
@init {int openEq = 0;}
  :  '[' ('=' {openEq++;})* '[' ({noCloseAhead(openEq)}?=> .)* {matchClose(openEq);}
  ;

// ... more lexer rules ...

请注意在ANTLR Wiki上找到的内容!顾名思义:它是Wiki,可以很容易地发布内容.您提到的Lua语法是一个不错的开始,但其中有很多错误(至少在我看时,二进制或十六进制文字也不正确).

Be careful with what you find on the ANTLR Wiki! As the name suggests: it's a Wiki and one can post stuff fairly easy. The Lua grammar you mention is a nice start, but has quite a bit of errors in it (binary or hex literals are incorrect as well, at least, at the time I looked at it...).

这篇关于ANTLR Lua长字符串语法规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆