File: settings.py

package info (click to toggle)
sphinx-a4doc 1.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: python: 9,560; makefile: 15
file content (502 lines) | stat: -rw-r--r-- 12,299 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
from dataclasses import dataclass, field
from enum import Enum

from sphinx_a4doc.contrib.configurator import Namespace

from typing import *


class InternalAlignment(Enum):
    """
    Controls how to align nodes within a single railroad.
    See `DiagramSettings.internal_alignment` for documentation on elements.

    """

    CENTER = 'CENTER'
    LEFT = 'LEFT'
    RIGHT = 'RIGHT'
    AUTO_LEFT = 'AUTO_LEFT'
    AUTO_RIGHT = 'AUTO_RIGHT'


class EndClass(Enum):
    """
    Controls how diagram start and end look like.
    See `DiagramSettings.end_class` for documentation on elements.

    """

    SIMPLE = 'SIMPLE'
    COMPLEX = 'COMPLEX'


class GrammarType(Enum):
    """
    Antlr4 grammar types.

    """

    MIXED = 'MIXED'
    LEXER = 'LEXER'
    PARSER = 'PARSER'


class OrderSettings(Enum):
    """
    Controls how autodoc orders rules that are extracted from sources.

    """

    BY_SOURCE = 'BY_SOURCE'
    """
    Order by position in source file.

    """

    BY_NAME = 'BY_NAME'
    """
    Order by human-readable name.

    """


class GroupingSettings(Enum):
    """
    Controls how autodoc groups rules that are extracted from sources.

    """

    MIXED = 'MIXED'
    """
    Rules are not ordered.

    """

    LEXER_FIRST = 'LEXER_FIRST'
    """
    Lexer rules go first.

    """

    PARSER_FIRST = 'PARSER_FIRST'
    """
    Parser rules go first.

    """


class LiteralRendering(Enum):
    """
    Controls how literal rules are rendered.

    """

    NAME = 'NAME'
    """
    Name of the rule is displayed.

    """

    CONTENTS = 'CONTENTS'
    """
    Contents of the rule are displayed.

    """

    CONTENTS_UNQUOTED = 'CONTENTS_UNQUOTED'
    """
    Contents of the rule are displayed, single quotes are stripped away.

    """


@dataclass(frozen=True)
class DiagramSettings:
    """
    Settings for diagram directive.

    """

    padding: Tuple[int, int, int, int] = (1, 1, 1, 1)
    """
    Array of four positive integers denoting top, right, bottom and left
    padding between the diagram and its container. By default, there is 1px
    of padding on each side.

    """

    vertical_separation: int = 8
    """
    Vertical space between diagram lines.

    """

    horizontal_separation: int = 10
    """
    Horizontal space between items within a sequence.

    """

    arc_radius: int = 10
    """
    Arc radius of railroads. 10px by default.

    """

    translate_half_pixel: bool = False
    """
    If enabled, the diagram will be translated half-pixel in both directions.
    May be used to deal with anti-aliasing issues when using odd stroke widths.

    """

    internal_alignment: InternalAlignment = InternalAlignment.AUTO_LEFT
    """
    Determines how nodes aligned within a single diagram line. Available
    options are:

    - ``center`` -- nodes are centered.

      .. parser-rule-diagram:: (A B | C D E) (',' (A B | C D E))*
         :internal-alignment: CENTER

    - ``left`` -- nodes are flushed to left in all cases.

      .. parser-rule-diagram:: (A B | C D E) (',' (A B | C D E))*
         :internal-alignment: LEFT

    - ``right`` -- nodes are flushed to right in all cases.

      .. parser-rule-diagram:: (A B | C D E) (',' (A B | C D E))*
         :internal-alignment: RIGHT

    - ``auto_left`` -- nodes in choice groups are flushed left,
      all other nodes are centered.

      .. parser-rule-diagram:: (A B | C D E) (',' (A B | C D E))*
         :internal-alignment: AUTO_LEFT

    - ``auto_right`` -- nodes in choice groups are flushed right,
      all other nodes are centered.

      .. parser-rule-diagram:: (A B | C D E) (',' (A B | C D E))*
         :internal-alignment: AUTO_RIGHT

    """

    character_advance: float = 8.4
    """
    Average length of one character in the used font. Since SVG elements
    cannot expand and shrink dynamically, length of text nodes is calculated
    as number of symbols multiplied by this constant.

    """

    end_class: EndClass = EndClass.SIMPLE
    """
    Controls how diagram start and end look like. Available options are:

    - ``simple`` -- a simple ``T``-shaped ending.

      .. parser-rule-diagram:: X
         :end-class: SIMPLE

    - ``complex`` -- a ``T``-shaped ending with vertical line doubled.

      .. parser-rule-diagram:: X
         :end-class: COMPLEX

    """

    max_width: int = 500
    """
    Max width after which a sequence will be wrapped. This option is used to
    automatically convert sequences to stacks. Note that this is a suggestive
    option, there is no guarantee that the diagram will
    fit to its ``max_width``.

    """

    literal_rendering: LiteralRendering = LiteralRendering.CONTENTS_UNQUOTED
    """
    Controls how literal rules (i.e. lexer rules that only consist of one
    string) are rendered. Available options are:
    
    - ``name`` -- only name of the literal rule is displayed.
    
    - ``contents`` -- quoted literal string is displayed.
    
      .. parser-rule-diagram:: 'def' Id
         :literal-rendering: contents
    
    - ``contents-unquoted``: -- literal string is displayed, quotes stripped
      away.
      

      .. parser-rule-diagram:: 'def' Id
         :literal-rendering: contents-unquoted
    
    """

    cc_to_dash: bool = False
    """
    If rule have no human-readable name set, convert its name from
    ``CamelCase`` to ``dash-case``.
    
    """

    alt: Optional[str] = None
    """
    If rendering engine does not support output of contents, specified
    string is used alternatively.
    """


@dataclass(frozen=True)
class GrammarSettings:
    """
    Settings for grammar directive.

    """

    name: Optional[str] = field(default=None, metadata=dict(no_global=True))
    """
    Specifies a human-readable name for the grammar.

    If given, the human-readable name will be rendered instead of the primary
    grammar name. It will also replace the primary name in all cross references.

    For example this code:

    .. code-block:: rst

       .. a4:grammar:: PrimaryName
          :name: Human-readable name

    will render the next grammar description:

    .. highlights::

       .. a4:grammar:: PrimaryName
          :noindex:
          :name: Human-readable name

    """

    type: GrammarType = field(default=GrammarType.MIXED, metadata=dict(no_global=True))
    """
    Specifies a grammar type. The type will be displayed in the grammar
    signature.

    For example these three grammars:

    .. code-block:: rst

       .. a4:grammar:: Grammar1

       .. a4:grammar:: Grammar2
          :type: lexer

       .. a4:grammar:: Grammar3
          :type: parser

    will be rendered differently:

    .. highlights::

       .. a4:grammar:: Grammar1
          :noindex:

       .. a4:grammar:: Grammar2
          :noindex:
          :type: lexer

       .. a4:grammar:: Grammar3
          :noindex:
          :type: parser

    """

    imports: List[str] = field(default_factory=list, metadata=dict(no_global=True))
    """
    Specifies a list of imported grammars.

    This option affects name resolution process for rule cross-references.
    That is, if there is a reference to ``grammar.rule`` and there is no
    ``rule`` found in the ``grammar``, the imported grammars will be searched
    as well.
    
    Note that this setting is not passed through intersphinx.

    """


@dataclass(frozen=True)
class RuleSettings:
    """
    Settings for rule directive.

    """

    name: Optional[str] = field(default=None, metadata=dict(no_global=True))
    """
    Specifies a human-readable name for this rule. Refer to the corresponding
    :rst:opt:`a4:grammar <a4:grammar:name>`'s option for more info.

    """


@dataclass(frozen=True)
class AutogrammarSettings(GrammarSettings):
    """
    Settings for autogrammar directive.

    """

    only_reachable_from: Optional[str] = field(default=None, metadata=dict(no_global=True, rebuild=True))
    """
    If given, autodoc will only render rules that are reachable from this root.
    This is useful to exclude rules from imported grammars that are not used
    by the primary grammar.

    The value should be either name of a rule from the grammar that's being
    documented or a full path which includes grammar name and rule name.

    For example, suppose there's ``Lexer.g4`` and ``Parser.g4``. To filter
    lexer rules that are not used by parser grammar, use:

    .. code-block:: rst

       .. a4:autogrammar:: Parser
          :only-reachable-from: Parser.root

       .. a4:autogrammar:: Lexer
          :only-reachable-from: Parser.root

    """

    mark_root_rule: bool = field(default=True, metadata=dict(rebuild=True))
    """
    If enabled, automatic diagram for the rule that's listed in
    :rst:opt:`only-reachable-from` will use complex line endings
    (see the :rst:opt:`end-class <railroad-diagram:end-class>` option
    of the :rst:dir:`railroad-diagram` directive).

    """

    lexer_rules: bool = field(default=True, metadata=dict(rebuild=True))
    """
    Controls whether lexer rules should appear in documentation.
    Enabled by default.

    """

    parser_rules: bool = field(default=True, metadata=dict(rebuild=True))
    """
    Controls whether parser rules should appear in documentation.
    Enabled by default.

    """

    fragments: bool = field(default=False, metadata=dict(rebuild=True))
    """
    Controls whether fragments should appear in documentation.
    Disabled by default.

    """

    undocumented: bool = field(default=False, metadata=dict(rebuild=True))
    """
    Controls whether undocumented rules should appear in documentation.
    Disabled by default.

    """

    grouping: GroupingSettings = field(default=GroupingSettings.MIXED, metadata=dict(rebuild=True))
    """
    Controls how autodoc groups rules that are extracted from sources.

    - ``mixed`` -- there's one group that contain all rules.

    - ``lexer-first`` -- there are two group: one for parser rules and one for
      lexer rules and fragments. Lexer group goes first.

    - ``parser-first`` -- like ``lexer-first``, but parser group preceeds
      lexer group.

    """

    ordering: OrderSettings = field(default=OrderSettings.BY_SOURCE, metadata=dict(rebuild=True))
    """
    Controls how autodoc orders rules within each group
    (see :rst:opt:`grouping` option).

    - ``by-source`` -- rules are ordered as they appear in the grammar file.

    - ``by-name`` -- rules are ordered lexicographically.

    """

    honor_sections: bool = field(default=True, metadata=dict(rebuild=True))
    """
    If true, render comments that start with a triple slash, treating them
    as paragraphs that placed between rules.

    This setting has no effect unless :rst:opt:`ordering` is ``by-source``.

    .. versionadded:: 1.2.0

    """

    cc_to_dash: bool = False
    """
    For rules without explicit human-readable names, generate ones by converting
    rule name from ``CamelCase`` to ``dash-case``.
    
    Setting this option will also set the ``diagram-cc-to-dash`` option, unless
    the latter is specified explicitly.
    
    """


@dataclass(frozen=True)
class AutoruleSettings(GrammarSettings):
    """
    Settings for autorule directive.

    .. versionadded:: 1.2.0

    """


@dataclass(frozen=True)
class GlobalSettings:
    """
    Global A4Doc settings. Each member of this dataclass will be added
    to the global sphinx settings registry with prefix ``a4_``.

    """

    base_path: str = field(default='.', metadata=dict(rebuild=True))
    """
    Path which autodoc searches for grammar files.

    """


diagram_namespace = Namespace('a4_diagram', DiagramSettings)
grammar_namespace = Namespace('a4_grammar', GrammarSettings)
rule_namespace = Namespace('a4_rule', RuleSettings)
autogrammar_namespace = Namespace('a4_autogrammar', AutogrammarSettings)
autorule_namespace = Namespace('a4_autorule', AutoruleSettings)
global_namespace = Namespace('a4', GlobalSettings)


def register_settings(app):
    diagram_namespace.register_settings(app)
    grammar_namespace.register_settings(app)
    rule_namespace.register_settings(app)
    autogrammar_namespace.register_settings(app)
    autorule_namespace.register_settings(app)
    global_namespace.register_settings(app)