\documentclass[10pt]{article}

\usepackage{a4wide}
\usepackage{palatino}
\usepackage{euler}
\usepackage{amssymb}
\usepackage{stmaryrd}

\title{A MathML Editor Based on \TeX{} Syntax\\Formal Specification}
\author{Paolo Marinelli\\Luca Padovani\\\small\{{\tt pmarinel},{\tt lpadovan}\}{\tt @cs.unibo.it}\\\small Department of Computer Science\\\small University of Bologna}
\date{}

\newcommand{\tmap}[1]{\llbracket#1\rrbracket}
\newcommand{\tadvance}{\vartriangle}
\newcommand{\tnext}{\rhd}
\newcommand{\G}{\texttt{g}}
\newcommand{\PNODE}{\texttt{p}}
\newcommand{\SNODE}{\texttt{s}}
\newcommand{\INODE}{\texttt{i}}
\newcommand{\NNODE}{\texttt{n}}
\newcommand{\ONODE}{\texttt{o}}
\newcommand{\CNODE}{\texttt{c}}
\newcommand{\TABLE}{\texttt{table}}
\newcommand{\SP}{\texttt{sp}}
\newcommand{\SB}{\texttt{sb}}
\newcommand{\CELL}{\texttt{cell}}
\newcommand{\ROW}{\texttt{row}}
\newcommand{\SLDROP}{\blacktriangleleft}
\newcommand{\NLDROP}{\vartriangleleft}

\begin{document}

\maketitle

\section{Tokens}

The following tokens are defined:

\begin{tabular}{lllp{0.5\textwidth}}
  \textbf{\TeX{}} & \textbf{Notation} & \textbf{Node} & \textbf{Description} \\
\hline
  \verb+{+ & $\mathrm{begin}$ & \texttt{g} & Beginning of a group \\
  \verb+}+ & $\mathrm{end}$ & & End of a group \\
  \verb+$+ & $\$$ & \texttt{math} & Math shift \\ %$ \\
  & &  & End-of-line \\
  \verb+#+$i$ & $p(i)$ & \texttt{p} & Parameter \\
  \verb+^+ & $\uparrow$ & \texttt{sp} & Superscript \\
  \verb+_+ & $\downarrow$ & \texttt{sb} & Subscript \\
  & $\square$ & & Space-like character that can be ignored \\
  & $s$ & \texttt{s} & Space-like character that may be significant \\
  letter & $i(v)$ & \texttt{i} & Identifier $v$ \\
  digit & $n(v)$ & \texttt{n} & Number $v$ \\
  other & $o(v)$ & \texttt{o} & Other character or operator $v$ \\
  \verb+~+ & $\sim$ & & Active character \\
  \verb+%+ & $\%$ & & Comment \\
  control & $c(v)\langle\alpha_1,\dots,\alpha_n\rangle$ & \texttt{c} & 
    Control sequence $v$ that expects the $\alpha_1,\dots,\alpha_n$ sequence of tokens. \\
  backspace & $\vartriangleleft$ & & \\
  backspace & $\blacktriangleleft$ & & \\
\end{tabular}

%% Some tokens are mapped directly into nodes of the TML tree. The following functions shows
%% the mapping:

\begin{tabular}{r@{\quad$=$\quad}l}
  $\tmap{\{}$ & \verb+g+ \\
  $\tmap{p(i)}$ & \verb+p[@index=+$i$\verb+]+ \\
  $\tmap{p_l(i)}$ & \verb+p[@index=+$i$\verb+][@left-open='1']+ \\
  $\tmap{p_r(i)}$ & \verb+p[@index=+$i$\verb+][@right-open='1']+ \\
  $\tmap{s}$ & \verb+s+ \\
  $\tmap{\uparrow}$ & \verb+sp+ \\
  $\tmap{\downarrow}$ & \verb+sb+ \\
  $\tmap{i(v)}$ & \verb+i[@value=+$v$\verb+]+ \\
  $\tmap{n(v)}$ & \verb+n[@value=+$v$\verb+]+ \\
  $\tmap{o(v)}$ & \verb+o[@value=+$v$\verb+]+ \\
  $\tmap{c(v)\langle\alpha_1,\dots,\alpha_n\rangle}$ & \verb+c[@name=+$v$\verb+][^+$\tmap{\alpha_1}\cdots\tmap{\alpha_n}$\verb+$]+\\
\end{tabular}
%$

%% \section{Description and Semantics of the Pattern Language}

%% \begin{eqnarray*}
%%   \mathit{NodeTest} & ::= & \mathtt{*} \\
%%   & | & \mathit{ElementType} \\
%%   & | & \mathtt{<}~\mathit{ElementTypePattern}~\mathtt{>} \\[1ex]
%%   \mathit{ElementTypePattern} & ::= & \mathtt{*} \\
%%   & | & \mathit{ElementType}~(\mathtt{|}~\mathit{ElementType})^* \\
%%   & | & \mathtt{!}\mathit{ElementType}~(\mathtt{|}~\mathit{ElementType})^*\\[1ex]
%%   \mathit{NodePattern} & ::= & \mathit{NodeTest}~\mathit{AttributeQualifier}^*\\[1ex]
%%   \mathit{AttributeQualifier} & ::= & \mathtt{[@}\mathit{AttributeTest}\mathtt{]}\\
%%   & | & \mathtt{[!@}\mathit{AttributeTest}\mathtt{]}\\[1ex]
%%   \mathit{AttributeTest} & ::= & \mathit{AttributeName} \\
%%   & | & \mathit{AttributeName}\mathtt{='}\mathit{Text}\mathtt{'}
%% \end{eqnarray*}

\section{Insert Rules}

\paragraph{Begin Group:} $\{$

\begin{description}
  \item{\verb+table/cursor+}\\
   create a \texttt{row} node, create a \texttt{cell} node, create a \texttt{g} node,
    append the cursor to the \texttt{g} node, append the \texttt{g} node to the \texttt{cell} node,
    append the \texttt{cell} node to the \texttt{row} node, append the \texttt{row} node to the
    \texttt{c} node 
  \item{\verb+cursor+} \\ create a \texttt{g} node, replace the cursor with the new \texttt{g} node,
    append the cursor to the new \texttt{g} node
\end{description}

% CASE: c/g[!@id]/cursor

% CASE: c/cursor

% ELSE:

% do_begin:
% CASE: c[@table='1']/cursor
% ELSE:
%   create a g node with id, replace the cursor with the fresh g and append
%   the cursor as only child of it

\paragraph{End Group:} $\}$

\begin{description}
  \item{\verb+g[@id]/cursor+}\\
  remove the cursor, put $\tadvance$ after the \texttt{g} node
  \item{\verb+row/cell/g/cursor+}\\
  remove the cursor, put $\tadvance$ after the \texttt{row} node
  \item{\verb+math/g[!@id]/cursor+}\\
  ?
  \item{\verb+cursor+}\\
  error ?
\end{description}

\paragraph{Math Shift:} $\$$

\begin{description}
  \item{\verb+tex/cursor+}\\
  create a \texttt{math} node, create a \texttt{g} node, append the \texttt{g} node
    as child of the \texttt{math} node, append the cursor as child of the \texttt{g} node
  \item{\verb+math[@display='1']/g[!@id][*#]/cursor+}\\
  append the cursor as last child of the \texttt{math} node
  \item{\verb+math/g[!@id][*#]/cursor+}\\
  remove the cursor
  \item{\verb+math[!display='1']/g[!@id]/cursor+}\\
  set \verb+display='1'+ in the \texttt{math} node
  \item{\verb+math/g[!@id]+}\\
  append the cursor after the \texttt{math} node
  \item{\verb+math/cursor+}\\
  remove the cursor
  \item{\verb+cursor+} \\
  error ?
\end{description}

% do_shift:
% CASE: tex/cursor
%   create a math node. create a g node. append g as child of math.
%   append the cursor as child of g
% CASE: math[@display='1']/g[!@id][*#]/cursor
%   append the cursor as last child of math
% CASE: math/g[!@id][*#]/cursor
%   remove the cursor. Editing is done
% CASE: math[!display='1']/g[!@id]/cursor
%   set the display attribute to '1'
% CASE: math/g[!@id]
%   append the cursor after math (?)
% CASE: math/cursor
%   remove the cursor. Editing is done
% ELSE:
%   error

\paragraph{Align:} $\&$

\begin{description}
  \item{\verb+g[@id]/cursor+}\\
  create a \texttt{row} node, create a \texttt{cell} node, create a \texttt{g} node,
    append the cursor to the new \texttt{g} node, append the \texttt{cell} node to the
    the \texttt{row} node ?
  \item{\verb+row/cell/g/cursor+}\\
  create the \texttt{g} node, create the \texttt{cell} node, append the cursor
    as child of the new \texttt{g} node, append the new \texttt{g} node to the new
    \texttt{cell} node after the old \texttt{cell} node
  \item{\verb+cursor+}\\
  error
\end{description}

% do_align:
% CASE: g[@id]/cursor
%   create a row node. create a cell node. create a g node. append the
%   cursor to g, append the g to cell, append the cell to row, ???
% CASE: row/cell/g/cursor
%   create a g node. create a cell node. appent the cursor to g,
%   append the g to cell, insert the new cell after the existing cell
% ELSE:
%   error

\paragraph{End-of-line:}

% do_eol:
%   ignored

\paragraph{Parameter:} $p(i)$
% do_parameter:
%   ignored

\paragraph{Superscript:} $\uparrow$

\begin{description}
  \item{\verb+<g|p>[^#]/cursor+}\\
  create a \SP{} node, create a \G{} node, replace the cursor with the \SP{} node,
    append the \G{} node as first child of the \SP{} node, append the cursor as last
    child of the \SP{} node
  \item{\verb+<g|p>[*#]/cursor+}\\
  create a \SP{} node, replace \texttt{*} with the \SP{} node, append \texttt{*} to
    the \SP{} node, append cursor to the \SP{} node
  \item{\verb+sp[^*#$][!@over='1']/cursor+}\\ %$
  set \verb+over='1'+ in the \SP{} node
  \item{\verb+sp[^*#$][@over='1']/cursor+}\\ %$
  error
  \item{\verb+cursor+}\\
  error ?
\end{description}
% do_superscript:
% CASE: g[^#]/cursor
%   create sp node. create g node, replace cursor with sp, append g to sp, append cursor to sp
% CASE: g[*#]/cursor
%   create sp node, replace * with sp, append * to sp, append cursor to sp
% CASE: sp[^*#$][!@over='1']/cursor
%   set over='1' in sp node
% CASE: sp[^*#$][@over='1']/cursor
%   error
% ELSE:
%   error ?

\paragraph{Subscript:} $\downarrow$

\begin{description}
  \item{\verb+<g|p>[^#]/cursor+}\\
  create a \SB{} node, create a \G{} node, replace the cursor with the \SB{} node,
    append the \G{} node as first child of the \SB{} node, append the cursor as last
    child of the \SB{} node
  \item{\verb+<g|p>[*#]/cursor+}\\
  create a \SB{} node, replace \texttt{*} with the \SB{} node, append \texttt{*} to
    the \SB{} node, append cursor to the \SB{} node
  \item{\verb+sb[^*#$][!@under='1']/cursor+}\\ %$
  set \verb+under='1'+ in the \SB{} node
  \item{\verb+sb[^*#$][@under='1']/cursor+}\\ %$
  error
  \item{\verb+cursor+}\\
  error ?
\end{description}
% do_subscript:
% CASE: g[^#]/cursor
%   create sb node. create g node, replace cursor with sb, append g to sb, append cursor to sb
% CASE: g[*#]/cursor
%   create sb node, replace * with sb, append * to sb, append cursor to sb
% CASE: sb[^*#$][!@under='1']/cursor
%   set over='1' in sb node
% CASE: sb[^*#$][@under='1']/cursor
%   error
% ELSE:
%   error ?

\paragraph{Ignorable space:} $\square$

% do_ignorable_space:
%   do nothing?

\paragraph{Space:} $s$

\begin{description}
  \item{\verb+cursor+}\\
  create \SNODE{} node, replace cursor with the \SNODE{} node, append
  $\tadvance$ after \SNODE{} node
\end{description}

% do_space
%   create s node, replace cursor with s, append \advance after s

\paragraph{Identifier:} $i(v)$

\begin{description}
  \item{\verb+cursor+}\\
  create an \INODE{}, set \verb+value=+$v$ in the \INODE{}, replace
  cursor with \INODE{}, append $\tadvance$ after the \INODE{} node
\end{description}

% do_identifier
%   create i node, replace cursor with i, append \advance after i

\paragraph{Number:} $n(v)$

\begin{description}
  \item{\verb+cursor+}\\
  create an \NNODE{}, set \verb+value=+$v$ in the \NNODE{}, replace
  cursor with \NNODE{}, append $\tadvance$ after the \NNODE{} node
\end{description}

% do_number
%   create n node, replace cursor with n, append \advance after n

\paragraph{Apostrophe:} $o({}')$

\begin{description}
  \item{\verb+<g/p>[(sp[*#$]/g[o[@name='prime']$])#]/cursor+}\\
  create a \ONODE{} node, set \verb+name='prime'+ in the \ONODE{},
    append the \ONODE{} to the innermost \G{} node
  \item{\verb+<g|p>[(sb[^sp[^*#$]/g[o[@name='prime']]$])#]/cursor+}\\
  create a \ONODE{} node, set \verb+name='prime'+ in the \ONODE{},
    append the \ONODE{} to the innermost \G{} node
  \item{\verb+<g|p>[*#]/cursor+}\\
  create a \ONODE{} node, set \verb+name='prime'+ in the \ONODE{},
    create a \SP{} node, create a \G{} node, replace \texttt{*} with \SP{} node,
    append the new \G{} node to the \SP{} node, append the \ONODE{}
    node to the new \G{} node
  \item{\verb+<g|p>[^#]/cursor+}\\
  error?
  \item{\verb+cursor+}\\
  cursor is not in a group, error?
\end{description}

% do_apostrophe
% CASE: g[(sp[^*#$]/g[o[@name='prime']$])#]/cursor
%   append a new o[@name='prime'] node to the inner g node
% CASE: g[(sb[^sp[^*#$]/g[o[@name='prime']]$])#]/cursor
%   append a new o[@name='prime'] node to the inner g node
% CASE: g[*#]/cursor
%   create sp node, create g node, replace * with sp, append * to sp, append g to sp,
%   append a new o[@name='prime'[ node to the new g node
% CASE: g[^#]/cursor
%   error?
% ELSE:
%   cursor is not in a group, error?

\paragraph{Other:} $o(v)$

create an \ONODE{}, set \verb+value=+$v$ in the \ONODE{}, replace
cursor with \ONODE{}, append $\tadvance$ after the \ONODE{} node

% do_other
%   create o node, replace cursor with o, append \advance after o

\paragraph{Active:} $\sim$

% do_active:
%   ignored ???

\paragraph{Comment:} $\%$

% do_comment:
%   ignored ???

\paragraph{Begin Environment:} $c(\mathtt{begin})\langle\alpha_1,\dots,\alpha_n\rangle$

\paragraph{End Environment:} $c(\mathtt{end})\langle\rangle$

\paragraph{Left Delimiter:} $c(\mathtt{left})\langle\alpha\rangle$

\paragraph{Right Delimiter:} $c(\mathtt{right})\langle\alpha\rangle$

\paragraph{Carriage-Return:} $c(\mathtt{cr})\langle\rangle$

\begin{description}
  \item{\verb+row/cell/g/cursor+}\\
  create a \ROW{} node, create a \CELL{} node, create a \G{}
    node, append the cursor to the new \G{} node, append the new \G{}
    node to the new \CELL{} node, append the new \CELL{} node to the
    new \ROW{} node, insert the new \ROW{} node after the old \ROW{} node
  \item{\verb+cursor+}\\
  ignored?
\end{description}

% do_cr:
% CASE: row/cell/g/cursor
%   create row node, create cell node, create g node,
%   append cursor to g, append g to cell, append cell to row,
%   insert new row after old row
% ELSE:
%   ignored ???

\paragraph{Macro:} $c(v)\langle\alpha_1,\dots,\alpha_n\rangle$

\begin{description}
  \item{\verb+<p|g>/cursor+}\\
  create a \CNODE{} node with the children corresponding to the pattern
  $\tmap{\alpha_1}$,\dots,$\tmap{\alpha_n}$, replace the cursor with
  the new \CNODE{} node. put $\tnext$ as the first child of the new
  \CNODE{} node

  \item{\verb+*/cursor+}\\
  create a \CNODE{} node with the children corresponding to the pattern
  $\tmap{\alpha_1}$,\dots,$\tmap{\alpha_n}$, replace the cursor with
  the new \CNODE{} node, put $\tnext$ as the first child of the new
  \CNODE{} node. If $n\ne0$ emit a warning (the macro has arguments but
  but the context wouldn't normally allow them to be entered)
\end{description}

% do_macro:
% CASE: g/cursor
%   create a c node with children corresponding to the pattern of the macro
%   append \nextparam as first child of the macro

\section{Left Drop Rules}

\paragraph{Normal Left Drop:} $\NLDROP$

\begin{description}

  %********* rules that try to express situations completely specified (there is an effective deletion) ******

  % in the rules below, a token is either an i node, an n node, an o node, an s node or an empty c node.
  % an empty c node is either an undefined macro or an empty macro. These c node are handled as they actually were 
  % tokens (i, n, o, s).
  % An important observation is: a sequence of groups with id, in which every group has one and only one child and where 
  % the last group contains the cursor, is equivalent to the cursor (Is it clear?). For example:
  % <g id="id1"><g id="id2">...<g id="idn"><cursor/></g>...</g></g> is equivalent to:
  % <cursor/>

  % there is an optional sequence of groups with id, all of which having one and only one child. The last
  % child of this sequence (or the root, if the sequence is empty) is a group with id, has as first element a
  % token and has as second and last element an eventually empty sequence of groups with id, all of which having
  %one and only one child. The last element of this sequence has the cursor as its child.
  \item{\verb+(g[@id][^#$]/++\verb+)?g[@id][^(i|n|o|s|c[ ])#$]/(g[@id][^#$]/++\verb+)?cursor+}\\
  replace the whole fragment with the cursor.

  % the cursor is a macro's child. There is an eventually empty sequence of delimiters, which precedes the
  % cursor. Before this sequence, there is a p node. This p node has as its last child a token
  \item{\verb+c[p[(i|n|o|s|c[ ])$]((i|n|o|s|c[ ])*)#]/cursor+}\\
  remove the cursor and replace the token with the cursor.

  % there is a token, which precedes the cursor. The cursor is child of either a group or a parameter.
  % the case where the cursor is child of a group with id, there are no nodes after the cusror and there 
  % are no nodes preceding the token, is handled by the first rule.
  % This rule is duplicated below, but that one could have a wrong syntax. This one has been tested.
  \item{\verb+<g|p>[(i|n|o|c[ ])#]/cursor+}\\
  remove the cursor and replace the token with the cursor.

  % cursor's parent is a script (the cursor is not the base). The script's base is an empty group with id.
  % The script is preceded by a token.The script's parent is group or a parameter.
  % The case where the script's parent is a group with id and the token is the first child, while 
  % the script is the last one, is handled some rules later.
  \item{\verb+<g|p>[(i|n|o|s|c[ ])#]/<sp|sb>[^(g[@id][ ])#$]/cursor+}\\
  remove the script and replace the token with the cursor.

  % As above, but: we don't care who precedes the script (we do not have to remove it); the script's base 
  % is still an empty group, but it has no id.
  % Ad above, we have to precise that the situation where the script is the only child of a group with id
  % is handled some rules later.
  \item{\verb+<g|p>/<sp|sb>[^(g[!@id][ ])#$]/curosr+}\\
  replace the script with the cursor.

  % The cursor is the last child of a script. The script's base is token.
  \item{\verb+<g|p>/<sp|sb>[^(i|n|o|s|c[ ])#$]/cursor+}\\
  repalce the script with the cursor.
  

  %*******************************************************************************************************
  %************** rules handling the case in which the cursor has a preceding node ***********************
  %*******************************************************************************************************

  %************************* the cursor's parent is a group or a parameter *******************************

  %we consider an empty macro as a token. An empty macro is either undefined or simply empty.

  %rule handling the case where the cursor has a preceding token and this is the first node of a group with id
  \item{\verb+g[@id][^(i||n||o||s||c[^$])#]/cursor+}\\
  remove the cursor and replace the \INODE{}, \NNODE{}, \ONODE{}, \SNODE{} or \CNODE{} node with the $\NLDROP$.
  \item{\verb+<g|p>[(i||n||o||s||c[^$])#]/cursor+}\\
  remove the \INODE{}, \NNODE{}, \ONODE{}, \SNODE{} or \CNODE{} node.
  %it corresponds to the drop_prev_group(false).
  \item{\verb+<g|p>[g#]/cursor+}\\
  remove the cursor and append the $\NLDROP$ to the \G{} node.
  %corresponds to the drop_prev_script(false).
  \item{\verb+<g|p>[(<sp|sb>)#]/cursor+}\\
  remove the cursor and append the $\NLDROP$ to the \SP{} or \SB{} node.
  %rules handling the right open macros preceding the cursor.
  \item{\verb+<g|p>[(c[^p[@right-open='1']$])#]/cursor+}\\
  remove the cursor and append the $\NLDROP$ to the \PNODE{} node.
  %we don't have a macro of this kind, but we don't know the future...
  \item{\verb+<g|p>[(c[^p[!(@right-open='1')][@left-open='1'][^$]$])#]/cursor+}\\
  remove the cursor and replace the \CNODE node with the $\NLDROP$.
  \item{\verb+<g|p>[(c[^p[!(@right-open='1')][@left-open='1']$])#]/cursor+}\\
  replace the \CNODE{} node with the content of the \PNODE node and replace the cursor with the $\NLDROP$.
  %rules handling macro with parameter(s) and preceding the cursor.
  \item{\verb+<g|p>[c#]/cursor+}\\
  remove the cursor and append the $\NLDROP$ to the \CNODE{} node.
  %rule handling table with rows preceding the cursor.
  \item{\verb+<g|p>[(table[row[cell[^g$]$]$])#]/cursor+}\\
  remove the cursor and append the $\NLDROP$ to the \G{} node, which is child of the \CELL{} node.
  %rule handling tables without rows preceding the cursor.
  \item{\verb+<g|p>[table#]/cursor+}\\
  remove the cursor and append the $\NLDROP$ to the \TABLE{} node

  %********************************* the cursor's parent is a script ******************************

  %rule handling the case where the script's base is an empty group with id.
  \item{\verb+<sp|sb>[^(g[@id][^$])#$]/cursor+}\\
  replace the \SP{} or \SB{} node with the $\NLDROP$.
  %rule handling the case where the script's base is an empty group without id
  \item{\verb+<sp|sb>[^(g[^$])#$]/cursor+}\\
  replace the \SP{} or \SB{} node with the cursor.
  %rule handling the case where the script's base is something else
  \item{\verb+<sp|sb>[^*#$]/cursor+}\\
  replace the \SP{} or \SB{} node with it's first child and insert the $\NLDROP$ after it.

  %********************************* the cursor's parent is a macro ********************************

  %rule handling the case where the preceding node is a parameter
  \item{\verb+c[p#]/cursor+}\\
  remove the cursor and append the $\NLDROP$ to the PNODE{} node.
  %rule handling the case where the preceding node is a delimiter
  \item{\verb+c[(<i|n|o|s|c>)#]/cursor+}\\
  remove the cursor and insert the $\NLDROP$ before the \INODE{}, \NNODE{}, \ONODE{}, \SNODE{} or \CNODE{} node.

  %*************************************************************************************************
  %*********** rules handling the case in which the cursor has no preceding nodes ******************
  %*************************************************************************************************

  %rule handling the case where the cursor has something else after it
  \item{\verb+<g|p>[#*]/cursor+}\\
  nothing to drop.

  %rules handling the case where the cursor's parent is a p node and the cursor has no nodes after it.

  %rule handling the case where the p node is a left and right open macro's child and the first p node has no elements.
  \item{\verb+c[^(p[@left-open='1'][^$])#$]/p[@right-open='1'][^#$]/cursor+}\\
  replace the \CNODE{} with the $\NLDROP$.
  %rule handling the case where the p node is a left and right open macro's child and the first p node has elements.
  \item{\verb+c[^(p[@left-open='1']#$]/p[@right-open='1'][^#$]/cursor+}\\
  replace the \CNODE{} with the content of the PNODE{} with attribute texttt{left-open='1'} and insert $\NLDROP$ after this.
  %rule handling the case where the p node is right open macro's child.
  \item{\verb+c[^#$]/p[@right-open='1'][^#$]/cursor+}\\
  replace the \CNODE{} with the $\NLDROP$.
  %rule handling the case where the p node is a macro's child and this macro has parameter.
  \item{\verb+c/p[^#$]/cursor+}\\
  remove the cursor and insert the $\NLDROP$ before the \PNODE{} node.
  
  %rule handling the case where the cursor has no nodes after it and its parent is a group with id
  \item{\verb+g[@id][^#$]/cursor+}\\
  replace the \G{} node with the $\NLDROP$.

\end{description}

\paragraph{Special Left Drop:} $\SLDROP$

\begin{description}

  %*******************************************************************************************************
  %************** rules handling the case in which the cursor has a preceding node ***********************
  %*******************************************************************************************************

  %************************* the cursor's parent is a group or a parameter *******************************

  %this rule is more specific than the one below which handle the case of the cursor preceded by a c node
  \item{\verb+<g|p>[(i||n||o||s||c[^$])#]/cursor+}\\
  remove the \INODE{}, \NNODE{}, \ONODE{}, \SNODE{} or \CNODE{} node.
  %it corresponds to the drop_prev_group(true).
  \item{\verb+<g|p>[g#]/cursor+}\\
  remove the cursor and append the it to the \G{} node.
  %corresponds to the drop_prev_script(true).
  \item{\verb+<g|p>[(<sp|sb>)#]/cursor+}\\
  remove the cursor and append the $\SLDROP$ to the \SP{} or \SB{} node.
  %rules handling the right open macros preceding the cursor.
  \item{\verb+<g|p>[(c[^p[@right-open='1']$])#]/cursor+}\\
  remove the cursor and append the $\SLDROP$ to the \PNODE{} node.
  %we don't have a macro of this kind, but we don't know the future...
  \item{\verb+<g|p>[(c[^p[!(@right-open='1')][@left-open='1'][^$]$])#]/cursor+}\\
  remove the cursor and replace the \CNODE node with the cursor.
  \item{\verb+<g|p>[(c[^p[!(@right-open='1')][@left-open='1']$])#]/cursor+}\\
  replace the \CNODE{} node with the content of the \PNODE node.
  %rule handling macro with parameter(s) and preceding the cursor.
  \item{\verb+<g|p>[c#]/cursor+}\\
  remove the cursor and append the $\SLDROP$ to the \CNODE{} node.
  %rule handling table with rows preceding the cursor.
  \item{\verb+<g|p>[(table[row[cell[^g$]$]$])#]/cursor+}\\
  remove the cursor and append the it to the \G{} node, which is child of the \CELL{} node.
  %rule handling tables without rows preceding the cursor.
  \item{\verb+<g|p>[table#]/cursor+}\\
  remove the cursor and append the $\SLDROP$ to the \TABLE{} node

  %********************************* the cursor's parent is a script ******************************

  %rule handling the case where the script's base is an empty group with id.
  \item{\verb+<sp|sb>[^(g[@id][^$])#$]/cursor+}\\
  replace the \SP{} or \SB{} node with the \G{} node and insert the cursor after it.
  %rule handling the case where the script's base is an empty group without id
  \item{\verb+<sp|sb>[^(g[^$])#$]/cursor+}\\
  replace the \SP{} or \SB{} node with the cursor.
  %rule handling the case where the scrip's base is something else
  \item{\verb+<sp|sb>[^*#$]/cursor+}\\
  replace the \SP{} or \SB{} node with it's first child and insert the cursor after it.

  %********************************* the cursor's parent is a macro ********************************

  %rule handling the case where the preceding node is a parameter
  \item{\verb+c[p#]/cursor+}\\
  remove the cursor and append the $\SLDROP$ to the PNODE{} node.
  %rule handling the case where the preceding node is a delimiter
  \item{\verb+c[(<i|n|o|s|c>)#]/cursor+}\\
  remove the cursor and insert the it before the \INODE{}, \NNODE{}, \ONODE{}, \SNODE{} or \CNODE{} node.

  %*************************************************************************************************
  %*********** rules handling the case in which the cursor has no preceding nodes ******************
  %*************************************************************************************************

  %rule handling the case where the cursor has something else after it
  \item{\verb+<g|p>[#*]/cursor+}\\
  nothing to drop.

  %rules handling the case where the cursor's parent is a p node and the cursor has no nodes after it.

  %rule handling the case where the p node is a left and right open macro's child and the first p node has no elements.
  \item{\verb+c[^(p[@left-open='1'][^$])#$]/p[@right-open='1'][^#$]/cursor+}\\
  replace the \CNODE{} with the cursor.
  %rule handling the case where the p node is a left and right open macro's child and the first p node has elements.
  \item{\verb+c[^(p[@left-open='1']#$]/p[@right-open='1'][^#$]/cursor+}\\
  replace the \CNODE{} with the content of the PNODE{} with attribute texttt{left-open='1'} and insert cursor after this.
  %rule handling the case where the p node is right open macro's child.
  \item{\verb+c[^#$]/p[@right-open='1'][^#$]/cursor+}\\
  replace the \CNODE{} with the cursor.
  %rule handling the case where the p node is a macro's child and this macro has parameter.
  \item{\verb+c/p[^#$]/cursor+}\\
  remove the cursor and insert the $\SLDROP$ before the \PNODE{} node.
  
  %rule handling the case where the cursor has no nodes after it and its parent is a group with id
  \item{\verb+g[@id][^#$]/cursor+}\\
  replace the \G{} node with the cursor.

\end{description}

\section{Right Drop Rules}

\section{$\varepsilon$-rules}

\paragraph{Nromal Left Drop}

\begin{description}


  %********************************************* \NLDROP has a preceding node *********************************************

  %rule handling the case where the preceding node is a token or an empty macro.
  \item{\verb+*[(i||n||o||s||c[^$])#]/+$\NLDROP$}\\
  remove the \INODE{}, \NNODE{}, \ONODE{}, \SNODE{} or the \CNODE{} node.
  %rule handling the case where the preceding node is a group
  \item{\verb+*[g#]/+$\NLDROP$}\\
  remove the $\NLDROP$ append it to the \G{} node.
  %rule handling the case where the preceding node is a script
  \item{\verb+*[<sp|sb>#]/+$\NLDROP$}\\
  remove the $\NLDROP$ append it to the \SP{} or \SB{} node.
  %rule handling the case where the preceding node is an open-right macro
  \item{\verb+*[(c[^p[@right-open='1']$])#]/+$\NLDROP$}\\
  remove the $\NLDROP$ and append it to the \PNODE{} node.
  %rule handling the case where the preceding node is a left-open macro (and not right-open), and the p node of this macro has no children
  \item{\verb+*[(c[^p[!(@right-open='1')][@left-open='1'][^$]$])#]/+$\NLDROP$}\\
  remove the \CNODE node.
  %rule handling the case where the preceding node is a left-open macro (and not right-open), and the p node of this macro has children
  \item{\verb+*[(c[^p[!(@right-open='1')][@left-open='1']$])#]/+$\NLDROP$}\\
  replace the \CNODE{} node with the content of the \PNODE{} node.
  %rule handling the case where the preceding node is a macro with parameter(s)
  \item{\verb+*[c#]/+$\NLDROP$}\\
  remove the $\NLDROP$ and append it to the \CNODE{} node.

  %rule handling the deletion of primes in superscript

  %there are more than one prime in the phantom group
  \item{\verb+sp[!(@id)][*#$]/g[!(@id)][o#]/+$\NLDROP$}\\
  remove the \ONODE node, remove the $\NLDROP$ and insert the cursor after the \SP{} node.
  %there is one and only one prime in the phantom group
  \item{\verb+sp[!(@id)][*#$]/g[!(@id)][^o#]/+$\NLDROP$}\\
  replace the \SP{} node with it's first child and insert the cursor after it

  %*** rules handling the case where the \NLDROP's parent is a macro (with parameter) ***

  %the node preceding the \NLDROP is a delimiter
  \item{\verb+c[(<i|n|o|s|c>)#]/+$\NLDROP$}\\
  remove the $\NLDROP$ and insert it before the \INODE{}, \NNODE{}, \ONODE{}, \SNODE{} or \CNODE{} node.
  %the node preceding the \NLDROP is parameter
  \item{\verb+c[p#]/+$\NLDROP$}\\
  remove the $\NLDROP$ and append it to the \PNODE{} node.

  %********************************************* the \NLDROP has no preceding nodes ****************************************************

  %if the \NLDROP has no preceding nodes, but has following nodes...
  \item{\verb+<g|p>[#*]/+$\NLDROP$}\\
  replace the $\NLDROP$ with the cursor.
  %rule handling the case where the $\NLDROP$ is the only child of a group with id.
  \item{\verb+g[@id][^#$]/+$\NLDROP$}\\
  replace the \G{} node with the $\NLDROP$.
  %\NLDROP is the first child of a macro with no inserted parameter
  \item{\verb+c[!(p[*])][^#]/+$\NLDROP$}\\
  replace the \CNODE{} with the $\NLDROP$.
  %\NLDROP is the first child of a macro with some inserted parameter
  \item{\verb+c[p[*]][^#]/+$\NLDROP$}\\
  put the cursor in the first \PNODE{} node.
  

  %**** rule handling the case where the \NLDROP has a preceding token or a preceding empty macro, which is either undefined or empty ****

  %%the \NLDROP's parent is neither a macro nor a phantom group which in turn is a sp's child and the token is a prime
  %\item{\verb+*[(i||n||o||s||c[^$])#]/+$\NLDROP$}\\
  %replace the \INODE{}, \NNODE{}, \SNODE{}, \ONODE{}, \SNODE{} or \CNODE{} node with the cursor and remove the $\NLDROP$.
  %%the \NLDROP's parent is a macro with argument
  %\item{\verb+c[((!(p[@left-open='1']))&(!(p[@right-open+'1'])))][(i||n||o||s||c[^$])#]/+$\NLDROP$}\\
  %remove the $\NLDROP$ and insert it before the \INODE{}, \NNODE{}, \ONODE{}, \SNODE{} or \CNODE{} node.
  %%rule handling the case where the \NLDROP's parent is a phantom group, which in turn is a sp's child
  %\item{\verb+sp[!(@id)][^*#$]/g[!(@id)][o#$]/+$\NLDROP$}\\
  %remove the \ONODE{} node

  %********************************* rules handling the case where the \NLDROP has a preceding group **************************************
  \item{\verb+*[g#]/+$\NLDROP$}\\
  remove the $\NLDROP$ and append it to the \G{} node.

\end{description}

\paragraph{Special Left Drop}

\begin{description}

  \item{special left}\\

\end{description}

\paragraph{Advance}

\begin{description}
  \item{\verb+g/+$\tadvance$}\\
  replace $\tadvance$ with the cursor

  \item{\verb+p[#$]/+$\tadvance$}\\ %$
  put $\tadvance$ after the \PNODE{} node

  \item{\verb+c[#p]/+$\tadvance$} \\
  remove $\tadvance$, put the cursor as first child of the \PNODE{} node

  \item{\verb+c[#*]/+$\tadvance$} \\ %$
  replace $\tadvance$ with the cursor 

  \item{\verb+c[#$]/+$\tadvance$} \\ %$
  move $\tadvance$ after the \CNODE{} node
\end{description}

\paragraph{Next Parameter}

\paragraph{Next Token}

%% \begin{description}
%%   \item{\verb+c[#p]/+$\tnext$} \\
%% \end{description}

% g[@id]/(c[#$][@right-open]/g[!@id][#$]/)+cursor  }   let p = cursor.parent() in remove; advance(p)

% c/g[!@id]/cursor
% c/cursor 
% */cursor  {   let g = new group in replace

% g[@id][^#$]/cursor  <=   cursor.parent().replace(cursor)
% g[@id][^#$]/cursor  <-   cursor
% (!g[@id][^#$])[A#B]/(g[@id][^#$]/)+cursor  <-  (!g[@id][^#$])[A#B]/cursor  

\end{document}
