예제 #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);
        }
예제 #2
0
        /// <summary>
        /// Constrói o leitor de símbolos a partir de um leitor de texto.
        /// </summary>
        /// <param name="input">O leitor de texto.</param>
        /// <returns>O leitor de símbolos.</returns>
        public SymbolReader <TextReader, string, string> BuildReader(TextReader input)
        {
            CharSymbolReader <string> result = new CharSymbolReader <string>(input, "any", "eof");

            result.RegisterCharType('_', "underscore");
            result.RegisterCharType('(', "left_parenthesis");
            result.RegisterCharType(')', "right_parenthesis");
            result.RegisterCharType('[', "left_bracket");
            result.RegisterCharType(']', "right_bracket");
            result.RegisterCharType('"', "double_quote");
            result.RegisterCharType('\'', "quote");
            result.RegisterCharType('+', "plus");
            result.RegisterCharType('-', "minus");
            result.RegisterCharType('*', "times");
            result.RegisterCharType('/', "right_bar");
            result.RegisterCharType('\\', "left_bar");
            result.RegisterCharType('.', "point");
            result.RegisterCharType(':', "colon");
            result.RegisterCharType(';', "semi_colon");
            result.RegisterCharType(',', "comma");
            result.RegisterCharType('~', "tild");
            result.RegisterCharType('^', "hat");
            result.RegisterCharType('?', "question_mark");
            result.RegisterCharType('!', "exclamation_mark");
            result.RegisterCharType('<', "less_than");
            result.RegisterCharType('>', "great_than");
            result.RegisterCharType('|', "bitwise_or");
            result.RegisterCharType('&', "bitwise_and");
            result.RegisterCharType(' ', "space");
            result.RegisterCharType('\n', "new_line");
            result.RegisterCharType('\t', "tab");
            result.RegisterCharType('\r', "carriage_return");
            result.RegisterCharType('=', "equal");
            result.RegisterCharType('{', "left_brace");
            result.RegisterCharType('}', "right_brace");
            result.RegisterCharType('@', "at");
            result.RegisterCharType('#', "cardinal");
            result.RegisterCharType('$', "dollar");
            result.RegisterCharType('%', "mod");
            result.RegisterCharType('£', "pound");
            result.RegisterCharType('§', "chapter");
            result.RegisterCharType('€', "euro");
            result.DeciderFunction = this.DeciderFunction;
            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());
        }