# Module: `lib.lexers`

Defines a variety of Pygments lexers for highlighting IPython code.

This includes:

> IPythonLexer, IPython3Lexer
>   Lexers for pure IPython (python + magic/shell commands)
> IPythonPartialTracebackLexer, IPythonTracebackLexer
>   Supports 2.x and 3.x via keyword `python3`. The partial traceback lexer reads everything but the Python code appearing in a traceback. The full lexer combines the partial lexer with an IPython lexer.
> IPythonConsoleLexer
>   A lexer for IPython console sessions, with support for tracebacks.
> IPyLexer
>   A friendly lexer which examines the first line of text and from it, decides whether to use an IPython lexer or an IPython console lexer. This is probably the only lexer that needs to be explicitly added to Pygments.

## 4 Classes

class `IPython.lib.lexers.``IPythonPartialTracebackLexer`(**options)

  Bases: `pygments.lexer.RegexLexer`

  Partial lexer for IPython tracebacks.

  Handles all the non-python output. This works for both Python 2.x and 3.x.

class `IPython.lib.lexers.``IPythonTracebackLexer`(**options)

  Bases: `pygments.lexer.DelegatingLexer`

  IPython traceback lexer.

  For doctests, the tracebacks can be snipped as much as desired with the exception to the lines that designate a traceback. For non-syntax error tracebacks, this is the line of hyphens. For syntax error tracebacks, this is the line which lists the File and line number.

  `__init__`(**options)

class `IPython.lib.lexers.``IPythonConsoleLexer`(**options)

  Bases: `pygments.lexer.Lexer`

  An IPython console lexer for IPython code-blocks and doctests, such as:

      .. code-block:: ipythonconsole

          In [1]: a = 'foo'

          In [2]: a
          Out[2]: 'foo'

          In [3]: print a
          foo

          In [4]: 1 / 0

  Support is also provided for IPython exceptions:

      .. code-block:: ipythonconsole

          In [1]: raise Exception

          ---------------------------------------------------------------------------
          Exception                                 Traceback (most recent call last)
          <ipython-input-1-fca2ab0ca76b> in <module>()
          ----> 1 raise Exception

          Exception:

  `__init__`(**options)

    Initialize the IPython console lexer.

    Parameters:

    - python3 (bool) – If `True`, then the console inputs are parsed using a Python 3 lexer. Otherwise, they are parsed using a Python 2 lexer.
    - in1_regex (RegexObject) – The compiled regular expression used to detect the start of inputs. Although the IPython configuration setting may have a trailing whitespace, do not include it in the regex. If `None`, then the default input prompt is assumed.
    - in2_regex (RegexObject) – The compiled regular expression used to detect the continuation of inputs. Although the IPython configuration setting may have a trailing whitespace, do not include it in the regex. If `None`, then the default input prompt is assumed.
    - out_regex (RegexObject) – The compiled regular expression used to detect outputs. If `None`, then the default output prompt is assumed.

  `buffered_tokens`()

    Generator of unprocessed tokens after doing insertions and before changing to a new state.

  `get_mci`(line)

    Parses the line and returns a 3-tuple: (mode, code, insertion).

    `mode` is the next mode (or state) of the lexer, and is always equal to ‘input’, ‘output’, or ‘tb’.

    `code` is a portion of the line that should be added to the buffer corresponding to the next mode and eventually lexed by another lexer. For example, `code` could be Python code if `mode` were ‘input’.

    `insertion` is a 3-tuple (index, token, text) representing an unprocessed “token” that will be inserted into the stream of tokens that are created from the buffer once we change modes. This is usually the input or output prompt.

    In general, the next mode depends on current mode and on the contents of `line`.

  `ipytb_start` = re.compile('^(\\^C)?(-+\\n)|^( File)(.*)(, line )(\\d+\\n)')

    The regex to determine when a traceback starts.

class `IPython.lib.lexers.``IPyLexer`(**options)

  Bases: `pygments.lexer.Lexer`

  Primary lexer for all IPython-like code.

  This is a simple helper lexer. If the first line of the text begins with “In [[0-9]+]:”, then the entire text is parsed with an IPython console lexer. If not, then the entire text is parsed with an IPython lexer.

  The goal is to reduce the number of lexers that are registered with Pygments.

  `__init__`(**options)

## 1 Function

`IPython.lib.lexers.``build_ipy_lexer`(python3)

  Builds IPython lexers depending on the value of `python3`.

  The lexer inherits from an appropriate Python lexer and then adds information about IPython specific keywords (i.e. magic commands, shell commands, etc.)

  Parameters: | python3 (bool) – If `True`, then build an IPython lexer from a Python 3 lexer.