from characteristic import Attribute, attributes

from cycy import include


@attributes(
    [
        Attribute(name="includers")
    ],
    apply_with_init=False,
)
class Preprocessor(object):
    def __init__(self, includers=None):
        if includers is None:
            includers = []
        self.includers = includers + _DEFAULT_INCLUDE

    def preprocessed(self, tokens, parser):
        """
        Preprocess a stream of tokens.

        """

        # NOTE: Can't get this to translate as a generator function.
        preprocessed = []
        for token in tokens:
            if token.name == "INCLUDE":
                literal = next(tokens)
                assert isinstance(literal, str) and len(literal) > 2
                name = literal[1:-1]
                for token in self.include(name=name, parser=parser).tokens():
                    preprocessed.append(token)
            else:
                preprocessed.append(token)
        return iter(preprocessed)

    def include(self, name, parser):
        for includer in self.includers:
            try:
                return includer.include(name=name, parser=parser)
            except include.NotFound:
                pass
        raise include.NotFound(path=name, searched=self.includers)


def with_directories(directories):
    return Preprocessor(
        includers=[
            include.DirectoryIncluder(path=directory)
            for directory in directories
        ],
    )


_DEFAULT_INCLUDE = [
    include.DirectoryIncluder(path="/usr/local/include/"),
    include.DirectoryIncluder(path="/usr/include/"),
]

@@ -462,271 +462,146 @@
     
-# NOTE: Can't get this to translate as a generator function.%0A        preprocessed = %5B%5D%0A        for token in tokens:%0A            if token.name == %22INCLUDE%22:%0A                literal = next(tokens)%0A                assert isinstance(literal, str) and len(literal) %3E 2
+for token in tokens:%0A            if token.name == %22INCLUDE%22:%0A                assert False  # XXX: Can't see how to translate the generator
 %0A   
@@ -624,15 +624,26 @@
 e = 
-literal
+next(tokens).value
 %5B1:-
@@ -665,25 +665,35 @@
     
-for token in self
+included = self.environment
 .inc
@@ -722,16 +722,54 @@
 =parser)
+%0A                for token in included
 .tokens(
@@ -795,34 +795,19 @@
     
-preprocessed.append(
+yield 
 token
-)
 %0A   
@@ -841,68 +841,19 @@
     
-preprocessed.append(token)%0A        return iter(preprocessed)
+yield token
 %0A%0A  

Revert "Translate plz."