aboutsummaryrefslogtreecommitdiff
path: root/module-test/lexer_test.cc
blob: b563f749fc95782aa402bd3e50a9ff78312be6fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

// test lexer
/* 
$ 
g++ \
    module-test/lexer_test.cc \
    src/lang_analysis/token.cc \
    src/lang_analysis/ast.cc \
    src/lang_analysis/symbol_property.cc \
    src/util/string.cc \
    -std=c++23 -Iinclude -Ithird_party/magic_enum/include \
    -O0 -ggdb3 -fno-omit-frame-pointer -fno-inline -D_GLIBCXX_ASSERTIONS \
    -o module-test/lexer_test.out
*/

#include <iostream>
#include <string>
#include <type_traits>
#include <unistd.h>
#include <cstring>
#include <vector>
#include <print>
#include <stdexcept>

#include "magic_enum/magic_enum.hpp"

#include "lang_analysis/ast.h"
#include "lang_analysis/enum_type.h"
#include "lang_analysis/token.h"
#include "global.h"
#include "error.h"
#include "util/string.h"

using namespace std;
using namespace ov4;

constexpr int MAXLINE = 1024;


int main()
{
    verbose = true;
    int test_case = 19;
    string s;

    switch (test_case)
    {
        case 0:
            s = "hello | hi || foo && bar || baz &";
            break;
        case 1:
            s = "hi";
            break;
        case 2:
            s = "&&";
            break;
        case 3:
            s = "hi (hello || foo)";
            break;
        case 4:
            s = "alpha || beta (gamma argument && (delta || epsilon second_argument & ) )";
            break;
        case 5:
            s = "alpha || beta && (gamma || epsilon)";
            break;
            
        // bracket matching test
        case 6:
            s = ")alpha && beta(";
            break;

        // basic scriptio continua
        // syntax is wrong but just to test tokenizer
        case 7:
            s = "((alpha&|&&beta)||gamma&&&delta)";
            break;

        // simple guard test
        case 8:
            s = "\'text\' &&";
            break;

        // guard test
        case 9:
            s = "\'realOperator\' && \"fake&&\" real ||";
            break;

        // guard test
        // with space
        case 10:
            s = "\'This should be 1 token\' symbol && text \'&&\'";
            break;

        // missing test
        // missing right
        case 11:
            s = "alpha&&";
            break;
        
        // missing test
        // missing left
        case 12:
            s = "&&left";
            break;

        // existance test
        // exist right
        case 13:
            s = "alpha&beta";
            break;

        // simple subshell test
        case 14:
            s = "alpha && (beta)";
            break;

        // complex subshell test
        case 15:
            s = "alpha && (beta || gamma) && (sleep &)";
            break;

        // \n test
        case 16:
            s = "(ls)";
            break;

        // more LEFT brackets than RIGHT
        case 17:
            s = "((alpha)";
            break;

        // quotation mark in command
        case 18:
            s = "argument_printer.out \"hello hi\" second third &";
            break;

        // harder quotation mark test
        case 19:
            s = "alpha \"arg1 arg2 \'arg3 arg4\' arg5\" && beta &";
            break;

        default:
            return 0;
    }

    try
    {
        T_lexer lexer_instance;
        lexer_instance.tokenizer(s);
        cout << "---------- tokenizer ----------" << endl;
        for (auto c : lexer_instance.token)
        {
            if (c.token_type == LOGIC_AND)
                println("{} LOGIC_AND", c.bracket_depth);
            else if (c.token_type == LOGIC_OR)
                println("{} LOGIC_OR", c.bracket_depth);
            else if (c.token_type == ASYNC)
                println("{} ASYNC", c.bracket_depth);
            else if (c.token_type == PIPE)
                println("{} PIPE", c.bracket_depth);
            else if (c.token_type == LEFT_BRACKET)
                println("{} LEFT_BRACKET", c.bracket_depth);
            else if (c.token_type == RIGHT_BRACKET)
                println("{} RIGHT_BRACKET", c.bracket_depth);
            else if (c.token_type == TEXT)
                println("{} TEXT .{}.", c.bracket_depth, c.text);
        }

        cout << "---------- parse ----------" << endl;

        lexer_instance.parse(0, lexer_instance.token.size(), lexer_instance.alloc_ast());

        cout << "---------- AST ----------" << endl;
        for (size_t i = 0; i < lexer_instance.ast.size(); i++)
        {
            print("{}: type: {} left: {} right: {}, subshell {}", 
                i, 
                magic_enum::enum_name(lexer_instance.ast[i].token_type), 
                lexer_instance.ast[i].left, 
                lexer_instance.ast[i].right,
                lexer_instance.ast[i].subshell ? "TRUE" : "FALSE");
            if (lexer_instance.ast[i].token_type == TEXT)
                print(" command: {}", lexer_instance.ast[i].command_text);
            println();
        }

    }
    catch(const T_error &e)
    {
        cerr << "Error: " << magic_enum::enum_name(e.code) << endl;
        cerr << "what: " << e.what() << endl;
    }
    

    

    
    return 0;
}