fixed word parsing
This commit is contained in:
parent
e953b1cc90
commit
5082bc6481
@ -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
|
||||
/// <returns></returns>
|
||||
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<string, VariableFlags>(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);
|
||||
}
|
||||
/// <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
|
||||
#region Data Handling
|
||||
@ -507,7 +527,18 @@ namespace Assignment_1
|
||||
/// <returns></returns>
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user