| 1 |
"""Alternate Ada and Project Files parsers for Sphinx/Rest.""" |
| 2 |
|
| 3 |
import re |
| 4 |
from pygments.lexer import RegexLexer, bygroups |
| 5 |
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
| 6 |
Number, Punctuation |
| 7 |
|
| 8 |
|
| 9 |
def get_lexer_tokens(tag_highlighting=False, project_support=False): |
| 10 |
"""Return the tokens needed for RegexLexer. |
| 11 |
|
| 12 |
:param tag_highlighting: if True we support tag highlighting. See |
| 13 |
AdaLexerWithTags documentation |
| 14 |
:type tag_highlighting: bool |
| 15 |
:param project_support: if True support additional keywords associated |
| 16 |
with project files. |
| 17 |
:type project_support: bool |
| 18 |
|
| 19 |
:return: a dictionary following the structure required by RegexLexer |
| 20 |
:rtype: dict |
| 21 |
""" |
| 22 |
if project_support: |
| 23 |
project_pattern = r'project\s+|' |
| 24 |
project_pattern2 = \ |
| 25 |
r'aggregate|extends|external|external_as_list|library|project|' |
| 26 |
else: |
| 27 |
project_pattern = r'' |
| 28 |
project_pattern2 = r'' |
| 29 |
|
| 30 |
result = { |
| 31 |
'root': [ |
| 32 |
# Comments |
| 33 |
(r'--.*$', Comment), |
| 34 |
# Character literal |
| 35 |
(r"'.'", String.Char), |
| 36 |
# Strings |
| 37 |
(r'"[^"]*"', String), |
| 38 |
# Numeric |
| 39 |
# Based literal |
| 40 |
(r'[0-9][0-9_]*#[0-9a-f][0-9a-f_]*#(E[\+-]?[0-9][0-9_]*)?', |
| 41 |
Number.Integer), |
| 42 |
(r'[0-9][0-9_]*#[0-9a-f][0-9a-f_]*' |
| 43 |
r'\.[0-9a-f][0-9a-f_]*#(E[\+-]?[0-9][0-9_]*)?', Number.Float), |
| 44 |
# Decimal literal |
| 45 |
(r'[0-9][0-9_]*\.[0-9][0-9_](E[\+-]?[0-9][0-9_]*)?', Number.Float), |
| 46 |
(r'[0-9][0-9_]*(E[\+-]?[0-9][0-9_]*)?', Number.Integer), |
| 47 |
# Match use and with statements |
| 48 |
# The first part of the pattern is be sure we don't match |
| 49 |
# for/use constructs. |
| 50 |
(r'(\n\s*|;\s*)(with|use)(\s+[\w\.]+\s*;)', |
| 51 |
bygroups(Punctuation, Keyword.Reserved, Name.Namespace)), |
| 52 |
# Match procedure, package and function declarations |
| 53 |
(r'end\s+(if|loop|record)', Keyword), |
| 54 |
(r'(package(?:\s+body)?\s+|' + project_pattern + |
| 55 |
r'function\s+|end\s+|procedure\s+)([\w\.]+)', |
| 56 |
bygroups(Keyword, Name.Function)), |
| 57 |
# Ada 2012 standard attributes, GNAT specific ones and |
| 58 |
# Spark 2014 ones ('Update and 'Loop_Entry) |
| 59 |
# (reversed order to avoid having for |
| 60 |
# example Max before Max_Alignment_For_Allocation). |
| 61 |
(r'\'(Write|Width|Wide_Width|Wide_Wide_Width|Wide_Wide_Value|' |
| 62 |
r'Wide_Wide_Image|Wide_Value|Wide_Image|Word_Size|Wchar_T_Size|' |
| 63 |
r'Version|Value_Size|Value|Valid_Scalars|VADS_Size|Valid|Val|' |
| 64 |
r'Update|Unrestricted_Access|Universal_Literal_String|' |
| 65 |
r'Unconstrained_Array|Unchecked_Access|Unbiased_Rounding|' |
| 66 |
r'UET_Address|Truncation|Type_Class|To_Address|Tick|Terminated|' |
| 67 |
r'Target_Name|Tag|System_Allocator_Alignment|Succ|Stub_Type|' |
| 68 |
r'Stream_Size|Storage_Unit|Storage_Size|Storage_Pool|Small|Size|' |
| 69 |
r'Simple_Storage_Pool|Signed_Zeros|Scaling|Scale|' |
| 70 |
r'Scalar_Storage_Order|Safe_Last|Safe_Large|Safe_First|' |
| 71 |
r'Safe_Emax|Rounding|Round|Result|Remainder|Ref|Read|' |
| 72 |
r'Range_Length|Range|Priority|Pred|' |
| 73 |
r'Position|Pos|Pool_Address|Passed_By_Reference|Partition_Id|' |
| 74 |
r'Overlaps_Storage|Output|Old|Object_Size|Null_Parameter|Modulus|' |
| 75 |
r'Model_Small|Model_Mantissa|Model_Epsilon|Model_Emin|Model|Mod|' |
| 76 |
r'Min|Mechanism_Code|Maximum_Alignment|' |
| 77 |
r'Max_Size_In_Storage_Elements|Max_Priority|' |
| 78 |
r'Max_Interrupt_Priority|Max_Alignment_For_Allocation|' |
| 79 |
r'Max|Mantissa|Machine_Size|Machine_Rounds|Machine_Rounding|' |
| 80 |
r'Machine_Radix|Machine_Overflows|Machine_Mantissa|Machine_Emin|' |
| 81 |
r'Machine_Emax|Machine|Loop_Entry|Length|Length|Leading_Part|' |
| 82 |
r'Last_Valid|Last_Bit|Last|Large|Invalid_Value|Integer_Value|' |
| 83 |
r'Input|Image|Img|Identity|Has_Same_Storage|Has_Discriminants|' |
| 84 |
r'Has_Access_Values|Fraction|Fore|Floor|Fixed_Value|First_Valid|' |
| 85 |
r'First_Bit|First|External_Tag|Exponent|Epsilon|Enum_Val|' |
| 86 |
r'Enum_Rep|Enabled|Emax|Elaborated|Elab_Subp_Body|Elab_Spec|' |
| 87 |
r'Elab_Body|Descriptor_Size|Digits|Denorm|Delta|Definite|' |
| 88 |
r'Default_Bit_Order|Count|Copy_Sign|Constrained|' |
| 89 |
r'Compose|Component_Size|Compiler_Version|Code_Address|Class|' |
| 90 |
r'Ceiling|Caller|Callable|Body_Version|Bit_Order|Bit_Position|' |
| 91 |
r'Bit|Base|Asm_Output|Asm_Input|Alignment|Aft|Adjacent|' |
| 92 |
r'Address_Size|Address|Access|Abort_Signal|AST_Entry)', |
| 93 |
Name.Attribute), |
| 94 |
# All Ada2012 reserved words |
| 95 |
(r'(abort|abstract|abs|accept|access|aliased|all|and|array|at|' |
| 96 |
r'begin|body|case|constant|declare|delay|delta|digits|do|' |
| 97 |
r'else|elsif|end|entry|exception|exit|for|function|generic|goto|' |
| 98 |
r'if|interface|in|is|limited|loop|mod|new|not|null|' |
| 99 |
r'of|or|others|out|overriding|' + project_pattern2 + |
| 100 |
r'package|pragma|private|procedure|protected|' |
| 101 |
r'raise|range|record|rem|renames|requeue|return|reverse|' |
| 102 |
r'select|separate|some|subtype|synchronized|' |
| 103 |
r'tagged|task|terminate|then|type|until|use|when|while|with|xor' |
| 104 |
r')([\s;,])', |
| 105 |
bygroups(Keyword.Reserved, Punctuation)), |
| 106 |
# Two characters operators |
| 107 |
(r'=>|\.\.|\*\*|:=|/=|>=|<=|<<|>>|<>', Operator), |
| 108 |
# One character operators |
| 109 |
(r'&|\'|\(|\)|\*|\+|-|\.|/|:|<|=|>|\|', Operator), |
| 110 |
(r',|;', Punctuation), |
| 111 |
# Spaces |
| 112 |
(r'\s+', Text), |
| 113 |
# Builtin values |
| 114 |
(r'False|True', Keyword.Constant), |
| 115 |
# Identifiers |
| 116 |
(r'[\w\.]+', Name), |
| 117 |
(r'.', Text)]} |
| 118 |
|
| 119 |
# Insert tag highlighting before identifiers |
| 120 |
if tag_highlighting: |
| 121 |
result['root'].insert(-1, (r'\[[\w ]*\]', Name.Tag)) |
| 122 |
|
| 123 |
return result |
| 124 |
|
| 125 |
|
| 126 |
class AdaLexer(RegexLexer): |
| 127 |
"""Alternate Pygments lexer for Ada source code and project files. |
| 128 |
|
| 129 |
The default pygments lexer always fails causing disabling of syntax |
| 130 |
highlighting in Sphinx. This lexer is simpler but safer. |
| 131 |
|
| 132 |
In order to use this lexer in your Sphinx project add the following |
| 133 |
code at the end of your conf.py |
| 134 |
|
| 135 |
.. code-block:: python |
| 136 |
|
| 137 |
import gnatpython.ada_pygments |
| 138 |
|
| 139 |
def setup(app): |
| 140 |
app.add_lexer('ada', gnatpython.ada_pygments.AdaLexer()) |
| 141 |
|
| 142 |
""" |
| 143 |
name = 'Ada' |
| 144 |
aliases = ['ada', 'ada83', 'ada95', 'ada2005', 'ada2012'] |
| 145 |
filenames = ['*.adb', '*.ads', '*.ada'] |
| 146 |
mimetypes = ['text/x-ada'] |
| 147 |
|
| 148 |
flags = re.MULTILINE | re.I # Ignore case |
| 149 |
|
| 150 |
tokens = get_lexer_tokens() |
| 151 |
|
| 152 |
|
| 153 |
class TaggedAdaLexer(AdaLexer): |
| 154 |
"""Alternate Pygments lexer for Ada source code with tags. |
| 155 |
|
| 156 |
A tag is a string of the form:: |
| 157 |
|
| 158 |
[MY STRING] |
| 159 |
|
| 160 |
Only alphanumerical characters and spaces are considered inside the |
| 161 |
brackets. |
| 162 |
""" |
| 163 |
|
| 164 |
name = 'TaggedAda' |
| 165 |
aliases = ['tagged_ada'] |
| 166 |
tokens = get_lexer_tokens(True) |
| 167 |
|
| 168 |
|
| 169 |
class GNATProjectLexer(RegexLexer): |
| 170 |
"""Pygment lexer for project files. |
| 171 |
|
| 172 |
This is the same as the AdaLexer but with support of ``project`` |
| 173 |
keyword. |
| 174 |
""" |
| 175 |
name = 'GPR' |
| 176 |
aliases = ['gpr'] |
| 177 |
filenames = ['*.gpr'] |
| 178 |
mimetypes = ['text/x-gpr'] |
| 179 |
|
| 180 |
flags = re.MULTILINE | re.I # Ignore case |
| 181 |
|
| 182 |
tokens = get_lexer_tokens(project_support=True) |