Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improve the CythonLexer so that it covers all code shown in the docs.

  • Loading branch information...
commit 5266ef627b70f9f40fcf98a7a671653d07c64054 1 parent a6795eb
Georg Brandl birkenfeld authored
8 docs/extension_types.rst
Source Rendered
@@ -283,6 +283,8 @@ when it is deleted.::
283 283 del shop.cheese
284 284 print shop.cheese
285 285
  286 +.. sourcecode:: text
  287 +
286 288 # Test output
287 289 We don't have: []
288 290 We don't have: ['camembert']
@@ -342,6 +344,8 @@ extension types.::
342 344 print "p2:"
343 345 p2.describe()
344 346
  347 +.. sourcecode:: text
  348 +
345 349 # Output
346 350 p1:
347 351 This parrot is resting.
@@ -437,7 +441,9 @@ built-in complex object.::
437 441
438 442 1. In this example, :keyword:`ctypedef` class has been used. This is
439 443 because, in the Python header files, the ``PyComplexObject`` struct is
440   - declared with::
  444 + declared with:
  445 +
  446 + .. sourcecode:: c
441 447
442 448 ctypedef struct {
443 449 ...
10 docs/external_C_code.rst
Source Rendered
@@ -179,19 +179,19 @@ same applies equally to union and enum declarations.
179 179 +-------------------------+---------------------------------------------+-----------------------------------------------------------------------+
180 180 | C code | Possibilities for corresponding Cython Code | Comments |
181 181 +=========================+=============================================+=======================================================================+
182   -| :: | :: | Cython will refer to the as ``struct Foo`` in the generated C code. |
  182 +| .. sourcecode:: c | :: | Cython will refer to the as ``struct Foo`` in the generated C code. |
183 183 | | | |
184 184 | struct Foo { | cdef struct Foo: | |
185 185 | ... | ... | |
186 186 | }; | | |
187 187 +-------------------------+---------------------------------------------+-----------------------------------------------------------------------+
188   -| :: | :: | Cython will refer to the type simply as ``Foo`` in |
  188 +| .. sourcecode:: c | :: | Cython will refer to the type simply as ``Foo`` in |
189 189 | | | the generated C code. |
190 190 | typedef struct { | ctypedef struct Foo: | |
191 191 | ... | ... | |
192 192 | } Foo; | | |
193 193 +-------------------------+---------------------------------------------+-----------------------------------------------------------------------+
194   -| :: | :: | If the C header uses both a tag and a typedef with *different* |
  194 +| .. sourcecode:: c | :: | If the C header uses both a tag and a typedef with *different* |
195 195 | | | names, you can use either form of declaration in Cython |
196 196 | typedef struct foo { | cdef struct foo: | (although if you need to forward reference the type, |
197 197 | ... | ... | you'll have to use the first form). |
@@ -202,7 +202,7 @@ same applies equally to union and enum declarations.
202 202 | | ctypedef struct Foo: | |
203 203 | | ... | |
204 204 +-------------------------+---------------------------------------------+-----------------------------------------------------------------------+
205   -| :: | :: | If the header uses the *same* name for the tag and typedef, you |
  205 +| .. sourcecode:: c | :: | If the header uses the *same* name for the tag and typedef, you |
206 206 | | | won't be able to include a :keyword:`ctypedef` for it -- but then, |
207 207 | typedef struct Foo { | cdef struct Foo: | it's not necessary. |
208 208 | ... | ... | |
@@ -352,6 +352,8 @@ made available when you include :file:`modulename_api.h`.::
352 352 if v.speed >= 88 and v.power >= 1.21:
353 353 print "Time travel achieved"
354 354
  355 +.. sourcecode:: c
  356 +
355 357 # marty.c
356 358 #include "delorean_api.h"
357 359
12 docs/source_files_and_compilation.rst
Source Rendered
@@ -12,7 +12,9 @@ file named :file:`primes.pyx`.
12 12
13 13 Once you have written your ``.pyx`` file, there are a couple of ways of turning it
14 14 into an extension module. One way is to compile it manually with the Cython
15   -compiler, e.g.::
  15 +compiler, e.g.:
  16 +
  17 +.. sourcecode:: text
16 18
17 19 $ cython primes.pyx
18 20
@@ -45,7 +47,9 @@ would be::
45 47
46 48 To understand the :file:`setup.py` more fully look at the official
47 49 :mod:`distutils` documentation. To compile the extension for use in the
48   -current directory use::
  50 +current directory use:
  51 +
  52 +.. sourcecode:: text
49 53
50 54 $ python setup.py build_ext --inplace
51 55
@@ -135,7 +139,9 @@ If you would always like to import Cython files without building them
135 139 specially, you can also the first line above to your :file:`sitecustomize.py`.
136 140 That will install the hook every time you run Python. Then you can use
137 141 Cython modules just with simple import statements. I like to test my
138   -Cython modules like this::
  142 +Cython modules like this:
  143 +
  144 +.. sourcecode:: text
139 145
140 146 $ python -c "import foo"
141 147
8 docs/tutorial.rst
Source Rendered
@@ -50,7 +50,9 @@ information see :ref:`compilation`)::
50 50 ext_modules = [Extension("helloworld", ["helloworld.pyx"])]
51 51 )
52 52
53   -To use this to build your Cython file use the commandline options::
  53 +To use this to build your Cython file use the commandline options:
  54 +
  55 +.. sourcecode:: text
54 56
55 57 $ python setup.py build_ext --inplace
56 58
@@ -93,7 +95,9 @@ module name, doing this we have:
93 95
94 96 .. literalinclude:: ../examples/tutorial/fib1/setup.py
95 97
96   -Build the extension with the same command used for the helloworld.pyx::
  98 +Build the extension with the same command used for the helloworld.pyx:
  99 +
  100 +.. sourcecode:: text
97 101
98 102 $ python setup.py build_ext --inplace
99 103
14 docs/wrapping_CPlusPlus.rst
Source Rendered
@@ -39,7 +39,9 @@ An example C++ API
39 39
40 40 Here is a tiny C++ API which we will use as an example throughout this
41 41 document. Let's assume it will be in a header file called
42   -:file:`Rectangle.h`::
  42 +:file:`Rectangle.h`:
  43 +
  44 +.. sourcecode:: c++
43 45
44 46 class Rectangle {
45 47 public:
@@ -116,7 +118,9 @@ Add class methods
116 118
117 119 Now, let's add the class methods. You can circumvent Cython syntax
118 120 limitations by declaring these as function pointers. Recall that in the C++
119   -class we have::
  121 +class we have:
  122 +
  123 +.. sourcecode:: c++
120 124
121 125 int getLength();
122 126 int getHeight();
@@ -278,7 +282,9 @@ Overloading
278 282 ^^^^^^^^^^^^
279 283
280 284 To support function overloading simply add a different alias to each
281   -signature, so if you have e.g. ::
  285 +signature, so if you have e.g.
  286 +
  287 +.. sourcecode:: c++
282 288
283 289 int foo(int a);
284 290 int foo(int a, int b);
@@ -294,7 +300,7 @@ Operators
294 300 Some operators (e.g. +,-,...) can be accessed from Cython like this::
295 301
296 302 ctypedef struct c_Rectangle "Rectangle":
297   - c_Rectangle add "operator+"(c_Rectangle right)
  303 + c_Rectangle add "operator+"(c_Rectangle right)
298 304
299 305 Declaring/Using References
300 306 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
59 sphinxext/cython_highlighting.py
@@ -33,14 +33,18 @@ class CythonLexer(RegexLexer):
33 33 (r'\\\n', Text),
34 34 (r'\\', Text),
35 35 (r'(in|is|and|or|not)\b', Operator.Word),
36   - (r'!=|==|<<|>>|[-~+/*%=<>&^|.]', Operator),
37   - (r'(from)(\d+)(<=)(\s+)(<)(\d+)(:)', bygroups(Keyword, Number.Integer, Operator, Name, Operator, Name, Punctuation)),
  36 + (r'(<)([a-zA-Z0-9.?]+)(>)',
  37 + bygroups(Punctuation, Keyword.Type, Punctuation)),
  38 + (r'!=|==|<<|>>|[-~+/*%=<>&^|.?]', Operator),
  39 + (r'(from)(\d+)(<=)(\s+)(<)(\d+)(:)',
  40 + bygroups(Keyword, Number.Integer, Operator, Name, Operator,
  41 + Name, Punctuation)),
38 42 include('keywords'),
39   - (r'(def)(\s+)', bygroups(Keyword, Text), 'funcname'),
40   - (r'(cdef)(\s+)', bygroups(Keyword, Text), 'cfuncname'),
41   - (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),
  43 + (r'(def|property)(\s+)', bygroups(Keyword, Text), 'funcname'),
  44 + (r'(cp?def)(\s+)', bygroups(Keyword, Text), 'cdef'),
  45 + (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'classname'),
42 46 (r'(from)(\s+)', bygroups(Keyword, Text), 'fromimport'),
43   - (r'(import)(\s+)', bygroups(Keyword, Text), 'import'),
  47 + (r'(c?import)(\s+)', bygroups(Keyword, Text), 'import'),
44 48 include('builtins'),
45 49 include('backtick'),
46 50 ('(?:[rR]|[uU][rR]|[rR][uU])"""', String, 'tdqs'),
@@ -55,20 +59,22 @@ class CythonLexer(RegexLexer):
55 59 include('numbers'),
56 60 ],
57 61 'keywords': [
58   - (r'(assert|break|continue|del|elif|else|except|exec|'
59   - r'finally|for|global|if|lambda|pass|print|raise|'
  62 + (r'(assert|break|by|continue|ctypedef|del|elif|else|except\??|exec|'
  63 + r'finally|for|gil|global|if|include|lambda|nogil|pass|print|raise|'
60 64 r'return|try|while|yield|as|with)\b', Keyword),
  65 + (r'(DEF|IF|ELIF|ELSE)\b', Comment.Preproc),
61 66 ],
62 67 'builtins': [
63   - (r'(?<!\.)(__import__|abs|apply|basestring|bool|buffer|callable|'
64   - r'chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|'
65   - r'divmod|double|enumerate|eval|execfile|exit|file|filter|float|getattr|'
66   - r'globals|hasattr|hash|hex|id|input|int|intern|isinstance|'
67   - r'issubclass|iter|len|list|locals|long|map|max|min|object|oct|'
68   - r'open|ord|pow|property|range|raw_input|reduce|reload|repr|'
69   - r'round|setattr|slice|staticmethod|str|sum|super|tuple|type|'
70   - r'unichr|unicode|unsigned|vars|xrange|zip)\b', Name.Builtin),
71   - (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True'
  68 + (r'(?<!\.)(__import__|abs|all|any|apply|basestring|bin|bool|buffer|'
  69 + r'bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|'
  70 + r'complex|delattr|dict|dir|divmod|enumerate|eval|execfile|exit|'
  71 + r'file|filter|float|frozenset|getattr|globals|hasattr|hash|hex|id|'
  72 + r'input|int|intern|isinstance|issubclass|iter|len|list|locals|'
  73 + r'long|map|max|min|next|object|oct|open|ord|pow|property|range|'
  74 + r'raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|'
  75 + r'sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|'
  76 + r'vars|xrange|zip)\b', Name.Builtin),
  77 + (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|NULL'
72 78 r')\b', Name.Builtin.Pseudo),
73 79 (r'(?<!\.)(ArithmeticError|AssertionError|AttributeError|'
74 80 r'BaseException|DeprecationWarning|EOFError|EnvironmentError|'
@@ -101,8 +107,19 @@ class CythonLexer(RegexLexer):
101 107 'funcname': [
102 108 ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Function, '#pop')
103 109 ],
104   - 'cfuncname': [
105   - ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Function, '#pop')
  110 + 'cdef': [
  111 + (r'(public|readonly|extern|api|inline)\b', Keyword.Reserved),
  112 + (r'(struct|enum|union|class)\b', Keyword),
  113 + (r'([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(?=[(:#=]|$)',
  114 + bygroups(Name.Function, Text), '#pop'),
  115 + (r'([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(,)',
  116 + bygroups(Name.Function, Text, Punctuation)),
  117 + (r'from\b', Keyword, '#pop'),
  118 + (r'as\b', Keyword),
  119 + (r':', Punctuation, '#pop'),
  120 + (r'(?=["\'])', Text, '#pop'),
  121 + (r'[a-zA-Z_][a-zA-Z0-9_]*', Keyword.Type),
  122 + (r'.', Text),
106 123 ],
107 124 'classname': [
108 125 ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop')
@@ -114,8 +131,10 @@ class CythonLexer(RegexLexer):
114 131 (r'', Text, '#pop') # all else: go back
115 132 ],
116 133 'fromimport': [
117   - (r'(\s+)((c)?import)\b', bygroups(Text, Keyword), '#pop'),
  134 + (r'(\s+)(c?import)\b', bygroups(Text, Keyword), '#pop'),
118 135 (r'[a-zA-Z_.][a-zA-Z0-9_.]*', Name.Namespace),
  136 + # ``cdef foo from "header"``, or ``for foo from 0 < i < 10``
  137 + (r'', Text, '#pop'),
119 138 ],
120 139 'stringescape': [
121 140 (r'\\([\\abfnrtv"\']|\n|N{.*?}|u[a-fA-F0-9]{4}|'

0 comments on commit 5266ef6

Please sign in to comment.
Something went wrong with that request. Please try again.