Exemple #1
0
 public override string GetTargetCharLiteralFromANTLRCharLiteral(
         CodeGenerator generator,
         string literal )
 {
     int c = Grammar.GetCharValueFromGrammarCharLiteral( literal );
     return ( (char)c ).ToString();
 }
Exemple #2
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 );
        }
Exemple #3
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() );
        }
Exemple #4
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();
 }
Exemple #5
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);
 }
Exemple #6
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");
            }
        }
Exemple #7
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();
        }
Exemple #8
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;
        }
Exemple #9
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;
        }
Exemple #10
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 );
        }
Exemple #11
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 );
        }
        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);
        }
Exemple #13
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 );
 }
Exemple #14
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);
        }
        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);
        }
Exemple #16
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);
            */
        }
        //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 );
        }
Exemple #18
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 );
        }
Exemple #19
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);
        }
Exemple #20
0
 public StringRenderer(CodeGenerator generator, CSharp2Target target)
 {
     _generator = generator;
     _target = target;
 }
Exemple #21
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);
        }
Exemple #22
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);
        }
Exemple #23
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 );
        }
Exemple #24
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);
        }
        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 );
        }
Exemple #26
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 );
 }
 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 );
 }
Exemple #28
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;
 }
Exemple #29
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 );
        }
Exemple #30
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);
        }