carbon.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. pygments.lexers.carbon
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the Carbon programming language.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, words
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation, Whitespace
  12. __all__ = ['CarbonLexer']
  13. class CarbonLexer(RegexLexer):
  14. """
  15. For Carbon source.
  16. """
  17. name = 'Carbon'
  18. url = 'https://github.com/carbon-language/carbon-lang'
  19. filenames = ['*.carbon']
  20. aliases = ['carbon']
  21. mimetypes = ['text/x-carbon']
  22. version_added = '2.15'
  23. flags = re.MULTILINE | re.DOTALL
  24. tokens = {
  25. 'root': [
  26. (r'\n', Whitespace),
  27. (r'\s+', Whitespace),
  28. (r'\\\n', Text),
  29. # comments
  30. (r'//(.*?)\n', Comment.Single),
  31. (r'/(\\\n)?[*].*?[*](\\\n)?/', Comment.Multiline),
  32. # Declaration
  33. (r'(package|import|api|namespace|library)\b', Keyword.Namespace),
  34. (r'(abstract|alias|fn|class|interface|let|var|virtual|external|'
  35. r'base|addr|extends|choice|constraint|impl)\b', Keyword.Declaration),
  36. # Keywords
  37. (words(('as', 'or', 'not', 'and', 'break', 'continue', 'case',
  38. 'default', 'if', 'else', 'destructor', 'for', 'forall',
  39. 'while', 'where', 'then', 'in', 'is', 'return', 'returned',
  40. 'friend', 'partial', 'private', 'protected', 'observe', 'Self',
  41. 'override', 'final', 'match', 'type', 'like'), suffix=r'\b'), Keyword),
  42. (r'(self)\b', Keyword.Pseudo),
  43. (r'(true|false)\b', Keyword.Constant),
  44. (r'(auto|bool|string|i8|i16|i32|i64|u8|u16|u32|u64|'
  45. r'f8|f16|f32|f64)\b', Keyword.Type),
  46. # numeric literals
  47. (r'[0-9]*[.][0-9]+', Number.Double),
  48. (r'0b[01]+', Number.Bin),
  49. (r'0o[0-7]+', Number.Oct),
  50. (r'0x[0-9a-fA-F]+', Number.Hex),
  51. (r'[0-9]+', Number.Integer),
  52. # string literal
  53. (r'"(\\.|[^"\\])*"', String),
  54. # char literal
  55. (r'\'(\\.|[^\'\\])\'', String.Char),
  56. # tokens
  57. (r'<<=|>>=|<<|>>|<=|>=|\+=|-=|\*=|/=|\%=|\|=|&=|\^=|&&|\|\||&|\||'
  58. r'\+\+|--|\%|\^|\~|==|!=|::|[.]{3}|->|=>|[+\-*/&]', Operator),
  59. (r'[|<>=!()\[\]{}.,;:\?]', Punctuation),
  60. # identifiers
  61. (r'[^\W\d]\w*', Name.Other),
  62. ]
  63. }
  64. def analyse_text(text):
  65. result = 0
  66. if 'forall' in text:
  67. result += 0.1
  68. if 'type' in text:
  69. result += 0.1
  70. if 'Self' in text:
  71. result += 0.1
  72. if 'observe' in text:
  73. result += 0.1
  74. if 'package' in text:
  75. result += 0.1
  76. if 'library' in text:
  77. result += 0.1
  78. if 'choice' in text:
  79. result += 0.1
  80. if 'addr' in text:
  81. result += 0.1
  82. if 'constraint' in text:
  83. result += 0.1
  84. if 'impl' in text:
  85. result += 0.1
  86. return result