summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rouge-4.5.2/lib/rouge/lexers/json.rb
blob: 85228bf43e8b3d019aec5e0f1a9568a2e493e12a (plain)
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
72
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
  module Lexers
    class JSON < RegexLexer
      title 'JSON'
      desc "JavaScript Object Notation (json.org)"
      tag 'json'
      filenames '*.json', 'Pipfile.lock'
      mimetypes 'application/json', 'application/vnd.api+json',
                'application/hal+json', 'application/problem+json',
                'application/schema+json'

      state :whitespace do
        rule %r/\s+/, Text::Whitespace
      end

      state :root do
        mixin :whitespace
        rule %r/{/, Punctuation, :object
        rule %r/\[/, Punctuation, :array

        mixin :name
        mixin :value

        # These characters may be invalid but syntax correctness is a non-goal
        rule %r/[\]}]/, Punctuation
      end

      state :object do
        mixin :whitespace
        mixin :name
        mixin :value
        rule %r/}/, Punctuation, :pop!
        rule %r/,/, Punctuation
      end

      state :name do
        rule %r/("(?:\\.|[^"\\\n])*?")(\s*)(:)/ do
          groups Name::Label, Text::Whitespace, Punctuation
        end
      end

      state :value do
        mixin :whitespace
        mixin :constants
        rule %r/"/, Str::Double, :string
        rule %r/\[/, Punctuation, :array
        rule %r/{/, Punctuation, :object
      end

      state :string do
        rule %r/[^\\"]+/, Str::Double
        rule %r/\\./, Str::Escape
        rule %r/"/, Str::Double, :pop!
      end

      state :array do
        mixin :value
        rule %r/\]/, Punctuation, :pop!
        rule %r/,/, Punctuation
      end

      state :constants do
        rule %r/(?:true|false|null)/, Keyword::Constant
        rule %r/-?(?:0|[1-9]\d*)\.\d+(?:e[+-]?\d+)?/i, Num::Float
        rule %r/-?(?:0|[1-9]\d*)(?:e[+-]?\d+)?/i, Num::Integer
      end
    end
  end
end