<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
A univeral Python parser combinator library inspirted by Parsec library of Haskell.
'''

__author__ = '<NAME>, <EMAIL>'

import re
from functools import wraps
from collections import namedtuple

##########################################################################
## Text.Parsec.Error
##########################################################################

class ParseError(RuntimeError):
    '''Parser error.'''
    def __init__(self, expected, text, index):
        super().__init__()
        self.expected = expected
        self.text = text
        self.index = index

    @staticmethod
    def loc_info(text, index):
        '''Location of `index` in source code `text`.'''
        if index > len(text):
            raise ValueError('Invalid index.')
        line, last_ln = text.count('\n', 0, index), text.rfind('\n', 0, index)
        col = index - (last_ln+1)
        return (line, col)

    def loc(self):
        '''Locate the error position in the source code text.'''
        try:
            return '{}:{}'.format(*ParseError.loc_info(self.text, self.index))
        except ValueError:
            return '<out of bounds index {!r}>'.format(self.index)

    def __str__(self):
        return 'excepted {} at {}'.format(self.expected, self.loc())

##########################################################################
## Defination the Value model of parsec.py.
##########################################################################

class Value(namedtuple('Value', 'status index value expected')):
    '''Represent the result of the Parser.'''
    @staticmethod
    def success(index, actual):
        '''Create success value.'''
        return Value(True, index, actual, None)

    @staticmethod
    def failure(index, expected):
        '''Create failure value.'''
        return Value(False, index, None, expected)

    def aggregate(self, other=None):
        '''collect the furthest failure from self and other.'''<commit_msg>fix typo ('excepted' vs. 'expected')<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
A univeral Python parser combinator library inspirted by Parsec library of Haskell.
'''

__author__ = '<NAME>, <EMAIL>'

import re
from functools import wraps
from collections import namedtuple

##########################################################################
## Text.Parsec.Error
##########################################################################

class ParseError(RuntimeError):
    '''Parser error.'''
    def __init__(self, expected, text, index):
        super().__init__()
        self.expected = expected
        self.text = text
        self.index = index

    @staticmethod
    def loc_info(text, index):
        '''Location of `index` in source code `text`.'''
        if index > len(text):
            raise ValueError('Invalid index.')
        line, last_ln = text.count('\n', 0, index), text.rfind('\n', 0, index)
        col = index - (last_ln+1)
        return (line, col)

    def loc(self):
        '''Locate the error position in the source code text.'''
        try:
            return '{}:{}'.format(*ParseError.loc_info(self.text, self.index))
        except ValueError:
            return '<out of bounds index {!r}>'.format(self.index)

    def __str__(self):
        return 'expected {} at {}'.format(self.expected, self.loc())

##########################################################################
## Defination the Value model of parsec.py.
##########################################################################

class Value(namedtuple('Value', 'status index value expected')):
    '''Represent the result of the Parser.'''
    @staticmethod
    def success(index, actual):
        '''Create success value.'''
        return Value(True, index, actual, None)

    @staticmethod
    def failure(index, expected):
        '''Create failure value.'''
        return Value(False, index, None, expected)

    def aggregate(self, other=None):
        '''collect the furthest failure from self and other.'''
{'fraction_non_alphanumeric': 0.2637766348273328, 'fraction_numerical': 0.0019593436198873378, 'mean_word_length': 3.4295010845986984, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4516869', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1036, 'n_words': 323}