private static TypeSpecifier ParseArraySpecifier(CharReader reader)
        {
            System.Diagnostics.Debug.Assert(reader.Peek() == '[');
            int rank = 1;

            // Consume the leading '['
            reader.Read();

            while (true)
            {
                int ch = reader.Read();
                switch (ch)
                {
                case ',':
                    rank++;
                    break;

                case ']':
                    return(TypeSpecifier.Array(rank));

                case '*':
                    int la1 = reader.Peek();
                    if (la1 != ',' && la1 != ']')
                    {
                        throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(la1 == -1 ? "EOS" : ((char)la1).ToString())}' at position {reader.Position}; expected one of ',', ']'.");
                    }
                    break;

                default:
                    throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(char)ch}' at position {reader.Position}; one of ',', ']', '*'.");
                }
            }
        }
        private static bool TryParseIdentifierInto(CharReader reader, StringBuilder target)
        {
            int startPos = reader.Position;
            int ch;

            while ((ch = reader.Peek()) != -1)
            {
                if (ch != '\\' && IsSpecialCharacter((char)ch))
                {
                    break;
                }

                reader.Read();

                if (ch == '\\' && reader.HasMore)
                {
                    target.Append('\\');
                    ch = reader.Read();
                }

                target.Append((char)ch);
            }

            return(reader.Position > startPos);
        }
예제 #3
0
        public void CharReaderShouldReadCharacters()
        {
            string     source = "abc";
            CharReader reader = new CharReader(source);

            Assert.False(reader.IsDone);
            Assert.Equal('a', reader.Read());
            Assert.Equal('b', reader.Read());
            Assert.Equal('c', reader.Read());
            Assert.Equal(CharReader.EOF, reader.Read());
            Assert.True(reader.IsDone);
        }
        private static IList <TypeSpecifier> ParseRefPtrArrSpec(CharReader reader)
        {
            List <TypeSpecifier> specifiers = null;
            int ch;

            while ((ch = reader.Peek()) != -1)
            {
                TypeSpecifier specifier;

                switch (ch)
                {
                case '[':
                    int la1 = reader.Peek(1);
                    if (la1 == '*' || la1 == ']' || la1 == ',')
                    {
                        specifier = ParseArraySpecifier(reader);
                    }
                    else
                    {
                        return(specifiers);
                    }
                    break;

                case '*':
                    specifier = TypeSpecifier.Pointer;
                    reader.Read();
                    break;

                case '&':
                    specifier = TypeSpecifier.Reference;
                    reader.Read();
                    break;

                case ',':
                case ']':
                    return(specifiers);

                default:
                    throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(char)ch}' at position {reader.Position}; one of '[', '*', '&', ',', ']'.");
                }

                if (specifiers == null)
                {
                    specifiers = new List <TypeSpecifier>();
                }

                specifiers.Add(specifier);
            }

            return(specifiers);
        }
예제 #5
0
        static private Token[] ExpandParameters(CharReader reader, StringBuilder literal, IResolver resolver)
        {
            List <Token> tokens = new List <Token>();

            while (reader.Next())
            {
                char c = reader.Current;
                switch (c)
                {
                case ',':
                    if (literal.Length == 0)
                    {
                        throw ThrowSyntaxError(reader.Position);
                    }

                    literal.Length = 0;
                    break;

                case ')':
                    literal.Length = 0;
                    return(tokens.ToArray());

                case '\'':
                    reader.Read();     //skip the char
                    tokens.Add(ExpandString(reader, literal));
                    continue;

                default:
                    if (char.IsLetter(c))
                    {
                        tokens.Add(ExpandVariable(reader, literal, resolver));
                        continue;
                    }
                    else if (char.IsDigit(c))
                    {
                        tokens.Add(ExpandNumber(reader, literal));
                        continue;
                    }
                    else
                    {
                        // it is whitespace or newline, to do nothing...
                    }
                    break;
                }
                literal.Length = 0;
                reader.Read();
            }
            throw ThrowSyntaxError(reader.Position);
        }
        // ID ('.' ID)* ('+' ID)*
        private static (string Namespace, List <string> NestedTypeName) ParseNamespaceTypeName(CharReader reader, bool allowTrailingCharacters)
        {
            List <string> nestedTypeName   = new List <string>();
            StringBuilder namespaceBuilder = new StringBuilder();
            int           lastDelimiterPos = -1;

            // Parse namespace including root type name, stop at nested types.
            while (reader.HasMore && TryParseIdentifierInto(reader, namespaceBuilder))
            {
                if (reader.Peek() == '.')
                {
                    lastDelimiterPos = namespaceBuilder.Length;
                    namespaceBuilder.Append('.');
                    reader.Read();
                }
                else
                {
                    break;
                }
            }

            // Verify that we actually parsed something.
            if (namespaceBuilder.Length == 0)
            {
                throw new TypeNameParserException($"Failed to parse type name from \"{reader.Data}\"; Expected NamespaceTypeName, but none found.");
            }

            // The type name is the identifier following the last dot. Extract that from the namespaceBuilder.
            nestedTypeName.Add(namespaceBuilder.ToString(lastDelimiterPos + 1, namespaceBuilder.Length - lastDelimiterPos - 1));
            namespaceBuilder.Length = lastDelimiterPos == -1 ? 0 : lastDelimiterPos;


            // Now parse any NestedTypeNames
            while (reader.Peek() == '+')
            {
                // Consume the +
                reader.Read();

                nestedTypeName.Add(ParseIdentifier(reader));
            }

            if (!allowTrailingCharacters && reader.HasMore)
            {
                throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(char)reader.Peek()}' at position {reader.Position}; expected end-of-string.");
            }

            return((namespaceBuilder.Length == 0) ? null : namespaceBuilder.ToString(), nestedTypeName);
        }
예제 #7
0
        static private Token ExpandString(CharReader reader, StringBuilder literal)
        {
            bool expandEscapeChar = false;
            char c;

            while (reader.Next())
            {
                c = reader.Read();
                if (expandEscapeChar)
                {
                    literal.Append(c);
                }
                else if (c == '\\')
                {
                    expandEscapeChar = true;
                }
                else if (c == '\'')
                {
                    break;
                }
                else
                {
                    literal.Append(c);
                }
            }
            return(literal.ToString());
        }
예제 #8
0
        public void CharReaderKeepsReadingAfterFoundPosition()
        {
            string     source = "abc:123";
            CharReader reader = new CharReader(source);

            reader.ReadTo(':', false);
            Assert.Equal('1', reader.Read());
        }
        private static void ConsumeWhitespace(CharReader reader, StringBuilder target)
        {
            int ch;

            while ((ch = reader.Peek()) != -1 && Char.IsWhiteSpace((char)ch))
            {
                target.Append((char)reader.Read());
            }
        }
        private static void ConsumeUnquotedValue(CharReader reader, StringBuilder target)
        {
            int ch;

            while ((ch = reader.Peek()) != -1 && ch != ',' && ch != ']' && !Char.IsWhiteSpace((char)ch))
            {
                target.Append((char)reader.Read());
            }
        }
        private static void SkipWhitespace(CharReader reader)
        {
            int ch;

            while ((ch = reader.Peek()) != -1 && Char.IsWhiteSpace((char)ch))
            {
                reader.Read();
            }
        }
예제 #12
0
        static private Token ExpandVariable(CharReader reader, StringBuilder literal, IResolver resolver)
        {
            char c;

            while (reader.Next())
            {
                c = reader.Current;
                if (char.IsLetterOrDigit(c))
                {
                    literal.Append(c);
                }
                else
                {
                    if (literal.ToString() == "true")
                    {
                        return(true);
                    }
                    else if (literal.ToString() == "false")
                    {
                        return(false);
                    }
                    else if (char.IsWhiteSpace(c))
                    {
                        continue;
                    }
                    else if (c == '(')
                    {
                        string name = literal.ToString();
                        literal.Length = 0;
                        reader.Read(); // skip the char
                        Token[] args   = ExpandParameters(reader, literal, resolver);
                        Token   result = resolver.HandleFunction(name, args);
                        return(result);
                    }
                    else
                    {
                        Token result = resolver.HandleVariable(literal.ToString());
                        return(result);
                    }
                }
                reader.Read();
            }
            return(Token.Null);
        }
        private static int ConsumeAssemblyName(CharReader reader, StringBuilder target)
        {
            int pos = reader.Position;
            int ch;

            while ((ch = reader.Peek()) != -1 && ch != ',' && ch != ']')
            {
                target.Append((char)reader.Read());
            }
            return(reader.Position - pos);
        }
        private static void ConsumeQuotedValue(CharReader reader, StringBuilder target)
        {
            target.Append((char)reader.Read()); // Leading quote

            int ch;

            while ((ch = reader.Peek()) != -1)
            {
                target.Append((char)reader.Read());
                if (ch == '\"')
                {
                    break;
                }
            }

            if (ch != '\"')
            {
                throw new ArgumentException("Invalid type name; Missing closing quote in assembly name property value.");
            }
        }
        private static void ConsumeAssemblyNameProperties(CharReader reader, StringBuilder assemblyName)
        {
            ConsumeWhitespace(reader, assemblyName);
            while (ConsumeAssemblyNamePropertyName(reader, assemblyName))
            {
                ConsumeWhitespace(reader, assemblyName);
                if (reader.Peek() != '=')
                {
                    throw new ArgumentException($"Invalid type name; Missing value for assembly name property.");
                }
                assemblyName.Append((char)reader.Read()); // Consume the equal sign.
                ConsumeAssemblyNamePropertyValue(reader, assemblyName);
                ConsumeWhitespace(reader, assemblyName);
                if (reader.Peek() != ',')
                {
                    break;
                }

                // Consume the comma.
                assemblyName.Append((char)reader.Read());
            }
        }
        private static bool ConsumeAssemblyNamePropertyName(CharReader reader, StringBuilder target)
        {
            int initialLength = target.Length;

            ConsumeWhitespace(reader, target);
            int ch;

            while ((ch = reader.Peek()) != -1 && ch != '=' && !Char.IsWhiteSpace((char)ch) && ch != ',')
            {
                target.Append((char)reader.Read());
            }

            return(target.Length > initialLength);
        }
        private static IList <TypeIdentifier> ParseGenericArguments(CharReader reader)
        {
            List <TypeIdentifier> args = new List <TypeIdentifier>();

            if (reader.Peek() == '[')
            {
                do
                {
                    reader.Read();
                    bool fullyQualified = false;
                    if (reader.Peek() == '[')
                    {
                        fullyQualified = true;
                        reader.Read();
                    }

                    args.Add(ParseTypeIdentifier(reader, fullyQualified));


                    if (fullyQualified == true)
                    {
                        if (reader.Peek() == ']')
                        {
                            reader.Read();
                        }
                        else
                        {
                            throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(char)reader.Peek()}' at position {reader.Position}; expected ']'.");
                        }
                    }
                }while (reader.Peek() == ',');

                reader.Read(); // Consume closing ']'
            }
            return(args);
        }
예제 #18
0
        static private Token ExpandNumber(CharReader reader, StringBuilder literal)
        {
            char c;

            while (reader.Next())
            {
                c = reader.Current;
                if (char.IsDigit(c))
                {
                    literal.Append(c);
                }
                else
                {
                    break;
                }
                reader.Read();
            }
            return(int.Parse(literal.ToString()));
        }
        private static (string Namespace, List <string> NestedTypeName, IList <TypeIdentifier> GenericArguments, IList <TypeSpecifier> Specifiers, AssemblyName AssemblyName) ParseAssemblyQualifiedName(CharReader reader, bool allowTrailingCharacters)
        {
            var fullName = ParseFullName(reader, true);

            AssemblyName assemblyName = null;

            if (reader.Peek() == ',')
            {
                reader.Read();
                SkipWhitespace(reader);
                assemblyName = ParseAssemblyName(reader);
            }

            if (!allowTrailingCharacters && reader.HasMore)
            {
                throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(char)reader.Peek()}' at position {reader.Position}; expected end-of-string.");
            }

            return(fullName.Namespace, fullName.NestedTypeName, fullName.GenericArguments, fullName.Specifiers, assemblyName);
        }
        private static AssemblyName ParseAssemblyName(CharReader reader)
        {
            StringBuilder assemblyName = new StringBuilder();

            if (ConsumeAssemblyName(reader, assemblyName) == 0)
            {
                throw new ArgumentException("Invalid type name; Expected assembly name.");
            }

            ConsumeWhitespace(reader, assemblyName);

            if (reader.Peek() == ',')
            {
                assemblyName.Append((char)reader.Read());
                ConsumeWhitespace(reader, assemblyName);
                ConsumeAssemblyNameProperties(reader, assemblyName);
            }

            return(new AssemblyName(assemblyName.ToString()));
        }
예제 #21
0
        /// <summary>
        /// 从字符串解析Json对象
        /// </summary>
        public static JValue Parse(string json)
        {
            // 从0开始
            long           seek   = 0;
            CharReader     reader = new CharReader(json);
            Stack <JToken> stack  = new Stack <JToken>();
            // 当前的字符对象
            char ch = ' ';

            // 转义中
            bool transition = false;

            stack.Push(new JToken(TokenType.NONE, 0));

            while (reader.Read(ref ch))
            {
                seek = reader.Seek;

                if (transition)
                {
                    stack.Peek().AddChar(ch);
                    transition = false;
                    continue;
                }

                switch (ch)
                {
                case '\\':
                {
                    transition = true;
                }
                break;

                case '{':
                {
                    if (
                        TokenType.NONE == stack.Peek().TType ||
                        TokenType.ARRAY == stack.Peek().TType ||
                        TokenType.DATA_MARK == stack.Peek().TType ||
                        TokenType.PROPERTY_MARK == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.OBJECT, seek));
                    }
                }
                break;

                case '}':
                {
                    if (TokenType.NUMBER == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek));
                    }
                    if (TokenType.BOOLEAN == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.BOOLEAN_CLOSE, seek));
                    }
                    if (TokenType.NULL == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.NULL_CLOSE, seek));
                    }

                    if (TokenType.PROPERTY_MARK == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.NUMBER, seek));
                        stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek));
                    }

                    if (
                        TokenType.STRING_CLOSE == stack.Peek().TType ||
                        TokenType.OBJECT_CLOSE == stack.Peek().TType ||
                        TokenType.ARRAY_CLOSE == stack.Peek().TType ||
                        TokenType.NUMBER_CLOSE == stack.Peek().TType ||
                        TokenType.BOOLEAN_CLOSE == stack.Peek().TType ||
                        TokenType.NULL_CLOSE == stack.Peek().TType ||
                        TokenType.OBJECT == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.OBJECT_CLOSE, seek));
                    }
                }
                break;

                case '[':
                {
                    if (
                        TokenType.NONE == stack.Peek().TType ||
                        TokenType.ARRAY == stack.Peek().TType ||
                        TokenType.DATA_MARK == stack.Peek().TType ||
                        TokenType.PROPERTY_MARK == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.ARRAY, seek));
                    }
                }
                break;

                case ']':
                {
                    if (TokenType.NUMBER == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek));
                    }
                    if (TokenType.BOOLEAN == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.BOOLEAN_CLOSE, seek));
                    }
                    if (TokenType.NULL == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.NULL_CLOSE, seek));
                    }

                    if (TokenType.DATA_MARK == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.NUMBER, seek));
                        stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek));
                    }

                    if (
                        TokenType.STRING_CLOSE == stack.Peek().TType ||
                        TokenType.OBJECT_CLOSE == stack.Peek().TType ||
                        TokenType.ARRAY_CLOSE == stack.Peek().TType ||
                        TokenType.NUMBER_CLOSE == stack.Peek().TType ||
                        TokenType.BOOLEAN_CLOSE == stack.Peek().TType ||
                        TokenType.NULL_CLOSE == stack.Peek().TType ||
                        TokenType.ARRAY == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.ARRAY_CLOSE, seek));
                    }
                }
                break;

                case ',':
                {
                    if (TokenType.NUMBER == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek));
                    }
                    if (TokenType.BOOLEAN == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.BOOLEAN_CLOSE, seek));
                    }
                    if (TokenType.NULL == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.NULL_CLOSE, seek));
                    }

                    if (
                        TokenType.DATA_MARK == stack.Peek().TType ||
                        TokenType.PROPERTY_MARK == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.NUMBER, seek));
                        stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek));
                    }

                    if (
                        TokenType.NUMBER_CLOSE == stack.Peek().TType ||
                        TokenType.STRING_CLOSE == stack.Peek().TType ||
                        TokenType.ARRAY_CLOSE == stack.Peek().TType ||
                        TokenType.OBJECT_CLOSE == stack.Peek().TType ||
                        TokenType.BOOLEAN_CLOSE == stack.Peek().TType ||
                        TokenType.NULL_CLOSE == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.DATA_MARK, seek));
                    }
                }
                break;

                case ':':
                {
                    if (TokenType.STRING_CLOSE == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.PROPERTY_MARK, seek));
                    }
                }
                break;

                case '"':
                {
                    if (TokenType.STRING == stack.Peek().TType)
                    {
                        stack.Push(new JToken(TokenType.STRING_CLOSE, seek));
                    }

                    if (
                        TokenType.DATA_MARK == stack.Peek().TType ||
                        TokenType.PROPERTY_MARK == stack.Peek().TType ||
                        TokenType.OBJECT == stack.Peek().TType ||
                        TokenType.ARRAY == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.STRING, seek));
                    }
                }
                break;

                case '-':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                case '0':
                {
                    if (
                        TokenType.ARRAY == stack.Peek().TType ||
                        TokenType.DATA_MARK == stack.Peek().TType ||
                        TokenType.PROPERTY_MARK == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.NUMBER, seek));
                    }
                }
                break;

                case 't':
                case 'f':
                {
                    if (
                        TokenType.ARRAY == stack.Peek().TType ||
                        TokenType.DATA_MARK == stack.Peek().TType ||
                        TokenType.PROPERTY_MARK == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.BOOLEAN, seek));
                    }
                }
                break;

                case 'n':
                {
                    if (
                        TokenType.ARRAY == stack.Peek().TType ||
                        TokenType.DATA_MARK == stack.Peek().TType ||
                        TokenType.PROPERTY_MARK == stack.Peek().TType
                        )
                    {
                        stack.Push(new JToken(TokenType.NULL, seek));
                    }
                }
                break;

                default:
                    break;
                }
                stack.Peek().AddChar(ch);
            }

            // 返回结果
            return(Parse(stack));
        }
예제 #22
0
        static private ParseTree Parse(string expression, IResolver resolver)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            ParseTree     tokenTree = new ParseTree();
            StringBuilder literal   = new StringBuilder();
            CharReader    reader    = new CharReader(expression);
            Token         last      = Token.Null;

            while (reader.Next())
            {
                Debug.Assert(literal.Length == 0);
                char c = reader.Read();

                switch (c)
                {
                case '(':
                {
                    switch (last.Type)
                    {
                    case TokenType.Prefix:
                    case TokenType.Operation:
                    case TokenType.Expression:
                    case TokenType.None:
                        last = Token.Create(TokenType.Expression, null);
                        tokenTree.Append(last);
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                }
                break;

                case ')':
                {
                    switch (last.Type)
                    {
                    case TokenType.Boolean:
                    case TokenType.Number:
                    case TokenType.String:
                    case TokenType.Variable:
                    case TokenType.Function:
                    case TokenType.EndExpression:
                        last = Token.Create(TokenType.EndExpression, null);
                        tokenTree.Append(last);
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                }
                break;

                case '?':
                    switch (last.Type)
                    {
                    case TokenType.Boolean:
                    case TokenType.Number:
                    case TokenType.String:
                    case TokenType.Variable:
                    case TokenType.Function:
                    case TokenType.EndExpression:
                        last = Token.CreateOperator(c.ToString());
                        tokenTree.Append(last);
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                    break;

                case '|':
                case '&':
                    switch (last.Type)
                    {
                    case TokenType.Boolean:
                    case TokenType.Number:
                    case TokenType.String:
                    case TokenType.Variable:
                    case TokenType.Function:
                    case TokenType.Expression:
                        literal.Append(c);
                        if (!reader.Next())
                        {
                            throw ThrowSyntaxError(reader.Position);
                        }
                        if (reader.Current != c)
                        {
                            throw ThrowSyntaxError(reader.Position);
                        }

                        literal.Append(reader.Read());

                        last = Token.CreateOperator(literal.ToString());
                        tokenTree.Append(last);
                        literal.Length = 0;
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                    break;

                case '!':
                    switch (last.Type)
                    {
                    case TokenType.Operation:
                    case TokenType.Expression:
                    case TokenType.None:
                        // prefix
                        last = Token.CreatePrefix(c.ToString());
                        tokenTree.Append(last);
                        break;

                    case TokenType.Boolean:
                    case TokenType.Number:
                    case TokenType.String:
                    case TokenType.Variable:
                    case TokenType.Function:
                    case TokenType.EndExpression:
                        // operator
                        literal.Append(c);
                        if (!reader.Next())
                        {
                            throw ThrowSyntaxError(reader.Position);
                        }
                        if (reader.Current != '=')
                        {
                            throw ThrowSyntaxError(reader.Position);
                        }

                        literal.Append(reader.Read());

                        last = Token.CreateOperator(literal.ToString());
                        tokenTree.Append(last);
                        literal.Length = 0;
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                    break;

                case '-':
                    switch (last.Type)
                    {
                    case TokenType.Operation:
                    case TokenType.Expression:
                    case TokenType.None:
                        // prefix
                        last = Token.CreatePrefix(c.ToString());
                        tokenTree.Append(last);
                        break;

                    case TokenType.Boolean:
                    case TokenType.Number:
                    case TokenType.String:
                    case TokenType.Variable:
                    case TokenType.Function:
                    case TokenType.EndExpression:
                        // operator
                        last = Token.CreateOperator(c.ToString());
                        tokenTree.Append(last);
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                    break;

                case '+':
                case '*':
                case '/':
                case '%':
                    switch (last.Type)
                    {
                    case TokenType.Boolean:
                    case TokenType.Number:
                    case TokenType.String:
                    case TokenType.Variable:
                    case TokenType.Function:
                    case TokenType.EndExpression:
                        last = Token.CreateOperator(c.ToString());
                        tokenTree.Append(last);
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                    break;

                case '>':
                case '<':
                    switch (last.Type)
                    {
                    case TokenType.Boolean:
                    case TokenType.Number:
                    case TokenType.String:
                    case TokenType.Variable:
                    case TokenType.Function:
                    case TokenType.EndExpression:
                        literal.Append(c);
                        if (!reader.Next())
                        {
                            throw ThrowSyntaxError(reader.Position);
                        }
                        if (reader.Current == '=')
                        {
                            literal.Append(reader.Read());
                        }
                        last = Token.CreateOperator(literal.ToString());
                        tokenTree.Append(last);
                        literal.Length = 0;
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                    break;

                case '=':
                    switch (last.Type)
                    {
                    case TokenType.Boolean:
                    case TokenType.Number:
                    case TokenType.String:
                    case TokenType.Variable:
                    case TokenType.Function:
                    case TokenType.EndExpression:
                        literal.Append(c);
                        if (!reader.Next())
                        {
                            throw ThrowSyntaxError(reader.Position);
                        }
                        if (reader.Current != '=')
                        {
                            throw ThrowSyntaxError(reader.Position);
                        }

                        literal.Append(reader.Read());

                        last = Token.CreateOperator(literal.ToString());
                        tokenTree.Append(last);
                        literal.Length = 0;
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                    break;

                case '\'':
                    switch (last.Type)
                    {
                    case TokenType.Operation:
                    case TokenType.Expression:
                    case TokenType.None:
                        last = ExpandString(reader, literal);
                        tokenTree.Append(last);
                        literal.Length = 0;
                        break;

                    default:
                        throw ThrowSyntaxError(reader.Position);
                    }
                    break;

                default:
                    if (char.IsLetter(c))
                    {
                        switch (last.Type)
                        {
                        case TokenType.Prefix:
                        case TokenType.Operation:
                        case TokenType.Expression:
                        case TokenType.None:
                            literal.Append(c);
                            last = ExpandVariable(reader, literal, resolver);
                            tokenTree.Append(last);
                            literal.Length = 0;
                            break;

                        default:
                            throw ThrowSyntaxError(reader.Position);
                        }
                    }
                    else if (char.IsDigit(c))
                    {
                        switch (last.Type)
                        {
                        case TokenType.Prefix:
                        case TokenType.Operation:
                        case TokenType.Expression:
                        case TokenType.None:
                            literal.Append(c);
                            last = ExpandNumber(reader, literal);
                            tokenTree.Append(last);
                            literal.Length = 0;
                            break;

                        default:
                            throw ThrowSyntaxError(reader.Position);
                        }
                    }
                    else
                    {
                        // it is whitespace or newline, to do nothing...
                    }
                    break;
                }
            }
            return(tokenTree);
        }
예제 #23
0
        /// <summary>
        ///		Lee los tokens del archivo
        /// </summary>
        internal IEnumerable <TokenModel> Read()
        {
            string content = string.Empty;

            TokenModel.TokenType tokenReaded = TokenModel.TokenType.Unknown;
            bool endToken = false, specialChar = false;

            // Lee los caracteres y crea los tokens
            foreach ((CharFileReader.CharType type, char character)readed in CharReader.Read())
            {
                // Añade el carácter al contenido y cierra el token si es necesario
                switch (readed.type)
                {
                case CharFileReader.CharType.Character:
                    // Lee el carácter
                    if (readed.character == ' ' || readed.character == '\t' || readed.character == '\r' || readed.character == '\n')
                    {
                        endToken = true;
                    }
                    else                                             // Trata los caracteres especiales
                    {
                        switch (readed.character)
                        {
                        case '[':
                            tokenReaded = TokenModel.TokenType.StartTag;
                            break;

                        case ']':
                        case '}':
                        case ')':
                        case '>':
                            specialChar = true;
                            break;

                        case '{':
                            tokenReaded = TokenModel.TokenType.StartComment;
                            break;

                        case ';':
                            tokenReaded = TokenModel.TokenType.StartCommentToEoL;
                            break;

                        case '(':
                            tokenReaded = TokenModel.TokenType.StartRecursiveVariation;
                            break;

                        case '<':
                            tokenReaded = TokenModel.TokenType.StartExpansion;
                            break;

                        case '"':
                            specialChar = true;
                            break;

                        default:
                            content += readed.character;
                            break;
                        }
                    }
                    // Si se ha leído un carácter especial, se marca el final del token
                    if (tokenReaded != TokenModel.TokenType.Unknown)
                    {
                        endToken = true;
                    }
                    break;

                case CharFileReader.CharType.EoL:
                case CharFileReader.CharType.EoF:
                    specialChar = true;
                    break;
                }
                // Devuelve el token
                if (specialChar)
                {
                    // Devuelve el token que teníamos hasta ahora
                    if (tokenReaded != TokenModel.TokenType.Unknown || !string.IsNullOrEmpty(content))
                    {
                        yield return(new TokenModel(tokenReaded, content));
                    }
                    // Devuelve un token con el carácter especial
                    switch (readed.type)
                    {
                    case CharFileReader.CharType.EoL:
                        yield return(new TokenModel(TokenModel.TokenType.EoL, string.Empty));

                        break;

                    case CharFileReader.CharType.EoF:
                        yield return(new TokenModel(TokenModel.TokenType.EoF, string.Empty));

                        break;

                    default:
                        switch (readed.character)
                        {
                        case '"':
                            yield return(new TokenModel(TokenModel.TokenType.Quote, string.Empty));

                            break;

                        case '}':
                            yield return(new TokenModel(TokenModel.TokenType.EndComment, string.Empty));

                            break;

                        case ']':
                            yield return(new TokenModel(TokenModel.TokenType.EndTag, string.Empty));

                            break;

                        case ')':
                            yield return(new TokenModel(TokenModel.TokenType.EndRecursiveVariation, string.Empty));

                            break;

                        case '>':
                            yield return(new TokenModel(TokenModel.TokenType.EndExpansion, string.Empty));

                            break;
                        }
                        break;
                    }
                }
                // Si hemos llegado al final del token, lo devuelve
                if (endToken && (tokenReaded != TokenModel.TokenType.Unknown || !string.IsNullOrEmpty(content)))
                {
                    yield return(new TokenModel(tokenReaded, content));
                }
                // Inicializa los datos si es necesario
                if (specialChar || endToken)
                {
                    tokenReaded = TokenModel.TokenType.Unknown;
                    content     = string.Empty;
                    endToken    = false;
                    specialChar = false;
                }
            }
        }
예제 #24
0
        // Add some non-zero constants to the mix.
        public void AddConstantsToTDB(RandoopConfiguration config)
        {
            foreach (SimpleTypeValues vs in config.simpleTypeValues)
            {
                Type type = Type.GetType(vs.simpleType);

                if (type == null)
                {
                    throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file.");
                }

                foreach (FileName fn in vs.fileNames)
                {
                    string fileName = fn.fileName;
                    if (!File.Exists(fileName))
                    {
                        throw new Common.RandoopBareExceptions.InvalidUserParamsException("Configuration file does not exist: " + fileName);
                    }

                    if (type.Equals(typeof(sbyte)))
                    {
                        SByteReader r = new SByteReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(sbyte), o));
                    }
                    else if (type.Equals(typeof(byte)))
                    {
                        ByteReader r = new ByteReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(byte), o));
                    }
                    else if (type.Equals(typeof(short)))
                    {
                        ShortReader r = new ShortReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(short), o));
                    }
                    else if (type.Equals(typeof(ushort)))
                    {
                        UshortReader r = new UshortReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(ushort), o));
                    }
                    else if (type.Equals(typeof(int)))
                    {
                        IntReader r = new IntReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(int), o));
                    }
                    else if (type.Equals(typeof(uint)))
                    {
                        UintReader r = new UintReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(uint), o));
                    }
                    else if (type.Equals(typeof(long)))
                    {
                        LongReader r = new LongReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(long), o));
                    }
                    else if (type.Equals(typeof(ulong)))
                    {
                        UlongReader r = new UlongReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(ulong), o));
                    }
                    else if (type.Equals(typeof(char)))
                    {
                        CharReader r = new CharReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(char), o));
                    }
                    else if (type.Equals(typeof(float)))
                    {
                        FloatReader r = new FloatReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(float), o));
                    }
                    else if (type.Equals(typeof(double)))
                    {
                        DoubleReader r = new DoubleReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(double), o));
                    }
                    else if (type.Equals(typeof(bool)))
                    {
                        BoolReader r = new BoolReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(bool), o));
                    }
                    else if (type.Equals(typeof(decimal)))
                    {
                        DecimalReader r = new DecimalReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(decimal), o));
                    }
                    else
                    {
                        if (!type.Equals(typeof(string)))
                        {
                            throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file.");
                        }
                        Common.StringReader r = new Common.StringReader();
                        foreach (object o in r.Read(fileName))
                            this.AddPlan(Plan.Constant(typeof(string), o));
                    }
                }
            }
        }
예제 #25
0
        // Add some non-zero constants to the mix.
        public void AddConstantsToTDB(RandoopConfiguration config)
        {
            foreach (SimpleTypeValues vs in config.simpleTypeValues)
            {
                Type type = Type.GetType(vs.simpleType);

                if (type == null)
                {
                    throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file.");
                }

                foreach (FileName fn in vs.fileNames)
                {
                    string fileName = fn.fileName;
                    if (!File.Exists(fileName))
                    {
                        throw new Common.RandoopBareExceptions.InvalidUserParamsException("Configuration file does not exist: " + fileName);
                    }

                    if (type.Equals(typeof(sbyte)))
                    {
                        SByteReader r = new SByteReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(sbyte), o));
                        }
                    }
                    else if (type.Equals(typeof(byte)))
                    {
                        ByteReader r = new ByteReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(byte), o));
                        }
                    }
                    else if (type.Equals(typeof(short)))
                    {
                        ShortReader r = new ShortReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(short), o));
                        }
                    }
                    else if (type.Equals(typeof(ushort)))
                    {
                        UshortReader r = new UshortReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(ushort), o));
                        }
                    }
                    else if (type.Equals(typeof(int)))
                    {
                        IntReader r = new IntReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(int), o));
                        }
                    }
                    else if (type.Equals(typeof(uint)))
                    {
                        UintReader r = new UintReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(uint), o));
                        }
                    }
                    else if (type.Equals(typeof(long)))
                    {
                        LongReader r = new LongReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(long), o));
                        }
                    }
                    else if (type.Equals(typeof(ulong)))
                    {
                        UlongReader r = new UlongReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(ulong), o));
                        }
                    }
                    else if (type.Equals(typeof(char)))
                    {
                        CharReader r = new CharReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(char), o));
                        }
                    }
                    else if (type.Equals(typeof(float)))
                    {
                        FloatReader r = new FloatReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(float), o));
                        }
                    }
                    else if (type.Equals(typeof(double)))
                    {
                        DoubleReader r = new DoubleReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(double), o));
                        }
                    }
                    else if (type.Equals(typeof(bool)))
                    {
                        BoolReader r = new BoolReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(bool), o));
                        }
                    }
                    else if (type.Equals(typeof(decimal)))
                    {
                        DecimalReader r = new DecimalReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(decimal), o));
                        }
                    }
                    else
                    {
                        if (!type.Equals(typeof(string)))
                        {
                            throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file.");
                        }
                        Common.StringReader r = new Common.StringReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(string), o));
                        }
                    }
                }
            }
        }
예제 #26
0
        internal virtual IEnumerable <Token> Tokenize(CharReader reader)
        {
            Compile();

            if (dfa.Start == null)
            {
                yield break;
            }

            PositionCounter start = new PositionCounter();
            PositionCounter end   = null;

            int[] tokenTypeIDs = null;

            DFA.State current = dfa.Start;

            while (!reader.IsEnd)
            {
                char c = reader.Read();
                current = current[c];

                // reached the end, nowhere else to go
                // return the match until the prev final state
                // and go back to the next char right after the prev final state
                if (current == null)
                {
                    yield return(CreateToken(reader, start, end, tokenTypeIDs));

                    reader.MoveBack(end.Position);
                    reader.Release();

                    start        = reader.PositionCounter;
                    end          = null;
                    tokenTypeIDs = null;

                    current = dfa.Start;
                    continue;
                }

                // remember this position in case we need to come back
                if (current.IsFinal)
                {
                    end          = reader.PositionCounter;
                    tokenTypeIDs = current.Values;
                }
            }

            if (end != null)
            {
                yield return(CreateToken(reader, start, end, tokenTypeIDs));

                if (end.Position != reader.Position)
                {
                    reader.MoveBack(end.Position - 1);
                    foreach (Token token in Tokenize(reader))
                    {
                        yield return(token);
                    }
                }
            }
            else
            {
                yield return(CreateToken(reader, start, reader.PositionCounter, tokenTypeIDs));
            }
        }