String literal recognition problem
Posted
by
helicera
on Stack Overflow
See other posts from Stack Overflow
or by helicera
Published on 2011-01-12T21:04:48Z
Indexed on
2011/01/12
21:54 UTC
Read the original article
Hit count: 220
Hello! I'm trying to recognize string literal by reading string per symbol. Here is a sample code:
#region [String Literal (")]
case '"': // {string literal ""}
{
// skipping '"'
ChCurrent = Line.ElementAtOrDefault<Char>(++ChPosition);
while(ChCurrent != '"')
{
Value.Append(ChCurrent);
ChCurrent = Line.ElementAtOrDefault<Char>(++ChPosition);
if(ChCurrent == '"')
{
// "" sequence only acceptable
if(Line.ElementAtOrDefault<Char>(ChPosition + 1) == '"')
{
Value.Append(ChCurrent);
// skip 2nd double quote
ChPosition++;
// move position next
ChCurrent = Line.ElementAtOrDefault<Char>(++ChPosition);
}
}
else if(default(Char) == ChCurrent)
{
// message: unterminated string
throw new ScanningException();
}
}
ChPosition++;
break;
}
#endregion
When I run test:
[Test]
[ExpectedException(typeof(ScanningException))]
public void ScanDoubleQuotedStrings()
{
this.Scanner.Run(@"""Hello Language Design""", default(System.Int32));
this.Scanner.Run(@"""Is there any problems with the """"strings""""?""", default(System.Int32));
this.Scanner.Run(@"""v#:';?325;.<>,|+_)""(*&^%$#@![]{}\|-_=""", default(System.Int32));
while(0 != this.Scanner.TokensCount - 1)
{
Assert.AreEqual(Token.TokenClass.StringLiteral, this.Scanner.NextToken.Class);
}
}
It passes with success.. while I'm expecting to have an exception according to unmatched " mark in
this.Scanner.Run(@"""v#:';?325;.<>,|+_)""(*&^%$#@![]{}\|-_=""", default(System.Int32));
Can anyone explain where is my mistake or give an advice on algorithm.
© Stack Overflow or respective owner