void ConstraintsTokenizer::parseTerm()

in cli/ctokenizer.cpp [257:400]


void ConstraintsTokenizer::parseTerm( IN OUT CTokenList& tokens )
{
    skipWhiteChars();
    wstring::iterator position = _currentPosition;

    // check whether it's one of the functions
    CFunction *function = getFunction();
    if( nullptr != function )
    {
        CToken* token;
        try
        {
            token = new CToken( function, position );
        }
        catch( ... )
        {
            delete( function );
            throw; 
        }
        tokens.push_back( token );
    }
    
    // if not, parse anything that starts with para_name
    else
    {
        wstring paramName = getParameterName();
        CParameters::iterator found = _model.findParamByName( paramName );
        
        CParameter* param = nullptr;
        if ( found != _model.Parameters.end() )
        {
            param = &*found;
        }

        skipWhiteChars();
        RelationType relationType = getRelationType();

        skipWhiteChars();

        CTerm* term = nullptr;
        switch( relationType )
        {
            case RelationType::In:
            case RelationType::NotIn:
            {
                CValueSet* valueSet = new CValueSet;

                if ( ! isNextSubstring( charArrToStr( TEXT_TokenValueSetOpen )))
                {
                    throw CSyntaxError( SyntaxErrorType::NoValueSetOpen, _currentPosition );
                }
                
                try
                {
                    getValueSet( *valueSet );
                }
                catch( ... )
                {
                    delete( valueSet );
                    throw;
                }

                skipWhiteChars();
                if ( ! isNextSubstring( charArrToStr( TEXT_TokenValueSetClose )))
                {
                    throw CSyntaxError( SyntaxErrorType::NoValueSetClose, _currentPosition );
                }

                // raw text of a term
                wstring rawText;
                rawText.assign( position, _currentPosition );

                try
                {
                    term = new CTerm( param, relationType, TermDataType::ValueSet, valueSet, rawText );
                }
                catch( ... )
                {
                    delete( valueSet );
                    throw;
                }
                break;
            }

            // At this point the relation LIKE is treated as an ordinary relation 
            //   despite the fact it can only have a string as an argument on
            //   the right-side. It will be verified later during parsing.
            default:
            {
                if ( isNextSubstring( charArrToStr( TEXT_TokenParameterNameOpen ), true ))
                {
                    wstring paramName2 = getParameterName();
                    
                    //
                    // look up parameters by their names and return references
                    //
                    CParameter *param2 = nullptr;
                    found = _model.findParamByName( paramName2 );
                    if ( found != _model.Parameters.end() )
                    {
                        param2 = &*found;
                    }

                    wstring rawText;
                    rawText.assign( position, _currentPosition );

                    term = new CTerm( param, relationType, TermDataType::ParameterName, param2, rawText );
                }
                else
                {
                    CValue* value = getValue();

                    // raw text of a term
                    wstring rawText;
                    rawText.assign( position, _currentPosition );

                    try
                    {
                        term = new CTerm( param, relationType, TermDataType::Value, value, rawText );
                    }
                    catch( ... )
                    {
                        delete( value );
                        throw;
                    }
                }
                break;
            }
        }

        // now create token of type 'term'; this token has data
        CToken* token;
        try
        {
            token = new CToken( term, position );
        }
        catch( ... )
        {
            delete( term );
            throw; 
        }
        tokens.push_back( token );
    }
}