public TokenCollection Lex(LexReader src) { _stack = new Stack<TokenId>(); _tokens = new TokenCollection(); _src = src; _curChar = src.Read(); Lex(); return _tokens; }
public TokenCollection Lex(LexReader src) { _stack = new Stack <TokenId>(); _tokens = new TokenCollection(); _src = src; _curChar = src.Read(); Lex(); return(_tokens); }
private void Lex() { _html.Length = 0; Location startLocation = new Location(_src.LineIndex, _src.CharacterIndex - 1); while (!_src.EndOfStream) { switch (_curChar) { case '$': { _curChar = _src.Read(); if (_html.Length > 0) { Location endLocation = new Location(_src.LineIndex, _src.CharacterIndex - 2); _tokens.Add(new Token(TokenId.Html, _html.ToString(), startLocation, endLocation)); _html.Length = 0; } if (_curChar == '{') { TokenId tokenId = TokenId.Block; _stack.Push(tokenId); _tokens.Add(new Token(tokenId, _src.LineIndex, _src.CharacterIndex - 2, 2)); _curChar = _src.Read(); LexCode(true, '\0'); startLocation = new Location(_src.LineIndex, _src.CharacterIndex - 1); } else if (WillBeKeyOrIdent(_curChar)) { _tokens.Add(new Token(TokenId.Dollar, _src.LineIndex, _src.CharacterIndex - 2, 1)); Token last = LexKeyOrIdent(); _tokens.Add(last); if (last.TokenId == TokenId.Ident || last.TokenId == TokenId.Echo) { LexInvoke(); } else { LexCode(false, '\0'); } startLocation = new Location(_src.LineIndex, _src.CharacterIndex - 1); } else { _html.Append('$'); } break; } case '}': { if (_stack.Count > 0 && _stack.Peek() == TokenId.LCurly) { if (_html.Length > 0) { Location endLocation = new Location(_src.LineIndex, _src.CharacterIndex - 1); string data = _html.ToString(); _tokens.Add(new Token(TokenId.Html, data, startLocation, endLocation)); _html.Length = 0; } LexCode(false, '\0'); _html.Length = 0; startLocation = new Location(_src.LineIndex, _src.CharacterIndex - 1); } else { _html.Append(_curChar); _curChar = _src.Read(); } break; } case '\\': { _curChar = _src.Read(); if (_curChar == '$' || _curChar == '}') { _html.Append(_curChar); } else { _html.Append('\\'); _html.Append(_curChar); } _curChar = _src.Read(); break; } default: { _html.Append(_curChar); _curChar = _src.Read(); break; } } } if (_html.Length > 0) { Location endLocation = new Location(_src.LineIndex, _src.CharacterIndex - 1); _tokens.Add(new Token(TokenId.Html, _html.ToString(), startLocation, endLocation)); _html.Length = 0; } }