summaryrefslogtreecommitdiffstats
path: root/public/prism/prism-v.js
blob: 9615cec3fa3e53a22e4c6350ab22a3044aee7d1c (plain)
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
(function (Prism) {
  var interpolationExpr = {
    pattern: /[\s\S]+/,
    inside: null,
  };

  Prism.languages.v = Prism.languages.extend('clike', {
    string: {
      pattern: /r?(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
      alias: 'quoted-string',
      greedy: true,
      inside: {
        interpolation: {
          pattern:
            /((?:^|[^\\])(?:\\{2})*)\$(?:\{[^{}]*\}|\w+(?:\.\w+(?:\([^\(\)]*\))?|\[[^\[\]]+\])*)/,
          lookbehind: true,
          inside: {
            'interpolation-variable': {
              pattern: /^\$\w[\s\S]*$/,
              alias: 'variable',
            },
            'interpolation-punctuation': {
              pattern: /^\$\{|\}$/,
              alias: 'punctuation',
            },
            'interpolation-expression': interpolationExpr,
          },
        },
      },
    },
    'class-name': {
      pattern: /(\b(?:enum|interface|struct|type)\s+)(?:C\.)?\w+/,
      lookbehind: true,
    },
    keyword:
      /(?:\b(?:__global|as|asm|assert|atomic|break|chan|const|continue|defer|else|embed|enum|fn|for|go(?:to)?|if|import|in|interface|is|lock|match|module|mut|none|or|pub|return|rlock|select|shared|sizeof|static|struct|type(?:of)?|union|unsafe)|\$(?:else|for|if)|#(?:flag|include))\b/,
    number:
      /\b(?:0x[a-f\d]+(?:_[a-f\d]+)*|0b[01]+(?:_[01]+)*|0o[0-7]+(?:_[0-7]+)*|\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?)\b/i,
    operator:
      /~|\?|[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\.?/,
    builtin:
      /\b(?:any(?:_float|_int)?|bool|byte(?:ptr)?|charptr|f(?:32|64)|i(?:8|16|64|128|nt)|rune|size_t|string|u(?:16|32|64|128)|voidptr)\b/,
  });

  interpolationExpr.inside = Prism.languages.v;

  Prism.languages.insertBefore('v', 'string', {
    char: {
      pattern: /`(?:\\`|\\?[^`]{1,2})`/, // using {1,2} instead of `u` flag for compatibility
      alias: 'rune',
    },
  });

  Prism.languages.insertBefore('v', 'operator', {
    attribute: {
      pattern:
        /(^[\t ]*)\[(?:deprecated|direct_array_access|flag|inline|live|ref_only|typedef|unsafe_fn|windows_stdcall)\]/m,
      lookbehind: true,
      alias: 'annotation',
      inside: {
        punctuation: /[\[\]]/,
        keyword: /\w+/,
      },
    },
    generic: {
      pattern: /<\w+>(?=\s*[\)\{])/,
      inside: {
        punctuation: /[<>]/,
        'class-name': /\w+/,
      },
    },
  });

  Prism.languages.insertBefore('v', 'function', {
    'generic-function': {
      // e.g. foo<T>( ...
      pattern: /\b\w+\s*<\w+>(?=\()/,
      inside: {
        function: /^\w+/,
        generic: {
          pattern: /<\w+>/,
          inside: Prism.languages.v.generic.inside,
        },
      },
    },
  });
})(Prism);
">: /[[\](){},;:/]/, }); Prism.languages.xquery.tag.pattern = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\[\s\S]|\{(?!\{)(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}|(?!\1)[^\\])*\1|[^\s'">=]+))?)*\s*\/?>/; Prism.languages.xquery['tag'].inside['attr-value'].pattern = /=(?:("|')(?:\\[\s\S]|\{(?!\{)(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}|(?!\1)[^\\])*\1|[^\s'">=]+)/; Prism.languages.xquery['tag'].inside['attr-value'].inside['punctuation'] = /^="|"$/; Prism.languages.xquery['tag'].inside['attr-value'].inside['expression'] = { // Allow for two levels of nesting pattern: /\{(?!\{)(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}/, inside: Prism.languages.xquery, alias: 'language-xquery', }; // The following will handle plain text inside tags var stringifyToken = function (token) { if (typeof token === 'string') { return token; } if (typeof token.content === 'string') { return token.content; } return token.content.map(stringifyToken).join(''); }; var walkTokens = function (tokens) { var openedTags = []; for (var i = 0; i < tokens.length; i++) { var token = tokens[i]; var notTagNorBrace = false; if (typeof token !== 'string') { if ( token.type === 'tag' && token.content[0] && token.content[0].type === 'tag' ) { // We found a tag, now find its kind if (token.content[0].content[0].content === '</') { // Closing tag if ( openedTags.length > 0 && openedTags[openedTags.length - 1].tagName === stringifyToken(token.content[0].content[1]) ) { // Pop matching opening tag openedTags.pop(); } } else { if (token.content[token.content.length - 1].content === '/>') { // Autoclosed tag, ignore } else { // Opening tag openedTags.push({ tagName: stringifyToken(token.content[0].content[1]), openedBraces: 0, }); } } } else if ( openedTags.length > 0 && token.type === 'punctuation' && token.content === '{' && // Ignore `{{` (!tokens[i + 1] || tokens[i + 1].type !== 'punctuation' || tokens[i + 1].content !== '{') && (!tokens[i - 1] || tokens[i - 1].type !== 'plain-text' || tokens[i - 1].content !== '{') ) { // Here we might have entered an XQuery expression inside a tag openedTags[openedTags.length - 1].openedBraces++; } else if ( openedTags.length > 0 && openedTags[openedTags.length - 1].openedBraces > 0 && token.type === 'punctuation' && token.content === '}' ) { // Here we might have left an XQuery expression inside a tag openedTags[openedTags.length - 1].openedBraces--; } else if (token.type !== 'comment') { notTagNorBrace = true; } } if (notTagNorBrace || typeof token === 'string') { if ( openedTags.length > 0 && openedTags[openedTags.length - 1].openedBraces === 0 ) { // Here we are inside a tag, and not inside an XQuery expression. // That's plain text: drop any tokens matched. var plainText = stringifyToken(token); // And merge text with adjacent text if ( i < tokens.length - 1 && (typeof tokens[i + 1] === 'string' || tokens[i + 1].type === 'plain-text') ) { plainText += stringifyToken(tokens[i + 1]); tokens.splice(i + 1, 1); } if ( i > 0 && (typeof tokens[i - 1] === 'string' || tokens[i - 1].type === 'plain-text') ) { plainText = stringifyToken(tokens[i - 1]) + plainText; tokens.splice(i - 1, 1); i--; } if (/^\s+$/.test(plainText)) { tokens[i] = plainText; } else { tokens[i] = new Prism.Token( 'plain-text', plainText, null, plainText ); } } } if (token.content && typeof token.content !== 'string') { walkTokens(token.content); } } }; Prism.hooks.add('after-tokenize', function (env) { if (env.language !== 'xquery') { return; } walkTokens(env.tokens); }); })(Prism);