Пример #1
0
        //Adds a single context to the grammar string.
        private void AddContext(IroContext ctx, IroPrecompileData data, ref PyStringMaker text)
        {
            //Open the context.
            text.AppendLine("'" + ctx.Name + "' : [");
            text.TabIn();

            //Add context members.
            foreach (var member in ctx.Members)
            {
                AddContextMember(ctx.Name, member, data, ref text);
            }
            text.TrimEnd(',', ' ');

            text.TabOut();
            text.AppendLine("],");
        }
Пример #2
0
        //Adds a queued context to the text.
        private void AddQueuedContext(string name, string originalCtx, InlinePushContextMember ilp, IroPrecompileData data, ref PyStringMaker text)
        {
            //Open the context.
            text.AppendLine("'" + name + "' : [");
            text.TabIn();

            //Add the pop rule.
            var popStyles = GetPatternStyles(ilp.PopStyles, data);

            text.AppendLine("(u'" + ilp.PopData.Replace("\\", "\\\\") + "', byGroups(" + string.Join(", ", popStyles.Select(x => x.PygmentsScope)).TrimEnd(',', ' ') + "), '" + originalCtx + "')");

            //Add context members.
            includedThisRun = new List <string>();
            foreach (var member in ilp.Patterns)
            {
                AddContextMember(name, member, data, ref text);
            }
            text.TrimEnd(',', ' ');

            text.TabOut();
            text.AppendLine("],");
        }
Пример #3
0
        /// <summary>
        /// Compiles pre-compile data into a Pygments grammar file.
        /// </summary>
        public CompileResult Compile(IroPrecompileData data)
        {
            var text = new PyStringMaker();

            //Header for imports and class naming.
            text.AppendLine("from pygments.lexer import RegexLexer, bygroups");
            text.AppendLine("from pygments.token import *");
            text.AppendLine("import re");
            text.AppendLine("__all__ =['" + data.Name + "Lexer']");
            text.AppendLine();
            text.AppendLine("class " + data.Name + "Lexer(RegexLexer):");
            text.TabIn();

            //Name with capital first letter, aliases.
            text.AppendLine("name = '" + data.Name[0].ToString().ToUpper() + data.Name.Substring(1) + "'");
            text.AppendLine("aliases = ['" + data.Name + "']");

            //File names.
            string fNames = "[";

            foreach (var ext in data.FileExtensions)
            {
                fNames += "'" + ext + "', ";
            }
            fNames = fNames.TrimEnd(',', ' ') + "]";
            text.AppendLine(fNames);

            //Template flags.
            text.AppendLine("flags = re.MULTILINE | re.UNICODE");
            text.AppendLine();

            //Open the tokens map.
            text.AppendLine("tokens = {");
            text.TabIn();

            //Get the root context and evaluate.
            var rootCtx = data.Contexts.FirstOrDefault(x => x.Name == "main");

            if (rootCtx == null)
            {
                Error.Compile("No 'main' context exists to create the root grammar state from.");
                return(null);
            }
            AddContext(rootCtx, data, ref text);
            includedThisRun = new List <string>();

            //Evaluate the rest of the contexts.
            foreach (var context in data.Contexts)
            {
                //Ignore main, already done.
                if (context.Name == "main")
                {
                    continue;
                }

                //Add, reset includes.
                AddContext(context, data, ref text);
                includedThisRun = new List <string>();
            }

            //Add queued contexts.
            for (int i = 0; i < queuedContexts.Count; i++)
            {
                var queued = queuedContexts.ElementAt(i);
                AddQueuedContext(queued.Key.Item2, queued.Key.Item1, queued.Value, data, ref text);
            }

            text.TabOut();
            text.AppendLine("}");

            return(new CompileResult()
            {
                GeneratedFile = text.ToString(),
                Target = Target.Pygments
            });
        }