Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81709 / JSCompiler.Core / BaseInputStream.cs
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.Core/BaseInputStream.cs
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
1 files changed, 91 insertions, 0 deletions
-rw-r--r--homework/81709/JSCompiler.Core/BaseInputStream.cs91
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;
+ }
+ }
+}