/// <summary> /// Interpreta un bloque de sentencias /// </summary> private SentenceCollection ParseBlockSentences(TokenSqlSeparator.SeparatorType endBlock) { SentenceCollection sentences = new SentenceCollection(); TokenSqlBase token = Lexer.LookUp(); // Mientras haya algún token que leer while (!HasError && !(token is TokenSqlEof) && !FoundToken(token, endBlock)) { // Añade las sentencias sentences.Add(ParseSentence()); // Mira el siguiente token (sin sacarlo de la pila) token = Lexer.LookUp(); } // Si el token que hemos encontrado no es el de fin de bloque, añadimos un error if (!FoundToken(token, endBlock)) { AddError("Error when read sentences block. Waiting end block", token); } else // quita el token de fin de sentencia de la pila { Lexer.Next(); } // Devuelve la colección de sentencias return(sentences); }
/// <summary> /// Interpreta una sentencia /// </summary> private SentenceBase ParseSentence() { SentenceBase sentence = null; TokenSqlBase token = Lexer.Next(); // Interpreta la sentencia switch (token) { case TokenSqlComment tokenInner: sentence = ParseSentenceComment(tokenInner); break; case TokenSqlCommand tokenInner: sentence = ParseSentenceCommand(tokenInner); break; case TokenSqlReservedWord tokenInner: sentence = ParseSentenceReservedWord(tokenInner); break; default: AddError("Unknown token", token); break; } // Devuelve la sentencia generada return(sentence); }
/// <summary> /// Interpreta la sentencia de declaración /// </summary> /// <remarks> /// declare type name = expression; /// declare type name; /// </remarks> private SentenceDeclare ParseSentenceDeclare() { SentenceDeclare sentence = new SentenceDeclare(); TokenSqlBase token = Lexer.Next(); // Obtiene el tipo: type name = expression; if (token is TokenSqlReservedWord tokenType) { // Obtiene el tipo de variable sentence.Variable.Type = GetVariableType(tokenType); // Comprueba antes de continuar if (sentence.Variable.Type == SymbolModel.SymbolType.Unknown) { AddError("Declaration type unknown", token); } else { // Siguiente token: name = expression; token = Lexer.Next(); // Debería ser una variable if (token is TokenSqlVariable tokenVariable && !string.IsNullOrWhiteSpace(tokenVariable.ReadToken.Value)) { // Asigna el nombre de variable sentence.Variable.Name = tokenVariable.ReadToken.Value; // Continua con el resto: = expression; o bien ; token = Lexer.Next(); // Comprueba que sea un igual o un punto y coma if (token is TokenSqlSeparator tokenSeparator) { if (tokenSeparator.Type == TokenSqlSeparator.SeparatorType.Assign) { // Lee las expresiones sentence.Expressions.AddRange(ParseExpression()); // Comprueba que lo siguiente sea un fin de sentencia CheckEndSentenceToken(); } else if (tokenSeparator.Type != TokenSqlSeparator.SeparatorType.EndSentence) { AddError("Waiting end of sentence (;)", token); } } else { AddError("Waiting assign operator (=) or end of command (;)", token); } } else { AddError("Waiting variable name", token); } }