Example #1
0
 internal ConstantNode(Token token)
 {
     _token = token;
     _value = token.Value;
 }
        //#if !NETMF
        //        public static IEnumerable<ExpressionVariable> EnumerateVariables(string expression)
        //        {
        //            int index = 0;
        //            int start;
        //            int len = expression.Length;
        //            int iline = 0;
        //            int ichar = 0;

        //            while (index < len)
        //            {
        //                var token = GetNextToken(expression, ref index, out start, ref iline, ref ichar);
        //                if (token.TokenType == TokenTypes.Variable)
        //                {
        //                    yield return new ExpressionVariable
        //                    {
        //                        Name = (string)token.Value,
        //                        Start = start,
        //                        Length = index - start
        //                    };
        //                }
        //                else if (token.TokenType == TokenTypes.Term)
        //                {
        //                    foreach (var subVar in EnumerateVariables((string)token.Value))
        //                    {
        //                        yield return new ExpressionVariable
        //                        {
        //                            Name = subVar.Name,
        //                            Start = start + 1 + subVar.Start, // offset of term + 1 for bracket
        //                            Length = subVar.Length
        //                        };
        //                    }
        //                }
        //            }
        //        }
        //#endif

        private Token GetNextToken(string exp, ref int index, out int start, ref int iline, ref int ichar)
        {
            Token token = new InvalidToken();

            while (index < exp.Length && IsWhiteSpace(exp[index]))
            {
                if (exp[index] == '\n')
                {
                    ichar = 1;
                    ++iline;
                }
                ++ichar;

                ++index;
            }

            start = index;

            if (index >= exp.Length || start >= exp.Length || exp[index] == ';')
            {
                token = new Token {
                    TokenType = TokenTypes.End,
                    lineStart = iline,
                    lineCharStart = ichar,
                    charStart = start,
                    charEnd = index - start
                };
            }
            else if (exp[index] == '\n')
            {
                token = new Token {
                    TokenType = TokenTypes.NewLine,
                    lineStart = iline,
                    lineCharStart = ichar,
                    charStart = start,
                    charEnd = index - start
                };
            }
            else if (exp[index] == '(')
            {
                index = MatchCharPair(exp, start, '(', ')') + 1;
                token = new Token
                {
                    TokenType = TokenTypes.Term,
                    Value = exp.Substring(start + 1, (index - start) - 2),
                    lineStart = iline,
                    lineCharStart = ichar,
                    charStart = start,
                    charEnd = index - start
                };
            }
            else if (exp[index] == '{')
            {
                index = MatchCharPair(exp, start, '{', '}') + 1;
                token = new Token
                {
                    TokenType = TokenTypes.Term,
                    Value = exp.Substring(start + 1, (index - start) - 2),
                    lineStart = iline,
                    lineCharStart = ichar,
                    charStart = start,
                    charEnd = index - start
                };
            }
            else if (exp[index] == '[')
            {
                index = MatchCharPair(exp, start, '[', ']') + 1;
                token = new Token
                {
                    TokenType = TokenTypes.Term,
                    Value = exp.Substring(start + 1, (index - start) - 2),
                    lineStart = iline,
                    lineCharStart = ichar,
                    charStart = start,
                    charEnd = index - start
                };
            }
            else if (exp[index] == '\"')
            {
                index = MatchQuote(exp, start) + 1;
                token = new Token
                {
                    TokenType = TokenTypes.StringConst,
                    Value = UnescapeString(exp.Substring(start, index - start)),
                    lineStart = iline,
                    lineCharStart = ichar,
                    charStart = start,
                    charEnd = index - start
                };
            }
            else if (IsLetter(exp[index]) || exp[index] == '_' || exp[index] == ':' || exp[index] == '.' || exp[index] == '$')
            {
                ++ichar;
                ++index;

                while (index < exp.Length &&
                   (IsLetterOrDigit(exp[index]) || exp[index] == '-' || exp[index] == '_' || exp[index] == '.'))
                {
                    ++ichar;
                    if (exp[index] == '\n')
                    {
                        ichar = 1;
                        ++iline;
                    }

                    ++index;
                }

                var name = exp.Substring(start, index - start);
                if (IsCmdlet(name))
                {

                }
                else if (IsOperator(name))
                {
                    token = new Token
                    {
                        TokenType = TokenTypes.Operator,
#if NETMF
                        Operator = _Operators[name.ToLower()] as Operator
#else
                        Operator = _Operators[name.ToLowerInvariant()]
#endif
                        ,
                        lineStart = iline,
                        lineCharStart = ichar,
                        charStart = start,
                        charEnd = index - start
                    };
                }
                else if (IsConstant(name))
                {
                    token = new Token
                    {
                        TokenType = TokenTypes.Constant,
                        Value = _Constants[name.ToLower()],
                        //Value = name,
                        lineStart = iline,
                        lineCharStart = ichar,
                        charStart = start,
                        charEnd = index - start
                    };
                }
                else if (IsVariable(name))
                {
                    if (!this._Variables.ContainsKey(name))
                    {
                        this._Variables.Add(name, new object());
                    }

                    token = new Token
                    {
                        TokenType = TokenTypes.Variable,
                        Value = name,
                        lineStart = iline,
                        lineCharStart = ichar,
                        charStart = start,
                        charEnd = index - start
                    };
                }
                else
                {
                    throw new CommandNotFoundException(String.Format(
@"{0} : The term '{0}' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:{1} char:{2}
+ {0}
+ ~~~~
    +CategoryInfo          : ObjectNotFound: ({0}:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
.", name, iline, ichar));
                }
            }
            else if (IsDigit(exp[index]))
            {
                ++ichar;
                ++index;
                while (index < exp.Length &&
                   (IsDigit(exp[index]) || exp[index] == '.' || exp[index] == 'e'))
                {
                    ++ichar;
                    if (exp[index] == '\n')
                    {
                        ichar = 1;
                        ++iline;
                    }

                    ++index;
                }

                var no = exp.Substring(start, index - start);

                token = new Token
                {
                    TokenType = TokenTypes.Number,
                    lineStart = iline,
                    lineCharStart = ichar,
                    charStart = start,
                    charEnd = index - start
                };

                if (no.IndexOf('.') >= 0 || no.IndexOf('e') >= 0)
                {
                    token.Value = Double.Parse(no);
                }
                else
                {
                    token.Value = Int32.Parse(no);
                }
            }
            else if (IsOperatorSym(exp[index]))
            {
                ++ichar;
                ++index;
                while (index < exp.Length && IsOperatorSym(exp[index]))
                {
                    if (exp[index] == '\n')
                    {
                        ichar = 1;
                        ++iline;
                    }
                    ++index;
                }

                var ops = exp.Substring(start, index - start);
                if (IsAssignmentOperator(ops))
                {
                    token = new Token
                    {
                        TokenType = TokenTypes.Assignment,
                        Operator = new Operator("=", 0, true),
                        lineStart = iline,
                        lineCharStart = ichar,
                        charStart = start,
                        charEnd = index - start
                    };
                }
                else if (IsOperator(ops))
                {
                    token = new Token
                    {
                        TokenType = TokenTypes.Operator,
#if NETMF
                        Operator = _Operators[ops.ToLower()] as Operator
#else
                        Operator = _Operators[ops.ToLowerInvariant()]
#endif
                        ,
                        lineStart = iline,
                        lineCharStart = ichar,
                        charStart = start,
                        charEnd = index - start
                    };
                }
                else
                {
                    throw new Exception("Operator symbol matched but no Operator found.");
                }
            }

            // TODO: Figure out why this goes crazy!
            if (token != null)
            // But this is fine
            //if (!(token is InvalidToken))
            {
                return token;
            }
            else
            {
                throw new ParserException(String.Concat("Unallowed symbol: '", exp.Substring(start, index - start), "'"));
            }
        }