예제 #1
0
def test_lexer_on_cpp_class():
    code = textwrap.dedent('''
        #include <iostream>
        using namespace std;
        
        class Rectangle {
            int width, height;
          public:
            void set_values (int,int);
            int area() {return width*height;}
        };
        ''')
    result = [
        (Token.Comment.Preproc, '#'),
        (Token.Comment.Preproc, 'include'),
        (Token.Text, ' '),
        (Token.Comment.PreprocFile, '<iostream>'),
        (Token.Comment.Preproc, '\n'),
        (Token.Keyword, 'using'),
        (Token.Text, ' '),
        (Token.Keyword, 'namespace'),
        (Token.Text, ' '),
        (Token.Name, 'std'),
        (Token.Punctuation, ';'),
        (Token.Text, '\n'),
        (Token.Text, '\n'),
        (Token.Keyword, 'class'),
        (Token.Text, ' '),
        (Token.Name.Class, 'Rectangle'),  # <--
        (Token.Text, ' '),
        (Token.Punctuation, '{'),
        (Token.Text, '\n'),
        (Token.Text, '    '),
        (Token.Keyword.Type, 'int'),
        (Token.Text, ' '),
        (Token.Name, 'width'),
        (Token.Punctuation, ','),
        (Token.Text, ' '),
        (Token.Name, 'height'),
        (Token.Punctuation, ';'),
        (Token.Text, '\n'),
        (Token.Text, '  '),
        (Token.Keyword, 'public'),
        (Token.Operator, ':'),
        (Token.Text, '\n'),
        (Token.Text, '    '),
        (Token.Keyword.Type, 'void'),
        (Token.Text, ' '),
        (Token.Name, 'set_values'),
        (Token.Text, ' '),
        (Token.Punctuation, '('),
        (Token.Keyword.Type, 'int'),
        (Token.Punctuation, ','),
        (Token.Keyword.Type, 'int'),
        (Token.Punctuation, ')'),
        (Token.Punctuation, ';'),
        (Token.Text, '\n'),
        (Token.Text, '    '),
        (Token.Keyword.Type, 'int'),
        (Token.Text, ' '),
        (Token.Name.Function, 'area'),  # <--
        (Token.Punctuation, '('),
        (Token.Punctuation, ')'),
        (Token.Text, ' '),
        (Token.Punctuation, '{'),
        (Token.Keyword, 'return'),
        (Token.Text, ' '),
        (Token.Name, 'width'),
        (Token.Operator, '*'),
        (Token.Name, 'height'),
        (Token.Punctuation, ';'),
        (Token.Punctuation, '}'),
        (Token.Text, '\n'),
        (Token.Punctuation, '}'),
        (Token.Punctuation, ';'),
        (Token.Text, '\n')
    ]

    lex = CppLexer()
    tokenList = lex.get_tokens(code)
    # print(list(tokenList))
    assert list(tokenList) == result