Esempio n. 1
0
        public void ValidLiteralIntDecimal()
        {
            var inputString = new InputString("7894561230");
            var literalIntTokenGenerator = new LiteralIntTokenGenerator();
            var currentSymbol            = inputString.GetNextSymbol();

            do
            {
                Assert.True(literalIntTokenGenerator.validStart(currentSymbol));
                currentSymbol = inputString.GetNextSymbol();
            }while(currentSymbol.character != '\0');

            inputString = new InputString("123 12 8 0");
            var expectedLexemes = new string[] { "123", "12", "8", "0" };
            var lexer           = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken    = lexer.GetNextToken();
            int i = 0;

            do
            {
                Assert.True(currentToken.type == TokenType.LIT_INT);
                Assert.True(currentToken.lexeme == expectedLexemes[i++]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + TokenType.LIT_INT);
                currentToken = lexer.GetNextToken();
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 2
0
        public void ValidIDReservedWord()
        {
            var inputString      = new InputString("abc");
            var idTokenGenerator = new IDReservedWordTokenGenerator();
            var currentSymbol    = inputString.GetNextSymbol();

            do
            {
                Assert.True(idTokenGenerator.validStart(currentSymbol));
                currentSymbol = inputString.GetNextSymbol();
            }while(currentSymbol.character != '\0');

            inputString = new InputString("hola Hola abd142_ hola_ adios_1542 true false");
            var expectedTypes   = new TokenType[] { TokenType.ID, TokenType.ID, TokenType.ID, TokenType.ID, TokenType.ID, TokenType.LIT_BOOL, TokenType.LIT_BOOL };
            var expectedLexemes = new string[] { "hola", "Hola", "abd142_", "hola_", "adios_1542", "true", "false" };
            var lexer           = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken    = lexer.GetNextToken();
            int i = 0;

            do
            {
                Assert.True(currentToken.type == expectedTypes[i]);
                Assert.True(currentToken.lexeme == expectedLexemes[i]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + expectedTypes[i]);
                currentToken = lexer.GetNextToken();
                i++;
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 3
0
        public void ValidPuntuation()
        {
            var inputString     = new InputString(@"
:						// ':'
,						// ','
;						// ';'
(						// '('
)						// ')'
{						// '{'
}                       // '}'
[                       // '['
]                       // ']'"        );
            var expectedLexemes = new string[] {
                ":"
                , ","
                , ";"
                , "("
                , ")"
                , "{"
                , "}"
                , "["
                , "]"
            };
            var expectedTypes = new TokenType[] {
                TokenType.PUNT_COLON
                , TokenType.PUNT_COMMA
                , TokenType.PUNT_END_STATEMENT_SEMICOLON
                , TokenType.PUNT_PAREN_OPEN
                , TokenType.PUNT_PAREN_CLOSE
                , TokenType.PUNT_CURLY_BRACKET_OPEN
                , TokenType.PUNT_CURLY_BRACKET_CLOSE
                , TokenType.PUNT_SQUARE_BRACKET_OPEN
                , TokenType.PUNT_SQUARE_BRACKET_CLOSE
            };

            var lexer        = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken = lexer.GetNextToken();
            int i            = 0;

            do
            {
                Assert.True(currentToken.type == expectedTypes[i]);
                Assert.True(currentToken.lexeme == expectedLexemes[i]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + expectedTypes[i]);
                currentToken = lexer.GetNextToken();
                i++;
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 4
0
        void validComments()
        {
            var inputString = new InputString(@"
            /*fds f sdf sd f sd f sdf int float */");
            // var expectedLexemes = new string[]{"'a'", "'b'","'\\a'","'\\b'"};
            var lexer        = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken = lexer.GetNextToken();

            do
            {
                // Assert.True(currentToken.type == TokenType.LIT_CHAR);
                // Assert.True(currentToken.lexeme == expectedLexemes[i++]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + TokenType.LIT_CHAR);
                currentToken = lexer.GetNextToken();
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 5
0
        public void ValidLiteralChar()
        {
            var inputString     = new InputString("'a' 'b' '\\a' '\\b'");
            var expectedLexemes = new string[] { "'a'", "'b'", "'\\a'", "'\\b'" };
            var lexer           = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken    = lexer.GetNextToken();
            int i = 0;

            do
            {
                Assert.True(currentToken.type == TokenType.LIT_CHAR);
                Assert.True(currentToken.lexeme == expectedLexemes[i++]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + TokenType.LIT_CHAR);
                currentToken = lexer.GetNextToken();
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 6
0
        public void ValidLiteralString()
        {
            var inputString     = new InputString("\"hola 15245\" \"adios dsd sa \\t \\v sta\"");
            var expectedLexemes = new string[] { "\"hola 15245\"", "\"adios dsd sa \\t \\v sta\"" };
            var lexer           = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken    = lexer.GetNextToken();
            int i = 0;

            do
            {
                Assert.True(currentToken.type == TokenType.LIT_STRING);
                Assert.True(currentToken.lexeme == expectedLexemes[i++]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + TokenType.LIT_STRING);
                currentToken = lexer.GetNextToken();
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 7
0
        public void ValidLiteralFloat()
        {
            var inputString     = new InputString("123f 12F 8.58f 0.05F");
            var expectedLexemes = new string[] { "123f", "12F", "8.58f", "0.05F" };
            var lexer           = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken    = lexer.GetNextToken();
            int i = 0;

            do
            {
                Assert.True(currentToken.type == TokenType.LIT_FLOAT);
                Assert.True(currentToken.lexeme == expectedLexemes[i++]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + TokenType.LIT_FLOAT);
                currentToken = lexer.GetNextToken();
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 8
0
        public void ValidLiteralIntBinary()
        {
            var inputString     = new InputString("0b0100 0B01110");
            var expectedLexemes = new string[] { "0b0100", "0B01110" };
            var lexer           = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken    = lexer.GetNextToken();
            int i = 0;

            do
            {
                Assert.True(currentToken.type == TokenType.LIT_INT);
                Assert.True(currentToken.lexeme == expectedLexemes[i++]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + TokenType.LIT_INT);
                currentToken = lexer.GetNextToken();
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 9
0
 public Parser(Lexer lexer)
 {
     debug      = false;
     this.lexer = lexer;
     token      = lexer.GetNextToken();
     look_ahead = new List <Token>();
 }
Esempio n. 10
0
        void ValirReadAllTokensFromFile()
        {
            var inputString     = new InputFile(@"C:\Users\Kenystev\Documents\Compiladores\CStoJS_Compiler\lexer_test.txt");
            var tokenGenerators = Resources.getTokenGenerators();

            var lexer = new Compiler.Lexer(inputString, tokenGenerators);

            Token token = lexer.GetNextToken();

            while (token.type != TokenType.EOF)
            {
                System.Console.Out.WriteLine(token);
                token = lexer.GetNextToken();
            }

            System.Console.Out.WriteLine(token);
            Assert.True(token.type == TokenType.EOF);
        }
Esempio n. 11
0
        public void ValidLiteralVerbatimString()
        {
            var inputString     = new InputString(@"@""Hola como """"estan todos""""   
            nada \mas\ que 'hacer',....."" @""otra ves{](0)""");
            var expectedLexemes = new string[] { @"@""Hola como """"estan todos""""   
            nada \mas\ que 'hacer',.....""", @"@""otra ves{](0)""" };

            var lexer        = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken = lexer.GetNextToken();
            int i            = 0;

            do
            {
                Assert.True(currentToken.type == TokenType.LIT_STRING);
                Assert.True(currentToken.lexeme == expectedLexemes[i++]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + TokenType.LIT_STRING);
                currentToken = lexer.GetNextToken();
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 12
0
 /// <summary>
 /// Match() Matches the current token to which is expected in the grammar
 /// </summary>
 /// <param name="token"></param>
 private void Match(Globals.Symbol token)
 {
     if (token == Globals.Token)
     {
         l.GetNextToken();
     }
     else
     {
         Console.WriteLine($"Error: Line {Globals.LineNumber+1}: Expecting {token.ToString()}. Received {Globals.Token.ToString()}");
         Environment.Exit(-1);
     }
 }
Esempio n. 13
0
 /// <summary>
 /// Default Constructor to create a Parser Object
 /// </summary>
 /// <param name="fileName"></param>
 public Parser(string fileName)
 {
     l = new Lexer(fileName);
     l.GetNextToken();
 }
Esempio n. 14
0
        public void ValidOperators()
        {
            var inputString     = new InputString(@"= += -=	*= /= %= <<= >>= &= ^= |= & | ^ ~ << >>
            && || ! sizeof ? ?? is as == != > < >= <= + - * / % ++ --");
            var expectedLexemes = new string[] {
                "="
                , "+="
                , "-="
                , "*="
                , "/="
                , "%="
                , "<<="
                , ">>="
                , "&="
                , "^="
                , "|="
                , "&"
                , "|"
                , "^"
                , "~"
                , "<<"
                , ">>"
                , "&&"
                , "||"
                , "!"
                , "sizeof"
                , "?"
                , "??"
                , "is"
                , "as"
                , "=="
                , "!="
                , ">"
                , "<"
                , ">="
                , "<="
                , "+"
                , "-"
                , "*"
                , "/"
                , "%"
                , "++"
                , "--"
            };
            var expectedTypes = new TokenType[] {
                TokenType.OP_ASSIGN
                , TokenType.OP_ASSIGN_SUM
                , TokenType.OP_ASSIGN_SUBSTRACT
                , TokenType.OP_ASSIGN_MULTIPLICATION
                , TokenType.OP_ASSIGN_DIVISION
                , TokenType.OP_ASSIGN_MODULO
                , TokenType.OP_ASSIGN_SHIFT_LEFT
                , TokenType.OP_ASSIGN_SHIFT_RIGHT
                , TokenType.OP_ASSIGN_BITWISE_AND
                , TokenType.OP_ASSIGN_XOR
                , TokenType.OP_ASSIGN_BITWISE_OR
                , TokenType.OP_BITWISE_AND
                , TokenType.OP_BITWISE_OR
                , TokenType.OP_XOR
                , TokenType.OP_BITWISE_NOT
                , TokenType.OP_SHIFT_LEFT
                , TokenType.OP_SHIFT_RIGHT
                , TokenType.OP_AND
                , TokenType.OP_OR
                , TokenType.OP_NOT
                , TokenType.OP_SIZEOF
                , TokenType.OP_TERNARY
                , TokenType.OP_NULL_COALESCING
                , TokenType.OP_IS
                , TokenType.OP_AS
                , TokenType.OP_EQUAL
                , TokenType.OP_DISTINCT
                , TokenType.OP_MORE_THAN
                , TokenType.OP_LESS_THAN
                , TokenType.OP_MORE_AND_EQUAL_THAN
                , TokenType.OP_LESS_AND_EQUAL_THAN
                , TokenType.OP_SUM
                , TokenType.OP_SUBSTRACT
                , TokenType.OP_MULTIPLICATION
                , TokenType.OP_DIVISION
                , TokenType.OP_MODULO
                , TokenType.OP_PLUS_PLUS
                , TokenType.OP_MINUS_MINUS
            };

            var lexer        = new Compiler.Lexer(inputString, Resources.getTokenGenerators());
            var currentToken = lexer.GetNextToken();
            int i            = 0;

            do
            {
                Assert.True(currentToken.type == expectedTypes[i]);
                Assert.True(currentToken.lexeme == expectedLexemes[i]);
                Console.WriteLine("lexeme: " + currentToken.lexeme + " | TokenType: " + expectedTypes[i]);
                currentToken = lexer.GetNextToken();
                i++;
            }while(currentToken.type != TokenType.EOF);
        }
Esempio n. 15
0
 private void GetNextToken()
 {
     token = lexer.GetNextToken();
 }
Esempio n. 16
0
 public Parser(Lexer lexer)
 {
     this.lexer = lexer;
     this.token = lexer.GetNextToken();
     vars       = new Dictionary <string, float>();
 }