-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgnu_source_highlight_syntax.c
89 lines (70 loc) · 2.88 KB
/
gnu_source_highlight_syntax.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
/*
* 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 "gnu_source_highlight_syntax.h"
#include "session.h"
#include "config.h"
/* The following three function declarations are the exposed C interface
* from source_highlight.cc. As these functions only need to be called
* from this file they are declared here rather than expose them publicly
* in a header file */
Status sh_init(SHSyntaxDefinition *, const char *lang_dir, const char *lang);
SyntaxMatches *sh_tokenize(const SHSyntaxDefinition *, const char *str,
size_t str_len);
void sh_free_tokenizer(SHSyntaxDefinition *);
static Status sh_load(SyntaxDefinition *, const char *syntax_type);
static SyntaxMatches *sh_generate_matches(const SyntaxDefinition *,
const char *str, size_t str_len,
size_t offset);
static void sh_free(SyntaxDefinition *);
SyntaxDefinition *sh_new(Session *sess)
{
SHSyntaxDefinition *sh_def = malloc(sizeof(SHSyntaxDefinition));
RETURN_IF_NULL(sh_def);
sh_def->tokenizer = NULL;
sh_def->sess = sess;
sh_def->syn_def.load = sh_load;
sh_def->syn_def.generate_matches = sh_generate_matches;
sh_def->syn_def.free = sh_free;
return (SyntaxDefinition *)sh_def;
}
static Status sh_load(SyntaxDefinition *syn_def, const char *syntax_type)
{
SHSyntaxDefinition *sh_def = (SHSyntaxDefinition *)syn_def;
const char *shdd = cf_string(sh_def->sess->config, CV_SHDATADIR);
return sh_init(sh_def, shdd, syntax_type);
}
static SyntaxMatches *sh_generate_matches(const SyntaxDefinition *syn_def,
const char *str, size_t str_len,
size_t offset)
{
SHSyntaxDefinition *sh_def = (SHSyntaxDefinition *)syn_def;
SyntaxMatches *syn_matches = sh_tokenize(sh_def, str, str_len);
if (syn_matches != NULL) {
syn_matches->offset = offset;
}
return syn_matches;
}
static void sh_free(SyntaxDefinition *syn_def)
{
if (syn_def == NULL) {
return;
}
SHSyntaxDefinition *sh_def = (SHSyntaxDefinition *)syn_def;
sh_free_tokenizer(sh_def);
free(sh_def);
}