1"""
2 pygments.lexers.teal
3 ~~~~~~~~~~~~~~~~~~~~
4
5 Lexer for TEAL.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11from pygments.lexer import RegexLexer, bygroups, include, words
12from pygments.token import Comment, Name, Number, String, Text, Keyword, \
13 Whitespace
14
15__all__ = ['TealLexer']
16
17
18class TealLexer(RegexLexer):
19 """
20 For the Transaction Execution Approval Language (TEAL)
21
22 For more information about the grammar, see:
23 https://github.com/algorand/go-algorand/blob/master/data/transactions/logic/assembler.go
24 """
25 name = 'teal'
26 url = 'https://developer.algorand.org/docs/reference/teal/specification/'
27 aliases = ['teal']
28 filenames = ['*.teal']
29 version_added = '2.9'
30
31 keywords = words({
32 'Sender', 'Fee', 'FirstValid', 'FirstValidTime', 'LastValid', 'Note',
33 'Lease', 'Receiver', 'Amount', 'CloseRemainderTo', 'VotePK',
34 'SelectionPK', 'VoteFirst', 'VoteLast', 'VoteKeyDilution', 'Type',
35 'TypeEnum', 'XferAsset', 'AssetAmount', 'AssetSender', 'AssetReceiver',
36 'AssetCloseTo', 'GroupIndex', 'TxID', 'ApplicationID', 'OnCompletion',
37 'ApplicationArgs', 'NumAppArgs', 'Accounts', 'NumAccounts',
38 'ApprovalProgram', 'ClearStateProgram', 'RekeyTo', 'ConfigAsset',
39 'ConfigAssetTotal', 'ConfigAssetDecimals', 'ConfigAssetDefaultFrozen',
40 'ConfigAssetUnitName', 'ConfigAssetName', 'ConfigAssetURL',
41 'ConfigAssetMetadataHash', 'ConfigAssetManager', 'ConfigAssetReserve',
42 'ConfigAssetFreeze', 'ConfigAssetClawback', 'FreezeAsset',
43 'FreezeAssetAccount', 'FreezeAssetFrozen',
44 'NoOp', 'OptIn', 'CloseOut', 'ClearState', 'UpdateApplication',
45 'DeleteApplication',
46 'MinTxnFee', 'MinBalance', 'MaxTxnLife', 'ZeroAddress', 'GroupSize',
47 'LogicSigVersion', 'Round', 'LatestTimestamp', 'CurrentApplicationID',
48 'AssetBalance', 'AssetFrozen',
49 'AssetTotal', 'AssetDecimals', 'AssetDefaultFrozen', 'AssetUnitName',
50 'AssetName', 'AssetURL', 'AssetMetadataHash', 'AssetManager',
51 'AssetReserve', 'AssetFreeze', 'AssetClawback',
52 }, suffix=r'\b')
53
54 identifier = r'[^ \t\n]+(?=\/\/)|[^ \t\n]+'
55 newline = r'\r?\n'
56 tokens = {
57 'root': [
58 include('whitespace'),
59 # pragmas match specifically on the space character
60 (r'^#pragma .*' + newline, Comment.Directive),
61 # labels must be followed by a space,
62 # but anything after that is ignored
63 ('(' + identifier + ':' + ')' + '([ \t].*)',
64 bygroups(Name.Label, Comment.Single)),
65 (identifier, Name.Function, 'function-args'),
66 ],
67 'function-args': [
68 include('whitespace'),
69 (r'"', String, 'string'),
70 (r'(b(?:ase)?(?:32|64) ?)(\(?[a-zA-Z0-9+/=]+\)?)',
71 bygroups(String.Affix, String.Other)),
72 (r'[A-Z2-7]{58}', Number), # address
73 (r'0x[\da-fA-F]+', Number.Hex),
74 (r'\d+', Number.Integer),
75 (keywords, Keyword),
76 (identifier, Name.Attributes), # branch targets
77 (newline, Text, '#pop'),
78 ],
79 'string': [
80 (r'\\(?:["nrt\\]|x\d\d)', String.Escape),
81 (r'[^\\\"\n]+', String),
82 (r'"', String, '#pop'),
83 ],
84 'whitespace': [
85 (r'[ \t]+', Whitespace),
86 (r'//[^\n]+', Comment.Single),
87 ],
88 }