fixed word parsing

This commit is contained in:
Brychan Dempsey 2021-03-12 12:14:51 +13:00
parent e953b1cc90
commit 5082bc6481

View File

@ -71,7 +71,8 @@ namespace Assignment_1
printwords, printwords,
printwordcount, printwordcount,
set, set,
reverse reverse,
h
} }
public void StartParsing(Stream source, bool dynamicInput = false) public void StartParsing(Stream source, bool dynamicInput = false)
{ {
@ -125,6 +126,13 @@ namespace Assignment_1
case statements.reverse: case statements.reverse:
Reverse(source); Reverse(source);
break; 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"); else Console.WriteLine("Failed parsing statement");
@ -143,10 +151,9 @@ namespace Assignment_1
/// <returns></returns> /// <returns></returns>
bool Append(Stream source, long lineStart = -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) if (lineStart == -1)
{ {
lineStart = source.Position - "append ".Length; lineStart = GetLineStart(source, "append");
} }
string key; string key;
long keyEndPos = FindIdentifier(source, out key); long keyEndPos = FindIdentifier(source, out key);
@ -256,18 +263,13 @@ namespace Assignment_1
{ {
if(lineStart == -1) if(lineStart == -1)
{ {
lineStart = source.Position - "set ".Length; lineStart = GetLineStart(source, "set");
} }
string identifier; string identifier;
long identifierEndPos = FindIdentifier(source, out 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 WriteDebugLine(lineStart, "set ".Length, "???", source);
// set x
string errorMSG = " ^ could not identify object";
Console.WriteLine(fullLine);
Console.WriteLine(errorMSG);
source.SetLength(source.Position);
return false; return false;
} }
source.Position = identifierEndPos; source.Position = identifierEndPos;
@ -275,6 +277,7 @@ namespace Assignment_1
long expressionEndPos = FindExpression(source, out expression); long expressionEndPos = FindExpression(source, out expression);
if (expressionEndPos < 0) if (expressionEndPos < 0)
{ {
WriteDebugLine(lineStart, identifierEndPos, "failed parsing expression", source);
// Couldn't match expression // Couldn't match expression
return false; return false;
} }
@ -282,8 +285,7 @@ namespace Assignment_1
{ {
if (Symbols[identifier].Item2.HasFlag(VariableFlags.Reserved)) if (Symbols[identifier].Item2.HasFlag(VariableFlags.Reserved))
{ {
Console.WriteLine("Error: Cannot assign to {0} as it is a reserved constant.", identifier); WriteDebugLine(lineStart, identifierEndPos - identifier.Length, "cannot assign to a reserved constant", source);
source.SetLength(source.Position); // Wipe the remainder of the stream, so that it doesn't get read
return false; return false;
} }
Symbols[identifier] = new Tuple<string, VariableFlags>(expression, Symbols[identifier].Item2); Symbols[identifier] = new Tuple<string, VariableFlags>(expression, Symbols[identifier].Item2);
@ -331,11 +333,29 @@ namespace Assignment_1
{ {
source.Position = lineStart; source.Position = lineStart;
string fullLine = GetNextLine(source); 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(fullLine);
Console.WriteLine(errorMSG); Console.WriteLine(errorMSG);
source.SetLength(source.Position); source.SetLength(source.Position);
} }
/// <summary>
/// Gets the starting point of the expression at expected line.
/// </summary>
/// <param name="source"></param>
/// <param name="word"></param>
/// <returns></returns>
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 #endregion
#region Data Handling #region Data Handling
@ -507,7 +527,18 @@ namespace Assignment_1
/// <returns></returns> /// <returns></returns>
static long FindNextWord(Stream s, out string nextWord) 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)) else if (p(nextChar, s))
{ {
/*if (c == '\n')
{
s.Position--;
if (s.ReadByte() != '\r') s.Position--;
// Avoid capturing the carriage return
}*/
charFound = true; charFound = true;
} }
else else