Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81350 / py2html / py2html.cpp
blob: 65d28da0cda379a789e8687aabf8782dff632f05 (plain) (blame)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <iostream>
#include <string>
#include <cctype>
#include <unistd.h>
#include <sys/types.h>
#include <unordered_map>
#include <set>
#include "Token.h"

std::string operatorChars = "+-*/%=<>&|^~";
std::string parenthesisChars = "[]{}(),";
std::set<std::string> operators = {
    "+",
    "-",
    "*",
    "/",
    "//",
    "%",
    "**",
    ">",
    "<",
    ">=",
    "<=",
    "==",
    "!=",
    "&",
    "|",
    "~",
    "^",
    ">>",
    "<<",
    "=",
    "+=",
    "-=",
    "*=",
    "/=",
    "%=",
    "**=",
    "//=",
    "^=",
    "&=",
    "|=",
    ">>=",
    "<<="
};

using namespace std;
FILE* in;
FILE* out;

bool isNumber(char c){
    return c >= '0' && c <= '9';
}

bool notValidNumber(char c){
    return !(isNumber(c) || c == '.');
}

bool isQuote(char c){
    return c == '\'' || c == '"';
}

bool isLastQuote(char firstQuote, char previousChar, char currentChar){
    return firstQuote == currentChar && previousChar != '\\';
}

bool isOperator(char c){
    return operatorChars.find(c) != std::string::npos;
}

bool isParenthesis(char c){
    return parenthesisChars.find(c) != std::string::npos;
}

bool isValidIdentifierChar(char c){
    return !(isOperator(c) || isParenthesis(c) || isQuote(c) || c == '\n' || isblank(c) || c == ',');
}

bool isValidOperator(std::string word){
    std::set<std::string>::iterator iter;
    iter = operators.find(word);
    return iter != operators.end();
}

std::string readFormat(){
    std::string word = "{";
    char c;

    while((c = fgetc(in)) != EOF && c != '}'){
        word += c;
    }
    word += '}';
    return word;
}

std::string readNumber(){
    fseek(in, -1, SEEK_CUR);
    std::string word;
    char c;
    int dotCount = 0;

    while((c = fgetc(in)) != EOF && !notValidNumber(c)){
        //printf("%c", c);
        if(notValidNumber(c)){
            return "";
        } else if(c == '.'){
            dotCount++;
            if(dotCount >= 2){
                return "";
            }
        }
        word += c;
    }
    if(notValidNumber(c)){
        fseek(in, -1, SEEK_CUR);
    }
    return word;
}

std::string readString(char firstQuote){
    std::string word = "";
    word += firstQuote;
    char c;
    char previousChar = ' ';

    while((c = fgetc(in)) != EOF && !isLastQuote(firstQuote, previousChar, c)){
        if( c == '{'){
            fprintf(out, "<span class=\"string\">%s</span>", word.c_str());
            word = readFormat();
            fprintf(out, "<span class=\"formatString\">%s</span>", word.c_str());
            word = "";
        } else {
            word += c;
        }
        previousChar = c;
    }
    word += firstQuote;
    fprintf(out, "<span class=\"string\">%s</span>", word.c_str());
    return word;
}

std::string readOperator(){
    fseek(in, -1, SEEK_CUR);
    std::string word;
    char c;
    while((c = fgetc(in)) != EOF && isOperator(c)){
        word += c;
    }
    if(!isOperator(c)){
        fseek(in, -1, SEEK_CUR);
    }
    return word;
}

std::string readWord(){
    fseek(in, -1, SEEK_CUR);
    std::string word;
    char c;
    while((c = fgetc(in)) != EOF && isValidIdentifierChar(c)){
        word += c;
     }

     if(!isValidIdentifierChar(c)){
        fseek(in, -1, SEEK_CUR);
     }
    return word;
}

void lexer(){
    char c;
    bool validSyntaxis = 1;
    std::string word;
    std::unordered_map<std::string, TokenType>::const_iterator iter;

    while((c = fgetc(in)) != EOF){
        if(isQuote(c)){
            word = readString(c);
            //fprintf(out, "<span class=\"string\">%s</span>", word.c_str());
            continue;
        }
        if(isNumber(c)){
            word = readNumber();
            fprintf(out, "<span class=\"number\">%s</span>", word.c_str());
            continue;
        }
        if(isOperator(c)){
            word = readOperator();
            if(isValidOperator(word)){
                validSyntaxis = 0;
            }
            fprintf(out, "<span class=\"operator\">%s</span>", word.c_str());
            // trqbwa da se proweti dali e korektno
            // t.e. da e w nqkyw set ili hash
            continue;
        }
        if(isParenthesis(c)){
            fprintf(out, "%c", c);
            continue;
        }
        if(isblank(c) || c == '\n'){
            fprintf(out, "%c", c);
            continue;
        }

        word = readWord();
        iter = keywords.find(word);
        if(iter == keywords.end()){
            fprintf(out, "%s", word.c_str());
        } else {
            fprintf(out, "<span class=\"keyword\">%s</span>", word.c_str());
        }

    }
}

int main(int argc, const char* argv[])
{
    ++argv, --argc;  /* skip over program name */
    if ( argc > 0 )
            in = fopen( argv[0], "r" );
    else
            in = stdin;

    out = fopen("index.html", "w");

    fputs(
        "<!doctype>"
        "<html>"
        "<head>"
        "    <title>program.py</title>"
        "    <style>"
        "        .keyword {"
        "            color: purple;"
        "        }"
        "        .number {"
        "            color: orange;"
        "        }"
        "        .string {"
        "            color: green;"
        "        }"
        "        .operator {"
        "            font-style: bold;"
        "        }"
        "        .formatString {"
        "            color: red;"
        "        }"
        "    </style>"
        "</head>"
        "<body>"
        "    <pre class=\"code\">",
        out);

    lexer();
    fputs("</pre></body></html>", out);
    fclose(in);
    fclose(out);
    return 0;
}