ThirdParty/Pygments/pygments/lexers/math.py

changeset 684
2f29a0b6e1c7
parent 0
de9c2efb9d02
child 808
8f85926125ef
equal deleted inserted replaced
682:91114a975eda 684:2f29a0b6e1c7
3 pygments.lexers.math 3 pygments.lexers.math
4 ~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~
5 5
6 Lexers for math languages. 6 Lexers for math languages.
7 7
8 :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 11
12 import re 12 import re
13 try:
14 set
15 except NameError:
16 from sets import Set as set
17 13
18 from pygments.lexer import Lexer, RegexLexer, bygroups, include, do_insertions 14 from pygments.lexer import Lexer, RegexLexer, bygroups, include, do_insertions
19 from pygments.token import Comment, String, Punctuation, Keyword, Name, \ 15 from pygments.token import Comment, String, Punctuation, Keyword, Name, \
20 Operator, Number, Text, Generic 16 Operator, Number, Text, Generic
21 17
22 from pygments.lexers.agile import PythonLexer 18 from pygments.lexers.agile import PythonLexer
23 19
24 __all__ = ['MuPADLexer', 'MatlabLexer', 'MatlabSessionLexer', 'NumPyLexer', 20 __all__ = ['MuPADLexer', 'MatlabLexer', 'MatlabSessionLexer', 'NumPyLexer',
25 'SLexer'] 21 'RConsoleLexer', 'SLexer']
26 22
27 23
28 class MuPADLexer(RegexLexer): 24 class MuPADLexer(RegexLexer):
29 """ 25 """
30 A `MuPAD <http://www.mupad.com>`_ lexer. 26 A `MuPAD <http://www.mupad.com>`_ lexer.
338 yield index, Keyword.Pseudo, value 334 yield index, Keyword.Pseudo, value
339 else: 335 else:
340 yield index, token, value 336 yield index, token, value
341 337
342 338
339 class RConsoleLexer(Lexer):
340 """
341 For R console transcripts or R CMD BATCH output files.
342 """
343
344 name = 'RConsole'
345 aliases = ['rconsole', 'rout']
346 filenames = ['*.Rout']
347
348 def get_tokens_unprocessed(self, text):
349 slexer = SLexer(**self.options)
350
351 current_code_block = ''
352 insertions = []
353
354 for match in line_re.finditer(text):
355 line = match.group()
356 if line.startswith('>') or line.startswith('+'):
357 # Colorize the prompt as such,
358 # then put rest of line into current_code_block
359 insertions.append((len(current_code_block),
360 [(0, Generic.Prompt, line[:2])]))
361 current_code_block += line[2:]
362 else:
363 # We have reached a non-prompt line!
364 # If we have stored prompt lines, need to process them first.
365 if current_code_block:
366 # Weave together the prompts and highlight code.
367 for item in do_insertions(insertions,
368 slexer.get_tokens_unprocessed(current_code_block)):
369 yield item
370 # Reset vars for next code block.
371 current_code_block = ''
372 insertions = []
373 # Now process the actual line itself, this is output from R.
374 yield match.start(), Generic.Output, line
375
376 # If we happen to end on a code block with nothing after it, need to
377 # process the last code block. This is neither elegant nor DRY so
378 # should be changed.
379 if current_code_block:
380 for item in do_insertions(insertions,
381 slexer.get_tokens_unprocessed(current_code_block)):
382 yield item
383
384
343 class SLexer(RegexLexer): 385 class SLexer(RegexLexer):
344 """ 386 """
345 For S, S-plus, and R source code. 387 For S, S-plus, and R source code.
346 388
347 *New in Pygments 0.10.* 389 *New in Pygments 0.10.*

eric ide

mercurial