我正在解析javascript ES6,但如果没有分号,我就无法使其工作。我使用了来自ES5的Tin语法作为基础,(https://github.com/usethesource/rascal/blob/master/src/org/rascalmpl/library/lang/javascript/saner/Syntax.rsc(,并且已经获得了ES6的大部分新功能,但我仍然无法消除对分号的需求
举个例子:
lexical Whitespace
= [t-nr ];
lexical LAYOUT
= Whitespace
| Comment
;
layout LAYOUTLIST
= LAYOUT*
!>> [t n]
!>> "/*"
!>> "//" ;
syntax Variable
= VariableIdentifier {VariableDeclaration ","}+ declarations ";"
syntax Statement
= varDecl: Variable varDecl;
我在语法变量上将";"替换为"\n"时遇到解析错误,甚至为语句结尾创建了一个新规则:
syntax EOS
= ";" | "n";
解析错误会转移到换行后的下一行。
从空白区或布局列表中删除会使我在文件开头的注释中出现解析错误。
从布局列表中删除会让我产生歧义。
考虑到一种较小的语言,我正在处理这个问题(语句的结尾可能是";"、"\n"或文件的结尾(,使用:
syntax EOS = ";" // handle ";" as end of statement
| Epsilon $ // handle "n" and EOF as end of statement
| Epsilon >> "}" // it might be also necessary (from ANTLR grammar)
;
syntax Epsilon = ;
//and a test case
test bool testParseEOS() {
try {
parse(#BlockStmt, "{x := x + 1; y := 10 n y := z + 1; x := z }");
return true;
}
catch ParseError(loc l): {
println("I found a parse error at line <l.begin.line>, column <l.begin.column>");
return false;
}
}