go.py 3.7 KB

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