Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81709 / JSCompiler.SyntaxHighlighter
diff options
context:
space:
mode:
Commit47e8c6f331525f01f150ba80a3557a18618b0cf5 (patch)
Author
 Georgi Barnev <gbarnev@gmail.com>
2018-11-07 13:05:43 +0200
Committer
 dtrendafilov <trendafilov.dn@gmail.com>
2018-11-07 13:05:43 +0200
Files3fde3f4fc002e4d10c49436d85188d4f3b02e33f /homework/81709/JSCompiler.SyntaxHighlighter
IPL-47e8c6f331525f01f150ba80a3557a18618b0cf5.tar
IPL-47e8c6f331525f01f150ba80a3557a18618b0cf5.tar.gz
IPL-47e8c6f331525f01f150ba80a3557a18618b0cf5.zip
Parent5a4403ef23e17a3da84f1ec9feb3ab294d23c198 (diff)
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
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 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+ <startup>
+ <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
+ </startup>
+</configuration>
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 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{D555ADB4-0161-4DF9-8098-1A72C03C1576}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>JSCompiler.SyntaxHighlighter</RootNamespace>
+ <AssemblyName>JSCompiler.SyntaxHighlighter</AssemblyName>
+ <TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+ <TargetFrameworkProfile />
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Net.Http" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="SyntaxHighlighter.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="App.config">
+ <SubType>Designer</SubType>
+ </None>
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\JSCompiler.Core\JSCompiler.Core.csproj">
+ <Project>{c8dd3d07-6ae9-444b-8ba0-22dc633a9432}</Project>
+ <Name>JSCompiler.Core</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project> \ 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<Token> 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 =
+ @"<!DOCTYPE html>
+<html>
+<head>
+{0}
+<title> {1} </title>
+</head>
+<body> {2} </body></html>";
+
+ private static readonly string DEFAULT_CSS =
+ @"<style>
+body {
+ font-size: 20px;
+ font-family: ""Verdana"";
+}
+.number {
+ color: Orange;
+}
+.string {
+ color: Gray;
+ white-space: pre-wrap;
+}
+.keyword {
+ color: Blue;
+}
+.identifier {
+ color: Black;
+}
+.operator {
+ color: DarkBlue;
+}
+.comment {
+ color: Green;
+ font-style: italic;
+ white-space: pre-wrap;
+}
+
+.reserved {
+ color: MediumVioletRed;
+}
+
+.undefined{
+ color: Black;
+}
+
+.default {
+ color: DarkBlue;
+}
+</style>";
+
+ private static IEnumerable<TokenType> KEYWORDS = ReservedStrings.KeywordsAsMap().Values;
+
+ private static IEnumerable<TokenType> OTHER_RESERVED = ReservedStrings.OthersAsMap().Values;
+
+ private static IEnumerable<TokenType> ONE_SYMBOL_OPERATORS = new List<TokenType>
+ {
+ 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<TokenType> MULT_SYMBOL_OPERATORS = new List<TokenType>
+ {
+ 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<Token> tokens, string fileName, string css = null)
+ {
+ if (css == null) css = DEFAULT_CSS;
+
+ string code = "";
+ IEnumerator<Token> tokenEnum = tokens.GetEnumerator();
+ if (!tokenEnum.MoveNext()) return;
+ Token cur;
+ do
+ {
+ cur = tokenEnum.Current;
+ TokenType type = cur.Type;
+
+ switch (type)
+ {
+ case TokenType.Whitespace:
+ code += "&nbsp";
+ break;
+ case TokenType.Tab:
+ code += "&nbsp" + "&nbsp" + "&nbsp" + "&nbsp";
+ break;
+ case TokenType.NewLine:
+ code += "<br>";
+ break;
+ case TokenType.Number:
+ code += "<span class=\"number\">" + ((NumToken)cur).Num + "</span>";
+ break;
+ case TokenType.String:
+ code += "<span class=\"string\">" + ((WordToken)cur).Lexeme + "</span>";
+ break;
+ case TokenType.Identifier:
+ code += "<span class=\"identifier\">" + ((WordToken)cur).Lexeme + "</span>";
+ break;
+ case TokenType.Comment:
+ code += "<span class=\"comment\">" + ((WordToken)cur).Lexeme + "</span>";
+ break;
+ case TokenType.Error:
+ code += "<span class=\"undefined\">" + ((WordToken)cur).Lexeme + "</span>";
+ break;
+
+ default:
+ if (KEYWORDS.Contains(type))
+ {
+ code += "<span class=\"keyword\">" + ((WordToken)cur).Lexeme + "</span>";
+ }
+ else if(OTHER_RESERVED.Contains(type))
+ {
+ code += "<span class=\"reserved\">" + ((WordToken)cur).Lexeme + "</span>";
+ }
+ else if (ONE_SYMBOL_OPERATORS.Contains(type))
+ {
+ code += "<span class=\"operator\">" + cur.ToString() + "</span>";
+ }
+ else if (MULT_SYMBOL_OPERATORS.Contains(type))
+ {
+ code += "<span class=\"operator\">" + ((WordToken)cur).Lexeme + "</span>";
+ }
+ else
+ {
+ code += "<span class=\"default\">" + cur.ToString() + "</span>";
+ 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 @@
+<!DOCTYPE html>
+<html>
+<head>
+<style>
+body {
+ font-size: 20px;
+ font-family: "Verdana";
+}
+.number {
+ color: Orange;
+}
+.string {
+ color: Gray;
+ white-space: pre-wrap;
+}
+.keyword {
+ color: Blue;
+}
+.identifier {
+ color: Black;
+}
+.operator {
+ color: DarkBlue;
+}
+.comment {
+ color: Green;
+ font-style: italic;
+ white-space: pre-wrap;
+}
+
+.reserved {
+ color: MediumVioletRed;
+}
+
+.undefined{
+ color: Black;
+}
+
+.default {
+ color: DarkBlue;
+}
+</style>
+<title> ..\..\test\testout.html </title>
+</head>
+<body> <span class="comment">/*
+** This is a test function to demonstrate syntax highlighting!
+*/</span><br><span class="keyword">function</span>&nbsp<span class="identifier">myFunction</span><span class="default">(</span><span class="default">)</span>&nbsp<span class="default">{</span><br>&nbsp&nbsp&nbsp&nbsp<span class="keyword">var</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="string">""</span><span class="default">;</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span><span class="reserved">isNaN</span><span class="default">(</span><span class="number">123</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="string">": 123"</span><span class="default">;</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span>&nbsp<span class="reserved">isNaN</span><span class="default">(</span><span class="operator">-</span><span class="number">1</span><span class="default">.</span><span class="number">23</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="string">": -1.23"</span><span class="default">;</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span>&nbsp<span class="reserved">isNaN</span><span class="default">(</span><span class="number">5</span><span class="operator">-</span><span class="number">2</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="string">": 5-2"</span><span class="default">;</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span>&nbsp<span class="reserved">isNaN</span><span class="default">(</span><span class="number">0</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="string">": 0"</span><span class="default">;</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span>&nbsp<span class="reserved">isNaN</span><span class="default">(</span><span class="string">'123'</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="undefined">": 123</span><br>&nbsp&nbsp&nbsp&nbsp<span class="undefined">"; </span><br>&nbsp&nbsp&nbsp&nbsp<span class="comment">//oops!</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span>&nbsp<span class="reserved">isNaN</span><span class="default">(</span><span class="keyword">true</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="string">": true"</span><span class="default">;</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span>&nbsp<span class="reserved">isNaN</span><span class="default">(</span><span class="reserved">undefined</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="string">": undefined \
+ nextString \
+ "</span><span class="default">;</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span>&nbsp<span class="reserved">isNaN</span><span class="default">(</span><span class="string">'NaN'</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="string">": NaN 'Hell no'"</span><span class="default">;</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span>&nbsp<span class="reserved">isNaN</span><span class="default">(</span><span class="reserved">NaN</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="undefined">': NaN \\n \';</span><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">res</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span>&nbsp<span class="operator">+</span>&nbsp<span class="reserved">isNaN</span><span class="default">(</span><span class="number">0</span>&nbsp<span class="operator">/</span>&nbsp<span class="number">0</span><span class="default">)</span>&nbsp<span class="operator">+</span>&nbsp<span class="string">": 0 / 0 \ "</span><span class="default">;</span><br><br>&nbsp&nbsp&nbsp&nbsp<span class="identifier">document</span><span class="default">.</span><span class="identifier">getElementById</span><span class="default">(</span><span class="string">"demo"</span><span class="default">)</span><span class="default">.</span><span class="identifier">innerHTML</span>&nbsp<span class="operator">=</span>&nbsp<span class="identifier">res</span><span class="default">;</span><br><span class="default">}</span><br><span class="comment">//Cool, it works!</span> </body></html> \ No newline at end of file