Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JASP configuration file feature + remote option #5062

Draft
wants to merge 38 commits into
base: stable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6c46eaf
Implements a data mode
JorisGoosen May 21, 2021
003123c
SKF JASP postmerge and fix
JorisGoosen Sep 21, 2021
aa63d26
latest version of jaspProcessControl and other submodules
JorisGoosen Dec 1, 2021
82f7f86
post qt6 merge fixes
JorisGoosen Mar 10, 2022
825d474
post rebase fixes 10-11-22
JorisGoosen Nov 10, 2022
8a49630
post rebase fix 7-12-22
JorisGoosen Dec 7, 2022
3d71dac
(event)=>{}
JorisGoosen Dec 7, 2022
7edb7cd
Rebasing mistake
boutinb Jan 27, 2023
6349af9
Make sure editing is quick and analyses are hidden in dataMode (#5040)
JorisGoosen Mar 1, 2023
191709f
dirty fix for the resize dialog (#5042)
RensDofferhoff Mar 1, 2023
55fea62
odule shenanigans
JorisGoosen Mar 16, 2023
aba724b
start
RensDofferhoff Apr 3, 2023
5f2fb73
progress
RensDofferhoff Apr 4, 2023
bef03ee
progress
RensDofferhoff Apr 5, 2023
a6a1bf2
progress
RensDofferhoff Apr 5, 2023
a44a893
progress
RensDofferhoff Apr 6, 2023
84807bc
progress
RensDofferhoff Apr 7, 2023
f286ea0
before overhaul
RensDofferhoff Apr 10, 2023
b0c3a38
functional
RensDofferhoff Apr 12, 2023
72b965f
giant oops
RensDofferhoff Apr 12, 2023
6945cab
v1
RensDofferhoff Apr 12, 2023
c94ee85
enabled sounds better
RensDofferhoff Apr 13, 2023
53c39e4
oops
RensDofferhoff Apr 15, 2023
b20d6ae
oops
RensDofferhoff Apr 15, 2023
d71aaa2
oops
RensDofferhoff Apr 16, 2023
c826d3d
constant access improvements
RensDofferhoff Apr 18, 2023
2d54356
add option locking
RensDofferhoff Apr 18, 2023
d56c6d3
oops
RensDofferhoff May 11, 2023
0dd5bbf
progress
RensDofferhoff May 12, 2023
3ed5704
factory
RensDofferhoff May 12, 2023
bb506d6
more extendable this way
RensDofferhoff May 15, 2023
8d58dd8
a small comment never hurts
RensDofferhoff May 15, 2023
f431a57
add toml
RensDofferhoff May 16, 2023
8a8df43
forgot the parser lib
RensDofferhoff May 16, 2023
a58c026
Documentation for analysis writers
RensDofferhoff May 17, 2023
8a232b9
Update jasp-qml-guide.md
RensDofferhoff May 22, 2023
c976461
dont change the submodule commits
RensDofferhoff May 22, 2023
311866d
added local conf browser
RensDofferhoff May 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
giant oops
  • Loading branch information
RensDofferhoff committed May 22, 2023
commit 72b965f747c71c9d38b3f3aa4b6a36ab5c0a63df
208 changes: 208 additions & 0 deletions Desktop/gui/jaspConfiguration/jaspConfigurationParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
#include "jaspConfigurationParser.h"
#include "log.h"

#include<iostream>

JASPConfigurationParser* JASPConfigurationParser::_instance = nullptr;

JASPConfigurationParser* JASPConfigurationParser::getInstance()
{
if(!_instance)
_instance = new JASPConfigurationParser();
return _instance;
}

JASPConfigurationParser::JASPConfigurationParser()
{
_parser.set_logger([](size_t line, size_t col, const std::string& msg, const std::string &rule) {
Log::log() << "JASPConfiguration parse error: line" << line << " column:" << col << ": " << msg << "\n";
});

_validGrammar = _parser.load_grammar(_jaspConfigGrammar);
if(!_validGrammar)
Log::log() << "Specified Grammar not valid. Recommend peglint to investigate" << std::endl;

_parser["JASPConf"] = parseJASPConf;
_parser["ModuleStmt"] = parseModuleStmt;
_parser["AnalysisStmt"] = parseAnalysisStmt;
_parser["OptionStmt"] = parseOptionStmt;
_parser["OptionDef"] = parseOptionDef;
_parser["LoadModuleList"] = parseLoadModuleList;
_parser["Version"] = parseVersion;
_parser["KeyValuePair"] = parseKeyValuePair;
_parser["Value"] = parseValue;
_parser["Name"] = parseName;
_parser["StringLiteral"] = parseString;
_parser["Int"] = parseInt;
_parser["Float"] = parseFloat;
_parser["Bool"] = parseBool;
}

std::any JASPConfigurationParser::parseBool(const peg::SemanticValues &vs, std::any &dt)
{
switch (vs.choice()) {
case 0: //true
return true;
default: //false
return false;
}
}

bool JASPConfigurationParser::parse(JASPConfiguration* target, const QString &input)
{
try {
std::any dt = target;
return _parser.parse(input.toStdString(), dt);
}
catch (std::exception& e) {
Log::log() << e.what() << std::endl;
}
return false;
}

std::any JASPConfigurationParser::parseInt(const peg::SemanticValues &vs, std::any &dt)
{
return vs.token_to_number<long long>();
}

std::any JASPConfigurationParser::parseFloat(const peg::SemanticValues &vs, std::any &dt)
{
return vs.token_to_number<double>();
}

std::any JASPConfigurationParser::parseString(const peg::SemanticValues &vs, std::any &dt)
{
return QString(vs.token_to_string().c_str());
}

std::any JASPConfigurationParser::parseName(const peg::SemanticValues &vs, std::any &dt)
{
return QString(vs.token_to_string().c_str());
}

std::any JASPConfigurationParser::parseValue(const peg::SemanticValues &vs, std::any &dt)
{
switch (vs.choice()) {
case 0: //Bool
return QVariant(any_cast<bool>(vs[0]));
case 1: //Float
return QVariant(any_cast<double>(vs[0]));
case 2: //Int
return QVariant(any_cast<long long>(vs[0]));
case 3: //StringLiteral
return QVariant(any_cast<QString>(vs[0]));
case 4: //Name
return QVariant(any_cast<QString>(vs[0]));
default:
return QVariant(any_cast<QString>(vs[0]));
}
}

std::any JASPConfigurationParser::parseKeyValuePair(const peg::SemanticValues &vs, std::any& dt)
{
return KeyValue{any_cast<QString>(vs[0]), any_cast<QVariant>(vs[1])};
}

std::any JASPConfigurationParser::parseVersion(const peg::SemanticValues &vs, std::any &dt)
{
try
{
Version v(vs.token_to_string());
return v;
}
catch (std::runtime_error& e)
{
throw std::runtime_error("Could not parse JASP Version number in configuration file: " + std::string(e.what()));
}
}

std::any JASPConfigurationParser::parseLoadModuleList(const peg::SemanticValues &vs, std::any &dt)
{
QStringList list;
for(int i = 0; i < vs.size(); i++)
list.push_back(any_cast<QString>(vs[i]));
return list;
}

std::any JASPConfigurationParser::parseOptionDef(const peg::SemanticValues &vs, std::any &dt)
{
switch (vs.choice()) {
case 0: //no lock
return Option{any_cast<KeyValue>(vs[0]), false};
default: //lock
return Option{any_cast<KeyValue>(vs[0]), true};
}
}

std::any JASPConfigurationParser::parseOptionStmt(const peg::SemanticValues &vs, std::any &dt)
{
std::vector<Option> options;
for(auto& option : vs)
options.push_back(any_cast<Option>(option));
return options;
}

std::any JASPConfigurationParser::parseAnalysisStmt(const peg::SemanticValues &vs, std::any &dt)
{
Analysis res;
res.name = any_cast<QString>(vs[0]);
for(int i = 1; i < vs.size(); i++)
{
if(vs[i].type() == typeid(std::vector<Option>)) //OptionStmt
res.options.insert(res.options.end(), any_cast<std::vector<Option>>(vs[i]).begin(), any_cast<std::vector<Option>>(vs[i]).end());
else if(vs[i].type() == typeid(KeyValue)) //KeyValuePair
res.constants.push_back(any_cast<KeyValue>(vs[i]));
}
return res;
}

std::any JASPConfigurationParser::parseModuleStmt(const peg::SemanticValues &vs, std::any &dt)
{
Module res;
res.name = any_cast<QString>(vs[0]);
for(int i = 1; i < vs.size(); i++)
{
if(vs[i].type() == typeid(Analysis)) //AnalysisStmt
res.analyses.push_back(any_cast<Analysis>(vs[i]));
else if(vs[i].type() == typeid(KeyValue)) //KeyValuePair
res.constants.push_back(any_cast<KeyValue>(vs[i]));
}
return res;
}

std::any JASPConfigurationParser::parseJASPConf(const peg::SemanticValues &vs, std::any &dt)
{
JASPConfiguration* conf = any_cast<JASPConfiguration*>(dt);
for(int i = 0; i < vs.size(); i++)
{
if(vs[i].type() == typeid(Module)) //ModuleStmt
{
QString moduleName = any_cast<Module>(vs[i]).name; //module constants
for(KeyValue& constant : any_cast<Module>(vs[i]).constants)
conf->addConstant(constant.key, constant.value, moduleName);

for(Analysis& analysis : any_cast<Module>(vs[i]).analyses) //analyses
{
QString analysisName = analysis.name;
for(KeyValue& constant : analysis.constants) //analyses constants
conf->addConstant(constant.key, constant.value, moduleName, analysisName);

for(Option& option : analysis.options) //analyses constants
conf->addOption(option.keyValue.key, option.keyValue.value, option.locked, moduleName, analysisName);
}
}
else if(vs[i].type() == typeid(KeyValue)) //global constants
{
conf->addConstant(any_cast<KeyValue>(vs[i]).key, any_cast<KeyValue>(vs[i]).value);
}
else if(vs[i].type() == typeid(LoadModulesList)) //LoadModulesList
{
conf->_modulesToLoad += any_cast<LoadModulesList>(vs[i]);
}
else if(vs[i].type() == typeid(Version)) //Version
{
conf->_jaspVersion = any_cast<Version>(vs[i]);
}
}
return conf;
}
82 changes: 82 additions & 0 deletions Desktop/gui/jaspConfiguration/jaspConfigurationParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#ifndef JASPCONFIGURATION_PARSER_H
#define JASPCONFIGURATION_PARSER_H

#include "jaspconfiguration.h"
#include "peglib/peglib.h"


class JASPConfigurationParser
{

public:
bool parse(JASPConfiguration* target, const QString& input);

//singleton stuff
static JASPConfigurationParser* getInstance();
JASPConfigurationParser(JASPConfigurationParser& other) = delete;
void operator=(const JASPConfigurationParser&) = delete;

struct KeyValue { QString key; QVariant value; };
struct Option { KeyValue keyValue; bool locked; };
struct Analysis { QString name; std::vector<Option> options; std::vector<KeyValue> constants; };
struct Module { QString name; std::vector<Analysis> analyses; std::vector<KeyValue> constants; };
using LoadModulesList = QStringList;

private:
JASPConfigurationParser();

static JASPConfigurationParser* _instance;

static std::any parseBool(const peg::SemanticValues &vs, std::any& dt);
static std::any parseInt(const peg::SemanticValues &vs, std::any& dt);
static std::any parseFloat(const peg::SemanticValues &vs, std::any& dt);
static std::any parseString(const peg::SemanticValues &vs, std::any& dt);
static std::any parseName(const peg::SemanticValues &vs, std::any& dt);
static std::any parseValue(const peg::SemanticValues &vs, std::any& dt);
static std::any parseKeyValuePair(const peg::SemanticValues &vs, std::any& dt);
static std::any parseVersion(const peg::SemanticValues &vs, std::any& dt);
static std::any parseLoadModuleList(const peg::SemanticValues &vs, std::any& dt);
static std::any parseOptionDef(const peg::SemanticValues &vs, std::any& dt);
static std::any parseOptionStmt(const peg::SemanticValues &vs, std::any& dt);
static std::any parseAnalysisStmt(const peg::SemanticValues &vs, std::any& dt);
static std::any parseModuleStmt(const peg::SemanticValues &vs, std::any& dt);
static std::any parseJASPConf(const peg::SemanticValues &vs, std::any& dt);





peg::parser _parser;
bool _validGrammar;
const char* _jaspConfigGrammar = R"(
JASPConf <- Statement* Version Statement*

Statement <- ModuleStmt / LoadModuleList / KeyValuePair / Comment
ModuleStmt <- 'Module' Name Comment? ( AnalysisStmt / KeyValuePair / Comment )* 'End Module' Comment?
AnalysisStmt <- 'Analysis' Name Comment? ( OptionStmt / KeyValuePair / Comment )* 'End Analysis' Comment?
OptionStmt <- 'Options' Comment? OptionDef* 'End Options' Comment?
OptionDef <- KeyValuePair Comment? / KeyValuePair 'lock'? Comment?

LoadModuleList <- 'Load Modules' ':' List(Name, ',') Comment?
Version <- 'JASP_Version' ':' < [0-9.]* > Comment?
KeyValuePair <- Name '=' Value Comment?

Value <- Bool / Float / Int / StringLiteral / Name
Name <- < [a-zA-Z_][a-zA-Z_0-9]* >
StringLiteral <- '"' < [^"]* > '"'
Digits <- [0-9]+
Int <- < [-+]? Digits >
Float <- < Int '.' Digits > / < Int 'e' Int >
Bool <- ('true' / 'True' / 'TRUE') / ('false' / 'False' / 'FALSE')

Comment <- '#' (!EndOfLine .)* EndOfLine
EndOfLine <- '\r\n' / '\n' / '\r'
%whitespace <- [ \t\n]*

# Macros
List(I, D) ← I (D I)*
)";

};

#endif // JASPCONFIGURATION_PARSER_H
Loading