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

15 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:16 +0000

1""" 

2 pygments.lexers.cddl 

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

4 

5 Lexer for the Concise data definition language (CDDL), a notational 

6 convention to express CBOR and JSON data structures. 

7 

8 More information: 

9 https://datatracker.ietf.org/doc/rfc8610/ 

10 

11 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. 

12 :license: BSD, see LICENSE for details. 

13""" 

14 

15from pygments.lexer import RegexLexer, bygroups, include, words 

16from pygments.token import Comment, Error, Keyword, Name, Number, Operator, \ 

17 Punctuation, String, Whitespace 

18 

19__all__ = ['CddlLexer'] 

20 

21 

22class CddlLexer(RegexLexer): 

23 """ 

24 Lexer for CDDL definitions. 

25 

26 .. versionadded:: 2.8 

27 """ 

28 name = "CDDL" 

29 url = 'https://datatracker.ietf.org/doc/rfc8610/' 

30 aliases = ["cddl"] 

31 filenames = ["*.cddl"] 

32 mimetypes = ["text/x-cddl"] 

33 

34 _prelude_types = [ 

35 "any", 

36 "b64legacy", 

37 "b64url", 

38 "bigfloat", 

39 "bigint", 

40 "bignint", 

41 "biguint", 

42 "bool", 

43 "bstr", 

44 "bytes", 

45 "cbor-any", 

46 "decfrac", 

47 "eb16", 

48 "eb64legacy", 

49 "eb64url", 

50 "encoded-cbor", 

51 "false", 

52 "float", 

53 "float16", 

54 "float16-32", 

55 "float32", 

56 "float32-64", 

57 "float64", 

58 "int", 

59 "integer", 

60 "mime-message", 

61 "nil", 

62 "nint", 

63 "null", 

64 "number", 

65 "regexp", 

66 "tdate", 

67 "text", 

68 "time", 

69 "true", 

70 "tstr", 

71 "uint", 

72 "undefined", 

73 "unsigned", 

74 "uri", 

75 ] 

76 

77 _controls = [ 

78 ".and", 

79 ".bits", 

80 ".cbor", 

81 ".cborseq", 

82 ".default", 

83 ".eq", 

84 ".ge", 

85 ".gt", 

86 ".le", 

87 ".lt", 

88 ".ne", 

89 ".regexp", 

90 ".size", 

91 ".within", 

92 ] 

93 

94 _re_id = ( 

95 r"[$@A-Z_a-z]" 

96 r"(?:[\-\.]+(?=[$@0-9A-Z_a-z])|[$@0-9A-Z_a-z])*" 

97 

98 ) 

99 

100 # While the spec reads more like "an int must not start with 0" we use a 

101 # lookahead here that says "after a 0 there must be no digit". This makes the 

102 # '0' the invalid character in '01', which looks nicer when highlighted. 

103 _re_uint = r"(?:0b[01]+|0x[0-9a-fA-F]+|[1-9]\d*|0(?!\d))" 

104 _re_int = r"-?" + _re_uint 

105 

106 tokens = { 

107 "commentsandwhitespace": [(r"\s+", Whitespace), (r";.+$", Comment.Single)], 

108 "root": [ 

109 include("commentsandwhitespace"), 

110 # tag types 

111 (r"#(\d\.{uint})?".format(uint=_re_uint), Keyword.Type), # type or any 

112 # occurrence 

113 ( 

114 r"({uint})?(\*)({uint})?".format(uint=_re_uint), 

115 bygroups(Number, Operator, Number), 

116 ), 

117 (r"\?|\+", Operator), # occurrence 

118 (r"\^", Operator), # cuts 

119 (r"(\.\.\.|\.\.)", Operator), # rangeop 

120 (words(_controls, suffix=r"\b"), Operator.Word), # ctlops 

121 # into choice op 

122 (r"&(?=\s*({groupname}|\())".format(groupname=_re_id), Operator), 

123 (r"~(?=\s*{})".format(_re_id), Operator), # unwrap op 

124 (r"//|/(?!/)", Operator), # double und single slash 

125 (r"=>|/==|/=|=", Operator), 

126 (r"[\[\]{}\(\),<>:]", Punctuation), 

127 # Bytestrings 

128 (r"(b64)(')", bygroups(String.Affix, String.Single), "bstrb64url"), 

129 (r"(h)(')", bygroups(String.Affix, String.Single), "bstrh"), 

130 (r"'", String.Single, "bstr"), 

131 # Barewords as member keys (must be matched before values, types, typenames, 

132 # groupnames). 

133 # Token type is String as barewords are always interpreted as such. 

134 (r"({bareword})(\s*)(:)".format(bareword=_re_id), 

135 bygroups(String, Whitespace, Punctuation)), 

136 # predefined types 

137 (words(_prelude_types, prefix=r"(?![\-_$@])\b", suffix=r"\b(?![\-_$@])"), 

138 Name.Builtin), 

139 # user-defined groupnames, typenames 

140 (_re_id, Name.Class), 

141 # values 

142 (r"0b[01]+", Number.Bin), 

143 (r"0o[0-7]+", Number.Oct), 

144 (r"0x[0-9a-fA-F]+(\.[0-9a-fA-F]+)?p[+-]?\d+", Number.Hex), # hexfloat 

145 (r"0x[0-9a-fA-F]+", Number.Hex), # hex 

146 # Float 

147 (r"{int}(?=(\.\d|e[+-]?\d))(?:\.\d+)?(?:e[+-]?\d+)?".format(int=_re_int), 

148 Number.Float), 

149 # Int 

150 (_re_int, Number.Integer), 

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

152 ], 

153 "bstrb64url": [ 

154 (r"'", String.Single, "#pop"), 

155 include("commentsandwhitespace"), 

156 (r"\\.", String.Escape), 

157 (r"[0-9a-zA-Z\-_=]+", String.Single), 

158 (r".", Error), 

159 # (r";.+$", Token.Other), 

160 ], 

161 "bstrh": [ 

162 (r"'", String.Single, "#pop"), 

163 include("commentsandwhitespace"), 

164 (r"\\.", String.Escape), 

165 (r"[0-9a-fA-F]+", String.Single), 

166 (r".", Error), 

167 ], 

168 "bstr": [ 

169 (r"'", String.Single, "#pop"), 

170 (r"\\.", String.Escape), 

171 (r"[^'\\]+", String.Single), 

172 ], 

173 }