teal.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.teal
  4. ~~~~~~~~~~~~~~~~~~~~
  5. Lexer for TEAL.
  6. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, bygroups, include, words
  10. from pygments.token import Comment, Name, Number, String, Text, Keyword
  11. __all__ = ['TealLexer']
  12. class TealLexer(RegexLexer):
  13. """
  14. For the `Transaction Execution Approval Language (TEAL)
  15. <https://developer.algorand.org/docs/reference/teal/specification/>`
  16. For more information about the grammar, see:
  17. https://github.com/algorand/go-algorand/blob/master/data/transactions/logic/assembler.go
  18. .. versionadded:: 2.9
  19. """
  20. name = 'teal'
  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]+', Text),
  77. (r'//[^\n]+', Comment.Single),
  78. ],
  79. }