Ejemplo n.º 1
0
        // for operator
        internal static void ParseOperatorTokens(int start_pos, string operator_string, ref List<AToken> list, ref List<AToken> declare_list)
        {
            int op_start = 0;
            int op_len = operator_string.Length;
            // need check Prefix Unary operator if there is no operand before or there is non-CLOSE operator before
            AToken lastToken = list.Count > 0 ? list[list.Count - 1] : null;
            bool checkPreUnary = lastToken == null || lastToken.op != null && lastToken.op.op_type != OPERATOR_TYPE.CLOSE;

            while (op_len > 0)
            {
                string op_str = operator_string.Substring(op_start, op_len);
                AnOperator exp_op = null;

                if (checkPreUnary && AnOperator.dicSystemOperator.ContainsKey(op_str + "U"))
                    exp_op = AnOperator.dicSystemOperator[op_str + "U"];
                if (exp_op == null && AnOperator.dicSystemOperator.ContainsKey(op_str))
                    exp_op = AnOperator.dicSystemOperator[op_str];
                if (exp_op != null)
                {
                    //if (op_str == "(" && lastToken != null && lastToken.tok_type == TOKEN_TYPE.VARIABLE) lastToken.tok_type = TOKEN_TYPE.FUNC;
                    AToken tok = new AToken(start_pos + op_start, TOKEN_TYPE.OPERATOR, exp_op, op_str);
                    if (tok.value == "=>" && declare_list==null)
                    {
                        declare_list = list;
                        list = new List<AToken>();
                        lastToken = null;
                    }
                    else
                    {
                        list.Add(tok);
                        lastToken = tok;
                    }
                    op_start += op_len;
                    op_len = operator_string.Length - op_start;
                    checkPreUnary = exp_op.op_type != OPERATOR_TYPE.CLOSE;
                }
                else
                    op_len--;
            }
            if (op_len == 0 && op_start < operator_string.Length)
            {
                // unknown operator
                string op_str = operator_string.Substring(op_start);
                list.Add(new AToken(start_pos + op_start, TOKEN_TYPE.OPERATOR, new AnOperator(OPERATOR_TYPE.UNKNOWN, 0), op_str));
            }
        }
Ejemplo n.º 2
0
 internal TokenStore NextUntilOperatorClose()
 {
     AToken start_tok = this.Current;
     int level = 1;
     List<AToken> list = new List<AToken>();
     for (this.current_token_idx++; this.current_token_idx < this.token_list.Count; this.current_token_idx++)
     {
         AToken curr_tok = this.token_list[this.current_token_idx];
         if (curr_tok.op != null)
         {
             if (start_tok.value == curr_tok.value) level++;
             else if (start_tok.value == "(" && curr_tok.value == ")" // ( )
                     || start_tok.value == "[" && curr_tok.value == "]"       // [ ]
                     || start_tok.value == "{" && curr_tok.value == "}"         // { }
                     || start_tok.value == "<" && curr_tok.value == ">")          // < >
                 level--;
             else if (start_tok.value == "<" && curr_tok.value == ">>" && level >= 2)    // v<xxx<xx>>
             {
                 level -= 2;
                 list.Add(new AToken(curr_tok.start_pos, TOKEN_TYPE.OPERATOR, AnOperator.dicSystemOperator[">"], ">"));
                 curr_tok = new AToken(curr_tok.start_pos+1, TOKEN_TYPE.OPERATOR, AnOperator.dicSystemOperator[">"], ">");
             }
             if (level == 0) break;
         }
         list.Add(curr_tok);
     }
     if (level != 0) throw new ExprException("Expecting closing operator for operator: '" + start_tok.value + "'");
     this.current_token_idx++;   // move to next token after close
     return new TokenStore(this.original_string, list);
 }
Ejemplo n.º 3
0
 private void Assert(bool check, AToken tok)
 {
     if (!check)
         if (tok == null)
             throw new ExprException("Unexpected end.");
         else
             throw new ExprException(string.Format("Unexpected token({0}) at: {1}.", tok.value, tok.start_pos+1));
 }