aboutsummaryrefslogtreecommitdiffstats
path: root/c_lexer/src/main.cpp
blob: 1e45bbdbc185ab673781cb712fc8225ed2ddb88f (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
#include "c_lexer.hpp"

#include <cassert>
#include <sstream>
#include <vector>

std::string *yylval;

int main() {
	printf("[\n");
	std::string sourceLine, streamLine, sourceCol, classType, text;
	while(1) {
		// get the token type and run the lexer
		std::stringstream str_line, src_col, src_line;

		TokenType type = (TokenType)yylex();

		str_line << lineCount;
		streamLine = str_line.str();

		src_col << spaceCount;
		sourceCol = src_col.str();

		src_line << sourceLineCount;
		sourceLine = src_line.str();

		if(type == None) {
			// returns None when the file ends and we want to break then
			break;
		} else if(type == Invalid) {
			// type is Invalid
			text = *yylval;
			classType = "Invalid";
			delete yylval;
		} else if(type == Keyword) {
			// found a keyword
			text = *yylval;
			classType = "Keyword";
			delete yylval;
		} else if(type == Identifier) {
			// found an identifier
			text = *yylval;
			classType = "Identifier";
			delete yylval;
		} else if(type == Operator) {
			// found an operator
			text = *yylval;
			classType = "Operator";
			delete yylval;
		} else if(type == Constant) {
			// found a constant
			text = *yylval;
			classType = "Constant";
			delete yylval;
		} else if(type == StringLiteral) {
			// found a string literal
			text = *yylval;
			classType = "StringLiteral";
			delete yylval;
		} else {
			// if any other type comes we assert to 0
			assert(0);
			return 1;
		}

		printf("%s,\n", toJson(classType, text, streamLine, sourceCol, sourceLine, fileName).c_str());

		spaceCount += charLength;
	}

	printf("{}\n]\n");

	return 0;
}