blob: d80af84312f9ae22dee9580528dd6d9afe242122 [file] [log] [blame]
Bram Moolenaar07d87792014-07-19 14:04:47 +02001" Vim syntax file
Bram Moolenaar7b61a542014-08-23 15:31:19 +02002" Language: JSON
3" Maintainer: Eli Parra <eli@elzr.com>
4" Last Change: 2014 Aug 23
5" Version: 0.12
Bram Moolenaar07d87792014-07-19 14:04:47 +02006
Bram Moolenaar7b61a542014-08-23 15:31:19 +02007if !exists("main_syntax")
Bram Moolenaar89bcfda2016-08-30 23:26:57 +02008 " quit when a syntax file was already loaded
9 if exists("b:current_syntax")
Bram Moolenaar7b61a542014-08-23 15:31:19 +020010 finish
11 endif
12 let main_syntax = 'json'
Bram Moolenaar07d87792014-07-19 14:04:47 +020013endif
14
Bram Moolenaar7b61a542014-08-23 15:31:19 +020015syntax match jsonNoise /\%(:\|,\)/
Bram Moolenaar07d87792014-07-19 14:04:47 +020016
Bram Moolenaar7b61a542014-08-23 15:31:19 +020017" NOTE that for the concealing to work your conceallevel should be set to 2
18
19" Syntax: Strings
20" Separated into a match and region because a region by itself is always greedy
21syn match jsonStringMatch /"\([^"]\|\\\"\)\+"\ze[[:blank:]\r\n]*[,}\]]/ contains=jsonString
22if has('conceal')
23 syn region jsonString oneline matchgroup=jsonQuote start=/"/ skip=/\\\\\|\\"/ end=/"/ concealends contains=jsonEscape contained
24else
25 syn region jsonString oneline matchgroup=jsonQuote start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=jsonEscape contained
26endif
27
28" Syntax: JSON does not allow strings with single quotes, unlike JavaScript.
29syn region jsonStringSQError oneline start=+'+ skip=+\\\\\|\\"+ end=+'+
30
31" Syntax: JSON Keywords
32" Separated into a match and region because a region by itself is always greedy
33syn match jsonKeywordMatch /"\([^"]\|\\\"\)\+"[[:blank:]\r\n]*\:/ contains=jsonKeyword
34if has('conceal')
35 syn region jsonKeyword matchgroup=jsonQuote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ concealends contained
36else
37 syn region jsonKeyword matchgroup=jsonQuote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ contained
38endif
39
40" Syntax: Escape sequences
41syn match jsonEscape "\\["\\/bfnrt]" contained
42syn match jsonEscape "\\u\x\{4}" contained
43
44" Syntax: Numbers
45syn match jsonNumber "-\=\<\%(0\|[1-9]\d*\)\%(\.\d\+\)\=\%([eE][-+]\=\d\+\)\=\>\ze[[:blank:]\r\n]*[,}\]]"
46
47" ERROR WARNINGS **********************************************
48if (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
49 " Syntax: Strings should always be enclosed with quotes.
50 syn match jsonNoQuotesError "\<[[:alpha:]][[:alnum:]]*\>"
51 syn match jsonTripleQuotesError /"""/
52
53 " Syntax: An integer part of 0 followed by other digits is not allowed.
54 syn match jsonNumError "-\=\<0\d\.\d*\>"
55
56 " Syntax: Decimals smaller than one should begin with 0 (so .1 should be 0.1).
57 syn match jsonNumError "\:\@<=[[:blank:]\r\n]*\zs\.\d\+"
58
59 " Syntax: No comments in JSON, see http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
60 syn match jsonCommentError "//.*"
61 syn match jsonCommentError "\(/\*\)\|\(\*/\)"
62
63 " Syntax: No semicolons in JSON
64 syn match jsonSemicolonError ";"
65
66 " Syntax: No trailing comma after the last element of arrays or objects
67 syn match jsonTrailingCommaError ",\_s*[}\]]"
68
69 " Syntax: Watch out for missing commas between elements
70 syn match jsonMissingCommaError /\("\|\]\|\d\)\zs\_s\+\ze"/
71 syn match jsonMissingCommaError /\(\]\|\}\)\_s\+\ze"/ "arrays/objects as values
72 syn match jsonMissingCommaError /}\_s\+\ze{/ "objects as elements in an array
73 syn match jsonMissingCommaError /\(true\|false\)\_s\+\ze"/ "true/false as value
74endif
75
76" ********************************************** END OF ERROR WARNINGS
77" Allowances for JSONP: function call at the beginning of the file,
78" parenthesis and semicolon at the end.
79" Function name validation based on
80" http://stackoverflow.com/questions/2008279/validate-a-javascript-function-name/2008444#2008444
81syn match jsonPadding "\%^[[:blank:]\r\n]*[_$[:alpha:]][_$[:alnum:]]*[[:blank:]\r\n]*("
82syn match jsonPadding ");[[:blank:]\r\n]*\%$"
83
84" Syntax: Boolean
85syn match jsonBoolean /\(true\|false\)\(\_s\+\ze"\)\@!/
86
87" Syntax: Null
88syn keyword jsonNull null
89
90" Syntax: Braces
91syn region jsonFold matchgroup=jsonBraces start="{" end=/}\(\_s\+\ze\("\|{\)\)\@!/ transparent fold
92syn region jsonFold matchgroup=jsonBraces start="\[" end=/]\(\_s\+\ze"\)\@!/ transparent fold
93
94" Define the default highlighting.
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020095" Only when an item doesn't have highlighting yet
Bram Moolenaarf37506f2016-08-31 22:22:10 +020096hi def link jsonPadding Operator
97hi def link jsonString String
98hi def link jsonTest Label
99hi def link jsonEscape Special
100hi def link jsonNumber Number
101hi def link jsonBraces Delimiter
102hi def link jsonNull Function
103hi def link jsonBoolean Boolean
104hi def link jsonKeyword Label
Bram Moolenaar7b61a542014-08-23 15:31:19 +0200105
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200106if (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200107hi def link jsonNumError Error
108hi def link jsonCommentError Error
109hi def link jsonSemicolonError Error
110hi def link jsonTrailingCommaError Error
111hi def link jsonMissingCommaError Error
112hi def link jsonStringSQError Error
113hi def link jsonNoQuotesError Error
114hi def link jsonTripleQuotesError Error
Bram Moolenaar7b61a542014-08-23 15:31:19 +0200115endif
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200116hi def link jsonQuote Quote
117hi def link jsonNoise Noise
Bram Moolenaar7b61a542014-08-23 15:31:19 +0200118
119let b:current_syntax = "json"
120if main_syntax == 'json'
121 unlet main_syntax
122endif
123
124" Vim settings
125" vim: ts=8 fdm=marker
126
127" MIT License
128" Copyright (c) 2013, Jeroen Ruigrok van der Werven, Eli Parra
129"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:
130"The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
131"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.
132"See https://twitter.com/elzr/status/294964017926119424