我正在解析 javascript ES6,但是如果没有分号我就无法让它工作。我使用 ES5 中的 Tin 语法作为基础,( https://github.com/usethesource/rascal/blob/master/src/org/rascalmpl/library/lang/javascript/saner/Syntax.rsc ) 并且已经获得了 ES6 中的大部分新功能,但我仍然无法消除对分号的需求
举个例子:
lexical Whitespace
= [\t-\n\r\ ];
lexical LAYOUT
= Whitespace
| Comment
;
layout LAYOUTLIST
= LAYOUT*
!>> [\t\ \n]
!>> "/*"
!>> "//" ;
syntax Variable
= VariableIdentifier {VariableDeclaration ","}+ declarations ";"
syntax Statement
= varDecl: Variable varDecl;
我在替换“;”时遇到解析错误到 Syntax Variable 上的“\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;
}
}
关于javascript - 新行作为空格和终止符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53410259/