xref: /vim-8.2.3635/runtime/syntax/json.vim (revision 589edb34)
107d87790SBram Moolenaar" Vim syntax file
207d87790SBram Moolenaar" Language:	JSON
3396e829fSBram Moolenaar" Maintainer:	vacancy
4396e829fSBram Moolenaar" Previous Maintainer:	Eli Parra <[email protected]>
5*589edb34SBram Moolenaar" Last Change:	2019 Sep 17
67b61a546SBram Moolenaar" Version:      0.12
707d87790SBram Moolenaar
87b61a546SBram Moolenaarif !exists("main_syntax")
989bcfda6SBram Moolenaar  " quit when a syntax file was already loaded
1089bcfda6SBram Moolenaar  if exists("b:current_syntax")
1107d87790SBram Moolenaar    finish
1207d87790SBram Moolenaar  endif
137b61a546SBram Moolenaar  let main_syntax = 'json'
147b61a546SBram Moolenaarendif
1507d87790SBram Moolenaar
167b61a546SBram Moolenaarsyntax match   jsonNoise           /\%(:\|,\)/
1707d87790SBram Moolenaar
187b61a546SBram Moolenaar" NOTE that for the concealing to work your conceallevel should be set to 2
197b61a546SBram Moolenaar
20396e829fSBram Moolenaar" Syntax: JSON Keywords
21396e829fSBram Moolenaar" Separated into a match and region because a region by itself is always greedy
22396e829fSBram Moolenaarsyn match  jsonKeywordMatch /"\([^"]\|\\\"\)\+"[[:blank:]\r\n]*\:/ contains=jsonKeyword
23*589edb34SBram Moolenaarif has('conceal') && (!exists("g:vim_json_conceal") || g:vim_json_conceal==1)
24396e829fSBram Moolenaar   syn region  jsonKeyword matchgroup=jsonQuote start=/"/  end=/"\ze[[:blank:]\r\n]*\:/ concealends contained
25396e829fSBram Moolenaarelse
26396e829fSBram Moolenaar   syn region  jsonKeyword matchgroup=jsonQuote start=/"/  end=/"\ze[[:blank:]\r\n]*\:/ contained
27396e829fSBram Moolenaarendif
28396e829fSBram Moolenaar
297b61a546SBram Moolenaar" Syntax: Strings
307b61a546SBram Moolenaar" Separated into a match and region because a region by itself is always greedy
31396e829fSBram Moolenaar" Needs to come after keywords or else a json encoded string will break the
32396e829fSBram Moolenaar" syntax
337b61a546SBram Moolenaarsyn match  jsonStringMatch /"\([^"]\|\\\"\)\+"\ze[[:blank:]\r\n]*[,}\]]/ contains=jsonString
34*589edb34SBram Moolenaarif has('conceal') && (!exists("g:vim_json_conceal") || g:vim_json_conceal==1)
357b61a546SBram Moolenaar	syn region  jsonString oneline matchgroup=jsonQuote start=/"/  skip=/\\\\\|\\"/  end=/"/ concealends contains=jsonEscape contained
367b61a546SBram Moolenaarelse
377b61a546SBram Moolenaar	syn region  jsonString oneline matchgroup=jsonQuote start=/"/  skip=/\\\\\|\\"/  end=/"/ contains=jsonEscape contained
387b61a546SBram Moolenaarendif
397b61a546SBram Moolenaar
407b61a546SBram Moolenaar" Syntax: JSON does not allow strings with single quotes, unlike JavaScript.
417b61a546SBram Moolenaarsyn region  jsonStringSQError oneline  start=+'+  skip=+\\\\\|\\"+  end=+'+
427b61a546SBram Moolenaar
437b61a546SBram Moolenaar
447b61a546SBram Moolenaar" Syntax: Escape sequences
457b61a546SBram Moolenaarsyn match   jsonEscape    "\\["\\/bfnrt]" contained
467b61a546SBram Moolenaarsyn match   jsonEscape    "\\u\x\{4}" contained
477b61a546SBram Moolenaar
487b61a546SBram Moolenaar" Syntax: Numbers
497b61a546SBram Moolenaarsyn match   jsonNumber    "-\=\<\%(0\|[1-9]\d*\)\%(\.\d\+\)\=\%([eE][-+]\=\d\+\)\=\>\ze[[:blank:]\r\n]*[,}\]]"
507b61a546SBram Moolenaar
517b61a546SBram Moolenaar" ERROR WARNINGS **********************************************
527b61a546SBram Moolenaarif (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
537b61a546SBram Moolenaar	" Syntax: Strings should always be enclosed with quotes.
547b61a546SBram Moolenaar	syn match   jsonNoQuotesError  "\<[[:alpha:]][[:alnum:]]*\>"
557b61a546SBram Moolenaar	syn match   jsonTripleQuotesError  /"""/
567b61a546SBram Moolenaar
577b61a546SBram Moolenaar	" Syntax: An integer part of 0 followed by other digits is not allowed.
587b61a546SBram Moolenaar	syn match   jsonNumError  "-\=\<0\d\.\d*\>"
597b61a546SBram Moolenaar
607b61a546SBram Moolenaar	" Syntax: Decimals smaller than one should begin with 0 (so .1 should be 0.1).
617b61a546SBram Moolenaar	syn match   jsonNumError  "\:\@<=[[:blank:]\r\n]*\zs\.\d\+"
627b61a546SBram Moolenaar
637b61a546SBram Moolenaar	" Syntax: No comments in JSON, see http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
647b61a546SBram Moolenaar	syn match   jsonCommentError  "//.*"
657b61a546SBram Moolenaar	syn match   jsonCommentError  "\(/\*\)\|\(\*/\)"
667b61a546SBram Moolenaar
677b61a546SBram Moolenaar	" Syntax: No semicolons in JSON
687b61a546SBram Moolenaar	syn match   jsonSemicolonError  ";"
697b61a546SBram Moolenaar
707b61a546SBram Moolenaar	" Syntax: No trailing comma after the last element of arrays or objects
717b61a546SBram Moolenaar	syn match   jsonTrailingCommaError  ",\_s*[}\]]"
727b61a546SBram Moolenaar
737b61a546SBram Moolenaar	" Syntax: Watch out for missing commas between elements
747b61a546SBram Moolenaar	syn match   jsonMissingCommaError /\("\|\]\|\d\)\zs\_s\+\ze"/
757b61a546SBram Moolenaar	syn match   jsonMissingCommaError /\(\]\|\}\)\_s\+\ze"/ "arrays/objects as values
767b61a546SBram Moolenaar	syn match   jsonMissingCommaError /}\_s\+\ze{/ "objects as elements in an array
777b61a546SBram Moolenaar	syn match   jsonMissingCommaError /\(true\|false\)\_s\+\ze"/ "true/false as value
787b61a546SBram Moolenaarendif
797b61a546SBram Moolenaar
807b61a546SBram Moolenaar" ********************************************** END OF ERROR WARNINGS
817b61a546SBram Moolenaar" Allowances for JSONP: function call at the beginning of the file,
827b61a546SBram Moolenaar" parenthesis and semicolon at the end.
837b61a546SBram Moolenaar" Function name validation based on
847b61a546SBram Moolenaar" http://stackoverflow.com/questions/2008279/validate-a-javascript-function-name/2008444#2008444
857b61a546SBram Moolenaarsyn match  jsonPadding "\%^[[:blank:]\r\n]*[_$[:alpha:]][_$[:alnum:]]*[[:blank:]\r\n]*("
867b61a546SBram Moolenaarsyn match  jsonPadding ");[[:blank:]\r\n]*\%$"
877b61a546SBram Moolenaar
887b61a546SBram Moolenaar" Syntax: Boolean
897b61a546SBram Moolenaarsyn match  jsonBoolean /\(true\|false\)\(\_s\+\ze"\)\@!/
907b61a546SBram Moolenaar
917b61a546SBram Moolenaar" Syntax: Null
927b61a546SBram Moolenaarsyn keyword  jsonNull      null
937b61a546SBram Moolenaar
947b61a546SBram Moolenaar" Syntax: Braces
957b61a546SBram Moolenaarsyn region  jsonFold matchgroup=jsonBraces start="{" end=/}\(\_s\+\ze\("\|{\)\)\@!/ transparent fold
967b61a546SBram Moolenaarsyn region  jsonFold matchgroup=jsonBraces start="\[" end=/]\(\_s\+\ze"\)\@!/ transparent fold
977b61a546SBram Moolenaar
987b61a546SBram Moolenaar" Define the default highlighting.
9989bcfda6SBram Moolenaar" Only when an item doesn't have highlighting yet
100f37506f6SBram Moolenaarhi def link jsonPadding         Operator
101f37506f6SBram Moolenaarhi def link jsonString          String
102f37506f6SBram Moolenaarhi def link jsonTest          Label
103f37506f6SBram Moolenaarhi def link jsonEscape          Special
104f37506f6SBram Moolenaarhi def link jsonNumber          Number
105f37506f6SBram Moolenaarhi def link jsonBraces          Delimiter
106f37506f6SBram Moolenaarhi def link jsonNull            Function
107f37506f6SBram Moolenaarhi def link jsonBoolean         Boolean
108f37506f6SBram Moolenaarhi def link jsonKeyword         Label
1097b61a546SBram Moolenaar
1107b61a546SBram Moolenaarif (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
111f37506f6SBram Moolenaarhi def link jsonNumError        Error
112f37506f6SBram Moolenaarhi def link jsonCommentError    Error
113f37506f6SBram Moolenaarhi def link jsonSemicolonError  Error
114f37506f6SBram Moolenaarhi def link jsonTrailingCommaError     Error
115f37506f6SBram Moolenaarhi def link jsonMissingCommaError      Error
116f37506f6SBram Moolenaarhi def link jsonStringSQError        	Error
117f37506f6SBram Moolenaarhi def link jsonNoQuotesError        	Error
118f37506f6SBram Moolenaarhi def link jsonTripleQuotesError     	Error
1197b61a546SBram Moolenaarendif
120f37506f6SBram Moolenaarhi def link jsonQuote           Quote
121f37506f6SBram Moolenaarhi def link jsonNoise           Noise
1227b61a546SBram Moolenaar
1237b61a546SBram Moolenaarlet b:current_syntax = "json"
1247b61a546SBram Moolenaarif main_syntax == 'json'
1257b61a546SBram Moolenaar  unlet main_syntax
1267b61a546SBram Moolenaarendif
1277b61a546SBram Moolenaar
1287b61a546SBram Moolenaar" Vim settings
1297b61a546SBram Moolenaar" vim: ts=8 fdm=marker
1307b61a546SBram Moolenaar
1317b61a546SBram Moolenaar" MIT License
1327b61a546SBram Moolenaar" Copyright (c) 2013, Jeroen Ruigrok van der Werven, Eli Parra
1337b61a546SBram Moolenaar"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:
1347b61a546SBram Moolenaar"The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
1357b61a546SBram Moolenaar"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.
1367b61a546SBram Moolenaar"See https://twitter.com/elzr/status/294964017926119424
137