-
Notifications
You must be signed in to change notification settings - Fork 57
/
pygments_json.py
71 lines (57 loc) · 1.96 KB
/
pygments_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from pygments.lexer import include, RegexLexer
from pygments.token import Token
import re
# JSON lexer, for its use, please use ".. code-block:: json" syntax
class JSONLexer(RegexLexer):
"""
Lexer for `JSON files`
"""
name = 'JSON Lexer'
aliases = ['json']
filenames = ['*.json']
mimetypes = ['application/json']
flags = re.DOTALL
tokens = {
'whitespace': [
(r'\s+', Token.Text),
],
# represents a simple terminal value
'simplevalue':[
(r'(true|false|null)\b', Token.Keyword.Constant),
(r'-?[0-9]+', Token.Number.Integer),
(r'"(\\\\|\\"|[^"])*"', Token.String.Double),
],
# the right hand side of an object, after the attribute name
'objectattribute': [
include('value'),
(r':', Token.Punctuation),
# comma terminates the attribute but expects more
(r',', Token.Punctuation, '#pop'),
# a closing bracket terminates the entire object, so pop twice
(r'}', Token.Punctuation, ('#pop', '#pop')),
],
# a json object - { attr, attr, ... }
'objectvalue': [
include('whitespace'),
(r'"(\\\\|\\"|[^"])*"', Token.Name.Tag, 'objectattribute'),
(r'}', Token.Punctuation, '#pop'),
],
# json array - [ value, value, ... }
'arrayvalue': [
include('whitespace'),
include('value'),
(r',', Token.Punctuation),
(r']', Token.Punctuation, '#pop'),
],
# a json value - either a simple value or a complex value (object or array)
'value': [
include('whitespace'),
include('simplevalue'),
(r'{', Token.Punctuation, 'objectvalue'),
(r'\[', Token.Punctuation, 'arrayvalue'),
],
# the root of a json document would be a value
'root': [
include('value'),
],
}