Exemple #1
0
 void Test(string source, string expectedResult)
 {
     try
     {
         Tokenizer    lexer           = new Tokenizer(new StringReader(source), "TokenizerTest");
         SyntaxParser parser          = new SyntaxParser();
         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         bool addSpace = false;
         do
         {
             SObject result = SyntaxParser.Parse(lexer);
             if (result == null)
             {
                 break;
             }
             if (addSpace)
             {
                 sb.Append(" ");
             }
             else
             {
                 addSpace = true;
             }
             sb.Append(Inspector.InspectObject(result));
         } while (lexer.LastToken != null);
         string sresult = sb.ToString();
         Assert.AreEqual(expectedResult, sresult);
     }
     catch (BaseSchemeError ex)
     {
         Debug.LogError(string.Format("Source: {0}\n{1}\n{2}", source, ex.Message, ex.StackTrace));
     }
 }
Exemple #2
0
        public void ParseComplexColumnConcat()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        SELECT '+61' || SUBSTR(svc_idnty, 1, 8) || '0'  FROM FPC_SERVICE  
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.AreNotEqual(null, oSelect);
            Assert.AreEqual(2, oSelect.Count);

            SyntaxNode oColumnList = oSelect[0];

            Assert.AreEqual(1, oColumnList.Count);
            Assert.AreEqual(SyntaxKind.ColumnListNode, oColumnList.ExpectedType);

            SyntaxNode oBarBar = oColumnList[0];

            Assert.AreEqual(3, oBarBar.Count);
            Assert.AreEqual(SyntaxKind.BarBarToken, oBarBar.ExpectedType);
            Assert.AreEqual(SyntaxKind.LiteralToken, oBarBar[0].ExpectedType);
            Assert.AreEqual(SyntaxKind.SubStringKeyword, oBarBar[1].ExpectedType);
            Assert.AreEqual(SyntaxKind.LiteralToken, oBarBar[2].ExpectedType);
        }
Exemple #3
0
        public void ParseBadColumnCommentFROM()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"
                        SELECT  
                        * --bad comment FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc      
            ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);
            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.AreNotEqual(oSelect, null);

            SyntaxNode oFrom = oSelect.FindFirst(SyntaxKind.FromKeyword);

            Assert.AreNotEqual(oFrom, null);

            SyntaxNode oTableIdn = oFrom.FindFirst(SyntaxKind.IdentifierTableSymbol);

            Assert.AreNotEqual(oTableIdn, null);
        }
Exemple #4
0
        public void ParseBadColumnCommentWHERE()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"
                        SELECT 
                        * --bad comment 
                        FROM APSHARE_FPVIEWS..FPC_SERVICE svc    
                        -- WHERE x  
            ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);
            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.AreNotEqual(oSelect, null);

            SyntaxNode oFrom = oSelect.FindFirst(SyntaxKind.FromKeyword);

            Assert.AreNotEqual(oFrom, null);

            SyntaxNode oWhere = oSelect.FindFirst(SyntaxKind.WhereKeyword);

            Assert.AreEqual(oWhere, null); // Should not be a where, They probably wanted this commented out
        }
Exemple #5
0
        public void ParseStringLiteralColumn()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                        'TEST'
                        FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc          
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);
            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.AreNotEqual(null, oSelect);

            SyntaxNode oColumnList = oSelect[0];

            Assert.AreEqual(1, oColumnList.Count); // Should be 1 column
            Assert.AreEqual(SyntaxKind.ColumnListNode, oColumnList.ExpectedType);

            SyntaxNode oLiteralColumn = oColumnList[0];

            Assert.AreEqual(SyntaxKind.LiteralToken, oLiteralColumn.ExpectedType);
        }
Exemple #6
0
        public void LexSelectExpression()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                        COUNT(*)
                        FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc     
                             
                ");

            SyntaxParser oParser = new SyntaxParser(oText);

            // Should be 8 (including 1 for the EOF Token)
            Assert.AreEqual(11, oParser.TokenList.Count);
            // First item should be a select keyword (ignoring all comments which is trivia)
            Assert.AreEqual(oParser.TokenList[0].ExpectedType, SyntaxKind.SelectKeyword);
            // COUNT
            Assert.AreEqual(oParser.TokenList[1].ExpectedType, SyntaxKind.CountKeyword);
            // (
            Assert.AreEqual(oParser.TokenList[2].ExpectedType, SyntaxKind.OpenParenthesisToken);
            // (
            Assert.AreEqual(oParser.TokenList[3].ExpectedType, SyntaxKind.StarToken);
            // )
            Assert.AreEqual(oParser.TokenList[4].ExpectedType, SyntaxKind.CloseParenthesisToken);
        }
        public void MultiLineComments()
        {
            string sText =
                @"/*Комментарий create 
create
-- function (*/x/**/function/*
 xxx";
            List <string> lines = this.GetLines(sText);

            SyntaxParser oParser        = new SyntaxParser(new MsSqlSyntaxSettings(), 4);
            List <Token> tokens         = oParser.Parse(lines);
            List <Token> expectedTokens = new List <Token>(new Token[]
            {
                new Token("/*Комментарий create ", new TextPoint(0, 0, 0), new TextPoint(0, 21, 21), new TextStyle("MultiLine Comment", Color.Black)),
                new Token("create", new TextPoint(1, 0, 0), new TextPoint(1, 6, 6), new TextStyle("MultiLine Comment", Color.Black)),
                new Token("-- function (*/", new TextPoint(2, 0, 0), new TextPoint(2, 15, 15), new TextStyle("MultiLine Comment", Color.Black)),
                new Token("x", new TextPoint(2, 15, 15), new TextPoint(2, 16, 16), new TextStyle("Normal", Color.Black)),
                new Token("/**/", new TextPoint(2, 16, 16), new TextPoint(2, 20, 20), new TextStyle("MultiLine Comment", Color.Black)),
                new Token("function", new TextPoint(2, 20, 20), new TextPoint(2, 28, 28), new TextStyle("Keywords1", Color.Black)),
                new Token("/*", new TextPoint(2, 28, 28), new TextPoint(2, 30, 30), new TextStyle("MultiLine Comment", Color.Black)),
                new Token(" xxx", new TextPoint(3, 0, 0), new TextPoint(3, 4, 4), new TextStyle("MultiLine Comment", Color.Black))
            });

            for (int i = 0; i < tokens.Count; i++)
            {
                Token oToken1 = tokens[i];
                Token oToken2 = expectedTokens[i];
                Assert.AreEqual(oToken1.Text, oToken2.Text);
                Assert.AreEqual(oToken1.Style.Name, oToken2.Style.Name);
                Assert.AreEqual(oToken1.Start.ToString(), oToken2.Start.ToString());
                Assert.AreEqual(oToken1.End.ToString(), oToken2.End.ToString());
            }
        }
Exemple #8
0
        public void ParseMultipleColumnParenthesised()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                        (col1), (col2), (col3)
                        FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc          
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);
            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.AreNotEqual(oSelect, null);

            SyntaxNode oColList = oSelect[0];

            Assert.AreEqual(3, oColList.Count);

            foreach (SyntaxNode oChild in oColList)
            {
                Assert.AreEqual(oChild.ExpectedType, SyntaxKind.OpenParenthesisToken);
                SyntaxNode oColInner = oChild[0];
                Assert.IsTrue(
                    oColInner.ExpectedType == SyntaxKind.StarToken ||
                    oColInner.ExpectedType == SyntaxKind.IdentifierColumnSymbol ||
                    oColInner.ExpectedType == SyntaxKind.IdentifierTableSymbol);
            }
        }
Exemple #9
0
        /// <summary>
        /// Merge given metric with new metric created from parser.
        /// </summary>
        /// <param name="resolveOccurrences"></param>
        /// <param name="metric"></param>
        /// <param name="parser"></param>
        /// <returns></returns>
        private static MetricInfo MergeWithParser(bool resolveOccurrences, MetricInfo metric,
                                                  SyntaxParser parser)
        {
            var metricInfo = new MetricInfo(resolveOccurrences, parser);

            return(metric.Merge(metricInfo));
        }
Exemple #10
0
        public void ParseAdditionInColumn()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                        svc_idnty + 2
                        FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc          
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);
            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.IsTrue(oSelect != null);

            SyntaxNode oColumnList = oSelect.FindFirst(SyntaxKind.ColumnListNode);

            Assert.AreNotEqual(oColumnList, null);
            SyntaxNode oStarExpression = oSelect.FindFirst(SyntaxKind.PlusToken);

            Assert.AreNotEqual(oStarExpression, null);

            SyntaxNode oLeft  = oStarExpression[0];
            SyntaxNode oRight = oStarExpression[1];

            Assert.AreEqual(oLeft.ExpectedType, SyntaxKind.IdentifierTableSymbol);
            Assert.AreEqual(oRight.ExpectedType, SyntaxKind.NumericToken);
        }
        public void Parse(string identifier)
        {
            this.classificationSpans.Clear();
            var syntaxParser = new SyntaxParser(this);

            syntaxParser.Parse(identifier);
        }
Exemple #12
0
        private CompiledMethod ParseAndCompileSingleMethod(string source)
        {
            var sourceBytes = Encoding.UTF8.GetBytes(source);
            var diagnostics = new BenchmarkDiagnosticsSink();

            // Parse the source
            const string sourceFilename = "test.cle";
            var          syntaxTree     = SyntaxParser.Parse(sourceBytes.AsMemory(), sourceFilename, diagnostics);

            if (syntaxTree is null || syntaxTree.Functions.Count != 1)
            {
                throw new InvalidOperationException("Expected a single method");
            }

            // Compile the declaration
            var declarationProvider = new NullDeclarationProvider();
            var declaration         = MethodDeclarationCompiler.Compile(syntaxTree.Functions[0],
                                                                        syntaxTree.Namespace, sourceFilename,
                                                                        0, declarationProvider, diagnostics);

            // Compile the method body
            var result = new MethodCompiler(declarationProvider, diagnostics)
                         .CompileBody(syntaxTree.Functions[0], declaration !, syntaxTree.Namespace, sourceFilename);

            if (diagnostics.DiagnosticCount > 0)
            {
                throw new InvalidOperationException("Expected no diagnostics");
            }
            return(result !);
        }
Exemple #13
0
        public void ParseParenthesisedExpression()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */
                        SELECT
                            col1, col2
                        FROM
                            APMART_FP.ADMIN.FPC_SERVICE svc
                        WHERE (svc.MKT_PROD_CD = 'MOB PT' AND svc.SVC_STAT_CD<> 'C')    
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            // Test that a subquery type node was built
            SyntaxNode oWhere = oTemp.FindFirst(SyntaxKind.WhereKeyword);

            Assert.IsTrue(oWhere != null);

            SyntaxNode oParenthesis = oWhere[0];

            Assert.AreEqual(oParenthesis.ExpectedType, SyntaxKind.OpenParenthesisToken);

            // Test that there is a select keyword in that subquery
            SyntaxNode oAND = oParenthesis[0];

            Assert.AreEqual(oAND.ExpectedType, SyntaxKind.AndKeyword);
        }
Exemple #14
0
        public void ParseInvalidColumnPreConcat()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        SELECT || svc.svc_idnty FROM FPC_SERVICE
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.AreNotEqual(null, oSelect);
            Assert.AreEqual(2, oSelect.Count);

            SyntaxNode oColumnList = oSelect[0];

            Assert.AreEqual(1, oColumnList.Count);
            Assert.AreEqual(SyntaxKind.ColumnListNode, oColumnList.ExpectedType);

            SyntaxNode oBarBar = oColumnList[0];

            Assert.AreEqual(2, oBarBar.Count);
            Assert.AreEqual(SyntaxKind.BarBarToken, oBarBar.ExpectedType);
        }
Exemple #15
0
        public void LexINNERJOINStatementBasic()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                        *
                        FROM
                        APMART_FPVIEWS..FPC_SERVICE svc
                        INNER JOIN 
                            APSHARE_FP..WR02290_FETCH ftch
                            ON svc.SVC_IDNTY = ftch.SERVICE_NO
                        WHERE svc.MKT_PROD_CD = 'A' ;            
                ");

            SyntaxParser oParser = new SyntaxParser(oText);

            // Should be 15 (including 1 for the semicolon)
            Assert.AreEqual(oParser.TokenList.Count, 29);
            // First item should be a select keyword (ignoring all comments which is trivia)
            Assert.AreEqual(oParser.TokenList[0].ExpectedType, SyntaxKind.SelectKeyword);

            // We only really test for the JOIN and its positioning
            Assert.AreEqual(oParser.TokenList[7].ExpectedType, SyntaxKind.InnerJoinKeyword);
            Assert.AreEqual(oParser.TokenList[8].ExpectedType, SyntaxKind.JoinKeyword);
        }
Exemple #16
0
        public void ParseSelectFromDBTable()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                        *
                        FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc          
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);
            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.IsTrue(oSelect != null);

            SyntaxNode oFrom = oSelect.FindFirst(SyntaxKind.FromKeyword);

            Assert.IsTrue(oFrom != null);

            SyntaxNode oTableIdn = oFrom.FindFirst(SyntaxKind.IdentifierTableSymbol);

            Assert.IsTrue(oTableIdn != null);
        }
Exemple #17
0
        public void LexWITHStatementBasic()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        WITH _X AS
                        (
                            SELECT * FROM APMART_FPVIEWS..FPC_SERVICE LIMIT 100
                        )
                        SELECT  
                        *
                        FROM
                            APSHARE_FPVIEWS..FPC_SERVICE svc   
                        INNER JOIN
                            _X
                            ON _X.SVC_IDNTY = svc.SVC_IDNTY   
                ");

            SyntaxParser oParser = new SyntaxParser(oText);

            // WITH
            Assert.AreEqual(oParser.TokenList[0].ExpectedType, SyntaxKind.WithKeyword);
            Assert.AreEqual(oParser.TokenList[0].RawSQLText, "WITH");

            // Identifier
            Assert.AreEqual(oParser.TokenList[1].ExpectedType, SyntaxKind.IdentifierToken);
            Assert.AreEqual(oParser.TokenList[1].RawSQLText, "_X");

            // Brackets
            Assert.AreEqual(oParser.TokenList[3].ExpectedType, SyntaxKind.OpenParenthesisToken);
            Assert.AreEqual(oParser.TokenList[3].RawSQLText, "(");
            Assert.AreEqual(oParser.TokenList[12].ExpectedType, SyntaxKind.CloseParenthesisToken);
            Assert.AreEqual(oParser.TokenList[12].RawSQLText, ")");
        }
Exemple #18
0
        public void ParseSelectColumnAlias()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                        col1 AS c2
                        FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc          
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);
            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.IsTrue(oSelect != null);

            SyntaxNode oColumnList = oSelect[0];

            Assert.AreEqual(1, oColumnList.Count); // Should be 1 column
            Assert.AreEqual(SyntaxKind.ColumnListNode, oColumnList.ExpectedType);

            SyntaxNode oLiteralColumn = oColumnList[0];

            Assert.AreEqual(SyntaxKind.IdentifierTableSymbol, oLiteralColumn.ExpectedType);
            Assert.AreEqual(typeof(Symbol), oLiteralColumn.GetType()); // Table is the top level then col
            Assert.AreEqual("c2", ((Symbol)oLiteralColumn).Alias);
        }
Exemple #19
0
        public void ParseBasicJoinTree()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                        *
                        FROM
                            APMART_FPVIEWS..FPC_SERVICE svc  
                        INNER JOIN
                            APSHARE_FP..WR02052_OMR_BASE omr
                        ON svc.SVC_IDNTY = omr.SVC_IDNTY        
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            // Test that a subquery type node was built
            SyntaxNode oFrom = oTemp.FindFirst(SyntaxKind.FromKeyword);

            Assert.IsTrue(oFrom != null);

            SyntaxNode oInnerJoin = oFrom[0];

            Assert.AreEqual(3, oInnerJoin.Count);
            Assert.AreEqual(SyntaxKind.InnerJoinKeyword, oInnerJoin.ExpectedType);

            // test children are correct
            Assert.AreEqual(oInnerJoin[0].ExpectedType, SyntaxKind.IdentifierDatabaseSymbol);
            Assert.AreEqual(oInnerJoin[1].ExpectedType, SyntaxKind.IdentifierDatabaseSymbol);
        }
Exemple #20
0
        public void ParseSingleFunctionInWhere()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        SELECT  
                        svc_idnty
                        FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc     
                        WHERE MAX(svc.somecode) > 100   
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);
            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.IsTrue(oSelect != null);

            SyntaxNode oWhere = oTemp.FindFirst(SyntaxKind.WhereKeyword);

            Assert.AreEqual(1, oWhere.Count);

            SyntaxNode oGreaterThan = oWhere[0];

            Assert.AreEqual(SyntaxKind.GreaterThanToken, oGreaterThan.ExpectedType);

            SyntaxNode oFunc = oGreaterThan[0];

            Assert.AreEqual(SyntaxKind.MaxKeyword, oFunc.ExpectedType);
        }
Exemple #21
0
        public ParseResult <IN, OUT> ParseWithContext(IList <Token <IN> > tokens, object parsingContext = null, string startingNonTerminal = null)
        {
            var result = new ParseResult <IN, OUT>();

            var cleaner      = new SyntaxTreeCleaner <IN>();
            var syntaxResult = SyntaxParser.Parse(tokens, startingNonTerminal);

            syntaxResult = cleaner.CleanSyntaxTree(syntaxResult);
            if (!syntaxResult.IsError && syntaxResult.Root != null)
            {
                var r = Visitor.VisitSyntaxTree(syntaxResult.Root, parsingContext);
                result.Result     = r;
                result.SyntaxTree = syntaxResult.Root;
                result.IsError    = false;
            }
            else
            {
                result.Errors = new List <ParseError>();
                var unexpectedTokens = syntaxResult.Errors.Cast <UnexpectedTokenSyntaxError <IN> >().ToList();
                var byEnding         = unexpectedTokens.GroupBy(x => x.UnexpectedToken.Position).OrderBy(x => x.Key);
                var errors           = new List <ParseError>();
                foreach (var expecting in byEnding)
                {
                    var expectingTokens = expecting.SelectMany(x => x.ExpectedTokens).Distinct();
                    var expected        = new UnexpectedTokenSyntaxError <IN>(expecting.First().UnexpectedToken, expectingTokens.ToArray());
                    errors.Add(expected);
                }

                result.Errors.AddRange(errors);
                result.IsError = true;
            }

            return(result);
        }
Exemple #22
0
        public void LexCreateTableBasic()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        CREATE TABLE _X AS 
                        SELECT  
                        *
                        FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc          
                ");

            SyntaxParser oParser = new SyntaxParser(oText);

            // Should be 8 (including 1 for the EOF Token)
            Assert.IsTrue(oParser.TokenList.Count == 12);

            // First item should be a CREATE keyword (ignoring all comments which is trivia)
            Assert.AreEqual(oParser.TokenList[0].ExpectedType, SyntaxKind.CreateKeyword);
            // _X
            Assert.AreEqual(oParser.TokenList[1].ExpectedType, SyntaxKind.TableKeyword);
            // Iden
            Assert.AreEqual(oParser.TokenList[2].ExpectedType, SyntaxKind.IdentifierToken);
            // Iden
            Assert.AreEqual(oParser.TokenList[3].ExpectedType, SyntaxKind.AsKeyword);
        }
Exemple #23
0
        public void ParseSingleFunctionInColumn()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        SELECT  
                        COALESCE(svc_idnty, 1, 10)
                        FROM
                        APSHARE_FPVIEWS..FPC_SERVICE svc        
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);
            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oSelect = oTemp.FindFirst(SyntaxKind.SelectKeyword);

            Assert.IsTrue(oSelect != null);

            SyntaxNode oColumnList = oSelect[0];

            Assert.AreEqual(1, oColumnList.Count);

            SyntaxNode oFunction = oColumnList[0];

            Assert.AreEqual(SyntaxKind.CoalesceKeyword, oFunction.ExpectedType);
            Assert.AreEqual(1, oFunction.Count);
            Assert.AreEqual(3, oFunction[0].Count);
        }
Exemple #24
0
        public void ParseBadWhere()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                            col1, col2
                        FROM
                        (
                            SELECT * FROM FPC_SERVICE
                        ) svc
                        WHERE     
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            // Test that a subquery type node was built
            SyntaxNode oWhere = oTemp.FindFirst(SyntaxKind.WhereKeyword);

            Assert.AreNotEqual(null, oWhere);

            Assert.AreEqual(SyntaxKind.BooleanToken, oWhere[0].ExpectedType); // Check for the Filler node
        }
Exemple #25
0
        public void ParseWhereOr()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                            col1, col2
                        FROM
                        (
                            SELECT * FROM FPC_SERVICE
                        ) svc
                        WHERE svc.MKT_PROD_CD = 'MOB PT' OR svc.SVC_STAT_CD <> 'C'      
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            // Test that a subquery type node was built
            SyntaxNode oWhere = oTemp.FindFirst(SyntaxKind.WhereKeyword);

            Assert.IsTrue(oWhere != null);

            // Test that there is a select keyword in that subquery
            SyntaxNode oOR = oWhere.FindFirst(SyntaxKind.OrKeyword);

            Assert.IsTrue(oOR != null);

            // Test that the OR was generated properly (exactly 2 children)
            Assert.AreEqual(2, oOR.Count);
        }
Exemple #26
0
        public void ParseEmptyAnd()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                            col1, col2
                        FROM
                        (
                            SELECT * FROM FPC_SERVICE
                        ) svc
                        WHERE AND    
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            // Test that a subquery type node was built
            SyntaxNode oWhere = oTemp.FindFirst(SyntaxKind.WhereKeyword);

            Assert.IsTrue(oWhere != null);

            // Test that there is a select keyword in that subquery
            SyntaxNode oAND = oWhere.FindFirst(SyntaxKind.AndKeyword);

            Assert.AreNotEqual(null, oAND);
            Assert.AreEqual(SyntaxKind.BooleanToken, oAND[0].ExpectedType); // Check for the Filler node

            // Test that the AND was generated properly (exactly 2 children)
            Assert.AreEqual(2, oAND.Count);
        }
Exemple #27
0
        public void ParseOnAnd()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                            col1, col2
                        FROM
                        (
                            SELECT * FROM FPC_SERVICE
                        ) svc
                        INNER JOIN 
                            APSHARE_FP..FWR02052_OMR_BASE omr
                            ON 
                                svc.MKT_PROD_CD = 'MOB PT' AND 
                                svc.SVC_IDNTY = omr.SERVICE_NO  
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            // Test that a subquery type node was built
            SyntaxNode oON = oTemp.FindFirst(SyntaxKind.OnKeyword);

            Assert.AreNotEqual(oON, null);

            // Test that there is a select keyword in that subquery
            SyntaxNode oAND = oON.FindFirst(SyntaxKind.AndKeyword);

            Assert.AreNotEqual(oAND, null);
            Assert.AreEqual(oAND.RawSQLText, "AND");

            // Test that the AND was generated properly (exactly 2 children)
            Assert.AreEqual(2, oAND.Count);

            SyntaxNode oLeftEquals = oAND[0];

            Assert.AreEqual(oLeftEquals.ExpectedType, SyntaxKind.EqualsToken);

            SyntaxNode oLeftEqualsL = oLeftEquals[0];

            Assert.AreEqual(oLeftEqualsL.ExpectedType, SyntaxKind.IdentifierTableSymbol);
            SyntaxNode oLeftEqualsR = oLeftEquals[1];

            Assert.AreEqual(oLeftEqualsR.ExpectedType, SyntaxKind.LiteralToken);


            SyntaxNode oRightEquals = oAND[1];

            Assert.AreEqual(oLeftEquals.ExpectedType, SyntaxKind.EqualsToken);
            SyntaxNode oRightEqualsL = oRightEquals[0];

            Assert.AreEqual(oRightEqualsL.ExpectedType, SyntaxKind.IdentifierTableSymbol);
            SyntaxNode oRightEqualsR = oRightEquals[1];

            Assert.AreEqual(oRightEqualsR.ExpectedType, SyntaxKind.IdentifierTableSymbol);
        }
Exemple #28
0
        public void ParseIN()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        SELECT * FROM FPC_SERVICE
                        WHERE svc_idnty IN ('0415783039')  
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            SyntaxNode oWhere = oTemp.FindFirst(SyntaxKind.WhereKeyword);

            Assert.AreNotEqual(null, oWhere);
            Assert.AreEqual(1, oWhere.Count);
            Assert.AreEqual(SyntaxKind.InKeyword, oWhere[0].ExpectedType);
            Assert.AreEqual(2, oWhere[0].Count);

            Assert.AreEqual(SyntaxKind.IdentifierTableSymbol, oWhere[0][0].ExpectedType);
            Assert.AreEqual(SyntaxKind.OpenParenthesisToken, oWhere[0][1].ExpectedType);
            Assert.AreEqual(1, oWhere[0][1].Count);
        }
Exemple #29
0
        public void ParseBasicWhere()
        {
            SlidingTextWindow oText = new SlidingTextWindow(
                @"   
                        /* TEST */      
                        SELECT  
                            col1, col2
                        FROM
                        (
                            SELECT * FROM FPC_SERVICE
                        ) svc
                        WHERE svc.MKT_PROD_CD = 'MOB PT'
                ");

            // Initialises the Parser
            SyntaxParser oParser = new SyntaxParser(oText);

            // Try and generate a tree
            SyntaxNode oTemp = oParser.ParseTree();

            // Test that a subquery type node was built
            SyntaxNode oWhere = oTemp.FindFirst(SyntaxKind.WhereKeyword);

            Assert.IsTrue(oWhere != null);

            // Test that there is a select keyword in that subquery
            SyntaxNode oEqual = oWhere.FindFirst(SyntaxKind.EqualsToken);

            Assert.AreNotEqual(oEqual, null);

            // Test that the AND was generated properly (exactly 2 children)
            Assert.AreEqual(2, oEqual.Count);
            Assert.AreEqual(SyntaxKind.IdentifierTableSymbol, oEqual[0].ExpectedType);
            Assert.AreEqual(SyntaxKind.LiteralToken, oEqual[1].ExpectedType);
        }
Exemple #30
0
        public LuaStatement Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            reader.VerifyExpectedTokenAndMoveNext(Keywords.Return);
            var returnedExpression = SyntaxParser.ReadExpression(reader, context);

            return(new ReturnStatement(returnedExpression));
        }
 public void WhenStringStartsWithGerkinSyntaxThenCallsObserverWithOneGivenAndMultiplePascalSpans()
 {
     var mockGherkinSyntaxParserObserver = Substitute.For<ISyntaxParserObserver>();
     var gherkinSyntaxParser = new SyntaxParser(mockGherkinSyntaxParserObserver);
     gherkinSyntaxParser.Parse("GivenTheAccountIsInCredit");
     mockGherkinSyntaxParserObserver.Received(1).AddGherkinSyntaxSpanAt(0, 5);   // Given
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(5, 3);      // The
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(8, 7);      // Account
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(15, 2);     // Is
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(17, 2);     // In
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(19, 6);     // Credit
 }
 public void WhenStringStartsWithAndContainsGherkinSntaxThenCallsObserverWithOneGivenAndOneAndSpan()
 {
     var mockGherkinSyntaxParserObserver = Substitute.For<ISyntaxParserObserver>();
     var gherkinSyntaxParser = new SyntaxParser(mockGherkinSyntaxParserObserver);
     gherkinSyntaxParser.Parse("GivenTheAccountIsInCreditAndTheCardIsValid");
     mockGherkinSyntaxParserObserver.Received(1).AddGherkinSyntaxSpanAt(0, 5);   // Given
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(5, 3);      // The
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(8, 7);      // Account
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(15, 2);     // Is
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(17, 2);     // In
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(19, 6);     // Credit
     mockGherkinSyntaxParserObserver.Received(1).AddGherkinSyntaxSpanAt(25, 3);  // And
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(28, 3);     // The
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(31, 4);     // Card
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(35, 2);     // Is
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(37, 5);     // Valid
 }
 public void WhenStringStartWithAndContainsGherkinSntaxThenCallsObserverWithOneWhenAndOneThenSpan()
 {
     var mockGherkinSyntaxParserObserver = Substitute.For<ISyntaxParserObserver>();
     var gherkinSyntaxParser = new SyntaxParser(mockGherkinSyntaxParserObserver);
     gherkinSyntaxParser.Parse("WhenCustomerRequestsCashThenEnsureAccountIsDebitedAndEnsureCashDispensed");
     mockGherkinSyntaxParserObserver.Received(1).AddGherkinSyntaxSpanAt(0, 4);   // When
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(4, 8);      // Customer
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(12, 8);     // Requests
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(20, 4);     // Cash
     mockGherkinSyntaxParserObserver.Received(1).AddGherkinSyntaxSpanAt(24, 4);  // Then
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(28, 6);     // Ensure
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(34, 7);     // Account
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(41, 2);     // Is
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(43, 7);     // Debited
     mockGherkinSyntaxParserObserver.Received(1).AddGherkinSyntaxSpanAt(50, 3);  // And
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(53, 6);     // Ensure
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(59, 4);     // Cash
     mockGherkinSyntaxParserObserver.Received(1).AddPascalCaseSpanAt(63, 9);     // Dispensed
 }
 public void Parse(string identifier)
 {
     this.classificationSpans.Clear();
     var syntaxParser = new SyntaxParser(this);
     syntaxParser.Parse(identifier);
 }