|
| 1 | +/* |
| 2 | +* Copyright (c) 2017, Masatake YAMATO <yamato@redhat.com> |
| 3 | +* |
| 4 | +* This source code is released for free distribution under the terms of the |
| 5 | +* GNU General Public License version 2 or (at your option) any later version. |
| 6 | +* |
| 7 | +* This module contains functions for generating tags for VUE file. |
| 8 | +*/ |
| 9 | + |
| 10 | +/* |
| 11 | +* INCLUDE FILES |
| 12 | +*/ |
| 13 | +#include "general.h" /* must always come first */ |
| 14 | + |
| 15 | +#include <string.h> |
| 16 | +#include "parse.h" |
| 17 | +#include "routines.h" |
| 18 | +#include "read.h" |
| 19 | +#include "mio.h" |
| 20 | +#include "promise.h" |
| 21 | + |
| 22 | +enum vueAreaType { |
| 23 | + vueAreaUnset, |
| 24 | + vueTemplateArea, |
| 25 | + vueScriptArea, |
| 26 | + vueStyleArea, |
| 27 | + COUNT_AREA, |
| 28 | +}; |
| 29 | + |
| 30 | +const char *vueDefaultGuestParser[] = { |
| 31 | + [vueTemplateArea] = "HTML", |
| 32 | + [vueScriptArea] = "JavaScript", |
| 33 | + [vueStyleArea] = "CSS", |
| 34 | +}; |
| 35 | + |
| 36 | +struct vueParserState { |
| 37 | + enum vueAreaType areaType; |
| 38 | + unsigned long inputLine; |
| 39 | + unsigned long inputOffset; |
| 40 | +}; |
| 41 | + |
| 42 | +struct vueCallbackData { |
| 43 | + const enum vueAreaType matchedAreaType; |
| 44 | + struct vueParserState *state; |
| 45 | +}; |
| 46 | + |
| 47 | +static bool enterArea (const char *line, |
| 48 | + const regexMatch *matches, |
| 49 | + unsigned int count CTAGS_ATTR_UNUSED, |
| 50 | + void *data) |
| 51 | +{ |
| 52 | + struct vueCallbackData *cdata = data; |
| 53 | + struct vueParserState *s = cdata->state; |
| 54 | + |
| 55 | + if (s->areaType != vueAreaUnset) |
| 56 | + return true; |
| 57 | + |
| 58 | + s->areaType = cdata->matchedAreaType; |
| 59 | + s->inputLine = getInputLineNumber (); |
| 60 | + |
| 61 | + if (matches[0].length < strlen(line)) |
| 62 | + s->inputOffset = matches[0].length + 1; |
| 63 | + else |
| 64 | + { |
| 65 | + s->inputLine++; |
| 66 | + s->inputOffset = 0; |
| 67 | + } |
| 68 | + |
| 69 | + return true; |
| 70 | +} |
| 71 | + |
| 72 | +static bool leaveArea (const char *line CTAGS_ATTR_UNUSED, |
| 73 | + const regexMatch *matches CTAGS_ATTR_UNUSED, |
| 74 | + unsigned int count CTAGS_ATTR_UNUSED, |
| 75 | + void *data) |
| 76 | +{ |
| 77 | + struct vueCallbackData *cdata = data; |
| 78 | + struct vueParserState *s = cdata->state; |
| 79 | + |
| 80 | + unsigned long endLine; |
| 81 | + |
| 82 | + if (s->areaType != cdata->matchedAreaType) |
| 83 | + return true; |
| 84 | + |
| 85 | + if (s->inputOffset == 0) |
| 86 | + return true; |
| 87 | + |
| 88 | + endLine = getInputLineNumber (); |
| 89 | + if (endLine == 0) |
| 90 | + goto out; |
| 91 | + |
| 92 | + if (s->inputLine >= endLine) |
| 93 | + goto out; |
| 94 | + |
| 95 | + makePromise (vueDefaultGuestParser[s->areaType], |
| 96 | + s->inputLine, s->inputOffset, |
| 97 | + endLine, 0, s->inputLine); |
| 98 | + |
| 99 | + out: |
| 100 | + memset (s, 0, sizeof (*s)); |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +static void initializeVueParser (langType language) |
| 106 | +{ |
| 107 | + static struct vueParserState parserState; |
| 108 | + |
| 109 | + memset (&parserState, 0, sizeof (parserState)); |
| 110 | + |
| 111 | + static struct vueCallbackData callbackData [COUNT_AREA] = { |
| 112 | + [vueTemplateArea] = { |
| 113 | + .matchedAreaType = vueTemplateArea, |
| 114 | + .state = &parserState, |
| 115 | + }, |
| 116 | + [vueScriptArea] = { |
| 117 | + .matchedAreaType = vueScriptArea, |
| 118 | + .state = &parserState, |
| 119 | + }, |
| 120 | + [vueStyleArea] = { |
| 121 | + .matchedAreaType = vueStyleArea, |
| 122 | + .state = &parserState, |
| 123 | + }, |
| 124 | + }; |
| 125 | + addLanguageCallbackRegex (language, "^<template>", "{exclusive}", |
| 126 | + enterArea, NULL, callbackData + vueTemplateArea); |
| 127 | + addLanguageCallbackRegex (language, "^</template>", "{exclusive}", |
| 128 | + leaveArea, NULL, callbackData + vueTemplateArea); |
| 129 | + addLanguageCallbackRegex (language, "^<script>", "{exclusive}", |
| 130 | + enterArea, NULL, callbackData + vueScriptArea); |
| 131 | + addLanguageCallbackRegex (language, "^</script>", "{exclusive}", |
| 132 | + leaveArea, NULL, callbackData + vueScriptArea); |
| 133 | + addLanguageCallbackRegex (language, "^<style>", "{exclusive}", |
| 134 | + enterArea, NULL, callbackData + vueStyleArea); |
| 135 | + addLanguageCallbackRegex (language, "^</style>", "{exclusive}", |
| 136 | + leaveArea, NULL, callbackData + vueStyleArea); |
| 137 | +} |
| 138 | + |
| 139 | +static void runVueParser (void) |
| 140 | +{ |
| 141 | + findRegexTags (); |
| 142 | +} |
| 143 | + |
| 144 | +extern parserDefinition* VueParser (void) |
| 145 | +{ |
| 146 | + static const char *const extensions [] = { "vue", NULL }; |
| 147 | + parserDefinition* const def = parserNew ("Vue"); |
| 148 | + def->extensions = extensions; |
| 149 | + def->initialize = initializeVueParser; |
| 150 | + def->method = METHOD_REGEX; |
| 151 | + def->parser = runVueParser; |
| 152 | + return def; |
| 153 | +} |
0 commit comments