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 --- homework/81709/JSCompiler.Core/BaseInputStream.cs | 91 +++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 homework/81709/JSCompiler.Core/BaseInputStream.cs (limited to 'homework/81709/JSCompiler.Core/BaseInputStream.cs') diff --git a/homework/81709/JSCompiler.Core/BaseInputStream.cs b/homework/81709/JSCompiler.Core/BaseInputStream.cs new file mode 100644 index 0000000..7145ce8 --- /dev/null +++ b/homework/81709/JSCompiler.Core/BaseInputStream.cs @@ -0,0 +1,91 @@ +using System; +using System.IO; +using System.Collections; +using System.Collections.Generic; + +namespace JSCompiler.Core +{ + public class BaseInputStream : IInputStream + { + private char[][] buffs; + + private int index; + + private int activeBuffer; + + private StreamReader reader; + + public BaseInputStream(Stream stream, int buffLength = 4096) + { + this.Line = 0; + this.index = 0; + this.activeBuffer = 0; + this.buffs = new char[2][]; + this.buffs[0] = new char[buffLength]; + this.buffs[1] = new char[buffLength]; + this.reader = new StreamReader(stream); + + ReadStream(buffs[0], buffLength); + } + + public BaseInputStream(string path, int buffLength = 4096) + : this(new FileStream(path, FileMode.Open), buffLength) { } + + public int Line { get; private set; } + + public void Close() + { + reader.Close(); + } + + public bool IsEndOfStream() + { + return Peek() == 0; + } + + public char Next() + { + if (IsEndOfStream()) throw new EndOfStreamException(); + + char cur = Peek(); + + if (cur == '\n') + { + Line++; + } + + index++; + + if (index >= buffs[activeBuffer].Length) + { + SwitchBuffers(); + } + + return cur; + } + + public char Peek() + { + return buffs[activeBuffer][index]; + } + + private void SwitchBuffers() + { + index = 0; + activeBuffer = activeBuffer == 0 ? 1 : 0; + ReadStream(buffs[activeBuffer], buffs[activeBuffer].Length); + } + + private int ReadStream(char[] buffer, int length) + { + int bytesRead = reader.Read(buffer, 0, length); + + if (bytesRead < length) + { + buffer[bytesRead] = (char) 0; + } + + return bytesRead; + } + } +} -- cgit v1.3.1-26-gb76a