summaryrefslogtreecommitdiff
path: root/code/core/regex.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'code/core/regex.cpp')
-rw-r--r--code/core/regex.cpp134
1 files changed, 134 insertions, 0 deletions
diff --git a/code/core/regex.cpp b/code/core/regex.cpp
new file mode 100644
index 0000000..ef98209
--- /dev/null
+++ b/code/core/regex.cpp
@@ -0,0 +1,134 @@
+// file access
+#include <fcntl.h>
+#include <errno.h>
+#include <list>
+#include <boost/xpressive/xpressive_static.hpp>
+#include <boost/xpressive/regex_actions.hpp>
+#include <boost/type_traits/is_same.hpp>
+#include <boost/foreach.hpp>
+
+#include "interface.h"
+
+#include "log.h"
+#include "simulate.h"
+#include "tracepoints.h"
+#include "global.h"
+#include "neuron.h"
+#include "synapse.h"
+#include "spike.h"
+
+#include "regex.h"
+
+// pollute seperate namespace with regex expressions
+namespace tracepoints {
+
+ using namespace boost::xpressive;
+
+ /* parser result variables */
+ std::set<int> intSet;
+ std::set<std::string> stringSet;
+ double targetTimeOffset = 0.0;
+ std::string filename;
+ bool ioDirection; // tue -> write; false -> read
+ bool ioBinary;
+
+ /* parser callback implementations */
+ struct push_impl
+ {
+ typedef void result_type; // Result type, needed for tr1::result_of
+
+ template<typename Set, class Value>
+ void operator()(Set &s, Value const &val) const { s.insert(val); }
+ };
+ function<push_impl>::type const push = {{}};
+
+ /* I/O interface launcher */
+ template<class T>
+ struct start_interface_impl
+ {
+ typedef void result_type; // Result type, needed for tr1::result_of
+
+ void operator() () const {
+ // open src/dst file
+ int fd = open(filename.c_str, write ? (O_WRONLY | O_APPEND | O_CREAT) : (O_RDONLY | O_NONBLOCK));
+ if (fd == -1) DIE("could not open file");
+
+ // execute task
+ if (ioDirection) { // write
+ if (boost::is_same<T, SpikeMUX>::value) {
+ // create a persistent interface to write out spikes as they occur
+ s.spikeOIfList.push_back(new OutputInterface<SpikeMUX>(fd, ioBinary, stringSet, intSet));
+ }else{
+ // create a temporary interface to write out any state
+ OutputInterface<T> oif(fd, ioBinary, stringSet, intSet);
+ oif.pushClass();
+ close(fd);
+ }
+ }else{ // read
+ // create long living input interface (terminates itself when EOF is discovered)
+ FileInputEvent<T>::createInputStream(new InputInterface<T>(fd, ioBinary, stringSet, intSet));
+ }
+
+ // clean up vars for next command
+ ioBinary = false;
+ stringSet.empty();
+ intSet.empty();
+ }
+ };
+
+ function<start_interface_impl<Global> >::type const start_interface_global = {{}};
+ function<start_interface_impl<Neuron> >::type const start_interface_neuron = {{}};
+ function<start_interface_impl<Synapse> >::type const start_interface_synapse = {{}};
+ function<start_interface_impl<Spike> >::type const start_interface_spike = {{}};
+
+ /* regex pattern definitions */
+
+ // character sets
+ cregex Delim = as_xpr(';');
+ cregex VarNameChar = alnum | as_xpr('_');
+
+ // basic elements
+ cregex Time = (+_d >> !('.' >> +_d))[ ref(targetOffseTime)=as<double>(_) ];
+
+ cregex Index = (+_d)[ push(as<int>(_)) ];
+
+ cregex Element = (+varnamechar)[ push(as<std::string>(_)) ];
+
+ cregex Filename = (+varnamechar)[ ref(filename)=<std::string>(_) ];
+
+ cregex IODirWrite = as_xpr('>') [ ref(ioDirection)=true ];
+ cregex IODirRead = as_xpr('>') [ ref(ioDirection)=false ];
+ cregex IODirection = IODirWrite | IODirRead;
+
+ cregex IOBinaryTrue = as_xpr('#') [ ref(ioBinary)=true ];
+ cregex IOBinary = !IOBinaryTrue;
+
+ cregex IOCmd = IOBinary >> IODirection >> Filename;
+
+ // lists
+ cregex ObjectListInner = *space >> (s1= index) >> *space;
+ cregex ObjectList = *space >> '(' >> object_list_inner >> *( delim >> object_list_inner ) >> ')';
+
+ cregex ElementListInner = *space >> (s1= element) >> *space;
+ cregex ElementList = *space >> '{' >> element_list_inner >> *( delim >> element_list_inner ) >> '}';
+
+ // trace commands
+ cregex TraceCmdTail = *space >> !ElementList >> *space >> !ObjectList >> *space >> IOCmd;
+
+ cregex TraceGlobal = (icase( "global" ) >> TraceCmdTail [ start_interface_global() ];
+ cregex TraceNeuron = (icase( "neuron" ) >> TraceCmdTail [ start_interface_neuron() ];
+ cregex TraceSynapse = (icase( "synapse" ) >> TraceCmdTail [ start_interface_synapse() ];
+ cregex TraceSpike = (icase( "spike" ) >> TraceCmdTail [ start_interface_spike() ];
+
+ // whole line
+ // target format what
+ // cregex channel = *space >> +delim >> *space >> ( neuron | synapse | global | spikes );
+ // cregex traceline = time >> *channel >> *( delim | space );
+ cregex traceLine = !icase("proceed") >> +space >> time;
+ cregex line = *space >> *(inputElem | outputElem | traceElem) >> *( delim | space ));
+
+ bool parseRequest(char *str) {
+ return regex_match(str, line);
+ }
+
+};
contact: Jan Huwald // Impressum