Example #1
0
        public IEnumerable<TokenType> GetColorTypes(string text, Lexer lexer)
        {
            int position = 0;

            foreach (var token in lexer.GetTokens(text))
            {
                while (position < token.Start)
                {
                    yield return TokenType.Space;
                    position++;
                }

                while (position < token.Start + token.Length)
                {
                    yield return token.Type;
                    position++;
                }
            }

            int length = text.Length;

            while (position < length)
            {
                yield return TokenType.Space;
                position++;
            }
        }
Example #2
0
        public static void ProcessFile(string filename)
        {
            string text = File.ReadAllText(filename);
            Lexer lexer = new Lexer();

            if (languages.Count > 0)
            {
                foreach (var lang in languages)
                {
                    if (IsFilename(lang))
                        lexer.ConfigureFromFile(lang);
                    else
                        lexer.Configure(lang);
                }
            }
            else if (filename.EndsWith(".rb"))
                lexer.Configure("Ruby");
            else if (filename.EndsWith(".cs"))
                lexer.Configure("CSharp");
            else if (filename.EndsWith(".js"))
                lexer.Configure("Javascript");
            else if (filename.EndsWith(".py"))
                lexer.Configure("Python");
            else if (filename.EndsWith(".cob"))
                lexer.Configure("Cobol");
            else if (filename.EndsWith(".ms"))
                lexer.Configure("Mass");

            if (styles.Count == 0)
            {
                WriteToConsole(text, lexer);
                return;
            }

            TextRenderer renderer = new TextRenderer(lexer);

            foreach (var t in styles)
            {
                if (IsFilename(t))
                    renderer.ConfigureFromFile(t);
                else
                    renderer.Configure(t);
            }

            Console.Write(renderer.Render(text));
        }
Example #3
0
        public static string Renderer(string text, string language, string style)
        {
            Lexer lexer = new Lexer();

            if (IsFilename(language))
                lexer.ConfigureFromFile(language);
            else
                lexer.Configure(language);

            TextRenderer renderer = new TextRenderer(lexer);

            if (IsFilename(style))
                renderer.ConfigureFromFile(style);
            else
                renderer.Configure(style);

            return renderer.Render(text);
        }
Example #4
0
 public TextRenderer(Lexer lexer)
 {
     this.lexer = lexer;
 }
Example #5
0
        public static void WriteToConsole(string text, Lexer lexer)
        {
            ConsoleColor foreground = Console.ForegroundColor;
            Colorizer colorizer = new Colorizer();

            int position = 0;
            TokenType lasttype = TokenType.Space;
            SetColor(lasttype);

            foreach (var colortype in colorizer.GetColorTypes(text, lexer))
            {
                if (colortype != lasttype)
                {
                    SetColor(colortype);
                    lasttype = colortype;
                }

                Console.Write(text[position]);
                position++;
            }

            Console.ForegroundColor = foreground;
        }
Example #6
0
 public void Setup()
 {
     this.lexer = new Lexer();
 }