Skip to content

Commit

Permalink
Mathcad generator and improved settings interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomilist committed Jun 23, 2022
1 parent 5908acf commit f606255
Show file tree
Hide file tree
Showing 30 changed files with 657 additions and 135 deletions.
1 change: 1 addition & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ target_link_libraries(MconHelpers Settings)

add_library(Settings Settings.cpp)
target_link_libraries(Settings Lexer)
target_link_libraries(Settings ParsingTree)
6 changes: 4 additions & 2 deletions include/CharacterSet.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef __CHARACTERSET_H__
#define __CHARACTERSET_H__

#include "definitions.hpp"

// Standard libraries
#include <iostream>
#include <fstream>
Expand All @@ -14,6 +12,10 @@
#include <locale>
#include <codecvt>

// Definitions and forward declarations
#include "Definitions.hpp"
#include "ForwardDeclarations.hpp"

// JSON header
#include <nlohmann\json.hpp>

Expand Down
3 changes: 1 addition & 2 deletions include/CharacterStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ namespace mcon
return;
}

// Read a string of characters from the clipboard.
// Returns true on error, false on success.
// Read a string of characters from the clipboard
void CharacterStream::ReadFromClipboard()
{
index = 0;
Expand Down
6 changes: 4 additions & 2 deletions include/CharacterStream.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#ifndef __CHARACTERSTREAM_H__
#define __CHARACTERSTREAM_H__

#include "definitions.hpp"

// Standard libraries
#include <string>
#include <iostream>

// Definitions and forward declarations
#include "Definitions.hpp"
#include "ForwardDeclarations.hpp"

// Windows header
#include <windows.h>

Expand Down
28 changes: 28 additions & 0 deletions include/ForwardDeclarations.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef __FORWARDDECLARATIONS_H__
#define __FORWARDDECLARATIONS_H__

namespace mcon
{
class Token;
enum class TokenType;
class CharacterSet;
class CharacterStream;
class Lexer;
class Settings;
enum class Setting;
enum class DecimalSeparator;
enum class InputLanguage;
enum class OutputLanguage;
enum class OutputMode;
class Node;
enum class NodeType;
class ParsingTree;
class Parser;
class Generator;
class MathcadParser;
class MathcadGenerator;
class LatexParser;
class LatexGenerator;
}

#endif // __FORWARDDECLARATIONS_H__
6 changes: 4 additions & 2 deletions include/Lexer.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#ifndef __LEXER_H__
#define __LEXER_H__

#include "definitions.hpp"

// Standard libraries
#include <string>
#include <vector>
#include <set>
#include <memory>
#include <stdexcept>

// Definitions and forward declarations
#include "Definitions.hpp"
#include "ForwardDeclarations.hpp"

// Custom headers
#include "CharacterStream.hpp"
#include "CharacterSet.hpp"
Expand Down
107 changes: 88 additions & 19 deletions include/MconHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
namespace mcon
{
// Wrapper function for SendInput to send an entire unicode string
void SendInputString(String str)
void SendInputString(String a_string)
{
std::vector<INPUT> characters;
INPUT input_base;
input_base.type = INPUT_KEYBOARD;
WORD current_character;
WORD current_character = STR('\0');
WORD previous_character = STR('\0');

// Release modifier keys SHIFT, CONTROL and ALT to avoid conflicts with user-pressed keys
Expand All @@ -35,10 +35,10 @@ namespace mcon
input_base.ki.wVk = 0;

// Send characters one at a time
for (std::size_t i = 0; i < str.length(); i++)
for (std::size_t i = 0; i < a_string.length(); i++)
{
previous_character = current_character;
current_character = str.at(i);
current_character = a_string.at(i);
input_base.ki.wScan = current_character;
characters.push_back(input_base);

Expand All @@ -61,16 +61,50 @@ namespace mcon

return;
}


// Replace the contents of the clipboard with the provided unicode string
void SetClipboardString(String a_string)
{
if (!OpenClipboard(NULL))
{
return;
}

EmptyClipboard();

HGLOBAL memory_buffer = GlobalAlloc(GMEM_MOVEABLE, (wcslen(a_string.c_str()) + 1) * sizeof(WCHAR));

if (memory_buffer == NULL)
{
CloseClipboard();
return;
}

LPTSTR buffer_handle = static_cast<LPTSTR>(GlobalLock(memory_buffer));
memcpy(buffer_handle, a_string.c_str(), (wcslen(a_string.c_str()) + 1) * sizeof(WCHAR));
GlobalUnlock(memory_buffer);

SetClipboardData(CF_UNICODETEXT, memory_buffer);

CloseClipboard();
return;
}

void MathConversionHotkey(std::shared_ptr<Settings> a_settings, std::shared_ptr<std::mutex> a_settings_mutex)
{
auto character_stream = std::make_unique<mcon::CharacterStream>();
auto character_set = std::make_shared<mcon::CharacterSet>();
character_set->LoadFromFolder(".\\resources\\character-sets");
auto lexer = std::make_unique<mcon::Lexer>(std::move(character_stream), character_set);
auto mathcad_parser = std::make_unique<mcon::MathcadParser>(std::move(lexer));
auto latex_generator = std::make_unique<mcon::LatexGenerator>(character_set);
auto parsing_tree = std::make_shared<mcon::ParsingTree>(std::move(mathcad_parser), std::move(latex_generator));
auto lexer = std::make_shared<mcon::Lexer>(std::move(character_stream), character_set);

std::unordered_map<mcon::InputLanguage, std::unique_ptr<mcon::Parser>> parsers;
parsers.insert({mcon::InputLanguage::Mathcad, std::make_unique<mcon::MathcadParser>(lexer)});

std::unordered_map<mcon::OutputLanguage, std::unique_ptr<mcon::Generator>> generators;
generators.insert({mcon::OutputLanguage::Mathcad, std::make_unique<mcon::MathcadGenerator>(character_set)});
generators.insert({mcon::OutputLanguage::Latex, std::make_unique<mcon::LatexGenerator>(character_set)});

auto parsing_tree = std::make_shared<mcon::ParsingTree>(std::move(parsers), std::move(generators));

// Register hotkey ALT + G or print error if it fails to register
if (RegisterHotKey(NULL, MCON_HOTKEY_SEND, MOD_CONTROL | MOD_SHIFT, 'V'))
Expand All @@ -83,16 +117,25 @@ namespace mcon
return;
}

// Poll the message queue for hotkey triggers
// Print the current settings
a_settings->ShowSettings();

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
MSG msg = {0};
#pragma GCC diagnostic push

InputLanguage input_language;
OutputLanguage output_language;
OutputMode output_mode;

// Poll the message queue for hotkey triggers
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
// Hotkeys trigger the WM_HOTKEY message with the hotkey ID in wParam
if (msg.message == WM_HOTKEY)
{
// Read and apply settings
a_settings_mutex->lock();

switch (a_settings->decimal_separator)
Expand All @@ -109,19 +152,45 @@ namespace mcon
}
}

input_language = a_settings->input_language;
output_language = a_settings->output_language;
output_mode = a_settings->output_mode;

a_settings_mutex->unlock();

// Parse and convert math
parsing_tree->Reset();
parsing_tree->parser->lexer->character_stream->ReadFromClipboard();
parsing_tree->parser->lexer->Scan();
parsing_tree->parser->Parse(parsing_tree);
parsing_tree->parser->Clean(parsing_tree->root_node);
parsing_tree->parsers.at(input_language)->lexer->character_stream->ReadFromClipboard();
parsing_tree->parsers.at(input_language)->lexer->Scan();
parsing_tree->parsers.at(input_language)->Parse(parsing_tree);
parsing_tree->parsers.at(input_language)->Clean(parsing_tree->root_node);
parsing_tree->Clean(parsing_tree->root_node);
parsing_tree->generator->Generate(parsing_tree);
parsing_tree->generator->Substitute(parsing_tree);
STRING_OUTPUT << STR("Input: ") << parsing_tree->parser->lexer->character_stream->buffer << std::endl;
STRING_OUTPUT << STR("Output: ") << parsing_tree->output << STR("\n") << std::endl;
mcon::SendInputString(parsing_tree->output);
parsing_tree->generators.at(output_language)->Generate(parsing_tree);
parsing_tree->generators.at(output_language)->Substitute(parsing_tree);

STRING_OUTPUT
<< STR("Input: ")
<< parsing_tree->parsers.at(input_language)->lexer->character_stream->buffer
<< std::endl;
STRING_OUTPUT
<< STR("Output: ")
<< parsing_tree->output
<< STR("\n")
<< std::endl;

switch (output_mode)
{
case OutputMode::Keystrokes:
{
SendInputString(parsing_tree->output);
break;
}
case OutputMode::Clipboard:
{
SetClipboardString(parsing_tree->output);
break;
}
}
}
}

Expand Down
29 changes: 14 additions & 15 deletions include/MconHelpers.hpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
#ifndef __MCONHELPERS_H__
#define __MCONHELPERS_H__

#include "definitions.hpp"

// Standard libraries
#include <string>
#include <iostream>
#include <stdexcept>
#include <memory>
#include <mutex>

// JSON header
#include <nlohmann/json.hpp>

// Definitions and forward declarations
#include "Definitions.hpp"
#include "ForwardDeclarations.hpp"

#ifdef WINDOWS
#include <windows.h>
#endif

// JSON header
#include <nlohmann/json.hpp>

// Custom headers
#include "CharacterStream.hpp"
#include "CharacterSet.hpp"
#include "Lexer.hpp"
#include "Settings.hpp"
#include "parsing/Parser.hpp"
#include "parsing/Generator.hpp"
#include "parsing/mathcad/MathcadParser.hpp"
#include "parsing/mathcad/MathcadGenerator.hpp"
#include "parsing/latex/LatexParser.hpp"
#include "parsing/latex/LatexGenerator.hpp"
#include "Settings.hpp"

// Definitions
#define MCON_HOTKEY_SEND 1

namespace mcon
{
class CharacterSet;
class CharacterStream;
class Token;
class Node;
class Lexer;
class Parser;
class Generator;

void SendInputString(String str);
void SendInputString(String a_string);
void SetClipboardString(String a_string);
void MathConversionHotkey(std::shared_ptr<Settings> a_settings, std::shared_ptr<std::mutex> a_settings_mutex);
void ConfigInput(std::shared_ptr<Settings> a_settings, std::shared_ptr<std::mutex> a_settings_mutex);
}
Expand Down
Loading

0 comments on commit f606255

Please sign in to comment.