Esempio n. 1
0
    def _match(self, stream_in):
        '''
        Pull indent and call the policy and update the global value, 
        then evaluate the contents.
        '''
        # detect a nested call
        key = s_key(stream_in)
        if key in self.__streams:
            self._debug('Avoided left recursive call to Block.')
            return
        self.__streams.add(key)
        try:
            ((tokens, token_stream), _) = s_next(stream_in)
            (indent, _) = s_line(token_stream, True)
            if START not in tokens:
                raise StopIteration
            current = self.__monitor.indent
            policy = self.policy(current, indent)

            generator = And(*self.lines)._match(stream_in)
            while True:
                self.__monitor.push_level(policy)
                try:
                    results = yield generator
                finally:
                    self.__monitor.pop_level()
                yield results
        finally:
            self.__streams.remove(key)
Esempio n. 2
0
 def _match(self, stream_in):
     '''
     Pull indent and call the policy and update the global value, 
     then evaluate the contents.
     '''
     # detect a nested call
     key = s_key(stream_in)
     if key in self.__streams:
         self._debug('Avoided left recursive call to Block.')
         return
     self.__streams.add(key)
     try:
         ((tokens, token_stream), _) = s_next(stream_in)
         (indent, _) = s_line(token_stream, True)
         if START not in tokens:
             raise StopIteration
         current = self.__monitor.indent
         policy = self.policy(current, indent)
         
         generator = And(*self.lines)._match(stream_in)
         while True:
             self.__monitor.push_level(policy)
             try:
                 results = yield generator
             finally:
                 self.__monitor.pop_level()
             yield results
     finally:
         self.__streams.remove(key)
Esempio n. 3
0
 def key(self, cons, other):
     try:
         (tokens, line_stream) = cons.head
         key = s_key(line_stream, other)
     except StopIteration:
         self._debug('Default hash (EOS)')
         tokens = '<EOS>'
         key = HashKey(self.id, other)
     #self._debug(fmt('Hash at {0!r} {1}', tokens, hash(key)))
     return key
Esempio n. 4
0
 def key(self, cons, other):
     try:
         (tokens, line_stream) = cons.head
         key = s_key(line_stream, other)
     except StopIteration:
         self._debug('Default hash (EOS)')
         tokens = '<EOS>'
         key = HashKey(self.id, other)
     #self._debug(fmt('Hash at {0!r} {1}', tokens, hash(key)))
     return key
Esempio n. 5
0
    def _untagged_match(self, stream):
        '''
        Match the stream without trampolining.
        '''
        key = s_key(stream, self.__state)
        if key not in self.__table:
            self.__table[key] = [False, [], self.matcher._match(stream)]
        descriptor = self.__table[key]
        if descriptor[0]:
            raise MemoException('''Left recursion was detected.
You can try .config.auto_memoize() or similar, but it is better to re-write 
the parser to remove left-recursive definitions.''')
        for i in count():
            assert not descriptor[0]
            if i == len(descriptor[1]):
                result = next(descriptor[2].generator)
                descriptor[1].append(result)
            yield descriptor[1][i]
Esempio n. 6
0
    def _untagged_match(self, stream):
        '''
        Match the stream without trampolining.
        '''
        key = s_key(stream, self.__state)
        if key not in self.__table:
            self.__table[key] = [False, [], self.matcher._match(stream)]
        descriptor = self.__table[key]
        if descriptor[0]:
            raise MemoException('''Left recursion was detected.
You can try .config.auto_memoize() or similar, but it is better to re-write 
the parser to remove left-recursive definitions.''')
        for i in count():
            assert not descriptor[0]
            if i == len(descriptor[1]):
                result = next(descriptor[2].generator)
                descriptor[1].append(result)
            yield descriptor[1][i]
Esempio n. 7
0
 def _untagged_match(self, stream):
     '''
     Match the stream without trampolining.
     '''
     key = s_key(stream, self.__state)
     if key not in self.__depth:
         self.__depth[key] = 0
     depth = self.__depth[key]
     if self.curtail(depth, s_len(stream)):
         return
     if (key, depth) not in self.__table:
         self.__table[(key, depth)] = [[], self.matcher._match(stream)]
     descriptor = self.__table[(key, depth)]
     for i in count():
         assert depth == self.__depth[key]
         if i == len(descriptor[0]):
             result = next(descriptor[1].generator)
             descriptor[0].append(result)
         yield descriptor[0][i]
Esempio n. 8
0
 def _untagged_match(self, stream):
     '''
     Match the stream without trampolining.
     '''
     key = s_key(stream, self.__state)
     if key not in self.__depth:
         self.__depth[key] = 0
     depth = self.__depth[key]
     if self.curtail(depth, s_len(stream)):
         return
     if (key, depth) not in self.__table:
         self.__table[(key, depth)] = [[], self.matcher._match(stream)]
     descriptor = self.__table[(key, depth)]
     for i in count():
         assert depth == self.__depth[key]
         if i == len(descriptor[0]):
             result = next(descriptor[1].generator)
             descriptor[0].append(result)
         yield descriptor[0][i]
Esempio n. 9
0
    def _match(self, stream):
        '''
        Attempt to match the stream.
        '''
        key = s_key(stream, self.__state)
        if key not in self.__table:
            self.__table[key] = [False, [], self.matcher._match(stream)]
        descriptor = self.__table[key]
        if descriptor[0]:
            raise MemoException('''Left recursion was detected.
You can try .config.auto_memoize() or similar, but it is better to re-write 
the parser to remove left-recursive definitions.''')
        for i in count():
            assert not descriptor[0]
            if i == len(descriptor[1]):
                try:
                    descriptor[0] = True
                    result = yield descriptor[2]
                finally:
                    descriptor[0] = False
                descriptor[1].append(result)
            yield descriptor[1][i]
Esempio n. 10
0
    def _match(self, stream):
        '''
        Attempt to match the stream.
        '''
        key = s_key(stream, self.__state)
        if key not in self.__table:
            self.__table[key] = [False, [], self.matcher._match(stream)]
        descriptor = self.__table[key]
        if descriptor[0]:
            raise MemoException('''Left recursion was detected.
You can try .config.auto_memoize() or similar, but it is better to re-write 
the parser to remove left-recursive definitions.''')
        for i in count():
            assert not descriptor[0]
            if i == len(descriptor[1]):
                try:
                    descriptor[0] = True
                    result = yield descriptor[2]
                finally:
                    descriptor[0] = False
                descriptor[1].append(result)
            yield descriptor[1][i]
Esempio n. 11
0
 def _match(self, stream):
     '''
     Attempt to match the stream.
     '''
     key = s_key(stream, self.__state)
     if key not in self.__depth:
         self.__depth[key] = 0
     depth = self.__depth[key]
     if self.curtail(depth, s_len(stream)):
         return
     if (key, depth) not in self.__table:
         self.__table[(key, depth)] = [[], self.matcher._match(stream)]
     descriptor = self.__table[(key, depth)]
     for i in count():
         assert depth == self.__depth[key]
         if i == len(descriptor[0]):
             try:
                 self.__depth[key] += 1
                 result = yield descriptor[1]
             finally:
                 self.__depth[key] -= 1
             descriptor[0].append(result)
         yield descriptor[0][i]
Esempio n. 12
0
 def _match(self, stream):
     '''
     Attempt to match the stream.
     '''
     key = s_key(stream, self.__state)
     if key not in self.__depth:
         self.__depth[key] = 0
     depth = self.__depth[key]
     if self.curtail(depth, s_len(stream)):
         return
     if (key, depth) not in self.__table:
         self.__table[(key, depth)] = [[], self.matcher._match(stream)]
     descriptor = self.__table[(key, depth)]
     for i in count():
         assert depth == self.__depth[key]
         if i == len(descriptor[0]):
             try:
                 self.__depth[key] += 1
                 result = yield descriptor[1]
             finally:
                 self.__depth[key] -= 1
             descriptor[0].append(result)
         yield descriptor[0][i]