Beispiel #1
0
        /// <summary>
        /// Obtém um leitor de símbolos a partir do texto que representa o padrão.
        /// </summary>
        /// <param name="pattern">O padrão.</param>
        /// <returns>O leitor de símbolos.</returns>
        private SimpleTextSymbolReader <ELambdaExpressionWordType> GetPatternReader(
            string pattern)
        {
            var innerPattern     = pattern == null ? string.Empty : pattern;
            var reader           = new StringReader(pattern);
            var charSymbolReader = new CharSymbolReader <ELambdaExpressionWordType>(
                reader,
                ELambdaExpressionWordType.OTHER,
                ELambdaExpressionWordType.EOF);

            charSymbolReader.RegisterCharRangeType('a', 'z', ELambdaExpressionWordType.ALPHA);
            charSymbolReader.RegisterCharRangeType('A', 'Z', ELambdaExpressionWordType.ALPHA);
            charSymbolReader.RegisterCharRangeType('0', '9', ELambdaExpressionWordType.NUMERIC);
            charSymbolReader.RegisterCharType('(', ELambdaExpressionWordType.OPEN_PARENTHESIS);
            charSymbolReader.RegisterCharType('"', ELambdaExpressionWordType.DELIMITER);
            charSymbolReader.RegisterCharType(')', ELambdaExpressionWordType.CLOSE_PARENTHESIS);
            charSymbolReader.RegisterCharType(',', ELambdaExpressionWordType.COMMA);
            charSymbolReader.RegisterCharType(' ', ELambdaExpressionWordType.SPACE);
            charSymbolReader.RegisterCharType('\r', ELambdaExpressionWordType.SPACE);
            charSymbolReader.RegisterCharType('\n', ELambdaExpressionWordType.SPACE);
            var result = new SimpleTextSymbolReader <ELambdaExpressionWordType>(
                charSymbolReader,
                ELambdaExpressionWordType.EOF);

            result.SetGroupCount(ELambdaExpressionWordType.DELIMITER, 1);
            result.SetGroupCount(ELambdaExpressionWordType.OPEN_PARENTHESIS, 1);
            result.SetGroupCount(ELambdaExpressionWordType.CLOSE_PARENTHESIS, 1);
            result.SetGroupCount(ELambdaExpressionWordType.COMMA, 1);

            return(result);
        }
        public void Get_Test()
        {
            var        testString        = "Amanhã. Tempo...\n\n\nNão! Porquê?";
            TextReader reader            = new StringReader(testString);
            var        endOfFileSymbType = "eof";
            var        charSymbolReader  = new CharSymbolReader <string>(reader, "desconhecido", "final");

            charSymbolReader.RegisterCharRangeType('a', 'z', "palavra");
            charSymbolReader.RegisterCharRangeType('A', 'Z', "palavra");
            charSymbolReader.RegisterCharType('ã', "palavra");
            charSymbolReader.RegisterCharType('ê', "palavra");
            charSymbolReader.RegisterCharType('.', "pontuação");
            charSymbolReader.RegisterCharType('!', "pontuação");
            charSymbolReader.RegisterCharType('?', "pontuação");
            charSymbolReader.RegisterCharType('\n', "brancos");
            charSymbolReader.RegisterCharType(' ', "brancos");

            var target = new SimpleTextSymbolReader <string>(
                charSymbolReader,
                endOfFileSymbType,
                StringComparer.InvariantCultureIgnoreCase);

            // Todos os símbolos que se espera encontrar na expressão de acordo com a configuração.
            var expectedSymbols = new GeneralSymbol <string, string>[] {
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = "Amanhã", SymbolType = "palavra"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = ".", SymbolType = "pontuação"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = " ", SymbolType = "brancos"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = "Tempo", SymbolType = "palavra"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = "...", SymbolType = "pontuação"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = "\n\n\n", SymbolType = "brancos"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = "Não", SymbolType = "palavra"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = "!", SymbolType = "pontuação"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = " ", SymbolType = "brancos"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = "Porquê", SymbolType = "palavra"
                },
                new GeneralSymbol <string, string>()
                {
                    SymbolValue = "?", SymbolType = "pontuação"
                }
            };

            for (int i = 0; i < expectedSymbols.Length; ++i)
            {
                var expected = expectedSymbols[i];
                var actual   = target.Get();
                Assert.AreEqual(expected.SymbolValue, actual.SymbolValue);
                Assert.AreEqual(expected.SymbolType, actual.SymbolType);
            }

            // Verifica se não existem mais símbolos a serem lidos.
            Assert.IsTrue(target.IsAtEOF());
        }