Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unittest
- from TestUtils import TestLexer
- # BOOLEAN: 'boolean';
- # BREAK: 'break';
- # CLASS: 'class';
- # CONTINUE: 'continue';
- # DO: 'do';
- # ELSE: 'else';
- # EXTENDS: 'extends';
- # FLOAT: 'float';
- # IF: 'if';
- # INT: 'int';
- # OBJECT_CREATION: 'new';
- # STRING: 'string';
- # THEN: 'then';
- # FOR: 'for';
- # RETURN: 'return';
- # TRUE: 'true';
- # FALSE: 'false';
- # VOID: 'void';
- # NIL: 'nil';
- # THIS: 'this';
- # FINAL: 'final';
- # STATIC: 'static';
- # TO: 'to';
- # DOWNTO: 'downto';
- # TYPE_LIST_PRIMITIVE: INT | FLOAT | BOOLEAN | STRING;
- # PLUS: '+';
- # MINUS: '-';
- # MULTIPLY: '*';
- # FLOAT_DIV: '/';
- # INT_DIV: '\\';
- # MOD: '%';
- # NOT_EQUAL: '!=';
- # EQUAL: '==';
- # LESS_THAN: '<';
- # GREATER_THAN: '>';
- # LESS_THAN_EQ: '<=';
- # GREATER_THAN_EQ: '>=';
- # LOGICAL_OR: '||';
- # LOGICAL_AND: '&&';
- # LOGICAL_NOT: '!';
- # CONCATENATION: '^';
- # ASIGN: ':=';
- # LEFT_SQUARE_BRACKET: '[';
- # RIGHT_SQUARE_BRACKET: ']';
- # LEFT_PARENTHESIS: '{';
- # RIGHT_PARENTHESIS: '}';
- # LEFT_BRACKET: '(';
- # RIGHT_BRACKET: ')';
- # SEMICOLON: ';';
- # COLON: ':';
- # DOT: '.';
- # COMMA: ',';
- # fragment DIGIT: [0-9];
- # fragment EXPONENT: ('e' | 'E') (PLUS | MINUS)? DIGIT+;
- # fragment DECIMAL: DOT DIGIT*;
- # // INTEGER_LITERAL: '0' | ([1-9]NUMBER) ;
- # INTEGER_LITERAL: DIGIT+;
- # FLOAT_LITERAL: INTEGER_LITERAL (DECIMAL? EXPONENT | DECIMAL);
- # BOOL_LITERAL: TRUE | FALSE;
- # STRING_LITERAL: '"' ( '\\' [btnfr"'\\] | ~[\r\n\\"])* '"';
- # fragment ARRAY_CONTENT:
- # INTEGER_LITERAL (COMMA INTEGER_LITERAL)*
- # | FLOAT_LITERAL (COMMA FLOAT_LITERAL)*
- # | BOOL_LITERAL (COMMA BOOL_LITERAL)*
- # | STRING_LITERAL (COMMA STRING_LITERAL)*;
- # ARRAY_LITERAL: LEFT_PARENTHESIS ARRAY_CONTENT RIGHT_PARENTHESIS;
- # integer_type: INTEGER_LITERAL;
- # float_type: FLOAT_LITERAL;
- # boolean_type: BOOL_LITERAL;
- # string_type: STRING_LITERAL;
- # varible_type:
- # integer_type
- # | float_type
- # | boolean_type
- # | string_type;
- # IDENTIFIER: [a-zA-Z_] [a-zA-Z0-9_]*;
- class LexerSuite(unittest.TestCase):
- def test_mixed_case_identifier(self):
- """test identifiers"""
- self.assertTrue(TestLexer.test("AbC","AbC,<EOF>",101))
- def test_identifier_with_digit_at_the_end(self):
- """test identifiers"""
- self.assertTrue(TestLexer.test("Abc1","Abc1,<EOF>",102))
- def test_boolean_mixed_with_digit(self):
- """test identifiers"""
- self.assertTrue(TestLexer.test("1boolean","1,boolean,<EOF>",103))
- def test_boolean_mixed_with_digit_and_underscore(self):
- """test identifiers"""
- self.assertTrue(TestLexer.test("1_boolean","1,_boolean,<EOF>",104))
- def test_boolean_token(self):
- """test token"""
- self.assertTrue(TestLexer.test("boolean","boolean,<EOF>",105))
- def test_PLUS_token(self):
- """test token"""
- self.assertTrue(TestLexer.test("+","+,<EOF>",106))
- def test_PLUS_with_identifier(self):
- """test token"""
- self.assertTrue(TestLexer.test("+abc123","+,abc123,<EOF>",107))
- def test_PLUS_with_identifier_and_digit(self):
- """test token"""
- self.assertTrue(TestLexer.test("123+abc123_1","123,+,abc123_1,<EOF>",108))
- def test_brackets(self):
- """test brackets"""
- self.assertTrue(TestLexer.test("()[]{}","(,),[,],{,},<EOF>",109))
- def test_brackets_with_identifier(self):
- """test brackets"""
- self.assertTrue(TestLexer.test("()abc123[]{}","(,),abc123,[,],{,},<EOF>",110))
- def test_brackets_with_identifier_and_digit(self):
- """test brackets"""
- self.assertTrue(TestLexer.test("()abc123_[1fd+12]{123qw12}",
- "(,),abc123_,[,1,fd,+,12,],{,123,qw12,},<EOF>",111))
- # simple program to test lexer
- def test_simple_program(self):
- """test simple program"""
- input = """
- void main();
- """
- expect = "void,main,(,),;,<EOF>"
- self.assertTrue(TestLexer.test(input,expect,112))
- # simple function to test lexer
- # function with return type int
- # function has many parameters
- # function has many statements
- # function has many expressions
- def test_simple_function(self):
- """test simple function"""
- input = """
- 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;
- }
- """
- 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>"
- self.assertTrue(TestLexer.test(input,expect,113))
- # complex function with many statements to test lexer
- # function with return type float
- # function has many parameters
- # function has if else statement
- # function has for loop
- # function has break statement
- # function has continue statement
- # function has return statements
- # function has method invocation statements
- def test_complex_function_with_statements(self):
- """test complex function with statements"""
- input = """
- 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;
- }
- """
- 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>"
- self.assertTrue(TestLexer.test(input,expect,114))
Add Comment
Please, Sign In to add comment