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