<commit_before>//
//  S3DXML.swift
//  Pods
//
//  Created by <NAME> on 10/12/15.
//
//

import Foundation
import Metal
import Ono

public class SpectraDescriptorManager {
    public var library: MTLLibrary // TODO: support multiple libraries?
    //TODO: load mtlEnums
    
    public var xsd: S3DXSD
    public var mtlEnums: [String: S3DMtlEnum] = [:]
    public var vertexFunctions: [String: MTLFunction] = [:]
    public var fragmentFunctions: [String: MTLFunction] = [:]
    public var computeFunctions: [String: MTLFunction] = [:]
    
    public var vertexDescriptors: [String: MTLVertexDescriptor] = [:]
    public var textureDescriptors: [String: MTLTextureDescriptor] = [:]
    public var samplerDescriptors: [String: MTLSamplerDescriptor] = [:]
    public var stencilDescriptors: [String: MTLStencilDescriptor] = [:]
    public var depthStencilDescriptors: [String: MTLDepthStencilDescriptor] = [:]
    public var colorAttachmentDescriptors: [String: MTLRenderPipelineColorAttachmentDescriptor] = [:]
    public var renderPipelineDescriptors: [String: MTLRenderPipelineDescriptor] = [:]
    public var renderPassColorAttachmentDescriptors: [String: MTLRenderPassColorAttachmentDescriptor] = [:]
    public var renderPassDepthAttachmentDescriptors: [String: MTLRenderPassDepthAttachmentDescriptor] = [:]
    public var renderPassStencilAttachmentDescriptors: [String: MTLRenderPassStencilAttachmentDescriptor] = [:]
    public var computePipelineDescriptors: [String: MTLComputePipelineDescriptor] = [:]
    
    public init(library: MTLLibrary) {
        self.library = library
        
        // just parsing enum types from XSD for now
        let xmlData = S3DXSD.readXSD("Spectra3D")
        xsd = S3DXSD(data: xmlData)
        xsd.parseEnumTypes()
        mtlEnums = xsd.enumTypes
    }
}

public class S3DXML {
    public var xml: ONOXMLDocument?
    
    public init(data: NSData) {
        xml = try! ONOXMLDocument(data: data)
    }
    
    public func parse(descriptorManager: SpectraDescriptorManager) -> SpectraDescriptorManager {
        for child in xml!.rootElement.children {
            let elem = child as! ONOXMLElement
            let tag = elem.tag
            let key = elem.valueForAttribute("key") as? String
            
            switch tag {
            case "vertex-function":
                descriptorManager.vertexFunctions[key!] = descriptorManager.vertexFunctions[key!] ?? S3DXMLMTLFunctionNode().parse(descriptorManager, elem: elem)
            case "vertex-descriptor":
                descriptorManager.vertexDescriptors[key!] = descriptorManager.vertexDescriptors[key!] ?? S3DXMLMTLVertexDescriptorNode().parse(descriptorManager, elem: elem)
//            case "texture-descriptor":
//                descriptorManager.textureDescriptors[key!] = descriptorManager.vertexDescriptors[key!] ?? S3DXMLMTLTextureDescriptorNode().parse(descriptorManager, elem: elem)
            case "sampler-descriptor":
                descriptorManager.samplerDescriptors[key!] = descriptorManager.samplerDescriptors[key!] ?? S3DXMLMTLSamplerDescriptorNode().parse(descriptorManager, elem: elem)
            case "stencil-descriptor":
                descriptorManager.stencilDescriptors[key!] = descriptorManager.stencilDescriptors[key!] ?? S3DXMLMTLStencilDescriptorNode().parse(descriptorManager, elem: elem)
            case "depth-stencil-descriptor":
                descriptorManager.depthStencilDescriptors[key!] = descriptorManager.depthStencilDescriptors[key!] ?? S3DXMLMTLDepthStencilDescriptorNode().parse(descriptorManager, elem: elem)
//            case "color-attachment-descriptor":
            default:
                break
            }
        }
        
        return descriptorManager
    }
    
//    public func parseXML(bundle:NSBundle, filename: String) {
//        let xmlData = S3DXML.readXML(bundle, filename: "TestXML")
//        xml.parse(self)
    //    }
    
    public class func readXML(bundle: NSBundle, filename: String, bundleResourceName: String? = nil) -> NSData {
        
        var resourceBundle: NSBundle = bundle<commit_msg>S3DXML: now parsing more elements<commit_after>//
//  S3DXML.swift
//  Pods
//
//  Created by <NAME> on 10/12/15.
//
//

import Foundation
import Metal
import Ono

public class SpectraDescriptorManager {
    public var library: MTLLibrary // TODO: support multiple libraries?
    //TODO: load mtlEnums
    
    public var xsd: S3DXSD
    public var mtlEnums: [String: S3DMtlEnum] = [:]
    public var vertexFunctions: [String: MTLFunction] = [:]
    public var fragmentFunctions: [String: MTLFunction] = [:]
    public var computeFunctions: [String: MTLFunction] = [:]
    
    public var vertexDescriptors: [String: MTLVertexDescriptor] = [:]
    public var textureDescriptors: [String: MTLTextureDescriptor] = [:]
    public var samplerDescriptors: [String: MTLSamplerDescriptor] = [:]
    public var stencilDescriptors: [String: MTLStencilDescriptor] = [:]
    public var depthStencilDescriptors: [String: MTLDepthStencilDescriptor] = [:]
    public var colorAttachmentDescriptors: [String: MTLRenderPipelineColorAttachmentDescriptor] = [:]
    public var renderPipelineDescriptors: [String: MTLRenderPipelineDescriptor] = [:]
    public var renderPassColorAttachmentDescriptors: [String: MTLRenderPassColorAttachmentDescriptor] = [:]
    public var renderPassDepthAttachmentDescriptors: [String: MTLRenderPassDepthAttachmentDescriptor] = [:]
    public var renderPassStencilAttachmentDescriptors: [String: MTLRenderPassStencilAttachmentDescriptor] = [:]
    public var renderPassDescriptors: [String: MTLRenderPassDescriptor] = [:]
    public var computePipelineDescriptors: [String: MTLComputePipelineDescriptor] = [:]
    
    public init(library: MTLLibrary) {
        self.library = library
        
        // just parsing enum types from XSD for now
        let xmlData = S3DXSD.readXSD("Spectra3D")
        xsd = S3DXSD(data: xmlData)
        xsd.parseEnumTypes()
        mtlEnums = xsd.enumTypes
    }
}

public class S3DXML {
    public var xml: ONOXMLDocument?
    
    public init(data: NSData) {
        xml = try! ONOXMLDocument(data: data)
    }
    
    public func parse(descriptorManager: SpectraDescriptorManager) -> SpectraDescriptorManager {
        for child in xml!.rootElement.children {
            let elem = child as! ONOXMLElement
            let tag = elem.tag
            let key = elem.valueForAttribute("key") as? String
            
            switch tag {
            case "vertex-function":
                descriptorManager.vertexFunctions[key!] = descriptorManager.vertexFunctions[key!] ?? S3DXMLMTLFunctionNode().parse(descriptorManager, elem: elem)
            case "vertex-descriptor":
                descriptorManager.vertexDescriptors[key!] = descriptorManager.vertexDescriptors[key!] ?? S3DXMLMTLVertexDescriptorNode().parse(descriptorManager, elem: elem)
            case "texture-descriptor":
                descriptorManager.textureDescriptors[key!] = descriptorManager.textureDescriptors[key!] ?? S3DXMLMTLTextureDescriptorNode().parse(descriptorManager, elem: elem)
            case "sampler-descriptor":
                descriptorManager.samplerDescriptors[key!] = descriptorManager.samplerDescriptors[key!] ?? S3DXMLMTLSamplerDescriptorNode().parse(descriptorManager, elem: elem)
            case "stencil-descriptor":
                descriptorManager.stencilDescriptors[key!] = descriptorManager.stencilDescriptors[key!] ?? S3DXMLMTLStencilDescriptorNode().parse(descriptorManager, elem: elem)
            case "depth-stencil-descriptor":
                descriptorManager.depthStencilDescriptors[key!] = descriptorManager.depthStencilDescriptors[key!] ?? S3DXMLMTLDepthStencilDescriptorNode().parse(descriptorManager, elem: elem)
            case "render-pipeline-color-attachment-descriptor":
                descriptorManager.colorAttachmentDescriptors[key!] = descriptorManager.colorAttachmentDescriptors[key!] ?? S3DXMLMTLColorAttachmentDescriptorNode().parse(descriptorManager, elem: elem)
            case "compute-pipeline-descriptor":
                descriptorManager.computePipelineDescriptors[key!] = descriptorManager.computePipelineDescriptors[key!] ??
                    S3DXMLMTLComputePipelineDescriptorNode().parse(descriptorManager, elem: elem)
            case "render-pipeline-descriptor":
                descriptorManager.renderPipelineDescriptors[key!] = descriptorManager.renderPipelineDescriptors[key!] ??
                    S3DXMLMTLRenderPipelineDescriptorNode().parse(descriptorManager, elem: elem)
            case "render-pass-color-attachment-descriptor":
                descriptorManager.renderPassColorAttachmentDescriptors[key!] = descriptorManager.renderPassColorAttachmentDescriptors[key!] ??
                    S3DXMLMTLRenderPassColorAttachmentDescriptorNode().parse(descriptorManager, elem: elem)
            case "render-pass-depth-attachment-descriptor":
                descriptorManager.renderPassDepthAttachmentDescriptors[key!] = descriptorManager.renderPassDepthAttachmentDescriptors[key!] ??
                    S3DXMLMTLRenderPassDepthAttachmentDescriptorNode().parse(descriptorManager, elem: elem)
            case "render-pass-stencil-attachment-descriptor":
                descriptorManager.renderPassStencilAttachmentDescriptors[key!] = descriptorManager.renderPassStencilAttachmentDescriptors[key!] ??
                    S3DXMLMTLRenderPassStencilAttachmentDescriptorNode().parse(descriptorManager, elem: elem)
            case "render-pass-descriptor":
                descriptorManager.renderPassDescriptors[key!] = descriptorManager.renderPassDescriptors[key!] ??
                    S3DXMLMTLRenderPassDescriptorNode().parse(descriptorManager, elem: elem)
            default:
                break
            }
        }
        
        return descriptorManager
    }
    
//    public func parseXML(bundle:NSBundle, filename: String) {
//        let xmlData = S3DXML.readXML(bundle, filename: "TestXML")
//        xml.parse(self)
    //    }
    
    public class func readXML(bundle: NSBundle, filename: String, bundleResourceName: String? = nil) -> NSData {
        
        var resourceBundle: NSBundle = bundle
{'fraction_non_alphanumeric': 0.0945734920944759, 'fraction_numerical': 0.004684755026351747, 'mean_word_length': 3.367860187553282, 'pattern_counts': {'":': 20, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '5180637', 'n_tokens_mistral': 2750, 'n_tokens_neox': 2759, 'n_words': 537}