コード例 #1
0
        private Ast.ExportTxtStmt ParseExportTxtStmt(TokenQueue q)   // or null
        {
            var stmt = new Ast.ExportTxtStmt {
                SourceToken = q.SourceToken
            };
            var start = q.GetLocation();

            if (!q.TakeMaybe("export"))
            {
                q.Jump(start); return(null);
            }
            if (!q.TakeMaybe("txt", "text"))
            {
                q.Jump(start); return(null);
            }
            stmt.FilenameExpr = ParseExpr(q);
            q.Take("from");
            q.Take("(");
            stmt.SelectStmt = ParseSqlStmt(q, "select-stmt");
            q.Take(")");
            if (q.TakeMaybe("options"))
            {
                stmt.OptionsList = Check(q, ParseOptionsList(q));
            }
            ConsumeSemicolon(q);
            return(stmt);
        }
コード例 #2
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);
        }
コード例 #3
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);
        }
コード例 #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);
        }
コード例 #5
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);
        }
コード例 #6
0
        public override MatchResult?MatchStep(MatchStack stack, MatchFrame frame, TokenQueue q)
        {
            var type = q.Take().Type;

            return(type == TokenType.Id || (AllowVariable && type == TokenType.Variable)
                ? MatchResult.Matched : MatchResult.NoMatch);
        }
コード例 #7
0
 private void ConsumeSemicolon(TokenQueue q)
 {
     if (q.Peek() == ";")
     {
         q.Take();
     }
 }
コード例 #8
0
        public override MatchResult?MatchStep(MatchStack stack, MatchFrame frame, TokenQueue q)
        {
            int idx   = (int)q.Take().Type;
            var match = idx >= 0 && idx < _bitmap.Length && _bitmap[idx];

            return(match ? MatchResult.Matched : MatchResult.NoMatch);
        }
コード例 #9
0
        private Ast.ImportXlsStmt ParseImportXlsStmt(TokenQueue q)   // or null
        {
            var stmt = new Ast.ImportXlsStmt {
                SourceToken = q.SourceToken
            };
            var start = q.GetLocation();

            if (!q.TakeMaybe("import"))
            {
                q.Jump(start); return(null);
            }
            if (!q.TakeMaybe("xls", "xlsx"))
            {
                q.Jump(start); return(null);
            }
            stmt.FilenameExpr = ParseExpr(q);
            if (q.TakeMaybe("worksheet"))
            {
                stmt.WhichSheetExpr = ParseExpr(q);
            }
            q.Take("into");
            stmt.ImportTable = Check(q, ParseImportTable(q));
            if (q.TakeMaybe("options"))
            {
                stmt.OptionsList = Check(q, ParseOptionsList(q));
            }
            ConsumeSemicolon(q);
            return(stmt);
        }
コード例 #10
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);
        }
コード例 #11
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);
        }
コード例 #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);
        }
コード例 #13
0
        private Ast.Stmt ParseThrowStmt(TokenQueue q)
        {
            var stmt = new Ast.ThrowStmt {
                SourceToken = q.SourceToken
            };

            q.Take("throw");
            if (PeekExpr(q))
            {
                stmt.HasErrorValues = true;
                stmt.ErrorNumber    = ParseExpr(q);
                q.Take(",");
                stmt.Message = ParseExpr(q);
                q.Take(",");
                stmt.State = ParseExpr(q);
            }
            ConsumeSemicolon(q);
            return(stmt);
        }
コード例 #14
0
        private Ast.Stmt ParseSetStmt(TokenQueue q)
        {
            var stmt = new Ast.SetStmt {
                SourceToken = q.SourceToken
            };

            q.Take("set");
            ParseAssignmentStmtCore(q, stmt);
            return(stmt);
        }
コード例 #15
0
 private void ParseAssignmentStmtCore(TokenQueue q, Ast.AssignmentStmt stmt)
 {
     stmt.VariableName = ParseVariableName(q);
     if (q.Peek() == "=")
     {
         q.Take();
         stmt.InitialValue = ParseExpr(q);
     }
     ConsumeSemicolon(q);
 }
コード例 #16
0
        private Ast.Stmt ParseContinueStmt(TokenQueue q)
        {
            var stmt = new Ast.ContinueStmt {
                SourceToken = q.SourceToken
            };

            q.Take("continue");
            ConsumeSemicolon(q);
            return(stmt);
        }
コード例 #17
0
        private Ast.Stmt ParseBreakStmt(TokenQueue q)
        {
            var stmt = new Ast.BreakStmt {
                SourceToken = q.SourceToken
            };

            q.Take("break");
            ConsumeSemicolon(q);
            return(stmt);
        }
コード例 #18
0
        private Ast.Stmt ParseForStmt(TokenQueue q)
        {
            var stmt = new Ast.ForStmt {
                SourceToken = q.SourceToken
            };

            q.Take("for");
            stmt.VariableName = ParseVariableName(q);
            q.Take("=");
            stmt.FirstNumberExpr = ParseExpr(q);
            q.Take("to");
            stmt.LastNumberExpr = ParseExpr(q);
            if (q.TakeMaybe("step"))
            {
                stmt.StepExpr = ParseExpr(q);
            }
            stmt.Block = ParseBlock(q);
            ConsumeSemicolon(q);
            return(stmt);
        }
コード例 #19
0
        private Ast.OptionsList ParseOptionsList(TokenQueue q)   // or null
        {
            var n = new Ast.OptionsList {
                SourceToken = q.SourceToken
            };

            if (!q.TakeMaybe("("))
            {
                return(null);
            }
            do
            {
                var key = Check(q, ParseIdentifier(q)).ToLower();
                q.Take(":");
                var value = ParseExpr(q);
                n.Options[key] = value;
            } while (q.TakeMaybe(","));
            q.Take(")");
            return(n);
        }
コード例 #20
0
        private Ast.Stmt ParsePrintStmt(TokenQueue q)
        {
            var stmt = new Ast.PrintStmt {
                SourceToken = q.SourceToken
            };

            q.Take("print");
            stmt.Value = ParseExpr(q);
            ConsumeSemicolon(q);
            return(stmt);
        }
コード例 #21
0
        private Ast.Stmt ParseWhileStmt(TokenQueue q)
        {
            var stmt = new Ast.WhileStmt {
                SourceToken = q.SourceToken
            };

            q.Take("while");
            stmt.Condition = ParseExpr(q);
            stmt.Block     = ParseBlock(q);
            ConsumeSemicolon(q);
            return(stmt);
        }
コード例 #22
0
        private string ParseIdentifier(TokenQueue q)   // or null
        {
            var token = q.PeekToken();

            if (token.Type == TokenType.Id)
            {
                return(q.Take().GetUnescapedText());
            }
            else
            {
                return(null);
            }
        }
コード例 #23
0
        private Ast.Stmt ParseReturnStmt(TokenQueue q)
        {
            var stmt = new Ast.ReturnStmt {
                SourceToken = q.SourceToken
            };

            q.Take("return");
            if (PeekExpr(q))
            {
                stmt.Value = ParseExpr(q);
            }
            ConsumeSemicolon(q);
            return(stmt);
        }
コード例 #24
0
        private static string ParseVariableName(TokenQueue q)
        {
            var t = q.PeekToken();

            if (t.Type == TokenType.Variable && IsVariableName(t.GetUnescapedText()))
            {
                q.Take();
                return(t.GetUnescapedText());
            }
            else
            {
                throw new SyntaxException(new[] { "variable name starting with @ $ :" }, q);
            }
        }
コード例 #25
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);
        }
コード例 #26
0
        private Ast.IdentifierOrExpr ParseIdentifierOrExpr(TokenQueue q)   // or null
        {
            var token = q.PeekToken();

            if (token.Type == TokenType.Id)
            {
                q.Take();
                return(new Ast.IdentifierOrExpr {
                    SourceToken = token, Identifier = token.GetUnescapedText()
                });
            }
            else if (PeekExpr(q))
            {
                return(new Ast.IdentifierOrExpr {
                    SourceToken = token, Expr = ParseExpr(q)
                });
            }
            else
            {
                return(null);
            }
        }
コード例 #27
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));
            }
        }
コード例 #28
0
 public override MatchResult?MatchStep(MatchStack stack, MatchFrame frame, TokenQueue q)
 {
     return(q.Take().Type == TokenType.String ? MatchResult.Matched : MatchResult.NoMatch);
 }
コード例 #29
0
 public override MatchResult?MatchStep(MatchStack stack, MatchFrame frame, TokenQueue q)
 {
     return(q.Take().Text.ToLower() == Text.ToLower() ? MatchResult.Matched : MatchResult.NoMatch);
 }