From: Georg Brandl Date: Sat, 7 Feb 2009 22:17:20 +0000 (+0100) Subject: Improve the CythonLexer so that it covers all code shown in the docs. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5266ef627b70f9f40fcf98a7a671653d07c64054;p=cython.git Improve the CythonLexer so that it covers all code shown in the docs. --- diff --git a/docs/extension_types.rst b/docs/extension_types.rst index 8ecea3ef..a8ed564c 100644 --- a/docs/extension_types.rst +++ b/docs/extension_types.rst @@ -283,6 +283,8 @@ when it is deleted.:: del shop.cheese print shop.cheese +.. sourcecode:: text + # Test output We don't have: [] We don't have: ['camembert'] @@ -342,6 +344,8 @@ extension types.:: print "p2:" p2.describe() +.. sourcecode:: text + # Output p1: This parrot is resting. @@ -437,7 +441,9 @@ built-in complex object.:: 1. In this example, :keyword:`ctypedef` class has been used. This is because, in the Python header files, the ``PyComplexObject`` struct is - declared with:: + declared with: + + .. sourcecode:: c ctypedef struct { ... diff --git a/docs/external_C_code.rst b/docs/external_C_code.rst index 6514c73f..4d5c3828 100644 --- a/docs/external_C_code.rst +++ b/docs/external_C_code.rst @@ -179,19 +179,19 @@ same applies equally to union and enum declarations. +-------------------------+---------------------------------------------+-----------------------------------------------------------------------+ | C code | Possibilities for corresponding Cython Code | Comments | +=========================+=============================================+=======================================================================+ -| :: | :: | Cython will refer to the as ``struct Foo`` in the generated C code. | +| .. sourcecode:: c | :: | Cython will refer to the as ``struct Foo`` in the generated C code. | | | | | | struct Foo { | cdef struct Foo: | | | ... | ... | | | }; | | | +-------------------------+---------------------------------------------+-----------------------------------------------------------------------+ -| :: | :: | Cython will refer to the type simply as ``Foo`` in | +| .. sourcecode:: c | :: | Cython will refer to the type simply as ``Foo`` in | | | | the generated C code. | | typedef struct { | ctypedef struct Foo: | | | ... | ... | | | } Foo; | | | +-------------------------+---------------------------------------------+-----------------------------------------------------------------------+ -| :: | :: | If the C header uses both a tag and a typedef with *different* | +| .. sourcecode:: c | :: | If the C header uses both a tag and a typedef with *different* | | | | names, you can use either form of declaration in Cython | | typedef struct foo { | cdef struct foo: | (although if you need to forward reference the type, | | ... | ... | you'll have to use the first form). | @@ -202,7 +202,7 @@ same applies equally to union and enum declarations. | | ctypedef struct Foo: | | | | ... | | +-------------------------+---------------------------------------------+-----------------------------------------------------------------------+ -| :: | :: | If the header uses the *same* name for the tag and typedef, you | +| .. sourcecode:: c | :: | If the header uses the *same* name for the tag and typedef, you | | | | won't be able to include a :keyword:`ctypedef` for it -- but then, | | typedef struct Foo { | cdef struct Foo: | it's not necessary. | | ... | ... | | @@ -352,6 +352,8 @@ made available when you include :file:`modulename_api.h`.:: if v.speed >= 88 and v.power >= 1.21: print "Time travel achieved" +.. sourcecode:: c + # marty.c #include "delorean_api.h" diff --git a/docs/source_files_and_compilation.rst b/docs/source_files_and_compilation.rst index 08ef2fa6..32b7df63 100644 --- a/docs/source_files_and_compilation.rst +++ b/docs/source_files_and_compilation.rst @@ -12,7 +12,9 @@ file named :file:`primes.pyx`. Once you have written your ``.pyx`` file, there are a couple of ways of turning it into an extension module. One way is to compile it manually with the Cython -compiler, e.g.:: +compiler, e.g.: + +.. sourcecode:: text $ cython primes.pyx @@ -45,7 +47,9 @@ would be:: To understand the :file:`setup.py` more fully look at the official :mod:`distutils` documentation. To compile the extension for use in the -current directory use:: +current directory use: + +.. sourcecode:: text $ python setup.py build_ext --inplace @@ -135,7 +139,9 @@ If you would always like to import Cython files without building them specially, you can also the first line above to your :file:`sitecustomize.py`. That will install the hook every time you run Python. Then you can use Cython modules just with simple import statements. I like to test my -Cython modules like this:: +Cython modules like this: + +.. sourcecode:: text $ python -c "import foo" diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 1c5e05be..7d8fee44 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -50,7 +50,9 @@ information see :ref:`compilation`):: ext_modules = [Extension("helloworld", ["helloworld.pyx"])] ) -To use this to build your Cython file use the commandline options:: +To use this to build your Cython file use the commandline options: + +.. sourcecode:: text $ python setup.py build_ext --inplace @@ -93,7 +95,9 @@ module name, doing this we have: .. literalinclude:: ../examples/tutorial/fib1/setup.py -Build the extension with the same command used for the helloworld.pyx:: +Build the extension with the same command used for the helloworld.pyx: + +.. sourcecode:: text $ python setup.py build_ext --inplace diff --git a/docs/wrapping_CPlusPlus.rst b/docs/wrapping_CPlusPlus.rst index 24360529..99db8567 100644 --- a/docs/wrapping_CPlusPlus.rst +++ b/docs/wrapping_CPlusPlus.rst @@ -39,7 +39,9 @@ An example C++ API Here is a tiny C++ API which we will use as an example throughout this document. Let's assume it will be in a header file called -:file:`Rectangle.h`:: +:file:`Rectangle.h`: + +.. sourcecode:: c++ class Rectangle { public: @@ -116,7 +118,9 @@ Add class methods Now, let's add the class methods. You can circumvent Cython syntax limitations by declaring these as function pointers. Recall that in the C++ -class we have:: +class we have: + +.. sourcecode:: c++ int getLength(); int getHeight(); @@ -278,7 +282,9 @@ Overloading ^^^^^^^^^^^^ To support function overloading simply add a different alias to each -signature, so if you have e.g. :: +signature, so if you have e.g. + +.. sourcecode:: c++ int foo(int a); int foo(int a, int b); @@ -294,7 +300,7 @@ Operators Some operators (e.g. +,-,...) can be accessed from Cython like this:: ctypedef struct c_Rectangle "Rectangle": - c_Rectangle add "operator+"(c_Rectangle right) + c_Rectangle add "operator+"(c_Rectangle right) Declaring/Using References ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sphinxext/cython_highlighting.py b/sphinxext/cython_highlighting.py index c86d9972..3a2e5f9e 100644 --- a/sphinxext/cython_highlighting.py +++ b/sphinxext/cython_highlighting.py @@ -33,14 +33,18 @@ class CythonLexer(RegexLexer): (r'\\\n', Text), (r'\\', Text), (r'(in|is|and|or|not)\b', Operator.Word), - (r'!=|==|<<|>>|[-~+/*%=<>&^|.]', Operator), - (r'(from)(\d+)(<=)(\s+)(<)(\d+)(:)', bygroups(Keyword, Number.Integer, Operator, Name, Operator, Name, Punctuation)), + (r'(<)([a-zA-Z0-9.?]+)(>)', + bygroups(Punctuation, Keyword.Type, Punctuation)), + (r'!=|==|<<|>>|[-~+/*%=<>&^|.?]', Operator), + (r'(from)(\d+)(<=)(\s+)(<)(\d+)(:)', + bygroups(Keyword, Number.Integer, Operator, Name, Operator, + Name, Punctuation)), include('keywords'), - (r'(def)(\s+)', bygroups(Keyword, Text), 'funcname'), - (r'(cdef)(\s+)', bygroups(Keyword, Text), 'cfuncname'), - (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'), + (r'(def|property)(\s+)', bygroups(Keyword, Text), 'funcname'), + (r'(cp?def)(\s+)', bygroups(Keyword, Text), 'cdef'), + (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'classname'), (r'(from)(\s+)', bygroups(Keyword, Text), 'fromimport'), - (r'(import)(\s+)', bygroups(Keyword, Text), 'import'), + (r'(c?import)(\s+)', bygroups(Keyword, Text), 'import'), include('builtins'), include('backtick'), ('(?:[rR]|[uU][rR]|[rR][uU])"""', String, 'tdqs'), @@ -55,20 +59,22 @@ class CythonLexer(RegexLexer): include('numbers'), ], 'keywords': [ - (r'(assert|break|continue|del|elif|else|except|exec|' - r'finally|for|global|if|lambda|pass|print|raise|' + (r'(assert|break|by|continue|ctypedef|del|elif|else|except\??|exec|' + r'finally|for|gil|global|if|include|lambda|nogil|pass|print|raise|' r'return|try|while|yield|as|with)\b', Keyword), + (r'(DEF|IF|ELIF|ELSE)\b', Comment.Preproc), ], 'builtins': [ - (r'(?