forked from xjuric29/ifj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.h
120 lines (107 loc) · 3.69 KB
/
scanner.h
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
#ifndef IFJ_COMPILATOR_SCANNER_H
#define IFJ_COMPILATOR_SCANNER_H
#include "str.h"
/** @file token.h
* @brief Header file for scanner and token interface
* @author Jiri Jurica (xjuric29), Jiri Furda (xfurda00)
* @todo Does scanner create nonToken type or print error and die?
*/
#define SUCCESS 0
#define LEX_ERROR 1
#define SYN_ERROR 2
#define SEM_ERROR_FUNC 3
#define SEM_ERROR_COMP 4
#define SEM_ERROR_OTHER 6
#define INTERNAL_ERROR 99
// --- Constants and structures ---
/** @brief An enum type to determine token type
* @todo What means "Term" data type?
*/
typedef enum
{
// KEY WORDS
// Don't change order of keywords! This is important for correct identification in scanner
KW_as, /// Keyword "As"
KW_asc, /// Keyword "Asc"
KW_declare, /// Keyword "Declare"
KW_dim, /// Keyword "Dim"
KW_do, /// Keyword "Do"
KW_double, /// Keyword "Double"
KW_else, /// Keyword "Else"
KW_end, /// Keyword "End"
KW_chr, /// Keyword "Chr"
KW_function, /// Keyword "Function"
KW_if, /// Keyword "If"
KW_input, /// Keyword "Input"
KW_integer, /// Keyword "Integer"
KW_length, /// Keyword "Length"
KW_loop, /// Keyword "Loop"
KW_print, /// Keyword "Print"
KW_return, /// Keyword "Return"
KW_scope, /// Keyword "Scope"
KW_string, /// Keyword "String"
KW_subStr, /// Keyword "SubStr"
KW_then, /// Keyword "Then"
KW_while, /// Keyword "While"
// -- Reserved words below (Maybe won't use them but they are part of extensions) --
KW_and, /// Reserved keyword "And"
KW_boolean, /// Reserved keyword "Boolean"
KW_continue, /// Reserved keyword "Continue"
KW_elseif, /// Reserved keyword "Elseif"
KW_exit, /// Reserved keyword "Exit"
KW_false, /// Reserved keyword "False"
KW_for, /// Reserved keyword "For"
KW_next, /// Reserved keyword "Next"
KW_not, /// Reserved keyword "Not"
KW_or, /// Reserved keyword "Or"
KW_shared, /// Reserved keyword "Shared"
KW_static, /// Reserved keyword "Static"
KW_true, /// Reserved keyword "True"
// TOKEN TYPES
TOK_identifier,
TOK_integer,
TOK_decimal,
TOK_string,
TOK_lParenth, /// Left parenthesis = "("
TOK_rParenth, /// Right parenthesis ")"
TOK_startBlock,
TOK_endBlock,
TOK_plus, /// Operator "+"
TOK_minus, /// Operator "-"
TOK_mul, /// Operator "*"
TOK_div, /// Operator "/"
TOK_divInt, /// Operator "\"
TOK_equal, /// Operator "="
TOK_notEqual, /// Operator "<>"
TOK_less, /// Operator "<"
TOK_lessEqual, /// Operator "<="
TOK_greater, /// Operator ">"
TOK_greaterEqual, /// Operator ">="
TOK_comma, /// Operator ","
TOK_semicolon, /// Operator ";"
TOK_endOfLine, /// "\n"
TOK_endOfFile
} tokenType_t;
/// @brief An union type representing all possible data types of token value
typedef struct
{
string *stringVal;
int integer;
double decimal;
} tokenValue_t;
/// @brief A structure type representing token - its type and value
typedef struct
{
tokenType_t type;
tokenValue_t value;
} token_t;
/**
* Reads part of code from stdin and coverts to correct token. If token type is TOK_identifier or TOK_string function
* also fills loadedToken.value.stringVal, if token type is TOK_integer fills loadedToken.value.integer and if token
* type is TOK_decimal fills loadedToken.value.decimal.
* @param loadedToken - Pre-allocated structure. Before using of this function is important to allocate and initialize
* loadedToken.value.stringVal
* @return 0 if everything is ok, 1 for lexical analysis error, else 99 for internal error
*/
int getToken (token_t *loadedToken);
#endif