teal.py 3.4 KB

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