protected virtual void CheckForRuleDefinitionProblems(Rule r) { string ruleName = r.Name; IToken ruleToken = r.Tree.Token; int msgID = 0; if ((grammar.type == GrammarType.Parser || grammar.type == GrammarType.TreeParser) && Rule.GetRuleType(ruleName) == RuleType.Lexer) { msgID = ErrorManager.MSG_LEXER_RULES_NOT_ALLOWED; } else if (grammar.type == GrammarType.Lexer && Rule.GetRuleType(ruleName) == RuleType.Parser && !r.IsSynPred) { msgID = ErrorManager.MSG_PARSER_RULES_NOT_ALLOWED; } else if (grammar.GetGlobalScope(ruleName) != null) { msgID = ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE; } if (msgID != 0) { ErrorManager.GrammarError(msgID, grammar, ruleToken, ruleName); } }
/** For references to tokens rather than by label such as $ID, we * need to get the existing label for the ID ref or create a new * one. */ public virtual string GetElementLabel(string refdSymbol, int outerAltNum, CodeGenerator generator) { GrammarAST uniqueRefAST; if (Grammar.type != GrammarType.Lexer && Rule.GetRuleType(refdSymbol) == RuleType.Lexer) { // symbol is a token IList <GrammarAST> tokenRefs = GetTokenRefsInAlt(refdSymbol, outerAltNum); uniqueRefAST = tokenRefs[0]; } else { // symbol is a rule IList <GrammarAST> ruleRefs = GetRuleRefsInAlt(refdSymbol, outerAltNum); uniqueRefAST = ruleRefs[0]; } if (uniqueRefAST.code == null) { // no code? must not have gen'd yet; forward ref return(null); } string labelName = null; string existingLabelName = (string)uniqueRefAST.code.GetAttribute("label"); // reuse any label or list label if it exists if (existingLabelName != null) { labelName = existingLabelName; } else { // else create new label labelName = generator.CreateUniqueLabel(refdSymbol); CommonToken label = new CommonToken(ANTLRParser.ID, labelName); if (Grammar.type != GrammarType.Lexer && Rule.GetRuleType(refdSymbol) == Tool.RuleType.Lexer) { Grammar.DefineTokenRefLabel(Name, label, uniqueRefAST); } else { Grammar.DefineRuleRefLabel(Name, label, uniqueRefAST); } uniqueRefAST.code.SetAttribute("label", labelName); } return(labelName); }
protected override void TrackTokenRule(GrammarAST t, GrammarAST modifier, GrammarAST block) { // imported token names might exist, only add if new if (grammar.type == GrammarType.Lexer || grammar.type == GrammarType.Combined) { if (Rule.GetRuleType(t.Text) == RuleType.Parser) { return; } if (t.Text.Equals(Grammar.ArtificialTokensRuleName)) { // don't add Tokens rule return; } // track all lexer rules so we can look for token refs w/o // associated lexer rules. grammar.composite.lexerRules.Add(t.Text); int existing = grammar.GetTokenType(t.Text); if (existing == Label.INVALID) { tokens[t.Text] = Unassigned; } // look for "<TOKEN> : <literal> ;" pattern // (can have optional action last) if (block.HasSameTreeStructure(charAlias) || block.HasSameTreeStructure(stringAlias) || block.HasSameTreeStructure(charAlias2) || block.HasSameTreeStructure(stringAlias2)) { tokenRuleDefs.Add(t.Text); /* * Grammar parent = grammar.composite.getDelegator(grammar); * boolean importedByParserOrCombined = * parent!=null && * (parent.type==GrammarType.Lexer||parent.type==GrammarType.Parser); */ if (grammar.type == GrammarType.Combined || grammar.type == GrammarType.Lexer) { // only call this rule an alias if combined or lexer Alias(t, (GrammarAST)block.GetChild(0).GetChild(0)); } } } // else error }
public virtual void TranslateLeftRecursiveRules() { IList <Grammar> grammars = delegateGrammarTreeRoot.GetPostOrderedGrammarList(); for (int i = 0; grammars != null && i < grammars.Count; i++) { Grammar g = (Grammar)grammars[i]; if (!(g.type == GrammarType.Parser || g.type == GrammarType.Combined)) { continue; } foreach (GrammarAST r in g.Tree.FindAllType(ANTLRParser.RULE)) { if (Rule.GetRuleType(r.GetChild(0).Text) == RuleType.Parser) { g.TranslateLeftRecursiveRule(r); } } } }
/** Track string literals (could be in tokens{} section) */ protected override void TrackString(GrammarAST t) { // if lexer, don't allow aliasing in tokens section if (currentRuleName == null && grammar.type == GrammarType.Lexer) { ErrorManager.GrammarError(ErrorManager.MSG_CANNOT_ALIAS_TOKENS_IN_LEXER, grammar, t.token, t.Text); return; } // in a plain parser grammar rule, cannot reference literals // (unless defined previously via tokenVocab option) // don't warn until we hit root grammar as may be defined there. if (grammar.IsRoot && grammar.type == GrammarType.Parser && grammar.GetTokenType(t.Text) == Label.INVALID) { ErrorManager.GrammarError(ErrorManager.MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE, grammar, t.token, t.Text); } // Don't record literals for lexers, they are things to match not tokens if (grammar.type == GrammarType.Lexer) { return; } // otherwise add literal to token types if referenced from parser rule // or in the tokens{} section if ((currentRuleName == null || Rule.GetRuleType(currentRuleName) == RuleType.Parser) && grammar.GetTokenType(t.Text) == Label.INVALID) { stringLiterals[t.Text] = UnassignedInParserRule; } }
public static void ACTION(TokenRewriteStream tokens, CommonTree t) { CommonTree parent = (CommonTree)t.Parent; int ptype = parent.Type; if (ptype == ANTLRParser.SCOPE || // we have special rules for these ptype == ANTLRParser.AMPERSAND) { return; } //Console.Out.WriteLine( "ACTION: " + t.Text ); CommonTree root = (CommonTree)t.GetAncestor(ANTLRParser.RULE); if (root != null) { CommonTree rule = (CommonTree)root.GetChild(0); //Console.Out.WriteLine( "rule: " + rule ); if (Rule.GetRuleType(rule.Text) == RuleType.Parser) { tokens.Delete(t.TokenStartIndex, t.TokenStopIndex); KillTrailingNewline(tokens, t.Token.TokenIndex); } } }