Skip to content

Commit b78537d

Browse files
jrappenAshwin Shenoyjfcherngjwortmannjskinner
committed
[JSON] Rewrite syntax
- Split `JSON.sublime-syntax` into ... using inheritance: - `JSON (Basic).sublime-syntax` with `scope:source.json.basic` - `JSON.sublime-syntax` with `scope:source.json` - `JSONC.sublime-syntax` with `scope:source.json.jsonc` - `JSON5.sublime-syntax` with `scope:source.json.json5` - Although the base syntax does not define a `prototype`, we add `meta_include_prototype: false` to the base syntax, to prevent inheriting syntaxes from injecting rules. - make use of `variables` - Add many more file extensions for `JSON` and `JSONC` - Significantly extend tests to cover more parts of the syntaxes defined: - Split original test file into logical parts - Add indentation tests for: - `JSON`, `JSONC` - `mapping` (objects), `sequence` (arrays) - leave `JSON` headers in `Markdown` as json only, but split up fenced code blocks into `json` and `jsonc` to behave similarly to `GitHub Flavored Markdown` - fix tests for `meta.mapping meta.mapping.*` - make `mapping.*` contexts more modular - fix sublimehq#285 as requested by Jon - address sublimehq#757 and use tips to fix line comments for `JSONC` - address sublimehq#2430 and use sort-order as requested by deathaxe - address sublimehq#2852 and use tips to fix scopes of curly braces & square brackets in `JSON` Co-authored-by: Ashwin Shenoy <[email protected]> Co-authored-by: Jack Cherng <[email protected]> Co-authored-by: Janos Wortmann <[email protected]> Co-authored-by: Jon Skinner <[email protected]> Co-authored-by: FichteFoll <[email protected]> Co-authored-by: Keith Hall <[email protected]> Co-authored-by: Michael B. Lyons <[email protected]> Co-authored-by: Rafał Chłodnicki <[email protected]> Co-authored-by: deathaxe <[email protected]>
1 parent 258ace9 commit b78537d

33 files changed

+2450
-314
lines changed

JSON/Comments - JSONC.tmPreferences

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<plist version="1.0">
3+
<dict>
4+
<key>scope</key>
5+
<string>source.json.jsonc</string>
6+
<key>settings</key>
7+
<dict>
8+
<key>shellVariables</key>
9+
<array>
10+
<dict>
11+
<key>name</key><string>TM_COMMENT_START</string>
12+
<key>value</key><string>// </string>
13+
</dict>
14+
<dict>
15+
<key>name</key><string>TM_COMMENT_START_2</string>
16+
<key>value</key><string>/*</string>
17+
</dict>
18+
<dict>
19+
<key>name</key><string>TM_COMMENT_END_2</string>
20+
<key>value</key><string>*/</string>
21+
</dict>
22+
</array>
23+
</dict>
24+
</dict>
25+
</plist>

JSON/Comments.tmPreferences

Lines changed: 0 additions & 31 deletions
This file was deleted.

JSON/Default.sublime-keymap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@
6060
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
6161
]
6262
},
63-
]
63+
]

JSON/Indentation Rules.tmPreferences

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"(?:[^"\\]|\\.)*"
5252
|
5353
# Consume all chars that don't start a string, comment or
54-
# end the array that was opened on this line
54+
# end the object that was opened on this line
5555
[^"/\]]
5656
)*
5757
# Stop matching at the end of the line, or once we hit a comment

JSON/JSON (Basic).sublime-syntax

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
%YAML 1.2
2+
---
3+
# https://yaml.org/spec/1.2/spec.html
4+
# https://www.sublimetext.com/docs/syntax.html#ver-dev
5+
# https://www.sublimetext.com/docs/syntax.html#testing:ver-dev
6+
# https://www.sublimetext.com/docs/scope_naming.html
7+
name: JSON (Basic)
8+
scope: source.json.basic
9+
version: 2
10+
hidden: true
11+
12+
13+
####[ Variables ]#######################################################################################################
14+
15+
16+
variables:
17+
exponent: '(?:[eE][-+]?\d+)'
18+
optional_exponent: '(?:{{exponent}}?)'
19+
optional_neg_sign: '(?:[-]?)'
20+
pos_integer_decimal: '(?:0|[1-9]\d*)'
21+
22+
23+
####[ Contexts ]########################################################################################################
24+
25+
26+
contexts:
27+
28+
main:
29+
- include: any
30+
31+
any:
32+
- include: constants
33+
- include: numbers
34+
- include: strings
35+
- include: sequence
36+
- include: mapping
37+
38+
####[ Constants ]#######################################################################################################
39+
40+
constants:
41+
- match: \b(?:null)\b
42+
scope: constant.language.null.json
43+
- match: \b(?:false|true)\b
44+
scope: constant.language.boolean.json
45+
46+
####[ Numbers ]#########################################################################################################
47+
48+
# TODO: maybe define decimal fraction
49+
# TODO: maybe define decimal exponent
50+
51+
numbers:
52+
- include: floats
53+
- include: integers
54+
55+
floats:
56+
- include: decimal-float
57+
58+
decimal-float:
59+
- include: explicitly-positive-decimal-float-json
60+
- include: decimal-float-json
61+
62+
explicitly-positive-decimal-float-json:
63+
- match: |-
64+
(?x: # ignore whitespace
65+
([+])
66+
(
67+
{{pos_integer_decimal}}
68+
(?:
69+
(?:(\.)\d+){{optional_exponent}}
70+
|
71+
{{exponent}}
72+
)
73+
)
74+
)
75+
scope: invalid.illegal.unrecognized-float-decimal.json
76+
captures:
77+
1: keyword.operator.arithmetic.json
78+
2: constant.numeric.value.json
79+
3: punctuation.separator.decimal.json
80+
81+
decimal-float-json:
82+
- match: |-
83+
(?x: # ignore whitespace
84+
({{optional_neg_sign}})
85+
(
86+
{{pos_integer_decimal}}
87+
(?:
88+
(?:(\.)\d+){{optional_exponent}}
89+
|
90+
{{exponent}}
91+
)
92+
)
93+
)
94+
scope: meta.number.float.decimal.json
95+
captures:
96+
1: keyword.operator.arithmetic.json
97+
2: constant.numeric.value.json
98+
3: punctuation.separator.decimal.json
99+
100+
integers:
101+
- include: decimal-integer
102+
103+
decimal-integer:
104+
- include: explicitly-positive-decimal-integer-json
105+
- include: decimal-integer-json
106+
107+
explicitly-positive-decimal-integer-json:
108+
- match: '([+])({{pos_integer_decimal}})'
109+
scope: invalid.illegal.unrecognized-integer-decimal.json
110+
captures:
111+
1: keyword.operator.arithmetic.json
112+
2: constant.numeric.value.json
113+
114+
decimal-integer-json:
115+
- match: '({{optional_neg_sign}})({{pos_integer_decimal}})'
116+
scope: meta.number.integer.decimal.json
117+
captures:
118+
1: keyword.operator.arithmetic.json
119+
2: constant.numeric.value.json
120+
121+
####[ Strings ]#########################################################################################################
122+
123+
strings:
124+
- include: double-quoted-string-ahead
125+
126+
double-quoted-string-ahead:
127+
- match: '(?=\")'
128+
push: double-quoted-string
129+
130+
double-quoted-string:
131+
- meta_scope: meta.string.json string.quoted.double.json
132+
- match: '"'
133+
scope: punctuation.definition.string.begin.json
134+
set: inside-double-quoted-string
135+
136+
inside-double-quoted-string:
137+
- meta_scope: meta.string.json string.quoted.double.json
138+
- meta_include_prototype: false # for inheriting syntaxes
139+
- match: '"'
140+
scope: punctuation.definition.string.end.json
141+
pop: 1
142+
- include: double-quoted-string-escape-characters
143+
- match: \n
144+
scope: invalid.illegal.unclosed-string.json
145+
pop: 1
146+
147+
double-quoted-string-escape-characters:
148+
- match: \\\"
149+
scope: constant.character.escape.double-quote.json
150+
- include: string-escape-characters
151+
152+
string-escape-characters:
153+
- match: \\\\
154+
scope: constant.character.escape.back-slash.json
155+
- match: \\\/
156+
scope: constant.character.escape.forward-slash.json
157+
- match: \\b
158+
scope: constant.character.escape.backspace.json
159+
- match: \\f
160+
scope: constant.character.escape.form-feed.json
161+
- match: \\n
162+
scope: constant.character.escape.newline.json # linefeed
163+
- match: \\r
164+
scope: constant.character.escape.carriage-return.json
165+
- match: \\t
166+
scope: constant.character.escape.horizontal-tab.json
167+
- match: \\u[0-9a-fA-F]{4}
168+
scope: constant.character.escape.unicode-symbol.json
169+
- match: \\.
170+
scope: invalid.illegal.unrecognized-string-escape.json
171+
172+
####[ Sequence ]########################################################################################################
173+
174+
# FIXME: move trailing commas to JSONC
175+
176+
sequence:
177+
- match: \[
178+
scope: punctuation.definition.sequence.begin.json
179+
push: meta-sequence
180+
181+
meta-sequence:
182+
- meta_scope: meta.sequence.json
183+
- match: \]
184+
scope: punctuation.definition.sequence.end.json
185+
pop: 1
186+
- include: any
187+
- match: ','
188+
scope: punctuation.separator.sequence.json
189+
- match: '[^\s\]]'
190+
scope: invalid.illegal.expected-sequence-separator.json
191+
192+
####[ Mapping ]#########################################################################################################
193+
194+
# FIXME: move trailing commas to JSONC
195+
196+
mapping:
197+
- match: \{
198+
scope: punctuation.definition.mapping.begin.json
199+
push: meta-mapping
200+
201+
meta-mapping:
202+
- meta_scope: meta.mapping.json
203+
- match: \}
204+
scope: punctuation.definition.mapping.end.json
205+
pop: 1
206+
- include: mapping-key
207+
- include: mapping-separator
208+
- match: '[^\s\}]'
209+
scope: invalid.illegal.expected-mapping-key.json
210+
211+
mapping-key:
212+
- match: '"'
213+
scope: punctuation.definition.string.begin.json
214+
push: mapping-key-double-quoted
215+
216+
mapping-key-double-quoted:
217+
- clear_scopes: 1
218+
- meta_scope: meta.mapping.key.json meta.string.json string.quoted.double.json
219+
- meta_include_prototype: false # for inheriting syntaxes
220+
- include: inside-double-quoted-string
221+
222+
mapping-separator:
223+
- match: ':'
224+
scope: punctuation.separator.mapping.key-value.json
225+
push: mapping-expect-value
226+
227+
mapping-expect-value:
228+
- match: ',|\s?(?=\})'
229+
scope: invalid.illegal.expected-mapping-value.json
230+
pop: 1
231+
- match: (?=\S)
232+
set: mapping-value
233+
234+
mapping-value:
235+
- clear_scopes: 1
236+
- meta_scope: meta.mapping.value.json
237+
- include: any
238+
- match: ''
239+
set:
240+
- match: ','
241+
scope: punctuation.separator.mapping.pair.json
242+
pop: 1
243+
- match: \s*(?=\})
244+
pop: 1
245+
- match: \s(?!/[/*])(?=[^\s,])|[^\s,]
246+
scope: invalid.illegal.expected-mapping-separator.json
247+
pop: 1

0 commit comments

Comments
 (0)