<commit_before>#include "Parser.h"

#include <algorithm>

#include "HackCommandParser.h"

using Hasm::Parser;

Parser::Parser(std::istream& input)
    : input(input) {
}

const std::string& Parser::getCommand() const {
  return command;
}

void removeSpaces(std::string& str) {
  str.erase(
      std::remove_if(
          str.begin(),
          str.end(),
          [](char c) { return (c == '\r' || c == '\t' || c == ' ' || c == '\n'); }
      ), str.end()
  );
}

void removeComments(std::string& str) {
  std::size_t pos = str.find("//");

  if (pos != std::string::npos) {
    str.erase(pos, str.size());
  }
}

bool Parser::advance() {
  while (getline(input, command)) {
    lineNumber++;

    removeSpaces(command);
    removeComments(command);

    if (!command.empty()) {
      if (isValidCommand()) {
        return true;
      }
      else {
        std::cerr << "error at line " << lineNumber << std::endl;

        break;
      }
    }
  }

  return false;
}

Hasm::CommandType Parser::getCommandType() const {
  if (command.front() == '@') {
    return Hasm::CommandType::A_COMMAND;
  }

  if (command.front() == '(') {
    return Hasm::CommandType::L_COMMAND;
  }

  return Hasm::CommandType::C_COMMAND;
}

std::string Parser::symbol() const {
  if (getCommandType() == Hasm::CommandType::A_COMMAND) {
    return command.substr(1);
  }

  return command.substr(1, command.size() - 2);
}

std::string Parser::dest() const {
  // C_COMMAND dest=comp;jump
  if (command.find('=') != std::string::npos) {
    return command.substr(0, command.find('='));
  }

  return "";
}

std::string Parser::comp() const {
  // C_COMMAND dest=comp;jump
  if (command.find('=') == std::string::npos) { // dest omitted
    return command.substr(0, command.find(';'));
  }

  if (command.find(';') == std::string::npos) { // jump omitted
    return command.substr(command.find('=') + 1);
  }

  std::string s = command.substr(command.find('=') + 1);

  return s.substr(0, s.find(';'));
}

std::string Parser::jump() const {
  // C_COMMAND dest=comp;jump
  if (command.find(';') != std::string::npos) {
    return command.substr(command.find(';') + 1);
  }

  return "";
}

void Parser::reset() {
  input.clear();
  input.seekg(0);
  command = std::string("");
  lineNumber = 0;
}

bool Parser::isValidCommand() const {
  return isACommand() || isLCommand() || isCCommand();
}

bool Parser::isACommand() const {
  return (command.front() == '@') && (command.size() > 1);
}

bool Parser::isCCommand() const {
  return Hasm::HackCommandParser::isComputationCommand(command);
}

bool Parser::isLCommand() const {
  return Hasm::HackCommandParser::isLabelCommand(command);
}<commit_msg>Update parser load command validation<commit_after>#include "Parser.h"

#include <algorithm>

#include "HackCommandParser.h"

using Hasm::Parser;

Parser::Parser(std::istream& input)
    : input(input) {
}

const std::string& Parser::getCommand() const {
  return command;
}

void removeSpaces(std::string& str) {
  str.erase(
      std::remove_if(
          str.begin(),
          str.end(),
          [](char c) { return (c == '\r' || c == '\t' || c == ' ' || c == '\n'); }
      ), str.end()
  );
}

void removeComments(std::string& str) {
  std::size_t pos = str.find("//");

  if (pos != std::string::npos) {
    str.erase(pos, str.size());
  }
}

bool Parser::advance() {
  while (getline(input, command)) {
    lineNumber++;

    removeSpaces(command);
    removeComments(command);

    if (!command.empty()) {
      if (isValidCommand()) {
        return true;
      }
      else {
        std::cerr << "error at line " << lineNumber << std::endl;

        break;
      }
    }
  }

  return false;
}

Hasm::CommandType Parser::getCommandType() const {
  if (command.front() == '@') {
    return Hasm::CommandType::A_COMMAND;
  }

  if (command.front() == '(') {
    return Hasm::CommandType::L_COMMAND;
  }

  return Hasm::CommandType::C_COMMAND;
}

std::string Parser::symbol() const {
  if (getCommandType() == Hasm::CommandType::A_COMMAND) {
    return command.substr(1);
  }

  return command.substr(1, command.size() - 2);
}

std::string Parser::dest() const {
  // C_COMMAND dest=comp;jump
  if (command.find('=') != std::string::npos) {
    return command.substr(0, command.find('='));
  }

  return "";
}

std::string Parser::comp() const {
  // C_COMMAND dest=comp;jump
  if (command.find('=') == std::string::npos) { // dest omitted
    return command.substr(0, command.find(';'));
  }

  if (command.find(';') == std::string::npos) { // jump omitted
    return command.substr(command.find('=') + 1);
  }

  std::string s = command.substr(command.find('=') + 1);

  return s.substr(0, s.find(';'));
}

std::string Parser::jump() const {
  // C_COMMAND dest=comp;jump
  if (command.find(';') != std::string::npos) {
    return command.substr(command.find(';') + 1);
  }

  return "";
}

void Parser::reset() {
  input.clear();
  input.seekg(0);
  command = std::string("");
  lineNumber = 0;
}

bool Parser::isValidCommand() const {
  return isACommand() || isLCommand() || isCCommand();
}

bool Parser::isACommand() const {
  return Hasm::HackCommandParser::isLoadCommand(command);
}

bool Parser::isCCommand() const {
  return Hasm::HackCommandParser::isComputationCommand(command);
}

bool Parser::isLCommand() const {
  return Hasm::HackCommandParser::isLabelCommand(command);
}
{'fraction_non_alphanumeric': 0.19899572252185233, 'fraction_numerical': 0.004277478147665985, 'mean_word_length': 3.5770212765957448, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 6, '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': '295335', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1896, 'n_words': 360}