Ejemplo n.º 1
0
        public void TestRewriteRuleAndRewriteModeIgnoreActionsPredicates() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a: {action} {action2} x=A -> {ick}\n" +
                " | {pred1}? y+=B -> {ick}\n" +
                " | C {action} -> {ick}\n" +
                " | {pred2}?=> z+=D -> {ick}\n" +
                " | (E)=> ^(F G) -> {ick}\n" +
                " ;\n"
                );
            AntlrTool antlr = newTool();

            antlr.SetOutputDirectory(null);   // write to /dev/null
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
Ejemplo n.º 2
0
        public void TestPredsUsedAfterRecursionOverflow() /*throws Exception*/
        {
            // analysis must bail out due to non-LL(*) nature (ovf)
            // retries with k=1 (but with LL(*) algorithm not optimized version
            // as it has preds)
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "s : {p1}? e '.' | {p2}? e ':' ;\n" +
                "e : '(' e ')' | INT ;\n");
            string expecting =
                ".s0-'('->.s1" + NewLine +
                ".s0-INT->.s4" + NewLine +
                ".s1-{p1}?->:s2=>1" + NewLine +
                ".s1-{p2}?->:s3=>2" + NewLine +
                ".s4-{p1}?->:s2=>1" + NewLine +
                ".s4-{p2}?->:s3=>2" + NewLine;

            DecisionProbe.verbose = true; // make sure we get all error info
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            CodeGenerator generator = new CodeGenerator(newTool(), g, "Java");

            g.CodeGenerator = generator;
            if (g.NumberOfDecisions == 0)
            {
                g.BuildNFA();
                g.CreateLookaheadDFAs(false);
            }

            Assert.AreEqual(0, equeue.size(), "unexpected number of expected problems");
            checkDecision(g, 1, expecting, null, null, null, null, null, 0, false);
        }
        public void TestNoWildcardAsRootError() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);

            string treeGrammar =
                "tree grammar TP;\n" +
                "options {output=AST;}\n" +
                "a : ^(. INT) \n" +
                "  ;\n";

            Grammar   g     = new Grammar(treeGrammar);
            AntlrTool antlr = newTool();

            antlr.SetOutputDirectory(null);   // write to /dev/null
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(1, equeue.errors.Count, "unexpected errors: " + equeue);

            int    expectedMsgID                 = ErrorManager.MSG_WILDCARD_AS_ROOT;
            object expectedArg                   = null;
            RecognitionException expectedExc     = null;
            GrammarSyntaxMessage expectedMessage =
                new GrammarSyntaxMessage(expectedMsgID, g, null, expectedArg, expectedExc);

            checkError(equeue, expectedMessage);
        }
Ejemplo n.º 4
0
        public void TestSetAttrOfExprInMembers() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "@members {\n" +
                "%code.instr = o;" + // must not get null ptr!
                "}\n" +
                "a : ID\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates

            assertNoErrors(equeue);
        }
Ejemplo n.º 5
0
        public void TestMessageStringificationIsConsistent() /*throws Exception*/
        {
            string     action = "$other.tree = null;";
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar a;\n" +
                "options { output = AST;}" +
                "otherrule\n" +
                "    : 'y' ;" +
                "rule\n" +
                "    : other=otherrule {" + action + "}\n" +
                "    ;");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator(generator,
                                                               "rule",
                                                               new CommonToken(ANTLRParser.ACTION, action), 1);
            string rawTranslation =
                translator.Translate();

            int    expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
            object expectedArg   = "other";
            object expectedArg2  = "tree";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2);
            string expectedMessageString = expectedMessage.ToString();

            Assert.AreEqual(expectedMessageString, expectedMessage.ToString());
        }
Ejemplo n.º 6
0
            public override StringTemplate GenExpr(CodeGenerator generator, TemplateGroup templates, DFA dfa)
            {
                StringTemplate result =
                    Operands.Aggregate(default(StringTemplate),
                                       (template, operand) =>
                {
                    if (template == null)
                    {
                        return(operand.GenExpr(generator, templates, dfa));
                    }

                    StringTemplate eST = null;
                    if (templates != null)
                    {
                        eST = templates.GetInstanceOf("andPredicates");
                    }
                    else
                    {
                        eST = new StringTemplate("(<left>&&<right>)");
                    }

                    eST.SetAttribute("left", template);
                    eST.SetAttribute("right", operand.GenExpr(generator, templates, dfa));
                    return(eST);
                });

                return(result);
            }
Ejemplo n.º 7
0
        public void TestMessageStringificationIsConsistent()
        {
            string action = "$other.tree = null;";
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar a;\n" +
                "options { output = AST;}" +
                "otherrule\n" +
                "    : 'y' ;" +
                "rule\n" +
                "    : other=otherrule {" + action + "}\n" +
                "    ;" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator,
                                                                        "rule",
                                                                        new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();

            int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
            object expectedArg = "other";
            object expectedArg2 = "tree";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
            string expectedMessageString = expectedMessage.ToString();
            Assert.AreEqual( expectedMessageString, expectedMessage.ToString() );
        }
Ejemplo n.º 8
0
 public override string GetTargetCharLiteralFromANTLRCharLiteral(
         CodeGenerator generator,
         string literal )
 {
     int c = Grammar.GetCharValueFromGrammarCharLiteral( literal );
     return ( (char)c ).ToString();
 }
Ejemplo n.º 9
0
        public void TestCannotHaveSpaceAfterDot()
        {
            string action = "%x. y = z;";
            //String expecting = null;

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates

            int expectedMsgID = ErrorManager.MSG_INVALID_TEMPLATE_ACTION;
            object expectedArg = "%x.";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
            checkError( equeue, expectedMessage );
        }
Ejemplo n.º 10
0
        public void TestCannotHaveSpaceAfterDot() /*throws Exception*/
        {
            string action = "%x. y = z;";
            //String expecting = null;

            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n");
            AntlrTool     antlr     = newTool();
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates

            int    expectedMsgID = ErrorManager.MSG_INVALID_TEMPLATE_ACTION;
            object expectedArg   = "%x.";
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg);

            checkError(equeue, expectedMessage);
        }
Ejemplo n.º 11
0
 protected StringTemplate chooseWhereCyclicDFAsGo(AntlrTool tool,
                                                  CodeGenerator generator,
                                                  Grammar grammar,
                                                  StringTemplate recognizerST,
                                                  StringTemplate cyclicDFAST)
 {
     return(recognizerST);
 }
Ejemplo n.º 12
0
 protected override void GenRecognizerHeaderFile(AntlrTool tool,
                                                 CodeGenerator generator,
                                                 Grammar grammar,
                                                 StringTemplate headerFileST,
                                                 string extName)
 {
     generator.Write(headerFileST, grammar.name + Grammar.grammarTypeToFileNameSuffix[(int)grammar.type] + extName);
 }
Ejemplo n.º 13
0
        public override string GetTargetCharLiteralFromANTLRCharLiteral(
            CodeGenerator generator,
            string literal)
        {
            int c = Grammar.GetCharValueFromGrammarCharLiteral(literal);

            return(((char)c).ToString());
        }
Ejemplo n.º 14
0
            public override StringTemplate GenExpr(CodeGenerator generator, TemplateGroup templates, DFA dfa)
            {
                if (templates != null)
                {
                    return(templates.GetInstanceOf("false_value"));
                }

                return(new StringTemplate("false"));
            }
Ejemplo n.º 15
0
        public override string GetTargetCharLiteralFromANTLRCharLiteral(CodeGenerator generator, string literal)
        {
            int code_point = 0;

            literal = literal.Substring(1, literal.Length - 2);

            if (literal[0] == '\\')
            {
                switch (literal[1])
                {
                case '\\':
                case '"':
                case '\'':
                    code_point = literal[1];
                    break;

                case 'n':
                    code_point = 10;
                    break;

                case 'r':
                    code_point = 13;
                    break;

                case 't':
                    code_point = 9;
                    break;

                case 'b':
                    code_point = 8;
                    break;

                case 'f':
                    code_point = 12;
                    break;

                case 'u':    // Assume unnnn
                    code_point = int.Parse(literal.Substring(2), NumberStyles.HexNumber);
                    break;

                default:
                    Console.WriteLine("1: hey you didn't account for this: \"" + literal + "\"");
                    break;
                }
            }
            else if (literal.Length == 1)
            {
                code_point = literal[0];
            }
            else
            {
                Console.WriteLine("2: hey you didn't account for this: \"" + literal + "\"");
            }

            return("0x" + code_point.ToString("x"));
        }
Ejemplo n.º 16
0
        protected override void GenRecognizerHeaderFile(AntlrTool tool,
                                                        CodeGenerator generator,
                                                        Grammar grammar,
                                                        StringTemplate headerFileST,
                                                        string extName)
        {
            StringTemplateGroup templates = generator.Templates;

            generator.Write(headerFileST, grammar.name + extName);
        }
Ejemplo n.º 17
0
 /** Convert from an ANTLR string literal found in a grammar file to
 *  an equivalent string literal in the target language.  For Java, this
 *  is the translation 'a\n"' -> "a\n\"".  Expect single quotes
 *  around the incoming literal.  Just flip the quotes and replace
 *  double quotes with \"
 */
 public override string GetTargetStringLiteralFromANTLRStringLiteral( CodeGenerator generator,
                                                            string literal )
 {
     literal = literal.Replace( "\"", "\\\"" );
     StringBuilder buf = new StringBuilder( literal );
     buf[0] = '"';
     buf[literal.Length - 1] = '"';
     buf.Insert( 0, '@' );
     return buf.ToString();
 }
Ejemplo n.º 18
0
        public BuildDependencyGenerator(Tool tool, string grammarFileName)
        {
            this.tool            = tool;
            this.grammarFileName = grammarFileName;
            grammar = tool.GetRootGrammar(grammarFileName);
            string language = (string)grammar.GetOption("language");

            generator = new CodeGenerator(tool, grammar, language);
            generator.LoadTemplates(language);
        }
Ejemplo n.º 19
0
        /** Convert from an ANTLR string literal found in a grammar file to
         *  an equivalent string literal in the target language.  For Java, this
         *  is the identify translation; i.e., "\"\n" -> "\"\n".  Most languages
         *  will be able to use this 1-to-1 mapping.  Expect double quotes
         *  around the incoming literal.
         *  Depending on the charvocabulary the string should be prefixed with a 'L'
         */
        public override string GetTargetStringLiteralFromANTLRStringLiteral(CodeGenerator codegen, string literal)
        {
            StringBuilder buf    = Grammar.GetUnescapedStringFromGrammarStringLiteral(literal);
            string        prefix = "\"";

            if (codegen.grammar.MaxCharValue > 255)
            {
                prefix = "L\"";
            }
            return(prefix + EscapeString(buf.ToString()) + "\"");
        }
Ejemplo n.º 20
0
        /** Convert from an ANTLR string literal found in a grammar file to
         *  an equivalent string literal in the target language.  For Java, this
         *  is the translation 'a\n"' -> "a\n\"".  Expect single quotes
         *  around the incoming literal.  Just flip the quotes and replace
         *  double quotes with \"
         */
        public override string GetTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator,
                                                                            string literal)
        {
            literal = literal.Replace("\"", "\\\"");
            StringBuilder buf = new StringBuilder(literal);

            buf[0] = '"';
            buf[literal.Length - 1] = '"';
            buf.Insert(0, '@');
            return(buf.ToString());
        }
Ejemplo n.º 21
0
        public override string GetTokenTypeAsTargetLabel(CodeGenerator generator, int ttype)
        {
            string name = generator.grammar.GetTokenDisplayName(ttype);

            // If name is a literal, return the token type instead
            if (name[0] == '\'')
            {
                return(generator.grammar.ComputeTokenNameFromLiteral(ttype, name));
            }
            return(name);
        }
Ejemplo n.º 22
0
        protected override void GenRecognizerFile(AntlrTool tool, CodeGenerator generator, Grammar grammar, StringTemplate outputFileST)
        {
            /*
             *  Below is an experimental attempt at providing a few named action blocks
             *  that are printed in both lexer and parser files from combined grammars.
             *  ANTLR appears to first generate a parser, then generate an independent lexer,
             *  and then generate code from that. It keeps the combo/parser grammar object
             *  and the lexer grammar object, as well as their respective code generator and
             *  target instances, completely independent. So, while a bit hack-ish, this is
             *  a solution that should work without having to modify Terrence Parr's
             *  core tool code.
             *
             *  - sharedActionBlocks is a class variable containing a hash map
             *  - if this method is called with a combo grammar, and the action map
             *    in the grammar contains an entry for the named scope "all",
             *    add an entry to sharedActionBlocks mapping the grammar name to
             *    the "all" action map.
             *  - if this method is called with an `implicit lexer'
             *    (one that's extracted from a combo grammar), check to see if
             *    there's an entry in sharedActionBlocks for the lexer's grammar name.
             *  - if there is an action map entry, place it in the lexer's action map
             *  - the recognizerFile template has code to place the
             *    "all" actions appropriately
             *
             *  problems:
             *    - This solution assumes that the parser will be generated
             *      before the lexer. If that changes at some point, this will
             *      not work.
             *    - I have not investigated how this works with delegation yet
             *
             *  Kyle Yetter - March 25, 2010
             */
            if (grammar.type == GrammarType.Combined)
            {
                IDictionary <string, object> all;
                if (grammar.Actions.TryGetValue("all", out all))
                {
                    sharedActionBlocks[grammar.name] = all;
                }
            }
            else if (grammar.implicitLexer)
            {
                IDictionary <string, object> shared;
                if (sharedActionBlocks.TryGetValue(grammar.name, out shared))
                {
                    grammar.Actions["all"] = shared;
                }
            }

            generator.Templates.RegisterRenderer(typeof(string), new RubyRenderer());
            string fileName = generator.GetRecognizerFileName(grammar.name, grammar.type);

            generator.Write(outputFileST, fileName);
        }
Ejemplo n.º 23
0
 /** If we have a label, prefix it with the recognizer's name */
 public override string GetTokenTypeAsTargetLabel( CodeGenerator generator, int ttype )
 {
     string name = generator.grammar.GetTokenDisplayName( ttype );
     // If name is a literal, return the token type instead
     if ( name[0] == '\'' )
     {
         return ttype.ToString();
     }
     return generator.grammar.name + Grammar.grammarTypeToFileNameSuffix[(int)generator.grammar.type] + "_" + name;
     //return super.getTokenTypeAsTargetLabel(generator, ttype);
     //return this.getTokenTextAndTypeAsTargetLabel(generator, null, ttype);
 }
Ejemplo n.º 24
0
        public override string GetTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, string literal)
        {
            literal = literal.Replace("\\\'", "''");   // \' to ' to normalize
            literal = literal.Replace("\\r\\n", "'#13#10'");
            literal = literal.Replace("\\r", "'#13'");
            literal = literal.Replace("\\n", "'#10'");
            StringBuilder buf = new StringBuilder(literal);

            buf[0] = '\'';
            buf[literal.Length - 1] = '\'';
            return(buf.ToString());
        }
Ejemplo n.º 25
0
        /** Character constants get truncated to this value.
         * TODO: This should be derived from the charVocabulary. Depending on it
         * being 255 or 0xFFFF the templates should generate normal character
         * constants or multibyte ones.
         */
        public override int GetMaxCharValue(CodeGenerator codegen)
        {
            int maxval = 255; // codegen.grammar.get????();

            if (maxval <= 255)
            {
                return(255);
            }
            else
            {
                return(maxval);
            }
        }
Ejemplo n.º 26
0
        protected override void GenRecognizerFile(AntlrTool tool,
                                                  CodeGenerator generator,
                                                  Grammar grammar,
                                                  StringTemplate outputFileST)
        {
            // Before we write this, and cause it to generate its string,
            // we need to add all the string literals that we are going to match
            //
            outputFileST.SetAttribute("literals", _strings);
            string fileName = generator.GetRecognizerFileName(grammar.name, grammar.type);

            generator.Write(outputFileST, fileName);
        }
Ejemplo n.º 27
0
        /** 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);
        }
Ejemplo n.º 28
0
        /** If we have a label, prefix it with the recognizer's name */
        public override string GetTokenTypeAsTargetLabel(CodeGenerator generator, int ttype)
        {
            string name = generator.grammar.GetTokenDisplayName(ttype);

            // If name is a literal, return the token type instead
            if (name[0] == '\'')
            {
                return(ttype.ToString());
            }
            return(name);
            //return generator.grammar.name + Grammar.grammarTypeToFileNameSuffix[(int)generator.grammar.type] + "_" + name;
            //return super.getTokenTypeAsTargetLabel(generator, ttype);
            //return this.getTokenTextAndTypeAsTargetLabel(generator, null, ttype);
        }
Ejemplo n.º 29
0
        public override string GetTargetCharLiteralFromANTLRCharLiteral(CodeGenerator generator, string literal)
        {
            StringBuilder buf = new StringBuilder();
            int           c   = Grammar.GetCharValueFromGrammarCharLiteral(literal);

            if (c < Label.MIN_CHAR_VALUE)
            {
                return("0");
            }
            // normal char
            buf.Append(c);

            return(buf.ToString());
        }
Ejemplo n.º 30
0
        protected override void PerformGrammarAnalysis(CodeGenerator generator, Grammar grammar)
        {
            base.PerformGrammarAnalysis(generator, grammar);

            foreach (Rule rule in grammar.Rules)
                rule.ThrowsSpec.Add("RecognitionException");

            IEnumerable<Rule> delegatedRules = grammar.GetDelegatedRules();
            if (delegatedRules != null)
            {
                foreach (Rule rule in delegatedRules)
                    rule.ThrowsSpec.Add("RecognitionException");
            }
        }
Ejemplo n.º 31
0
        /** Convert from an ANTLR char literal found in a grammar file to
         *  an equivalent char literal in the target language.  For Java, this
         *  is the identify translation; i.e., '\n' -> '\n'.  Most languages
         *  will be able to use this 1-to-1 mapping.  Expect single quotes
         *  around the incoming literal.
         *  Depending on the charvocabulary the charliteral should be prefixed with a 'L'
         */
        public override string GetTargetCharLiteralFromANTLRCharLiteral(CodeGenerator codegen, string literal)
        {
            int    c      = Grammar.GetCharValueFromGrammarCharLiteral(literal);
            string prefix = "'";

            if (codegen.grammar.MaxCharValue > 255)
            {
                prefix = "L'";
            }
            else if ((c & 0x80) != 0)           // if in char mode prevent sign extensions
            {
                return("" + c);
            }
            return(prefix + EscapeChar(c) + "'");
        }
Ejemplo n.º 32
0
            public override StringTemplate GenExpr(CodeGenerator generator, TemplateGroup templates, DFA dfa)
            {
                StringTemplate eST = null;

                if (templates != null)
                {
                    eST = templates.GetInstanceOf("notPredicate");
                }
                else
                {
                    eST = new StringTemplate("!(<pred>)");
                }

                eST.SetAttribute("pred", ctx.GenExpr(generator, templates, dfa));
                return(eST);
            }
Ejemplo n.º 33
0
        protected override void GenRecognizerHeaderFile(AntlrTool tool,
                                                        CodeGenerator generator,
                                                        Grammar grammar,
                                                        StringTemplate headerFileST,
                                                        string extName)
        {
            // Pick up the file name we are generating. This method will return a
            // a file suffixed with .c, so we must substring and add the extName
            // to it as we cannot assign into strings in Java.
            //
            string fileName = generator.GetRecognizerFileName(grammar.name, grammar.type);

            fileName = fileName.Substring(0, fileName.Length - 2) + extName;

            generator.Write(headerFileST, fileName);
        }
Ejemplo n.º 34
0
        public override string GetTargetCharLiteralFromANTLRCharLiteral(CodeGenerator generator,
                                                                        string literal)
        {
            StringBuilder buf = new StringBuilder(10);

            int c = Grammar.GetCharValueFromGrammarCharLiteral(literal);

            if (c < Label.MIN_CHAR_VALUE)
            {
                buf.Append("\\x{0000}");
            }
            else if (c < targetCharValueEscape.Length &&
                     targetCharValueEscape[c] != null)
            {
                buf.Append(targetCharValueEscape[c]);
            }
            else if ((c < 0x7F) && !char.IsControl((char)c))
            {
                // normal char
                buf.Append((char)c);
            }
            else
            {
                // must be something unprintable...use \\uXXXX
                // turn on the bit above max "\\uFFFF" value so that we pad with zeros
                // then only take last 4 digits
                string hex = c.ToString("X4");
                buf.Append("\\x{");
                buf.Append(hex);
                buf.Append("}");
            }

            if (buf.ToString().IndexOf('\\') == -1)
            {
                // no need for interpolation, use single quotes
                buf.Insert(0, '\'');
                buf.Append('\'');
            }
            else
            {
                // need string interpolation
                buf.Insert(0, '\"');
                buf.Append('\"');
            }

            return(buf.ToString());
        }
Ejemplo n.º 35
0
            public override StringTemplate GenExpr(CodeGenerator generator,
                                                   StringTemplateGroup templates,
                                                   DFA dfa)
            {
                StringTemplate eST = null;

                if (templates != null)
                {
                    if (_synpred)
                    {
                        eST = templates.GetInstanceOf("evalSynPredicate");
                    }
                    else
                    {
                        eST = templates.GetInstanceOf("evalPredicate");
                        generator.grammar.decisionsWhoseDFAsUsesSemPreds.Add(dfa);
                    }
                    string predEnclosingRuleName = predicateAST.enclosingRuleName;

                    /*
                     * String decisionEnclosingRuleName =
                     *  dfa.getNFADecisionStartState().getEnclosingRule();
                     * // if these rulenames are diff, then pred was hoisted out of rule
                     * // Currently I don't warn you about this as it could be annoying.
                     * // I do the translation anyway.
                     */
                    //eST.setAttribute("pred", this.toString());
                    if (generator != null)
                    {
                        eST.SetAttribute("pred",
                                         generator.TranslateAction(predEnclosingRuleName, predicateAST));
                    }
                }
                else
                {
                    eST = new StringTemplate("$pred$");
                    eST.SetAttribute("pred", this.ToString());
                    return(eST);
                }
                if (generator != null)
                {
                    string description =
                        generator.target.GetTargetStringLiteralFromString(this.ToString());
                    eST.SetAttribute("description", description);
                }
                return(eST);
            }
Ejemplo n.º 36
0
        public override string GetTargetCharLiteralFromANTLRCharLiteral( CodeGenerator generator,
                                                                string literal )
        {
            StringBuilder buf = new StringBuilder( 10 );

            int c = Grammar.GetCharValueFromGrammarCharLiteral( literal );
            if ( c < Label.MIN_CHAR_VALUE )
            {
                buf.Append( "\\x{0000}" );
            }
            else if ( c < targetCharValueEscape.Length &&
                    targetCharValueEscape[c] != null )
            {
                buf.Append( targetCharValueEscape[c] );
            }
            else if ( ( c < 0x7F ) && !char.IsControl( (char)c ) )
            {
                // normal char
                buf.Append( (char)c );
            }
            else
            {
                // must be something unprintable...use \\uXXXX
                // turn on the bit above max "\\uFFFF" value so that we pad with zeros
                // then only take last 4 digits
                string hex = c.ToString( "X4" );
                buf.Append( "\\x{" );
                buf.Append( hex );
                buf.Append( "}" );
            }

            if ( buf.ToString().IndexOf( '\\' ) == -1 )
            {
                // no need for interpolation, use single quotes
                buf.Insert( 0, '\'' );
                buf.Append( '\'' );
            }
            else
            {
                // need string interpolation
                buf.Insert( 0, '\"' );
                buf.Append( '\"' );
            }

            return buf.ToString();
        }
Ejemplo n.º 37
0
        protected void checkDecision(Grammar g,
                                     int decision,
                                     string expecting,
                                     int[] expectingUnreachableAlts)
        //throws Exception
        {
            Antlr3.AntlrTool tool = new Antlr3.AntlrTool();
            // mimic actions of org.antlr.Tool first time for grammar g
            if (g.CodeGenerator == null)
            {
                CodeGenerator generator = new CodeGenerator(tool, g, "Java");
                g.CodeGenerator = generator;
                g.BuildNFA();
                g.CreateLookaheadDFAs(false);
            }

            DFA dfa = g.GetLookaheadDFA(decision);

            assertNotNull("unknown decision #" + decision, dfa);
            FASerializer serializer = new FASerializer(g);
            string       result     = serializer.Serialize(dfa.startState);
            //System.out.print(result);
            var nonDetAlts = dfa.UnreachableAlts;

            //System.out.println("alts w/o predict state="+nonDetAlts);

            // first make sure nondeterministic alts are as expected
            if (expectingUnreachableAlts == null)
            {
                if (nonDetAlts != null && nonDetAlts.Count != 0)
                {
                    Console.Error.WriteLine("nondeterministic alts (should be empty): " + ((IList)nonDetAlts).ToElementString());
                }
                assertEquals("unreachable alts mismatch", 0, nonDetAlts != null ? nonDetAlts.Count : 0);
            }
            else
            {
                for (int i = 0; i < expectingUnreachableAlts.Length; i++)
                {
                    assertTrue("unreachable alts mismatch",
                               nonDetAlts != null ? nonDetAlts.Contains(expectingUnreachableAlts[i]) : false);
                }
            }
            assertEquals(expecting, result);
        }
Ejemplo n.º 38
0
        public override string GetTargetCharLiteralFromANTLRCharLiteral( CodeGenerator generator,
                                                               string literal )
        {
            if ( literal.StartsWith( "'\\u" ) )
            {
                literal = "0x" + literal.Substring( 3, 4 );
            }
            else
            {
                int c = literal[1]; // TJP
                if ( c < 32 || c > 127 )
                {
                    literal = "0x" + c.ToString( "x" );
                }
            }

            return literal;
        }
Ejemplo n.º 39
0
        /** Target must be able to override the labels used for token types */
        public override string GetTokenTypeAsTargetLabel( CodeGenerator generator,
                            int ttype )
        {
            // use ints for predefined types;
            // <invalid> <EOR> <DOWN> <UP>
            if ( ttype >= 0 && ttype <= 3 )
            {
                return ttype.ToString();
            }

            string name = generator.grammar.GetTokenDisplayName( ttype );

            // If name is a literal, return the token type instead
            if ( name[0] == '\'' )
            {
                return ttype.ToString();
            }

            return name;
        }
Ejemplo n.º 40
0
 public void TestEscaped_InAction()
 {
     string action = "int \\$n; \"\\$in string\\$\"";
     string expecting = "int $n; \"$in string$\"";
     Grammar g = new Grammar(
         "parser grammar t;\n" +
         "@members {" + action + "}\n" +
         "a[User u, int i]\n" +
         "        : {" + action + "}\n" +
         "        ;" );
     AntlrTool antlr = newTool();
     CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
     g.CodeGenerator = generator;
     generator.GenRecognizer(); // forces load of templates
     ActionTranslator translator =
         new ActionTranslator( generator,
                                   "a",
                                   new CommonToken( ANTLRParser.ACTION, action ), 0 );
     string found = translator.Translate();
     Assert.AreEqual( expecting, found );
 }
Ejemplo n.º 41
0
 public void TestEscapedLessThanInAction()
 {
     Grammar g = new Grammar();
     AntlrTool antlr = newTool();
     CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
     string action = "i<3; '<xmltag>'";
     ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                  new CommonToken( ANTLRParser.ACTION, action ), 0 );
     string expecting = action;
     string rawTranslation =
         translator.Translate();
     StringTemplateGroup templates =
         new StringTemplateGroup();
     StringTemplate actionST = new StringTemplate( templates, "<action>" );
     actionST.SetAttribute( "action", rawTranslation );
     string found = actionST.Render();
     Assert.AreEqual( expecting, found );
 }
Ejemplo n.º 42
0
        public virtual void CreateStateTables( CodeGenerator generator )
        {
            //[email protected]("createTables:\n"+this);
            this._generator = generator;
            Description = NFADecisionStartState.Description;
            Description = generator.Target.GetTargetStringLiteralFromString( Description );

            // create all the tables
            //special = new List<int>( this.NumberOfStates ); // Vector<short>
            //special.setSize( this.NumberOfStates );
            _special = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
            _specialStates = new List<DFAState>();
            _specialStateSTs = new List<StringTemplate>();
            //accept = new List<int>( this.NumberOfStates ); // Vector<int>
            //accept.setSize( this.NumberOfStates );
            _accept = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
            //eot = new List<int>( this.NumberOfStates ); // Vector<int>
            //eot.setSize( this.NumberOfStates );
            _eot = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
            //eof = new List<int>( this.NumberOfStates ); // Vector<int>
            //eof.setSize( this.NumberOfStates );
            _eof = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
            //min = new List<int>( this.NumberOfStates ); // Vector<int>
            //min.setSize( this.NumberOfStates );
            _min = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
            //max = new List<int>( this.NumberOfStates ); // Vector<int>
            //max.setSize( this.NumberOfStates );
            _max = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
            _transition = new int[NumberOfStates][]; // Vector<Vector<int>>
            //transition.setSize( this.NumberOfStates );
            _transitionEdgeTables = new List<int?>( this.NumberOfStates ); // Vector<Vector<int>>
            _transitionEdgeTables.Resize( this.NumberOfStates );

            // for each state in the DFA, fill relevant tables.
            IEnumerable<DFAState> it = null;
            if ( UserMaxLookahead > 0 )
            {
                it = _states;
            }
            else
            {
                it = UniqueStates.Values;
            }

            foreach ( DFAState s in it )
            {
                if ( s == null )
                {
                    // ignore null states; some acylic DFA see this condition
                    // when inlining DFA (due to lacking of exit branch pruning?)
                    continue;
                }

                if ( s.IsAcceptState )
                {
                    // can't compute min,max,special,transition on accepts
                    _accept[s.StateNumber] = s.GetUniquelyPredictedAlt();
                }
                else
                {
                    CreateMinMaxTables( s );
                    CreateTransitionTableEntryForState( s );
                    CreateSpecialTable( s );
                    CreateEOTAndEOFTables( s );
                }
            }

            // now that we have computed list of specialStates, gen code for 'em
            for ( int i = 0; i < _specialStates.Count; i++ )
            {
                DFAState ss = _specialStates[i];
                StringTemplate stateST = generator.GenerateSpecialState( ss );
                _specialStateSTs.Add( stateST );
            }

            // check that the tables are not messed up by encode/decode
            /*
            testEncodeDecode(min);
            testEncodeDecode(max);
            testEncodeDecode(accept);
            testEncodeDecode(special);
            [email protected]("min="+min);
            [email protected]("max="+max);
            [email protected]("eot="+eot);
            [email protected]("eof="+eof);
            [email protected]("accept="+accept);
            [email protected]("special="+special);
            [email protected]("transition="+transition);
            */
        }
Ejemplo n.º 43
0
        public void TestForwardRefRuleLabels()
        {
            string action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.tree;";
            string expecting = "(r!=null?r.x:0); (r!=null?((Token)r.start):null); (r!=null?((Token)r.stop):null); (r!=null?((Object)r.tree):null); (r!=null?r.x:0); (r!=null?((Object)r.tree):null);";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "parser grammar t;\n" +
                "b : r=a {###" + action + "!!!}\n" +
                "  ;\n" +
                "a returns [int x]\n" +
                "  : ;\n" );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // codegen phase sets some vars we need

            StringTemplate codeST = generator.RecognizerST;
            string code = codeST.Render();
            int startIndex = code.IndexOf("###") + 3;
            int endIndex = code.IndexOf("!!!");
            string found = code.Substring(startIndex, endIndex - startIndex);
            Assert.AreEqual( expecting, found );

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Ejemplo n.º 44
0
        public void TestNoWildcardAsRootError()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            string treeGrammar =
                "tree grammar TP;\n" +
                "options {output=AST;}\n" +
                "a : ^(. INT) \n" +
                "  ;\n";

            Grammar g = new Grammar( treeGrammar );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            assertEquals( "unexpected errors: " + equeue, 1, equeue.errors.Count );

            int expectedMsgID = ErrorManager.MSG_WILDCARD_AS_ROOT;
            object expectedArg = null;
            RecognitionException expectedExc = null;
            GrammarSyntaxMessage expectedMessage =
                new GrammarSyntaxMessage( expectedMsgID, g, null, expectedArg, expectedExc );

            checkError( equeue, expectedMessage );
        }
Ejemplo n.º 45
0
        public void TestKnownRuleButNotInLHS()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            string grammar =
                "grammar T;\n" +
                "options {output=AST;}\n" +
                "a : INT -> b ;\n" +
                "b : 'b' ;\n" +
                "ID : 'a'..'z'+ ;\n" +
                "INT : '0'..'9'+;\n" +
                "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";

            Grammar g = new Grammar( grammar );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            int expectedMsgID = ErrorManager.MSG_REWRITE_ELEMENT_NOT_PRESENT_ON_LHS;
            object expectedArg = "b";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );

            checkError( equeue, expectedMessage );
        }
Ejemplo n.º 46
0
 /** 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 &&
          char.IsUpper( refdSymbol[0] ) )
     {
         // symbol is a token
         IList tokenRefs = GetTokenRefsInAlt( refdSymbol, outerAltNum );
         uniqueRefAST = (GrammarAST)tokenRefs[0];
     }
     else
     {
         // symbol is a rule
         IList ruleRefs = GetRuleRefsInAlt( refdSymbol, outerAltNum );
         uniqueRefAST = (GrammarAST)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 &&
              char.IsUpper( refdSymbol[0] ) )
         {
             grammar.DefineTokenRefLabel( Name, label, uniqueRefAST );
         }
         else
         {
             grammar.DefineRuleRefLabel( Name, label, uniqueRefAST );
         }
         uniqueRefAST.code.SetAttribute( "label", labelName );
     }
     return labelName;
 }
Ejemplo n.º 47
0
        //throws Exception
        protected void checkDecision( Grammar g,
                                     int decision,
                                     string expecting,
                                     int[] expectingUnreachableAlts )
        {
            Antlr3.AntlrTool tool = new Antlr3.AntlrTool();
            // mimic actions of org.antlr.Tool first time for grammar g
            if ( g.CodeGenerator == null )
            {
                CodeGenerator generator = new CodeGenerator( tool, g, "Java" );
                g.CodeGenerator = generator;
                g.BuildNFA();
                g.CreateLookaheadDFAs( false );
            }

            DFA dfa = g.GetLookaheadDFA( decision );
            Assert.IsNotNull(dfa, "unknown decision #" + decision);
            FASerializer serializer = new FASerializer( g );
            string result = serializer.Serialize( dfa.StartState );
            //System.out.print(result);
            var nonDetAlts = dfa.UnreachableAlts;
            //System.out.println("alts w/o predict state="+nonDetAlts);

            // first make sure nondeterministic alts are as expected
            if ( expectingUnreachableAlts == null )
            {
                if ( nonDetAlts != null && nonDetAlts.Count != 0 )
                {
                    Console.Error.WriteLine( "nondeterministic alts (should be empty): " + ( (IList)nonDetAlts ).ToElementString() );
                }
                Assert.AreEqual(0, nonDetAlts != null ? nonDetAlts.Count : 0, "unreachable alts mismatch");
            }
            else
            {
                for ( int i = 0; i < expectingUnreachableAlts.Length; i++ )
                {
                    Assert.IsTrue(nonDetAlts != null ? nonDetAlts.Contains(expectingUnreachableAlts[i]) : false, "unreachable alts mismatch");
                }
            }
            Assert.AreEqual( expecting, result );
        }
Ejemplo n.º 48
0
        public void TestDynamicScopeRefOkEvenThoughRuleRefExists()
        {
            string action = "$b::n;";
            string expecting = "((b_scope)b_stack.peek()).n;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "s : b ;\n" +
                "b\n" +
                "scope {\n" +
                "  int n;\n" +
                "} : '(' b ')' {" + action + "}\n" + // refers to current invocation's n
                "  ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "b",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Ejemplo n.º 49
0
        protected override void GenRecognizerFile(AntlrTool tool, CodeGenerator generator, Grammar grammar, Template outputFileST)
        {
            if (!grammar.IsRoot)
            {
                Grammar rootGrammar = grammar.composite.RootGrammar;
                string actionScope = grammar.GetDefaultActionScope(grammar.type);
                IDictionary<string, object> actions;
                object rootNamespace;
                if (rootGrammar.Actions.TryGetValue(actionScope, out actions) && actions.TryGetValue("namespace", out rootNamespace))
                {
                    if (!grammar.Actions.TryGetValue(actionScope, out actions))
                    {
                        actions = new Dictionary<string, object>();
                        grammar.Actions[actionScope] = actions;
                    }

                    actions["namespace"] = rootNamespace;
                }
            }

            generator.Templates.RegisterRenderer(typeof(string), new StringRenderer(generator, this));
            base.GenRecognizerFile(tool, generator, grammar, outputFileST);
        }
Ejemplo n.º 50
0
        public void TestUnknownToken()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );

            string grammar =
                "grammar T;\n" +
                "options {output=AST;}\n" +
                "a : INT -> ICK ;\n" +
                "ID : 'a'..'z'+ ;\n" +
                "INT : '0'..'9'+;\n" +
                "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";

            Grammar g = new Grammar( grammar );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            int expectedMsgID = ErrorManager.MSG_UNDEFINED_TOKEN_REF_IN_REWRITE;
            object expectedArg = "ICK";
            object expectedArg2 = null;
            GrammarSemanticsMessage expectedMessage =
                new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );

            checkError( equeue, expectedMessage );
        }
Ejemplo n.º 51
0
        public void TestRewriteRuleAndRewriteModeRefRule()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a  : b+ -> {ick}\n" +
                "   | b b A -> {ick}\n" +
                "   ;\n" +
                "b  : B ;\n"
            );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
Ejemplo n.º 52
0
        public void TestRewriteRuleAndRewriteModeIgnoreActionsPredicates()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a: {action} {action2} x=A -> {ick}\n" +
                " | {pred1}? y+=B -> {ick}\n" +
                " | C {action} -> {ick}\n" +
                " | {pred2}?=> z+=D -> {ick}\n" +
                " | (E)=> ^(F G) -> {ick}\n" +
                " ;\n"
            );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
Ejemplo n.º 53
0
        public void TestDynamicRuleScopeRefInSubrule()
        {
            string action = "$a::n;";
            string expecting = "((a_scope)a_stack.peek()).n;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a\n" +
                "scope {\n" +
                "  float n;\n" +
                "} : b ;\n" +
                "b : {" + action + "}\n" +
                "  ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "b",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Ejemplo n.º 54
0
        public void TestSetAttrOfExpr()
        {
            string action = "%{foo($ID.text).getST()}.y = z;";
            string expecting = "(foo((ID1!=null?ID1.getText():null)).getST()).setAttribute(\"y\", z);";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator,
                                                                         "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.Render();

            assertNoErrors( equeue );

            Assert.AreEqual( expecting, found );
        }
Ejemplo n.º 55
0
 public BuildDependencyGenerator( Tool tool, string grammarFileName )
 {
     this.tool = tool;
     this.grammarFileName = grammarFileName;
     grammar = tool.GetRootGrammar( grammarFileName );
     string language = (string)grammar.GetOption( "language" );
     generator = new CodeGenerator( tool, grammar, language );
     generator.LoadTemplates( language );
 }
Ejemplo n.º 56
0
        public void TestSetAttrOfExprInMembers()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "@members {\n" +
                "%code.instr = o;" + // must not get null ptr!
                "}\n" +
                "a : ID\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates

            assertNoErrors( equeue );
        }
Ejemplo n.º 57
0
        public void TestTemplateConstructorNoArgs()
        {
            string action = "x = %foo();";
            string expecting = "x = templateLib.getInstanceOf(\"foo\");";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {\n" +
                "    output=template;\n" +
                "}\n" +
                "\n" +
                "a : ID {" + action + "}\n" +
                "  ;\n" +
                "\n" +
                "ID : 'a';\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator =
                new ActionTranslator( generator,
                                            "a",
                                            new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string rawTranslation =
                translator.Translate();
            StringTemplateGroup templates =
                new StringTemplateGroup();
            StringTemplate actionST = new StringTemplate( templates, rawTranslation );
            string found = actionST.Render();

            assertNoErrors( equeue );

            Assert.AreEqual( expecting, found );
        }
Ejemplo n.º 58
0
        public void TestFullyQualifiedRefToCurrentRuleParameter()
        {
            string action = "$a.i;";
            string expecting = "i;";

            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "grammar t;\n" +
                "a[int i]: {" + action + "}\n" +
                "  ;\n" );
            AntlrTool antlr = newTool();
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer(); // forces load of templates
            ActionTranslator translator = new ActionTranslator( generator, "a",
                                                                         new CommonToken( ANTLRParser.ACTION, action ), 1 );
            string found = translator.Translate();
            Assert.AreEqual(expecting, found);

            Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
        }
Ejemplo n.º 59
0
 public StringRenderer(CodeGenerator generator, CSharp2Target target)
 {
     _generator = generator;
     _target = target;
 }
Ejemplo n.º 60
0
        public void TestWeirdRuleRef()
        {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            string grammar =
                "grammar T;\n" +
                "options {output=AST;}\n" +
                "a : ID a -> $a | INT ;\n" +
                "ID : 'a'..'z'+ ;\n" +
                "INT: '0'..'9'+ ;\n" +
                "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";

            Grammar g = new Grammar( grammar );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            // $a is ambig; is it previous root or ref to a ref in alt?
            Assert.AreEqual(1, equeue.errors.Count, "unexpected errors: " + equeue);
        }