File: json5.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 (74 lines) | stat: -rw-r--r-- 1,801 bytes parent folder | download | duplicates (2)
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
73
74
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
  module Lexers
    load_lexer 'json.rb'

    class JSON5 < JSON
      title 'JSON 5'
      tag 'json5'
      filenames '*.json5'
      mimetypes 'application/json5', 'application/x-json5'

      desc 'JSON 5 extension for JSON (json5.org)'

      append :whitespace do
        rule %r://.*$:, Comment

        # comments are non-nesting, so a single regex will do
        rule %r:/[*].*?[*]/:m, Comment
      end

      prepend :name do
        rule Javascript.id_regex, Name::Label
        rule %r/".*?"/, Name::Label
      end

      # comments can appear between keys and :, so we have to
      # manage our states a little more carefully
      append :object do
        rule %r/:/ do
          token Punctuation
          goto :object_value
        end
      end

      state :object_value do
        mixin :value
        rule %r/,/ do
          token Punctuation
          goto :object
        end

        rule %r/}/, Punctuation, :pop!
      end

      append :value do
        rule %r/'/, Str::Single, :sstring
      end

      state :sstring do
        rule %r/[^\\']+/, Str::Single
        rule %r/\\./m, Str::Escape
        rule %r/'/, Str::Single, :pop!
      end

      # can escape newlines
      append :string do
        rule %r/\\./m, Str::Escape
      end

      # override: numbers are very different in json5
      state :constants do
        rule %r/\b(?:true|false|null)\b/, Keyword::Constant
        rule %r/[+-]?\b(?:Infinity|NaN)\b/, Keyword::Constant
        rule %r/[+-]?0x\h+/i, Num::Hex

        rule %r/[+-.]?[0-9]+[.]?[0-9]?([eE][-]?[0-9]+)?/i, Num::Float
        rule %r/[+-]?\d+e[+-]?\d+/, Num::Integer
        rule %r/[+-]?(?:0|[1-9]\d*)(?:e[+-]?\d+)?/i, Num::Integer
      end
    end
  end
end