diff --git a/Assignment 1/Assignment 1.csproj b/Assignment 1/Assignment 1.csproj index 76fc6da..2349448 100644 --- a/Assignment 1/Assignment 1.csproj +++ b/Assignment 1/Assignment 1.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/Assignment 1/Program.cs b/Assignment 1/Program.cs index cc8f93a..ef186a9 100644 --- a/Assignment 1/Program.cs +++ b/Assignment 1/Program.cs @@ -39,6 +39,7 @@ namespace Assignment_1 '$', '\\', '\"', '\'' }; + static bool nonStrict = false; static void Main(string[] args) { @@ -48,14 +49,33 @@ namespace Assignment_1 Console.WriteLine("└──────────────────────────────────────────┘"); // Parse the source from the memory stream MemoryStream sourceStream = new MemoryStream(1024); - Parser parser = new Parser(); + Parser parser = new(); bool dynamicInput = false; + foreach (var arg in args) + { + if (arg == "-ns") + { + nonStrict = true; + } + } // From https://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected // Reading from pipes is equivalent to reading user input, though the input is redirected if (Console.IsInputRedirected) { sourceStream.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd())); + sourceStream.Position = 0; + Console.WriteLine("Input is piped"); + /*byte[] bytes = new byte[sourceStream.Length]; + sourceStream.Read(bytes); + string readBytes = Encoding.UTF8.GetString(bytes); + Console.WriteLine(readBytes); + sourceStream.Position = 0; + foreach (var item in bytes) + { + Console.Write(item+", "); + } + Console.WriteLine();*/ } else { @@ -86,8 +106,12 @@ namespace Assignment_1 public void StartParsing(Stream source, bool dynamicInput = false) { long initSourceLength = source.Length; - if ((byte)source.ReadByte() == '{') + if (nonStrict || PeekChar(source) == '{') { + if (PeekChar(source) == '{') + { + source.ReadByte(); + } long lastLinePos = 0; long initPos = 0; bool cont = false; @@ -206,9 +230,10 @@ namespace Assignment_1 { // Statement parse failed, // Ensure stream gets trimmed back to the correct position - Console.WriteLine("Failed parsing statement"); - source.Position = initPos; - source.SetLength(initPos); + //Console.WriteLine("Failed parsing statement"); + Console.WriteLine("Position is " + source.Position); + Console.WriteLine("Length is " + source.Length); + throw new ParserException("Failed parsing statement", 0, source.Position); } } // Throwing a parserexception will return us to this point immediately. From here, the line is automatically restored, @@ -230,12 +255,19 @@ namespace Assignment_1 source.Position = initPos; source.SetLength(initPos); } - + if (!dynamicInput) + { + Environment.Exit(-1); + } } } } + else + { + Console.WriteLine("First read character was not \'{\'. Use the launch flag -ns for non-strict checking"); + } } #region Function Handling diff --git a/Assignment 1/Properties/launchSettings.json b/Assignment 1/Properties/launchSettings.json new file mode 100644 index 0000000..9f75985 --- /dev/null +++ b/Assignment 1/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "Assignment 1": { + "commandName": "Project" + }, + "newProfile1": { + "commandName": "Executable", + "executablePath": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", + "commandLineArgs": "-command \"& type .\\outtest.txt | & '.\\Assignment 1.exe'\"" + } + } +} \ No newline at end of file