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

76 statements  

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

1""" 

2 pygments.lexers.algebra 

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

4 

5 Lexers for computer algebra systems. 

6 

7 :copyright: Copyright 2006-2023 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 .. versionadded:: 2.0 

26 """ 

27 name = 'GAP' 

28 url = 'http://www.gap-system.org' 

29 aliases = ['gap'] 

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

31 

32 tokens = { 

33 'root': [ 

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

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

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

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

38 if|then|elif|else|fi| 

39 for|while|do|od| 

40 repeat|until| 

41 break|continue| 

42 function|local|return|end| 

43 rec| 

44 quit|QUIT| 

45 IsBound|Unbind| 

46 TryNextMethod| 

47 Info|Assert 

48 )\b''', Keyword), 

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

50 true|false|fail|infinity 

51 )\b''', 

52 Name.Constant), 

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

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

55 BindGlobal|BIND_GLOBAL 

56 )\b''', 

57 Name.Builtin), 

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

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

60 and|or|not|mod|in 

61 )\b''', 

62 Operator.Word), 

63 (r'''(?x) 

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

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

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

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

68 (r'.', Text) 

69 ], 

70 } 

71 

72 def analyse_text(text): 

73 score = 0.0 

74 

75 # Declaration part 

76 if re.search( 

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

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

79 ): 

80 score += 0.7 

81 

82 # Implementation part 

83 if re.search( 

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

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

86 ): 

87 score += 0.7 

88 

89 return min(score, 1.0) 

90 

91 

92class GAPConsoleLexer(Lexer): 

93 """ 

94 For GAP console sessions. Modeled after JuliaConsoleLexer. 

95 

96 .. versionadded:: 2.14 

97 """ 

98 name = 'GAP session' 

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

100 filenames = ['*.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 .. versionadded:: 2.0 

154 """ 

155 name = 'Mathematica' 

156 url = 'http://www.wolfram.com/mathematica/' 

157 aliases = ['mathematica', 'mma', 'nb'] 

158 filenames = ['*.nb', '*.cdf', '*.nbp', '*.ma'] 

159 mimetypes = ['application/mathematica', 

160 'application/vnd.wolfram.mathematica', 

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

162 'application/vnd.wolfram.cdf'] 

163 

164 # http://reference.wolfram.com/mathematica/guide/Syntax.html 

165 operators = ( 

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

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

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

169 ) 

170 

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

172 

173 def _multi_escape(entries): 

174 return '(%s)' % ('|'.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 .. versionadded:: 0.8 

203 """ 

204 name = 'MuPAD' 

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

206 aliases = ['mupad'] 

207 filenames = ['*.mu'] 

208 

209 tokens = { 

210 'root': [ 

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

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

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

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

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

216 next|break|end| 

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

218 if|%if|then|elif|else|end_if| 

219 case|of|do|otherwise|end_case| 

220 while|end_while| 

221 repeat|until|end_repeat| 

222 for|from|to|downto|step|end_for| 

223 proc|local|option|save|begin|end_proc| 

224 delete|frame 

225 )\b''', Keyword), 

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

227 DOM_ARRAY|DOM_BOOL|DOM_COMPLEX|DOM_DOMAIN|DOM_EXEC|DOM_EXPR| 

228 DOM_FAIL|DOM_FLOAT|DOM_FRAME|DOM_FUNC_ENV|DOM_HFARRAY|DOM_IDENT| 

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

230 DOM_PROC_ENV|DOM_RAT|DOM_SET|DOM_STRING|DOM_TABLE|DOM_VAR 

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

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

233 PI|EULER|E|CATALAN| 

234 NIL|FAIL|undefined|infinity| 

235 TRUE|FALSE|UNKNOWN 

236 )\b''', 

237 Name.Constant), 

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

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

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

241 and|or|not|xor| 

242 assuming| 

243 div|mod| 

244 union|minus|intersect|in|subset 

245 )\b''', 

246 Operator.Word), 

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

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

249 (r'''(?x) 

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

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

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

253 (r'''(?x) 

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

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

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

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

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

259 (r'.', Text) 

260 ], 

261 'comment': [ 

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

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

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

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

266 ], 

267 } 

268 

269 

270class BCLexer(RegexLexer): 

271 """ 

272 A BC lexer. 

273 

274 .. versionadded:: 2.1 

275 """ 

276 name = 'BC' 

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

278 aliases = ['bc'] 

279 filenames = ['*.bc'] 

280 

281 tokens = { 

282 'root': [ 

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

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

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

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

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

288 'length', 'scale', 'sqrt', 'limits', 'quit', 

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

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

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

292 # bc doesn't support exponential 

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

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

295 (r'.', Text) 

296 ], 

297 'comment': [ 

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

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

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

301 ], 

302 }