summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rouge-4.5.2/lib/rouge/lexers/terraform.rb
blob: 2a26a6d0be66a76f8d7356a5b6577d3d871234b6 (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
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
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
  module Lexers
    load_lexer 'hcl.rb'

    class Terraform < Hcl
      title "Terraform"
      desc "Terraform HCL Interpolations"

      tag 'terraform'
      aliases 'tf'
      filenames '*.tf'

      def self.keywords
        @keywords ||= Set.new %w(
          terraform module provider variable resource data provisioner output
        )
      end

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

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

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

      def self.builtins
        @builtins ||= %w()
      end

      state :strings do
        rule %r/\\./, Str::Escape
        rule %r/(\$[\$]+|%[%]+)(\{)/, Str
        rule %r/\$\{/ do
          token Punctuation
          push :interpolation
        end
      end

      state :dq do
        rule %r/[^\\"\$]+/, Str::Double
        mixin :strings
        rule %r/"/, Str::Double, :pop!
      end

      state :sq do
        rule %r/[^\\'\$]+/, Str::Single
        mixin :strings
        rule %r/'/, Str::Single, :pop!
      end

      state :heredoc do
        rule %r/\n/, Str::Heredoc, :heredoc_nl
        rule %r/[^$\n]+/, Str::Heredoc
        rule %r/[$]/, Str::Heredoc
        mixin :strings
      end

      state :interpolation do
        rule %r/\}/ do
          token Punctuation
          pop!
        end

        mixin :expression
      end

      state :regexps do
        rule %r/"\// do
          token Str::Delimiter
          goto :regexp_inner
        end
      end

      state :regexp_inner do
        rule %r/[^"\/\\]+/, Str::Regex
        rule %r/\\./, Str::Regex
        rule %r/\/"/, Str::Delimiter, :pop!
        rule %r/["\/]/, Str::Regex
      end

      id = /[$a-z_\-][a-z0-9_\-]*/io

      state :expression do
        mixin :regexps
        mixin :primitives
        rule %r/\s+/, Text

        rule %r(\+\+ | -- | ~ | && | \|\| | \\(?=\n) | << | >>>? | == | != )x, Operator
        rule %r([-<>+*%&|\^/!=?:]=?), Operator
        rule %r/[(\[,]/, Punctuation
        rule %r/[)\].]/, Punctuation

        rule id do |m|
          if self.class.keywords.include? m[0]
            token Keyword
          elsif self.class.declarations.include? m[0]
            token Keyword::Declaration
          elsif self.class.reserved.include? m[0]
            token Keyword::Reserved
          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
    end
  end
end