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

42 statements  

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

1""" 

2 pygments.lexers.ul4 

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

4 

5 Lexer for the UL4 templating language. 

6 

7 More information: https://python.livinglogic.de/UL4.html 

8 

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

10 :license: BSD, see LICENSE for details. 

11""" 

12 

13import re 

14 

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

16from pygments.token import Comment, Text, Keyword, String, Number, Literal, \ 

17 Name, Other, Operator 

18from pygments.lexers.web import HtmlLexer, XmlLexer, CssLexer, JavascriptLexer 

19from pygments.lexers.python import PythonLexer 

20 

21__all__ = ['UL4Lexer', 'HTMLUL4Lexer', 'XMLUL4Lexer', 'CSSUL4Lexer', 

22 'JavascriptUL4Lexer', 'PythonUL4Lexer'] 

23 

24 

25class UL4Lexer(RegexLexer): 

26 """ 

27 Generic lexer for UL4. 

28 

29 .. versionadded:: 2.12 

30 """ 

31 

32 flags = re.MULTILINE | re.DOTALL 

33 

34 name = 'UL4' 

35 aliases = ['ul4'] 

36 filenames = ['*.ul4'] 

37 

38 tokens = { 

39 "root": [ 

40 ( 

41 # Template header without name: 

42 # ``<?ul4?>`` 

43 r"(<\?)(\s*)(ul4)(\s*)(\?>)", 

44 bygroups(Comment.Preproc, Text.Whitespace, Keyword, 

45 Text.Whitespace, Comment.Preproc), 

46 ), 

47 ( 

48 # Template header with name (potentially followed by the signature): 

49 # ``<?ul4 foo(bar=42)?>`` 

50 r"(<\?)(\s*)(ul4)(\s*)([a-zA-Z_][a-zA-Z_0-9]*)?", 

51 bygroups(Comment.Preproc, Text.Whitespace, Keyword, 

52 Text.Whitespace, Name.Function), 

53 "ul4", # Switch to "expression" mode 

54 ), 

55 ( 

56 # Comment: 

57 # ``<?note foobar?>`` 

58 r"<\?\s*note\s.*?\?>", 

59 Comment, 

60 ), 

61 ( 

62 # Template documentation: 

63 # ``<?doc foobar?>`` 

64 r"<\?\s*doc\s.*?\?>", 

65 String.Doc, 

66 ), 

67 ( 

68 # ``<?ignore?>`` tag for commenting out code: 

69 # ``<?ignore?>...<?end ignore?>`` 

70 r"<\?\s*ignore\s*\?>", 

71 Comment, 

72 "ignore", # Switch to "ignore" mode 

73 ), 

74 ( 

75 # ``<?def?>`` tag for defining local templates 

76 # ``<?def foo(bar=42)?>...<?end def?>`` 

77 r"(<\?)(\s*)(def)(\s*)([a-zA-Z_][a-zA-Z_0-9]*)?", 

78 bygroups(Comment.Preproc, Text.Whitespace, Keyword, 

79 Text.Whitespace, Name.Function), 

80 "ul4", # Switch to "expression" mode 

81 ), 

82 ( 

83 # The rest of the supported tags 

84 r"(<\?)(\s*)(printx|print|for|if|elif|else|while|code|renderblocks?|render)\b", 

85 bygroups(Comment.Preproc, Text.Whitespace, Keyword), 

86 "ul4", # Switch to "expression" mode 

87 ), 

88 ( 

89 # ``<?end?>`` tag for ending ``<?def?>``, ``<?for?>``, 

90 # ``<?if?>``, ``<?while?>``, ``<?renderblock?>`` and 

91 # ``<?renderblocks?>`` blocks. 

92 r"(<\?)(\s*)(end)\b", 

93 bygroups(Comment.Preproc, Text.Whitespace, Keyword), 

94 "end", # Switch to "end tag" mode 

95 ), 

96 ( 

97 # ``<?whitespace?>`` tag for configuring whitespace handlng 

98 r"(<\?)(\s*)(whitespace)\b", 

99 bygroups(Comment.Preproc, Text.Whitespace, Keyword), 

100 "whitespace", # Switch to "whitespace" mode 

101 ), 

102 # Plain text 

103 (r"[^<]+", Other), 

104 (r"<", Other), 

105 ], 

106 # Ignore mode ignores everything upto the matching ``<?end ignore?>`` tag 

107 "ignore": [ 

108 # Nested ``<?ignore?>`` tag 

109 (r"<\?\s*ignore\s*\?>", Comment, "#push"), 

110 # ``<?end ignore?>`` tag 

111 (r"<\?\s*end\s+ignore\s*\?>", Comment, "#pop"), 

112 # Everything else 

113 (r"[^<]+", Comment), 

114 (r".", Comment), 

115 ], 

116 # UL4 expressions 

117 "ul4": [ 

118 # End the tag 

119 (r"\?>", Comment.Preproc, "#pop"), 

120 # Start triple quoted string constant 

121 ("'''", String, "string13"), 

122 ('"""', String, "string23"), 

123 # Start single quoted string constant 

124 ("'", String, "string1"), 

125 ('"', String, "string2"), 

126 # Floating point number 

127 (r"\d+\.\d*([eE][+-]?\d+)?", Number.Float), 

128 (r"\.\d+([eE][+-]?\d+)?", Number.Float), 

129 (r"\d+[eE][+-]?\d+", Number.Float), 

130 # Binary integer: ``0b101010`` 

131 (r"0[bB][01]+", Number.Bin), 

132 # Octal integer: ``0o52`` 

133 (r"0[oO][0-7]+", Number.Oct), 

134 # Hexadecimal integer: ``0x2a`` 

135 (r"0[xX][0-9a-fA-F]+", Number.Hex), 

136 # Date or datetime: ``@(2000-02-29)``/``@(2000-02-29T12:34:56.987654)`` 

137 (r"@\(\d\d\d\d-\d\d-\d\d(T(\d\d:\d\d(:\d\d(\.\d{6})?)?)?)?\)", Literal.Date), 

138 # Color: ``#fff``, ``#fff8f0`` etc. 

139 (r"#[0-9a-fA-F]{8}", Literal.Color), 

140 (r"#[0-9a-fA-F]{6}", Literal.Color), 

141 (r"#[0-9a-fA-F]{3,4}", Literal.Color), 

142 # Decimal integer: ``42`` 

143 (r"\d+", Number.Integer), 

144 # Operators 

145 (r"//|==|!=|>=|<=|<<|>>|\+=|-=|\*=|/=|//=|<<=|>>=|&=|\|=|^=|=|[\[\]{},:*/().~%&|<>^+-]", Operator), 

146 # Keywords 

147 (words(("for", "in", "if", "else", "not", "is", "and", "or"), suffix=r"\b"), Keyword), 

148 # Builtin constants 

149 (words(("None", "False", "True"), suffix=r"\b"), Keyword.Constant), 

150 # Variable names 

151 (r"[a-zA-Z_][a-zA-Z0-9_]*", Name), 

152 # Whitespace 

153 (r"\s+", Text.Whitespace), 

154 ], 

155 # ``<?end ...?>`` tag for closing the last open block 

156 "end": [ 

157 (r"\?>", Comment.Preproc, "#pop"), 

158 (words(("for", "if", "def", "while", "renderblock", "renderblocks"), suffix=r"\b"), Keyword), 

159 (r"\s+", Text), 

160 ], 

161 # Content of the ``<?whitespace ...?>`` tag: 

162 # ``keep``, ``strip`` or ``smart`` 

163 "whitespace": [ 

164 (r"\?>", Comment.Preproc, "#pop"), 

165 (words(("keep", "strip", "smart"), suffix=r"\b"), Comment.Preproc), 

166 (r"\s+", Text.Whitespace), 

167 ], 

168 # Inside a string constant 

169 "stringescapes": [ 

170 (r"""\\[\\'"abtnfr]""", String.Escape), 

171 (r"\\x[0-9a-fA-F]{2}", String.Escape), 

172 (r"\\u[0-9a-fA-F]{4}", String.Escape), 

173 (r"\\U[0-9a-fA-F]{8}", String.Escape), 

174 ], 

175 # Inside a triple quoted string started with ``'''`` 

176 "string13": [ 

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

178 include("stringescapes"), 

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

180 (r'.', String), 

181 ], 

182 # Inside a triple quoted string started with ``"""`` 

183 "string23": [ 

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

185 include("stringescapes"), 

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

187 (r'.', String), 

188 ], 

189 # Inside a single quoted string started with ``'`` 

190 "string1": [ 

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

192 include("stringescapes"), 

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

194 (r'.', String), 

195 ], 

196 # Inside a single quoted string started with ``"`` 

197 "string2": [ 

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

199 include("stringescapes"), 

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

201 (r'.', String), 

202 ], 

203 } 

204 

205class HTMLUL4Lexer(DelegatingLexer): 

206 """ 

207 Lexer for UL4 embedded in HTML. 

208 """ 

209 

210 name = 'HTML+UL4' 

211 aliases = ['html+ul4'] 

212 filenames = ['*.htmlul4'] 

213 

214 def __init__(self, **options): 

215 super().__init__(HtmlLexer, UL4Lexer, **options) 

216 

217 

218class XMLUL4Lexer(DelegatingLexer): 

219 """ 

220 Lexer for UL4 embedded in XML. 

221 """ 

222 

223 name = 'XML+UL4' 

224 aliases = ['xml+ul4'] 

225 filenames = ['*.xmlul4'] 

226 

227 def __init__(self, **options): 

228 super().__init__(XmlLexer, UL4Lexer, **options) 

229 

230 

231class CSSUL4Lexer(DelegatingLexer): 

232 """ 

233 Lexer for UL4 embedded in CSS. 

234 """ 

235 

236 name = 'CSS+UL4' 

237 aliases = ['css+ul4'] 

238 filenames = ['*.cssul4'] 

239 

240 def __init__(self, **options): 

241 super().__init__(CssLexer, UL4Lexer, **options) 

242 

243 

244class JavascriptUL4Lexer(DelegatingLexer): 

245 """ 

246 Lexer for UL4 embedded in Javascript. 

247 """ 

248 

249 name = 'Javascript+UL4' 

250 aliases = ['js+ul4'] 

251 filenames = ['*.jsul4'] 

252 

253 def __init__(self, **options): 

254 super().__init__(JavascriptLexer, UL4Lexer, **options) 

255 

256 

257class PythonUL4Lexer(DelegatingLexer): 

258 """ 

259 Lexer for UL4 embedded in Python. 

260 """ 

261 

262 name = 'Python+UL4' 

263 aliases = ['py+ul4'] 

264 filenames = ['*.pyul4'] 

265 

266 def __init__(self, **options): 

267 super().__init__(PythonLexer, UL4Lexer, **options)