summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/formal-grammar/formal-grammar.sty
blob: d7cc0b7f1c7ee58b8468fe14548f841cc1c888f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
%%
%% This is file `formal-grammar.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% formal-grammar.dtx  (with options: `package')
%% This is a generated file.
%% Copyright (C) 2021 by Martin Vassor
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3 of this license or (at your option) any later
%% version.  The latest version of this license is in:
%% 
%% http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%% 

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{formal-grammar}[2022/02/09 v1.2 A package to typeset formal grammars]
\RequirePackage{xparse}
\RequirePackage{newfloat}
\RequirePackage{xcolor}
\RequirePackage{colortbl}
\RequirePackage{array}
\definecolor{LightCyan}{rgb}{0.8,1,1}
%% \begin{abstract}
%%   The \textsf{formal-grammar} package provides commands to typeset formal grammars.
%% \end{abstract}
    \newif\if@formalalignsymbol\@formalalignsymbolfalse
    \DeclareOption{center}{
      \@formalalignsymboltrue
    }
    \ProcessOptions\relax
\DeclareFloatingEnvironment[
name=Grammar,
listname={List of Grammars},
placement=tbhp,
]{floatgrammar}
%% Taken from https://tex.stackexchange.com/a/26364/107341
\newcommand*{\formal@rowstyle}{}

\newcommand*{\rowstyle}[1]{% sets the style of the next row
  \gdef\formal@rowstyle{#1}%
  \formal@rowstyle\ignorespaces%
}
\newcolumntype{\formal@reset}{% resets the row style
  >{\gdef\formal@rowstyle{}}%
}

\newcolumntype{\formal@add}{% adds the current row style to the next column
  >{\formal@rowstyle}%
}
%% End of stackexchange
    \if@formalalignsymbol
      \newcolumntype{\formal@symbol}{c}
    \else
      \newcolumntype{\formal@symbol}{r}
    \fi
\ExplSyntaxOn
%% 1st argument: caption (makes it float)
%% 2nd argument: positionning option (`p` by default)
%% 3rd argument: label
\NewDocumentEnvironment{grammar} {o O{p} o}
{
\IfNoValueF{#1}{
\begin{floatgrammar}[#2]
\centering
}

\begin{tabular}{\formal@reset l \formal@add \formal@symbol \formal@add l \formal@add l}
}{
\end{tabular}

\IfNoValueF{#1}{
\caption{#1}
\IfNoValueF{#3}{
\label{#3}
}
\end{floatgrammar}
}
}
\ExplSyntaxOff

\newcommand{\firstcase}[3]{\(\mathcal{#1}\) & \(\formal@Coloneqq\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\}
\newcommand{\firstcasesubtil}[3]{#1 & \(\formal@Coloneqq\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\}

\newcommand{\otherform}[2]{& \(|\) & \(#1\) & {\itshape \color{gray!90!black} #2}\\}

\newcommand{\nonterm}[1]{\mathcal{#1}}

\newcommand{\nontermsubtil}[1]{#1}

\newcommand{\gralt}[0]{\quad |\quad }

\newcommand{\highlight}[0]{\rowcolor{LightCyan}}

\newcommand{\lochighlight}[1]{{\color{red} #1}}

\newcommand{\downplay}[0]{\rowstyle{\color{white!80!black}}}
\AtBeginDocument{%
\ifdefined\Coloneqq
\let\formal@Coloneqq\Coloneqq
\else
\newcommand{\formal@Coloneqq}{::=}
\fi
}
\endinput
%%
%% End of file `formal-grammar.sty'.