go.py 3.6 KB

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