groovy.lang 11.5 KB
Newer Older
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 173 174 175 176
<?xml version="1.0" encoding="UTF-8"?>
<!--

 This file is part of GtkSourceView

 Copyright (C) 2003 Gustavo Giráldez <gustavo.giraldez@gmx.net>
 Copyright (C) 2006 Jeff Walden <jwalden@mit.edu>
 Copyright (C) 2017 Roman Donchenko

 GtkSourceView is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 GtkSourceView is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, see <http://www.gnu.org/licenses/>.

-->
<language id="groovy" name="Groovy" version="2.0" _section="Source">
  <metadata>
    <property name="globs">*.groovy</property>
    <property name="line-comment-start">//</property>
    <property name="block-comment-start">/*</property>
    <property name="block-comment-end">*/</property>
  </metadata>

  <styles>
    <style id="comment"           name="Comment"                   map-to="def:comment"/>
    <style id="escaped-character" name="Escaped Character"         map-to="def:special-char"/>
    <style id="interpolation"     name="Interpolation Syntax"      map-to="def:special-char"/>
    <style id="interpolated"      name="Interpolated Expression"   map-to="def:identifier"/>
    <style id="string"            name="String"                    map-to="def:string"/>
    <style id="external"          name="External"                  map-to="def:preprocessor"/>
    <style id="declaration"       name="Declaration"               map-to="def:type"/>
    <style id="storage-class"     name="Storage Class"             map-to="def:type"/>
    <style id="scope-declaration" name="Scope Declaration"         map-to="def:type"/>
    <style id="operator"          name="Operator"                  map-to="def:operator"/>
    <style id="keyword"           name="Keyword"                   map-to="def:keyword"/>
    <style id="null-value"        name="Null Value"                map-to="def:special-constant"/>
    <style id="boolean"           name="Boolean value"             map-to="def:boolean"/>
    <style id="number"            name="Number"                    map-to="def:number"/>
    <style id="type"              name="Data Type"                 map-to="def:type"/>
  </styles>

  <definitions>
    <define-regex id="escaped-character" extended="true">
      \\(
        # character escape
        [nrtbf"'\\$] |
        # unicode escape
        u[0-9A-Fa-f]{4} |
        # octal escape
        [0-3] ([0-7] [0-7]?)? | [4-7] [0-7]?
      )
    </define-regex>

    <define-regex id="letter" extended="true">
      [a-zA-Z\x{c0}-\x{d6}\x{d8}-\x{f6}\x{f8}-\x{ff}\x{100}-\x{fffe}_]
    </define-regex>

    <context id="escaped-character">
      <include>
        <context style-ref="escaped-character">
          <match>\%{escaped-character}</match>
        </context>
        <context ref="def:line-continue"/>
        <context style-ref="def:error">
          <!-- backslashes not part of a valid escape sequence are erroneous -->
          <match>\\</match>
        </context>
      </include>
    </context>

    <context id="placeholder">
      <include>
        <context style-ref="interpolated">
          <match extended="true">
            (\$)
            # one or more dollarless identifiers separated by dots
            \%{letter} (\%{letter} | \d)*
            (\. \%{letter} (\%{letter} | \d)*)*
          </match>
          <include>
            <context sub-pattern="1" style-ref="interpolation"/>
          </include>
        </context>
        <context style-ref="interpolated">
          <start>\$\{</start>
          <end>\}</end>
          <include>
            <context sub-pattern="0" where="start" style-ref="interpolation"/>
            <context sub-pattern="0" where="end" style-ref="interpolation"/>
            <context ref="groovy"/>
          </include>
        </context>
      </include>
    </context>

    <!-- a dollar that isn't part of a placeholder is erroneous in some instances -->
    <context id="bad-placeholder" style-ref="def:error">
      <match>\$</match>
    </context>

    <context id="triple-single-quoted-string" style-ref="string" class="string" class-disabled="no-spell-check">
      <start>'''</start>
      <end>'''</end>
      <include>
        <context ref="escaped-character"/>
      </include>
    </context>

    <context id="single-quoted-string" end-at-line-end="true" style-ref="string" class="string" class-disabled="no-spell-check">
      <start>'</start>
      <end>'</end>
      <include>
        <context ref="escaped-character"/>
      </include>
    </context>

    <context id="triple-double-quoted-string" style-ref="string" class="string" class-disabled="no-spell-check">
      <start>"""</start>
      <end>"""</end>
      <include>
        <context ref="escaped-character"/>
        <context ref="placeholder"/>
        <context ref="bad-placeholder"/>
      </include>
    </context>

    <context id="double-quoted-string" end-at-line-end="true" style-ref="string" class="string" class-disabled="no-spell-check">
      <start>"</start>
      <end>"</end>
      <include>
        <context ref="escaped-character"/>
        <context ref="placeholder"/>
        <context ref="bad-placeholder"/>
      </include>
    </context>

    <context id="slashy-string" style-ref="string" class="string">
      <start extended="true">
        # The initial slash must not be preceded by a token that can end an expression.
        # Otherwise, it's interpreted as a division operator, not the start of a slashy string.
        # It'd be complicated to verify whole tokens with a regex, but the last non-space
        # character is a good indicator by itself. We can use a negative lookbehind assertion
        # to verify that it's not a character that an expression-ending token can end with.

        # Trouble is, a lookbehind assertion has to consist of fixed-length alternatives, so
        # we can't have it match an arbitrary amount of whitespace. Thus, we do an approximate
        # check, only trying zero and one spaces.

        (?&lt;! \+\+   | --   | [])}"\d]   | \%{letter} |
                \+\+\s | --\s | [])}"\d]\s | \%{letter}\s )

        /
      </start>
      <end>/</end>
      <include>
        <context style-ref="escaped-character">
          <match>\\/</match>
        </context>
        <context ref="def:line-continue"/>
        <context ref="placeholder"/>
        <!-- standalone dollars and backslashes are interpreted literally -->
      </include>
    </context>

    <context id="dollar-slashy-string" style-ref="string" class="string">
      <start extended="true">
        \$/

177 178 179
        # Dollar slashy strings can't be empty (a would-be empty one is parsed
        # as a dollar followed by a single-line comment instead).
        (?! /\$)
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
      </start>
      <end>/\$</end>
      <include>
        <context style-ref="escaped-character">
          <match>\$[$/]</match>
        </context>
        <context ref="def:line-continue"/>
        <context ref="placeholder"/>
      </include>
    </context>

    <context id="numeric" style-ref="number">
      <match extended="true">
        \b (
          # floating-point
          \d ([\d_]* \d)? (
            \. \d ([\d_]* \d)? ([eE] [+-]? [\d_]* \d)? [dDfFgG]? |
            [eE] [+-]? [\d_]* \d [dDfFgG]? |
            [dDfF]
          ) |
          # integer
          (
            0 | # decimal zero
            0[bB] [01] ([01_]* [01])? | # binary
            0 [0-7] ([0-7_]* [0-7])? | # octal
            [1-9] ([\d_]* \d)? | # decimal
            0[xX] [\da-fA-F] ([\da-fA-F_]* [\da-fA-F])? # hexadecimal
          ) [iIlLgG]?
        ) \b
      </match>
    </context>

    <!--
      Some of the Java keywords are reserved in Groovy. We don't mark them
      with a special style, though, because in some instances keywords can
      be used as identifiers (e.g. when used as a member name), and even
      reserved keywords are valid when used like that.
    -->

    <context id="externals" style-ref="external">
      <keyword>import</keyword>
      <keyword>package</keyword>
    </context>

    <context id="declarations" style-ref="declaration">
      <keyword>class</keyword>
      <keyword>enum</keyword>
      <keyword>extends</keyword>
      <keyword>implements</keyword>
      <keyword>interface</keyword>
      <keyword>native</keyword>
      <keyword>throws</keyword>
      <keyword>trait</keyword>
    </context>

    <context id="primitive-types" style-ref="type">
      <keyword>boolean</keyword>
      <keyword>byte</keyword>
      <keyword>char</keyword>
      <keyword>def</keyword>
      <keyword>double</keyword>
      <keyword>float</keyword>
      <keyword>int</keyword>
      <keyword>long</keyword>
      <keyword>short</keyword>
      <keyword>void</keyword>
    </context>

    <context id="storage-class" style-ref="storage-class">
      <keyword>abstract</keyword>
      <keyword>const</keyword>
      <keyword>final</keyword>
      <keyword>static</keyword>
      <keyword>strictfp</keyword>
      <keyword>synchronized</keyword>
      <keyword>transient</keyword>
      <keyword>volatile</keyword>
    </context>

    <context id="scope-declarations" style-ref="scope-declaration">
      <keyword>private</keyword>
      <keyword>protected</keyword>
      <keyword>public</keyword>
    </context>

    <context id="flow" style-ref="keyword">
      <keyword>assert</keyword>
      <keyword>break</keyword>
      <keyword>case</keyword>
      <keyword>catch</keyword>
      <keyword>continue</keyword>
      <keyword>default</keyword>
      <keyword>do</keyword>
      <keyword>else</keyword>
      <keyword>finally</keyword>
      <keyword>for</keyword>
      <keyword>goto</keyword>
      <keyword>if</keyword>
      <keyword>return</keyword>
      <keyword>throw</keyword>
      <keyword>switch</keyword>
      <keyword>try</keyword>
      <keyword>while</keyword>
    </context>

    <context id="operator" style-ref="operator">
      <keyword>as</keyword>
      <keyword>in</keyword>
      <keyword>instanceof</keyword>
      <keyword>new</keyword>
      <keyword>super</keyword>
      <keyword>this</keyword>
    </context>

    <context id="null-value" style-ref="null-value">
      <keyword>null</keyword>
    </context>

    <context id="boolean" style-ref="boolean">
      <keyword>false</keyword>
      <keyword>true</keyword>
    </context>

    <context id="groovy" class="no-spell-check">
      <include>
        <context ref="def:shebang" style-ref="comment"/>
        <context ref="def:c-like-comment" style-ref="comment"/>
        <context ref="def:c-like-comment-multiline" style-ref="comment"/>
        <context ref="def:line-continue"/>

        <context ref="triple-single-quoted-string"/>
        <context ref="single-quoted-string"/>
        <context ref="triple-double-quoted-string"/>
        <context ref="double-quoted-string"/>
        <context ref="slashy-string"/>
        <context ref="dollar-slashy-string"/>
        <context ref="numeric"/>

        <context ref="externals"/>
        <context ref="declarations"/>
        <context ref="primitive-types"/>
        <context ref="storage-class"/>
        <context ref="scope-declarations"/>
        <context ref="flow"/>
        <context ref="operator"/>
        <context ref="null-value"/>
        <context ref="boolean"/>
      </include>
    </context>
  </definitions>
</language>