-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.h
More file actions
108 lines (89 loc) · 1.66 KB
/
Copy pathlex.h
File metadata and controls
108 lines (89 loc) · 1.66 KB
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
#ifndef PHPINTERP_LEX_H
#define PHPINTERP_LEX_H
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
typedef enum TOKENTYPE {
TK_OPENTAG = 256, // Make them outside ascii range
TK_IDENTIFIER,
TK_ECHO,
TK_STRING,
TK_LONG,
TK_FUNCTION,
TK_RETURN,
TK_IF,
TK_ELSE,
TK_TRUE,
TK_FALSE,
TK_NULL,
TK_VAR,
TK_CONST,
TK_AND,
TK_OR,
TK_EQ,
TK_LTEQ,
TK_GTEQ,
TK_WHILE,
TK_FOR,
TK_PLUSPLUS,
TK_MINUSMINUS,
TK_SHL,
TK_SHR,
TK_HTML,
TK_END
} TOKENTYPE;
enum MODE {
PHP, NONPHP, EMITOPENTAG
};
typedef uint32_t lineno_t;
typedef struct Token {
TOKENTYPE type;
lineno_t lineno;
} Token;
static inline Token create_token(TOKENTYPE type, lineno_t lineno) {
Token ret = { .type = type, .lineno = lineno };
return ret;
}
typedef enum VALTYPE {
NONE,
MALLOCSTR,
STATICSTR,
LONGVAL,
ERROR
} VALTYPE;
typedef struct Lexer {
enum MODE mode;
lineno_t lineno;
FILE* file;
Token token;
int lexchar;
VALTYPE val;
union {
char* string;
int64_t lint;
} u;
char* error;
} Lexer;
Lexer* create_lexer(FILE *);
void destroy_lexer(Lexer *);
Token get_token(Lexer*);
char* get_token_name(int);
void print_tokenstream(Lexer*);
static inline void state_set_string(Lexer* S, char* str)
{
if (S->val == MALLOCSTR) {
free(S->u.string);
}
S->val = MALLOCSTR;
S->u.string = str;
}
static inline void state_set_long(Lexer* S, int64_t n)
{
if (S->val == MALLOCSTR) {
free(S->u.string);
}
S->val = LONGVAL;
S->u.lint = n;
}
#endif //PHPINTERP_LEX_H