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

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

16 statements  

1""" 

2 pygments.lexers.cel 

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

4 

5 Lexer for CEL (Common Expression Language). 

6 

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

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11__all__ = ['CELLexer'] 

12 

13import re 

14 

15from pygments.lexer import RegexLexer, words 

16from pygments.token import ( 

17 Comment, Keyword, Name, Number, Operator, Punctuation, String, Whitespace, 

18) 

19 

20 

21class CELLexer(RegexLexer): 

22 """Lexer for CEL (Common Expression Language).""" 

23 

24 name = 'CEL' 

25 aliases = ['cel'] 

26 filenames = ['*.cel'] 

27 mimetypes = [] 

28 url = 'https://cel.dev' 

29 version_added = '2.21' 

30 

31 flags = re.MULTILINE | re.UNICODE 

32 

33 # Escape sequence per the CEL spec: 

34 # \a \b \f \n \r \t \v \\ \" \' \` \? 

35 # \xHH or \XHH (exactly 2 hex digits) 

36 # \uHHHH (exactly 4 hex digits) 

37 # \UHHHHHHHH (exactly 8 hex digits) 

38 # \OOO (3-digit octal, first digit 0-3) 

39 # \. (fallback for unknown escapes) 

40 _ESCAPE = r"""\\(?:[abfnrtv\\"'`?]|[xX][0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|[0-3][0-7][0-7]|.)""" 

41 

42 # Raw prefix: r/R alone, or b/B combined with r/R in either order 

43 _RAW = r'(?:[bB][rR]|[rR][bB]|[rR])' 

44 

45 tokens = { 

46 'root': [ 

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

48 # CEL only has line comments; no block comments. 

49 (r'//.*$', Comment.Single), 

50 

51 # Bytes and string literals. 

52 # Raw variants (no escape processing) must be tried before 

53 # non-raw so that br/rb/r prefixes are consumed greedily. 

54 # Triple-quoted raw (br""", rb""", r""") 

55 (_RAW + r'"""', String, 'triple-double-raw'), 

56 (_RAW + r"'''", String, 'triple-single-raw'), 

57 # Triple-quoted non-raw with optional b prefix (b""" or """) 

58 (r'[bB]?"""', String, 'triple-double'), 

59 (r"[bB]?'''", String, 'triple-single'), 

60 # Single-line raw 

61 (_RAW + r'"[^"\r\n]*"', String), 

62 (_RAW + r"'[^'\r\n]*'", String), 

63 # Single-line non-raw with optional b prefix 

64 (r'[bB]?"', String, 'double-string'), 

65 (r"[bB]?'", String, 'single-string'), 

66 

67 # Numbers. 

68 # uint_literal: integer (decimal or hex) followed by u/U suffix. 

69 (r'(?:0[xX][0-9a-fA-F]+|0|[1-9][0-9]*)[uU]', Number.Integer), 

70 # float_literal: the grammar requires digits on both sides of the 

71 # decimal point for the dotted form — `1.` is not valid CEL. 

72 (r'(?:[0-9]+\.[0-9]+(?:[eE][+-]?[0-9]+)?' 

73 r'|[0-9]+[eE][+-]?[0-9]+' 

74 r'|\.[0-9]+(?:[eE][+-]?[0-9]+)?)', 

75 Number.Float), 

76 # int_literal: hex or decimal. 

77 (r'0[xX][0-9a-fA-F]+|0|[1-9][0-9]*', Number.Integer), 

78 

79 # Reserved keywords (may not appear as identifiers). 

80 (words(( 

81 'as', 'break', 'const', 'continue', 'else', 'for', 

82 'function', 'if', 'import', 'let', 'loop', 'namespace', 

83 'package', 'return', 'var', 'void', 'while', 

84 ), suffix=r'\b'), Keyword.Reserved), 

85 # `in` is a comparison operator keyword. 

86 (r'in\b', Keyword), 

87 # Built-in literal constants. 

88 (words(('true', 'false', 'null'), suffix=r'\b'), Keyword.Constant), 

89 

90 # Logical and comparison operators. 

91 (r'&&|\|\|', Operator), 

92 (r'==|!=|<=|>=', Operator), 

93 # Arithmetic, unary, ternary, and remaining comparison operators. 

94 (r'[+\-*/%<>!?]', Operator), 

95 

96 # Punctuation: brackets, comma, dot, colon. 

97 (r'[(){}\[\]]', Punctuation), 

98 (r'[,.]', Punctuation), 

99 (r':', Punctuation), 

100 

101 # Identifiers: start with letter or underscore (Unicode-aware). 

102 (r'[^\W\d]\w*', Name), 

103 ], 

104 

105 # Raw triple-quoted (no escape tokens; content is one big String). 

106 'triple-double-raw': [ 

107 (r'"""', String, '#pop'), 

108 (r'"(?!"")', String), 

109 (r'[^"]+', String), 

110 ], 

111 'triple-single-raw': [ 

112 (r"'''", String, '#pop'), 

113 (r"'(?!'')", String), 

114 (r"[^']+", String), 

115 ], 

116 

117 # Non-raw triple-quoted (escape sequences highlighted separately). 

118 'triple-double': [ 

119 (r'"""', String, '#pop'), 

120 (_ESCAPE, String.Escape), 

121 (r'"(?!"")', String), 

122 (r'[^"\\]+', String), 

123 ], 

124 'triple-single': [ 

125 (r"'''", String, '#pop'), 

126 (_ESCAPE, String.Escape), 

127 (r"'(?!'')", String), 

128 (r"[^'\\]+", String), 

129 ], 

130 

131 # Single-line non-raw strings (cannot span lines). 

132 'double-string': [ 

133 (r'"', String, '#pop'), 

134 (_ESCAPE, String.Escape), 

135 (r'[^"\\\r\n]+', String), 

136 ], 

137 'single-string': [ 

138 (r"'", String, '#pop'), 

139 (_ESCAPE, String.Escape), 

140 (r"[^'\\\r\n]+", String), 

141 ], 

142 }