sophia.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. pygments.lexers.sophia
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Sophia.
  5. Derived from pygments/lexers/reason.py.
  6. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, include, default, words
  10. from pygments.token import Comment, Keyword, Name, Number, Operator, \
  11. Punctuation, String, Text
  12. __all__ = ['SophiaLexer']
  13. class SophiaLexer(RegexLexer):
  14. """
  15. A Sophia lexer.
  16. """
  17. name = 'Sophia'
  18. aliases = ['sophia']
  19. filenames = ['*.aes']
  20. mimetypes = []
  21. url = 'https://docs.aeternity.com/aesophia'
  22. version_added = '2.11'
  23. keywords = (
  24. 'contract', 'include', 'let', 'switch', 'type', 'record', 'datatype',
  25. 'if', 'elif', 'else', 'function', 'stateful', 'payable', 'public',
  26. 'entrypoint', 'private', 'indexed', 'namespace', 'interface', 'main',
  27. 'using', 'as', 'for', 'hiding',
  28. )
  29. builtins = ('state', 'put', 'abort', 'require')
  30. word_operators = ('mod', 'band', 'bor', 'bxor', 'bnot')
  31. primitive_types = ('int', 'address', 'bool', 'bits', 'bytes', 'string',
  32. 'list', 'option', 'char', 'unit', 'map', 'event',
  33. 'hash', 'signature', 'oracle', 'oracle_query')
  34. tokens = {
  35. 'escape-sequence': [
  36. (r'\\[\\"\'ntbr]', String.Escape),
  37. (r'\\[0-9]{3}', String.Escape),
  38. (r'\\x[0-9a-fA-F]{2}', String.Escape),
  39. ],
  40. 'root': [
  41. (r'\s+', Text.Whitespace),
  42. (r'(true|false)\b', Keyword.Constant),
  43. (r'\b([A-Z][\w\']*)(?=\s*\.)', Name.Class, 'dotted'),
  44. (r'\b([A-Z][\w\']*)', Name.Function),
  45. (r'//.*?\n', Comment.Single),
  46. (r'\/\*(?!/)', Comment.Multiline, 'comment'),
  47. (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex),
  48. (r'#[\da-fA-F][\da-fA-F_]*', Name.Label),
  49. (r'\d[\d_]*', Number.Integer),
  50. (words(keywords, suffix=r'\b'), Keyword),
  51. (words(builtins, suffix=r'\b'), Name.Builtin),
  52. (words(word_operators, prefix=r'\b', suffix=r'\b'), Operator.Word),
  53. (words(primitive_types, prefix=r'\b', suffix=r'\b'), Keyword.Type),
  54. (r'[=!<>+\\*/:&|?~@^-]', Operator.Word),
  55. (r'[.;:{}(),\[\]]', Punctuation),
  56. (r"(ak_|ok_|oq_|ct_)[\w']*", Name.Label),
  57. (r"[^\W\d][\w']*", Name),
  58. (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'",
  59. String.Char),
  60. (r"'.'", String.Char),
  61. (r"'[a-z][\w]*", Name.Variable),
  62. (r'"', String.Double, 'string')
  63. ],
  64. 'comment': [
  65. (r'[^/*]+', Comment.Multiline),
  66. (r'\/\*', Comment.Multiline, '#push'),
  67. (r'\*\/', Comment.Multiline, '#pop'),
  68. (r'\*', Comment.Multiline),
  69. ],
  70. 'string': [
  71. (r'[^\\"]+', String.Double),
  72. include('escape-sequence'),
  73. (r'\\\n', String.Double),
  74. (r'"', String.Double, '#pop'),
  75. ],
  76. 'dotted': [
  77. (r'\s+', Text),
  78. (r'\.', Punctuation),
  79. (r'[A-Z][\w\']*(?=\s*\.)', Name.Function),
  80. (r'[A-Z][\w\']*', Name.Function, '#pop'),
  81. (r'[a-z_][\w\']*', Name, '#pop'),
  82. default('#pop'),
  83. ],
  84. }