Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/lexers/carbon.py: 95%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

37 statements  

1""" 

2 pygments.lexers.carbon 

3 ~~~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexers for the Carbon programming language. 

6 

7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10import re 

11 

12from pygments.lexer import RegexLexer, words 

13from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 

14 Number, Punctuation, Whitespace 

15 

16__all__ = ['CarbonLexer'] 

17 

18 

19class CarbonLexer(RegexLexer): 

20 """ 

21 For Carbon source. 

22 """ 

23 name = 'Carbon' 

24 url = 'https://github.com/carbon-language/carbon-lang' 

25 filenames = ['*.carbon'] 

26 aliases = ['carbon'] 

27 mimetypes = ['text/x-carbon'] 

28 version_added = '2.15' 

29 

30 flags = re.MULTILINE | re.DOTALL 

31 

32 tokens = { 

33 'root': [ 

34 (r'\n', Whitespace), 

35 (r'\s+', Whitespace), 

36 (r'\\\n', Text), 

37 # comments 

38 (r'//(.*?)\n', Comment.Single), 

39 (r'/(\\\n)?[*].*?[*](\\\n)?/', Comment.Multiline), 

40 # Declaration 

41 (r'(package|import|api|namespace|library)\b', Keyword.Namespace), 

42 (r'(abstract|alias|fn|class|interface|let|var|virtual|external|' 

43 r'base|addr|extends|choice|constraint|impl)\b', Keyword.Declaration), 

44 # Keywords 

45 (words(('as', 'or', 'not', 'and', 'break', 'continue', 'case', 

46 'default', 'if', 'else', 'destructor', 'for', 'forall', 

47 'while', 'where', 'then', 'in', 'is', 'return', 'returned', 

48 'friend', 'partial', 'private', 'protected', 'observe', 'Self', 

49 'override', 'final', 'match', 'type', 'like'), suffix=r'\b'), Keyword), 

50 (r'(self)\b', Keyword.Pseudo), 

51 (r'(true|false)\b', Keyword.Constant), 

52 (r'(auto|bool|string|i8|i16|i32|i64|u8|u16|u32|u64|' 

53 r'f8|f16|f32|f64)\b', Keyword.Type), 

54 # numeric literals 

55 (r'[0-9]*[.][0-9]+', Number.Double), 

56 (r'0b[01]+', Number.Bin), 

57 (r'0o[0-7]+', Number.Oct), 

58 (r'0x[0-9a-fA-F]+', Number.Hex), 

59 (r'[0-9]+', Number.Integer), 

60 # string literal 

61 (r'"(\\.|[^"\\])*"', String), 

62 # char literal 

63 (r'\'(\\.|[^\'\\])\'', String.Char), 

64 # tokens 

65 (r'<<=|>>=|<<|>>|<=|>=|\+=|-=|\*=|/=|\%=|\|=|&=|\^=|&&|\|\||&|\||' 

66 r'\+\+|--|\%|\^|\~|==|!=|::|[.]{3}|->|=>|[+\-*/&]', Operator), 

67 (r'[|<>=!()\[\]{}.,;:\?]', Punctuation), 

68 # identifiers 

69 (r'[^\W\d]\w*', Name.Other), 

70 ] 

71 } 

72 

73 def analyse_text(text): 

74 result = 0 

75 if 'forall' in text: 

76 result += 0.1 

77 if 'type' in text: 

78 result += 0.1 

79 if 'Self' in text: 

80 result += 0.1 

81 if 'observe' in text: 

82 result += 0.1 

83 if 'package' in text: 

84 result += 0.1 

85 if 'library' in text: 

86 result += 0.1 

87 if 'choice' in text: 

88 result += 0.1 

89 if 'addr' in text: 

90 result += 0.1 

91 if 'constraint' in text: 

92 result += 0.1 

93 if 'impl' in text: 

94 result += 0.1 

95 return result