<?xml version="1.0" encoding="UTF-8"?>
<!--

 Authors: David Perez Cabrera <dperezcabrera@gmail.com>
 Copyright (C) 2019 David Perez Cabrera <dperezcabrera@gmail.com>

 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="docker" name="Dockerfile" version="2.0" _section="Source">
  <metadata>
    <property name="mimetypes">application/docker;text/docker</property>
    <property name="globs">Dockerfile</property>
    <property name="line-comment-start">#</property>
  </metadata>

  <styles>
    <style id="comment" name="Comment" map-to="def:comment"/>
    <style id="keyword" name="Keyword" map-to="def:keyword"/>
    <style id="operator" name="Operator" map-to="def:operator"/>
    <style id="string" name="String" map-to="def:string"/>
    <style id="variable" name="Variable" map-to="def:special-char"/>
    <style id="variable-definition" name="Variable Definition" map-to="docker:variable"/>
    <style id="user" name="User" map-to="def:identifier"/>
    <style id="alias-definition" name="Alias definition" map-to="def:identifier"/>
    <style id="number" name="Number" map-to="def:number"/>
  </styles>

  <definitions>

    <context id="operator">
      <match>(\s*)(&amp;&amp;|\|\|)(\s*)</match>
      <include>
        <context sub-pattern="2" style-ref="operator"/>
      </include>
    </context>

    <context id="short-parameter-expansion" style-ref="variable">
      <match>\$([!@?*#\$0-9]|[a-zA-Z0-9_]+)</match>
    </context>

    <context id="parameter-expansion" style-ref="variable">
      <start>\$\{</start>
      <end>\}</end>
      <include>
        <context ref="single-quoted-string"/>
        <context ref="double-quoted-string"/>
        <context ref="variable"/>
      </include>
    </context>

    <context id="variable">
      <include>
        <context ref="short-parameter-expansion"/>
        <context ref="parameter-expansion"/>
      </include>
    </context>

    <context id="user">
      <match>^\s*(USER)\s+([a-zA-Z_][a-zA-Z0-9_]*)</match>
      <include>
        <context sub-pattern="2" style-ref="user"/>
        <context sub-pattern="1" style-ref="keyword"/>
      </include>
    </context>

    <context id="alias-definition">
      <match>\s+(AS)\s+([a-zA-Z][a-zA-Z0-9_]*)</match>
      <include>
        <context sub-pattern="2" style-ref="user"/>
        <context sub-pattern="1" style-ref="keyword"/>
      </include>
    </context>

    <context id="alias-usage">
      <match>^\s*(COPY\s+\-\-from)\=([a-zA-Z][a-zA-Z0-9_]*)</match>
      <include>
        <context sub-pattern="2" style-ref="user"/>
        <context sub-pattern="1" style-ref="keyword"/>
      </include>
    </context>


    <context id="from-image">
      <match>^\s*(FROM)\s+([a-zA-Z][a-zA-Z0-9_/\:\-\.]*)</match>
      <include>
        <context sub-pattern="2" style-ref="user"/>
        <context sub-pattern="1" style-ref="keyword"/>
      </include>
    </context>

    <context id="variable-definition">
      <match>^\s*(ARG|ENV)\s+([a-zA-Z_][a-zA-Z0-9_]*)(\=|\s+)</match>
      <include>
        <context sub-pattern="2" style-ref="variable-definition"/>
        <context sub-pattern="1" style-ref="keyword"/>
      </include>
    </context>

    <context id="comment" end-at-line-end="true" style-ref="comment" class-disabled="no-spell-check">
      <start>#</start>
    </context>

    <context id="single-quoted-string" style-ref="string">
      <start>'</start>
      <end>'</end>
    </context>

    <context id="double-quoted-string" style-ref="string" class="string" class-disabled="no-spell-check">
      <start>"</start>
      <end>"</end>
    </context>

    <context id="numeric" style-ref="number">
      <match extended="true">
          (\b([0-9]+|0[xX][0-9a-fA-F]+)[Ll]?\b|
          \b(([0-9]+[Ee][-]?[0-9]+|
          ([0-9]*\.[0-9]+|[0-9]+\.)([Ee][-]?[0-9]+)?)[fFdD]?|
          [0-9]+[FfDd]))
      </match>
    </context>

    <context id="keywords" style-ref="keyword">
      <keyword>AS</keyword>
      <keyword>ADD</keyword>
      <keyword>ARG</keyword>
      <keyword>CMD</keyword>
      <keyword>COPY</keyword>
      <keyword>ENTRYPOINT</keyword>
      <keyword>ENV</keyword>
      <keyword>EXPOSE</keyword>
      <keyword>FROM</keyword>
      <keyword>HEALTHCHECK</keyword>
      <keyword>LABEL</keyword>
      <keyword>MAINTAINER</keyword>
      <keyword>ONBUILD</keyword>
      <keyword>RUN</keyword>
      <keyword>SHELL</keyword>
      <keyword>STOPSIGNAL</keyword>
      <keyword>USER</keyword>
      <keyword>VOLUME</keyword>
      <keyword>WORKDIR</keyword>
    </context>

    <context id="docker" class="no-spell-check">
      <include>
        <context ref="comment"/>
        <context ref="single-quoted-string"/>
        <context ref="double-quoted-string"/>
        <context ref="numeric"/>
        <context ref="variable"/>
        <context ref="variable-definition"/>
        <context ref="user"/>
        <context ref="alias-definition"/>
        <context ref="alias-usage"/>
        <context ref="from-image"/>
        <context ref="operator"/>
        <context ref="keywords"/>
      </include>
    </context>
  </definitions>
</language>
