sophia.py 3.3 KB

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