Created
August 11, 2025 21:33
-
-
Save keigoi/5f41652352b22a549bf7c4089c26993d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| \documentclass{article} | |
| \usepackage{xcolor} | |
| \usepackage{expl3} | |
| % --- 色の定義 --- | |
| \colorlet{stlcVarColor}{red!80!black} | |
| \colorlet{stlcLamColor}{blue!70!black} | |
| \colorlet{stlcOpColor}{gray} | |
| \colorlet{stlcTypeColor}{green!60!black} | |
| \ExplSyntaxOn | |
| % --- 変数の定義 --- | |
| % グローバル変数: デフォルトでハイライトする変数リスト | |
| \tl_new:N \g_stlcHighlight_defaultVars_tl | |
| \tl_set:Nn \g_stlcHighlight_defaultVars_tl | |
| { | |
| x,y,z,f,g,m,n,t,u,v,w, | |
| M,N,T,U,V,W, | |
| a,b,c,d,e,i,j,k,l, | |
| A,B,C,D,E, | |
| alpha,beta,gamma,Gamma,Delta | |
| } | |
| % ローカル変数 | |
| \tl_new:N \l_stlcHighlight_sourceCode_tl | |
| \tl_new:N \l_stlcHighlight_currentVarList_tl | |
| % --- キーの定義 --- | |
| \keys_define:nn { stlc } | |
| { | |
| vars .tl_set:N = \l_stlcHighlight_currentVarList_tl, | |
| vars .initial:V = \g_stlcHighlight_defaultVars_tl | |
| } | |
| % --- ハイライト用マクロ --- | |
| \NewDocumentCommand{\StlcVar}{m}{\textcolor{stlcVarColor}{#1}} | |
| \NewDocumentCommand{\StlcLam}{}{\textcolor{stlcLamColor}{\lambda}} | |
| \NewDocumentCommand{\StlcColon}{}{\mathrel{\textcolor{stlcOpColor}{:}}} | |
| \NewDocumentCommand{\StlcTo}{}{\mathrel{\textcolor{stlcTypeColor}{\to}}} | |
| % --- メインマクロ \stlc --- | |
| \NewDocumentCommand{\stlc}{ O{} m } | |
| { | |
| \ensuremath{ | |
| \group_begin: | |
| \keys_set:nn { stlc } { #1 } | |
| \tl_set:Nn \l_stlcHighlight_sourceCode_tl { #2 } | |
| % --- 基本的なハイライト処理 --- | |
| \tl_replace_all:Nnn \l_stlcHighlight_sourceCode_tl { \lambda } { \StlcLam } | |
| \tl_replace_all:Nnn \l_stlcHighlight_sourceCode_tl { : } { \StlcColon } | |
| \tl_replace_all:Nnn \l_stlcHighlight_sourceCode_tl { \to } { \StlcTo } | |
| % --- 変数のハイライト処理 --- | |
| % 各変数を個別に処理 | |
| \clist_map_inline:Nn \l_stlcHighlight_currentVarList_tl | |
| { | |
| \regex_replace_all:nnN | |
| { \b##1\b } | |
| { \\StlcVar\{##1\} } | |
| \l_stlcHighlight_sourceCode_tl | |
| } | |
| % 文字列をコマンドとして再評価 | |
| \exp_args:NV \scantokens \l_stlcHighlight_sourceCode_tl | |
| \group_end: | |
| } | |
| } | |
| \ExplSyntaxOff | |
| \begin{document} | |
| \title{STLC Syntax Highlighting Test} | |
| \maketitle | |
| \section{Basic Tests} | |
| Simple identity function: | |
| \stlc{\lambda x. x} | |
| Function application: | |
| \stlc{\lambda f. \lambda x. f x} | |
| \section{Type Annotations} | |
| With type annotations: | |
| \stlc{\lambda x : A \to B. x} | |
| Complex type: | |
| \stlc{\lambda f : (A \to B) \to C. \lambda x : A. f (\lambda y : A. y) x} | |
| \section{Custom Variable Lists} | |
| Using custom variables: | |
| \stlc[vars={foo,bar,baz}]{\lambda foo. foo\ bar\ baz} | |
| \end{document} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment