forked from marcoroth/herb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer_peek_helpers.c
More file actions
140 lines (110 loc) · 5.14 KB
/
lexer_peek_helpers.c
File metadata and controls
140 lines (110 loc) · 5.14 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
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
#include "include/lexer_peek_helpers.h"
#include "include/lexer.h"
#include "include/lexer_struct.h"
#include "include/macros.h"
#include "include/token.h"
#include "include/util/hb_string.h"
#include <ctype.h>
#include <stdbool.h>
char lexer_backtrack(const lexer_T* lexer, uint32_t offset) {
return lexer->source.data[MAX(lexer->current_position - offset, 0)];
}
char lexer_peek(const lexer_T* lexer, uint32_t offset) {
return lexer->source.data[MIN(lexer->current_position + offset, lexer->source.length)];
}
bool lexer_peek_for(const lexer_T* lexer, uint32_t offset, hb_string_T pattern, const bool case_insensitive) {
hb_string_T remaining_source = hb_string_slice(lexer->source, lexer->current_position + offset);
remaining_source.length = MIN(pattern.length, remaining_source.length);
if (case_insensitive) {
return hb_string_equals_case_insensitive(remaining_source, pattern);
} else {
return hb_string_equals(remaining_source, pattern);
}
}
bool lexer_peek_for_doctype(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("<!DOCTYPE"), true);
}
bool lexer_peek_for_xml_declaration(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("<?xml"), true);
}
bool lexer_peek_for_cdata_start(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("<![CDATA["), false);
}
bool lexer_peek_for_cdata_end(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("]]>"), false);
}
bool lexer_peek_for_html_comment_start(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("<!--"), false);
}
bool lexer_peek_for_html_comment_end(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("-->"), false);
}
bool lexer_peek_erb_close_tag(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("%>"), false);
}
bool lexer_peek_erb_dash_close_tag(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("-%>"), false);
}
bool lexer_peek_erb_percent_close_tag(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("%%>"), false);
}
bool lexer_peek_erb_equals_close_tag(const lexer_T* lexer, uint32_t offset) {
return lexer_peek_for(lexer, offset, hb_string_from_c_string("=%>"), false);
}
bool lexer_peek_erb_end(const lexer_T* lexer, uint32_t offset) {
return (
lexer_peek_erb_close_tag(lexer, offset) || lexer_peek_erb_dash_close_tag(lexer, offset)
|| lexer_peek_erb_percent_close_tag(lexer, offset) || lexer_peek_erb_equals_close_tag(lexer, offset)
);
}
bool lexer_peek_for_token_type_after_whitespace(lexer_T* lexer, token_type_T token_type) {
uint32_t saved_position = lexer->current_position;
uint32_t saved_line = lexer->current_line;
uint32_t saved_column = lexer->current_column;
char saved_character = lexer->current_character;
lexer_state_T saved_state = lexer->state;
token_T* token = lexer_next_token(lexer);
while (token && (token->type == TOKEN_WHITESPACE || token->type == TOKEN_NEWLINE)) {
token_free(token);
token = lexer_next_token(lexer);
}
bool result = (token && token->type == token_type);
if (token) { token_free(token); }
lexer->current_position = saved_position;
lexer->current_line = saved_line;
lexer->current_column = saved_column;
lexer->current_character = saved_character;
lexer->state = saved_state;
return result;
}
bool lexer_peek_for_close_tag_start(const lexer_T* lexer, uint32_t offset) {
if (lexer_peek(lexer, offset) != '<' || lexer_peek(lexer, offset + 1) != '/') { return false; }
int pos = offset + 2;
while (lexer_peek(lexer, pos) == ' ' || lexer_peek(lexer, pos) == '\t' || lexer_peek(lexer, pos) == '\n'
|| lexer_peek(lexer, pos) == '\r') {
pos++;
}
char c = lexer_peek(lexer, pos);
return isalpha(c) || c == '_';
}
lexer_state_snapshot_T lexer_save_state(lexer_T* lexer) {
lexer_state_snapshot_T snapshot = { .position = lexer->current_position,
.line = lexer->current_line,
.column = lexer->current_column,
.previous_position = lexer->previous_position,
.previous_line = lexer->previous_line,
.previous_column = lexer->previous_column,
.current_character = lexer->current_character,
.state = lexer->state };
return snapshot;
}
void lexer_restore_state(lexer_T* lexer, lexer_state_snapshot_T snapshot) {
lexer->current_position = snapshot.position;
lexer->current_line = snapshot.line;
lexer->current_column = snapshot.column;
lexer->previous_position = snapshot.previous_position;
lexer->previous_line = snapshot.previous_line;
lexer->previous_column = snapshot.previous_column;
lexer->current_character = snapshot.current_character;
lexer->state = snapshot.state;
}