Exemplo n.º 1
0
        /// <summary>
        /// Executes the test as a parsing test with a failing condition
        /// </summary>
        /// <param name="parser">The parser to use</param>
        /// <returns>The test result</returns>
        private static int TestFails(Hime.Redist.Parsers.BaseLRParser parser)
        {
            ParseResult result = parser.Parse();

            if (!result.IsSuccess)
            {
                return(RESULT_SUCCESS);
            }
            if (result.Errors.Count != 0)
            {
                return(RESULT_SUCCESS);
            }
            Console.WriteLine("No error found while parsing, while some were expected");
            return(RESULT_FAILURE_VERB);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the serialized expected AST
        /// </summary>
        /// <returns>The expected AST, or null if an error occurred</returns>
        private static ASTNode?GetExpectedAST()
        {
            string expectedText = File.ReadAllText("expected.txt", Encoding.UTF8);

            Hime.Redist.Parsers.BaseLRParser expectedParser = GetParser("Hime.Tests.Generated.ExpectedTreeParser", expectedText);
            ParseResult result = expectedParser.Parse();

            foreach (ParseError error in result.Errors)
            {
                Console.WriteLine(error);
                TextContext context = result.Input.GetContext(error.Position);
                Console.WriteLine(context.Content);
                Console.WriteLine(context.Pointer);
            }
            return(result.Errors.Count > 0 ? new ASTNode?() : (result.Root));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Executes the specified test
        /// </summary>
        /// <param name="parserName">The parser's name</param>
        /// <param name="verb">A verb specifying the type of test</param>
        /// <returns>The test result</returns>
        public int Execute(string parserName, string verb)
        {
            byte[] buffer = File.ReadAllBytes("input.txt");
            string input  = new string(Encoding.UTF8.GetChars(buffer));

            Hime.Redist.Parsers.BaseLRParser parser = GetParser(parserName, input);
            switch (verb)
            {
            case VERB_MATCHES:
                return(TestMatches(parser));

            case VERB_NOMATCHES:
                return(TestNoMatches(parser));

            case VERB_FAILS:
                return(TestFails(parser));

            case VERB_OUTPUTS:
                return(TestOutputs(parser));
            }
            return(RESULT_FAILURE_VERB);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Executes the test as a parsing test with a non-matching condition
        /// </summary>
        /// <param name="parser">The parser to use</param>
        /// <returns>The test result</returns>
        private int TestNoMatches(Hime.Redist.Parsers.BaseLRParser parser)
        {
            ASTNode?expected = GetExpectedAST();

            if (!expected.HasValue)
            {
                Console.WriteLine("Failed to parse the expected AST");
                return(RESULT_FAILURE_PARSING);
            }
            ParseResult result = parser.Parse();

            foreach (ParseError error in result.Errors)
            {
                Console.WriteLine(error);
                TextContext context = result.Input.GetContext(error.Position);
                Console.WriteLine(context.Content);
                Console.WriteLine(context.Pointer);
            }
            if (!result.IsSuccess)
            {
                Console.WriteLine("Failed to parse the input");
                return(RESULT_FAILURE_PARSING);
            }
            if (result.Errors.Count != 0)
            {
                Console.WriteLine("Some errors while parsing the input");
                return(RESULT_FAILURE_PARSING);
            }
            if (Compare(expected.Value, result.Root))
            {
                Console.WriteLine("Produced AST incorrectly matches the specified expectation");
                return(RESULT_FAILURE_VERB);
            }
            else
            {
                return(RESULT_SUCCESS);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Executes the test as an output test
        /// </summary>
        /// <param name="parser">The parser to use</param>
        /// <returns>The test result</returns>
        private static int TestOutputs(Hime.Redist.Parsers.BaseLRParser parser)
        {
            string[]    output = GetExpectedOutput();
            ParseResult result = parser.Parse();

            if (output.Length == 0 || (output.Length == 1 && output[0].Length == 0))
            {
                if (result.IsSuccess && result.Errors.Count == 0)
                {
                    return(RESULT_SUCCESS);
                }
                foreach (ParseError error in result.Errors)
                {
                    Console.WriteLine(error);
                    TextContext context = result.Input.GetContext(error.Position);
                    Console.WriteLine(context.Content);
                    Console.WriteLine(context.Pointer);
                }
                Console.WriteLine("Expected an empty output but some error where found while parsing");
                return(RESULT_FAILURE_VERB);
            }
            int i = 0;

            foreach (ParseError error in result.Errors)
            {
                string      message = error.ToString();
                TextContext context = result.Input.GetContext(error.Position);
                if (i + 2 >= output.Length)
                {
                    Console.WriteLine("Unexpected error:");
                    Console.WriteLine(message);
                    Console.WriteLine(context.Content);
                    Console.WriteLine(context.Pointer);
                    return(RESULT_FAILURE_VERB);
                }
                if (!message.StartsWith(output[i]))
                {
                    Console.WriteLine("Unexpected output: " + message);
                    Console.WriteLine("Expected prefix  : " + output[i]);
                    return(RESULT_FAILURE_VERB);
                }
                if (!context.Content.StartsWith(output[i + 1]))
                {
                    Console.WriteLine("Unexpected output: " + context.Content);
                    Console.WriteLine("Expected prefix  : " + output[i + 1]);
                    return(RESULT_FAILURE_VERB);
                }
                if (!context.Pointer.StartsWith(output[i + 2]))
                {
                    Console.WriteLine("Unexpected output: " + context.Pointer);
                    Console.WriteLine("Expected prefix  : " + output[i + 2]);
                    return(RESULT_FAILURE_VERB);
                }
                i += 3;
            }
            if (i == output.Length)
            {
                return(RESULT_SUCCESS);
            }
            for (int j = i; j != output.Length; j++)
            {
                Console.WriteLine("Missing output: " + output[j]);
            }
            return(RESULT_FAILURE_VERB);
        }