Exemple #1
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);
        }
Exemple #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);
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
0
        private Ast.SqlStmt ParseSqlStmt(TokenQueue q, string rootProdName = "sql-stmt")
        {
            var start = q.GetLocation();
            var tok   = q.SourceToken;

            Ast.SqliteSyntaxProduction syntaxNode;
            var result = SqliteParser.ReadStmt(q, out syntaxNode, rootProdName);

            if (result.IsValid)
            {
                var stmt = new Ast.SqlStmt {
                    Sql          = q.Substring(start, result.NumValidTokens),
                    SourceToken  = tok,
                    SqliteSyntax = syntaxNode
                };
                _preprocessor.PreprocessStmt(stmt);
                return(stmt);
            }
            else if (result.InvalidMessage != null)
            {
                throw new SyntaxException(result.InvalidMessage);
            }
            else
            {
                throw new SyntaxException(q);
            }
        }
Exemple #7
0
        private Ast.Expr ParseExpr(TokenQueue q)
        {
            var start = q.GetLocation();
            var tok   = q.SourceToken;

            Ast.SqliteSyntaxProduction syntaxNode;
            var result = SqliteParser.ReadExpr(q, out syntaxNode);

            if (result.IsValid)
            {
                return(new Ast.Expr {
                    Sql = q.Substring(start, result.NumValidTokens),
                    SourceToken = tok,
                    SqliteSyntax = syntaxNode
                });
            }
            else if (result.InvalidMessage != null)
            {
                throw new SyntaxException(result.InvalidMessage);
            }
            else
            {
                throw new SyntaxException(q);
            }
        }
Exemple #8
0
 private void ConsumeSemicolon(TokenQueue q)
 {
     if (q.Peek() == ";")
     {
         q.Take();
     }
 }
Exemple #9
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);
        }
Exemple #10
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);
        }
Exemple #11
0
        private Ast.Stmt ParseContinueStmt(TokenQueue q)
        {
            var stmt = new Ast.ContinueStmt {
                SourceToken = q.SourceToken
            };

            q.Take("continue");
            ConsumeSemicolon(q);
            return(stmt);
        }
Exemple #12
0
        private Ast.Stmt ParseBreakStmt(TokenQueue q)
        {
            var stmt = new Ast.BreakStmt {
                SourceToken = q.SourceToken
            };

            q.Take("break");
            ConsumeSemicolon(q);
            return(stmt);
        }
Exemple #13
0
 private void ParseAssignmentStmtCore(TokenQueue q, Ast.AssignmentStmt stmt)
 {
     stmt.VariableName = ParseVariableName(q);
     if (q.Peek() == "=")
     {
         q.Take();
         stmt.InitialValue = ParseExpr(q);
     }
     ConsumeSemicolon(q);
 }
Exemple #14
0
        private Ast.Stmt ParseSetStmt(TokenQueue q)
        {
            var stmt = new Ast.SetStmt {
                SourceToken = q.SourceToken
            };

            q.Take("set");
            ParseAssignmentStmtCore(q, stmt);
            return(stmt);
        }
Exemple #15
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)}\"");
            }
        }
Exemple #16
0
        // ----

        private static T Check <T>(TokenQueue q, T shouldBeNonNull)
        {
            if (shouldBeNonNull == null)
            {
                throw new SyntaxException(q);
            }
            else
            {
                return(shouldBeNonNull);
            }
        }
Exemple #17
0
        private bool PeekExpr(TokenQueue q)
        {
            var start = q.GetLocation();
            var tok   = q.SourceToken;

            Ast.SqliteSyntaxProduction ast;
            var result = SqliteParser.ReadExpr(q, out ast);

            q.Jump(start);
            return(result.IsValid);
        }
Exemple #18
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);
        }
Exemple #19
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);
        }
Exemple #20
0
        private string ParseIdentifier(TokenQueue q)   // or null
        {
            var token = q.PeekToken();

            if (token.Type == TokenType.Id)
            {
                return(q.Take().GetUnescapedText());
            }
            else
            {
                return(null);
            }
        }
Exemple #21
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);
            }
        }
Exemple #22
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);
        }
Exemple #23
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);
        }
        private Ast.Stmt ParseThrowStmt(TokenQueue q)
        {
            var stmt = new Ast.ThrowStmt {
                SourceToken = q.SourceToken
            };

            q.Take("throw");
            if (PeekExpr(q))
            {
                stmt.HasErrorValues = true;
                stmt.Message        = ParseExpr(q);
            }
            ConsumeSemicolon(q);
            return(stmt);
        }
Exemple #25
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);
        }
Exemple #26
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);
        }
Exemple #27
0
 public override MatchResult?MatchStep(MatchStack stack, MatchFrame frame, TokenQueue q)
 {
     if (!frame.ProdMatched)
     {
         if (_prod == null)
         {
             _prod = SqliteGrammar.Prods[ProdName];
         }
         stack.Push(_prod);
         frame.ProdMatched = true;
         return(null);
     }
     else
     {
         return(frame.SubResult);
     }
 }
Exemple #28
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);
        }
Exemple #29
0
 public override MatchResult?MatchStep(MatchStack stack, MatchFrame frame, TokenQueue q)
 {
     if (frame.OrState == OrTermState.Start)
     {
         // try to match the first sub-production
         stack.Push(Prods[0]);
         frame.OrState     = OrTermState.Match;
         frame.OrProdIndex = 0;
         frame.OrStartLoc  = q.GetLocation();
         return(null);
     }
     else if (frame.OrState == OrTermState.Match)
     {
         // we have finished matching one of the productions.  if it matched, then we're done.  if not, move on
         // to the next production.
         var result = frame.SubResult;
         if (result.IsMatch)
         {
             return(MatchResult.Matched);
         }
         else if (result.ErrorMessage == null)
         {
             // no match.  rewind to the beginning and retry with the next one.
             q.Jump(frame.OrStartLoc);
             frame.OrProdIndex++;
             if (frame.OrProdIndex >= Prods.Length)
             {
                 // we have exhausted all of the possibilities and none of them matched.
                 return(MatchResult.NoMatch);
             }
             stack.Push(Prods[frame.OrProdIndex]);
             return(null);
         }
         else
         {
             // started to match but mismatched past the point of no return.
             return(result);
         }
     }
     else
     {
         throw new Exception($"Unrecognized state: {frame.OrState}");
     }
 }
Exemple #30
0
        private Ast.Script ParseScript(TokenQueue q)
        {
            var script = new Ast.Script {
                SourceToken = q.SourceToken
            };

            script.Block = new Ast.Block {
                SourceToken = q.SourceToken
            };
            while (!q.Eof())
            {
                var stmt = ParseStmt(q);
                if (stmt != null)
                {
                    script.Block.Statements.Add(stmt);
                }
            }
            return(script);
        }