<commit_before>// This file may be redistributed and modified under the terms of the
// GNU Lesser General Public License (See COPYING for details).
// Copyright (C) 2002 <NAME> <<EMAIL>>

#include "Bach.h"

//#define DEBUG(a) a;
#define DEBUG(a) ;

namespace Atlas { namespace Codecs {

Bach::Bach(std::iostream& s, Atlas::Bridge* b)
    : m_socket(s)
    , m_bridge(b)
    , m_comma(false)
    , m_stringmode(false)
{
    m_state.push(PARSE_INIT);
}

void Bach::parseInit(char next)
{
    DEBUG(cout << "parseInit" << endl;)

    if (next=='[')
    {
        m_bridge->streamBegin();
        m_state.push(PARSE_STREAM);
    }
}

void Bach::parseStream(char next)
{
    DEBUG(cout << "parseStream" << endl;)

    switch (next)
    {
    case '{':
        m_bridge->streamMessage(m_mapBegin);
        m_state.push(PARSE_MAP);
        break;

    case ']':
        m_bridge->streamEnd();
        break;

    default:
        break;
    }
}

void Bach::parseMap(char next)
{
    DEBUG(cout << "parseMap" << endl;)

    switch (next)
    {
    case '}':
        m_bridge->mapEnd();
        m_state.pop();
        break;

    case ',':
    case ' ':
    case '\t':
        break;

    default:
        if (((next>='a')&&(next<='z'))||
            ((next>='A')&&(next<='Z')))
        {
            m_socket.putback(next);
            m_state.push(PARSE_DATA);
            m_state.push(PARSE_NAME);
        }
        else
        {
            cerr << "parseMap: unexpected character: " << next << endl;
        }
        break;
    }
}

void Bach::parseList(char next)
{
    DEBUG(cout << "parseList" << endl;)

    switch (next)
    {
    case ']':
        m_bridge->listEnd();
        m_state.pop();
        break;

    case '{':
        m_bridge->listItem(m_mapBegin);
        m_state.push(PARSE_MAP);
        break;

    case '[':
        cout << "BEGIN LIST 1" << endl;

        m_bridge->listItem(m_listBegin);
        m_state.push(PARSE_LIST);
        break;

    case ',':
    case ' ':
    case '\t':
        break;

    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '0':
    case '-':
        m_state.push(PARSE_INT);
        m_socket.putback(next);
        break;

    case '\"':
        m_state.push(PARSE_STRING);
        break;

    default:
        cerr << "parseMap: unexpected character: " << next << endl;
        break;
    }
}

void Bach::parseMapBegin(char next)
{
    DEBUG(cout << "parseMapBegin" << endl;)

    cout << "BEGIN MAP" << m_name << endl;

    m_bridge->mapItem(decodeString(m_name), m_mapBegin);
    m_socket.putback(next);
    m_state.pop();
    m_name.erase();
}

void Bach::parseListBegin(char next)
{
    DEBUG(cout << "parseListBegin" << endl;)

    cout << "BEGIN LIST 2" << m_name << endl;

    m_bridge->mapItem(decodeString(m_name), m_listBegin);
    m_socket.putback(next);
    m_state.pop();
    m_name.erase();
}

void Bach::parseInt(char next)
{
    DEBUG(cout << "parseInt" << endl;)

    switch (next)
    {
    case '[':
    case ']':
    case '(':
    case ')':
    case ',':
        m_socket.putback(next);
        m_state.pop();
        if (m_state.top() == PARSE_MAP)
        {
            DEBUG(cout << "Int: " << m_name << ": " << m_data << endl;)

            m_bridge->mapItem(decodeString(m_name), atof(m_data.c_str()));
        }
        else if (m_state.top() == PARSE_LIST)
        {
            DEBUG(cout << "Int: " << m_data << endl;)

            m_bridge->listItem(atof(m_data.c_str()));
        }
        else
        {
            cout << "Bach::parseIntt: Error" << endl;
        }
        m_name.erase();
        m_data.erase();
	break;

    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '-':
    case '+':
    case 'e':
    case 'E':
        m_data += next;
	break;

    case '.':
        m_state.pop();
        m_state.push(PARSE_FLOAT);
        m_data += next;
        break;

    default:
        cout << "parseInt: unexpected character: " << next << endl;
	break;
    }
}

void Bach::parseFloat(char next)
{
    DEBUG(cout << "parseFloat" << endl;)

    switch (next)
    {
    case '[':
    case ']':
    case '(':
    case ')':
    case ',':
        m_socket.putback(next);
        m_state.pop();
        if (m_state.top() == PARSE_MAP)
        {
            DEBUG(cout << "Float: " << m_name << ": " << m_data << endl;)

            m_bridge->mapItem(decodeString(m_name), atof(m_data.c_str()));
        }
        else if (m_state.top() == PARSE_LIST)
        {
            DEBUG(cout << "Float: " << m_data << endl;)

            m_bridge->listItem(atof(m_data.c_str()));
        }
        else
        {
            cout << "Bach::parseFloat: Error" << endl;
        }
        m_name.erase();
        m_data.erase();
	break;

    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':<commit_msg>Fix last bug in parseInt and parseFloat<commit_after>// This file may be redistributed and modified under the terms of the
// GNU Lesser General Public License (See COPYING for details).
// Copyright (C) 2002 <NAME> <<EMAIL>>

#include "Bach.h"

//#define DEBUG(a) a;
#define DEBUG(a) ;

namespace Atlas { namespace Codecs {

Bach::Bach(std::iostream& s, Atlas::Bridge* b)
    : m_socket(s)
    , m_bridge(b)
    , m_comma(false)
    , m_stringmode(false)
{
    m_state.push(PARSE_INIT);
}

void Bach::parseInit(char next)
{
    DEBUG(cout << "parseInit" << endl;)

    if (next=='[')
    {
        m_bridge->streamBegin();
        m_state.push(PARSE_STREAM);
    }
}

void Bach::parseStream(char next)
{
    DEBUG(cout << "parseStream" << endl;)

    switch (next)
    {
    case '{':
        m_bridge->streamMessage(m_mapBegin);
        m_state.push(PARSE_MAP);
        break;

    case ']':
        m_bridge->streamEnd();
        break;

    default:
        break;
    }
}

void Bach::parseMap(char next)
{
    DEBUG(cout << "parseMap" << endl;)

    switch (next)
    {
    case '}':
        m_bridge->mapEnd();
        m_state.pop();
        break;

    case ',':
    case ' ':
    case '\t':
        break;

    default:
        if (((next>='a')&&(next<='z'))||
            ((next>='A')&&(next<='Z')))
        {
            m_socket.putback(next);
            m_state.push(PARSE_DATA);
            m_state.push(PARSE_NAME);
        }
        else
        {
            cerr << "parseMap: unexpected character: " << next << endl;
        }
        break;
    }
}

void Bach::parseList(char next)
{
    DEBUG(cout << "parseList" << endl;)

    switch (next)
    {
    case ']':
        m_bridge->listEnd();
        m_state.pop();
        break;

    case '{':
        m_bridge->listItem(m_mapBegin);
        m_state.push(PARSE_MAP);
        break;

    case '[':
        cout << "BEGIN LIST 1" << endl;

        m_bridge->listItem(m_listBegin);
        m_state.push(PARSE_LIST);
        break;

    case ',':
    case ' ':
    case '\t':
        break;

    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '0':
    case '-':
        m_state.push(PARSE_INT);
        m_socket.putback(next);
        break;

    case '\"':
        m_state.push(PARSE_STRING);
        break;

    default:
        cerr << "parseMap: unexpected character: " << next << endl;
        break;
    }
}

void Bach::parseMapBegin(char next)
{
    DEBUG(cout << "parseMapBegin" << endl;)

    cout << "BEGIN MAP" << m_name << endl;

    m_bridge->mapItem(decodeString(m_name), m_mapBegin);
    m_socket.putback(next);
    m_state.pop();
    m_name.erase();
}

void Bach::parseListBegin(char next)
{
    DEBUG(cout << "parseListBegin" << endl;)

    cout << "BEGIN LIST 2" << m_name << endl;

    m_bridge->mapItem(decodeString(m_name), m_listBegin);
    m_socket.putback(next);
    m_state.pop();
    m_name.erase();
}

void Bach::parseInt(char next)
{
    DEBUG(cout << "parseInt" << endl;)

    switch (next)
    {
    case '[':
    case ']':
    case '{':
    case '}':
    case ',':
        m_socket.putback(next);
        m_state.pop();
        if (m_state.top() == PARSE_MAP)
        {
            DEBUG(cout << "Int: " << m_name << ": " << m_data << endl;)

            m_bridge->mapItem(decodeString(m_name), atof(m_data.c_str()));
        }
        else if (m_state.top() == PARSE_LIST)
        {
            DEBUG(cout << "Int: " << m_data << endl;)

            m_bridge->listItem(atof(m_data.c_str()));
        }
        else
        {
            cout << "Bach::parseIntt: Error" << endl;
        }
        m_name.erase();
        m_data.erase();
	break;

    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '-':
    case '+':
    case 'e':
    case 'E':
        m_data += next;
	break;

    case '.':
        m_state.pop();
        m_state.push(PARSE_FLOAT);
        m_data += next;
        break;

    default:
        cout << "parseInt: unexpected character: " << next << endl;
	break;
    }
}

void Bach::parseFloat(char next)
{
    DEBUG(cout << "parseFloat" << endl;)

    switch (next)
    {
    case '[':
    case ']':
    case '{':
    case '}':
    case ',':
        m_socket.putback(next);
        m_state.pop();
        if (m_state.top() == PARSE_MAP)
        {
            DEBUG(cout << "Float: " << m_name << ": " << m_data << endl;)

            m_bridge->mapItem(decodeString(m_name), atof(m_data.c_str()));
        }
        else if (m_state.top() == PARSE_LIST)
        {
            DEBUG(cout << "Float: " << m_data << endl;)

            m_bridge->listItem(atof(m_data.c_str()));
        }
        else
        {
            cout << "Bach::parseFloat: Error" << endl;
        }
        m_name.erase();
        m_data.erase();
	break;

    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
{'fraction_non_alphanumeric': 0.1721377101681345, 'fraction_numerical': 0.006405124099279423, 'mean_word_length': 1.880657249927933, 'pattern_counts': {'":': 4, '<': 225, '<?xml version=': 0, '>': 39, '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': '7447002', 'n_tokens_mistral': 3620, 'n_tokens_neox': 3556, 'n_words': 691}