/** * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*))) * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*))) */ public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) { if ( t==null ) return false; GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK); if ( blk==null ) return false; int n = blk.getChildren().size(); for (int i = 0; i < n; i++) { GrammarAST alt = (GrammarAST)blk.getChildren().get(i); Tree first = alt.getChild(0); if ( first==null ) continue; if (first.getType() == ELEMENT_OPTIONS) { first = alt.getChild(1); if (first == null) { continue; } } if ( first.getType()==RULE_REF && first.getText().equals(ruleName) ) return true; Tree rref = first.getChild(1); if ( rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName) ) return true; } return false; }
/** * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*))) * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*))) */ public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) { if ( t==null ) return false; GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK); if ( blk==null ) return false; int n = blk.getChildren().size(); for (int i = 0; i < n; i++) { GrammarAST alt = (GrammarAST)blk.getChildren().get(i); Tree first = alt.getChild(0); if ( first==null ) continue; if (first.getType() == ELEMENT_OPTIONS) { first = alt.getChild(1); if (first == null) { continue; } } if ( first.getType()==RULE_REF && first.getText().equals(ruleName) ) return true; Tree rref = first.getChild(1); if ( rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName) ) return true; } return false; }
/** * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*))) * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*))) */ public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) { if ( t==null ) return false; GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK); if ( blk==null ) return false; int n = blk.getChildren().size(); for (int i = 0; i < n; i++) { GrammarAST alt = (GrammarAST)blk.getChildren().get(i); Tree first = alt.getChild(0); if ( first==null ) continue; if (first.getType() == ELEMENT_OPTIONS) { first = alt.getChild(1); if (first == null) { continue; } } if ( first.getType()==RULE_REF && first.getText().equals(ruleName) ) return true; Tree rref = first.getChild(1); if ( rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName) ) return true; } return false; }
/** * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*))) * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*))) */ public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) { if ( t==null ) return false; GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK); if ( blk==null ) return false; int n = blk.getChildren().size(); for (int i = 0; i < n; i++) { GrammarAST alt = (GrammarAST)blk.getChildren().get(i); Tree first = alt.getChild(0); if ( first==null ) continue; if (first.getType() == ELEMENT_OPTIONS) { first = alt.getChild(1); if (first == null) { continue; } } if ( first.getType()==RULE_REF && first.getText().equals(ruleName) ) return true; Tree rref = first.getChild(1); if ( rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName) ) return true; } return false; }
/** * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*))) * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*))) */ public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) { if ( t==null ) return false; GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK); if ( blk==null ) return false; int n = blk.getChildren().size(); for (int i = 0; i < n; i++) { GrammarAST alt = (GrammarAST)blk.getChildren().get(i); Tree first = alt.getChild(0); if ( first==null ) continue; if (first.getType() == ELEMENT_OPTIONS) { first = alt.getChild(1); if (first == null) { continue; } } if ( first.getType()==RULE_REF && first.getText().equals(ruleName) ) return true; Tree rref = first.getChild(1); if ( rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName) ) return true; } return false; }
/** Manually get option node from tree; return null if no defined. */ public static GrammarAST findOptionValueAST(GrammarRootAST root, String option) { GrammarAST options = (GrammarAST)root.getFirstChildWithType(ANTLRParser.OPTIONS); if ( options!=null && options.getChildCount() > 0 ) { for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType() == ANTLRParser.ASSIGN && c.getChild(0).getText().equals(option) ) { return (GrammarAST)c.getChild(1); } } } return null; }
/** Manually get option node from tree; return null if no defined. */ public static GrammarAST findOptionValueAST(GrammarRootAST root, String option) { GrammarAST options = (GrammarAST)root.getFirstChildWithType(ANTLRParser.OPTIONS); if ( options!=null && options.getChildCount() > 0 ) { for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType() == ANTLRParser.ASSIGN && c.getChild(0).getText().equals(option) ) { return (GrammarAST)c.getChild(1); } } } return null; }
/** Manually get option node from tree; return null if no defined. */ public static GrammarAST findOptionValueAST(GrammarRootAST root, String option) { GrammarAST options = (GrammarAST)root.getFirstChildWithType(ANTLRParser.OPTIONS); if ( options!=null && options.getChildCount() > 0 ) { for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType() == ANTLRParser.ASSIGN && c.getChild(0).getText().equals(option) ) { return (GrammarAST)c.getChild(1); } } } return null; }
/** Manually get option node from tree; return null if no defined. */ public static GrammarAST findOptionValueAST(GrammarRootAST root, String option) { GrammarAST options = (GrammarAST)root.getFirstChildWithType(ANTLRParser.OPTIONS); if ( options!=null && options.getChildCount() > 0 ) { for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType() == ANTLRParser.ASSIGN && c.getChild(0).getText().equals(option) ) { return (GrammarAST)c.getChild(1); } } } return null; }
/** Manually get option node from tree; return null if no defined. */ public static GrammarAST findOptionValueAST(GrammarRootAST root, String option) { GrammarAST options = (GrammarAST)root.getFirstChildWithType(ANTLRParser.OPTIONS); if ( options!=null && options.getChildCount() > 0 ) { for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType() == ANTLRParser.ASSIGN && c.getChild(0).getText().equals(option) ) { return (GrammarAST)c.getChild(1); } } } return null; }
/** * {@code (BLOCK (ALT .))} or {@code (BLOCK (ALT 'a') (ALT .))}. */ public static boolean blockHasWildcardAlt(GrammarAST block) { for (Object alt : block.getChildren()) { if ( !(alt instanceof AltAST) ) continue; AltAST altAST = (AltAST)alt; if ( altAST.getChildCount()==1 || (altAST.getChildCount() == 2 && altAST.getChild(0).getType() == ANTLRParser.ELEMENT_OPTIONS) ) { Tree e = altAST.getChild(altAST.getChildCount() - 1); if ( e.getType()==ANTLRParser.WILDCARD ) { return true; } } } return false; }
/** * {@code (BLOCK (ALT .))} or {@code (BLOCK (ALT 'a') (ALT .))}. */ public static boolean blockHasWildcardAlt(GrammarAST block) { for (Object alt : block.getChildren()) { if ( !(alt instanceof AltAST) ) continue; AltAST altAST = (AltAST)alt; if ( altAST.getChildCount()==1 || (altAST.getChildCount() == 2 && altAST.getChild(0).getType() == ANTLRParser.ELEMENT_OPTIONS) ) { Tree e = altAST.getChild(altAST.getChildCount() - 1); if ( e.getType()==ANTLRParser.WILDCARD ) { return true; } } } return false; }
/** * {@code (BLOCK (ALT .))} or {@code (BLOCK (ALT 'a') (ALT .))}. */ public static boolean blockHasWildcardAlt(GrammarAST block) { for (Object alt : block.getChildren()) { if ( !(alt instanceof AltAST) ) continue; AltAST altAST = (AltAST)alt; if ( altAST.getChildCount()==1 || (altAST.getChildCount() == 2 && altAST.getChild(0).getType() == ANTLRParser.ELEMENT_OPTIONS) ) { Tree e = altAST.getChild(altAST.getChildCount() - 1); if ( e.getType()==ANTLRParser.WILDCARD ) { return true; } } } return false; }
/** * {@code (BLOCK (ALT .))} or {@code (BLOCK (ALT 'a') (ALT .))}. */ public static boolean blockHasWildcardAlt(GrammarAST block) { for (Object alt : block.getChildren()) { if ( !(alt instanceof AltAST) ) continue; AltAST altAST = (AltAST)alt; if ( altAST.getChildCount()==1 || (altAST.getChildCount() == 2 && altAST.getChild(0).getType() == ANTLRParser.ELEMENT_OPTIONS) ) { Tree e = altAST.getChild(altAST.getChildCount() - 1); if ( e.getType()==ANTLRParser.WILDCARD ) { return true; } } } return false; }
/** * {@code (BLOCK (ALT .))} or {@code (BLOCK (ALT 'a') (ALT .))}. */ public static boolean blockHasWildcardAlt(@NotNull GrammarAST block) { for (Object alt : block.getChildren()) { if ( !(alt instanceof AltAST) ) continue; AltAST altAST = (AltAST)alt; if ( altAST.getChildCount()==1 || (altAST.getChildCount() == 2 && altAST.getChild(0).getType() == ANTLRParser.ELEMENT_OPTIONS) ) { Tree e = altAST.getChild(altAST.getChildCount() - 1); if ( e.getType()==ANTLRParser.WILDCARD ) { return true; } } } return false; }
/** Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) * set option assoc=right in TOKEN_REF. */ public static void setNodeOptions(GrammarAST node, GrammarAST options) { if ( options==null ) return; GrammarASTWithOptions t = (GrammarASTWithOptions)node; if ( t.getChildCount()==0 || options.getChildCount()==0 ) return; for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType()==ANTLRParser.ASSIGN ) { t.setOption(c.getChild(0).getText(), (GrammarAST)c.getChild(1)); } else { t.setOption(c.getText(), null); // no arg such as ID<VarNodeType> } } }
/** Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) * set option assoc=right in TOKEN_REF. */ public static void setNodeOptions(GrammarAST node, GrammarAST options) { if ( options==null ) return; GrammarASTWithOptions t = (GrammarASTWithOptions)node; if ( t.getChildCount()==0 || options.getChildCount()==0 ) return; for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType()==ANTLRParser.ASSIGN ) { t.setOption(c.getChild(0).getText(), (GrammarAST)c.getChild(1)); } else { t.setOption(c.getText(), null); // no arg such as ID<VarNodeType> } } }
/** Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) * set option assoc=right in TOKEN_REF. */ public static void setNodeOptions(GrammarAST node, GrammarAST options) { if ( options==null ) return; GrammarASTWithOptions t = (GrammarASTWithOptions)node; if ( t.getChildCount()==0 || options.getChildCount()==0 ) return; for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType()==ANTLRParser.ASSIGN ) { t.setOption(c.getChild(0).getText(), (GrammarAST)c.getChild(1)); } else { t.setOption(c.getText(), null); // no arg such as ID<VarNodeType> } } }
/** Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) * set option assoc=right in TOKEN_REF. */ public static void setNodeOptions(GrammarAST node, GrammarAST options) { if ( options==null ) return; GrammarASTWithOptions t = (GrammarASTWithOptions)node; if ( t.getChildCount()==0 || options.getChildCount()==0 ) return; for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType()==ANTLRParser.ASSIGN ) { t.setOption(c.getChild(0).getText(), (GrammarAST)c.getChild(1)); } else { t.setOption(c.getText(), null); // no arg such as ID<VarNodeType> } } }
/** Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) * set option assoc=right in TOKEN_REF. */ public static void setNodeOptions(GrammarAST node, GrammarAST options) { if ( options==null ) return; GrammarASTWithOptions t = (GrammarASTWithOptions)node; if ( t.getChildCount()==0 || options.getChildCount()==0 ) return; for (Object o : options.getChildren()) { GrammarAST c = (GrammarAST)o; if ( c.getType()==ANTLRParser.ASSIGN ) { t.setOption(c.getChild(0).getText(), (GrammarAST)c.getChild(1)); } else { t.setOption(c.getText(), null); // no arg such as ID<VarNodeType> } } }