File: jsonnet.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 (153 lines) | stat: -rw-r--r-- 3,378 bytes parent folder | download | duplicates (3)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
  module Lexers
    class Jsonnet < RegexLexer
      title 'Jsonnet'
      desc 'An elegant, formally-specified config language for JSON'
      tag 'jsonnet'
      filenames '*.jsonnet', '*.libsonnet'
      mimetypes 'text/x-jsonnet'

      def self.keywords
        @keywords ||= Set.new %w(
          self super local for in if then else import importstr error
          tailstrict assert
        )
      end

      def self.declarations
        @declarations ||= Set.new %w(
          function
        )
      end

      def self.constants
        @constants ||= Set.new %w(
          null true false
        )
      end

      def self.builtins
        @builtins ||= Set.new %w(
          acos
          asin
          atan
          ceil
          char
          codepoint
          cos
          exp
          exponent
          filter
          floor
          force
          length
          log
          makeArray
          mantissa
          objectFields
          objectHas
          pow
          sin
          sqrt
          tan
          thisFile
          type
          abs
          assertEqual
          escapeStringBash
          escapeStringDollars
          escapeStringJson
          escapeStringPython
          filterMap
          flattenArrays
          foldl
          foldr
          format
          join
          lines
          manifestIni
          manifestPython
          manifestPythonVars
          map
          max
          min
          mod
          range
          set
          setDiff
          setInter
          setMember
          setUnion
          sort
          split
          stringChars
          substr
          toString
          uniq
        )
      end

      identifier = /[a-zA-Z_][a-zA-Z0-9_]*/

      state :root do
        rule %r/\s+/, Text
        rule %r(//.*?$), Comment::Single
        rule %r(#.*?$), Comment::Single
        rule %r(/\*.*?\*/)m, Comment::Multiline

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

        rule %r/[{}:\.,;+\[\]=%\(\)]/, Punctuation

        rule %r/"/, Str, :string_double
        rule %r/'/, Str, :string_single
        rule %r/\|\|\|/, Str, :string_block

        rule %r/\$/, Keyword

        rule identifier do |m|
          if self.class.keywords.include? m[0]
            token Keyword
          elsif self.class.declarations.include? m[0]
            token Keyword::Declaration
          elsif self.class.constants.include? m[0]
            token Keyword::Constant
          elsif self.class.builtins.include? m[0]
            token Name::Builtin
          else
            token Name::Other
          end
        end
      end

      state :string do
        rule %r/\\([\\\/bfnrt]|(u[0-9a-fA-F]{4}))/, Str::Escape
        rule %r/\\./, Str::Escape
      end

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

      state :string_single do
        mixin :string
        rule %r/'/, Str, :pop!
        rule %r/[^\\']+/, Str
      end

      state :string_block do
        mixin :string
        rule %r/[|][|][|]/, Str, :pop!
        rule %r/[^|\\]+/, Str
        rule %r/[|]/, Str
      end
    end
  end
end