forked from universal-ctags/ctags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.c
166 lines (142 loc) · 4.5 KB
/
robot.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) 2017, Daniel Riechers
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* Parser from Robot Framework http://robotframework.org
*/
#include "general.h"
#include <string.h>
#include "entry.h"
#include "parse.h"
#include "vstring.h"
#include "routines.h"
typedef enum {
K_TESTCASE,
K_KEYWORD,
K_VARIABLE,
COUNT_KIND
} RobotKind;
static RobotKind section = -1;
static kindDefinition RobotKinds[COUNT_KIND] = {
{true, 't', "testcase", "testcases"},
{true, 'k', "keyword", "keywords"},
{true, 'v', "variable", "variables"},
};
typedef enum {
X_WHITESPACE_SWAPPED,
} robotXtag;
static xtagDefinition RobotXtags [] = {
{
.enabled = true,
.name = "whitespaceSwapped",
.description = "Include tags swapping whitespace and underscore chars",
},
};
static void findRobotTags (void)
{
findRegexTags ();
}
static bool whitespaceSwap (vString *const s)
{
char replaceWith = '_';
char toReplace = ' ';
char changed = false;
if(strchr(vStringValue (s), '_'))
{
replaceWith = ' ';
toReplace = '_';
}
for(unsigned int i=0; i < vStringLength(s); i++)
if(vStringChar(s, i) == toReplace)
{
vStringChar (s, i) = replaceWith;
changed = true;
}
return changed;
}
static bool changeSection (const char *const line, const regexMatch *const matches,
const unsigned int count CTAGS_ATTR_UNUSED, void *data CTAGS_ATTR_UNUSED)
{
const char * const matchedSection = line + matches[1].start;
if(strncasecmp(matchedSection, "test cases", matches[1].length) == 0)
{
section = K_TESTCASE;
}
else if(strncasecmp(matchedSection, "keywords", matches[1].length) == 0)
{
section = K_KEYWORD;
}
else if(strncasecmp(matchedSection, "variables", matches[1].length) == 0)
{
section = K_VARIABLE;
}
return true;
}
static void makeSimpleXTag (const vString* const name, const int kind,
unsigned int xtagType)
{
tagEntryInfo e;
initTagEntry (&e, vStringValue(name), kind);
markTagExtraBit (&e, xtagType);
makeTagEntry (&e);
}
static bool tagKeywordsAndTestCases (const char *const line, const regexMatch *const matches,
const unsigned int count, void *data CTAGS_ATTR_UNUSED)
{
if (count > 1 && ( section == K_KEYWORD || section == K_TESTCASE) )
{
vString *const name = vStringNew ();
vStringNCopyS (name, line + matches [1].start, matches [1].length);
makeSimpleTag (name, section);
if (isXtagEnabled (RobotXtags[X_WHITESPACE_SWAPPED].xtype)
&& whitespaceSwap(name))
makeSimpleXTag (name, section,
RobotXtags[X_WHITESPACE_SWAPPED].xtype);
vStringDelete (name);
return true;
}
return false;
}
static bool tagVariables (const char *const line, const regexMatch *const matches,
const unsigned int count, void *data CTAGS_ATTR_UNUSED)
{
if (count > 1 && section == K_VARIABLE)
{
vString *const name = vStringNew ();
vStringNCopyS (name, line + matches [1].start, matches [1].length);
makeSimpleTag (name, K_VARIABLE);
if (isXtagEnabled (RobotXtags[X_WHITESPACE_SWAPPED].xtype)
&& whitespaceSwap(name))
makeSimpleXTag (name, K_VARIABLE,
RobotXtags[X_WHITESPACE_SWAPPED].xtype);
vStringDelete (name);
return true;
}
return false;
}
static void initialize (const langType language)
{
addLanguageCallbackRegex (language, "^\\*+ *([^* ].+[^* ]) *\\*+$",
"{exclusive}", changeSection, NULL, NULL);
addLanguageCallbackRegex (
language,
"(^([A-Za-z0-9]+|\\$\\{[_A-Za-z0-9][' _A-Za-z0-9]*(:([^}]|\\\\)+)*\\})([${}' _]([-_$A-Za-z0-9]+|\\{[_A-Za-z0-9][' _A-Za-z0-9]*(:([^}]|\\\\)+)*\\})+)*)",
"{exclusive}", tagKeywordsAndTestCases, NULL, NULL);
addLanguageCallbackRegex (language, "^[$@]\\{([_A-Za-z0-9][' _A-Za-z0-9]+)\\} [ ]*.+",
"{exclusive}", tagVariables, NULL, NULL);
}
extern parserDefinition* RobotParser (void)
{
static const char *const extensions[] = { "robot", NULL };
parserDefinition *def = parserNew ("Robot");
def->kindTable = RobotKinds;
def->kindCount = COUNT_KIND;
def->extensions = extensions;
def->initialize = initialize;
def->parser = findRobotTags;
def->xtagTable = RobotXtags;
def->xtagCount = ARRAY_SIZE (RobotXtags);
return def;
}