Exemple #1
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);
 }
Exemple #2
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);
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
0
        protected override void GenRecognizerHeaderFile(AntlrTool tool,
                                                        CodeGenerator generator,
                                                        Grammar grammar,
                                                        StringTemplate headerFileST,
                                                        string extName)
        {
            //Its better we remove the EOF Token, as it would have been defined everywhere in C.
            //we define it later as "EOF_TOKEN" instead of "EOF"
            IList <object> tokens = (IList <object>)headerFileST.GetAttribute("tokens");

            for (int i = 0; i < tokens.Count; ++i)
            {
                bool   can_break = false;
                object tok       = tokens[i];
                if (tok is Aggregate)
                {
                    Aggregate atok = (Aggregate)tok;
                    foreach (var pair in atok.Properties)
                    {
                        if (pair.Value.Equals("EOF"))
                        {
                            tokens.RemoveAt(i);
                            can_break = true;
                            break;
                        }
                    }
                }

                if (can_break)
                {
                    break;
                }
            }

            // 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 - 4) + extName;

            generator.Write(headerFileST, fileName);
        }
Exemple #7
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 );
 }
Exemple #8
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 );
 }
Exemple #9
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);
        }
Exemple #10
0
        protected override void GenRecognizerHeaderFile(AntlrTool tool,
                CodeGenerator generator,
                Grammar grammar,
                StringTemplate headerFileST,
                string extName)
        {
            //Its better we remove the EOF Token, as it would have been defined everywhere in C.
            //we define it later as "EOF_TOKEN" instead of "EOF"
            IList<object> tokens = (IList<object>)headerFileST.GetAttribute("tokens");
            for (int i = 0; i < tokens.Count; ++i)
            {
                bool can_break = false;
                object tok = tokens[i];
                if (tok is Aggregate)
                {
                    Aggregate atok = (Aggregate)tok;
                    foreach (var pair in atok.Properties)
                    {
                        if (pair.Value.Equals("EOF"))
                        {
                            tokens.RemoveAt(i);
                            can_break = true;
                            break;
                        }
                    }
                }

                if (can_break)
                    break;
            }

            // 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 - 4) + extName;

            generator.Write(headerFileST, fileName);
        }
Exemple #11
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.Add("literals", strings);
     string fileName = generator.GetRecognizerFileName(grammar.name, grammar.type);
     generator.Write(outputFileST, fileName);
 }
Exemple #12
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 );
        }
Exemple #13
0
 protected override void GenRecognizerFile(AntlrTool tool, CodeGenerator generator, Grammar grammar, StringTemplate outputFileST)
 {
     generator.Templates.RegisterRenderer(typeof(string), new RubyRenderer());
     string fileName = generator.GetRecognizerFileName(grammar.name, grammar.type);
     generator.Write(outputFileST, fileName);
 }