| 1 |
\documentclass{article} |
| 2 |
\usepackage[margin=1in]{geometry} |
| 3 |
\usepackage{microtype} |
| 4 |
\usepackage{booktabs} |
| 5 |
\usepackage{hyperref} |
| 6 |
\usepackage{fox-listings} |
| 7 |
|
| 8 |
\title{\texttt{fox-listings} \\ \large Listings language definition |
| 9 |
for COSYScript} |
| 10 |
\author{Eremey Valetov\\\url{https://github.com/evvaletov}} |
| 11 |
\date{March 21, 2026\quad v1.4} |
| 12 |
|
| 13 |
\begin{document} |
| 14 |
\maketitle |
| 15 |
|
| 16 |
\section{Introduction} |
| 17 |
|
| 18 |
The \texttt{fox-listings} package provides a language definition for the |
| 19 |
\texttt{listings} package to typeset source code in COSYScript, the |
| 20 |
programming language of |
| 21 |
\href{https://cosyinfinity.org}{COSY INFINITY}, a beam dynamics code |
| 22 |
using high-order differential-algebraic (DA) transfer maps and methods, |
| 23 |
developed at Michigan State University. |
| 24 |
COSYScript is compiled and executed by the FOXY subsystem; source files |
| 25 |
use the \texttt{.fox} extension. This package uses the name \texttt{FOX} |
| 26 |
for the \texttt{listings} language identifier and style prefixes. |
| 27 |
|
| 28 |
The package defines six keyword groups that can be independently styled, |
| 29 |
supports nested \texttt{\{...\}} comments and single-quoted string literals, |
| 30 |
and provides two ready-made styles. |
| 31 |
|
| 32 |
\section{Usage} |
| 33 |
|
| 34 |
Load the package after \texttt{listings}: |
| 35 |
|
| 36 |
\begin{verbatim} |
| 37 |
\usepackage{fox-listings} |
| 38 |
\end{verbatim} |
| 39 |
|
| 40 |
The package automatically loads \texttt{listings} and \texttt{xcolor}. |
| 41 |
|
| 42 |
\subsection{Color style} |
| 43 |
|
| 44 |
\begin{verbatim} |
| 45 |
\begin{lstlisting}[style=FOXcolor] |
| 46 |
... |
| 47 |
\end{lstlisting} |
| 48 |
\end{verbatim} |
| 49 |
|
| 50 |
\subsection{Monochrome style} |
| 51 |
|
| 52 |
\begin{verbatim} |
| 53 |
\begin{lstlisting}[style=FOXmono] |
| 54 |
... |
| 55 |
\end{lstlisting} |
| 56 |
\end{verbatim} |
| 57 |
|
| 58 |
\subsection{Language only (custom style)} |
| 59 |
|
| 60 |
\begin{verbatim} |
| 61 |
\begin{lstlisting}[language=FOX] |
| 62 |
... |
| 63 |
\end{lstlisting} |
| 64 |
\end{verbatim} |
| 65 |
|
| 66 |
\subsection{Inline code} |
| 67 |
|
| 68 |
\begin{verbatim} |
| 69 |
\lstinline[language=FOX]{VARIABLE X 1 ;} |
| 70 |
\end{verbatim} |
| 71 |
|
| 72 |
\section{Keyword groups} |
| 73 |
|
| 74 |
Keywords are split into six groups so that each category can be styled |
| 75 |
independently. Groups~1--3 cover the general-purpose language (control |
| 76 |
flow, math, DA algebra), group~4 covers the beam-physics command set, |
| 77 |
group~5 covers the graphics subsystem, and group~6 covers built-in |
| 78 |
constants and global variables. The \texttt{FOXcolor} style assigns a |
| 79 |
distinct color to each group; the \texttt{FOXmono} style bolds groups~1, 2, and~6, and leaves the |
| 80 |
rest unstyled. |
| 81 |
|
| 82 |
All keywords have been cross-checked against the pristine COSY INFINITY |
| 83 |
v10 source (\texttt{cosy.fox} procedures, functions, and global variables) |
| 84 |
and the FOXY compiler intrinsic tables (\texttt{foxy.f}). Group~3 also |
| 85 |
includes five COSYFFAG-specific configuration procedures |
| 86 |
(\texttt{CONFIG\_SET}, etc.)\ that are not in the pristine distribution. |
| 87 |
|
| 88 |
\medskip |
| 89 |
\begin{tabular}{cll} |
| 90 |
\toprule |
| 91 |
Group & Category & Examples \\ |
| 92 |
\midrule |
| 93 |
1 & Control flow, declarations & \texttt{PROCEDURE}, \texttt{IF}, \texttt{VARIABLE}, \texttt{WRITE} \\ |
| 94 |
2 & Intrinsic functions & \texttt{SIN}, \texttt{SQRT}, \texttt{ABS}, \texttt{CONS}, \texttt{DA} \\ |
| 95 |
3 & Intrinsic procedures & \texttt{DAINI}, \texttt{VELSET}, \texttt{CONFIG\_SET} \\ |
| 96 |
4 & Beam physics & \texttt{OV}, \texttt{MQ}, \texttt{CR}, \texttt{FR}, \texttt{ER} \\ |
| 97 |
5 & Graphics & \texttt{GRMOVE}, \texttt{GRDRAW}, \texttt{GREPS}, \texttt{PP} \\ |
| 98 |
6 & Constants/globals & \texttt{PI}, \texttt{CLIGHT}, \texttt{MAP}, \texttt{RAY} \\ |
| 99 |
\bottomrule |
| 100 |
\end{tabular} |
| 101 |
\medskip |
| 102 |
|
| 103 |
\section{Examples} |
| 104 |
|
| 105 |
\subsection{Color style (\texttt{FOXcolor})} |
| 106 |
|
| 107 |
\begin{lstlisting}[style=FOXcolor] |
| 108 |
INCLUDE 'COSY' ; |
| 109 |
|
| 110 |
PROCEDURE GREET A B ; |
| 111 |
VARIABLE C 1 ; |
| 112 |
C := A + B ; |
| 113 |
WRITE 6 'Sum =' C ; |
| 114 |
ENDPROCEDURE ; |
| 115 |
|
| 116 |
PROCEDURE RUN ; |
| 117 |
VARIABLE X 1 ; |
| 118 |
VARIABLE Y 1 ; |
| 119 |
VARIABLE B 1 ; |
| 120 |
|
| 121 |
{Compute and display a value} |
| 122 |
X := SIN(0.5) ; |
| 123 |
Y := SQRT(X) + 1 ; |
| 124 |
WRITE 6 'Result:' Y ; |
| 125 |
|
| 126 |
GREET 3 4 ; |
| 127 |
|
| 128 |
OV 3 2 0 ; |
| 129 |
RP 10000 1.00728 1 ; |
| 130 |
B := 0.01*CONS(CHIM)*0.05 ; |
| 131 |
UM ; |
| 132 |
MQ 0.5 B 0.05 ; |
| 133 |
DL 24 ; |
| 134 |
MQ 1 -B 0.05 ; |
| 135 |
DL 24 ; |
| 136 |
MQ 0.5 B 0.05 ; |
| 137 |
CR ; |
| 138 |
ENDPROCEDURE ; |
| 139 |
RUN ; END ; |
| 140 |
\end{lstlisting} |
| 141 |
|
| 142 |
\subsection{Monochrome style (\texttt{FOXmono})} |
| 143 |
|
| 144 |
\begin{lstlisting}[style=FOXmono] |
| 145 |
INCLUDE 'COSY' ; |
| 146 |
|
| 147 |
PROCEDURE RUN ; |
| 148 |
VARIABLE KF 1 ; VARIABLE KD 1 ; VARIABLE OBJ 1 ; |
| 149 |
|
| 150 |
OV 1 2 0 ; |
| 151 |
RP 10000 1.00728 1 ; |
| 152 |
KF := 0.01*CONS(CHIM)*0.05 ; |
| 153 |
KD := -0.01*CONS(CHIM)*0.05 ; |
| 154 |
|
| 155 |
FIT KF KD ; |
| 156 |
UM ; |
| 157 |
MQ 0.5 KF 0.05 ; |
| 158 |
DL 24 ; |
| 159 |
MQ 1 KD 0.05 ; |
| 160 |
DL 24 ; |
| 161 |
MQ 0.5 KF 0.05 ; |
| 162 |
OBJ := (ME(1,2))^2 + (ME(3,4))^2 ; |
| 163 |
ENDFIT 1E-10 100 1 OBJ ; |
| 164 |
|
| 165 |
WRITE 6 'Matched KF:' KF ; |
| 166 |
WRITE 6 'Matched KD:' KD ; |
| 167 |
ENDPROCEDURE ; |
| 168 |
RUN ; END ; |
| 169 |
\end{lstlisting} |
| 170 |
|
| 171 |
\subsection{Tune extraction} |
| 172 |
|
| 173 |
\begin{lstlisting}[style=FOXcolor] |
| 174 |
INCLUDE 'COSY' ; |
| 175 |
|
| 176 |
PROCEDURE COMPUTE_TUNES ; |
| 177 |
VARIABLE MU 100 2 ; |
| 178 |
VARIABLE B 1 ; |
| 179 |
B := 0.01*CONS(CHIM)*0.05 ; |
| 180 |
|
| 181 |
{Build a FODO cell transfer map} |
| 182 |
UM ; |
| 183 |
MQ 0.5 B 0.05 ; |
| 184 |
DL 24 ; |
| 185 |
MQ 1 -B 0.05 ; |
| 186 |
DL 24 ; |
| 187 |
MQ 0.5 B 0.05 ; |
| 188 |
|
| 189 |
{Extract tunes} |
| 190 |
TP MU ; |
| 191 |
WRITE 6 'Tunes: '&SF(CONS(MU(1)),'(F8.5)')&' '&SF(CONS(MU(2)),'(F8.5)') ; |
| 192 |
ENDPROCEDURE ; |
| 193 |
|
| 194 |
PROCEDURE RUN ; |
| 195 |
OV 3 2 0 ; |
| 196 |
RP 10000 1.00728 1 ; |
| 197 |
COMPUTE_TUNES ; |
| 198 |
ENDPROCEDURE ; |
| 199 |
RUN ; END ; |
| 200 |
\end{lstlisting} |
| 201 |
|
| 202 |
\subsection{Including an external file} |
| 203 |
|
| 204 |
Use \verb|\lstinputlisting| to typeset an external \texttt{.fox} source file: |
| 205 |
|
| 206 |
\begin{verbatim} |
| 207 |
\lstinputlisting[style=FOXcolor, caption={Simulation program}, |
| 208 |
firstline=1, lastline=30]{EEFFAGsim.fox} |
| 209 |
\end{verbatim} |
| 210 |
|
| 211 |
\section{Known limitations} |
| 212 |
|
| 213 |
The \texttt{listings} package does not highlight in-code numbers for |
| 214 |
user-defined languages. Fortran-style \texttt{D}-exponent notation |
| 215 |
(e.g., \texttt{1.5D-3}) and standard decimal literals (\texttt{0.5}, |
| 216 |
\texttt{1E-3}) are rendered in the base style. |
| 217 |
|
| 218 |
The \texttt{listings} tokenizer has no scope or context awareness, so a user |
| 219 |
variable that shares its name with a keyword (e.g., \texttt{OV}, \texttt{MQ}, |
| 220 |
\texttt{CR}) will be highlighted as a keyword. |
| 221 |
|
| 222 |
\section{License} |
| 223 |
|
| 224 |
This material is subject to the \LaTeX{} Project Public License 1.3c. |
| 225 |
See \url{https://www.latex-project.org/lppl/lppl-1-3c/}. |
| 226 |
|
| 227 |
\end{document} |