File: relaxng.c

package info (click to toggle)
codelite 17.0.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 136,384 kB
  • sloc: cpp: 491,550; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 805; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (265 lines) | stat: -rw-r--r-- 7,057 bytes parent folder | download | duplicates (6)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
*
*   Copyright (c) 2016, Masatake YAMATO
*   Copyright (c) 2016, Red Hat, K.K.
*
*   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.
*
*   This module contains functions for generating tags for RelaxNG.
*
*   https://www.oasis-open.org/committees/relax-ng/spec-20010811.html
*
*/

#include "general.h"	/* must always come first */
#include "entry.h"
#include "parse.h"
#include "read.h"
#include "xml.h"

typedef enum {
	K_ELEMENT,
	K_ATTRIBUTE,
	K_NAMED_PATTERN,
} relaxngKind;

static kindDefinition RelaxNGKinds [] = {
	{ true,  'e', "element",     "elements"       },
	{ true,  'a', "attribute",   "attributes"     },
	{ true,  'n', "namedPattern", "named patterns" },
};

enum relaxngXpathTable {
	TABLE_MAIN, TABLE_ELEMENT_NAME, TABLE_PATTERN, TABLE_GRAMMAR, TABLE_DEFINE_NAME,
};

static void relaxngMakeAndFindTagsUnderElement (xmlNode *node,
						const char *xpath,
						const struct sTagXpathRecurSpec *spec,
						xmlXPathContext *ctx,
						void *userData);
static void relaxngMakeAndFindTagsUnderDefine (xmlNode *node,
					       const char *xpath,
					       const struct sTagXpathRecurSpec *spec,
					       xmlXPathContext *ctx,
					       void *userData);

static void relaxngFindTags (xmlNode *node,
			     const char *xpath,
			     const struct sTagXpathRecurSpec *spec,
			     xmlXPathContext *ctx,
			     void *userData);


static tagXpathTable relaxngXpathMainTable [] = {
	{ "/*[local-name()='element']",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { relaxngMakeAndFindTagsUnderElement, TABLE_PATTERN } }
	},
	{ "/*[local-name()='grammar']",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { relaxngFindTags, TABLE_GRAMMAR } }
	},
};

static void makeTagWithScope (xmlNode *node,
			      const char *xpath,
			      const struct sTagXpathMakeTagSpec *spec,
			      struct sTagEntryInfo *tag,
			      void *userData);

static tagXpathTable relaxngXpathPatternTable [] = {
	{ "./*[local-name()='element']",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { relaxngMakeAndFindTagsUnderElement, TABLE_PATTERN } }
	},
	{ "./*[local-name()='attribute']/@name",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = { K_ATTRIBUTE, ROLE_DEFINITION_INDEX,
			     makeTagWithScope } }
	},
	{ "./*[not(local-name()='element')][not(local-name()='attribute')]",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { relaxngFindTags, TABLE_PATTERN } }
	},
};


static void makeTagWithUpdatingScope (xmlNode *node,
				      const char *xpath,
				      const struct sTagXpathMakeTagSpec *spec,
				      struct sTagEntryInfo *tag,
				      void *userData);

static tagXpathTable relaxngXpathElementNameTable [] = {
	{ "@name",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = { K_ELEMENT, ROLE_DEFINITION_INDEX,
			     makeTagWithUpdatingScope } }
	},
};

static tagXpathTable relaxngXpathGrammerTable [] = {
	{ "./*[local-name()='start']",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { relaxngFindTags, TABLE_PATTERN } }
	},
	{ "./*[local-name()='define']",
	  LXPATH_TABLE_DO_RECUR,
	  { .recurSpec = { relaxngMakeAndFindTagsUnderDefine, TABLE_PATTERN } }
	}
};

static tagXpathTable relaxngXpathDefineNameTable [] = {
	{ "@name",
	  LXPATH_TABLE_DO_MAKE,
	  { .makeTagSpec = { K_NAMED_PATTERN, ROLE_DEFINITION_INDEX,
			     makeTagWithUpdatingScope } }
	},
};

static tagXpathTableTable relaxngXpathTableTable[] = {
	[TABLE_MAIN]         = { ARRAY_AND_SIZE (relaxngXpathMainTable)        },
	[TABLE_ELEMENT_NAME] = { ARRAY_AND_SIZE (relaxngXpathElementNameTable) },
	[TABLE_PATTERN]      = { ARRAY_AND_SIZE (relaxngXpathPatternTable)     },
	[TABLE_GRAMMAR]      = { ARRAY_AND_SIZE (relaxngXpathGrammerTable)     },
	[TABLE_DEFINE_NAME]  = { ARRAY_AND_SIZE (relaxngXpathDefineNameTable)  },
};


static void
relaxngMakeAndFindTags(xmlNode *node,
		       const char *xpath,
		       const struct sTagXpathRecurSpec *spec,
		       xmlXPathContext *ctx,
		       int nextTable,
		       void *userData)
{
	int corkIndex = *(int *)userData;

	findXMLTags (ctx, node, nextTable, &corkIndex);

	relaxngFindTags (node, xpath, spec, ctx, &corkIndex);
}

static void
relaxngMakeAndFindTagsUnderElement (xmlNode *node,
				    const char *xpath,
				    const struct sTagXpathRecurSpec *spec,
				    xmlXPathContext *ctx,
				    void *userData)
{
	relaxngMakeAndFindTags (node, xpath, spec, ctx, TABLE_ELEMENT_NAME, userData);
}

static void
relaxngMakeAndFindTagsUnderDefine (xmlNode *node,
				   const char *xpath,
				   const struct sTagXpathRecurSpec *spec,
				   xmlXPathContext *ctx,
				   void *userData)
{
	relaxngMakeAndFindTags (node, xpath, spec, ctx, TABLE_DEFINE_NAME, userData);
}

static void
relaxngFindTags (xmlNode *node,
		 const char *xpath CTAGS_ATTR_UNUSED,
		 const struct sTagXpathRecurSpec *spec,
		 xmlXPathContext *ctx,
		 void *userData)
{
	int corkIndex = *(int *)userData;

	findXMLTags (ctx, node, spec->nextTable, &corkIndex);
}

static void
setScope (struct sTagEntryInfo *tag, int index)
{
	tag->extensionFields.scopeKindIndex = KIND_GHOST_INDEX;
	tag->extensionFields.scopeName  = NULL;
	tag->extensionFields.scopeIndex = index;

}

static void
makeTagWithUpdatingScope (xmlNode *node CTAGS_ATTR_UNUSED,
			  const char *xpath CTAGS_ATTR_UNUSED,
			  const struct sTagXpathMakeTagSpec *spec CTAGS_ATTR_UNUSED,
			  struct sTagEntryInfo *tag,
			  void *userData)
{
	int *corkIndex = userData;


#if 0
	if (*corkIndex == CORK_NIL)
		/* TODO: mark tag as an entry point */
		;
#endif

	setScope (tag, *corkIndex);

	*corkIndex = makeTagEntry (tag);
}


static void
findRelaxNGTags (void)
{
	scheduleRunningBaseparser (RUN_DEFAULT_SUBPARSERS);
}

static void
makeTagWithScope (xmlNode *node CTAGS_ATTR_UNUSED,
		  const char *xpath CTAGS_ATTR_UNUSED,
		  const struct sTagXpathMakeTagSpec *spec CTAGS_ATTR_UNUSED,
		  struct sTagEntryInfo *tag,
		  void *userData)
{
	setScope (tag, *(int *)userData);

	makeTagEntry (tag);
}

static void
runXPathEngine(xmlSubparser *s,
			   xmlXPathContext *ctx, xmlNode *root)
{
	int corkIndex = CORK_NIL;

	findXMLTags (ctx, root, TABLE_MAIN, &corkIndex);
}

static xmlSubparser relaxngSubparser = {
	.subparser = {
		.direction = SUBPARSER_BI_DIRECTION,
	},
	.runXPathEngine = runXPathEngine,
};

extern parserDefinition*
RelaxNGParser (void)
{
	static const char *const extensions [] = { "rng", NULL };
	parserDefinition* const def = parserNew ("RelaxNG");
	/* static selectLanguage selectors[] = { selectByDTD, NULL }; */
	static parserDependency dependencies [] = {
		[0] = { DEPTYPE_SUBPARSER, "XML", &relaxngSubparser },
	};

	def->kindTable         = RelaxNGKinds;
	def->kindCount     = ARRAY_SIZE (RelaxNGKinds);
	def->extensions    = extensions;
	def->parser        = findRelaxNGTags;
	def->tagXpathTableTable = relaxngXpathTableTable;
	def->tagXpathTableCount = ARRAY_SIZE (relaxngXpathTableTable);
	def->useCork = CORK_QUEUE;
	/* def->selectLanguage = selectors; */
	def->dependencies = dependencies;
	def->dependencyCount = ARRAY_SIZE (dependencies);

	return def;
}