-
Notifications
You must be signed in to change notification settings - Fork 11
/
scanner.h
142 lines (122 loc) · 2.88 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Project: Implementace překladače imperativního jazyka IFJ17.
*
* @brief Scanner interface.
*
* @author Timotej Halás <xhalas10@stud.fit.vutbr.cz>
* @author Dominik Harmim <xharmi00@stud.fit.vutbr.cz>
* @author Vojtěch Hertl <xhertl04@stud.fit.vutbr.cz>
*/
#ifndef _SCANNER_H
#define _SCANNER_H
#include <stdio.h>
#include "dynamic_string.h"
/**
* @enum Reserved keywords.
*/
typedef enum
{
KEYWORD_AND,
KEYWORD_AS,
KEYWORD_ASC,
KEYWORD_BOOLEAN,
KEYWORD_CONTINUE,
KEYWORD_DECLARE,
KEYWORD_DIM,
KEYWORD_DO,
KEYWORD_DOUBLE,
KEYWORD_ELSE,
KEYWORD_ELSEIF,
KEYWORD_END,
KEYWORD_EXIT,
KEYWORD_FALSE,
KEYWORD_FOR,
KEYWORD_CHR,
KEYWORD_FUNCTION,
KEYWORD_IF,
KEYWORD_INPUT,
KEYWORD_INTEGER,
KEYWORD_LENGTH,
KEYWORD_LOOP,
KEYWORD_NEXT,
KEYWORD_NOT,
KEYWORD_OR,
KEYWORD_PRINT,
KEYWORD_RETURN,
KEYWORD_SCOPE,
KEYWORD_SHARED,
KEYWORD_STRING,
KEYWORD_STATIC,
KEYWORD_SUBSTR,
KEYWORD_THEN,
KEYWORD_TRUE,
KEYWORD_WHILE,
} Keyword;
/**
* @enum Type of token.
*/
typedef enum
{
TOKEN_TYPE_EOF, /// End of file
TOKEN_TYPE_EOL, /// End of line
TOKEN_TYPE_EMPTY, /// Empty
TOKEN_TYPE_IDENTIFIER, /// Identifier
TOKEN_TYPE_KEYWORD, /// Keyword
TOKEN_TYPE_INT_NUMBER, /// Integer number
TOKEN_TYPE_DOUBLE_NUMBER, /// Double number
TOKEN_TYPE_STRING, /// String
TOKEN_TYPE_NEQ, /// Not equal <>
TOKEN_TYPE_LEQ, /// Less or equal <=
TOKEN_TYPE_LTN, /// Less than <
TOKEN_TYPE_MEQ, /// More or equal >=
TOKEN_TYPE_MTN, /// More than >
// operators
TOKEN_TYPE_ASSIGN, /// Assign =
TOKEN_TYPE_PLUS, /// Plus +
TOKEN_TYPE_MINUS, /// Minus -
TOKEN_TYPE_MUL, /// Multiplication *
TOKEN_TYPE_DIV, /// Division / result always double
TOKEN_TYPE_IDIV, /// Integer division \ only works with integers
TOKEN_TYPE_LEFT_BRACKET, /// Left bracket (
TOKEN_TYPE_RIGHT_BRACKET, /// Right bracket )
TOKEN_TYPE_COMMA, /// Comma ,
TOKEN_TYPE_SEMICOLON, /// Semicolon ;
} Token_type;
/**
* @union Token attribute.
*/
typedef union
{
Dynamic_string *string; /// String or identifier value.
int integer; /// Integer value.
Keyword keyword; /// Keyword, one of the KEYWORD_... constant
double decimal; /// Decimal value.
} Token_attribute;
/**
* @struct Token representation.
*/
typedef struct
{
Token_type type; /// Token type, one of token_type constants.
Token_attribute attribute; /// Attribute of token.
} Token;
/**
* Sets source file to be scanned.
*
* @param f Pointer to source file.
*/
void set_source_file(FILE *f);
/**
* Sets dynamic string to be written into.
*
* @param string Pointer to dynamic string.
*/
void set_dynamic_string(Dynamic_string *string);
/**
* This is the main function of scanner, scans token after token and sends it further.
*
* @param token Pointer to output token.
* @return 0 (SCANNER_TOKEN_OK) if token is OK, otherwise in case of lex error one of SCANNER_ERROR_... constant.
*/
int get_next_token(Token *token);
#endif //_SCANNER_H