示例#1
0
 /// <summary>
 /// CheckParms( Parser.Token, CValue) - This method makes certain the single argument is non-null.
 ///		Raises an exception if it is.
 /// </summary>
 /// <param name="oToken">Parser.Token object</param>
 /// <param name="arg1">CValue argument</param>
 private void CheckParms(Parser.Token oToken, CValue arg1)
 {
     if (arg1 == null)
     {
         throw new ApplicationException("Argument not supplied near " + oToken.ToString() + " operation.");
     }
 }
示例#2
0
        private static void RenderErrorInfoAsJson(ExportCommand command, Exception exception)
        {
            List <Exception> exceptions = new List <Exception>();

            if (exception != null)
            {
                if (exception is Parser.MultiParserException)
                {
                    exceptions.AddRange(((Parser.MultiParserException)exception).ParseExceptions);
                }
                else
                {
                    exceptions.Add(exception);
                }
            }

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("{ \"errors\": [");
            for (int i = 0; i < exceptions.Count; ++i)
            {
                if (i > 0)
                {
                    sb.Append(',');
                }
                Parser.FileScope       fileInfo        = null;
                Parser.Token           tokenInfo       = null;
                string                 message         = exceptions[i].Message;
                Parser.ParserException parserException = exceptions[i] as Parser.ParserException;
                if (parserException != null)
                {
                    fileInfo  = parserException.File;
                    tokenInfo = parserException.TokenInfo;
                    message   = parserException.OriginalMessage;
                }
                sb.Append("\n  {");
                if (fileInfo != null)
                {
                    sb.Append("\n    \"file\": \"");
                    sb.Append(fileInfo.Name.Replace("\\", "\\\\"));
                    sb.Append("\",");
                }
                if (tokenInfo != null)
                {
                    sb.Append("\n    \"col\": ");
                    sb.Append(tokenInfo.Col + 1);
                    sb.Append(",");
                    sb.Append("\n    \"line\": ");
                    sb.Append(tokenInfo.Line + 1);
                    sb.Append(",");
                }
                sb.Append("\n    \"message\": \"");
                sb.Append(message.Replace("\\", "\\\\").Replace("\"", "\\\""));
                sb.Append("\"\n  }");
            }
            sb.Append(" ] }");
            string output = sb.ToString();

            WriteCompileInformation(output);
        }
示例#3
0
    /// <summary>
    /// SetFunction(string): Sets the current function to a passed string.
    /// </summary>
    /// <param name="sEquation">string representing the function being used</param>
    public void SetFunction(string sEquation)
    {
        m_currentToken = null;
        m_nextToken    = null;

        m_sEquation = sEquation;
        m_Function  = null;
        InitFunctions();
    }
示例#4
0
    public void TestParseToken()
    {
        Parser.Token token = Parser.ParseToken("(name)");
        Assert.AreEqual(Parser.TokenType.Variable, token.type);
        Assert.AreEqual("name", token.value);

        token = Parser.ParseToken("\"string\"");
        Assert.AreEqual(Parser.TokenType.String, token.type);
        Assert.AreEqual("string", token.value);
    }
示例#5
0
    /// <summary>
    /// OpFactor(...): Reads the passed operator, identifies the associated implementation object
    ///		and requests an operation object to be used in evaluating the equation.
    /// </summary>
    /// <param name="oSourceOp">Parser.Token object representing the operator in question.</param>
    /// <param name="oValue1">The first value object to be operated on.</param>
    /// <param name="oValue2">The second value object to be operated on.</param>
    /// <returns>CValue object representing an operation.</returns>
    private CValue OpFactory(Parser.Token oSourceOp, CValue oValue1, CValue oValue2)
    {
        foreach (COperator oOp in m_aOps)
        {
            if (oOp.IsMatch(oSourceOp))
            {
                return(oOp.Factory(oValue1, oValue2));
            }
        }

        throw new ApplicationException("Invalid operator in equation.");
    }
示例#6
0
    /// <summary>
    /// AddSub(): Detects the operation to perform addition or substraction.
    /// </summary>
    /// <returns>CValue object representing an operation.</returns>
    private CValue AddSub()
    {
        CValue oValue = MultDiv();

        while (m_currentToken == "+" || m_currentToken == "-")
        {
            Parser.Token oOp = m_currentToken;
            PositionNextToken();

            CValue oNextVal = MultDiv();

            CheckParms(oOp, oValue, oNextVal);
            oValue = OpFactory(oOp, oValue, oNextVal);
        }

        return(oValue);
    }
示例#7
0
    /// <summary>
    /// Power():  Detects the operation to raise one number to the power
    ///		of another (a^2).
    /// </summary>
    /// <returns>CValue object representing an operation.</returns>
    private CValue Power()
    {
        CValue oValue = Sign();

        while (m_currentToken == "^")
        {
            Parser.Token oOp = m_currentToken;

            PositionNextToken();

            CValue oNextVal = Sign();

            CheckParms(oOp, oValue, oNextVal);
            oValue = OpFactory(oOp, oValue, oNextVal);
        }

        return(oValue);
    }
示例#8
0
    /// <summary>
    /// PositionNextToken():  Manipulates the current Token position forward in the chain of tokens
    ///		discovered by the parser.
    /// </summary>
    private void PositionNextToken()
    {
        if (m_currentToken == null)
        {
            if (!m_enumTokens.MoveNext())
            {
                throw new ApplicationException("Invalid equation.");
            }

            m_nextToken = (Parser.Token)m_enumTokens.Current;
        }

        m_currentToken = m_nextToken;

        if (!m_enumTokens.MoveNext())
        {
            m_nextToken = new Parser.Token();
        }
        else
        {
            m_nextToken = (Parser.Token)m_enumTokens.Current;
        }
    }
示例#9
0
    /// <summary>
    /// Sign():  This method detects the existence of sign operators before
    ///		a number or variable.
    /// </summary>
    /// <returns>CValue object representing an operation.</returns>
    private CValue Sign()
    {
        bool bNeg = false;

        Parser.Token oToken = null;
        if (m_currentToken == "+" || m_currentToken == "-")
        {
            oToken = m_currentToken;
            bNeg   = (m_currentToken == "-");
            PositionNextToken();
        }
        //CValue oFunc = Function();
        // sdh: should be function when ready.
        CValue oFunc = Paren();


        if (bNeg)
        {
            CheckParms(oToken, oFunc);
            oFunc = new CSignNeg(oFunc);
        }

        return(oFunc);
    }
示例#10
0
    public void TestGetBinding()
    {
        Parser.Binding binding = Parser.GetBinding("is (something)");
        Assert.AreEqual("is %s", binding.key);
        Parser.Token token = binding.tokens[0];
        Assert.AreEqual(Parser.TokenType.Variable, token.type);
        Assert.AreEqual("something", token.value);

        binding = Parser.GetBinding("(someone) is (something)");
        Parser.Token token1 = binding.tokens[0];
        Parser.Token token2 = binding.tokens[1];
        Assert.AreEqual(Parser.TokenType.Variable, token1.type);
        Assert.AreEqual("someone", token1.value);
        Assert.AreEqual(Parser.TokenType.Variable, token2.type);
        Assert.AreEqual("something", token2.value);

        binding = Parser.GetBinding("(someone) is \"a big meanie\"");
        token1  = binding.tokens[0];
        token2  = binding.tokens[1];
        Assert.AreEqual(Parser.TokenType.Variable, token1.type);
        Assert.AreEqual(Parser.TokenType.String, token2.type);
        Assert.AreEqual("someone", token1.value);
        Assert.AreEqual("a big meanie", token2.value);
    }
示例#11
0
    /// <summary>
    /// Relational():  Detects the operation to perform a relational operator (>, <, <=, etc.).
    /// </summary>
    /// <returns>CValue object representing an operation.</returns>
    private CValue Relational()
    {
        CValue oValue = AddSub();

        while (m_currentToken == "&&" ||
               m_currentToken == "||" ||
               m_currentToken == "==" ||
               m_currentToken == "<" ||
               m_currentToken == ">" ||
               m_currentToken == "<=" ||
               m_currentToken == ">=" ||
               m_currentToken == "!=" ||
               m_currentToken == "<>")
        {
            Parser.Token oOp = m_currentToken;
            PositionNextToken();
            CValue oNextVal = Relational();

            CheckParms(oOp, oValue, oNextVal);
            oValue = OpFactory(oOp, oValue, oNextVal);
        }

        return(oValue);
    }
示例#12
0
 public Evaluator(Parser.Token Tokens)
 {
     token = Tokens;
 }
示例#13
0
 public PostfixExpression(IExpression left, Parser.Token tokenId)
 {
     Left    = left;
     TokenId = tokenId;
 }
 public BinaryOperatorExpression(IExpression left, Parser.Token tokenId, IExpression right)
 {
     Left    = left;
     TokenId = tokenId;
     Right   = right;
 }
示例#15
0
 public PrefixExpression(Parser.Token tokenId, IExpression right)
 {
     TokenId = tokenId;
     Right   = right;
 }
示例#16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Evaluator" /> class.
 /// </summary>
 /// <param name="Tokens">The tokens.</param>
 public Evaluator(Parser.Token Tokens)
 {
     token = Tokens;
 }
示例#17
0
        private bool Eval(Parser.TokenItems Parameters, out Parser.TokenItem Result, out string ErrorMsg)
        {
            // initialize the outgoing variables
            ErrorMsg = "";
            Result = null;

            // make sure we have at least 1 parameter
            if (Parameters.Count != 1)
            {
                ErrorMsg = "Eval[] Operand Function requires 1 parameter.";
                return false;
            }

            // get the expression to be evaluated
            string expression = Support.DataTypeCheck.RemoveTextQuotes(Parameters[0].TokenName);

            // create the tokens
            Parser.Token tokens = new Parser.Token(expression);
            if (tokens.AnyErrors == true)
            {
                ErrorMsg = tokens.LastErrorMessage;
                return false;
            }

            // create the evaluator object
            Evaluator e = new Evaluator(tokens);

            string value = "";

            if (e.Evaluate(out value, out ErrorMsg) == false) return false;

            if (Support.DataTypeCheck.IsInteger(value) == true)
                Result = new Parser.TokenItem(value, Parser.TokenType.Token_Operand, Parser.TokenDataType.Token_DataType_Int, false);
            else if (Support.DataTypeCheck.IsDouble(value) == true)
                Result = new Parser.TokenItem(value, Parser.TokenType.Token_Operand, Parser.TokenDataType.Token_DataType_Double, false);
            else if (Support.DataTypeCheck.IsDate(value) == true)
                Result = new Parser.TokenItem(value, Parser.TokenType.Token_Operand, Parser.TokenDataType.Token_DataType_Date, false);
            else
                Result = new Parser.TokenItem(value, Parser.TokenType.Token_Operand, Parser.TokenDataType.Token_DataType_String, false);

            return true;
        }
示例#18
0
 public AnnoNode(Parser.Token token) : base(token)
 {
 }
示例#19
0
 public override bool IsMatch(Parser.Token tkn)
 {
     return(tkn.ToString() == "<=");
 }
示例#20
0
 /// <summary>
 /// IsMatch(): accepts an operation token and identifies if the implemented
 ///		operator class is responsible for it.  This allows for multiple operators
 ///		to represent a given operation (i.e. != and <> both represent the "not-equal" case.
 /// </summary>
 /// <param name="tkn">Parser.Token containing the operator in question</param>
 /// <returns>bool returning true if the object is reponsible for implementing the operator at hand.</returns>
 public abstract bool IsMatch(Parser.Token tkn);