go.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. pygments.lexers.go
  3. ~~~~~~~~~~~~~~~~~~
  4. Lexers for the Google Go language.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, bygroups, words
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation
  12. __all__ = ['GoLexer']
  13. class GoLexer(RegexLexer):
  14. """
  15. For `Go <http://golang.org>`_ source.
  16. .. versionadded:: 1.2
  17. """
  18. name = 'Go'
  19. filenames = ['*.go']
  20. aliases = ['go']
  21. mimetypes = ['text/x-gosrc']
  22. flags = re.MULTILINE | re.UNICODE
  23. tokens = {
  24. 'root': [
  25. (r'\n', Text),
  26. (r'\s+', Text),
  27. (r'\\\n', Text), # line continuations
  28. (r'//(.*?)\n', Comment.Single),
  29. (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
  30. (r'(import|package)\b', Keyword.Namespace),
  31. (r'(var|func|struct|map|chan|type|interface|const)\b',
  32. Keyword.Declaration),
  33. (words((
  34. 'break', 'default', 'select', 'case', 'defer', 'go',
  35. 'else', 'goto', 'switch', 'fallthrough', 'if', 'range',
  36. 'continue', 'for', 'return'), suffix=r'\b'),
  37. Keyword),
  38. (r'(true|false|iota|nil)\b', Keyword.Constant),
  39. # It seems the builtin types aren't actually keywords, but
  40. # can be used as functions. So we need two declarations.
  41. (words((
  42. 'uint', 'uint8', 'uint16', 'uint32', 'uint64',
  43. 'int', 'int8', 'int16', 'int32', 'int64',
  44. 'float', 'float32', 'float64',
  45. 'complex64', 'complex128', 'byte', 'rune',
  46. 'string', 'bool', 'error', 'uintptr',
  47. 'print', 'println', 'panic', 'recover', 'close', 'complex',
  48. 'real', 'imag', 'len', 'cap', 'append', 'copy', 'delete',
  49. 'new', 'make'), suffix=r'\b(\()'),
  50. bygroups(Name.Builtin, Punctuation)),
  51. (words((
  52. 'uint', 'uint8', 'uint16', 'uint32', 'uint64',
  53. 'int', 'int8', 'int16', 'int32', 'int64',
  54. 'float', 'float32', 'float64',
  55. 'complex64', 'complex128', 'byte', 'rune',
  56. 'string', 'bool', 'error', 'uintptr'), suffix=r'\b'),
  57. Keyword.Type),
  58. # imaginary_lit
  59. (r'\d+i', Number),
  60. (r'\d+\.\d*([Ee][-+]\d+)?i', Number),
  61. (r'\.\d+([Ee][-+]\d+)?i', Number),
  62. (r'\d+[Ee][-+]\d+i', Number),
  63. # float_lit
  64. (r'\d+(\.\d+[eE][+\-]?\d+|'
  65. r'\.\d*|[eE][+\-]?\d+)', Number.Float),
  66. (r'\.\d+([eE][+\-]?\d+)?', Number.Float),
  67. # int_lit
  68. # -- octal_lit
  69. (r'0[0-7]+', Number.Oct),
  70. # -- hex_lit
  71. (r'0[xX][0-9a-fA-F]+', Number.Hex),
  72. # -- decimal_lit
  73. (r'(0|[1-9][0-9]*)', Number.Integer),
  74. # char_lit
  75. (r"""'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
  76. r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'""",
  77. String.Char),
  78. # StringLiteral
  79. # -- raw_string_lit
  80. (r'`[^`]*`', String),
  81. # -- interpreted_string_lit
  82. (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
  83. # Tokens
  84. (r'(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\|'
  85. r'|<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&])', Operator),
  86. (r'[|^<>=!()\[\]{}.,;:]', Punctuation),
  87. # identifier
  88. (r'[^\W\d]\w*', Name.Other),
  89. ]
  90. }