私はjisonファイルを扱っており、python PLYのlexモジュールを使用してパーサージェネレーターに変換しています。
この jison ファイルでは、特定のトークンに複数のルールが関連付けられていることに気付きました。たとえば、 tokenCONTENT
の場合、ファイルは次の 3 つのルールを指定します。
[^\x00]*?/("{{") {
if(yytext.slice(-2) === "\\\\") {
strip(0,1);
this.begin("mu");
} else if(yytext.slice(-1) === "\\") {
strip(0,1);
this.begin("emu");
} else {
this.begin("mu");
}
if(yytext) return 'CONTENT';
}
[^\x00]+ return 'CONTENT';
// marks CONTENT up to the next mustache or escaped mustache
<emu>[^\x00]{2,}?/("{{"|"\\{{"|"\\\\{{"|<<EOF>>) {
this.popState();
return 'CONTENT';
}
COMMENT
別のケースでは、トークンに複数のルールがあります。
<com>[\s\S]*?"--}}" strip(0,4); this.popState(); return 'COMMENT';
<mu>"{{!--" this.popState(); this.begin('com');
<mu>"{{!"[\s\S]*?"}}" strip(3,5); this.popState(); return 'COMMENT';
異なる州に適用されるルールを区別するのは簡単に思えますが、同じ州に適用される場合はどうでしょうか?
ply.lex を使用して、このジソンを Python ルールに変換するにはどうすればよいですか?
編集
参考までに、この jison ファイルは handlebars.js ソース コードの一部です。参照: https://github.com/wycats/handlebars.js/blob/master/src/handlebars.l