Example #1
0
        private Ast.ImportColumn ParseImportColumn(TokenQueue q)
        {
            var n = new Ast.ImportColumn {
                SourceToken = q.SourceToken
            };

            Check(q, n.ColumnName = ParseIdentifierOrExpr(q));
            if (q.Peek() == "as")
            {
                q.Take("as");
                n.AsName = Check(q, ParseIdentifierOrExpr(q));
            }
            switch (q.Peek())
            {
            case "text": q.Take(); n.TypeConversion = Ast.TypeConversion.Text; break;

            case "integer": q.Take(); n.TypeConversion = Ast.TypeConversion.Integer; break;

            case "real": q.Take(); n.TypeConversion = Ast.TypeConversion.Real; break;

            case "date": q.Take(); n.TypeConversion = Ast.TypeConversion.Date; break;

            case "datetime": q.Take(); n.TypeConversion = Ast.TypeConversion.DateTime; break;

            case "datetimeoffset": q.Take(); n.TypeConversion = Ast.TypeConversion.DateTimeOffset; break;
            }
            return(n);
        }
Example #2
0
        private Ast.Block ParseBlock(TokenQueue q)
        {
            var stmt = new Ast.Block {
                SourceToken = q.SourceToken
            };

            if (q.Peek() == "begin")
            {
                q.Take("begin");
                while (q.Peek() != "end")
                {
                    var blockStmt = ParseStmt(q);
                    if (blockStmt != null)
                    {
                        stmt.Statements.Add(blockStmt);
                    }
                }
                q.Take("end");
            }
            else
            {
                var blockStmt = ParseStmt(q);
                if (blockStmt != null)
                {
                    stmt.Statements.Add(blockStmt);
                }
            }
            return(stmt);
        }
Example #3
0
        private Ast.Stmt ParseExportStmt(TokenQueue q)
        {
            switch (q.Peek(1))
            {
            case "txt":
            case "text": return(Check(q, ParseExportTxtStmt(q)));

            default: throw new SyntaxException($"Unknown export type: \"{q.Peek(1)}\"");
            }
        }
Example #4
0
        private Ast.Stmt ParseExecuteStmt(TokenQueue q)
        {
            var stmt = new Ast.ExecuteStmt {
                SourceToken = q.SourceToken
            };

            q.Take("exec", "execute");

            if (q.Peek(1) == "=")
            {
                stmt.ReturnVariableName = ParseVariableName(q);
                q.Take("=");
            }

            if (q.PeekToken().Type == TokenType.String || q.PeekToken().Type == TokenType.Id)
            {
                stmt.ScriptName = q.Take().GetUnescapedText();
            }
            else
            {
                throw new SyntaxException(new[] { "string", "identifier" }, q);
            }

            if (IsVariableName(q.PeekToken()?.GetUnescapedText() ?? "") && q.Peek(1) == "=")
            {
                while (true)
                {
                    var arg = new Ast.ArgumentPair();
                    arg.Name = ParseVariableName(q);
                    q.Take("=");
                    if (q.Peek() == "default")
                    {
                        q.Take();
                    }
                    else
                    {
                        arg.Value = ParseExpr(q);
                    }
                    stmt.Arguments.Add(arg);
                    if (!q.TakeMaybe(","))
                    {
                        break;
                    }
                }
            }

            ConsumeSemicolon(q);
            return(stmt);
        }
Example #5
0
 private void ConsumeSemicolon(TokenQueue q)
 {
     if (q.Peek() == ";")
     {
         q.Take();
     }
 }
Example #6
0
        private Ast.ImportTxtStmt ParseImportTxtStmt(TokenQueue q)   // or null
        {
            var stmt = new Ast.ImportTxtStmt {
                SourceToken = q.SourceToken
            };
            var start = q.GetLocation();

            if (!q.TakeMaybe("import"))
            {
                q.Jump(start); return(null);
            }
            if (!q.TakeMaybe("txt", "text"))
            {
                q.Jump(start); return(null);
            }
            stmt.FilenameExpr = ParseExpr(q);
            q.Take("into");
            stmt.TableName = Check(q, ParseIdentifierOrExpr(q));
            if (q.Peek() == "(")
            {
                q.Take("(");
                stmt.LineNumberColumnName = Check(q, ParseIdentifierOrExpr(q));
                q.Take(",");
                stmt.TextColumnName = Check(q, ParseIdentifierOrExpr(q));
                q.Take(")");
            }
            if (q.TakeMaybe("options"))
            {
                stmt.OptionsList = Check(q, ParseOptionsList(q));
            }
            ConsumeSemicolon(q);
            return(stmt);
        }
Example #7
0
 private void ParseAssignmentStmtCore(TokenQueue q, Ast.AssignmentStmt stmt)
 {
     stmt.VariableName = ParseVariableName(q);
     if (q.Peek() == "=")
     {
         q.Take();
         stmt.InitialValue = ParseExpr(q);
     }
     ConsumeSemicolon(q);
 }
Example #8
0
        private Ast.Stmt ParseTryCatchStmt(TokenQueue q)
        {
            var stmt = new Ast.TryCatchStmt {
                SourceToken = q.SourceToken
            };

            q.Take("begin");
            q.Take("try");
            stmt.TryBlock = new Ast.Block {
                SourceToken = q.SourceToken
            };
            while (q.Peek() != "end")
            {
                var tryStmt = ParseStmt(q);
                if (tryStmt != null)
                {
                    stmt.TryBlock.Statements.Add(tryStmt);
                }
            }
            q.Take("end");
            q.Take("try");

            q.Take("begin");
            q.Take("catch");
            stmt.CatchBlock = new Ast.Block {
                SourceToken = q.SourceToken
            };
            while (q.Peek() != "end")
            {
                var catchStmt = ParseStmt(q);
                if (catchStmt != null)
                {
                    stmt.CatchBlock.Statements.Add(catchStmt);
                }
            }
            q.Take("end");
            q.Take("catch");
            return(stmt);
        }
Example #9
0
        private Ast.Stmt ParseStmt(TokenQueue q)   // or null
        {
            switch (q.Peek(0))
            {
            case ";": q.Take(";"); return(null);

            case "declare": return(ParseDeclareStmt(q));

            case "while": return(ParseWhileStmt(q));

            case "break": return(ParseBreakStmt(q));

            case "continue": return(ParseContinueStmt(q));

            case "print": return(ParsePrintStmt(q));

            case "exec":
            case "execute": return(ParseExecuteStmt(q));

            case "return": return(ParseReturnStmt(q));

            case "throw": return(ParseThrowStmt(q));

            case "set": return(ParseSetStmt(q));

            case "if": return(ParseIfStmt(q));

            case "begin": return(q.Peek(1) == "try" ? ParseTryCatchStmt(q) : ParseSqlStmt(q));

            case "import": return(ParseImportStmt(q));

            case "export": return(ParseExportStmt(q));

            case "for": return(ParseForStmt(q));

            default: return(ParseSqlStmt(q));
            }
        }
Example #10
0
        private Ast.DeclareStmt ParseDeclareStmt(TokenQueue q)
        {
            var stmt = new Ast.DeclareStmt {
                SourceToken = q.SourceToken
            };

            q.Take("declare");
            if (q.Peek() == "parameter")
            {
                q.Take();
                stmt.IsParameter = true;
            }
            ParseAssignmentStmtCore(q, stmt);
            return(stmt);
        }
Example #11
0
        private Ast.Stmt ParseIfStmt(TokenQueue q)
        {
            var stmt = new Ast.IfStmt {
                SourceToken = q.SourceToken
            };

            q.Take("if");
            stmt.Condition = ParseExpr(q);
            stmt.Block     = ParseBlock(q);
            if (q.Peek() == "else")
            {
                q.Take("else");
                stmt.ElseBlock = ParseBlock(q);
            }
            return(stmt);
        }
Example #12
0
        private Ast.ImportTable ParseImportTable(TokenQueue q)
        {
            var n = new Ast.ImportTable {
                SourceToken = q.SourceToken
            };

            n.TableName = Check(q, ParseIdentifierOrExpr(q));
            if (q.Peek() == "(")
            {
                q.Take("(");
                do
                {
                    n.ImportColumns.Add(Check(q, ParseImportColumn(q)));
                } while (q.TakeMaybe(","));
                q.Take(")");
            }
            return(n);
        }