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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/*
* Copyright (c) 2016, Masatake YAMATO
* Copyright (c) 2016, Red Hat, Inc.
* 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.
*
* This module contains functions for applying yaml traversing.
*/
#include "general.h" /* must always come first */
#include <inttypes.h>
#include "debug.h"
#include "entry.h"
#include "htable.h"
#include "options.h"
#include "parse.h"
#include "read.h"
#include "subparser.h"
#include "trace.h"
#include "types.h"
#include "yaml.h"
#include "numarray.h"
#include "keyword.h"
#include "trashbox.h"
#define entry(X) [X] = #X
static const char *tokenTypeName [] = {
entry (YAML_NO_TOKEN),
entry (YAML_STREAM_START_TOKEN),
entry (YAML_STREAM_END_TOKEN),
entry (YAML_VERSION_DIRECTIVE_TOKEN),
entry (YAML_TAG_DIRECTIVE_TOKEN),
entry (YAML_DOCUMENT_START_TOKEN),
entry (YAML_DOCUMENT_END_TOKEN),
entry (YAML_BLOCK_SEQUENCE_START_TOKEN),
entry (YAML_BLOCK_MAPPING_START_TOKEN),
entry (YAML_BLOCK_END_TOKEN),
entry (YAML_FLOW_SEQUENCE_START_TOKEN),
entry (YAML_FLOW_SEQUENCE_END_TOKEN),
entry (YAML_FLOW_MAPPING_START_TOKEN),
entry (YAML_FLOW_MAPPING_END_TOKEN),
entry (YAML_BLOCK_ENTRY_TOKEN),
entry (YAML_FLOW_ENTRY_TOKEN),
entry (YAML_KEY_TOKEN),
entry (YAML_VALUE_TOKEN),
entry (YAML_ALIAS_TOKEN),
entry (YAML_ANCHOR_TOKEN),
entry (YAML_TAG_TOKEN),
entry (YAML_SCALAR_TOKEN),
};
static void yamlInit (yaml_parser_t *yaml)
{
const unsigned char* data;
size_t size;
yaml_parser_initialize (yaml);
data = getInputFileData (&size);
Assert (data);
yaml_parser_set_input_string (yaml, data, size);
}
static void yamlFini (yaml_parser_t *yaml)
{
yaml_parser_delete (yaml);
}
extern void attachYamlPosition (tagEntryInfo *tag, yaml_token_t *token, bool asEndPosition)
{
unsigned int ln = token->start_mark.line + 1;
if (asEndPosition)
tag->extensionFields.endLine = ln;
else
{
tag->lineNumber = ln;
tag->filePosition = getInputFilePositionForLine (tag->lineNumber);
}
}
enum YamlKind {
K_ANCHOR,
};
enum YamlAnchorRole {
R_ANCHOR_ALIAS,
};
static roleDefinition YamlAnchorRoles [] = {
{ true, "alias", "alias" },
};
static kindDefinition YamlKinds [] = {
{ true, 'a', "anchor", "anchors",
.referenceOnly = false, ATTACH_ROLES(YamlAnchorRoles) },
};
static void handlYamlToken (yaml_token_t *token)
{
tagEntryInfo tag;
if (token->type == YAML_ANCHOR_TOKEN)
{
initTagEntry (&tag, (char *)token->data.anchor.value,
K_ANCHOR);
attachYamlPosition (&tag, token, false);
makeTagEntry (&tag);
}
else if (token->type == YAML_ALIAS_TOKEN)
{
initRefTagEntry (&tag, (char *)token->data.alias.value,
K_ANCHOR, R_ANCHOR_ALIAS);
attachYamlPosition (&tag, token, false);
makeTagEntry (&tag);
}
}
static void findYamlTags (void)
{
subparser *sub;
yaml_parser_t yaml;
yaml_token_t token;
bool done;
yamlInit (&yaml);
findRegexTags ();
foreachSubparser(sub, false)
{
enterSubparser (sub);
((yamlSubparser*)sub)->ypathTypeStack = NULL;
leaveSubparser ();
}
sub = getSubparserRunningBaseparser();
if (sub)
chooseExclusiveSubparser (sub, NULL);
done = false;
while (!done)
{
if (!yaml_parser_scan (&yaml, &token))
break;
handlYamlToken (&token);
foreachSubparser(sub, false)
{
enterSubparser (sub);
((yamlSubparser *)sub)->newTokenNotfify ((yamlSubparser *)sub, &token);
leaveSubparser ();
}
TRACE_PRINT("yaml token:%s<%d>@Line:%"PRIuPTR"", tokenTypeName[token.type], token.type,
token.start_mark.line + 1);
if (isTraced() && token.type == YAML_SCALAR_TOKEN)
{
TRACE_PRINT_FMT(" ");
for (size_t i = 0; i < token.data.scalar.length; i++)
TRACE_PRINT_FMT("%c", token.data.scalar.value[i]);
TRACE_PRINT_NEWLINE();
}
if (token.type == YAML_STREAM_END_TOKEN)
done = true;
yaml_token_delete (&token);
}
foreachSubparser(sub, false)
{
enterSubparser (sub);
ypathPopAllTypes ((yamlSubparser*)sub);
leaveSubparser ();
}
yamlFini (&yaml);
}
extern parserDefinition* YamlParser (void)
{
static const char *const extensions [] = { "yml", "yaml", NULL };
parserDefinition* const def = parserNew ("Yaml");
def->kindTable = YamlKinds;
def->extensions = extensions;
def->parser = findYamlTags;
def->useCork = CORK_QUEUE;
def->kindTable = YamlKinds;
def->kindCount = ARRAY_SIZE (YamlKinds);
def->useMemoryStreamInput = true;
return def;
}
/*
* Experimental Ypath code
*/
struct ypathTypeStack {
yaml_token_type_t type;
int key;
struct ypathTypeStack *next;
};
extern int ypathCompileTable (langType language, tagYpathTable *table, int keywordId)
{
vString *tmpkey = vStringNew();
intArray *code = intArrayNew ();
for (const char *ypath = table->ypath; true; ypath++)
{
if (*ypath == '/' || *ypath == '\0')
{
if (!vStringIsEmpty (tmpkey))
{
int k;
if (vStringLength (tmpkey) == 1 && (vStringValue (tmpkey)[0] == '*'))
k = KEYWORD_NONE;
else
{
k = lookupKeyword (vStringValue (tmpkey), language);
if (k == KEYWORD_NONE)
{
char *keyword = DEFAULT_TRASH_BOX(vStringStrdup (tmpkey), eFree);
k = keywordId++;
addKeyword (keyword, language, k);
}
}
intArrayAdd (code, k);
vStringClear (tmpkey);
}
if (*ypath == '\0')
break;
}
else
vStringPut (tmpkey, *ypath);
}
intArrayReverse (code);
table->code = code;
vStringDelete (tmpkey);
return keywordId;
}
extern void ypathCompileTables (langType language, tagYpathTable tables[], size_t count, int keywordId)
{
for (size_t i = 0; i < count; i++)
keywordId = ypathCompileTable (language, tables + i, keywordId);
}
extern void ypathCompiledCodeDelete (tagYpathTable tables[], size_t count)
{
for (size_t i = 0; i < count; i++)
{
intArrayDelete (tables[i].code);
tables[i].code = NULL;
}
}
extern void ypathPushType (yamlSubparser *yaml, yaml_token_t *token)
{
struct ypathTypeStack *s;
s = xMalloc (1, struct ypathTypeStack);
s->next = yaml->ypathTypeStack;
yaml->ypathTypeStack = s;
s->type = token->type;
s->key = KEYWORD_NONE;
}
extern void ypathPopType (yamlSubparser *yaml)
{
struct ypathTypeStack *s;
s = yaml->ypathTypeStack;
yaml->ypathTypeStack = s->next;
s->next = NULL;
eFree (s);
}
extern void ypathPopAllTypes (yamlSubparser *yaml)
{
while (yaml->ypathTypeStack)
ypathPopType (yaml);
}
extern void ypathFillKeywordOfTokenMaybe (yamlSubparser *yaml, yaml_token_t *token, langType lang)
{
if (!yaml->ypathTypeStack)
return;
int k = lookupKeyword ((char *)token->data.scalar.value, lang);
yaml->ypathTypeStack->key = k;
}
static bool ypathStateStackMatch (struct ypathTypeStack *stack,
intArray *code, size_t offset)
{
if (intArrayCount (code) - offset == 0)
{
if (stack == NULL)
return true;
else
return false;
}
if (stack == NULL)
return false;
if (stack->key == intArrayItem (code, offset))
return ypathStateStackMatch (stack->next, code, offset + 1);
else
return false;
}
extern void ypathHandleToken (yamlSubparser *yaml, yaml_token_t *token, int state,
tagYpathTable tables[], size_t count)
{
if (!yaml->ypathTypeStack)
return;
for (size_t i = 0; i < count; i++)
{
if (tables[i].expected_state != state)
continue;
if (ypathStateStackMatch(yaml->ypathTypeStack, tables[i].code, 0))
{
tagEntryInfo tag;
bool r = true;
if (tables[i].initTagEntry)
r = (* tables[i].initTagEntry) (&tag, (char *)token->data.scalar.value,
tables[i].data);
else
initTagEntry (&tag, (char *)token->data.scalar.value, tables[i].kind);
if (r)
{
attachYamlPosition (&tag, token, false);
makeTagEntry (&tag);
}
break;
}
}
}
#ifdef DO_TRACING
extern void ypathPrintTypeStack0(struct ypathTypeStack *stack)
{
if (!stack)
{
TRACE_PRINT_NEWLINE();
return;
}
tracePrintFmt("[%d] - ", stack->key);
ypathPrintTypeStack0 (stack->next);
}
extern void ypathPrintTypeStack(yamlSubparser *yaml)
{
ypathPrintTypeStack0 (yaml->ypathTypeStack);
}
#else
extern void ypathPrintTypeStack(yamlSubparser *yaml) {}
#endif
|