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 --- .../81709/JSCompiler.SyntaxHighlighter/App.config | 6 + .../JSCompiler.SyntaxHighlighter.csproj | 62 +++++++ .../81709/JSCompiler.SyntaxHighlighter/Program.cs | 36 ++++ .../Properties/AssemblyInfo.cs | 36 ++++ .../SyntaxHighlighter.cs | 187 +++++++++++++++++++++ .../JSCompiler.SyntaxHighlighter/test/test.js | 23 +++ .../JSCompiler.SyntaxHighlighter/test/testout.html | 49 ++++++ 7 files changed, 399 insertions(+) create mode 100644 homework/81709/JSCompiler.SyntaxHighlighter/App.config create mode 100644 homework/81709/JSCompiler.SyntaxHighlighter/JSCompiler.SyntaxHighlighter.csproj create mode 100644 homework/81709/JSCompiler.SyntaxHighlighter/Program.cs create mode 100644 homework/81709/JSCompiler.SyntaxHighlighter/Properties/AssemblyInfo.cs create mode 100644 homework/81709/JSCompiler.SyntaxHighlighter/SyntaxHighlighter.cs create mode 100644 homework/81709/JSCompiler.SyntaxHighlighter/test/test.js create mode 100644 homework/81709/JSCompiler.SyntaxHighlighter/test/testout.html (limited to 'homework/81709/JSCompiler.SyntaxHighlighter') diff --git a/homework/81709/JSCompiler.SyntaxHighlighter/App.config b/homework/81709/JSCompiler.SyntaxHighlighter/App.config new file mode 100644 index 0000000..62c8f44 --- /dev/null +++ b/homework/81709/JSCompiler.SyntaxHighlighter/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/homework/81709/JSCompiler.SyntaxHighlighter/JSCompiler.SyntaxHighlighter.csproj b/homework/81709/JSCompiler.SyntaxHighlighter/JSCompiler.SyntaxHighlighter.csproj new file mode 100644 index 0000000..cb321ea --- /dev/null +++ b/homework/81709/JSCompiler.SyntaxHighlighter/JSCompiler.SyntaxHighlighter.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {D555ADB4-0161-4DF9-8098-1A72C03C1576} + Exe + JSCompiler.SyntaxHighlighter + JSCompiler.SyntaxHighlighter + v4.7.1 + 512 + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + Designer + + + + + {c8dd3d07-6ae9-444b-8ba0-22dc633a9432} + JSCompiler.Core + + + + \ No newline at end of file diff --git a/homework/81709/JSCompiler.SyntaxHighlighter/Program.cs b/homework/81709/JSCompiler.SyntaxHighlighter/Program.cs new file mode 100644 index 0000000..fd0d539 --- /dev/null +++ b/homework/81709/JSCompiler.SyntaxHighlighter/Program.cs @@ -0,0 +1,36 @@ +using JSCompiler.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JSCompiler.SyntaxHighlighter +{ + class Program + { + static int Main(string[] args) + { + if(args.Length < 2) + { + Console.WriteLine("Input and output destinations not specified!"); + return 1; + } + + try + { + BaseInputStream stream = new BaseInputStream(args[0]); + Lexer lex = new Lexer(stream); + List tokens = lex.Tokenize((line, cause) => Console.Out.WriteLine($"On line {line} error: {cause}")); + SyntaxHighlighter.GenerateHtml(tokens, args[1]); + } + catch(Exception e) + { + Console.WriteLine(e.Message); + return 1; + } + + return 0; + } + } +} diff --git a/homework/81709/JSCompiler.SyntaxHighlighter/Properties/AssemblyInfo.cs b/homework/81709/JSCompiler.SyntaxHighlighter/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..6d5d657 --- /dev/null +++ b/homework/81709/JSCompiler.SyntaxHighlighter/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("JSCompiler.SyntaxHighlighter")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("JSCompiler.SyntaxHighlighter")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d555adb4-0161-4df9-8098-1a72c03c1576")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] 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); + } + } + } +} diff --git a/homework/81709/JSCompiler.SyntaxHighlighter/test/test.js b/homework/81709/JSCompiler.SyntaxHighlighter/test/test.js new file mode 100644 index 0000000..537e59f --- /dev/null +++ b/homework/81709/JSCompiler.SyntaxHighlighter/test/test.js @@ -0,0 +1,23 @@ +/* +** This is a test function to demonstrate syntax highlighting! +*/ +function myFunction() { + var res = ""; + res = res +isNaN(123) + ": 123"; + res = res + isNaN(-1.23) + ": -1.23"; + res = res + isNaN(5-2) + ": 5-2"; + res = res + isNaN(0) + ": 0"; + res = res + isNaN('123') + ": 123 + "; + //oops! + res = res + isNaN(true) + ": true"; + res = res + isNaN(undefined) + ": undefined \ + nextString \ + "; + res = res + isNaN('NaN') + ": NaN 'Hell no'"; + res = res + isNaN(NaN) + ': NaN \\n \'; + res = res + isNaN(0 / 0) + ": 0 / 0 \ "; + + document.getElementById("demo").innerHTML = res; +} +//Cool, it works! \ No newline at end of file diff --git a/homework/81709/JSCompiler.SyntaxHighlighter/test/testout.html b/homework/81709/JSCompiler.SyntaxHighlighter/test/testout.html new file mode 100644 index 0000000..d1182c2 --- /dev/null +++ b/homework/81709/JSCompiler.SyntaxHighlighter/test/testout.html @@ -0,0 +1,49 @@ + + + + + ..\..\test\testout.html + + /* +** This is a test function to demonstrate syntax highlighting! +*/
function myFunction() {
    var res = "";
    res = res +isNaN(123) + ": 123";
    res = res + isNaN(-1.23) + ": -1.23";
    res = res + isNaN(5-2) + ": 5-2";
    res = res + isNaN(0) + ": 0";
    res = res + isNaN('123') + ": 123
    ";
    //oops!
    res = res + isNaN(true) + ": true";
    res = res + isNaN(undefined) + ": undefined \ + nextString \ + ";
    res = res + isNaN('NaN') + ": NaN 'Hell no'";
    res = res + isNaN(NaN) + ': NaN \\n \';
    res = res + isNaN(0 / 0) + ": 0 / 0 \ ";

    document.getElementById("demo").innerHTML = res;
}
//Cool, it works! \ No newline at end of file -- cgit v1.3.1-26-gb76a