language MAPLE extension mpl 

module definition stmt use stmt
token newline category ignore_scrap translation <>

comment begin <"#"> end newline

default translation <*> mathness no

line begin <"#line"> end <>

token pseudo_semi category terminator translation <"\\rx">

token identifier	category ident
token number	category numbr
token {		category slopen translation <"\\{">
token }		category slclose translation <"\\}">
token [		category slopen tangleto <space-"\[">
token ]		category slclose tangleto <"\]"-space>
token (		category open
token )		category close

#what to do with ' and ` and "??
#token `		category backquote

token '		category math tangleto <space-"'">
token .		category point
token ;		category terminator translation <";"-break_space>
token :		category terminator translation <":"-break_space>
token ,		category comma translation <","-opt-1>
token \		category backslash translation <"\\backslash">
#tijdelijk: van vorige regel: tangleto <"\\">
token :=	category assignment translation <":=">
token $		category binop translation <"\\,\\$\\,">
#token ..	category binop translation <"..">
token *		category binop
token ^		category binop
token /		category binop
token <>	category binop translation <"\\NEQ">  tangleto <"<>">
token <=	category binop translation <"\\leq">
token >=	category binop translation <"\\geq">
token =		category binop
token <		category binop
token >		category binop
token !		category unop
token +		category unorbinop
token -		category unorbinop

#ilk definitions
ilk forstart_like category forstart
ilk forbodystart_like category forbodystart
ilk forbodyend_like category forbodyend
ilk ifbegin_like category ifbegin
ilk ifthen_like category ifthen
ilk elif_like category elif
ilk ifbody_like category ifbody
ilk ifend_like category ifend
ilk procedure_like category procedure
ilk procextra_like category procextra
ilk procend_like category procend
ilk return_like category return
ilk ident_like category ident

default translation <"\\"-space-*-"\\"-space>

ilk binop_like category binop 

reserved proc ilk procedure_like
reserved end ilk procend_like
reserved local ilk procextra_like
reserved option ilk procextra_like
reserved minus ilk binop_like

reserved for  ilk forstart_like
reserved while  ilk forstart_like
reserved to ilk forstart_like
reserved in ilk forstart_like
reserved from  ilk forstart_like
reserved do  ilk forbodystart_like
reserved od  ilk forbodyend_like
reserved if  ilk ifbegin_like
reserved then ilk ifthen_like
reserved elif ilk elif_like
reserved else ilk ifbody_like
reserved fi ilk ifend_like
reserved RETURN ilk return_like
reserved break ilk ident_like
reserved next ilk ident_like
#token category 

#production rules
#? ignore_scrap --> #1

#handling statements;
<"$"> math <"$"> terminator --> stmt
stmt <space>  stmt --> stmt
stmt terminator --> stmt
ignore_scrap --> terminator
terminator --> stmt

#handling comments
#comment newline --> newline
#comment ? --> comment

#handling idents and math etc.
<force> return --> math
ident --> math
numbr --> math
(unop|unorbinop|binop) math --> math
math (unop|unorbinop|binop) --> math
math math --> math
math comma --> math
math [ point point ] --> math binop
math point --> math
math assignment --> math
<"$"> math <"$"> <space> terminator --> stmt


#handlig forloops
forstart <space>  <"$"> math <"$"> <space> --> forstart
forstart forstart  --> forstart
forstart <space> forbodystart <force-indent>  --> forbodystart
forbodystart stmt --> forbodystart
<force> forbodystart <outdent-force> forbodyend terminator <force> --> stmt
<force> forbodystart <outdent-force> forbodyend  <force> --> stmt
<force> forbodystart <space> <"$"> math <"$"> <outdent-force> forbodyend terminator <force> --> stmt
<force> forbodystart <space> <"$"> math <"$"> <outdent> <force> forbodyend  <force> --> stmt


#handling ifstatements
<force> ifbegin <space> <"$"> math <"$"> <space>  ifthen <force-indent> --> ifbegin
ifbegin stmt --> ifbegin
ifbegin <outdent-force> ifend terminator <force> --> stmt
ifbegin <outdent-force> ifend <force> --> stmt
ifbegin <"$"> math <"$"> <outdent-force> ifend terminator  <force> --> stmt
ifbegin <"$"> math <"$"> <outdent-force> ifend  <force> --> stmt

ifbegin <outdent-force> ifbody <space> <indent> --> ifbegin
ifbegin <space> <"$"> math <"$"> <outdent-force> ifbody <space> <indent> --> ifbegin

elif  <space> <"$"> math <"$"> <space>  ifthen <force-indent> --> elif
elif stmt --> elif
elif <space> <"$"> math <"$">  <outdent-force> elif --> elif
elif <outdent-force> elif --> elif
elif <outdent-force> ifbody <space> <indent> --> elif
elif   <"$"> math <"$"> <outdent-force> ifbody <space> <indent>  --> elif
elif  <"$">  math <"$"> <outdent-force> ifend  terminator <force>  --> elifend
elif   <"$"> math <"$"> <outdent-force> ifend <force>  --> elifend
elif <outdent-force> ifend terminator <force> --> elifend
elif <outdent-force> ifend <force> --> elifend

ifbegin <outdent-force> elifend --> stmt
ifbegin <"$"> math <"$"> <outdent-force> elifend --> stmt

#handling procedures

<"$"> math  <"$"> <force> <indent> procedure <"$"> math  <"$"> <force> --> procedure
procedure  <force> procextra <space> stmt <force> --> procedure
procedure <indent> stmt --> procbody
procbody stmt --> procbody

procbody <outdent> <force> procend terminator <big_force-outdent> --> stmt
procbody <"$"> math  <"$"> <outdent> <force> procend terminator <big_force-outdent> --> stmt

procbody <outdent> <force> procend <big_force-outdent> --> stmt
procbody <"$"> math  <"$"> <outdent> <force> procend <big_force-outdent> --> stmt
procedure <space> <indent> <"$"> math  <"$"> <force> <outdent>  procend terminator <big_force-outdent>  --> stmt
procedure <space> <indent> <"$"> math  <"$"> <force> <outdent> procend <big_force-outdent> --> stmt

#handling sets and lists 
open <"\\,"> close --> math
open <"\\,"> math <"\\,"> close --> math
slopen <"\\,"> slclose --> math
slopen  <"\\,"> math  <"\\,"> slclose  --> math

#handling backslash
math backslash math    -->  math
backslash newline --> newline
backslash backslash --> math
backslash --> math

#handling backquote
#changefile makes '`' reading as string!


macros begin
\def\commentbegin{\#}
\def\commentend{}
\def\NEQ{\ne}
\def\LL{\ll\,}
\def\RR{\,\gg}
\def\PS{\joinrel{+\equiv}}
\let\rx\relax

macros end