aboutsummaryrefslogtreecommitdiff
path: root/plugins/language_yaml.lua
blob: 8b7f6347861e7f432ce2781c308dd5f7dffa8688 (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
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
-- mod-version:3
local syntax = require "core.syntax"

local yaml_bracket_list = {
  patterns = {
    -- comments
    { pattern = { "#", "\n"},                  type = "comment"  },
    -- strings
    { pattern = { '"', '"', '\\' },            type = "string"   },
    { pattern = { "'", "'", '\\' },            type = "string"   },
    -- keys
    {
      pattern = "[%w%d]+%g+()%s*():()%s",
      type = { "keyword2", "normal", "operator", "normal" }
    },
     -- variables
    { pattern = "%$%a%w+",                     type = "keyword"  },
    { pattern = "%$%{%{.-%}%}",                type = "keyword"  },
    -- numeric place holders
    { pattern = "%-?%.inf",                    type = "number"   },
    { pattern = "%.NaN",                       type = "number"   },
    -- numbers
    { pattern = "[%+%-]?0%d+",                 type = "number"   },
    { pattern = "[%+%-]?0x%x+",                type = "number"   },
    { pattern = "[%+%-]?%d+[,%.eE:%+%d]*%d+",  type = "number"   },
    { pattern = "[%+%-]?%d+",                  type = "number"   },
    -- others
    { pattern = ",",                           type = "operator" },
    { pattern = "%w+",                         type = "string"   },
    {
      pattern = "[_%(%)%*@~`!%%%^&=%+%-\\;%.><%?/%s]+",
      type = "string"
    }
  },
  symbols = {}
}

syntax.add {
  name = "YAML",
  files = { "%.yml$", "%.yaml$" },
  comment = "#",
  space_handling = false,
  patterns = {
  --- rules that start with spaces first and those taking precedence
    -- parent and child keys
    {
      pattern = "^[%w%d]+%g+%s*%f[:]",
      type = "literal"
    },
    {
      pattern = "^%s+[%w%d]+%g+%s*%f[:]",
      type = "keyword2"
    },
    -- bracket lists after key declaration
    {
      pattern = { ":%s+%[", "%]" },
      syntax = yaml_bracket_list, type = "operator"
    },
    {
      pattern = { ":%s+{", "}" },
      syntax = yaml_bracket_list, type = "operator"
    },
    -- child key
    {
      pattern = "^%s+()[%w%d]+%g+()%s*():()%s",
      type = { "normal", "keyword2", "normal", "operator", "normal" }
    },
    -- child list element
    {
      pattern = "^%s+()%-()%s+()[%w%d]+%g+()%s*():()%s",
      type = { "normal", "operator", "normal", "keyword2", "normal", "operator", "normal" }
    },
    -- unkeyed bracket lists
    {
      pattern = { "^%s*%[", "%]" },
      syntax = yaml_bracket_list, type = "operator"
    },
    {
      pattern = { "^%s*{", "}" },
      syntax = yaml_bracket_list, type = "operator"
    },
    {
      pattern = { "^%s*%-%s*%[", "%]" },
      syntax = yaml_bracket_list, type = "operator"
    },
    {
      pattern = { "^%s*%-%s*{", "}" },
      syntax = yaml_bracket_list, type = "operator"
    },
    -- rule to optimize space handling
    { pattern = "%s+",                         type = "normal"   },
  --- all the other rules
    -- comments
    { pattern = { "#", "\n"},                  type = "comment"  },
    -- strings
    { pattern = { '"', '"', '\\' },            type = "string"   },
    { pattern = { "'", "'", '\\' },            type = "string"   },
    -- extra bracket lists rules on explicit type
    {
      pattern = { "!!%w+%s+%[", "%]"},
      syntax = yaml_bracket_list, type = "operator"
    },
    {
      pattern = { "!!%w+%s+{", "}"},
      syntax = yaml_bracket_list, type = "operator"
    },
    -- numeric place holders
    { pattern = "%-?%.inf",                    type = "number"   },
    { pattern = "%.NaN",                       type = "number"   },
    -- parent list element
    {
      pattern = "^%-()%s+()[%w%d]+%g+()%s*():()%s",
      type = { "operator", "normal", "keyword2", "normal", "operator", "normal" }
    },
    -- key label
    {
      pattern = "%&()%g+",
      type = { "keyword", "literal" }
    },
    -- key elements expansion
    { pattern = "<<",                          type = "literal"  },
    {
      pattern = "%*()[%w%d_]+",
      type = { "keyword", "literal" }
    },
    -- explicit data types
    { pattern = "!!%g+",                       type = "keyword"  },
    -- parent key
    {
      pattern = "^[%w%d]+%g+()%s*():()%s",
      type = { "literal", "normal", "operator", "normal" }
    },
    -- variables
    { pattern = "%$%a%w+",                     type = "keyword"  },
    { pattern = "%$%{%{.-%}%}",                type = "keyword"  },
    -- numbers
    { pattern = "[%+%-]?0%d+",                 type = "number"   },
    { pattern = "[%+%-]?0x%x+",                type = "number"   },
    { pattern = "[%+%-]?%d+[,%.eE:%+%d]*%d+",  type = "number"   },
    { pattern = "[%+%-]?%d+",                  type = "number"   },
    -- special operators
    { pattern = "[%*%|%!>%%]",                 type = "keyword"  },
    { pattern = "[%-%$:%?]+",                  type = "operator" },
    -- Everything else as a string
    { pattern = "[%d%a_][%g_]*",               type = "string"   },
    { pattern = "%p+",                         type = "string"   }
  },
  symbols = {
    ["true"]  = "number",
    ["false"] = "number",
    ["y"]     = "number",
    ["n"]     = "number"
  }
}