示例#1
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);
        }
示例#2
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);
        }
示例#3
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);
        }
示例#4
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);
        }
        /** From T.g return a list of File objects that
         *  name files ANTLR will emit from T.g.
         */
        public virtual IList <string> GetGeneratedFileList()
        {
            List <FileInfo> files = new List <FileInfo>();

            System.IO.DirectoryInfo outputDir = tool.GetOutputDirectory(grammarFileName);
            if (outputDir.Name.Equals("."))
            {
                outputDir = outputDir.Parent;
            }
            else if (outputDir.Name.IndexOf(' ') >= 0)
            { // has spaces?
                string escSpaces = outputDir.ToString().Replace(
                    " ",
                    "\\ ");
                outputDir = new System.IO.DirectoryInfo(escSpaces);
            }
            // add generated recognizer; e.g., TParser.java
            string recognizer =
                generator.GetRecognizerFileName(grammar.name, grammar.type);

            files.Add(new FileInfo(System.IO.Path.Combine(outputDir.FullName, recognizer)));
            // add output vocab file; e.g., T.tokens. This is always generated to
            // the base output directory, which will be just . if there is no -o option
            //
            files.Add(new FileInfo(System.IO.Path.Combine(tool.OutputDirectory, generator.VocabFileName)));
            // are we generating a .h file?
            StringTemplate headerExtST = null;
            StringTemplate extST       = generator.Templates.GetInstanceOf("codeFileExtension");

            if (generator.Templates.IsDefined("headerFile"))
            {
                headerExtST = generator.Templates.GetInstanceOf("headerFileExtension");
                string suffix   = Grammar.grammarTypeToFileNameSuffix[(int)grammar.type];
                string fileName = grammar.name + suffix + headerExtST.Render();
                files.Add(new FileInfo(System.IO.Path.Combine(outputDir.FullName, fileName)));
            }
            if (grammar.type == GrammarType.Combined)
            {
                // add autogenerated lexer; e.g., TLexer.java TLexer.h TLexer.tokens
                // don't add T__.g (just a temp file)
                string suffix = Grammar.grammarTypeToFileNameSuffix[(int)GrammarType.Lexer];
                string lexer  = grammar.name + suffix + extST.Render();
                files.Add(new FileInfo(System.IO.Path.Combine(outputDir.FullName, lexer)));

                // TLexer.h
                if (headerExtST != null)
                {
                    string header = grammar.name + suffix + headerExtST.Render();
                    files.Add(new FileInfo(System.IO.Path.Combine(outputDir.FullName, header)));
                }
                // for combined, don't generate TLexer.tokens
            }

            // handle generated files for imported grammars
            IList <Grammar> imports =
                grammar.composite.GetDelegates(grammar.composite.RootGrammar);

            foreach (Grammar g in imports)
            {
                outputDir = tool.GetOutputDirectory(g.FileName);
                string fname = GroomQualifiedFileName(outputDir.ToString(), g.GetRecognizerName() + extST.Render());
                files.Add(new FileInfo(fname));
            }

            if (files.Count == 0)
            {
                return(null);
            }

            return(files.Select(info => info.FullName).ToArray());
        }
示例#6
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);
        }
示例#7
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);
        }
示例#8
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);
 }
示例#9
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 );
        }
示例#10
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);
 }