<?xml version="1.0" encoding="UTF-8"?>
<!--
 Author: Christian Troost
 Copyright (C) 2012-2015 Christian Troost

 This program 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 General Public License for more details.
-->

<language id="mpmasql_cll" _name="mpmasql (cll)" version="2.0" _section="mpmasql">
  <metadata>
    <property name="mimetypes">text/mpmasql-cll</property>
    <property name="globs">*.cll</property>
    <property name="block-comment-start">/*</property>
    <property name="block-comment-end">*/</property>
    <property name="line-comment-start">'</property>
  </metadata>
 <styles>
    <style id="comment"           _name="Comment"            map-to="def:comment"/>
    <style id="error" _name="Error" map-to="def:error"/>
    <style id="keyword" _name="Keyword" map-to="def:keyword"/>
    <style id="headline" _name="Headline" map-to="def:preprocessor"/>
    <style id="statement" _name="Statement" map-to="def:type"/>
    <style id="operator" _name="Statement" map-to="def:type"/>
    <style id="function" _name="Function" map-to="def:type"/>
    <style id="localvar" _name="Function" map-to="def:function"/>
     <style id="class_def" _name="Class Definition" />
     <style id="rtext" _name="Class Definition" map-to="def:underlined"/>
     <style id="string" _name="Class Definition" map-to="def:string"/>
     <style id="special-char" _name="Class Definition" map-to="def:special-char"/>
     <style id="floating-point" _name="Class Definition" map-to="def:floating-point"/>
 </styles>

 <definitions>

  <context id="comment" style-ref="comment" class="comment" class-disabled="no-spell-check">
      <start>/\*</start>
      <end>\*/</end>
      <include>
        <context style-ref="error" extend-parent="false">
          <match>/\*</match>
        </context>
        <context ref="def:in-comment"/>
      </include>
    </context>

    <context id="close-comment-outside-comment" style-ref="error">
      <match>\*/(?!\*)</match>
    </context>

    <context id="line-comment" style-ref="comment" end-at-line-end="true">
      <start>^\s*'</start>
    </context>
    
      <context id="escape" style-ref="special-char">
            <match>\\.</match>
        </context>

        <!-- usual quoted string, ends at line end, \ is an escape char -->
      
    
    <context id="mpmasql_lang">
	<include>
	 <define-regex id="operator" extended="true">
	    \-|\+|\*|\^|\/|~|==|!=|&gt;|&lt;|&gt;=|&lt;=|&amp;&amp;|\|\|
	</define-regex>
	  <context ref="def:decimal"/>
	  <context ref="def:float"/>
	  
	    <context id="string" style-ref="string" end-at-line-end="true" class="string" class-disabled="no-spell-check">
            <start>"</start>
            <end>"</end>
            <include>
                <context ref="escape"/>
               <!-- <context ref="line-continue"/>-->
               
            </include>
        </context>
	  <context id="operator" style-ref="operator"><!-- see `man perlop` -->
		<match>\%{operator}</match>
	  </context>
	 
	  <context id="mpmasql_key" style-ref="function">
	    <prefix>\#</prefix>
	    <keyword>annuity</keyword>
	    <keyword>annuity_factor</keyword>
	    <keyword>count</keyword>
	    <keyword>e</keyword>
	    <keyword>finance_FRM_interest_paid_at_age</keyword>
	    <keyword>finance_FRM_principal_paid_at_age</keyword>
	    <keyword>finance_FRM_rate</keyword>
	    <keyword>floor</keyword>
	    <keyword>foreach</keyword>
	    <keyword>fvalue</keyword>
	    <keyword>ifalone</keyword>
	    <keyword>if_elsif</keyword>
	    <keyword>ifthenelse</keyword>
	    <keyword>if</keyword>
	    <keyword>isequal</keyword>
	    <keyword>listelement</keyword>
	    <keyword>list_concat</keyword>
	    <keyword>list</keyword>
		<keyword>ln</keyword>
	    <keyword>mapcell</keyword>
	    <keyword>normlike</keyword>
	    <keyword>protected_divide</keyword>
	    <keyword>qlist</keyword>
	    <keyword>random_number</keyword>
	    <keyword>regexp</keyword>
	    <keyword>replace_null</keyword>
	    <keyword>report</keyword>
	    <keyword>round_to</keyword>
	    <keyword>sql</keyword>
	    <keyword>steps</keyword>
	    <keyword>tablekeys</keyword>  
	    <keyword>tablevalue</keyword>  
	    <keyword>table</keyword>  
	    <keyword>this</keyword>
	    <keyword>var</keyword>
	  </context>
	</include>
    </context>
    
    <context id="empty" >
      <match>^\s*$</match>
    </context>
    
    <context id="anything-else" style-ref="error"  extend-parent="false" >
	    <match>.*</match>
    </context> 
    
    <context id="element-def-outside-class" style-ref="error"  >
	<start>^\s*\#(activity|constraint|asset|perennial|livestock|sos|timepreference_penalty)(by)?</start>	
    </context>
    <context id="activity_def" style-ref="class_def">
	    <start>^\s*(\#)(activity|constraint|asset|perennial|livestock|constraint|sos|timepreference_penalty)(by)?\s*(\w+)\s+</start>
	    <include>
	     <context id="element_w1" sub-pattern="1" where="start" style-ref="keyword"/>
	     <context id="element_w2" sub-pattern="2" where="start" style-ref="keyword"/>
	     <context id="element_w3" sub-pattern="3" where="start" style-ref="keyword"/>
	     <context id="element_arg1" sub-pattern="4" where="start" style-ref="rtext"/>
	     <context ref="mpmasql_lang:*"/>
	     <context ref="element-def-outside-class" />
	     <context id="eldef" end-parent="true"> 
		  <start>\{\s*$</start>
		  <end>^\s*\}\s*$</end>
		  <include>
			      <context id="resultgroup" style-ref="keyword">
				<match>^\s*\#resultgroup&gt;</match>
			      </context>
			      <context id="subelement" style-ref="keyword">
				<match>^\s*\#\s*\w+\s*&gt;</match>
			      </context>
			      <context id="element-def-inside-elementdef" style-ref="error">
				<match>^\s*\#(activity|constraint|asset|perennial|livestock|sos|timepreference_penalty)(by)?</match>
			      </context>
			      <context ref="line-comment"/>
			      <context ref="comment"/>
			      <context ref="close-comment-outside-comment" />
			      <!--<context ref="empty" end-parent="true"/>-->
			      <context ref="mpmasql_lang:*"/>
			      <context id="value_def"   end-at-line-end="true">
				<start>(\#=)</start>
				
				<include>
				  <context id="cll_EE" sub-pattern="1" where="start" style-ref="keyword"/>
				    <context ref="mpmasql_lang:*"/>
				</include>
			      </context>
			
		  </include>
	     </context> 
	    </include>
      </context>
      
      <context id="class_def" style-ref="class_def">
		<start>^\s*(\#class)\s*(\w+)\s*\{</start>
		<end>^\s*\}</end>
		<include>
		  <context id="class_w" sub-pattern="1" where="start" style-ref="keyword"/>
		  <context id="class_arg1" sub-pattern="2" where="start" style-ref="rtext"/>
		  <context ref="activity_def"/>
		  <context ref="line-comment"/>
		  <context ref="comment"/>
		  <context ref="close-comment-outside-comment" />
		  <context id="inherits"  end-at-line-end="true">
			<start>\s*(\#inherits|\#has|\#have)\s*\((.+)\)\s+(elements\s+like|from)\s+(\w+)</start>
			<include>
			<context id="inherits_w1" sub-pattern="1" where="start" style-ref="keyword"/>
			<context id="inherits_arg1" sub-pattern="2" where="start" style-ref="rtext"/>
			<context id="inherits_w2" sub-pattern="3" where="start" style-ref="keyword"/>
			<context id="inherits_arg2" sub-pattern="4" where="start" style-ref="rtext"/>
			</include>
		  </context>
		  <context id="instance_attr"  end-at-line-end="true">
			<start>^\s*(\#\s*instance_attribute\s*&gt;)</start>
			<include>
			  <context id="instance_attr_w1" sub-pattern="1" where="start" style-ref="keyword"/>
			  <context ref="mpmasql_lang:*"/>
			  <context id="value_def2"   end-at-line-end="true">
				<start>(\#=)</start>				
				<include>
					<context id="cll_EE" sub-pattern="1" where="start" style-ref="keyword"/>
					<context ref="mpmasql_lang:*"/>
				</include>
			
			  </context>
			</include>
		  </context>
		  <context ref="empty" />
		  <context ref="anything-else"/>
		</include>
    </context>  
	  
    <context id="types_section">
      <start>\s*\[\s*(ACTIVITY\s+TYPES|CONSTRAINT\s+TYPES)\s*\]</start>
      <end>^(?=\s*\[\s*(ACTIVITY\s+TYPES|CONSTRAINT\s+TYPES|DEFAULTS|MODEL|INSTANCES)\s*\])</end>
	<include>
	<context id="ts_head" sub-pattern="1" where="start" style-ref="headline"/>
	    <context ref="line-comment"/>
	  <context ref="comment"/>
	  <context ref="close-comment-outside-comment" />
	</include>
    </context>
    
	    <context id="instances_section">
      <start>\s*\[\s*(INSTANCES)\s*\]</start>
      <end>^(?=\s*\[\s*(ACTIVITY\s+TYPES|CONSTRAINT\s+TYPES|DEFAULTS|MODEL|INSTANCES)\s*\])</end>
	<include>
	<context id="ts_head" sub-pattern="1" where="start" style-ref="headline"/>
	    <context ref="line-comment"/>
	  <context ref="comment"/>
	  <context ref="close-comment-outside-comment" />
	  <context ref="mpmasql_lang:*"/>
	</include>
    </context>  
	  
    <context id="default_section">
      <start>\s*\[\s*(DEFAULTS)\s*\]</start>
      <end>^(?=\s*\[\s*(ACTIVITY\s+TYPES|CONSTRAINT\s+TYPES|DEFAULTS|MODEL|INSTANCES)\s*\])</end>
     
	<include>
	 <context id="df_head" sub-pattern="1" where="start" style-ref="headline"/>
	 <context ref="activity_def"/>
	  <context ref="line-comment"/>
	  <context ref="comment"/>
	  <context ref="close-comment-outside-comment" />
	 <context ref="empty" />
	 <context ref="anything-else"/>
	</include>
    </context>
    
    
    <context id="close-class-outside" style-ref="error"  extend-parent="false" >
	    <match>\}</match>
    </context> 
    
     <context id="model_section">
      <start>\s*\[\s*(MODEL)\s*\]</start>
      <end>^(?=\s*\[\s*(ACTIVITY\s+TYPES|CONSTRAINT\s+TYPES|DEFAULTS|MODEL|INSTANCES)\s*\])</end>
	<include>
	<context id="md_head" sub-pattern="1" where="start" style-ref="headline"/>
	  <context ref="class_def"/> 
	  <context ref="line-comment"/>
	  <context ref="comment"/>
	  <context ref="close-class-outside"/>
	<context ref="close-comment-outside-comment" />
	<context ref="element-def-outside-class" />
	 <context ref="empty" />
	 <context ref="anything-else"/>
	</include>
    </context>
    
   
    
   <context id="mpmasql_cll">
      <include>
	<context ref="line-comment"/>
	<context ref="comment"/>
	<context ref="types_section"/>
	<context ref="default_section"/>
	<context ref="model_section"/>
	<context ref="instances_section"/>
	<context ref="close-class-outside"/>
	<context ref="close-comment-outside-comment" />
	<context ref="element-def-outside-class" />
      </include>
    </context>


</definitions>
</language>
