File: openapi.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 (196 lines) | stat: -rw-r--r-- 4,721 bytes parent folder | download | duplicates (3)
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
/*
*
*   Copyright (c) 2016, Masatake YAMATO
*   Copyright (c) 2016, Red Hat, K.K.
*   Copyright (c) 2022, Vasily Kulikov
*
*   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.
*
*   Documentation on the schemas:
*   https://github.com/OAI/OpenAPI-Specification/tree/main/versions
*/

#include "general.h"	/* must always come first */
#include "debug.h"
#include "entry.h"
#include "kind.h"
#include "yaml.h"
#include "parse.h"
#include "subparser.h"
#include "keyword.h"
#include "read.h"
#include "trace.h"


typedef enum {
	KIND_SCHEMA,
	KIND_PATH,
	KIND_RESPONSE,
	KIND_PARAMETER,
	KIND_TITLE,
	KIND_SERVER,
	KIND_TAG,
} openapiKind;

static kindDefinition OpenAPIKinds [] = {
	{ true, 'd', "schema", "schemas" },
	{ true, 'p', "path", "paths" },
	{ true, 'R', "response", "responses" },
	{ true, 'P', "parameter", "parameters" },
	{ true, 't', "title", "titles" },
	{ true, 's', "server", "servers (or hosts in swagger)" },
	{ true, 'T', "tag", "tags"},
};

/* - name: "THE NAME" */
enum openapiDetectingState {
	DSTAT_LAST_KEY,
	DSTAT_LAST_VALUE,
	DSTAT_INITIAL,
};


struct sOpenAPISubparser {
	yamlSubparser yaml;
	enum openapiDetectingState detection_state;
};

static tagYpathTable ypathTables [] = {
	{ "paths/*",
	  DSTAT_LAST_KEY,   KIND_PATH,      },
	{ "components/responses/*",
	  DSTAT_LAST_KEY,   KIND_RESPONSE,  },
	{ "responses/*",
	  DSTAT_LAST_KEY,   KIND_RESPONSE,  },
	{ "components/parameters/*",
	  DSTAT_LAST_KEY,   KIND_PARAMETER, },
	{ "parameters/*",
	  DSTAT_LAST_KEY,   KIND_PARAMETER, },
	{ "components/schemas/*",
	  DSTAT_LAST_KEY,   KIND_SCHEMA,    },
	{ "definitions/*",
	  DSTAT_LAST_KEY,   KIND_SCHEMA,    },
	{ "info/title",
	  DSTAT_LAST_VALUE, KIND_TITLE,     },
	{ "servers/*/url",
	  DSTAT_LAST_VALUE, KIND_SERVER,    },
	{ "host",
	  DSTAT_LAST_VALUE, KIND_SERVER,    },
	{ "tags/*/name",
	  DSTAT_LAST_VALUE, KIND_TAG,       },
};

static void	openapiStateMachine (struct sOpenAPISubparser *openapi,
								 yaml_token_t *token)
{
#ifdef DO_TRACING
	ypathPrintTypeStack (YAML(openapi));
#endif

	switch (token->type)
	{
	case YAML_KEY_TOKEN:
		openapi->detection_state = DSTAT_LAST_KEY;
		break;
	case YAML_SCALAR_TOKEN:
		switch (openapi->detection_state)
		{
		case DSTAT_LAST_KEY:
			ypathFillKeywordOfTokenMaybe (YAML(openapi), token, getInputLanguage ());
			/* FALL THROUGH */
		case DSTAT_LAST_VALUE:
			TRACE_PRINT("token-callback: %s: %s",
						(openapi->detection_state == DSTAT_LAST_KEY)? "key": "value",
						(char*)token->data.scalar.value);
			ypathHandleToken (YAML(openapi), token, openapi->detection_state,
							  ypathTables, ARRAY_SIZE (ypathTables));
			break;
		default:
			break;
		}

		openapi->detection_state = DSTAT_INITIAL;

		break;
	case YAML_VALUE_TOKEN:
		openapi->detection_state = DSTAT_LAST_VALUE;
		break;

	default:
		openapi->detection_state = DSTAT_INITIAL;
		break;
	}
}

static void newTokenCallback (yamlSubparser *s, yaml_token_t *token)
{
	if (token->type == YAML_BLOCK_SEQUENCE_START_TOKEN
		|| token->type == YAML_BLOCK_MAPPING_START_TOKEN)
		ypathPushType (s, token);

	openapiStateMachine ((struct sOpenAPISubparser *)s, token);

	if (token->type == YAML_BLOCK_END_TOKEN)
		ypathPopType (s);
	else if (token->type == YAML_STREAM_END_TOKEN)
		ypathPopAllTypes (s);
}

static void inputStart(subparser *s)
{
	((struct sOpenAPISubparser*)s)->detection_state = DSTAT_INITIAL;
}

static void inputEnd(subparser *s)
{
	Assert (((yamlSubparser*)s)->ypathTypeStack == NULL);
}

static void findOpenAPITags (void)
{
	scheduleRunningBaseparser (0);
}

static void initialize (langType language)
{
	ypathCompileTables (language, ypathTables, ARRAY_SIZE (ypathTables), 0);
}

static void finalize (langType language, bool initialized)
{
	if (initialized)
		ypathCompiledCodeDelete (ypathTables, ARRAY_SIZE (ypathTables));
}

extern parserDefinition* OpenAPIParser (void)
{
	static const char *const patterns [] = { "openapi.yaml", NULL };
	static struct sOpenAPISubparser openapiSubparser = {
		.yaml = {
			.subparser = {
				.direction = SUBPARSER_BI_DIRECTION,
				.inputStart = inputStart,
				.inputEnd = inputEnd,
			},
			.newTokenNotfify = newTokenCallback
		},
	};
	static parserDependency dependencies [] = {
		{ DEPTYPE_SUBPARSER, "Yaml", &openapiSubparser },
	};

	parserDefinition* const def = parserNew ("OpenAPI");

	def->patterns   = patterns;

	def->dependencies = dependencies;
	def->dependencyCount = ARRAY_SIZE (dependencies);

	def->kindTable	= OpenAPIKinds;
	def->kindCount = ARRAY_SIZE (OpenAPIKinds);
	def->parser	= findOpenAPITags;
	def->initialize = initialize;
	def->finalize = finalize;
	return def;
}