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
|
module CodeRay
module Scanners
# Scanner for JSON (JavaScript Object Notation).
#
# See http://json.org/ for a definition of the JSON lexic/grammar.
class JSON5 < RuleBasedScanner
register_for :json5
file_extension 'json'
KINDS_NOT_LOC = [
:float, :char, :content, :delimiter,
:error, :integer, :operator, :value,
] # :nodoc:
ESCAPE = / [bfnrt\\"\/] /x # :nodoc:
UNICODE_ESCAPE = / u[a-fA-F0-9]{4} /x # :nodoc:
KEY = / (?> (?: [^\\"]+ | \\. )* ) " \s* : /mx
state :initial do
on %r/ \s+ /x, :space
on %r/ [:,\[{\]}] /x, :operator
on %r/ " (?=#{KEY}) /x, push(:key), :delimiter
on %r/ " /x, push(:string), :delimiter
on %r/ true | false | null /x, :value
on %r/ -? (?: 0 | [1-9]\d* ) (?: \.\d+ (?: e[-+]? \d+ )? | e[-+]? \d+ ) /ix, :float
on %r/ -? (?: 0 | [1-9]\d* ) (?: e[+-] \d+ )? /ix, :integer
end
state :key, :string do
on %r/ [^\\"]+ /x, :content
on %r/ " /x, :delimiter, pop
on %r/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /x, :char
on %r/ \\. /mx, :content
on %r/ \\ /x, :error, pop
end
def close_groups encoder, states
if [:string, :key].include? states.last
encoder.end_group states.last
end
end
end
end
end
|