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
|
from collections.abc import Iterable
from sybil import Document, Region
from sybil.evaluators.doctest import DocTestEvaluator
from sybil.parsers.abstract import DocTestStringParser
from .lexers import DirectiveLexer
class DocTestParser:
"""
A :any:`Parser` for :ref:`doctest-parser` examples.
:param optionflags:
:ref:`doctest option flags<option-flags-and-directives>` to use
when evaluating the examples found by this parser.
"""
def __init__(self, optionflags: int = 0) -> None:
self.string_parser = DocTestStringParser(DocTestEvaluator(optionflags))
def __call__(self, document: Document) -> Iterable[Region]:
return self.string_parser(document.text, document.path)
class DocTestDirectiveParser:
"""
A :any:`Parser` for :rst:dir:`doctest` directives.
:param optionflags:
:ref:`doctest option flags<option-flags-and-directives>` to use
when evaluating the examples found by this parser.
"""
def __init__(self, optionflags: int = 0) -> None:
self.lexer = DirectiveLexer(directive='doctest')
self.string_parser = DocTestStringParser(DocTestEvaluator(optionflags))
def __call__(self, document: Document) -> Iterable[Region]:
for lexed in self.lexer(document):
source = lexed.lexemes['source']
for doctest_region in self.string_parser(source, document.path):
doctest_region.adjust(lexed, source)
yield doctest_region
|