<commit_before>'''
Copyright 2010-2011 DIMA Research Group, TU Berlin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 
Created on Feb 25, 2012

@author: <NAME> <<EMAIL>>
'''

import logging
import os
import re

from myriad.compiler.ast import RandomSequenceNode
from myriad.compiler.ast import LiteralArgumentNode
from myriad.compiler.ast import ResolvedFunctionRefArgumentNode
from myriad.compiler.ast import ResolvedFieldRefArgumentNode
from myriad.compiler.ast import ResolvedHydratorRefArgumentNode
from myriad.compiler.ast import StringSetRefArgumentNode
from myriad.compiler.ast import DepthFirstNodeFilter
from myriad.compiler.ast import StringSetNode
from myriad.util.stringutil import StringTransformer

class SourceCompiler(object):
    '''
    classdocs
    '''
    
    BUFFER_SIZE = 512
    
    _args = None
    _srcPath = None
    
    _log = None
    
    _expr_pattern = re.compile('%([\w.]+)%')
    _param_pattern = re.compile('^\${(.+)}$')
    
    def __init__(self, args):
        '''
        Constructor
        '''
        self._args = args
        self._srcPath = "%s/../../src/cpp" % (args.base_path)
        
        self._log = logging.getLogger("source.compiler")
        
    def _argumentCode(self, argumentNode):
        if isinstance(argumentNode, LiteralArgumentNode):
            attributeType = argumentNode.getAttribute("type").strip()
            attributeValue = argumentNode.getAttribute("value").strip()
            
            m = self._expr_pattern.match(attributeValue)
            if (m):
                return 'config.parameter<%s>("%s")' % (attributeType, m.group(1))
            
            m = self._param_pattern.match(attributeValue)
            if (m):
                exprExpandedParams = self._expr_pattern.sub(lambda m: 'config.parameter<Decimal>("%s")' % (m.group(1)), attributeValue)
                return "static_cast<%s>(%s)" % (attributeType, exprExpandedParams[3:-2])
            
            else:
                if type == "String":
                    return '"%s"' % (attributeValue)
                else:
                    return '%s' % (attributeValue)
        
        elif isinstance(argumentNode, ResolvedFunctionRefArgumentNode):
            functionType = argumentNode.getAttribute("type")
            functionName = argumentNode.getAttribute("ref")
            return 'config.func<%s> ("%s")' % (functionType, functionName)
        
        elif isinstance(argumentNode, ResolvedFieldRefArgumentNode):
            typeName = StringTransformer.us2ccAll(argumentNode.getRecordTypeRef().getAttribute("key"))
            fieldAccessMethodName = StringTransformer.us2cc(argumentNode.getFieldRef().getAttribute("name"))
            return '&%s::%s' % (typeName, fieldAccessMethodName)
        
        elif isinstance(argumentNode, ResolvedHydratorRefArgumentNode):
            hydratorVarName = StringTransformer.us2cc(argumentNode.getHydratorRef().getAttribute("key"))
            return '_%s' % (hydratorVarName)
        
        elif isinstance(argumentNode, StringSetRefArgumentNode):
            stringSetKey = StringTransformer.us2cc(argumentNode.getAttribute("ref"))
            return 'config.stringSet("%s")' % (stringSetKey)
        
        else:
            
            return "NULL /* unknown */"
        <commit_msg>Fixed a bug in argument C++ code generation for string-set references<commit_after>'''
Copyright 2010-2011 DIMA Research Group, TU Berlin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 
Created on Feb 25, 2012

@author: <NAME> <<EMAIL>>
'''

import logging
import os
import re

from myriad.compiler.ast import RandomSequenceNode
from myriad.compiler.ast import LiteralArgumentNode
from myriad.compiler.ast import ResolvedFunctionRefArgumentNode
from myriad.compiler.ast import ResolvedFieldRefArgumentNode
from myriad.compiler.ast import ResolvedHydratorRefArgumentNode
from myriad.compiler.ast import StringSetRefArgumentNode
from myriad.compiler.ast import DepthFirstNodeFilter
from myriad.compiler.ast import StringSetNode
from myriad.util.stringutil import StringTransformer

class SourceCompiler(object):
    '''
    classdocs
    '''
    
    BUFFER_SIZE = 512
    
    _args = None
    _srcPath = None
    
    _log = None
    
    _expr_pattern = re.compile('%([\w.]+)%')
    _param_pattern = re.compile('^\${(.+)}$')
    
    def __init__(self, args):
        '''
        Constructor
        '''
        self._args = args
        self._srcPath = "%s/../../src/cpp" % (args.base_path)
        
        self._log = logging.getLogger("source.compiler")
        
    def _argumentCode(self, argumentNode):
        if isinstance(argumentNode, LiteralArgumentNode):
            attributeType = argumentNode.getAttribute("type").strip()
            attributeValue = argumentNode.getAttribute("value").strip()
            
            m = self._expr_pattern.match(attributeValue)
            if (m):
                return 'config.parameter<%s>("%s")' % (attributeType, m.group(1))
            
            m = self._param_pattern.match(attributeValue)
            if (m):
                exprExpandedParams = self._expr_pattern.sub(lambda m: 'config.parameter<Decimal>("%s")' % (m.group(1)), attributeValue)
                return "static_cast<%s>(%s)" % (attributeType, exprExpandedParams[3:-2])
            
            else:
                if type == "String":
                    return '"%s"' % (attributeValue)
                else:
                    return '%s' % (attributeValue)
        
        elif isinstance(argumentNode, ResolvedFunctionRefArgumentNode):
            functionType = argumentNode.getAttribute("type")
            functionName = argumentNode.getAttribute("ref")
            return 'config.func<%s> ("%s")' % (functionType, functionName)
        
        elif isinstance(argumentNode, ResolvedFieldRefArgumentNode):
            typeName = StringTransformer.us2ccAll(argumentNode.getRecordTypeRef().getAttribute("key"))
            fieldAccessMethodName = StringTransformer.us2cc(argumentNode.getFieldRef().getAttribute("name"))
            return '&%s::%s' % (typeName, fieldAccessMethodName)
        
        elif isinstance(argumentNode, ResolvedHydratorRefArgumentNode):
            hydratorVarName = StringTransformer.us2cc(argumentNode.getHydratorRef().getAttribute("key"))
            return '_%s' % (hydratorVarName)
        
        elif isinstance(argumentNode, StringSetRefArgumentNode):
            stringSetKey = argumentNode.getAttribute("ref")
            return 'config.stringSet("%s")' % (stringSetKey)
        
        else:
            
            return "NULL /* unknown */"
        
{'fraction_non_alphanumeric': 0.10161906015532447, 'fraction_numerical': 0.0075029616954060815, 'mean_word_length': 2.755808205635195, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1443659', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1963, 'n_words': 506}