#!/bin/bash

# Create a Kate syntax highlighting configuration file
# The script is inspired by FooBar's and Terkhen's nppp_nml_syntax.sh
#     which creates a syntax highlighting file for Notepad++

# Version 1.0 from 11. March 2013

#INSTRUCTIONS
#The script must be run inside the folder that contains the nml repository

output_file="nml_kate.xml"
relpath=".."

echo "$output_file"

cat > $output_file << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
<!--
        This is a syntax highlighter for NML, the NewGRF markup language
        (http://dev.openttdcoop.org/nml) used to write NewGRF
        extensions for OpenTTD (http://www.openttd.org)

        The part on highlighting preprocessor instructions are taken from kate's c++ highlighter
        in the version 1.49 by Sebastian Pipping (webmaster@hartwork.org)
-->
<language name="NML" section="Sources" version="0.01" kateversion="2.4"
      extensions="*.nml;*.pnml;*.tnml"
      mimetype="text/x-nml"
      indenter="cstyle"
      author="Ingo von Borstel"
      license="GPL v2"
      priority="10">

  <highlighting>
    <list name="blockwords">
EOF

cat $relpath/nml/tokens.py          | grep "^    '" | awk -F\' '{ print "      <item> " $2 " </item>" }' >> $output_file

cat >> $output_file << EOF
    </list>
    <list name="features">
EOF

# Obtain the list of features
cat $relpath/nml/ast/general.py     | grep "^    '" | awk -F\' '{ print "      <item> " $2 " </item>" }' >> $output_file

cat >> $output_file << EOF
    </list>
    <list name="builtin">
EOF

cat $relpath/nml/expression/functioncall.py | grep "^    '" | awk -F\' '{ print "      <item> " $2 " </item>" }' >> $output_file

cat >> $output_file << EOF
    </list>
    <list name="constants">
EOF

# Now add a list of constants and global variables
cat $relpath/nml/global_constants.py    | grep "^    '" | awk -F\' '{ print "      <item> " $2 " </item>" }' >> $output_file
cat $relpath/nml/actions/action5.py     | grep "^    '" | awk -F\' '{ print "      <item> " $2 " </item>" }' >> $output_file
cat $relpath/nml/actions/real_sprite.py | grep "^    '" | awk -F\' '{ print "      <item> " $2 " </item>" }' >> $output_file
cat $relpath/nml/actions/action12.py    | grep "^    '" | awk -F\' '{ print "      <item> " $2 " </item>" }' >> $output_file
cat $relpath/nml/actions/actionB.py     | grep "^    '" | awk -F\' '{ print "      <item> " $2 " </item>" }' >> $output_file
cat $relpath/nml/ast/basecost.py        | grep "^    '" | awk -F\' '{ print "      <item> " $2 " </item>" }' >> $output_file

cat >> $output_file << EOF
    </list>

    <contexts>
      <!-- Normal text with the usual keywords and inserts -->
      <context name="Normal" attribute="Normal Text" lineEndContext="#stay">
        <DetectSpaces />
        <DetectChar context="AfterHash" char="#" firstNonSpace="true" lookAhead="true" />
        <keyword attribute="Block" context="#stay" String="blockwords" />
        <keyword attribute="Feature" context="#stay" String="features" />
        <keyword attribute="Built-in Function" context="#stay" String="builtin" />
        <keyword attribute="Constant" context="#stay" String="constants" />
        <DetectChar attribute="String" context="String" char="&quot;"/>
        <Detect2Chars attribute="Comment" context="Comment 1" char="/" char1="/" />
        <Detect2Chars attribute="Comment" context="Comment 2" char="/" char1="*" />
      </context>

      <!-- Strings within quotation marks -->
      <context attribute="String" lineEndContext="#pop" name="String">
        <LineContinue attribute="String" context="#stay"/>
        <HlCStringChar attribute="String Char" context="#stay"/>
        <DetectChar attribute="String" context="#pop" char="&quot;"/>
      </context>

      <!-- Strings starting with // -->
      <context attribute="Comment" lineEndContext="#pop" name="Comment 1">
        <LineContinue attribute="Comment" context="#stay"/>
        <DetectSpaces />
        <IncludeRules context="##Alerts" />
        <DetectIdentifier />
      </context>
      <!-- Multi-line strings like /* ... */ -->
      <context attribute="Comment" lineEndContext="#stay" name="Comment 2">
        <DetectSpaces />
        <Detect2Chars attribute="Comment" context="#pop" char="*" char1="/" endRegion="Comment"/>
        <IncludeRules context="##Alerts" />
        <DetectIdentifier />
      </context>

      <!-- Preprocessor commands starting with a hash - Main switch for preprocessor -->
      <context attribute="Error" lineEndContext="#pop" name="AfterHash">
        <!-- define, elif, else, endif, error, if, ifdef, ifndef, include, include_next, line, pragma, undef, warning -->
        <RegExpr attribute="Preprocessor" context="Preprocessor" String="#\s*if(?:def|ndef)?(?=\s+\S)" insensitive="true" beginRegion="PP" firstNonSpace="true" />
        <RegExpr attribute="Preprocessor" context="Preprocessor" String="#\s*endif" insensitive="true" endRegion="PP" firstNonSpace="true" />
        <RegExpr attribute="Preprocessor" context="Define" String="#\s*define.*((?=\\))" insensitive="true" firstNonSpace="true" />
        <RegExpr attribute="Preprocessor" context="Preprocessor" String="#\s*(?:el(?:se|if)|include(?:_next)?|define|undef|line|error|warning|pragma)" insensitive="true" firstNonSpace="true" />
        <RegExpr attribute="Preprocessor" context="Preprocessor" String="#\s+[0-9]+" insensitive="true" firstNonSpace="true" />
      </context>
      <!-- Preprocessor instructions -->
      <context attribute="Preprocessor" lineEndContext="#pop" name="Preprocessor">
        <LineContinue attribute="Preprocessor" context="#stay"/>
        <RangeDetect attribute="Prep. Lib" context="#stay" char="&quot;" char1="&quot;"/>
        <RangeDetect attribute="Prep. Lib" context="#stay" char="&lt;" char1="&gt;"/>
        <IncludeRules context="##Doxygen" />
        <Detect2Chars attribute="Comment" context="Comment/Preprocessor" char="/" char1="*" beginRegion="Comment2" />
        <Detect2Chars attribute="Comment" context="Comment 1" char="/" char1="/"/>
      </context>
      <!-- Preprocessor #define -->
      <context attribute="Preprocessor" lineEndContext="#pop" name="Define">
        <LineContinue attribute="Preprocessor" context="#stay"/>
      </context>
      <!-- Preprocessor comments -->
      <context attribute="Comment" lineEndContext="#stay" name="Comment/Preprocessor">
        <DetectSpaces />
        <Detect2Chars attribute="Comment" context="#pop" char="*" char1="/" endRegion="Comment2" />
        <DetectIdentifier />
      </context>

    </contexts>

    <itemDatas>
      <itemData name="Normal Text"       defStyleNum="dsNormal" />
      <itemData name="Block"             defStyleNum="dsKeyword" />
      <itemData name="Feature"           defStyleNum="dsKeyword" color="#0095ff" selColor="#ffffff" bold="1" italic="0" spellChecking="false"/>
      <itemData name="Built-in Function" defStyleNum="dsDataType" spellChecking="false" />
      <itemData name="String"            defStyleNum="dsString"/>
      <itemData name="Preprocessor"      defStyleNum="dsOthers" spellChecking="false"/>
      <itemData name="Constant"          defStyleNum="dsOthers" color="#006400" italic="1" spellChecking="false" />
      <itemData name="Comment"           defStyleNum="dsComment"/>
    </itemDatas>
  </highlighting>

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

  </language>
<!--
// kate: space-indent on; indent-width 2; replace-tabs on;
-->
EOF