예제 #1
0
        public IParseTree <TToken, TRule> Parse(IList <Token <TToken> > tokens, ITextIndexHelper translator)
        {
            if (!this.configCompleted)
            {
                throw ParserConfigurationException.NotCompleted();
            }

            var parseTree = this.rulesByType[this.rootType].TryParse(tokens, 0, this.rulesByType);

            if (parseTree.IsError || parseTree.NextTokenIndex != tokens.Count)
            {
                if (parseTree.MaxMatchedNextTokenIndex == tokens.Count)
                {
                    throw new EndOfTokensException();
                }

                if (parseTree.MaxMatchedNextTokenIndex < 0 || parseTree.MaxMatchedNextTokenIndex > tokens.Count)
                {
                    throw new ApplicationException("An unexpected error was encountered.");
                }

                throw UnexpectedTokenException.For(tokens[parseTree.MaxMatchedNextTokenIndex], translator);
            }

            return(parseTree.TreeItems);
        }
        public void TestConstructor()
        {
            var e = new UnexpectedTokenException(1, 10, TokenType.EOF, TokenType.Comma, TokenType.RightBrace);

            Assert.AreEqual(1, e.LineNo);
            Assert.AreEqual(10, e.ColNo);
            Assert.AreEqual("Line 1, Col 10. Unexpected token: EOF. Expected: Comma, RightBrace", e.Message);
        }
예제 #3
0
        public void MessagePropertyShouldHaveHelpfulMessage(int index, string token, string exceptedMessage)
        {
            var exception = new UnexpectedTokenException(index, token);

            exception.Index.Should().Be(index);
            exception.Token.Should().Be(token);
            exception.Message.Should().Be(exceptedMessage);
        }
예제 #4
0
        public void ShouldThrowError_WhenTheSecondCharIsNot_Letter()
        {
            ParseStream parseStream = new ParseStream("/1ab");
            TagParser   parser      = new TagParser(parseStream);

            UnexpectedTokenException ex = Assert.Throws <UnexpectedTokenException>(() => parser.Parse());

            Assert.AreEqual(1, ex.TokenIndex.Index);
            Assert.AreEqual('1', ex.Character);
        }
예제 #5
0
        private UnexpectedTokenException GetFirstError()
        {
            stopAtFirstError.Should().BeTrue();
            parsingError.Errors.Should().HaveCount(1);
            var error = parsingError.Errors.First();

            if (error is UnexpectedEOFException)
            {
                var eofEx = (UnexpectedEOFException)error;
                error = new UnexpectedTokenException(new Token(BerpGrammar.TokenType.EOF, 0, 0), eofEx.ExpectedTokenTypes, eofEx.StateComment);
            }
            return(error as UnexpectedTokenException);
        }
        public void BuildTree_IllegalTokenInFormal_ThrowsException()
        {
            List <Token> tokens = new List <Token>
            {
                new Token(TokenType.Addition)
            };

            UnexpectedTokenException exception = Assert.Throws <UnexpectedTokenException>(
                () => builder.BuildTree(new Tokens(tokens))
                );

            Assert.AreEqual("Expected one of these 'LeftParentheses, Identifier, Number' tokens, but got 'Addition' instead.", exception.Message);
        }
        public void UnexpectedTokenExceptionShouldBeSerializableButOnlyMessageAndLocation()
        {
            var token     = new Token(null, new Location(1, 2));
            var exception = new UnexpectedTokenException(token, new [] { "#T1", "#T2 " }, "state-comment");

            var deserializedException = SerializeDeserialize(exception);

            AssertMessageAndLocation(exception, deserializedException);

            // the custom details are not serialized (yet?)
            Assert.IsNull(deserializedException.ReceivedToken);
            Assert.IsNull(deserializedException.ExpectedTokenTypes);
            Assert.IsNull(deserializedException.StateComment);
        }
        public void BuildTree_IllegallyParenthesizedExpression_ThrowsException()
        {
            List <Token> tokens = new List <Token>
            {
                new Token(TokenType.LeftParentheses),
                new Token(TokenType.Number, "1"),
                new Token(TokenType.Number, "1")
            };

            UnexpectedTokenException exception = Assert.Throws <UnexpectedTokenException>(
                () => builder.BuildTree(new Tokens(tokens))
                );

            Assert.AreEqual("Expected 'RightParentheses' token, but got 'Number' instead.", exception.Message);
        }
        public void BuildTree_UnexpectedTokenAtRightParen_ThrowsException()
        {
            List <Token> tokens = new List <Token>
            {
                new Token(TokenType.Identifier, "sin"),
                new Token(TokenType.LeftParentheses),
                new Token(TokenType.Number, "1"),
                new Token(TokenType.Number, "1"),
            };

            UnexpectedTokenException exception = Assert.Throws <UnexpectedTokenException>(
                () => builder.BuildTree(new Tokens(tokens))
                );

            Assert.AreEqual($"Expected 'RightParentheses' token, but got 'Number' instead.", exception.Message);
        }
예제 #10
0
파일: Steps.cs 프로젝트: chebizarro/berp
 private UnexpectedTokenException GetFirstError()
 {
     stopAtFirstError.Should().BeTrue();
     parsingError.Errors.Should().HaveCount(1);
     var error = parsingError.Errors.First();
     if (error is UnexpectedEOFException)
     {
         var eofEx = (UnexpectedEOFException) error;
         error = new UnexpectedTokenException(new Token(BerpGrammar.TokenType.EOF, 0, 0), eofEx.ExpectedTokenTypes, eofEx.StateComment);
     }
     return error as UnexpectedTokenException;
 }
예제 #11
0
        private AcfFile(string input, bool forgive)
        {
            HasError = false;
            Root     = new CompoundNode(null, null);

            const string ExpectedTokensNameK = "key string or \"}\"";

            var status = ParserStatus.ExpectingKeyOrRPar;
            var reader = new StringCodeReader(input);
            //if (reader.Peek() == 65279) reader.Read();
            var             stack      = new Stack <CompoundNode>(64);
            var             isKeyNaked = false;
            string          lastKey    = null;
            var             parent     = Root;
            Token           token;
            Node            node;
            LeafNode        lnode, comnode;
            CompoundNode    cnode;
            ParserException e;

            do
            {
                token = Lexer.NextToken(reader, forgive);
                if (token.Type == TokenType.Failure)
                {
                    HasError = true;
                    return;
                }
                switch (status)
                {
                case ParserStatus.ExpectingKeyOrRPar:
                    switch (token.Type)
                    {
                    case TokenType.StringType:
                    case TokenType.NakedString:
                        lastKey    = token.Str;
                        isKeyNaked = token.Type == TokenType.NakedString;
                        status     = ParserStatus.ExpectingLParOrVal;
                        break;

                    case TokenType.Comment:
                        comnode = new LeafNode(null, token.Str, parent)
                        {
                            IsComment = true
                        };
                        parent.Add(comnode);
                        continue;

                    case TokenType.Eof:
                        if (stack.Count == 0)
                        {
                            return;
                        }
                        e = new UnexpectedTokenException(
                            reader.LineNo, reader.Col, ExpectedTokensNameK, token.ToString());
                        PardonOrDeath(e, forgive);
                        return;

                    case TokenType.LstEnd:
                        if (stack.Count == 0)
                        {
                            e = new UnexpectedTokenException(
                                reader.LineNo, reader.Col, ExpectedTokensNameK, token.ToString());
                            PardonOrDeath(e, forgive);
                            return;
                        }
                        parent = stack.Pop();
                        break;

                    default:
                        e = new UnexpectedTokenException(
                            reader.LineNo, reader.Col, ExpectedTokensNameK, token.ToString());
                        PardonOrDeath(e, forgive);
                        return;
                    }
                    break;


                case ParserStatus.ExpectingLParOrVal:
                    switch (token.Type)
                    {
                    case TokenType.StringType:
                    case TokenType.NakedString:
                        lnode              = new LeafNode(lastKey, token.Str, parent);
                        node               = lnode;
                        lnode.IsKeyNaked   = isKeyNaked;
                        lnode.IsValueNaked = token.Type == TokenType.NakedString;
                        parent.Add(node);
                        break;

                    case TokenType.Comment:
                        comnode = new LeafNode(null, token.Str, parent)
                        {
                            IsComment = true
                        };
                        parent.Add(comnode);
                        continue;

                    case TokenType.LstBegin:
                        cnode = new CompoundNode(lastKey, parent)
                        {
                            IsKeyNaked = isKeyNaked
                        };
                        node = cnode;
                        parent.Add(node);
                        stack.Push(parent);
                        parent = cnode;
                        break;

                    default:
                        e = new UnexpectedTokenException(
                            reader.LineNo, reader.Col,
                            "\"{\" or value string", token.ToString());
                        PardonOrDeath(e, forgive);
                        return;
                    }
                    status = ParserStatus.ExpectingKeyOrRPar;
                    break;
                }
            } while (true);
        }