From 5082bc64818d9580c92e04dea8ddf3455b5d4b5f Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Fri, 12 Mar 2021 12:14:51 +1300 Subject: [PATCH] fixed word parsing --- Assignment 1/Program.cs | 67 ++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/Assignment 1/Program.cs b/Assignment 1/Program.cs index 95902da..aa6e0af 100644 --- a/Assignment 1/Program.cs +++ b/Assignment 1/Program.cs @@ -71,7 +71,8 @@ namespace Assignment_1 printwords, printwordcount, set, - reverse + reverse, + h } public void StartParsing(Stream source, bool dynamicInput = false) { @@ -125,6 +126,13 @@ namespace Assignment_1 case statements.reverse: Reverse(source); break; + case statements.h: + Console.WriteLine("Commands are: "); + foreach (var item in Enum.GetValues(typeof(statements))) + { + Console.WriteLine("\t{0}", ((statements)item).ToString()); + } + break; } } else Console.WriteLine("Failed parsing statement"); @@ -143,10 +151,9 @@ namespace Assignment_1 /// bool Append(Stream source, long lineStart = -1) { - // If it wasn't explicitly set, assume line starts the length of 'append ' before the current position if (lineStart == -1) { - lineStart = source.Position - "append ".Length; + lineStart = GetLineStart(source, "append"); } string key; long keyEndPos = FindIdentifier(source, out key); @@ -256,18 +263,13 @@ namespace Assignment_1 { if(lineStart == -1) { - lineStart = source.Position - "set ".Length; + lineStart = GetLineStart(source, "set"); } string identifier; long identifierEndPos = FindIdentifier(source, out identifier); - if (identifierEndPos < 0 || identifierEndPos == source.Position) + if (identifierEndPos <= source.Position + 1 ) { - string fullLine = GetNextLine(source); // Grab a copy of the line to show the user - // set x - string errorMSG = " ^ could not identify object"; - Console.WriteLine(fullLine); - Console.WriteLine(errorMSG); - source.SetLength(source.Position); + WriteDebugLine(lineStart, "set ".Length, "???", source); return false; } source.Position = identifierEndPos; @@ -275,6 +277,7 @@ namespace Assignment_1 long expressionEndPos = FindExpression(source, out expression); if (expressionEndPos < 0) { + WriteDebugLine(lineStart, identifierEndPos, "failed parsing expression", source); // Couldn't match expression return false; } @@ -282,8 +285,7 @@ namespace Assignment_1 { if (Symbols[identifier].Item2.HasFlag(VariableFlags.Reserved)) { - Console.WriteLine("Error: Cannot assign to {0} as it is a reserved constant.", identifier); - source.SetLength(source.Position); // Wipe the remainder of the stream, so that it doesn't get read + WriteDebugLine(lineStart, identifierEndPos - identifier.Length, "cannot assign to a reserved constant", source); return false; } Symbols[identifier] = new Tuple(expression, Symbols[identifier].Item2); @@ -331,11 +333,29 @@ namespace Assignment_1 { source.Position = lineStart; string fullLine = GetNextLine(source); - string errorMSG = new string (' ', (int)(caratPos - lineStart)) + "^ " + errorMessage; + string errorMSG = new string (' ', (caratPos - lineStart) >= 0 ? (int)(caratPos - lineStart):0) + "^ " + errorMessage; Console.WriteLine(fullLine); Console.WriteLine(errorMSG); source.SetLength(source.Position); } + /// + /// Gets the starting point of the expression at expected line. + /// + /// + /// + /// + long GetLineStart(Stream source, string word) + { + // Decrement and check the previous value is 32 (space), before incrementing back to our current pos + // Don't need to guard against oob - implied it's within bounds by the set command being at least 3 bytes long + source.Position--; + if (source.ReadByte() == 32) + { + source.Position--; + } + //source.Position++; + return source.Position - word.Length; + } #endregion #region Data Handling @@ -507,7 +527,18 @@ namespace Assignment_1 /// static long FindNextWord(Stream s, out string nextWord) { - return FindNextOccurance(s, (c, s) => Char.IsWhiteSpace(c), out nextWord); + // Find the next occurance of a whitespace character + long originalPos = s.Position; + string tempstring; + long nextWSOccurance = FindNextOccurance(s, (c, s) => Char.IsWhiteSpace(c), out tempstring); + while (tempstring.Length == 0) + { + nextWSOccurance = FindNextOccurance(s, (c, s) => Char.IsWhiteSpace(c), out tempstring); + s.Position++; + } + nextWord = tempstring; + s.Position = originalPos; + return nextWSOccurance; } @@ -532,12 +563,6 @@ namespace Assignment_1 } else if (p(nextChar, s)) { - /*if (c == '\n') - { - s.Position--; - if (s.ReadByte() != '\r') s.Position--; - // Avoid capturing the carriage return - }*/ charFound = true; } else