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

查看:32
本文介绍了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天全站免登陆