<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">

<!-- This a KDE syntax definition file for grammar displays in the
     Dafny reference manual, used by the Rouge syntax highligher
     when creating highlighted dafny code examples in pandoc-generated pdf files.e
     https://docs.kde.org/trunk5/en/applications/katepart/highlight.html
-->

<language name="grammar" version="3" kateversion="5.0" section="Sources" extensions="*.dfy" author="David Cok" license="MIT">

  <highlighting>

    <contexts>
      <context name="Normal" attribute="Normal Text" lineEndContext="#stay">
         <!-- NOTE: THE ORDER OF REGEXPR RULES MATTERS:  the first match wins.
              This fact can be used to simplify later rules,
              at the risk of introducing errors if the rules are reordered.  -->

        <!-- Either an entire attribute if it has no expressions, or just the opening and attribute name,
             so that the expressions get their own coloring -->

        <!-- Complicated set of rules to distinguish builtin types and identifiers -->
        <RegExpr attribute="Ident" String="(?:object[?])[a-zA-Z0-9_'][a-zA-Z0-9_'?]*" />
        <RegExpr attribute="Error" String="(?:seq|set|iset|map|imap|multiset)(?![&lt;0-9a-zA-Z_'?])" />
        <RegExpr attribute="Error" String="(?:seq|set|iset|map|imap|multiset)(?=\s+[^&lt;])" />
        <!-- Digit sequence ending in _ is an error -->
        <RegExpr attribute="Error" String="[0-9]+(?:_[0-9]+)*_(?![0-9])" />

        <!--      Normal quoted character, not followed by an further ident-characters, e.g. 'a' -->
        <RegExpr attribute="Char" String="\'[^\\]\'(?![a-zA-Z0-9'_?])" />
        <!--      Escape characters, not followed by any further ident-characters, e.g. '\n' -->
        <RegExpr attribute="Char" String="\'\\[0ntr'&quot;\\]\'(?![a-zA-Z0-9'_?])" />
        <!--      Unicode characters, not followed by any further ident-characters, e.g. '\u5555' -->
        <RegExpr attribute="Char" String="\'\\u[0-9A-Fa-f]{4}\'(?![a-zA-Z0-9'_?])" />
        <!--      Malformed unicode is an identifier, e.g. '\u2222X' '\u222' '\u222Z' -->

        <!-- Comments must be before operators -->
        <Detect2Chars attribute="Comment" char="/" char1="/" context="SingleLineComment" />
        <Detect2Chars attribute="Comment" char="/" char1="*" context="MultiLineComment" />

        <RegExpr attribute="Punctuation" String="[+-={}()\.\[\]]" />

        <!-- Openings for quoted strings -->
        <DetectChar attribute="String" char="&quot;" context="Quoted String" />

        <RegExpr attribute="Ident" String="[a-zA-Z0-9_'?][a-zA-Z0-9_'?]*" />

      </context>

      <context name="Quoted String" attribute="String" lineEndContext="#pop">
        <!-- string holds anything other than a double-quote or backslash -->
        <RegExpr attribute="String" String="[^&quot;\\]+" />
        <!-- standard 7 escape characters -->
        <RegExpr attribute="String" String="\\[0nrt'&quot;\\]" />
        <!-- unicode characters -->
        <RegExpr attribute="String" String="\\u[0-9A-Fa-f]{4}" />
        <!-- badly formed unicode character -->
        <RegExpr attribute="Error" String="\\u[0-9A-Fa-f]{0,3}(?![0-9A_Fa-f])" />
        <!-- badly formed escape -->
        <RegExpr attribute="Error" String="\\[^u0rnt\\'&quot;]" />
        <!-- unescaped quote ends string -->
        <DetectChar attribute="String" char="&quot;" context="#pop" />
      </context>

      <context name="SingleLineComment" attribute="Comment" lineEndContext="#pop"/>
      <context name="MultiLineComment" attribute="Comment" lineEndContext="#stay">
        <Detect2Chars attribute="Comment" context="MultiLineComment" char="/" char1="*"/>
        <Detect2Chars attribute="Comment" context="#pop" char="*" char1="/"/>
      </context>
      <context name="Documentation" attribute="Comment" lineEndContext="#pop" />
    </contexts>

    <itemDatas>
      <itemData name="Normal Text" defStyleNum="dsNormal" />

      <itemData name="Keyword" defStyleNum="dsKeyword" />
      <itemData name="Types" defStyleNum="dsDataType" />
      <itemData name="Assertions" defStyleNum="dsOthers" />

      <itemData name="Decimal" defStyleNum="dsDecVal" />
      <itemData name="BaseN" defStyleNum="dsBaseN" />
      <itemData name="Float" defStyleNum="dsFloat" />
      <itemData name="Char" defStyleNum="dsChar" />
      <itemData name="String" defStyleNum="dsString" />
      <itemData name="VerbatimString" defStyleNum="dsVerbatimString" />
      <itemData name="Attribute" defStyleNum="dsAttribute" />
      <itemData name="Operators" defStyleNum="dsOperator" />
      <itemData name="Punctuation" defStyleNum="dsSpecialChar" />

      <itemData name="Comment" defStyleNum="dsComment" />
      <itemData name="Error" defStyleNum="dsError" />
      <itemData name="Ident" defStyleNum="dsVariable" />
    </itemDatas>
  </highlighting>

  <general>
    <comments>
      <comment name="singleLine" start="//" />
      <comment name="multiLine" start="/*" end="*/" />
    </comments>
    <keywords casesensitive="1" />
  </general>
</language>
