currentTok = default(Token);
}
/// <summary>
- /// Save current token into current TokensLine after having skipped white spaces and raz current token
- /// </summary>
- protected void saveAndResetAfterWhiteSpaceSkipping() {
- buffer[currentLine].Tokens.Add (currentTok);
- currentTok = default(Token);
- if (WpToken == null)
- return;
- buffer[currentLine].Tokens.Add ((Token)WpToken);
- WpToken = null;
- }
- /// <summary>
/// read one char and add current token to current TokensLine, current token is reset
/// </summary>
/// <param name="type">Type of the token</param>
saveAndResetCurrentTok ();
}
/// <summary>
- /// Save current tok after having skipped white spaces
- /// </summary>
- /// <param name="type">set the type of the tok</param>
- protected void saveAndResetAfterWhiteSpaceSkipping(System.Enum type) {
- currentTok.Type = (TokenType)type;
- saveAndResetAfterWhiteSpaceSkipping ();
- }
- /// <summary>
/// Peek next char, emit '\n' if current column > buffer's line length
/// Throw error if eof is true
/// </summary>
return tmp;
}
/// <summary>
- /// skip white spaces, but not line break. Save spaces in a WhiteSpace token and dont
- /// save it directely if currentTok is not null
+ /// skip white spaces, but not line break. Save spaces in a WhiteSpace token.
/// </summary>
protected void SkipWhiteSpaces () {
- if (WpToken != null)
- throw new ParsingException (this, "white space token already pending");
- Token wp = default(Token);
+ if (currentTok.Type != TokenType.Unknown)
+ throw new ParsingException (this, "current token should be reset to unknown (0) before skiping white spaces");
while (!eol) {
if (!char.IsWhiteSpace (Peek ())||Peek()=='\n')
break;
- if (wp.Type == TokenType.Unknown)
- wp.Start = CurrentPosition;
- wp += Read();
- wp.Type = TokenType.WhiteSpace;
+ readToCurrTok (currentTok.Type == TokenType.Unknown);
+ currentTok.Type = TokenType.WhiteSpace;
}
- if (wp.Type == TokenType.Unknown)
- return;
- wp.End = CurrentPosition;
- if (currentTok.Type == TokenType.Unknown)
- buffer [currentLine].Tokens.Add (wp);
- else
- WpToken = wp;
+ if (currentTok.Type != TokenType.Unknown)
+ saveAndResetCurrentTok ();
}
- protected object WpToken = null;
#endregion
}
}
\ No newline at end of file
{
public class StyleParser : Parser
{
- enum States { init, classNames, members }
+ enum States { init, classNames, members, value, endOfStatement }
public StyleParser (CodeBuffer _buffer) : base(_buffer)
{
Debug.WriteLine (string.Format("parsing line:{0}", currentLine));
CodeLine cl = buffer [currentLine];
cl.Tokens = new List<Token> ();
- WpToken = null;
//retrieve current parser state from previous line
if (currentLine > 0)
}
break;
case ',':
- if (currentTok.Type != TokenType.Identifier || curState == States.members )
+ if (curState != States.init || curState != States.classNames )
throw new ParsingException (this, "Unexpected char ','");
- saveAndResetAfterWhiteSpaceSkipping (TokenType.Type);//save previous token as class
- readToCurrTok (true);
- saveAndResetCurrentTok (TokenType.UnaryOp);
+ readAndResetCurrentTok (TokenType.UnaryOp, true);
curState = States.classNames;
break;
case '{':
- if (currentTok.Type != TokenType.Identifier || curState == States.members)
- throw new ParsingException (this, "Unexpected char '}'");
-
- saveAndResetAfterWhiteSpaceSkipping (TokenType.Type);//save previous token as class
-
- readToCurrTok (true);
- saveAndResetCurrentTok (TokenType.OpenBlock);
+ if (!(curState == States.init || curState == States.classNames))
+ throw new ParsingException (this, "Unexpected char '{'");
+ readAndResetCurrentTok (TokenType.OpenBlock, true);
curState = States.members;
break;
case '}':
if (curState != States.members)
throw new ParsingException (this, "Unexpected char '}'");
- readToCurrTok (true);
- saveAndResetCurrentTok (TokenType.CloseBlock);
+ readAndResetCurrentTok (TokenType.CloseBlock, true);
curState = States.classNames;
break;
case '=':
- if (currentTok.Type != TokenType.Identifier)
+ if (curState == States.classNames)
throw new ParsingException (this, "Unexpected char '='");
-
- saveAndResetAfterWhiteSpaceSkipping ();//save previous token as propertyname
-
- curState = States.members;
-
- readToCurrTok (true);
- saveAndResetCurrentTok (TokenType.Affectation);
-
- SkipWhiteSpaces ();
-
- currentTok+=ReadLineUntil(";");
+ readAndResetCurrentTok (TokenType.Affectation, true);
+ curState = States.value;
+ break;
+ case '"':
+ if (curState != States.value)
+ throw new ParsingException (this, "Unexpected char '\"'");
+ readAndResetCurrentTok (TokenType.StringLitteralOpening, true);
+
+ while (!eol) {
+ currentTok += ReadLineUntil ("\"");
+ if (currentTok.Content [currentTok.Content.Length - 1] == '\\')
+ readToCurrTok ();
+ else
+ break;
+ }
+ if (eol)
+ throw new ParsingException (this, "Unexpected end of line");
saveAndResetCurrentTok (TokenType.StringLitteral);
- if (Peek() != ';')
- throw new ParsingException (this, "Expecting ';'");
- readToCurrTok (true);
- saveAndResetCurrentTok (TokenType.StatementEnding);
+ readAndResetCurrentTok (TokenType.StringLitteralClosing, true);
+ curState = States.endOfStatement;
+ break;
+ case ';':
+ if (curState != States.endOfStatement)
+ throw new ParsingException (this, "Unexpected end of statement");
+ readAndResetCurrentTok (TokenType.StatementEnding, true);
+ curState = States.members;
break;
default:
if (currentTok.Type != TokenType.Unknown)
- throw new ParsingException (this, "error");
+ throw new ParsingException (this, "error curtok not null");
+ if (curState == States.value)
+ throw new ParsingException (this, "expecting value enclosed in '\"'");
+ if (curState == States.endOfStatement)
+ throw new ParsingException (this, "expecting end of statement");
if (nextCharIsValidCharStartName) {
readToCurrTok (true);
while (nextCharIsValidCharName)
readToCurrTok ();
}
- currentTok.Type = TokenType.Identifier;
- currentTok.End = CurrentPosition;
+ saveAndResetCurrentTok (TokenType.Identifier);
break;
}
}