forked from jasder/antlr
reorg r : '...' ; matching. deals with -> cmds better (and more of em)
This commit is contained in:
parent
de744f912a
commit
4923cb3874
|
@ -742,6 +742,17 @@ public class Grammar implements AttributeResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String,String> getStringLiteralAliasesFromLexerRules(GrammarRootAST ast) {
|
public static Map<String,String> getStringLiteralAliasesFromLexerRules(GrammarRootAST ast) {
|
||||||
|
String[] patterns = {
|
||||||
|
"(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL)))",
|
||||||
|
"(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL ACTION)))",
|
||||||
|
"(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL SEMPRED)))",
|
||||||
|
"(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) .)))",
|
||||||
|
"(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . .)))",
|
||||||
|
"(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .))))",
|
||||||
|
"(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . (LEXER_ACTION_CALL . .))))",
|
||||||
|
"(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .) .)))",
|
||||||
|
// TODO: allow doc comment in there
|
||||||
|
};
|
||||||
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(ast.token.getInputStream());
|
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(ast.token.getInputStream());
|
||||||
TreeWizard wiz = new TreeWizard(adaptor,ANTLRParser.tokenNames);
|
TreeWizard wiz = new TreeWizard(adaptor,ANTLRParser.tokenNames);
|
||||||
Map<String,String> lexerRuleToStringLiteral = new HashMap<String,String>();
|
Map<String,String> lexerRuleToStringLiteral = new HashMap<String,String>();
|
||||||
|
@ -751,51 +762,36 @@ public class Grammar implements AttributeResolver {
|
||||||
|
|
||||||
for (GrammarAST r : ruleNodes) {
|
for (GrammarAST r : ruleNodes) {
|
||||||
//tool.log("grammar", r.toStringTree());
|
//tool.log("grammar", r.toStringTree());
|
||||||
|
System.out.println("chk: "+r.toStringTree());
|
||||||
Tree name = r.getChild(0);
|
Tree name = r.getChild(0);
|
||||||
if ( name.getType()==ANTLRParser.TOKEN_REF ) {
|
if ( name.getType()==ANTLRParser.TOKEN_REF ) {
|
||||||
Map nodes = new HashMap();
|
// check rule against patterns
|
||||||
boolean isLitRule =
|
boolean isLitRule = false;
|
||||||
wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL)))", nodes);
|
for (String pattern : patterns) {
|
||||||
if ( isLitRule ) {
|
|
||||||
GrammarAST litNode = (GrammarAST)nodes.get("lit");
|
|
||||||
GrammarAST nameNode = (GrammarAST)nodes.get("name");
|
|
||||||
lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// TODO: allow doc comment in there
|
|
||||||
nodes = new HashMap();
|
|
||||||
// try with action in there
|
|
||||||
isLitRule =
|
isLitRule =
|
||||||
wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL ACTION)))", nodes);
|
defAlias(r, pattern, wiz, lexerRuleToStringLiteral);
|
||||||
if ( isLitRule ) {
|
if ( isLitRule ) break;
|
||||||
GrammarAST litNode = (GrammarAST)nodes.get("lit");
|
|
||||||
GrammarAST nameNode = (GrammarAST)nodes.get("name");
|
|
||||||
lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
nodes = new HashMap();
|
|
||||||
// try with pred in there
|
|
||||||
isLitRule =
|
|
||||||
wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL SEMPRED)))", nodes);
|
|
||||||
if ( isLitRule ) {
|
|
||||||
GrammarAST litNode = (GrammarAST)nodes.get("lit");
|
|
||||||
GrammarAST nameNode = (GrammarAST)nodes.get("name");
|
|
||||||
lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText());
|
|
||||||
}
|
|
||||||
nodes = new HashMap();
|
|
||||||
// try with -> lexercommand in there
|
|
||||||
isLitRule =
|
|
||||||
wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) ID)))", nodes);
|
|
||||||
if ( isLitRule ) {
|
|
||||||
GrammarAST litNode = (GrammarAST)nodes.get("lit");
|
|
||||||
GrammarAST nameNode = (GrammarAST)nodes.get("name");
|
|
||||||
lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText());
|
|
||||||
}
|
}
|
||||||
|
if ( !isLitRule ) System.out.println("no pattern matched");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return lexerRuleToStringLiteral;
|
return lexerRuleToStringLiteral;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static boolean defAlias(GrammarAST r, String pattern,
|
||||||
|
TreeWizard wiz,
|
||||||
|
Map<String, String> lexerRuleToStringLiteral)
|
||||||
|
{
|
||||||
|
HashMap nodes = new HashMap();
|
||||||
|
if ( wiz.parse(r, pattern, nodes) ) {
|
||||||
|
GrammarAST litNode = (GrammarAST)nodes.get("lit");
|
||||||
|
GrammarAST nameNode = (GrammarAST)nodes.get("name");
|
||||||
|
lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<String> getStringLiterals() {
|
public Set<String> getStringLiterals() {
|
||||||
final Set<String> strings = new HashSet<String>();
|
final Set<String> strings = new HashSet<String>();
|
||||||
GrammarTreeVisitor collector = new GrammarTreeVisitor() {
|
GrammarTreeVisitor collector = new GrammarTreeVisitor() {
|
||||||
|
|
Loading…
Reference in New Issue