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

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

84 statements  

1""" 

2 pygments.lexers.algebra 

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

4 

5 Lexers for computer algebra systems. 

6 

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

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11import re 

12 

13from pygments.lexer import Lexer, RegexLexer, bygroups, do_insertions, words 

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

15 Number, Punctuation, Generic, Whitespace 

16 

17__all__ = ['GAPLexer', 'GAPConsoleLexer', 'MathematicaLexer', 'MuPADLexer', 

18 'BCLexer'] 

19 

20 

21class GAPLexer(RegexLexer): 

22 """ 

23 For GAP source code. 

24 """ 

25 name = 'GAP' 

26 url = 'https://www.gap-system.org' 

27 aliases = ['gap'] 

28 filenames = ['*.g', '*.gd', '*.gi', '*.gap'] 

29 version_added = '2.0' 

30 

31 tokens = { 

32 'root': [ 

33 (r'#.*$', Comment.Single), 

34 (r'"(?:[^"\\]|\\.)*"', String), 

35 (r'\(|\)|\[|\]|\{|\}', Punctuation), 

36 (r'''(?x)\b(?: 

37 if|then|elif|else|fi| 

38 for|while|do|od| 

39 repeat|until| 

40 break|continue| 

41 function|local|return|end| 

42 rec| 

43 quit|QUIT| 

44 IsBound|Unbind| 

45 TryNextMethod| 

46 Info|Assert 

47 )\b''', Keyword), 

48 (r'''(?x)\b(?: 

49 true|false|fail|infinity 

50 )\b''', 

51 Name.Constant), 

52 (r'''(?x)\b(?: 

53 (Declare|Install)([A-Z][A-Za-z]+)| 

54 BindGlobal|BIND_GLOBAL 

55 )\b''', 

56 Name.Builtin), 

57 (r'\.|,|:=|;|=|\+|-|\*|/|\^|>|<', Operator), 

58 (r'''(?x)\b(?: 

59 and|or|not|mod|in 

60 )\b''', 

61 Operator.Word), 

62 (r'''(?x) 

63 (?:\w+|`[^`]*`) 

64 (?:::\w+|`[^`]*`)*''', Name.Variable), 

65 (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number), 

66 (r'\.[0-9]+(?:e[0-9]+)?', Number), 

67 (r'.', Text) 

68 ], 

69 } 

70 

71 def analyse_text(text): 

72 score = 0.0 

73 

74 # Declaration part 

75 if re.search( 

76 r"(InstallTrueMethod|Declare(Attribute|Category|Filter|Operation" + 

77 r"|GlobalFunction|Synonym|SynonymAttr|Property))", text 

78 ): 

79 score += 0.7 

80 

81 # Implementation part 

82 if re.search( 

83 r"(DeclareRepresentation|Install(GlobalFunction|Method|" + 

84 r"ImmediateMethod|OtherMethod)|New(Family|Type)|Objectify)", text 

85 ): 

86 score += 0.7 

87 

88 return min(score, 1.0) 

89 

90 

91class GAPConsoleLexer(Lexer): 

92 """ 

93 For GAP console sessions. Modeled after JuliaConsoleLexer. 

94 """ 

95 name = 'GAP session' 

96 aliases = ['gap-console', 'gap-repl'] 

97 filenames = ['*.tst'] 

98 url = 'https://www.gap-system.org' 

99 version_added = '2.14' 

100 _example = "gap-repl/euclidean.tst" 

101 

102 def get_tokens_unprocessed(self, text): 

103 gaplexer = GAPLexer(**self.options) 

104 start = 0 

105 curcode = '' 

106 insertions = [] 

107 output = False 

108 error = False 

109 

110 for line in text.splitlines(keepends=True): 

111 if line.startswith('gap> ') or line.startswith('brk> '): 

112 insertions.append((len(curcode), [(0, Generic.Prompt, line[:5])])) 

113 curcode += line[5:] 

114 output = False 

115 error = False 

116 elif not output and line.startswith('> '): 

117 insertions.append((len(curcode), [(0, Generic.Prompt, line[:2])])) 

118 curcode += line[2:] 

119 else: 

120 if curcode: 

121 yield from do_insertions( 

122 insertions, gaplexer.get_tokens_unprocessed(curcode)) 

123 curcode = '' 

124 insertions = [] 

125 if line.startswith('Error, ') or error: 

126 yield start, Generic.Error, line 

127 error = True 

128 else: 

129 yield start, Generic.Output, line 

130 output = True 

131 start += len(line) 

132 

133 if curcode: 

134 yield from do_insertions( 

135 insertions, gaplexer.get_tokens_unprocessed(curcode)) 

136 

137 # the following is needed to distinguish Scilab and GAP .tst files 

138 def analyse_text(text): 

139 # GAP prompts are a dead give away, although hypothetical;y a 

140 # file in another language could be trying to compare a variable 

141 # "gap" as in "gap> 0.1". But that this should happen at the 

142 # start of a line seems unlikely... 

143 if re.search(r"^gap> ", text): 

144 return 0.9 

145 else: 

146 return 0.0 

147 

148 

149class MathematicaLexer(RegexLexer): 

150 """ 

151 Lexer for Mathematica source code. 

152 """ 

153 name = 'Mathematica' 

154 url = 'https://www.wolfram.com/language/' 

155 aliases = ['mathematica', 'mma', 'nb', 'wl', 'wolfram'] 

156 filenames = ['*.nb', '*.cdf', '*.nbp', '*.ma', '*.wl', '*.wls'] 

157 mimetypes = ['application/mathematica', 

158 'application/vnd.wolfram.mathematica', 

159 'application/vnd.wolfram.mathematica.package', 

160 'application/vnd.wolfram.cdf', 

161 'application/vnd.wolfram.wl'] 

162 version_added = '2.0' 

163 

164 # https://reference.wolfram.com/language/guide/Syntax.html 

165 operators = ( 

166 ";;", "=", "=.", "!=" "==", ":=", "->", ":>", "/.", "+", "-", "*", "/", 

167 "^", "&&", "||", "!", "<>", "|", "/;", "?", "@", "//", "/@", "@@", 

168 "@@@", "~~", "===", "&", "<", ">", "<=", ">=", 

169 ) 

170 

171 punctuation = (",", ";", "(", ")", "[", "]", "{", "}") 

172 

173 def _multi_escape(entries): 

174 return '({})'.format('|'.join(re.escape(entry) for entry in entries)) 

175 

176 tokens = { 

177 'root': [ 

178 (r'(?s)\(\*.*?\*\)', Comment), 

179 

180 (r'([a-zA-Z]+[A-Za-z0-9]*`)', Name.Namespace), 

181 (r'([A-Za-z0-9]*_+[A-Za-z0-9]*)', Name.Variable), 

182 (r'#\d*', Name.Variable), 

183 (r'([a-zA-Z]+[a-zA-Z0-9]*)', Name), 

184 

185 (r'-?\d+\.\d*', Number.Float), 

186 (r'-?\d*\.\d+', Number.Float), 

187 (r'-?\d+', Number.Integer), 

188 

189 (words(operators), Operator), 

190 (words(punctuation), Punctuation), 

191 (r'".*?"', String), 

192 (r'\s+', Text.Whitespace), 

193 ], 

194 } 

195 

196 

197class MuPADLexer(RegexLexer): 

198 """ 

199 A MuPAD lexer. 

200 Contributed by Christopher Creutzig <christopher@creutzig.de>. 

201 """ 

202 name = 'MuPAD' 

203 url = 'http://www.mupad.com' 

204 aliases = ['mupad'] 

205 filenames = ['*.mu'] 

206 version_added = '0.8' 

207 

208 tokens = { 

209 'root': [ 

210 (r'//.*?$', Comment.Single), 

211 (r'/\*', Comment.Multiline, 'comment'), 

212 (r'"(?:[^"\\]|\\.)*"', String), 

213 (r'\(|\)|\[|\]|\{|\}', Punctuation), 

214 (r'''(?x)\b(?: 

215 next|break|end| 

216 axiom|end_axiom|category|end_category|domain|end_domain|inherits| 

217 if|%if|then|elif|else|end_if| 

218 case|of|do|otherwise|end_case| 

219 while|end_while| 

220 repeat|until|end_repeat| 

221 for|from|to|downto|step|end_for| 

222 proc|local|option|save|begin|end_proc| 

223 delete|frame 

224 )\b''', Keyword), 

225 (r'''(?x)\b(?: 

226 DOM_ARRAY|DOM_BOOL|DOM_COMPLEX|DOM_DOMAIN|DOM_EXEC|DOM_EXPR| 

227 DOM_FAIL|DOM_FLOAT|DOM_FRAME|DOM_FUNC_ENV|DOM_HFARRAY|DOM_IDENT| 

228 DOM_INT|DOM_INTERVAL|DOM_LIST|DOM_NIL|DOM_NULL|DOM_POLY|DOM_PROC| 

229 DOM_PROC_ENV|DOM_RAT|DOM_SET|DOM_STRING|DOM_TABLE|DOM_VAR 

230 )\b''', Name.Class), 

231 (r'''(?x)\b(?: 

232 PI|EULER|E|CATALAN| 

233 NIL|FAIL|undefined|infinity| 

234 TRUE|FALSE|UNKNOWN 

235 )\b''', 

236 Name.Constant), 

237 (r'\b(?:dom|procname)\b', Name.Builtin.Pseudo), 

238 (r'\.|,|:|;|=|\+|-|\*|/|\^|@|>|<|\$|\||!|\'|%|~=', Operator), 

239 (r'''(?x)\b(?: 

240 and|or|not|xor| 

241 assuming| 

242 div|mod| 

243 union|minus|intersect|in|subset 

244 )\b''', 

245 Operator.Word), 

246 (r'\b(?:I|RDN_INF|RD_NINF|RD_NAN)\b', Number), 

247 # (r'\b(?:adt|linalg|newDomain|hold)\b', Name.Builtin), 

248 (r'''(?x) 

249 ((?:[a-zA-Z_#][\w#]*|`[^`]*`) 

250 (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*)(\s*)([(])''', 

251 bygroups(Name.Function, Text, Punctuation)), 

252 (r'''(?x) 

253 (?:[a-zA-Z_#][\w#]*|`[^`]*`) 

254 (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*''', Name.Variable), 

255 (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number), 

256 (r'\.[0-9]+(?:e[0-9]+)?', Number), 

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

258 (r'.', Text) 

259 ], 

260 'comment': [ 

261 (r'[^/*]+', Comment.Multiline), 

262 (r'/\*', Comment.Multiline, '#push'), 

263 (r'\*/', Comment.Multiline, '#pop'), 

264 (r'[*/]', Comment.Multiline) 

265 ], 

266 } 

267 

268 

269class BCLexer(RegexLexer): 

270 """ 

271 A BC lexer. 

272 """ 

273 name = 'BC' 

274 url = 'https://www.gnu.org/software/bc/' 

275 aliases = ['bc'] 

276 filenames = ['*.bc'] 

277 version_added = '2.1' 

278 

279 tokens = { 

280 'root': [ 

281 (r'/\*', Comment.Multiline, 'comment'), 

282 (r'"(?:[^"\\]|\\.)*"', String), 

283 (r'[{}();,]', Punctuation), 

284 (words(('if', 'else', 'while', 'for', 'break', 'continue', 

285 'halt', 'return', 'define', 'auto', 'print', 'read', 

286 'length', 'scale', 'sqrt', 'limits', 'quit', 

287 'warranty'), suffix=r'\b'), Keyword), 

288 (r'\+\+|--|\|\||&&|' 

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

290 # bc doesn't support exponential 

291 (r'[0-9]+(\.[0-9]*)?', Number), 

292 (r'\.[0-9]+', Number), 

293 (r'.', Text) 

294 ], 

295 'comment': [ 

296 (r'[^*/]+', Comment.Multiline), 

297 (r'\*/', Comment.Multiline, '#pop'), 

298 (r'[*/]', Comment.Multiline) 

299 ], 

300 }