File: json.rb

package info (click to toggle)
ruby-rouge 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: ruby: 38,489; sed: 2,071; perl: 152; makefile: 8
file content (72 lines) | stat: -rw-r--r-- 1,836 bytes parent folder | download | duplicates (4)
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