\chapter{Concrete Syntax}

\begin{figure}[t]
  \[
    \begin{array}{rcl}
      \defterm{compilationUnit}  & : & \nonterm{import}^\star\s\nonterm{scopeExpression}\\
      \defterm{import}           & : & \term{import}\s\token{UIDENT}\s\term{;}
    \end{array}
  \]
  \caption{Compilation unit concrete syntax}
\end{figure}

\begin{figure}[t]
  \[
    \begin{array}{rcll}
      \defterm{expression}        & : & \nonterm{basicExpression}\s(\s\term{;}\s\nonterm{expression}\s)&\\
      \defterm{basicExpression}   & : & \nonterm{binaryExpression}&\\
      \defterm{binaryExpression}  & : & \nonterm{binaryOperand}\s\token{INFIX}\s\nonterm{binaryOperand}&\alt\\
                                  &   & \nonterm{binaryOperand}&\\
      \defterm{binaryOperand}     & : & \nonterm{binaryExpression}&\alt\\
                                  &   & [\s\term{-}\s]\s\nonterm{postfixExpression}&\\
      \defterm{postfixExpression} & : & \nonterm{primary}&\alt\\
                                  &   & \nonterm{postfixExpression}\s\term{(}\s[\s\nonterm{expression}\s(\s\term{,}\s\nonterm{expression}\s)^\star\s]\s\term{)}&\alt\\
                                  &   & \nonterm{postfixExpression}\s\term{[}\s\nonterm{expression}\s\term{]}&\alt\\
                                  &   & \nonterm{postfixExpression}\s\term{.}\s\term{length}&\alt\\
                                  &   & \nonterm{postfixExpression}\s\term{.}\s\term{string}&\\      

      \defterm{primary}           & : & \token{DECIMAL}&\alt\\
                                  &   & \token{STRING}&\alt\\
                                  &   & \token{CHAR}&\alt\\
                                  &   & \token{LIDENT}&\alt\\
                                  &   & \term{true}&\alt\\
                                  &   & \term{false}&\alt\\
                                  &   & \term{infix}\s\token{INFIX}&\alt\\
                                  &   & \term{skip}&\alt\\
                                  &   & \term{fun}\s\term{(}\s\nonterm{functionArguments}\s\term{)}\s\nonterm{functionBody}&\alt\\
                                  &   & \term{\{}\s\nonterm{scopeExpression}\s\term{\}}&\alt\\
                                  &   & \nonterm{listExpression}&\alt\\
                                  &   & \nonterm{arrayExpression}&\alt\\
                                  &   & \nonterm{S-expression}&\alt\\
                                  &   & \nonterm{ifExpression}&\alt\\
                                  &   & \nonterm{whileExpression}&\alt\\
                                  &   & \nonterm{repeatExpression}&\alt\\
                                  &   & \nonterm{forExpression}&\alt\\
                                  &   & \nonterm{caseExpression}&\alt\\
                                  &   & \term{(}\s\nonterm{expression}\s\term{)}&
    \end{array}
  \]
  \caption{Expression concrete syntax}
\end{figure}


\begin{figure}[t]
  \[
    \begin{array}{rcl}
      \defterm{scopeExpression}                & : & \nonterm{definition}^\star\s\nonterm{expression}\\
      \defterm{definition}                     & : & \nonterm{variableDefinition}\alt\nonterm{functionDefinition}\alt\nonterm{infixDefinition}\\
      \defterm{variableDefinition}             & : & (\s\term{local}\alt\term{public}\s)\s\nonterm{variableDefinitionSequence}\s\term{;}\\
      \defterm{variableDefinitionSequence}     & : & \nonterm{variableDefinitionSequenceItem}\s(\s\term{,}\s\nonterm{variableDefinitionSequenceItem}\s)^\star\\
      \defterm{variableDefinitionSequenceItem} & : & \token{LIDENT}\s[\s\term{=}\s\nonterm{basicExpression}\s]\\
      \defterm{functionDefinition}             & : & [\s\term{public}\s]\s\term{fun}\s\token{LIDENT}\s\term{(}\s\nonterm{functionArguments}\s\term{)}\s\nonterm{functionBody}\\
      \defterm{functionArguments}              & : & [\s\token{LIDENT}\s(\s\term{,}\s\token{LIDENT}\s)^\star\s]\\
      \defterm{functionBody}                   & : & \term{\{}\s\nonterm{scopeExpression}\s\term{\}}
    \end{array}
  \]
  \caption{Scope expression concrete syntax}
\end{figure}


\begin{figure}[t]
  \[
    \begin{array}{rcll}
      \defterm{pattern}         & : & \nonterm{consPattern}\alt\nonterm{simplePattern}&\\
      \defterm{consPattern}     & : & \nonterm{simplePattern}\s\term{:}\s\nonterm{pattern}&\\
      \defterm{simplePattern}   & : & \nonterm{wildcardPattern} & \alt\\
                                &   & \nonterm{S-exprPattern} & \alt \\
                                &   & \nonterm{arrayPattern} & \alt \\
                                &   & \nonterm{listPattern} & \alt \\
                                &   & \token{LIDENT}\s[\s\term{@}\s\nonterm{pattern} \s] & \alt \\
                                &   & [\s\term{-}\s]\s\token{DECIMAL}& \alt \\
                                &   & \token{STRING} & \alt \\
                                &   & \token{CHAR} & \alt \\
                                &   & \term{true} & \alt \\
                                &   & \term{false} & \alt \\
                                &   & \term{\#}\s\term{boxed} & \alt \\
                                &   & \term{\#}\s\term{unboxed} & \alt \\
                                &   & \term{\#}\s\term{string} & \alt \\
                                &   & \term{\#}\s\term{array} & \alt \\
                                &   & \term{\#}\s\term{sexp} & \alt \\
                                &   & \term{\#}\s\term{fun} & \alt \\
                                &   & \term{(}\s\nonterm{pattern}\s\term{)} & \\
      \defterm{wildcardPattern} & : & \term{\_} &\\
      \defterm{S-exprPattern}   & : & \token{UIDENT}\s[\s\term{(}\s\nonterm{pattern}\s(\s\term{,}\s\nonterm{pattern})^\star\s\term{)}\s] &\\
      \defterm{arrayPattern}    & : & \term{[}\s[\s\nonterm{pattern}\s(\s\term{,}\s\nonterm{pattern})^\star\s]\s\term{]} &\\
      \defterm{listPattern}     & : & \term{\{}\s[\s\nonterm{pattern}\s(\s\term{,}\s\nonterm{pattern})^\star\s]\s\term{\}} &
    \end{array}
  \]
  \caption{Pattern concrete syntax}
\end{figure}

\begin{figure}[t]
  \[
    \begin{array}{rcll}
      \defterm{ifExpression}  & : & \term{if}\s\nonterm{expression}\s\term{then}\s\nonterm{scopeExpression}\s[\s\nonterm{elsePart}\s]\s\term{fi}&\\
      \defterm{elsePart}      & : & \term{elif}\s\nonterm{expression}\s\term{then}\s\nonterm{scopeExpression}\s[\s\nonterm{elsePart}\s]&\alt\\
                              &   & \term{else}\s\nonterm{scopeExpression}&
    \end{array}
  \]
  \caption{If-expression concrete syntax}
\end{figure}

\begin{figure}[t]
  \[
    \begin{array}{rcl}
      \defterm{whileExpression}  & : & \term{while}\s\nonterm{expression}\s\term{do}\s\nonterm{scopeExpression}\s\term{od}\\
      \defterm{repeatExpression} & : & \term{repeat}\s\nonterm{scopeExpression}\s\term{until}\s\nonterm{basicExpression}\\
      \defterm{forExpression}    & : & \term{for}\s\nonterm{expression}\s\term{,}\s\nonterm{expression}\s\term{,}\s\nonterm{expression}\\
                                 &   & \term{do}\nonterm{scopeExpresssion}\s\term{od}
    \end{array}
  \]
  \caption{Loop expressions concrete syntax}  
\end{figure}

\begin{figure}[t]
  \[
    \begin{array}{rcl}
      \defterm{arrayExpression} & : & \term{[}\s[\s\nonterm{expression}\s(\s\term{,}\s\nonterm{expression}\s)^\star\s]\s\term{]}\\
      \defterm{listExpression}  & : & \term{\{}\s[\s\nonterm{expression}\s(\s\term{,}\s\nonterm{expression}\s)^\star\s]\s\term{\}}\\
      \defterm{S-expression}    & : & \token{UIDENT}\s[\s\term{(}\s\nonterm{expression}\s[\s(\s\term{,}\s\nonterm{expression}\s)^\star\s]\term{)}\s]
    \end{array}
  \]
  \caption{Array, list, and S-expressions concrete syntax}  
\end{figure}


\begin{figure}[t]
  \[
    \begin{array}{rcl}
      \defterm{caseExpression}  & : & \term{case}\s\nonterm{expression}\s\term{of}\s\nonterm{caseBranches}\s\term{esac}\\
      \defterm{caseBranches}    & : & \nonterm{caseBranch}\s[\s(\s\term{$\mid$}\s\nonterm{caseBranch}\s)^\star\s]\\
      \defterm{caseBranch}      & : & \nonterm{pattern}\s\term{$\rightarrow$}\s\nonterm{scopeExpression}
    \end{array}
  \]
  \caption{Case-expression concrete syntax}
\end{figure}


