\documentclass[a4paper,oneside,svgnames]{scrbook}

\usepackage{microtype}
\usepackage[toc,page]{appendix}
% For pretty, well-behaved tables.
\usepackage{booktabs}
\usepackage{longtable}

\usepackage{textcomp}

% for arithmetic expression table
\usepackage{pifont}
\newcommand{\tick}{\ding{51}}
\usepackage{multirow}

% Erewhon is for math mode.
\usepackage[proportional,scaled=1.064]{erewhon}
\usepackage[erewhon,vvarbb,bigdelims]{newtxmath}
\usepackage{lmodern}
\usepackage{roboto}
\usepackage[T1]{fontenc}
\renewcommand*\oldstylenums[1]{\textosf{#1}}

\usepackage[dvipsnames]{xcolor}
\usepackage[tocindentauto]{tocstyle}
\usetocstyle{standard}
\usepackage[most]{tcolorbox} % for coloured boxes and warning boxes
\usetikzlibrary{patterns}
\usepackage[pdfauthor={Edward Hart},
            pdftitle={The GnuCOBOL 4.0-early-dev Grammar},
            pdfkeywords={GnuCOBOL,COBOL,manual,grammar,guide},
            colorlinks]{hyperref}

\newcommand{\clearpageifnotfirst}[1]{%
  \ifnum\value{#1}>0 \clearpage{} \fi%
}

%% Title page

\newtcolorbox{streak}[1][]{frame code={},
  halign title=flush center,
  left=40mm,
  right=40mm,
  top=10pt,
  bottom=10pt,
  colback=Maroon,
  colupper=white,
  left skip=40mm,
  grow to left by=40mm,
  right skip=40mm,
  grow to right by=40mm,
  width=\paperwidth,
  text width=\textwidth,
  arc=0pt,outer arc=0pt,
  #1
}

% \newtcolorbox{syntaxbox}[1][]{enhanced,
%   before skip=2mm,after skip=3mm,
%   boxrule=0.4pt,left=5mm,right=2mm,top=1mm,bottom=1mm,
%   colback=yellow!50,
%   colframe=yellow!20!black,
%   sharp corners,rounded corners=southeast,arc is angular,arc=3mm,
%   underlay={%
%     \path[fill=tcbcol@back!80!black] ([yshift=3mm]interior.south east)--++(-0.4,-0.1)--++(0.1,-0.2);
%     \path[draw=tcbcol@frame,shorten <=-0.05mm,shorten >=-0.05mm] ([yshift=3mm]interior.south east)--++(-0.4,-0.1)--++(0.1,-0.2);
%     \path[fill=yellow!50!black,draw=none] (interior.south west) rectangle node[white]{\Huge\bfseries !} ([xshift=4mm]interior.north west);
%     },
%   drop fuzzy shadow}

%% Links in changelog.

\newcommand{\bug}[1]{%
  \href{https://sourceforge.net/p/gnucobol/bugs/#1}{bug~\##1}%
}
\newcommand{\bugnoname}[1]{%
  \href{https://sourceforge.net/p/gnucobol/bugs/#1}{\##1}%
}
\newcommand{\fr}[1]{%
  \href{https://sourceforge.net/p/gnucobol/feature-requests/#1}{feature request~\##1}%
}
\newcommand{\frnoname}[1]{%
  \href{https://sourceforge.net/p/gnucobol/feature-requests/#1}{\##1}%
}
\newcommand{\patch}[1]{%
  \href{https://sourceforge.net/p/gnucobol/patches/#1}{patch~\##1}%
}

%% Syntax diagram stuff

\newtcolorbox{syntaxbox}[1][]{enhanced,
  boxrule=0.4pt,left=1mm,right=1mm,top=1mm,bottom=1mm,%
  sharp corners,% Rectangular shape
  halign=flush left,% To prevent spread out lines
  parbox=false,% To give normal paragraph indentation.
  colback=#1,% To allow different background colours.
  before upper={\parskip3pt\parindent15pt}%
}

\renewcommand{\arraystretch}{1.2}
\def\defaultparskip{\parskip}

\newenvironment{syntax}[1][black!5!white]{% black!5!white is the tcolorbox default
  \begin{syntaxbox}[#1]\small\selectfont\setlength{\parskip}{\baselineskip}}{%
  \setlength{\parskip}{\defaultparskip}\normalsize\selectfont\end{syntaxbox}}

\renewcommand{\familydefault}{\sfdefault}

\newcommand{\metaelement}[1]{%
  \textit{#1}%
}

\newcommand{\defdmetaelement}[1]{%
  \hyperlink{#1}{\textit{#1}}%
}

\newcommand{\defnmetaelement}[1]{%
  \hypertarget{#1}{\textit{#1}}%
}

% TO-DO: Add an optional argument specifying the value of counter.
% TO-DO: Add an optional argument specifying the name of a variable in which to save the counter value.

\newcommand{\newlexicalelement}[1]{%
  \newcounter{#1}[subsection]%
  \expandafter\newcommand\csname #1\endcsname{%
    \stepcounter{#1}%
    \metaelement{#1-\arabic{#1}}\ {}%
  }%
}

\newcommand{\newlexicalelementwithname}[2]{%
  \newcounter{#1}[subsection]%
  \expandafter\newcommand\csname #1\endcsname{%
    \stepcounter{#1}%
    \metaelement{#2-\arabic{#1}}\ {}%
  }%
}

\newcounter{format}[subsection]
\newcommand{\format}[1]{%
  \stepcounter{format}%
  \paragraph{Format \arabic{format}\vspace{1em}\ {}(#1)}\ {}\newline%
}

\newcommand{\key}[1]{\underline{#1}}
\newcommand{\directiveindicator}[0]{>{}>}

\newcommand{\deletedcolour}{red!75}
\newcommand{\deleted}[1]{%
  \colorbox{\deletedcolour}{#1}}

\newcommand{\archaiccolour}{pink}
\newcommand{\archaic}[1]{%
  \colorbox{\archaiccolour}{#1}}

\newcommand{\obsoletecolour}{red!60}
\newcommand{\obsolete}[1]{%
  \colorbox{\obsoletecolour}{#1}}

\newcommand{\xopencolour}{green!75}
\newcommand{\xopen}[1]{%
  \colorbox{\xopencolour}{#1}}

\newcommand{\gnucobolcolour}{orange!75}
\newcommand{\gnucobol}[1]{%
  \colorbox{\gnucobolcolour}{#1}}

\newcommand{\miscextcolour}{cyan!30!white}
\newcommand{\miscext}[1]{%
  \colorbox{\miscextcolour}{#1}}

\newcommand{\pending}[1]{%
  \tikz[baseline=(n.base)]{\node[
    pattern=north east lines, % overlay a pattern of north east lines
    pattern color=black!50!white,
    above=-1em
    ] (n)
    {#1};
  }
}

\newcommand{\standard}[1]{%
  \colorbox{white}{#1}}

\newenvironment{0-1}{$\left[ \begin{tabular}{@{}l@{}}}{\end{tabular} \right]$}
\newenvironment{0+}{$\left[\left| \begin{tabular}{@{}l@{}}}{\end{tabular} \right|\right]$}
\newenvironment{1=}{$\left\{ \begin{tabular}{@{}l@{}}}{\end{tabular} \right\}$}
\newenvironment{1+}{$\left\{\left| \begin{tabular}{@{}l@{}}}{\end{tabular} \right|\right\}$}

\begin{document}

\newlexicalelementwithname{arithmeticexpression}{arithmetic-expression}
\newlexicalelementwithname{conditionname}{condition-name}
\newlexicalelementwithname{filename}{file-name}
\newlexicalelementwithname{functionname}{function-name}
\newlexicalelementwithname{imperativestatement}{imperative-statement}
\newlexicalelementwithname{cobolindexname}{index-name}
\newlexicalelementwithname{libraryname}{library-name}
\newlexicalelementwithname{mnemonicname}{mnemonic-name}
\newlexicalelementwithname{procedurename}{procedure-name}
\newlexicalelementwithname{pseudotext}{pseudo-text}
\newlexicalelementwithname{recordname}{record-name}
\newlexicalelementwithname{reportname}{report-name}
\newlexicalelementwithname{sourcetext}{source-text}
\newlexicalelementwithname{textname}{text-name}
\newlexicalelementwithname{cdname}{cd-name}
\newlexicalelement{argument}
\newlexicalelement{condition}
\newlexicalelement{expression}
\newlexicalelement{identifier}
\newlexicalelement{integer}
\newlexicalelement{literal}
\newlexicalelementwithname{switchstatusname}{switch-status-name}
\newlexicalelementwithname{switchname}{switch-name}
\newlexicalelementwithname{systemname}{system-name}
\newlexicalelementwithname{computername}{computer-name}
\newlexicalelementwithname{programname}{program-name}
\newlexicalelementwithname{symboliccharacter}{symbolic-character}
\newlexicalelement{character}
\newlexicalelementwithname{hexcharacter}{hex-character}

\frontmatter

\begin{titlepage}
  \pagecolor{Maroon!75}

  \centering

  \vfill

  \begin{streak}[bottom=7.5pt]
    {\centering\fontsize{27pt}{0cm}\bfseries
      The GnuCOBOL 4.0-early-dev Grammar \par
    }
    {\centering\scshape\LARGE for r3967 \par}
  \end{streak}

  \vspace{3cm}
  \begin{streak}
    {\LARGE\itshape Edward Hart\par}
    \vspace{5pt}
    {\large\href{mailto:edward.dan.hart@gmail.com}{\color{white}{edward.dan.hart@gmail.com}}}
  \end{streak}

  \vfill

  \begin{streak}[top=3pt,bottom=3pt]
    {\Large \today\par}
  \end{streak}
\end{titlepage}

\ifpdf
\nopagecolor
\else
\pagecolor{white}
\fi

\topskip0pt
\vspace*{\fill}

COBOL is an industry language and is not the property of any company or group of companies, or of any organisation or group of organisations.

No warranty, expressed or implied, is made by any contributor, or by the CODASYL COBOL Committee,\footnote{The CODASYL COBOL committee was dissolved in 1992. Its work was continued by ANSI X3J4 and then INCITS PL22.4, which was itself dissolved in 2015.} as to the accuracy and functioning of the programming system and language. Moreover, no responsibility is assumed by any contributor, or by the committee, in connection therewith.

The authors and copyright holders of the copyrighted materials used herein are:
\begin{itemize}
\item \textit{FLOW-MATIC \textup{[trademark of Sperry Rand Corporation]} Programming for the UNIVAC \textregistered{} I and II Data Automation Systems}, copyrighted 1958, 1959 by Sperry Rand Corporation,\footnote{Sperry Rand's computer business is now part of Unisys.}
\item IBM Commercial Translator, Form No. F28-8013, copyrighted 1959 by IBM, and
\item FACT, DSI 27A5260-2760, copyrighted 1960 by Minneapolis-Honeywell.
\end{itemize}

They have specially authorised the use of this material, in whole or in part, in the COBOL specifications. Such authorisation extends to the reproduction and use of COBOL specifications in programming manuals or similar publications.

\vfill

\begin{center}
  This work is typeset in Roboto.
\end{center}

\vfill

\begin{center}
  Copyright \textcopyright{} \the\year{} Edward Hart

  \vspace{5pt}

  Permission is granted to copy, distribute and\slash{}or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. Your attention is drawn to the copy of the license in Appendix \ref{label_fdl}.
  \vspace{5pt}

  The moral rights of the author have been asserted.
\end{center}

\tableofcontents

\input{foreword}
\input{changelog}

\mainmatter

\input{key}

\preto\section{\clearpageifnotfirst{section}}

\input{language-fundamentals}
\input{concepts}

\preto\subsection{\clearpageifnotfirst{subsection}}

\input{directives}
\input{compilation-group}
\input{identification}
\input{environment}
\input{data}
\input{procedure}
\input{intrinsics}

\begin{appendices}
  \input{gfdl}
  \input{language-element-lists}
\end{appendices}

\end{document}
