Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 8
0
 private void ConsumeSemicolon(TokenQueue q)
 {
     if (q.Peek() == ";")
     {
         q.Take();
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 11
0
        private Ast.Stmt ParseContinueStmt(TokenQueue q)
        {
            var stmt = new Ast.ContinueStmt {
                SourceToken = q.SourceToken
            };

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

            q.Take("break");
            ConsumeSemicolon(q);
            return(stmt);
        }
Exemplo n.º 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);
 }
Exemplo n.º 14
0
        private Ast.Stmt ParseSetStmt(TokenQueue q)
        {
            var stmt = new Ast.SetStmt {
                SourceToken = q.SourceToken
            };

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

        private static T Check <T>(TokenQueue q, T shouldBeNonNull)
        {
            if (shouldBeNonNull == null)
            {
                throw new SyntaxException(q);
            }
            else
            {
                return(shouldBeNonNull);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 24
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.Message        = ParseExpr(q);
            }
            ConsumeSemicolon(q);
            return(stmt);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 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}");
     }
 }
Exemplo n.º 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);
        }