<commit_before>//
//  state.swift
//  sparsec
//
//  Created by <NAME> on 15/3/9.
//  Copyright (c) 2015年 Dwarf Artisan. All rights reserved.
//

import Foundation

protocol State {
    typealias T
    typealias I
    mutating func next() throws -> T
    var pos : I{get}
    mutating func begin() -> I
    mutating func commit(_: I)
    mutating func rollback(_: I)
}

class BasicState<S:CollectionType>{
    typealias T = S.Generator.Element
    typealias I = S.Index
    var container: S
    
    init(_ container: S) {
        self.container = container
        self._pos = container.startIndex
    }
    var pos : S.Index {
        get {
            return _pos
        }
        set(value){
            _pos = value
        }
    }
    private var tran: S.Index? = nil
    private var _pos: S.Index
}

extension BasicState:State {
    func next() throws -> T {
        if self.pos == self.container.endIndex.successor() {
            throw ParsecError.Eof(pos: self.pos)
        }
        let item = container[self.pos]
        self.pos = self.pos.successor()
        return item
    }
    func begin() -> BasicState.I {
        if self.tran == nil {
            self.tran = self.pos
        }
        return self.pos
    }
    
    func commit(tran: BasicState.I) {
        if self.tran == tran {
            self.tran = nil
        }
    }
    func rollback(tran: I) {
        self._pos = tran
        if self.tran == tran {
            self.tran = nil
        }
    }
}

class LinesState<S:CollectionType where S.Index: IntegerArithmeticType>:<commit_msg>use transaction type but index<commit_after>//
//  state.swift
//  sparsec
//
//  Created by <NAME> on 15/3/9.
//  Copyright (c) 2015年 Dwarf Artisan. All rights reserved.
//

import Foundation

protocol State {
    typealias T
    typealias I
    typealias Trans
    mutating func next() throws -> T
    var pos : I{get}
    mutating func begin() -> I
    mutating func commit(_: I)
    mutating func rollback(_: I)
}

class BasicState<S:CollectionType>{
    typealias T = S.Generator.Element
    typealias I = S.Index
    typealias Trans = S.Index
    var container: S
    
    init(_ container: S) {
        self.container = container
        self._pos = container.startIndex
    }
    var pos : S.Index {
        get {
            return _pos
        }
        set(value){
            _pos = value
        }
    }
    private var tran: S.Index? = nil
    private var _pos: S.Index
}

extension BasicState:State {
    func next() throws -> T {
        if self.pos == self.container.endIndex.successor() {
            throw ParsecError.Eof(pos: self.pos)
        }
        let item = container[self.pos]
        self.pos = self.pos.successor()
        return item
    }
    func begin() -> BasicState.Trans {
        if self.tran == nil {
            self.tran = self.pos
        }
        return self.pos
    }
    
    func commit(tran: BasicState.Trans) {
        if self.tran == tran {
            self.tran = nil
        }
    }
    func rollback(tran: Trans) {
        self._pos = tran
        if self.tran == tran {
            self.tran = nil
        }
    }
}

class LinesState<S:CollectionType where S.Index: IntegerArithmeticType>:
{'fraction_non_alphanumeric': 0.10477081384471469, 'fraction_numerical': 0.004989086373557842, 'mean_word_length': 1.7140439932318106, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '4909778', 'n_tokens_mistral': 1029, 'n_tokens_neox': 1002, 'n_words': 272}