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