tombroskipc

LexerSuite.py

Sep 9th, 2021 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.44 KB | None | 0 0
  1. import unittest
  2. from TestUtils import TestLexer
  3.  
  4. # BOOLEAN: 'boolean';
  5. # BREAK: 'break';
  6. # CLASS: 'class';
  7. # CONTINUE: 'continue';
  8. # DO: 'do';
  9. # ELSE: 'else';
  10. # EXTENDS: 'extends';
  11. # FLOAT: 'float';
  12. # IF: 'if';
  13. # INT: 'int';
  14. # OBJECT_CREATION: 'new';
  15. # STRING: 'string';
  16. # THEN: 'then';
  17. # FOR: 'for';
  18. # RETURN: 'return';
  19. # TRUE: 'true';
  20. # FALSE: 'false';
  21. # VOID: 'void';
  22. # NIL: 'nil';
  23. # THIS: 'this';
  24. # FINAL: 'final';
  25. # STATIC: 'static';
  26. # TO: 'to';
  27. # DOWNTO: 'downto';
  28. # TYPE_LIST_PRIMITIVE: INT | FLOAT | BOOLEAN | STRING;
  29. # PLUS: '+';
  30. # MINUS: '-';
  31. # MULTIPLY: '*';
  32. # FLOAT_DIV: '/';
  33. # INT_DIV: '\\';
  34. # MOD: '%';
  35. # NOT_EQUAL: '!=';
  36. # EQUAL: '==';
  37. # LESS_THAN: '<';
  38. # GREATER_THAN: '>';
  39. # LESS_THAN_EQ: '<=';
  40. # GREATER_THAN_EQ: '>=';
  41. # LOGICAL_OR: '||';
  42. # LOGICAL_AND: '&&';
  43. # LOGICAL_NOT: '!';
  44. # CONCATENATION: '^';
  45. # ASIGN: ':=';
  46.  
  47. # LEFT_SQUARE_BRACKET: '[';
  48. # RIGHT_SQUARE_BRACKET: ']';
  49. # LEFT_PARENTHESIS: '{';
  50. # RIGHT_PARENTHESIS: '}';
  51. # LEFT_BRACKET: '(';
  52. # RIGHT_BRACKET: ')';
  53. # SEMICOLON: ';';
  54. # COLON: ':';
  55. # DOT: '.';
  56. # COMMA: ',';
  57.  
  58.  
  59.  
  60.  
  61.  
  62. # fragment DIGIT: [0-9];
  63. # fragment EXPONENT: ('e' | 'E') (PLUS | MINUS)? DIGIT+;
  64. # fragment DECIMAL: DOT DIGIT*;
  65.  
  66. # // INTEGER_LITERAL: '0' | ([1-9]NUMBER) ;
  67. # INTEGER_LITERAL: DIGIT+;
  68.  
  69. # FLOAT_LITERAL: INTEGER_LITERAL (DECIMAL? EXPONENT | DECIMAL);
  70.  
  71. # BOOL_LITERAL: TRUE | FALSE;
  72.  
  73. # STRING_LITERAL: '"' ( '\\' [btnfr"'\\] | ~[\r\n\\"])* '"';
  74.  
  75. # fragment ARRAY_CONTENT:
  76. #   INTEGER_LITERAL (COMMA INTEGER_LITERAL)*
  77. #   | FLOAT_LITERAL (COMMA FLOAT_LITERAL)*
  78. #   | BOOL_LITERAL (COMMA BOOL_LITERAL)*
  79. #   | STRING_LITERAL (COMMA STRING_LITERAL)*;
  80.  
  81. # ARRAY_LITERAL: LEFT_PARENTHESIS ARRAY_CONTENT RIGHT_PARENTHESIS;
  82.  
  83. # integer_type: INTEGER_LITERAL;
  84. # float_type: FLOAT_LITERAL;
  85. # boolean_type: BOOL_LITERAL;
  86. # string_type: STRING_LITERAL;
  87. # varible_type:
  88. #   integer_type
  89. #   | float_type
  90. #   | boolean_type
  91. #   | string_type;
  92.  
  93. # IDENTIFIER: [a-zA-Z_] [a-zA-Z0-9_]*;
  94.  
  95.  
  96. class LexerSuite(unittest.TestCase):
  97.     def test_mixed_case_identifier(self):
  98.         """test identifiers"""
  99.         self.assertTrue(TestLexer.test("AbC","AbC,<EOF>",101))
  100.     def test_identifier_with_digit_at_the_end(self):
  101.         """test identifiers"""
  102.         self.assertTrue(TestLexer.test("Abc1","Abc1,<EOF>",102))
  103.     def test_boolean_mixed_with_digit(self):
  104.         """test identifiers"""
  105.         self.assertTrue(TestLexer.test("1boolean","1,boolean,<EOF>",103))
  106.     def test_boolean_mixed_with_digit_and_underscore(self):
  107.         """test identifiers"""
  108.         self.assertTrue(TestLexer.test("1_boolean","1,_boolean,<EOF>",104))
  109.     def test_boolean_token(self):
  110.         """test token"""
  111.         self.assertTrue(TestLexer.test("boolean","boolean,<EOF>",105))
  112.     def test_PLUS_token(self):
  113.         """test token"""
  114.         self.assertTrue(TestLexer.test("+","+,<EOF>",106))
  115.     def test_PLUS_with_identifier(self):
  116.         """test token"""
  117.         self.assertTrue(TestLexer.test("+abc123","+,abc123,<EOF>",107))
  118.     def test_PLUS_with_identifier_and_digit(self):
  119.         """test token"""
  120.         self.assertTrue(TestLexer.test("123+abc123_1","123,+,abc123_1,<EOF>",108))
  121.     def test_brackets(self):
  122.         """test brackets"""
  123.         self.assertTrue(TestLexer.test("()[]{}","(,),[,],{,},<EOF>",109))
  124.     def test_brackets_with_identifier(self):
  125.         """test brackets"""
  126.         self.assertTrue(TestLexer.test("()abc123[]{}","(,),abc123,[,],{,},<EOF>",110))
  127.     def test_brackets_with_identifier_and_digit(self):
  128.         """test brackets"""
  129.         self.assertTrue(TestLexer.test("()abc123_[1fd+12]{123qw12}",
  130.                 "(,),abc123_,[,1,fd,+,12,],{,123,qw12,},<EOF>",111))
  131.     # simple program to test lexer
  132.     def test_simple_program(self):
  133.         """test simple program"""
  134.         input = """
  135.        void main();
  136.                """
  137.         expect = "void,main,(,),;,<EOF>"
  138.         self.assertTrue(TestLexer.test(input,expect,112))
  139.     # simple function to test lexer
  140.     # function with return type int
  141.     # function has many parameters
  142.     # function has many statements
  143.     # function has many expressions
  144.     def test_simple_function(self):
  145.                 """test simple function"""
  146.                 input = """
  147.                int main(int a, int b, int c){
  148.                    if a<b then
  149.                        io.writeStrLn("Expression is true");
  150.                    else
  151.                        io.writeStrLn("Expression is false");
  152.                    return a+b*c/2;
  153.                }
  154.                """
  155.                 expect = "int,main,(,int,a,,,int,b,,,int,c,),{,if,a,<,b,then,io,.,writeStrLn,(,\"Expression is true\",),;,else,io,.,writeStrLn,(,\"Expression is false\",),;,return,a,+,b,*,c,/,2,;,},<EOF>"
  156.                 self.assertTrue(TestLexer.test(input,expect,113))
  157.    
  158.     # complex function with many statements to test lexer
  159.     # function with return type float
  160.     # function has many parameters
  161.     # function has if else statement
  162.     # function has for loop
  163.     # function has break statement
  164.     # function has continue statement
  165.     # function has return statements
  166.     # function has method invocation statements
  167.     def test_complex_function_with_statements(self):
  168.         """test complex function with statements"""
  169.         input = """
  170.        float main(int a, int b, int c){
  171.            int d,e,f;
  172.            d := a+b;
  173.            e := b*c/(g+h)*a[2]%(b[c]);
  174.            if d<e then
  175.                io.writeStrLn("d is less than e");
  176.            else
  177.                io.writeStrLn("d is greater than e");
  178.            for d:=0 downto f do
  179.            {
  180.                io.writeStrLn("d is less than f");
  181.                if d>=f then
  182.                    break;
  183.                else
  184.                    continue;
  185.            }
  186.            return f;
  187.        }
  188.        """                                                                                                                                                                                                                                      
  189.         expect = "float,main,(,int,a,,,int,b,,,int,c,),{,int,d,,,e,,,f,;,d,:=,a,+,b,;,e,:=,b,*,c,/,(,g,+,h,),*,a,[,2,],%,(,b,[,c,],),;,if,d,<,e,then,io,.,writeStrLn,(,\"d is less than e\",),;,else,io,.,writeStrLn,(,\"d is greater than e\",),;,for,d,:=,0,downto,f,do,{,io,.,writeStrLn,(,\"d is less than f\",),;,if,d,>=,f,then,break,;,else,continue,;,},return,f,;,},<EOF>"
  190.         self.assertTrue(TestLexer.test(input,expect,114))
Add Comment
Please, Sign In to add comment