1
1
from pygments .lexer import include , RegexLexer
2
2
from pygments .token import Token
3
3
4
+
4
5
def _get_regex_from_options (options : list [str ]) -> str :
5
6
"""
6
7
Constructs a regular expression pattern allowing any string from the options list.
@@ -15,12 +16,14 @@ def _get_regex_from_options(options: list[str]) -> str:
15
16
regex_str = "(" + "|" .join (options ) + ")"
16
17
return regex_str
17
18
19
+
18
20
class JSONSchemaLexer (RegexLexer ):
19
21
"""
20
22
Lexer for JSON Schema syntax highlighting.
21
23
"""
24
+
22
25
name = "JSON Schema Lexer"
23
-
26
+
24
27
data_types = ["object" , "integer" , "string" , "number" , "array" , "boolean" , "null" ]
25
28
core_keywords = [
26
29
r"\$schema" ,
@@ -46,7 +49,7 @@ class JSONSchemaLexer(RegexLexer):
46
49
"additionalProperties" ,
47
50
"dependentSchemas" ,
48
51
"propertyNames" ,
49
- "prefixNames " ,
52
+ "prefixItems " ,
50
53
"contains" ,
51
54
"items" ,
52
55
]
@@ -81,7 +84,15 @@ class JSONSchemaLexer(RegexLexer):
81
84
"maxContains" ,
82
85
"uniqueItems" ,
83
86
]
84
- other_keywords = ["format" , "unevaluated" , "content" , "format_assertion" ]
87
+ other_keywords = [
88
+ "format" ,
89
+ "unevaluatedItems" ,
90
+ "unevaluatedProperties" ,
91
+ "contentEncoding" ,
92
+ "contentMediaType" ,
93
+ "contentSchema" ,
94
+ "format_assertion" ,
95
+ ]
85
96
86
97
tokens = {
87
98
"whitespace" : [
@@ -133,15 +144,13 @@ class JSONSchemaLexer(RegexLexer):
133
144
include ("meta_data_keywords" ),
134
145
include ("other_keywords" ),
135
146
],
136
-
137
147
# represents a simple terminal value
138
148
"simplevalue" : [
139
149
include ("data_types" ),
140
150
(r"(true|false)" , Token .Number ),
141
151
(r"-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?" , Token .Number .Integer ),
142
152
('"(\\ |"|[^"])*"' , Token .String .Double ),
143
153
],
144
-
145
154
# the right hand side of an object, after the attribute name
146
155
"objectattribute" : [
147
156
include ("value" ),
@@ -151,31 +160,27 @@ class JSONSchemaLexer(RegexLexer):
151
160
# a closing bracket terminates the entire object, so pop twice
152
161
(r"}" , Token .Punctuation , ("#pop" , "#pop" )),
153
162
],
154
-
155
163
# a json object - { attr, attr, ... }
156
164
"objectvalue" : [
157
165
include ("whitespace" ),
158
166
include ("keywords" ),
159
167
(r'"(\\\\|\\"|[^"])*"' , Token .Name .Tag , "objectattribute" ),
160
168
(r"}" , Token .Punctuation , "#pop" ),
161
169
],
162
-
163
170
# json array - [ value, value, ... }
164
171
"arrayvalue" : [
165
172
include ("whitespace" ),
166
173
include ("value" ),
167
174
(r"," , Token .Punctuation ),
168
175
(r"]" , Token .Punctuation , "#pop" ),
169
176
],
170
-
171
177
# a json value - either a simple value or a complex value (object or array)
172
178
"value" : [
173
179
include ("whitespace" ),
174
180
include ("simplevalue" ),
175
181
(r"{" , Token .Punctuation , "objectvalue" ),
176
182
(r"\[" , Token .Punctuation , "arrayvalue" ),
177
183
],
178
-
179
184
# the root of a json document whould be a value
180
185
"root" : [
181
186
include ("value" ),
0 commit comments