-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyntax_manager.c
148 lines (121 loc) · 3.79 KB
/
syntax_manager.c
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
143
144
145
146
147
148
/*
* Copyright (C) 2016 Richard Burke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include "syntax_manager.h"
#include "session.h"
#include "util.h"
#include "wed_syntax.h"
#if WED_FEATURE_GNU_SOURCE_HIGHLIGHT
#include "gnu_source_highlight_syntax.h"
#endif
#if WED_FEATURE_LUA
#include "scintillua_syntax.h"
#endif
typedef SyntaxDefinition *(*SyntaxDefinitionCreator)(Session *sess);
/* Map each syntax definition type to a syntax definition creator function.
* This allows us to create instances of different syntax definition types in
* a generic way */
static const SyntaxDefinitionCreator sm_syntax_def_types[] = {
[SDT_WED] = ws_new
#if WED_FEATURE_GNU_SOURCE_HIGHLIGHT
,[SDT_GNU_SOURCE_HIGHLIGHT] = sh_new
#endif
#if WED_FEATURE_LUA
,[SDT_SCINTILLUA] = sl_new
#endif
};
static void sm_free_syn_def(SyntaxDefinition *);
int sm_init(SyntaxManager *sm)
{
memset(sm, 0, sizeof(SyntaxManager));
sm->syn_defs = new_hashmap();
if (sm->syn_defs == NULL) {
return 0;
}
return 1;
}
void sm_free(SyntaxManager *sm)
{
free_hashmap_values(sm->syn_defs, (void (*)(void *))sm_free_syn_def);
free_hashmap(sm->syn_defs);
}
static void sm_free_syn_def(SyntaxDefinition *syn_def)
{
if (syn_def == NULL) {
return;
}
syn_def->free(syn_def);
}
Status sm_load_definition(SyntaxManager *sm, Session *sess,
SyntaxDefinitionType type,
const char *syntax_type)
{
assert(!is_null_or_empty(syntax_type));
if (sm_has_def(sm, syntax_type)) {
return STATUS_SUCCESS;
}
SyntaxDefinitionCreator creator = sm_syntax_def_types[type];
SyntaxDefinition *syn_def = creator(sess);
if (syn_def == NULL) {
return OUT_OF_MEMORY("Unable to create syntax definition");
}
Status status = syn_def->load(syn_def, syntax_type);
if (!STATUS_IS_SUCCESS(status)) {
sm_free_syn_def(syn_def);
return status;
}
if (!hashmap_set(sm->syn_defs, syntax_type, syn_def)) {
return OUT_OF_MEMORY("Unable to save syntax definition");
}
return STATUS_SUCCESS;
}
int sm_get_syntax_definition_type(const char *syn_def_type,
SyntaxDefinitionType *type_ptr)
{
static const char *syn_def_types[] = {
[SDT_WED] = "wed"
#if WED_FEATURE_GNU_SOURCE_HIGHLIGHT
,[SDT_GNU_SOURCE_HIGHLIGHT] = "sh"
#endif
#if WED_FEATURE_LUA
,[SDT_SCINTILLUA] = "sl"
#endif
};
static const size_t syn_def_num = ARRAY_SIZE(syn_def_types, const char *);
if (is_null_or_empty(syn_def_type)) {
return 0;
}
for (size_t k = 0; k < syn_def_num; k++) {
if (strcmp(syn_def_type, syn_def_types[k]) == 0) {
if (type_ptr != NULL) {
*type_ptr = k;
}
return 1;
}
}
return 0;
}
const SyntaxDefinition *sm_get_def(const SyntaxManager *sm,
const char *syntax_type)
{
return hashmap_get(sm->syn_defs, syntax_type);
}
int sm_has_def(const SyntaxManager *sm, const char *syntax_type)
{
return sm_get_def(sm, syntax_type) != NULL;
}