| 1 |
|
| 2 |
\documentclass[oneside]{book} |
| 3 |
\usepackage[a4paper,margin=2cm]{geometry} |
| 4 |
|
| 5 |
\newcommand*{\myversion}{2025B} |
| 6 |
\newcommand*{\mydate}{Version \myversion\ (\the\year-\mylpad\month-\mylpad\day)} |
| 7 |
\newcommand*{\mylpad}[1]{\ifnum#1<10 0\the#1\else\the#1\fi} |
| 8 |
|
| 9 |
\setlength{\parindent}{0pt} |
| 10 |
\setlength{\parskip}{4pt plus 1pt minus 1pt} |
| 11 |
|
| 12 |
\usepackage{hyperref} |
| 13 |
\hypersetup{ |
| 14 |
colorlinks=true, |
| 15 |
urlcolor=blue3, |
| 16 |
linkcolor=green3, |
| 17 |
} |
| 18 |
|
| 19 |
\usepackage{tabularray} |
| 20 |
|
| 21 |
\NewTblrEnviron{spectblr} |
| 22 |
\SetTblrOuter[spectblr]{long} |
| 23 |
\SetTblrInner[spectblr]{ |
| 24 |
hlines = {gray3}, column{Z} = {co=1}, colsep = 5pt, |
| 25 |
row{2-Z} = {brown8}, |
| 26 |
row{1} = {fg=white, bg=purple2, font=\bfseries\sffamily}, |
| 27 |
rowhead = 1, |
| 28 |
} |
| 29 |
|
| 30 |
\usepackage{codehigh} |
| 31 |
\colorlet{highback}{azure9} |
| 32 |
\CodeHigh{language=latex/latex2,style/main=gray9,style/code=gray9} |
| 33 |
|
| 34 |
\NewDocumentCommand\PP{m}{\texttt{\fakeverb{#1}}} % package |
| 35 |
\NewDocumentCommand\CC{m}{\texttt{\fakeverb{#1}}} % command |
| 36 |
\NewDocumentCommand\VV{m}{\texttt{\fakeverb{#1}}} % variable |
| 37 |
\NewDocumentCommand\VT{m}{\texttt{\fakeverb{#1}}} % variable type |
| 38 |
\NewDocumentCommand\TT{m}{\texttt{\fakeverb{#1}}} % text |
| 39 |
|
| 40 |
\usepackage{pegmatch} |
| 41 |
|
| 42 |
\begin{document} |
| 43 |
|
| 44 |
\title{\textsf{\color{green3}PEGMATCH: Parsing Expression Grammars for TeX}} |
| 45 |
\author{Jianrui Lyu ([email protected])\\ \url{https://github.com/lvjr/pegmatch}} |
| 46 |
\date{\mydate} |
| 47 |
\maketitle |
| 48 |
|
| 49 |
\tableofcontents |
| 50 |
|
| 51 |
\chapter{Package Interfaces} |
| 52 |
|
| 53 |
\section{Introduction} |
| 54 |
|
| 55 |
The \PP{pegmatch} package ports PEG (Parsing Expression Grammars)% |
| 56 |
\footnote{See Parsing Expression Grammars page: \url{https://bford.info/packrat/}.} to TeX. |
| 57 |
Following the design in LPEG (Parsing Expression Grammars for Lua),% |
| 58 |
\footnote{See Parsing Expression Grammars for Lua page: \url{https://www.inf.puc-rio.br/~roberto/lpeg/}.} |
| 59 |
it defines patterns as LaTeX3 variables, and offers several operators to compose patterns. |
| 60 |
|
| 61 |
In general, PEG matching is much more powerful than RE (Regular Expressions) matching. |
| 62 |
At this time, \PP{pegmatch} package only supports TeX strings.% |
| 63 |
\footnote{I started to write it for my \PP{codehigh} package to get rid of \PP{l3regex} dependency.} |
| 64 |
Also it is still in experimental status, hence some interfaces may change in future releases. |
| 65 |
|
| 66 |
\section{The first example} |
| 67 |
|
| 68 |
The following is the first example: |
| 69 |
\begin{demohigh} |
| 70 |
\NewSpeg\lMyTestSpeg |
| 71 |
\SetSpeg\lMyTestSpeg{\SpegP{abc}} |
| 72 |
\IfSpegMatchTF\lMyTestSpeg{a}{T}{F} |
| 73 |
\IfSpegMatchTF\lMyTestSpeg{ab}{T}{F} |
| 74 |
\IfSpegMatchTF\lMyTestSpeg{abc}{T}{F} |
| 75 |
\IfSpegMatchTF\lMyTestSpeg{abcd}{T}{F} |
| 76 |
\end{demohigh} |
| 77 |
In this example, we use \CC{\NewSpeg} to create a new \VT{speg} variable \VV{\lMyTestSpeg}, |
| 78 |
and use \CC{\SetSpeg} to set the variable with a pattern expression, |
| 79 |
then use \CC{\IfSpegMatchTF} to match it against different subject strings. |
| 80 |
The pattern \TT{\SpegP{abc}} matches the string \TT{abc} literally. |
| 81 |
|
| 82 |
\section{Basic commands} |
| 83 |
|
| 84 |
This package provides the following commands for creating and matching \VT{speg} patterns:\nopagebreak |
| 85 |
\begin{spectblr}[ |
| 86 |
caption = Basic commands |
| 87 |
]{} |
| 88 |
Command & Description \\ |
| 89 |
\CC{\NewSpeg#1} & create \VT{speg} variable \TT{#1}\\ |
| 90 |
\CC{\SetSpeg#1{#2}} & set \VT{speg} variable \TT{#1} with \VT{speg} expresssion \VT{#2}\\ |
| 91 |
\CC{\IfSpegMatchT#1{#2}{#3}} |
| 92 |
& match \TT{#1} against string \TT{#2}, then run code \TT{#3} if the match succeeds\\ |
| 93 |
\CC{\IfSpegMatchF#1{#2}{#3}} |
| 94 |
& match \TT{#1} against string \TT{#2}, then run code \TT{#3} if the match fails\\ |
| 95 |
\CC{\IfSpegMatchTF#1{#2}{#3}{#4}} |
| 96 |
& match \TT{#1} against string \TT{#2}, then run code \TT{#3} if the match succeeds, |
| 97 |
othewise run code \TT{#4}\\ |
| 98 |
\CC{\IfSpegExtractT#1{#2}#3{#4}} |
| 99 |
& match \TT{#1} against string \TT{#2}, |
| 100 |
then store captures in \TT{#3} and run code \TT{#4} if the match succeeds\\ |
| 101 |
\CC{\IfSpegExtractF#1{#2}#3{#4}} |
| 102 |
& match \TT{#1} against string \TT{#2}, |
| 103 |
then clear \TT{#3} and run code \TT{#4} if the match fails\\ |
| 104 |
\CC{\IfSpegExtractTF#1{#2}#3{#4}{#5}} |
| 105 |
& match \TT{#1} against string \TT{#2}, |
| 106 |
then store captures in \TT{#3} and run code \TT{#4} if the match succeeds, |
| 107 |
othewise clear \TT{#3} and run code \TT{#5} |
| 108 |
\end{spectblr} |
| 109 |
|
| 110 |
\section{Scratch variables} |
| 111 |
|
| 112 |
There are two predefined scratch \VT{speg} variables for setting \VT{speg} patterns: |
| 113 |
\VV{\lTmpaSpeg} and \VV{\lTmpbSpeg}. |
| 114 |
Also there are two predefined scratch \VT{seq} variables for storing captures |
| 115 |
(see Section~\ref{sect:capture}): \VV{\lSpegTmpaSeq} and \VV{\lSpegTmpbSeq}.% |
| 116 |
|
| 117 |
\section{Primitive patterns} |
| 118 |
|
| 119 |
This package provides the following commands for making primitive patterns:%\nopagebreak |
| 120 |
\begin{spectblr}[ |
| 121 |
caption = Primitive patterns |
| 122 |
]{} |
| 123 |
Pattern & Description \\ |
| 124 |
\TT{\SpegP{<string>}} & match \TT{<string>} literally \\ |
| 125 |
\TT{\SpegQ{<n>}} & match exactly \TT{<n>} characters \\ |
| 126 |
\TT{\SpegR{<X><Y><x><y>}} |
| 127 |
& match any character between \TT{<X>} and \TT{<Y>} or between \TT{<x>} and \TT{<y>} \\ |
| 128 |
\TT{\SpegS{<string>}} & match any character in \TT{<string>} |
| 129 |
\end{spectblr} |
| 130 |
|
| 131 |
The following examples demonstrate pattern matching with other primitive patterns: |
| 132 |
\begin{demohigh} |
| 133 |
\SetSpeg\lMyTestSpeg{\SpegQ{2}} |
| 134 |
\IfSpegMatchTF\lMyTestSpeg{u}{T}{F} |
| 135 |
\IfSpegMatchTF\lMyTestSpeg{vw}{T}{F} |
| 136 |
\IfSpegMatchTF\lMyTestSpeg{xyz}{T}{F} |
| 137 |
\end{demohigh} |
| 138 |
\begin{demohigh} |
| 139 |
\SetSpeg\lMyTestSpeg{\SpegR{AZ}} |
| 140 |
\IfSpegMatchTF\lMyTestSpeg{Qq}{T}{F} |
| 141 |
\IfSpegMatchTF\lMyTestSpeg{q1}{T}{F} |
| 142 |
\IfSpegMatchTF\lMyTestSpeg{1Q}{T}{F} |
| 143 |
\SetSpeg\lMyTestSpeg{\SpegR{AZaz}} |
| 144 |
\IfSpegMatchTF\lMyTestSpeg{Qq}{T}{F} |
| 145 |
\IfSpegMatchTF\lMyTestSpeg{q1}{T}{F} |
| 146 |
\IfSpegMatchTF\lMyTestSpeg{1Q}{T}{F} |
| 147 |
\end{demohigh} |
| 148 |
|
| 149 |
\begin{demohigh} |
| 150 |
\SetSpeg\lMyTestSpeg{\SpegS{world}} |
| 151 |
\IfSpegMatchTF\lMyTestSpeg{one}{T}{F} |
| 152 |
\IfSpegMatchTF\lMyTestSpeg{two}{T}{F} |
| 153 |
\end{demohigh} |
| 154 |
|
| 155 |
By default, PEG always starts at the first character. |
| 156 |
Since both \CC{\SpegR} and \CC{\SpegS} match only one letter, |
| 157 |
both last commands in previous two examples give \TT{F}. |
| 158 |
|
| 159 |
\section{Pattern operators} |
| 160 |
|
| 161 |
This package provides the following pattern operators for composing patterns: |
| 162 |
\begin{spectblr}[ |
| 163 |
caption = Pattern operators |
| 164 |
]{} |
| 165 |
Operator & Precedence & Description \\ |
| 166 |
\TT{patt1/patt2} & 1 (choice) & match \TT{patt1} or \TT{patt2} (ordered choice) \\ |
| 167 |
\TT{patt1*patt2} & 2 (concat) & match \TT{patt1} followed by \TT{patt2} \\ |
| 168 |
\TT{!patt} & 3 (not predicate) & match only if \TT{patt} does not match, and consume no input \\ |
| 169 |
\TT{&patt} & 3 (and predicate) & match \TT{patt} but consume no input \\ |
| 170 |
\TT{patt^{<n>}} & 4 (repeat) & match at least \TT{<n>} ($n\ge0)$ repetitions of \TT{patt} \\ |
| 171 |
\TT{patt^{-<n>}} & 4 (repeat) & match at most \TT{<n>} ($n>0$) repetitions of \TT{patt} \\ |
| 172 |
\TT{{patt expr}} & 5 (group) & match \TT{patt expr} (pattern expression) |
| 173 |
\end{spectblr} |
| 174 |
|
| 175 |
With \TT{!} and \TT{*} operators, we can create negative character sets: |
| 176 |
\begin{demohigh} |
| 177 |
\SetSpeg\lMyTestSpeg{!\SpegR{09} * \SpegQ{1}} |
| 178 |
\IfSpegMatchTF\lMyTestSpeg{A}{T}{F} |
| 179 |
\IfSpegMatchTF\lMyTestSpeg{5}{T}{F} |
| 180 |
\SetSpeg\lMyTestSpeg{!\SpegS{abc} * \SpegQ{1}} |
| 181 |
\IfSpegMatchTF\lMyTestSpeg{B}{T}{F} |
| 182 |
\IfSpegMatchTF\lMyTestSpeg{b}{T}{F} |
| 183 |
\end{demohigh} |
| 184 |
|
| 185 |
With \TT{^} operator, we can match words: |
| 186 |
\begin{demohigh} |
| 187 |
\SetSpeg\lMyTestSpeg{\SpegR{AZaz} ^ {1}} |
| 188 |
\IfSpegMatchTF\lMyTestSpeg{HELLO}{T}{F} |
| 189 |
\IfSpegMatchTF\lMyTestSpeg{world}{T}{F} |
| 190 |
\IfSpegMatchTF\lMyTestSpeg{ text }{T}{F} |
| 191 |
\IfSpegMatchTF\lMyTestSpeg{(text)}{T}{F} |
| 192 |
\end{demohigh} |
| 193 |
|
| 194 |
In fact, \TT{patt^{-1}} is similar to \TT{expr?}, \TT{patt^0} is similar to \TT{expr*}, |
| 195 |
and \TT{patt^1} is similar to \TT{expr+} in regular expression matching. |
| 196 |
|
| 197 |
\section{Pattern variables} |
| 198 |
|
| 199 |
In using \CC{\SetSpeg} command to set a \VT{speg} variable with a pattern expression, |
| 200 |
you can use other \VT{speg} variables. For example: |
| 201 |
\begin{demohigh} |
| 202 |
\SetSpeg\lTmpaSpeg{\SpegR{AZ} / \SpegR{az}} |
| 203 |
\SetSpeg\lTmpbSpeg{\SpegS{135} * \lTmpaSpeg} |
| 204 |
\IfSpegMatchTF\lTmpbSpeg{2ab}{[T]}{[F]} |
| 205 |
\IfSpegMatchTF\lTmpbSpeg{3ab}{[T]}{[F]} |
| 206 |
\SetSpeg\lTmpbSpeg{\SpegS{135} * \lTmpaSpeg^{3}} |
| 207 |
\IfSpegMatchTF\lTmpbSpeg{3ab}{[T]}{[F]} |
| 208 |
\IfSpegMatchTF\lTmpbSpeg{3abcd}{[T]}{[F]} |
| 209 |
\end{demohigh} |
| 210 |
|
| 211 |
By using another recursive pattern, we can make \PP{speg} find a pattern anywhere in a string. |
| 212 |
The following example demonstrates how to match a word with at least three letters |
| 213 |
inside a string:\nopagebreak |
| 214 |
\begin{demohigh} |
| 215 |
\NewSpeg\lMyWordSpeg |
| 216 |
\NewSpeg\lMyAnywhereSpeg |
| 217 |
\SetSpeg\lMyWordSpeg{\SpegR{AZaz}^{3}} |
| 218 |
\SetSpeg\lMyAnywhereSpeg{\lMyWordSpeg / \SpegQ{1} * \lMyAnywhereSpeg} |
| 219 |
\IfSpegMatchTF\lMyAnywhereSpeg{foo bar}{[T]}{[F]} |
| 220 |
\IfSpegMatchTF\lMyAnywhereSpeg{fo bar}{[T]}{[F]} |
| 221 |
\IfSpegMatchTF\lMyAnywhereSpeg{123 ba}{[T]}{[F]} |
| 222 |
\IfSpegMatchTF\lMyAnywhereSpeg{123 bar}{[T]}{[F]} |
| 223 |
\end{demohigh} |
| 224 |
In this example, \VV{\lMyAnywhereSpeg} tries to match \VV{\lMyWordSpeg}, |
| 225 |
skipping one letter and tries again if it fails. |
| 226 |
|
| 227 |
\section{Capture patterns}% |
| 228 |
\label{sect:capture} |
| 229 |
|
| 230 |
This package provides the following commands for making capture patterns: |
| 231 |
\begin{spectblr}[ |
| 232 |
caption = Primitive patterns |
| 233 |
]{} |
| 234 |
Pattern & Name & Description \\ |
| 235 |
\CC{\SpegC{<patt>}} & simple capture & capture the match for \TT{<patt>}\\ |
| 236 |
\CC{\SpegCp} & position capture & capture current position |
| 237 |
\end{spectblr} |
| 238 |
|
| 239 |
Position capture \CC{\SpegCp} must be concatenated with other patterns (by using \TT{*} operator): |
| 240 |
\begin{demohigh} |
| 241 |
\SetSpeg\lTmpaSpeg{\SpegCp * \SpegR{az}^{1} * \SpegCp * \SpegR{09}^{1} * \SpegCp} |
| 242 |
\IfSpegExtractTF\lTmpaSpeg{12ab}\lSpegTmpaSeq{% |
| 243 |
\MapSpegSeqInline\lSpegTmpaSeq{[#1]}% |
| 244 |
}{Failed} |
| 245 |
\IfSpegExtractTF\lTmpaSpeg{ab12}\lSpegTmpaSeq{% |
| 246 |
\MapSpegSeqInline\lSpegTmpaSeq{[#1]}% |
| 247 |
}{Failed} |
| 248 |
\IfSpegExtractTF\lTmpaSpeg{abcd12345}\lSpegTmpaSeq{% |
| 249 |
\MapSpegSeqInline\lSpegTmpaSeq{[#1]}% |
| 250 |
}{Failed} |
| 251 |
\end{demohigh} |
| 252 |
In this example, we use \CC{\IfSpegExtractTF} command to extract all captures, |
| 253 |
which are stored in the \VT{seq} variable (\CC{\lSpegTmpaSeq}) specified by the third argument. |
| 254 |
Then we use \CC{\MapSpegSeqInline} command to print each capture. |
| 255 |
|
| 256 |
If you want to capture the substrings, you can modified the above example as follows:\nopagebreak |
| 257 |
\begin{demohigh} |
| 258 |
\SetSpeg\lTmpaSpeg{\SpegC{\SpegR{az}^{1}} * \SpegC{\SpegR{09}^{1}}} |
| 259 |
\IfSpegExtractTF\lTmpaSpeg{12ab}\lSpegTmpaSeq{% |
| 260 |
\MapSpegSeqInline\lSpegTmpaSeq{[#1]}% |
| 261 |
}{Failed} |
| 262 |
\IfSpegExtractTF\lTmpaSpeg{ab12}\lSpegTmpaSeq{% |
| 263 |
\MapSpegSeqInline\lSpegTmpaSeq{[#1]}% |
| 264 |
}{Failed} |
| 265 |
\IfSpegExtractTF\lTmpaSpeg{abcd12345}\lSpegTmpaSeq{% |
| 266 |
\MapSpegSeqInline\lSpegTmpaSeq{[#1]}% |
| 267 |
}{Failed} |
| 268 |
\end{demohigh} |
| 269 |
|
| 270 |
\chapter{The Source Code} |
| 271 |
|
| 272 |
\dochighinput[language=latex/latex3]{pegmatch.sty} |
| 273 |
|
| 274 |
\end{document} |