Ejemplo n.º 1
0
        public static bool TryParseNode(Genero4glParser parser, out ActionAttributesListmodTriggers node)
        {
            node            = new ActionAttributesListmodTriggers();
            node.StartIndex = parser.Token.Span.Start;
            bool result = true;

            switch (parser.PeekToken().Kind)
            {
            case TokenKind.TextKeyword:
            case TokenKind.CommentKeyword:
            case TokenKind.ImageKeyword:
            case TokenKind.AcceleratorKeyword:
            {
                parser.NextToken();
                if (parser.PeekToken(TokenKind.Equals))
                {
                    parser.NextToken();
                }
                else
                {
                    parser.ReportSyntaxError("Expected equals token in display attribute.");
                }

                // get the help number
                StringExpressionNode stringExpressionNode;
                if (!StringExpressionNode.TryGetExpressionNode(parser, out stringExpressionNode))
                {
                    parser.ReportSyntaxError("Invalid string expression found in action attribute.");
                }
            }
            break;

            case TokenKind.DefaultViewKeyword:
            case TokenKind.ContextMenuKeyword:
            {
                parser.NextToken();
                if (parser.PeekToken(TokenKind.Equals))
                {
                    var tokKind = parser.NextToken().Kind;
                    if (tokKind != TokenKind.YesKeyword ||
                        tokKind != TokenKind.NoKeyword ||
                        tokKind != TokenKind.AutoKeyword)
                    {
                        parser.ReportSyntaxError("Invalid token found in action attribute.");
                    }
                }
                else
                {
                    parser.ReportSyntaxError("Expected equals token in display attribute.");
                }
            }
            break;

            default:
                result = false;
                break;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static bool TryParseNode(IParser parser, out FormClause node)
        {
            node = null;
            bool result = false;

            if (parser.PeekToken(TokenKind.FormKeyword))
            {
                node   = new FormClause();
                result = true;
                parser.NextToken();
                node.StartIndex = parser.Token.Span.Start;

                StringExpressionNode expr;
                if (StringExpressionNode.TryGetExpressionNode(parser, out expr))
                {
                    node.FormFile = expr;
                }
                else
                {
                    parser.ReportSyntaxError("Invalid token found, expecting string literal.");
                }

                node.EndIndex = parser.Token.Span.End;
            }

            return(result);
        }
Ejemplo n.º 3
0
        public static bool TryParseDefine(Genero4glParser parser, out SchemaSpecificationNode defNode)
        {
            defNode = null;
            bool result = false;

            if (parser.PeekToken(TokenKind.SchemaKeyword))
            {
                defNode = new SchemaSpecificationNode();
                parser.NextToken();
                defNode.StartIndex = parser.Token.Span.Start;
                result             = true;

                FglNameExpression nameExp;
                if (FglNameExpression.TryParseNode(parser, out nameExp))
                {
                    defNode.SchemaName = nameExp;
                    defNode.EndIndex   = parser.Token.Span.End;
                }
                else
                {
                    StringExpressionNode strExp;
                    if (StringExpressionNode.TryGetExpressionNode(parser, out strExp))
                    {
                        defNode.SchemaName = nameExp;
                        defNode.EndIndex   = parser.Token.Span.End;
                    }
                    else
                    {
                        parser.ReportSyntaxError(defNode.StartIndex, parser.Token.Span.End, "Invalid token type found.");
                    }
                }
            }
            else
            {
                if (parser.PeekToken(TokenKind.DescribeKeyword))
                {
                    defNode            = new SchemaSpecificationNode();
                    defNode.StartIndex = parser.Token.Span.Start;
                    parser.NextToken();
                    result = true;
                }

                if (parser.PeekToken(TokenKind.DatabaseKeyword))
                {
                    if (defNode == null)
                    {
                        defNode            = new SchemaSpecificationNode();
                        defNode.StartIndex = parser.Token.Span.Start;
                        parser.NextToken();
                        result = true;
                    }

                    parser.NextToken();
                    parser.NextToken();
                    FglNameExpression nameExp;
                    if (FglNameExpression.TryParseNode(parser, out nameExp))
                    {
                        defNode.SchemaName = nameExp;
                        defNode.EndIndex   = parser.Token.Span.End;
                        defNode.IsComplete = true;
                    }
                    else
                    {
                        StringExpressionNode strExp;
                        if (StringExpressionNode.TryGetExpressionNode(parser, out strExp))
                        {
                            defNode.SchemaName = nameExp;
                            defNode.EndIndex   = parser.Token.Span.End;
                            defNode.IsComplete = true;
                        }
                        else
                        {
                            parser.ReportSyntaxError(defNode.StartIndex, parser.Token.Span.End, "Invalid token type found.");
                        }
                    }
                }
                else if (defNode != null)
                {
                    parser.ReportSyntaxError(defNode.StartIndex, parser.Token.Span.End, "Legacy schema specification is incomplete.");
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
 public object Visit(StringExpressionNode n, object o)
 {
     return(null);
 }
Ejemplo n.º 5
0
        public static bool TryParseNode(IParser parser, out ScreenNode node)
        {
            bool result = false;

            node = null;

            if (parser.PeekToken(TokenKind.ScreenKeyword))
            {
                result = true;
                node   = new ScreenNode();
                parser.NextToken();
                node.StartIndex = parser.Token.Span.Start;

                if (parser.PeekToken(TokenKind.SizeKeyword))
                {
                    parser.NextToken();
                    var linesVal = parser.NextToken().Value.ToString();
                    int tempLines;
                    if (int.TryParse(linesVal, out tempLines))
                    {
                        node.Lines = tempLines;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Invalid value found for screen height.");
                    }

                    if (parser.PeekToken(TokenKind.ByKeyword))
                    {
                        parser.NextToken();
                        var charsVal = parser.NextToken().Value.ToString();
                        if (int.TryParse(linesVal, out tempLines))
                        {
                            node.Chars = tempLines;
                        }
                        else
                        {
                            parser.ReportSyntaxError("Invalid value found for screen width.");
                        }
                    }
                }

                if (parser.PeekToken(TokenKind.TitleKeyword))
                {
                    parser.NextToken();
                    StringExpressionNode strExpr;
                    if (StringExpressionNode.TryGetExpressionNode(parser, out strExpr))
                    {
                        node.Title = strExpr.LiteralValue;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Invalid screen title found.");
                    }
                }

                if (parser.PeekToken(TokenKind.LeftBrace))
                {
                    parser.NextToken();

                    while (!parser.PeekToken(TokenKind.RightBrace))
                    {
                        parser.NextToken();
                    }

                    if (parser.PeekToken(TokenKind.RightBrace))
                    {
                        parser.ReportSyntaxError("Expected right-brace.");
                    }

                    if (parser.PeekToken(TokenKind.EndKeyword))
                    {
                        parser.NextToken();
                    }
                }
                else
                {
                    parser.ReportSyntaxError("Expected left-brace.");
                }

                node.EndIndex = parser.Token.Span.End;
            }

            return(result);
        }
Ejemplo n.º 6
0
        protected bool BindPrepareCursorFromIdentifier(PrepareStatement prepStmt)
        {
            // If the prepare statement uses a variable from prepare from, that variable should have been encountered
            // prior to the prepare statement. So we'll do a binary search in the children of this function to look for
            // a LetStatement above the prepare statement where the prepare statement's from identifier was assigned.
            // If it can't be found, then we have to assume that the identifier was assigned outside of this function,
            // and we have no real way to determining the cursor SQL text.
            bool result = false;

            if (prepStmt.Children.Count == 1)
            {
                StringExpressionNode strExpr = prepStmt.Children[prepStmt.Children.Keys[0]] as StringExpressionNode;
                if (strExpr != null)
                {
                    prepStmt.SetSqlStatement(strExpr.LiteralValue);
                }
                else
                {
                    FglNameExpression exprNode = prepStmt.Children[prepStmt.Children.Keys[0]] as FglNameExpression;
                    if (exprNode != null)
                    {
                        string ident = exprNode.Name;

                        List <int> keys        = Children.Select(x => x.Key).ToList();
                        int        searchIndex = keys.BinarySearch(prepStmt.StartIndex);
                        if (searchIndex < 0)
                        {
                            searchIndex = ~searchIndex;
                            if (searchIndex > 0)
                            {
                                searchIndex--;
                            }
                        }

                        LetStatement letStmt = null;
                        while (searchIndex >= 0 && searchIndex < keys.Count)
                        {
                            int key = keys[searchIndex];
                            letStmt = Children[key] as LetStatement;
                            if (letStmt != null)
                            {
                                // check for the LetStatement's identifier
                                if (ident.Equals(letStmt.Variable.Name, StringComparison.OrdinalIgnoreCase))
                                {
                                    break;
                                }
                                else
                                {
                                    letStmt = null;
                                }
                            }
                            searchIndex--;
                        }

                        if (letStmt != null)
                        {
                            // we have a match, bind the let statement's value
                            prepStmt.SetSqlStatement(letStmt.GetLiteralValue());
                            result = true;
                        }
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 7
0
        public static bool TryParseAttribute(IParser parser, out LayoutAttribute attrib)
        {
            attrib = new LayoutAttribute();
            bool result = true;

            int?intVal = null;
            StringExpressionNode stringNode = null;

            switch (parser.PeekToken().Kind)
            {
            case TokenKind.ImageKeyword:
                parser.NextToken();
                attrib.StartIndex = parser.Token.Span.Start;
                attrib.Type       = LayoutAttributes.Image;

                if (StringExpressionNode.TryGetExpressionNode(parser, out stringNode))
                {
                    attrib.Image = stringNode;
                }
                else
                {
                    parser.ReportSyntaxError("Invalid token found in Image attribute, expected string literal.");
                }

                break;

            case TokenKind.MinHeightKeyword:
                parser.NextToken();
                attrib.StartIndex = parser.Token.Span.Start;
                attrib.Type       = LayoutAttributes.MinHeight;

                intVal = GetIntegerValue(parser);
                if (intVal.HasValue)
                {
                    attrib.MinHeight = intVal.Value;
                }
                break;

            case TokenKind.MinWidthKeyword:
                parser.NextToken();
                attrib.StartIndex = parser.Token.Span.Start;
                attrib.Type       = LayoutAttributes.MinWidth;

                intVal = GetIntegerValue(parser);
                if (intVal.HasValue)
                {
                    attrib.MinWidth = intVal.Value;
                }

                break;

            case TokenKind.SpacingKeyword:
                parser.NextToken();
                attrib.StartIndex = parser.Token.Span.Start;
                attrib.Type       = LayoutAttributes.Spacing;

                if (parser.PeekToken(TokenKind.NormalKeyword))
                {
                    parser.NextToken();
                    attrib.Spacing = LayoutSpacing.Normal;
                }
                else if (parser.PeekToken(TokenKind.CompactKeyword))
                {
                    parser.NextToken();
                    attrib.Spacing = LayoutSpacing.Compact;
                }
                else
                {
                    parser.ReportSyntaxError("Invalid token found in Spacing attribute, expected \"NORMAL\" or \"COMPACT\".");
                }

                break;

            case TokenKind.StyleKeyword:
                parser.NextToken();
                attrib.StartIndex = parser.Token.Span.Start;
                attrib.Type       = LayoutAttributes.Style;

                if (StringExpressionNode.TryGetExpressionNode(parser, out stringNode))
                {
                    attrib.Style = stringNode;
                }
                else
                {
                    parser.ReportSyntaxError("Invalid token found in Style attribute, expected string literal.");
                }

                break;

            case TokenKind.TextKeyword:
                parser.NextToken();
                attrib.StartIndex = parser.Token.Span.Start;
                attrib.Type       = LayoutAttributes.Text;

                if (StringExpressionNode.TryGetExpressionNode(parser, out stringNode))
                {
                    attrib.Text = stringNode;
                }
                else
                {
                    parser.ReportSyntaxError("Invalid token found in Text attribute, expected string literal.");
                }
                break;

            case TokenKind.TagKeyword:
                parser.NextToken();
                attrib.StartIndex = parser.Token.Span.Start;
                attrib.Type       = LayoutAttributes.Tag;

                if (StringExpressionNode.TryGetExpressionNode(parser, out stringNode))
                {
                    attrib.Tag = stringNode;
                }
                else
                {
                    parser.ReportSyntaxError("Invalid token found in Tag attribute, expected string literal.");
                }

                break;

            case TokenKind.VersionKeyword:
                parser.NextToken();
                attrib.StartIndex = parser.Token.Span.Start;
                attrib.Type       = LayoutAttributes.Version;

                if (StringExpressionNode.TryGetExpressionNode(parser, out stringNode))
                {
                    attrib.Version = stringNode;
                }
                else
                {
                    parser.ReportSyntaxError("Invalid token found in Version attribute, expected string literal.");
                }
                break;

            case TokenKind.WindowStyleKeyword:
                parser.NextToken();
                attrib.StartIndex = parser.Token.Span.Start;
                attrib.Type       = LayoutAttributes.WindowStyle;

                if (StringExpressionNode.TryGetExpressionNode(parser, out stringNode))
                {
                    attrib.WindowStyle = stringNode;
                }
                else
                {
                    parser.ReportSyntaxError("Invalid token found in WindowStyle attribute, expected string literal.");
                }
                break;

            default:
                result = false;
                attrib = null;
                break;
            }

            if (attrib != null)
            {
                attrib.EndIndex = parser.Token.Span.End;
            }


            return(result);
        }
Ejemplo n.º 8
0
 public object Visit(StringExpressionNode n, object o)
 {
     n.StringLiteral.Accept(this, null);
     return(null);
 }
Ejemplo n.º 9
0
        public static bool TryGetExpressionNode(IParser parser, out ExpressionNode node, List <TokenKind> breakTokens = null, ExpressionParsingOptions options = null)
        {
            if (options == null)
            {
                options = new ExpressionParsingOptions();
            }
            node = null;
            bool result            = false;
            bool start             = true;
            bool requireExpression = false;

            TokenExpressionNode startingToken = null;

            while (true)
            {
                // First check for allowed pre-expression tokens
                if (_preExpressionTokens.Contains(parser.PeekToken().Kind))
                {
                    parser.NextToken();
                    if (node == null)
                    {
                        node = new TokenExpressionNode(parser.Token);
                    }
                    else
                    {
                        node.AppendExpression(new TokenExpressionNode(parser.Token));
                    }
                    requireExpression = true;
                }

                if (parser.PeekToken(TokenKind.LeftParenthesis))
                {
                    ParenWrappedExpressionNode parenExpr;
                    if (ParenWrappedExpressionNode.TryParseExpression(parser, out parenExpr, options))
                    {
                        if (node == null)
                        {
                            node = parenExpr;
                        }
                        else
                        {
                            node.AppendExpression(parenExpr);
                        }
                        result = true;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Paren-nested expression expected.");
                    }
                }
                else if (parser.PeekToken(TokenKind.LeftBracket))
                {
                    BracketWrappedExpressionNode brackNode;
                    if (BracketWrappedExpressionNode.TryParseNode(parser, out brackNode))
                    {
                        if (node == null)
                        {
                            node = brackNode;
                        }
                        else
                        {
                            node.AppendExpression(brackNode);
                        }
                        result = true;
                    }
                    else
                    {
                        parser.ReportSyntaxError("Bracket-nested expression expected.");
                    }
                }
                else if (parser.PeekToken(TokenCategory.StringLiteral) ||
                         parser.PeekToken(TokenCategory.CharacterLiteral) ||
                         parser.PeekToken(TokenCategory.IncompleteMultiLineStringLiteral))
                {
                    result = true;
                    parser.NextToken();
                    if (node == null)
                    {
                        node = new StringExpressionNode(parser.Token);
                    }
                    else
                    {
                        node.AppendExpression(new StringExpressionNode(parser.Token));
                    }
                }
                else if (parser.PeekToken(TokenCategory.NumericLiteral))
                {
                    result = true;
                    parser.NextToken();
                    if (node == null)
                    {
                        node = new TokenExpressionNode(parser.Token);
                    }
                    else
                    {
                        node.AppendExpression(new TokenExpressionNode(parser.Token));
                    }
                }
                else if (parser.PeekToken(TokenKind.CurrentKeyword))
                {
                    result = true;
                    parser.NextToken();
                    int    currentExprStart = parser.Token.Span.Start;
                    string currentTypeConstraint;
                    if (TypeConstraints.VerifyValidConstraint(parser, out currentTypeConstraint, TokenKind.CurrentKeyword, true))
                    {
                        result = true;
                        StringExpressionNode strExpr = new StringExpressionNode(string.Format("current {0}", currentTypeConstraint));
                        strExpr.StartIndex = currentExprStart;
                        strExpr.EndIndex   = parser.Token.Span.End;
                        if (node == null)
                        {
                            node = strExpr;
                        }
                        else
                        {
                            node.AppendExpression(strExpr);
                        }
                    }
                    else
                    {
                        if (node == null)
                        {
                            node = new TokenExpressionNode(parser.Token);
                        }
                        else
                        {
                            node.AppendExpression(new TokenExpressionNode(parser.Token));
                        }
                    }
                }
                else if (parser.PeekToken(TokenKind.IntervalKeyword))
                {
                    parser.NextToken();
                    TokenExpressionNode intervalNode = new TokenExpressionNode(parser.Token);
                    if (node == null)
                    {
                        node = intervalNode;
                    }
                    else
                    {
                        node.AppendExpression(intervalNode);
                    }
                    if (parser.PeekToken(TokenKind.LeftParenthesis))
                    {
                        parser.NextToken();
                        node.AppendExpression(new TokenExpressionNode(parser.Token));
                        while (!parser.PeekToken(TokenKind.EndOfFile))
                        {
                            parser.NextToken();
                            node.AppendExpression(new TokenExpressionNode(parser.Token));
                            if (parser.Token.Token.Kind == TokenKind.RightParenthesis)
                            {
                                break;
                            }
                        }
                        string intervalString;
                        if (TypeConstraints.VerifyValidConstraint(parser, out intervalString, TokenKind.IntervalKeyword, true))
                        {
                            result = true;
                            node.AppendExpression(new StringExpressionNode(intervalString));
                        }
                        else
                        {
                            parser.ReportSyntaxError("Invalid interval expression found.");
                        }
                    }
                }
                else if (parser.PeekToken(TokenKind.SelectKeyword) && options.AllowNestedSelectStatement)
                {
                    FglStatement selStmt;
                    bool         dummy;
                    if (SqlStatementFactory.TryParseSqlStatement(parser, out selStmt, out dummy))
                    {
                        result = true;
                        if (node == null)
                        {
                            node = new FglStatementExpression(selStmt);
                        }
                        else
                        {
                            node.AppendExpression(new FglStatementExpression(selStmt));
                        }
                    }
                    else
                    {
                        parser.ReportSyntaxError("Invalid select statement found in expression.");
                    }
                }
                else if (parser.PeekToken(TokenCategory.Identifier) ||
                         parser.PeekToken(TokenCategory.Keyword))
                {
                    bool isCustomExpression = false;
                    if (options != null &&
                        options.AdditionalExpressionParsers != null)
                    {
                        ExpressionNode parsedExpr;
                        foreach (var exprParser in options.AdditionalExpressionParsers)
                        {
                            if ((parsedExpr = exprParser(parser)) != null)
                            {
                                result = true;
                                if (node == null)
                                {
                                    node = parsedExpr;
                                }
                                else
                                {
                                    node.AppendExpression(parsedExpr);
                                }
                                isCustomExpression = true;
                            }
                        }
                    }
                    if (!isCustomExpression)
                    {
                        FunctionCallExpressionNode funcCall;
                        FglNameExpression          nonFuncCallName;
                        if (FunctionCallExpressionNode.TryParseExpression(parser, out funcCall, out nonFuncCallName, false, options))
                        {
                            result = true;
                            if (node == null)
                            {
                                node = funcCall;
                            }
                            else
                            {
                                node.AppendExpression(funcCall);
                            }
                        }
                        else if (nonFuncCallName != null)
                        {
                            bool isDatetime = false;
                            var  dtToken    = Tokens.GetToken(nonFuncCallName.Name);
                            if (dtToken != null)
                            {
                                if (TypeConstraints.DateTimeQualifiers.Contains(dtToken.Kind))
                                {
                                    string dtString;
                                    isDatetime = true;
                                    if (TypeConstraints.VerifyValidConstraint(parser, out dtString, TokenKind.DatetimeKeyword, true, dtToken.Kind))
                                    {
                                        result = true;
                                        var strExpr = new StringExpressionNode(dtString);
                                        if (node == null)
                                        {
                                            node = strExpr;
                                        }
                                        else
                                        {
                                            node.AppendExpression(strExpr);
                                        }
                                    }
                                    else
                                    {
                                        isDatetime = false;
                                    }
                                }
                            }

                            if (!isDatetime)
                            {
                                // it's a name expression
                                result = true;
                                if (node == null)
                                {
                                    node = nonFuncCallName;
                                }
                                else
                                {
                                    node.AppendExpression(nonFuncCallName);
                                }
                            }
                        }
                        else
                        {
                            result = true;
                            parser.NextToken();
                            if (node == null)
                            {
                                node = new TokenExpressionNode(parser.Token);
                            }
                            else
                            {
                                node.AppendExpression(new TokenExpressionNode(parser.Token));
                            }
                        }
                    }
                }
                else if (parser.PeekToken(TokenKind.Multiply) && options.AllowStarParam)
                {
                    result = true;
                    parser.NextToken();
                    if (node == null)
                    {
                        node = new TokenExpressionNode(parser.Token);
                    }
                    else
                    {
                        node.AppendExpression(new TokenExpressionNode(parser.Token));
                    }
                }
                else if (parser.PeekToken(TokenKind.QuestionMark) && options.AllowQuestionMark)
                {
                    parser.NextToken();
                    if (node == null)
                    {
                        parser.ReportSyntaxError("Invalid token '?' found in expression.");
                    }
                    else
                    {
                        node.AppendExpression(new TokenExpressionNode(parser.Token));
                    }
                }
                else
                {
                    if (requireExpression)
                    {
                        var tok = parser.PeekToken();
                        if (breakTokens != null && !breakTokens.Contains(tok.Kind))
                        {
                            parser.ReportSyntaxError("Invalid token type found in expression.");
                        }
                        else
                        {
                            parser.ReportSyntaxError("Expression required.");
                        }
                    }
                    break;
                }
                requireExpression = false;

                Token nextTok    = parser.PeekToken();
                bool  isOperator = true;
                while (isOperator && !requireExpression)
                {
                    if ((breakTokens == null ||
                         (breakTokens != null && !breakTokens.Contains(nextTok.Kind))) &&
                        nextTok.Kind >= TokenKind.FirstOperator &&
                        nextTok.Kind <= TokenKind.LastOperator)
                    {
                        parser.NextToken();
                        // TODO: not sure if we want to do more analysis on what operators can start an expression
                        if (node == null)
                        {
                            node = new TokenExpressionNode(parser.Token);
                        }
                        else
                        {
                            node.AppendExpression(new TokenExpressionNode(parser.Token));
                        }

                        switch (parser.Token.Token.Kind)
                        {
                        case TokenKind.LessThan:
                            // check for '<=' or '<>'
                            if (parser.PeekToken(TokenKind.Equals) ||
                                parser.PeekToken(TokenKind.GreaterThan))
                            {
                                parser.NextToken();
                                node.AppendExpression(new TokenExpressionNode(parser.Token));
                            }
                            break;

                        case TokenKind.GreaterThan:
                            // check for '>='
                            if (parser.PeekToken(TokenKind.Equals))
                            {
                                parser.NextToken();
                                node.AppendExpression(new TokenExpressionNode(parser.Token));
                            }
                            break;

                        case TokenKind.Exclamation:
                            // check for '!='
                            if (parser.PeekToken(TokenKind.Equals))
                            {
                                parser.NextToken();
                                node.AppendExpression(new TokenExpressionNode(parser.Token));
                            }
                            else
                            {
                                parser.ReportSyntaxError("Invalid token '!' found in expression.");
                            }
                            break;

                        case TokenKind.Equals:
                            // check for '=='
                            if (parser.PeekToken(TokenKind.Equals))
                            {
                                parser.NextToken();
                                node.AppendExpression(new TokenExpressionNode(parser.Token));
                            }
                            break;

                        case TokenKind.SingleBar:
                            //  check for '||'
                            if (parser.PeekToken(TokenKind.SingleBar))
                            {
                                parser.NextToken();
                                node.AppendExpression(new TokenExpressionNode(parser.Token));
                            }
                            else
                            {
                                parser.ReportSyntaxError("Invalid token '|' found in expression.");
                            }
                            break;
                        }
                        requireExpression = true;
                    }
                    else
                    {
                        // check for non-symbol operators
                        switch (nextTok.Kind)
                        {
                        case TokenKind.DoubleBar:
                        case TokenKind.AsKeyword:
                        case TokenKind.AndKeyword:
                        case TokenKind.OrKeyword:
                        case TokenKind.ModKeyword:
                        case TokenKind.UsingKeyword:
                        case TokenKind.InstanceOfKeyword:
                        case TokenKind.UnitsKeyword:
                        case TokenKind.LikeKeyword:
                        case TokenKind.MatchesKeyword:
                        case TokenKind.ThroughKeyword:
                        case TokenKind.ThruKeyword:
                        case TokenKind.BetweenKeyword:
                        {
                            // require another expression
                            requireExpression = true;
                            parser.NextToken();
                            node.AppendExpression(new TokenExpressionNode(parser.Token));
                        }
                        break;

                        case TokenKind.ClippedKeyword:
                        case TokenKind.SpacesKeyword:
                        {
                            parser.NextToken();
                            node.AppendExpression(new TokenExpressionNode(parser.Token));
                        }
                        break;

                        case TokenKind.IsKeyword:
                        {
                            parser.NextToken();
                            node.AppendExpression(new TokenExpressionNode(parser.Token));
                            if (parser.PeekToken(TokenKind.NotKeyword))
                            {
                                parser.NextToken();
                                node.AppendExpression(new TokenExpressionNode(parser.Token));
                            }
                            if (parser.PeekToken(TokenKind.NullKeyword))
                            {
                                parser.NextToken();
                                node.AppendExpression(new TokenExpressionNode(parser.Token));
                            }
                            else
                            {
                                parser.ReportSyntaxError("NULL keyword required in expression.");
                            }
                        }
                        break;

                        case TokenKind.NotKeyword:
                        {
                            parser.NextToken();
                            node.AppendExpression(new TokenExpressionNode(parser.Token));
                            if (parser.PeekToken(TokenKind.LikeKeyword) ||
                                parser.PeekToken(TokenKind.MatchesKeyword) ||
                                parser.PeekToken(TokenKind.InKeyword))
                            {
                                // require another expression
                                requireExpression = true;
                                parser.NextToken();
                                node.AppendExpression(new TokenExpressionNode(parser.Token));
                            }
                            else
                            {
                                parser.ReportSyntaxError("LIKE or MATCHES keyword required in expression.");
                            }
                        }
                        break;

                        default:
                        {
                            isOperator = false;
                            break;
                        }
                        }
                        if (!isOperator)
                        {
                            break;
                        }
                        else
                        {
                            nextTok = parser.PeekToken();
                            if (nextTok.Kind == TokenKind.EndOfFile)
                            {
                                break;
                            }
                        }
                    }
                }
                if (!requireExpression)
                {
                    break;
                }
            }

            if (result && node != null)
            {
                node.EndIndex = parser.Token.Span.End;
            }

            return(result);
        }
Ejemplo n.º 10
0
        private ExpressionNode Primary()
        {
            ExpressionNode     itsAST = null; // in case there is a syntactic error
            SourceCodePosition itsPos = _currentToken.SourcePosition;

            if (_currentToken.Type == Token.TokenType.LeftParen)
            {
                Accept(Token.TokenType.LeftParen);
                itsAST = Expression();
                Accept(Token.TokenType.RightParen);
            }
            else if (Array.Exists(_literalFirstSet, e => e == _currentToken.Type))
            {
                switch (_currentToken.Type)
                {
                case Token.TokenType.IntLiteral:
                    IntLiteralNode intLiteral = new IntLiteralNode(_currentToken.Value, _currentToken.SourcePosition);
                    Accept(Token.TokenType.IntLiteral);
                    itsAST = new IntExpressionNode(intLiteral, itsPos);
                    break;

                case Token.TokenType.RealLiteral:
                    RealLiteralNode realLiteral = new RealLiteralNode(_currentToken.Value, _currentToken.SourcePosition);
                    Accept(Token.TokenType.RealLiteral);
                    itsAST = new RealExpressionNode(realLiteral, itsPos);
                    break;

                case Token.TokenType.StringLiteral:
                    StringLiteralNode stringLiteral = new StringLiteralNode(_currentToken.Value, _currentToken.SourcePosition);
                    Accept(Token.TokenType.StringLiteral);
                    itsAST = new StringExpressionNode(stringLiteral, itsPos);
                    break;

                case Token.TokenType.BooleanLiteral:
                    BooleanLiteralNode booleanLiteral = new BooleanLiteralNode(_currentToken.Value, _currentToken.SourcePosition);
                    Accept(Token.TokenType.BooleanLiteral);
                    itsAST = new BooleanExpressionNode(booleanLiteral, itsPos);
                    break;

                case Token.TokenType.Null:
                    NullLiteralNode nullLiteral = new NullLiteralNode(_currentToken.Value, _currentToken.SourcePosition);
                    Accept(Token.TokenType.Null);
                    itsAST = new NullExpressionNode(nullLiteral, itsPos);
                    break;

                default:
                    itsAST = null;      // should not happen
                    break;
                }
            }
            else if (_currentToken.Type == Token.TokenType.Identifier)
            {
                itsAST = MemberReference();
            }
            else if (_currentToken.Type == Token.TokenType.This)
            {
                itsAST = ThisReference();
            }
            else if (_currentToken.Type == Token.TokenType.New)
            {
                itsAST = Instantiation();
            }
            else
            {
                Error("The token is invalid in the current context");
            }
            return(itsAST);
        }