<?xml version="1.0" encoding="UTF-8"?>
<!--
 Author: Christian Troost
 Copyright (C) 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_var" _name="mpmasql (var)" version="2.0" _section="mpmasql">
  <metadata>
    <property name="mimetypes">text/mpmasql-var</property>
    <property name="globs">*.var</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:identifier"/>
    <style id="headline" _name="Headline" map-to="def:keyword"/>
    <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:string"/>
     <style id="vartab" _name="Tab" />
     <style id="varparam" _name="Parameter"  />
 </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" 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>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>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="param-def-outside-section" style-ref="error"  >
	<start>^\s*\w+\s+=</start>	
    </context>
    
     <context id="parameter_def" style-ref="class_def" end-at-line-end="true">
	    <start>^\s*(\w+)\s*=</start>
	    <include>
	     <context id="parameter_w1" sub-pattern="1" where="start" style-ref="varparam"/>
	     <context ref="mpmasql_lang:*"/>
	    </include>
    </context>

	     <context id="tabdef" end-parent="true"> 
		  <start>\s*\{\s*</start>
		  <end>\s*\}\s*</end>
		  <include>
			      <context ref="line-comment"/>
			      <context ref="comment"/>
			      <context ref="close-comment-outside-comment" />
			      <!--<context ref="empty" end-parent="true"/>-->
			      <context ref="mpmasql_lang:*"/>		
		  </include>
	     </context> 



     <context id="mpmas-table-def" style-ref="class_def">
	    <start>^\s*(\w+)\s+=\s*\(\s*(ascii|sql|here|link)\s*\)</start>
	    <include>
	     <context id="parameter_w1" sub-pattern="1" where="start" style-ref="vartab"/>
	     <context id="parameter_w2" sub-pattern="2" where="start" style-ref="keyword"/>
	     <context ref="tabdef"/>
	   </include>
    </context>
    <context id="user-table-def" style-ref="class_def">
	    <start>^\s*(\w+)\s+=\s*\(\s*(ascii|sql|here)\s*,\s*\d+\s*\)</start>
	    <include>
	     <context id="parameter_w1" sub-pattern="1" where="start" style-ref="vartab"/>
	     <context id="parameter_w2" sub-pattern="2" where="start" style-ref="keyword"/>
	     <context ref="tabdef"/>
	   </include>
    </context>
  
    <context id="mpmas-tables-section">
      <start>\s*\[\s*(MPMAS\s+TABLES)\s*\]</start>
      <end>^(?=\s*\[\s*(GLOBALS|INSTANCES|MPMAS\s+PARAMETERS|USER\s+VARIABLES|USER\s+TABLES|MPMAS\s+TABLES|TABLES|DB|VARIABLES)\s*\])</end>
     
	<include>
	 <context id="mt_head" sub-pattern="1" where="start" style-ref="headline"/>
	 <context ref="mpmas-table-def"/>
	  <context ref="line-comment"/>
	  <context ref="comment"/>
	  <context ref="close-comment-outside-comment" />
	  <context ref="param-def-outside-section" />
	 <context ref="empty" />
	 <context ref="anything-else"/>
	</include>
    </context>

    <context id="user-tables-section">
      <start>\s*\[\s*(USER\s+TABLES|TABLES)\s*\]</start>
      <end>^(?=\s*\[\s*(GLOBALS|INSTANCES|MPMAS\s+PARAMETERS|USER\s+VARIABLES|USER\s+TABLES|MPMAS\s+TABLES|TABLES|DB|VARIABLES)\s*\])</end>
     
	<include>
	 <context id="ut_head" sub-pattern="1" where="start" style-ref="headline"/>
	 <context ref="user-table-def"/>
	  <context ref="line-comment"/>
	  <context ref="comment"/>
	  <context ref="close-comment-outside-comment" />
	  <context ref="param-def-outside-section" />
	 <context ref="empty" />
	 <context ref="anything-else"/>
	</include>
    </context>
	  
    <context id="other-section">
      <start>\s*\[\s*(GLOBALS|INSTANCES|MPMAS\s+PARAMETERS|USER\s+VARIABLES|DB|VARIABLES)\s*\]</start>
      <end>^(?=\s*\[\s*(GLOBALS|INSTANCES|MPMAS\s+PARAMETERS|USER\s+VARIABLES|USER\s+TABLES|MPMAS\s+TABLES|TABLES|DB|VARIABLES)\s*\])</end>
     
	<include>
	 <context id="df_head" sub-pattern="1" where="start" style-ref="headline"/>
	 <context ref="parameter_def"/>
	  <context ref="line-comment"/>
	  <context ref="comment"/>
	  <context ref="close-comment-outside-comment" />
	  <context ref="param-def-outside-section" />
	 <context ref="empty" />
	 <context ref="anything-else"/>
	</include>
    </context>
    
    
   
 
   
    
   <context id="mpmasql_var">
      <include>
	<context ref="line-comment"/>
	<context ref="comment"/>
	<context ref="user-tables-section"/>
	<context ref="mpmas-tables-section"/>
	<context ref="other-section"/>
	<context ref="close-comment-outside-comment" />
	<context ref="param-def-outside-section" />

      </include>
    </context>


</definitions>
</language>
