blob: 3f49b0c5ea5c4fa16437af1b4c31c949aed1a05f [file] [log] [blame]
Bram Moolenaar07d87792014-07-19 14:04:47 +02001" Vim syntax file
Bram Moolenaar7b61a542014-08-23 15:31:19 +02002" Language: JSON
Bram Moolenaar396e8292019-07-13 23:04:31 +02003" Maintainer: vacancy
4" Previous Maintainer: Eli Parra <eli@elzr.com>
Bram Moolenaar589edb32019-09-20 14:38:13 +02005" Last Change: 2019 Sep 17
Bram Moolenaar7b61a542014-08-23 15:31:19 +02006" Version: 0.12
Bram Moolenaar07d87792014-07-19 14:04:47 +02007
Bram Moolenaar7b61a542014-08-23 15:31:19 +02008if !exists("main_syntax")
Bram Moolenaar89bcfda2016-08-30 23:26:57 +02009 " quit when a syntax file was already loaded
10 if exists("b:current_syntax")
Bram Moolenaar7b61a542014-08-23 15:31:19 +020011 finish
12 endif
13 let main_syntax = 'json'
Bram Moolenaar07d87792014-07-19 14:04:47 +020014endif
15
Bram Moolenaar7b61a542014-08-23 15:31:19 +020016syntax match jsonNoise /\%(:\|,\)/
Bram Moolenaar07d87792014-07-19 14:04:47 +020017
Bram Moolenaar7b61a542014-08-23 15:31:19 +020018" NOTE that for the concealing to work your conceallevel should be set to 2
19
Bram Moolenaar396e8292019-07-13 23:04:31 +020020" Syntax: JSON Keywords
21" Separated into a match and region because a region by itself is always greedy
22syn match jsonKeywordMatch /"\([^"]\|\\\"\)\+"[[:blank:]\r\n]*\:/ contains=jsonKeyword
Bram Moolenaar589edb32019-09-20 14:38:13 +020023if has('conceal') && (!exists("g:vim_json_conceal") || g:vim_json_conceal==1)
Bram Moolenaar396e8292019-07-13 23:04:31 +020024 syn region jsonKeyword matchgroup=jsonQuote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ concealends contained
25else
26 syn region jsonKeyword matchgroup=jsonQuote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ contained
27endif
28
Bram Moolenaar7b61a542014-08-23 15:31:19 +020029" Syntax: Strings
30" Separated into a match and region because a region by itself is always greedy
Bram Moolenaar396e8292019-07-13 23:04:31 +020031" Needs to come after keywords or else a json encoded string will break the
32" syntax
Bram Moolenaar7b61a542014-08-23 15:31:19 +020033syn match jsonStringMatch /"\([^"]\|\\\"\)\+"\ze[[:blank:]\r\n]*[,}\]]/ contains=jsonString
Bram Moolenaar589edb32019-09-20 14:38:13 +020034if has('conceal') && (!exists("g:vim_json_conceal") || g:vim_json_conceal==1)
Bram Moolenaar7b61a542014-08-23 15:31:19 +020035 syn region jsonString oneline matchgroup=jsonQuote start=/"/ skip=/\\\\\|\\"/ end=/"/ concealends contains=jsonEscape contained
36else
37 syn region jsonString oneline matchgroup=jsonQuote start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=jsonEscape contained
38endif
39
40" Syntax: JSON does not allow strings with single quotes, unlike JavaScript.
41syn region jsonStringSQError oneline start=+'+ skip=+\\\\\|\\"+ end=+'+
42
Bram Moolenaar7b61a542014-08-23 15:31:19 +020043
44" Syntax: Escape sequences
45syn match jsonEscape "\\["\\/bfnrt]" contained
46syn match jsonEscape "\\u\x\{4}" contained
47
48" Syntax: Numbers
49syn match jsonNumber "-\=\<\%(0\|[1-9]\d*\)\%(\.\d\+\)\=\%([eE][-+]\=\d\+\)\=\>\ze[[:blank:]\r\n]*[,}\]]"
50
51" ERROR WARNINGS **********************************************
52if (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
53 " Syntax: Strings should always be enclosed with quotes.
54 syn match jsonNoQuotesError "\<[[:alpha:]][[:alnum:]]*\>"
55 syn match jsonTripleQuotesError /"""/
56
57 " Syntax: An integer part of 0 followed by other digits is not allowed.
58 syn match jsonNumError "-\=\<0\d\.\d*\>"
59
60 " Syntax: Decimals smaller than one should begin with 0 (so .1 should be 0.1).
61 syn match jsonNumError "\:\@<=[[:blank:]\r\n]*\zs\.\d\+"
62
63 " Syntax: No comments in JSON, see http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
64 syn match jsonCommentError "//.*"
65 syn match jsonCommentError "\(/\*\)\|\(\*/\)"
66
67 " Syntax: No semicolons in JSON
68 syn match jsonSemicolonError ";"
69
70 " Syntax: No trailing comma after the last element of arrays or objects
71 syn match jsonTrailingCommaError ",\_s*[}\]]"
72
73 " Syntax: Watch out for missing commas between elements
74 syn match jsonMissingCommaError /\("\|\]\|\d\)\zs\_s\+\ze"/
75 syn match jsonMissingCommaError /\(\]\|\}\)\_s\+\ze"/ "arrays/objects as values
76 syn match jsonMissingCommaError /}\_s\+\ze{/ "objects as elements in an array
77 syn match jsonMissingCommaError /\(true\|false\)\_s\+\ze"/ "true/false as value
78endif
79
80" ********************************************** END OF ERROR WARNINGS
81" Allowances for JSONP: function call at the beginning of the file,
82" parenthesis and semicolon at the end.
83" Function name validation based on
84" http://stackoverflow.com/questions/2008279/validate-a-javascript-function-name/2008444#2008444
85syn match jsonPadding "\%^[[:blank:]\r\n]*[_$[:alpha:]][_$[:alnum:]]*[[:blank:]\r\n]*("
86syn match jsonPadding ");[[:blank:]\r\n]*\%$"
87
88" Syntax: Boolean
89syn match jsonBoolean /\(true\|false\)\(\_s\+\ze"\)\@!/
90
91" Syntax: Null
92syn keyword jsonNull null
93
94" Syntax: Braces
95syn region jsonFold matchgroup=jsonBraces start="{" end=/}\(\_s\+\ze\("\|{\)\)\@!/ transparent fold
96syn region jsonFold matchgroup=jsonBraces start="\[" end=/]\(\_s\+\ze"\)\@!/ transparent fold
97
98" Define the default highlighting.
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020099" Only when an item doesn't have highlighting yet
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200100hi def link jsonPadding Operator
101hi def link jsonString String
102hi def link jsonTest Label
103hi def link jsonEscape Special
104hi def link jsonNumber Number
105hi def link jsonBraces Delimiter
106hi def link jsonNull Function
107hi def link jsonBoolean Boolean
108hi def link jsonKeyword Label
Bram Moolenaar7b61a542014-08-23 15:31:19 +0200109
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200110if (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200111hi def link jsonNumError Error
112hi def link jsonCommentError Error
113hi def link jsonSemicolonError Error
114hi def link jsonTrailingCommaError Error
115hi def link jsonMissingCommaError Error
116hi def link jsonStringSQError Error
117hi def link jsonNoQuotesError Error
118hi def link jsonTripleQuotesError Error
Bram Moolenaar7b61a542014-08-23 15:31:19 +0200119endif
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200120hi def link jsonQuote Quote
121hi def link jsonNoise Noise
Bram Moolenaar7b61a542014-08-23 15:31:19 +0200122
123let b:current_syntax = "json"
124if main_syntax == 'json'
125 unlet main_syntax
126endif
127
128" Vim settings
129" vim: ts=8 fdm=marker
130
131" MIT License
132" Copyright (c) 2013, Jeroen Ruigrok van der Werven, Eli Parra
133"Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
134"The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
135"THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
136"See https://twitter.com/elzr/status/294964017926119424