From 47e8c6f331525f01f150ba80a3557a18618b0cf5 Mon Sep 17 00:00:00 2001 From: Georgi Barnev Date: Wed, 7 Nov 2018 13:05:43 +0200 Subject: Homework 1 - C# Lexer and Syntax Highlighter for Javascript (#34) * Initial lexer structure * Added IInputStream interface for the Lexer and its base implementation in FileInputStream not implemented yet. * Extend InputStream interface * Implement FileInputStream not tested * Lexer almost ready - comments need to be parsed. InputStream ready * Added some TODOs * Add error token type and almost parse comments * Parse multiline comments and fix string parsing to not include the quotes. * Added Syntax Highlighter, added support for more js reserved words, removed error tokens to not break the actual word tokens, fixed comments and string token representation to include its special characters * Initial lexer structure * Added IInputStream interface for the Lexer and its base implementation in FileInputStream not implemented yet. * Extend InputStream interface * Implement FileInputStream not tested * Lexer almost ready - comments need to be parsed. InputStream ready * Added some TODOs * Add error token type and almost parse comments * Parse multiline comments and fix string parsing to not include the quotes. * Added Syntax Highlighter, added support for more js reserved words, removed error tokens to not break the actual word tokens, fixed comments and string token representation to include its special characters * Improve project structure and add tests * Fix physical path structure of the projects * Write tests, fix tokenizer bugs * Fix README format, fix AssemblyInfo name * Escape slashes in readme --- .../SyntaxHighlighter.cs | 187 +++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 homework/81709/JSCompiler.SyntaxHighlighter/SyntaxHighlighter.cs (limited to 'homework/81709/JSCompiler.SyntaxHighlighter/SyntaxHighlighter.cs') diff --git a/homework/81709/JSCompiler.SyntaxHighlighter/SyntaxHighlighter.cs b/homework/81709/JSCompiler.SyntaxHighlighter/SyntaxHighlighter.cs new file mode 100644 index 0000000..fd8572f --- /dev/null +++ b/homework/81709/JSCompiler.SyntaxHighlighter/SyntaxHighlighter.cs @@ -0,0 +1,187 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using JSCompiler.Core; + +namespace JSCompiler.SyntaxHighlighter +{ + public static class SyntaxHighlighter + { + private static readonly string HTML = + @" + + +{0} + {1} + + {2} "; + + private static readonly string DEFAULT_CSS = + @""; + + private static IEnumerable KEYWORDS = ReservedStrings.KeywordsAsMap().Values; + + private static IEnumerable OTHER_RESERVED = ReservedStrings.OthersAsMap().Values; + + private static IEnumerable ONE_SYMBOL_OPERATORS = new List + { + TokenType.Minus, // - + TokenType.Plus, // + + TokenType.Star, // * + TokenType.Division, // / + TokenType.Modulo, // % + TokenType.BitwiseNot, // ~ + TokenType.BitwiseAnd, // & + TokenType.BitwiseXor, // ^ + TokenType.BitwiseOr, // | + TokenType.QuestionMark, // ? + TokenType.Bang, // ! + TokenType.Equal, // = + TokenType.Greater, // > + TokenType.Less, // < + TokenType.Backslash, // \ + }; + + private static IEnumerable MULT_SYMBOL_OPERATORS = new List + { + TokenType.NotEqual, // != + TokenType.EqualEqual, // == + TokenType.StrictEqual, // === + TokenType.StrictNotEqual, // !== + TokenType.GreaterEqual, // >= + TokenType.LessEqual, // <= + TokenType.MinusMinus, // -- + TokenType.PlusPlus, // ++ + TokenType.LeftShift, // << + TokenType.RightShift, // >> + TokenType.LogicalAnd, // && + TokenType.LogicalOr, // || + TokenType.StarEqual, // *= + TokenType.DivideEqual, // /= + TokenType.ModuloEqual, // %= + TokenType.PlusEqual, // += + TokenType.MinusEqual, // -= + TokenType.LeftShiftEqual, // <<= + TokenType.RightShiftEqual, // >>= + TokenType.BitwiseAndEqual, // &= + TokenType.BitwiseXorEqual, // ^= + TokenType.BitwiseOrEqual, // |= + }; + + public static void GenerateHtml(IEnumerable tokens, string fileName, string css = null) + { + if (css == null) css = DEFAULT_CSS; + + string code = ""; + IEnumerator tokenEnum = tokens.GetEnumerator(); + if (!tokenEnum.MoveNext()) return; + Token cur; + do + { + cur = tokenEnum.Current; + TokenType type = cur.Type; + + switch (type) + { + case TokenType.Whitespace: + code += " "; + break; + case TokenType.Tab: + code += " " + " " + " " + " "; + break; + case TokenType.NewLine: + code += "
"; + break; + case TokenType.Number: + code += "" + ((NumToken)cur).Num + ""; + break; + case TokenType.String: + code += "" + ((WordToken)cur).Lexeme + ""; + break; + case TokenType.Identifier: + code += "" + ((WordToken)cur).Lexeme + ""; + break; + case TokenType.Comment: + code += "" + ((WordToken)cur).Lexeme + ""; + break; + case TokenType.Error: + code += "" + ((WordToken)cur).Lexeme + ""; + break; + + default: + if (KEYWORDS.Contains(type)) + { + code += "" + ((WordToken)cur).Lexeme + ""; + } + else if(OTHER_RESERVED.Contains(type)) + { + code += "" + ((WordToken)cur).Lexeme + ""; + } + else if (ONE_SYMBOL_OPERATORS.Contains(type)) + { + code += "" + cur.ToString() + ""; + } + else if (MULT_SYMBOL_OPERATORS.Contains(type)) + { + code += "" + ((WordToken)cur).Lexeme + ""; + } + else + { + code += "" + cur.ToString() + ""; + break; + } + break; + } + } + while (tokenEnum.MoveNext() && tokenEnum.Current.Type != TokenType.EOF); + + string shouldWrite = string.Format(HTML, css, fileName, code); + + using (StreamWriter writer = new StreamWriter(fileName)) + { + writer.Write(shouldWrite); + } + } + } +} -- cgit v1.3.1-26-gb76a