Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <cstdlib>
#include <libwccl/values/strset.h>
#include <libwccl/parser/Parser.h>
#include <boost/bind.hpp>
#include <antlr/MismatchedTokenException.hpp>
// ----------------------------------------------------------------------------
//#ifdef HAVE_LIBEDIT
#include <histedit.h>
//#endif
/**
* @desc A simple command line tester for testing operators
*/
namespace {
const char* _prompt = "Enter any operator expression: ";
}
void std_read_loop(boost::function<bool (const std::string&)>& line_cb)
{
while (std::cin.good()) {
std::string s;
getline(std::cin, s);
if (line_cb(s)) {
return;
}
}
}
#ifdef HAVE_LIBEDIT
const char* query_prompt(EditLine*) {
return _prompt;
}
void libedit_read_loop(boost::function<bool (const std::string&)>& line_cb)
{
EditLine *el = el_init("wccl-parser", stdin, stdout, stderr);
el_set(el, EL_PROMPT, &query_prompt);
el_set(el, EL_EDITOR, "emacs");
History* myhistory = history_init();
if (myhistory == NULL) {
std::cerr << "EditLine history init error\n";
el_end(el);
std_read_loop(line_cb);
return;
}
HistEvent ev;
history(myhistory, &ev, H_SETSIZE, 1024);
el_set(el, EL_HIST, history, myhistory);
bool more = true;
while (more) {
int count;
const char *line = el_gets(el, &count); // line gets a trailing \n
if (line == NULL || line[0] == 0) {
more = false;
} else {
std::string s(line, strlen(line) - 1);
if (line_cb(s)) {
more = false;
} else {
history(myhistory, &ev, H_ENTER, line);
}
}
}
history_end(myhistory);
el_end(el);
}
#endif
bool process_line(const std::string& line, Parser& parser)
{
if (line.empty() || line == "exit" || line == "quit") {
return true;
} else if (line == "clear" || line == "cls") {
if (system("clear")) {}
return false;
}
boost::shared_ptr<const Wccl::Value> retVal;
boost::shared_ptr<ANTLRParserResultBase> retOp;
boost::shared_ptr<Corpus2::Sentence> sentence;
Wccl::SentenceContext sc(sentence);
try {
retOp = parser.parseAnyOperator(line);
if (retOp) {
Wccl::FunExecContext cx(sc, retOp->variables);
retVal = retOp->get_op_base()->apply_internal(cx);
if (retVal) {
std::cerr << "Parsed expression: " << retVal->to_raw_string()
<< std::endl;
} else {
std::cerr << "Problem while parsing -- "
<< "retVal is NULL!" << std::endl;
}
} else {
std::cerr << "Problem while parsing -- "
<< "parser returned NULL!" << std::endl;
}
} catch (antlr::MismatchedTokenException &e) {
std::cerr << e.getMessage() << std::endl;
} catch (Wccl::InvalidVariableName &e) {
std::cerr << "Wccl::InvalidVariableName " << e.info() << std::endl;
} catch (Wccl::VariableTypeMismatch &e) {
std::cerr << "Wccl::VariableTypeMismatch " << e.info() << std::endl;
} catch (Wccl::WcclError& e) {
std::cerr << "Wccl::WcclError " << e.info() << std::endl;
} catch (PwrNlp::PwrNlpError& e) {
std::cerr << "PwrNlp::PwrNlpError " << e.info() << std::endl;
} catch (antlr::ANTLRException& e) {
std::cerr << "Antlr error " << e.getMessage() << std::endl;
}
return false;
}
int main()
{
Corpus2::Tagset tagset;
Parser parser(tagset);
if (system("clear")) {
//
}
boost::function<bool (const std::string&)> f;
f = boost::bind(&process_line, _1, boost::ref(parser));
#ifdef HAVE_LIBEDIT
libedit_read_loop(f);
#else
std_read_loop(f);
#endif
return 0;
}