File: common.rb

package info (click to toggle)
ruby-rugments 1.0.0~beta8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 820 kB
  • sloc: ruby: 10,293; makefile: 2
file content (172 lines) | stat: -rw-r--r-- 4,155 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
module Rugments
  module Lexers
    # shared states with SCSS
    class SassCommon < RegexLexer
      id = /[\w-]+/

      state :content_common do
        rule /@for\b/, Keyword, :for
        rule /@(debug|warn|if|while)/, Keyword, :value
        rule /(@mixin)(\s+)(#{id})/ do
          groups Keyword, Text, Name::Function
          push :value
        end

        rule /@extend\b/, Keyword, :selector

        rule /(@include)(\s+)(#{id})/ do
          groups Keyword, Text, Name::Decorator
          push :value
        end

        rule /@#{id}/, Keyword, :selector

        # $variable: assignment
        rule /([$]#{id})([ \t]*)(:)/ do
          groups Name::Variable, Text, Punctuation
          push :value
        end
      end

      state :value do
        mixin :end_section
        rule /[ \t]+/, Text
        rule /[$]#{id}/, Name::Variable
        rule /url[(]/, Str::Other, :string_url
        rule /#{id}(?=\s*[(])/, Name::Function
        rule /%#{id}/, Name::Decorator

        # named literals
        rule /(true|false)\b/, Name::Builtin::Pseudo
        rule /(and|or|not)\b/, Operator::Word

        # colors and numbers
        rule /#[a-z0-9]{1,6}/i, Num::Hex
        rule /-?\d+(%|[a-z]+)?/, Num
        rule /-?\d*\.\d+(%|[a-z]+)?/, Num::Integer

        mixin :has_strings
        mixin :has_interp

        rule /[~^*!&%<>\|+=@:,.\/?-]+/, Operator
        rule /[\[\]()]+/, Punctuation
        rule %r{/[*]}, Comment::Multiline, :inline_comment
        rule %r{//[^\n]*}, Comment::Single

        # identifiers
        rule(id) do |m|
          if CSS.builtins.include? m[0]
            token Name::Builtin
          elsif CSS.constants.include? m[0]
            token Name::Constant
          else
            token Name
          end
        end
      end

      state :has_interp do
        rule /[#][{]/, Str::Interpol, :interpolation
      end

      state :has_strings do
        rule /"/, Str::Double, :dq
        rule /'/, Str::Single, :sq
      end

      state :interpolation do
        rule /}/, Str::Interpol, :pop!
        mixin :value
      end

      state :selector do
        mixin :end_section

        mixin :has_strings
        mixin :has_interp
        rule /[ \t]+/, Text
        rule /:/, Name::Decorator, :pseudo_class
        rule /[.]/, Name::Class, :class
        rule /#/, Name::Namespace, :id
        rule /%/, Name::Variable, :placeholder
        rule id, Name::Tag
        rule /&/, Keyword
        rule /[~^*!&\[\]()<>\|+=@:;,.\/?-]/, Operator
      end

      state :dq do
        rule /"/, Str::Double, :pop!
        mixin :has_interp
        rule /(\\.|#(?![{])|[^\n"#])+/, Str::Double
      end

      state :sq do
        rule /'/, Str::Single, :pop!
        mixin :has_interp
        rule /(\\.|#(?![{])|[^\n'#])+/, Str::Single
      end

      state :string_url do
        rule /[)]/, Str::Other, :pop!
        rule /(\\.|#(?![{])|[^\n)#])+/, Str::Other
        mixin :has_interp
      end

      state :selector_piece do
        mixin :has_interp
        rule(//) { pop! }
      end

      state :pseudo_class do
        rule id, Name::Decorator
        mixin :selector_piece
      end

      state :class do
        rule id, Name::Class
        mixin :selector_piece
      end

      state :id do
        rule id, Name::Namespace
        mixin :selector_piece
      end

      state :placeholder do
        rule id, Name::Variable
        mixin :selector_piece
      end

      state :for do
        rule /(from|to|through)/, Operator::Word
        mixin :value
      end

      state :attr_common do
        mixin :has_interp
        rule id do |m|
          if CSS.attributes.include? m[0]
            token Name::Label
          else
            token Name::Attribute
          end
        end
      end

      state :attribute do
        mixin :attr_common

        rule /([ \t]*)(:)/ do
          groups Text, Punctuation
          push :value
        end
      end

      state :inline_comment do
        rule /(\\#|#(?=[^\n{])|\*(?=[^\n\/])|[^\n#*])+/, Comment::Multiline
        mixin :has_interp
        rule %r{[*]/}, Comment::Multiline, :pop!
      end
    end
  end
end