Example #1
0
        /// <summary>
        /// Process the term2 rule.
        /// </summary>
        /// <remarks>
        ///
        /// term2 ::= '*' factor term2 |
        ///           '/' factor term2 |
        ///           {empty}
        ///
        /// </remarks>
        /// <returns>Parsed node.</returns>
        protected virtual EvalNode ParseTerm2()
        {
            if (Tokeniser.Next(TokenType.Multiply))
            {
                EvalNode op    = new EvalNodeBinaryOp(EvalNodeBinaryOp.BinaryOp.Multiply, ParseFactor(), Language);
                EvalNode term2 = ParseTerm2();
                if (term2 != null)
                {
                    term2.Prepend(op);
                }
                return(op);
            }

            if (Tokeniser.Next(TokenType.Divide))
            {
                EvalNode op    = new EvalNodeBinaryOp(EvalNodeBinaryOp.BinaryOp.Divide, ParseFactor(), Language);
                EvalNode term2 = ParseTerm2();
                if (term2 != null)
                {
                    term2.Prepend(op);
                }
                return(op);
            }

            return(null);
        }
Example #2
0
        /// <summary>
        /// Parse the provided input string.
        /// </summary>
        /// <param name="input">Input text.</param>
        public virtual void Parse(string input)
        {
            if (!string.IsNullOrEmpty(input))
            {
                // Generate entire set of tokens in one go
                _tokeniser = new Tokeniser(input, Language);

                // Parse into abstract syntax tree
                EvalNode eval = ParseExpression();

                // Should be no more input available
                if (Tokeniser.Peek.Type != TokenType.EndOfInput)
                {
                    throw new ParseException(Tokeniser.Peek.Index, "Found '" + Tokeniser.Peek.Type.ToString() + "' instead of end of input");
                }
                else
                {
                    _root = eval;
                }
            }
            else
            {
                _root = null;
            }
        }
Example #3
0
        /// <summary>
        /// Parse the 'expression2' rule.
        /// </summary>
        /// <remarks>
        ///
        /// expression2 ::= '+' term expression2 |
        ///                 '-' term expression2 |
        ///                 {empty}
        ///
        /// </remarks>
        /// <returns>Parsed node.</returns>
        protected virtual EvalNode ParseExpression2()
        {
            if (Tokeniser.Next(TokenType.Plus))
            {
                EvalNode op    = new EvalNodeBinaryOp(EvalNodeBinaryOp.BinaryOp.Add, ParseTerm(), Language);
                EvalNode expr2 = ParseExpression2();
                if (expr2 != null)
                {
                    expr2.Prepend(op);
                }
                return(op);
            }

            if (Tokeniser.Next(TokenType.Minus))
            {
                EvalNode op    = new EvalNodeBinaryOp(EvalNodeBinaryOp.BinaryOp.Subtract, ParseTerm(), Language);
                EvalNode expr2 = ParseExpression2();
                if (expr2 != null)
                {
                    expr2.Prepend(op);
                }
                return(op);
            }

            return(null);
        }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeCondLogicOp node.
 /// </summary>
 /// <param name="operation">Specifies the binary compare to represent.</param>
 /// <param name="child1">Specifies the first child node.</param>
 /// <param name="child2">Specifies the second child node.</param>
 public EvalNodeCondLogicOp(CompareOp operation,
                            EvalNode child1,
                            EvalNode child2)
 {
     _operation = operation;
     Append(child1);
     Append(child2);
 }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeCondLogicOp node.
 /// </summary>
 /// <param name="operation">Specifies the binary compare to represent.</param>
 /// <param name="child">Specifies the first child node.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeCondLogicOp(CompareOp operation,
                            EvalNode child,
                            Language language)
 {
     _language  = language;
     _operation = operation;
     Append(child);
 }
Example #6
0
 /// <summary>
 /// Instantiate a new instance of the EvalNodeUnaryOp node.
 /// </summary>
 /// <param name="operation">Specifies the unary operation to represent.</param>
 /// <param name="child">Specifies the child node.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeUnaryOp(UnaryOp operation,
                        EvalNode child,
                        Language language)
 {
     _operation = operation;
     _language  = language;
     Append(child);
 }
Example #7
0
 /// <summary>
 /// Instantiate a new instance of the EvalNodeShiftOp node.
 /// </summary>
 /// <param name="operation">Specifies the shift operation to represent.</param>
 /// <param name="child">Specifies the first child node.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeShiftOp(ShiftOp operation,
                        EvalNode child,
                        Language language)
 {
     _operation = operation;
     _language  = language;
     Append(child);
 }
Example #8
0
 /// <summary>
 /// Appends a node to the end of the child collection.
 /// </summary>
 /// <param name="item">Reference to node for appending.</param>
 public void Append(EvalNode item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item", "cannot append an empty reference");
     }
     else
     {
         _children.Add(item);
         item.Parent = this;
     }
 }
Example #9
0
        /// <summary>
        /// Parse the 'term' rule.
        /// </summary>
        /// <remarks>
        ///
        /// term ::= factor term2
        ///
        /// </remarks>
        /// <returns>Parsed node.</returns>
        protected virtual EvalNode ParseTerm()
        {
            EvalNode factor = ParseFactor();
            EvalNode term2  = ParseTerm2();

            if (term2 != null)
            {
                term2.Prepend(factor);
                return(term2);
            }
            else
            {
                return(factor);
            }
        }
Example #10
0
        /// <summary>
        /// Parse the 'expression' rule.
        /// </summary>
        /// <remarks>
        ///
        /// expression ::= term expression2
        ///
        /// </remarks>
        /// <returns>Parsed node.</returns>
        protected virtual EvalNode ParseExpression()
        {
            EvalNode term  = ParseTerm();
            EvalNode expr2 = ParseExpression2();

            if (expr2 != null)
            {
                expr2.Prepend(term);
                return(expr2.Root);
            }
            else
            {
                return(term.Root);
            }
        }
Example #11
0
        /// <summary>
        /// Parse the 'primary' rule.
        /// </summary>
        /// <remarks>
        ///
        /// primary ::= integer-literal |
        ///             real-literal
        ///             '(' expression ')'
        ///
        /// </remarks>
        /// <returns>Parsed node.</returns>
        protected virtual EvalNode ParsePrimary()
        {
            if ((Tokeniser.Peek.Type == TokenType.IntegerLiteral) ||
                (Tokeniser.Peek.Type == TokenType.RealLiteral))
            {
                return(new EvalNodeLiteral(Tokeniser.Peek.TypeCode, Tokeniser.Next().Value, Language));
            }

            if (Tokeniser.Next(TokenType.OpenRoundBracket))
            {
                EvalNode expr = ParseExpression();

                if (!Tokeniser.Next(TokenType.CloseRoundBracket))
                {
                    if (Tokeniser.Peek.Type == TokenType.EndOfInput)
                    {
                        throw new ParseException(Tokeniser.Peek.Index, "Found '" + Tokeniser.Peek.Type.ToString() + "' instead of expected ')'");
                    }
                    else
                    {
                        throw new ParseException(Tokeniser.Peek.Index, "Found '" + Tokeniser.Peek.Type.ToString() + "' at index '" + Tokeniser.Peek.Index + "' instead of expected ')'");
                    }
                }

                return(expr);
            }

            if (Tokeniser.Peek.Type == TokenType.EndOfInput)
            {
                throw new ParseException(Tokeniser.Peek.Index, "Found '" + Tokeniser.Peek.Type.ToString() + "' instead of some more input");
            }
            else
            {
                throw new ParseException(Tokeniser.Peek.Index, "Found '" + Tokeniser.Peek.Type.ToString() + "' at index '" + Tokeniser.Peek.Index + "' instead of either a primary or '('");
            }
        }
Example #12
0
 /// <summary>
 /// Initialize a new instance of the EvalNodeArgList class.
 /// </summary>
 /// <param name="argument">Initial argument to store.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeArgList(EvalNode argument,
                        Language language)
 {
     _language = language;
     Append(argument);
 }
Example #13
0
 /// <summary>
 /// Instantiate a new instance of the EvalNodeExponent node.
 /// </summary>
 /// <param name="child">Specifies the first child node.</param>
 /// <param name="language">Language syntax and semantics to use.</param>
 public EvalNodeExponent(EvalNode child,
                         Language language)
 {
     _language = language;
     Append(child);
 }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeNullCoalescing node.
 /// </summary>
 /// <param name="child">Specifies the child node.</param>
 public EvalNodeNullCoalescing(EvalNode child)
 {
     Append(child);
 }
Example #15
0
 /// <summary>
 /// Instantiate a new instance of the EvalNodeConcatenate node.
 /// </summary>
 /// <param name="child">Specifies the first child node.</param>
 public EvalNodeConcatenate(EvalNode child)
 {
     Append(child);
 }
Example #16
0
 /// <summary>
 /// Instantiate a new instance of the EvalNodeConditional node.
 /// </summary>
 /// <param name="child1">Specifies the first child.</param>
 /// <param name="child2">Specifies the second child.</param>
 public EvalNodeConditional(EvalNode child1,
                            EvalNode child2)
 {
     Append(child1);
     Append(child2);
 }
 /// <summary>
 /// Initialize a new instance of the EvalNodeArrayIndex class.
 /// </summary>
 /// <param name="child">Specifies the child node.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeArrayIndex(EvalNode child,
                           Language language)
 {
     _language = language;
     Append(child);
 }