Beispiel #1
0
        public void NextChar_RetornaOToken_NaPosicao()
        {
            var ext = TokenExtractor.FromString("123");

            Assert.Equal('1', ext.NextChar());
            Assert.Equal('2', ext.NextChar());
            Assert.Equal('3', ext.NextChar());
        }
Beispiel #2
0
        public void Reconhece_Caractere_Invalido()
        {
            var element   = new CharGrammarElement('a');
            var extractor = TokenExtractor.FromString("A");
            var token     = element.Eval(extractor);

            Assert.Null(token);
        }
Beispiel #3
0
        public void Requer_String_AoInstanciar()
        {
            var ext = Assert.Throws <ArgumentNullException>(
                () => TokenExtractor.FromString(null)
                );

            Assert.Equal("code", ext.ParamName);
        }
Beispiel #4
0
        public void Reconhece_Caractere_Valido(char validChar)
        {
            var element      = new CharGrammarElement(validChar);
            var onCharString = new string(new[] { validChar });
            var extractor    = TokenExtractor.FromString(onCharString);
            var token        = element.Eval(extractor);

            Assert.NotNull(token);
            Assert.Equal(validChar, token.Content);
        }
Beispiel #5
0
        public void AvaliaCorretamente_UmaGramaticaSimples()
        {
            var grammar = new SampleGrammar1();
            var parser  = new Parser(grammar);

            var extractor1 = TokenExtractor.FromString(SampleGrammar1.SampleCode1);
            var tree1      = parser.Parse(extractor1);

            SampleGrammar1.AssertSampleCode1(tree1);
        }
Beispiel #6
0
        public void DesconsideraCaracteres_Conforme_IgnoreDelegate()
        {
            /*
             * A IDEIA BASICA SOBRE IGNORAR:
             * -----------------------------
             * > O token é que determina se vai ou não ignorar
             *   algo antes iniciar o consumo de seus caracteres.
             *
             * > Pra isso, além de receber o TokenExtractor, ele
             *   precisa receber o [ignorer]
             *
             * > Normalmente o ignorar é feito no início do processo,
             *   logo antes de consumir o primeiro [char]
             *
             *   # Imagine um "ignorar espaços vazios [espaço, tab, nova linha, etc],
             *   # Agora imagine os tokens de:
             *      - Comentário de única linha -> // comentário aqui
             *      - Comentário de várias linhas -> (* várias linhas *)
             *      - Strings -> "uma   string   qualquer"
             *   # Esses devem ignorar os caracteres iniciais, mas uma vez
             *     que estão dentro de seu escopo, devem considerar os "espaços vazios"
             *
             * > Imaginamos que o elemento [CharGrammarElement] não faz mais
             *   sentido aqui. Talvez um [WordGrammarElement] faça mais sentido.
             *   Se será uma palavra de um único caractere ele é que determina
             */

            bool ignore(char c) => new[] { ' ', '*', '-' }.Contains(c);

            var ABRoot = new AndListGrammarElement(new[] {
                new CharGrammarElement('A'),
                new CharGrammarElement('B')
            });

            var grammar = new Grammar(ABRoot, ignore);
            var parser  = new Parser(grammar);

            var code = TokenExtractor.FromString(
                "- - * A - - --- ******  B **    -----"
                );

            var tree = parser.Parse(code);

            Assert.NotNull(tree);
            Assert.NotNull(tree.RootNode);
            Assert.NotNull(tree.RootNode.Token);
            Assert.NotNull(tree.RootNode.Token.Content);
            Assert.True(tree.IsValid);
            Assert.IsType <Token[]>(tree.RootNode.Token.Content);
            Assert.Equal(2, (tree.RootNode.Token.Content as Token[]).Length);
            Assert.Equal("A", ((tree.RootNode.Token.Content as Token[])[0] as Token).Content.ToString());
            Assert.Equal("B", ((tree.RootNode.Token.Content as Token[])[1] as Token).Content.ToString());
        }
Beispiel #7
0
        public void Parse_Retorna_ArvoreSintaticaInvalida_ParaCodigoVazio()
        {
            var grammarElement = new Mock <GrammarElement>().Object;
            var grammar        = new Grammar(grammarElement);
            var extractor      = TokenExtractor.FromString("");
            var parser         = new Parser(grammar);

            var tree = parser.Parse(extractor);

            Assert.NotNull(tree);
            Assert.False(tree.IsValid);
        }
Beispiel #8
0
        public void ParaCodigoVazio_ArvoreEhInvalida_MesmoSeGramaticaNaoDisser()
        {
            var grammarElementMock = new Mock <GrammarElement>();
            var grammar            = new Grammar(grammarElementMock.Object);
            var extractor          = TokenExtractor.FromString("");
            var parser             = new Parser(grammar);

            var tree = parser.Parse(extractor);

            Assert.False(tree.IsValid);
            Assert.True(extractor.EndOfCode);
        }
Beispiel #9
0
        public void SaveCursor_Retorna_APosicaoAtualNoCodigo()
        {
            var ext = TokenExtractor.FromString("123");

            ext.NextChar();
            ext.NextChar();

            // Dois tokens à frente
            var cursor = ext.SaveCursor();

            Assert.Equal(2, cursor.Position);
        }
Beispiel #10
0
        public void RollbackCursor_RequerCursorNoRange()
        {
            var ext    = TokenExtractor.FromString("");
            var cursor = new TokenExtractorCursor
            {
                Position = 1
            };

            Assert.Throws <IndexOutOfRangeException>(
                () => ext.RollbackCursor(cursor)
                );
        }
Beispiel #11
0
        public void EndOfCode_Indica_QuandoNoFim()
        {
            var ext   = TokenExtractor.FromString("az");
            var noEOC = ext.EndOfCode;

            ext.NextChar();
            ext.NextChar();

            var yesEOC = ext.EndOfCode;

            Assert.False(noEOC);
            Assert.True(yesEOC);
        }
Beispiel #12
0
        public void Reconhece_ListaInvalida_AoAvaliar(string text)
        {
            var list = new[]
            {
                new CharGrammarElement('1'),
                new CharGrammarElement('2'),
                new CharGrammarElement('3')
            };

            var grammar   = new OrListGrammarElement(list);
            var extractor = TokenExtractor.FromString(text);
            var token     = grammar.Eval(extractor);

            Assert.Null(token);
        }
Beispiel #13
0
        public void Reconhece_ListaValida_AoAvaliar()
        {
            var charList = new[]
            {
                new CharGrammarElement('h'),
                new CharGrammarElement('h'),
                new CharGrammarElement('h')
            };

            var grammar   = new CharListGrammarElement(charList);
            var extractor = TokenExtractor.FromString("hhh");
            var token     = grammar.Eval(extractor);

            Assert.NotNull(token);
            Assert.Equal("hhh", (string)token.Content);
        }
Beispiel #14
0
        public void ArvoreSintaticaEhInvalida_SeTodoCodigo_NaoForConsumido()
        {
            var grammarElementMock = new Mock <GrammarElement>();
            var grammar            = new Grammar(grammarElementMock.Object);
            var extractor          = TokenExtractor.FromString("a");
            var parser             = new Parser(grammar);

            // Uma gramática é válida se retornar um [Token] não nulo
            grammarElementMock
            .Setup(el => el.Eval(It.IsAny <TokenExtractor>()))
            .Returns(new Token(null));

            var tree = parser.Parse(extractor);

            Assert.False(tree.IsValid);
            Assert.False(extractor.EndOfCode);
        }
Beispiel #15
0
        public void Reconhece_ListaValida_AoAvaliar(string text)
        {
            var list = new[]
            {
                new CharGrammarElement('a'),
                new CharGrammarElement('b'),
                new CharGrammarElement('c')
            };

            var grammar   = new OrListGrammarElement(list);
            var extractor = TokenExtractor.FromString(text);
            var token     = grammar.Eval(extractor);

            Assert.NotNull(token);
            Assert.IsType <char>(token.Content);
            Assert.Contains((char)token.Content, new char[] { 'a', 'b', 'c' });
        }
Beispiel #16
0
        public void Consome_SoOsCaracteres_Necessarios()
        {
            var charList = new[]
            {
                new CharGrammarElement('h'),
                new CharGrammarElement('h'),
                new CharGrammarElement('h')
            };

            var grammar   = new CharListGrammarElement(charList);
            var extractor = TokenExtractor.FromString("hhhhhh");
            var posBegin  = extractor.SaveCursor();
            var token     = grammar.Eval(extractor);

            Assert.NotNull(token);
            Assert.Equal("hhh", (string)token.Content);
            Assert.Equal(0, posBegin.Position);
            Assert.Equal(3, extractor.SaveCursor().Position);
        }
Beispiel #17
0
        public void Reconhece_ListaValida_AoAvaliar()
        {
            var list = new[]
            {
                new CharGrammarElement('a'),
                new CharGrammarElement('b'),
                new CharGrammarElement('c')
            };

            var grammar   = new AndListGrammarElement(list);
            var extractor = TokenExtractor.FromString("abc");
            var token     = grammar.Eval(extractor);

            Assert.NotNull(token);
            Assert.IsType <Token[]>(token.Content);
            Assert.Equal(3, (token.Content as Token[]).Length);
            Assert.Equal("a", (token.Content as Token[])[0].Content.ToString());
            Assert.Equal("b", (token.Content as Token[])[1].Content.ToString());
            Assert.Equal("c", (token.Content as Token[])[2].Content.ToString());
        }
Beispiel #18
0
        public void Reconhece_TodosTokens_Validos()
        {
            var blanks = new OrListGrammarElement(new[] {
                new CharGrammarElement(' '),
                new CharGrammarElement('\t'),
            });

            var grammar   = new WhileGrammarElement(blanks);
            var extractor = TokenExtractor.FromString("  \t  \t  \t  final!");

            var token = grammar.Eval(extractor);

            Assert.NotNull(token);
            Assert.IsType <Token[]>(token.Content);
            Assert.Equal(11, (token.Content as Token[]).Length);
            Assert.Equal("  \t  \t  \t  ", (token.Content as Token[]).Aggregate("", (current, tk) =>
            {
                return(current + tk.Content.ToString());
            }));
        }
Beispiel #19
0
        public void RollbackCursor_MovePara_QuandoCursorValido()
        {
            var ext = TokenExtractor.FromString("abc");

            // Inicia em 0
            var cursor1 = ext.SaveCursor();

            ext.NextChar();

            // 1 token à frente
            var cursor2 = ext.SaveCursor();

            ext.RollbackCursor(cursor1);

            // Volta ao início
            var cursor3 = ext.SaveCursor();

            Assert.Equal(0, cursor1.Position);
            Assert.Equal(1, cursor2.Position);
            Assert.Equal(0, cursor3.Position);
        }
Beispiel #20
0
        public void Consome_SoOsCaracteres_Necessarios()
        {
            var list = new[]
            {
                new CharGrammarElement('e'),
                new CharGrammarElement('f'),
                new CharGrammarElement('g')
            };

            var grammar   = new AndListGrammarElement(list);
            var extractor = TokenExtractor.FromString("efghhh");
            var posBegin  = extractor.SaveCursor();
            var token     = grammar.Eval(extractor);

            Assert.NotNull(token);
            Assert.Equal(0, posBegin.Position);
            Assert.Equal(3, extractor.SaveCursor().Position);

            Assert.IsType <Token[]>(token.Content);
            Assert.Equal(3, (token.Content as Token[]).Length);
            Assert.Equal("e", (token.Content as Token[])[0].Content.ToString());
            Assert.Equal("f", (token.Content as Token[])[1].Content.ToString());
            Assert.Equal("g", (token.Content as Token[])[2].Content.ToString());
        }
Beispiel #21
0
        public void Reconhece_QualquerDosTokensNaLista()
        {
            var list = new[]
            {
                new CharGrammarElement('a'),
                new CharGrammarElement('b'),
                new CharGrammarElement('c')
            };

            var grammar = new OrListGrammarElement(list);

            var extractorOk = TokenExtractor.FromString("cba");
            var tk1Ok       = grammar.Eval(extractorOk);
            var tk2Ok       = grammar.Eval(extractorOk);
            var tk3Ok       = grammar.Eval(extractorOk);

            var extractorErr = TokenExtractor.FromString("acx");
            var tk1Err       = grammar.Eval(extractorErr);
            var tk2Err       = grammar.Eval(extractorErr);
            var tk3Err       = grammar.Eval(extractorErr);

            // Ok
            Assert.NotNull(tk1Ok);
            Assert.NotNull(tk2Ok);
            Assert.NotNull(tk3Ok);
            Assert.Equal('c', (char)tk1Ok.Content);
            Assert.Equal('b', (char)tk2Ok.Content);
            Assert.Equal('a', (char)tk3Ok.Content);

            // Erro
            Assert.NotNull(tk1Err);
            Assert.NotNull(tk2Err);
            Assert.Null(tk3Err);
            Assert.Equal('a', (char)tk1Err.Content);
            Assert.Equal('c', (char)tk2Err.Content);
        }
Beispiel #22
0
        public void ConteudoVazio_JaInicia_NoFim()
        {
            var ext = TokenExtractor.FromString("");

            Assert.True(ext.EndOfCode);
        }
Beispiel #23
0
        public void ConteudoNaoVazio_NaoInicia_NoFim()
        {
            var ext = TokenExtractor.FromString(".");

            Assert.False(ext.EndOfCode);
        }