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
|
#include "Piraha.hpp"
#include <fstream>
#include <stdlib.h>
using namespace piraha;
void read_file(const char *file,std::string& buf) {
std::ifstream in;
in.open(file);
while(true) {
int c = in.get();
if(c < 0)
break;
buf += (char)c;
}
in.close();
}
void usage() {
std::cerr << "usage: generic [--perl] grammar input" << std::endl;
exit(2);
}
#define VAR(X) " " << #X << "=" << X
bool newEnd(std::string& in,const char *new_end,std::string& out) {
out = "/dev/null";
int n = in.size();
int i;
for(i=n-1;i>0;i--) {
if(in[i] == '.') {
break;
}
if(in[i] == '/') {
std::cout << VAR(in) << VAR(i) << std::endl;
break;
}
}
if(in[i] != '.') {
std::cout << VAR(in) << VAR(i) << std::endl;
return false;
}
out.clear();
for(int j=0;j<i;j++)
out += in[j];
out += new_end;
return true;
}
int main(int argc,char **argv) {
std::string grammarArg, inputArg;
bool perlFlag = false;
bool oFlag = false;
std::string outFile;
int narg = 0;
for(int n=1;n<argc;n++) {
std::string arg = argv[n];
if(arg == "--perl") {
perlFlag = true;
} else if(arg == "-o") {
outFile = argv[++n];
oFlag = true;
} else if(arg.size()>2 && arg[0]=='-' && arg[1]=='o') {
outFile = arg.substr(2,arg.size());
oFlag = true;
} else if(narg == 0) {
grammarArg = argv[n];
narg++;
} else if(narg == 1) {
inputArg = argv[n];
narg++;
} else {
usage();
}
}
if(!oFlag) {
if(perlFlag) {
newEnd(inputArg,".pm",outFile);
} else {
newEnd(inputArg,".pegout",outFile);
}
}
std::cout << "reading file: " << inputArg << std::endl;
std::string grammar_file, input_file;
read_file(grammarArg.c_str(),grammar_file);
read_file(inputArg.c_str(),input_file);
smart_ptr<Grammar> g = new Grammar();
compileFile(g,grammar_file.c_str());
smart_ptr<Matcher> mg =
new Matcher (g,g->default_rule.c_str(),input_file.c_str());
if(mg->matches()) {
//std::vector<char> vec(4096);
std::ofstream o;
//o.rdbuf()->pubsetbuf(&vec.front(),vec.size());
o.open(outFile.c_str());
std::cout << "writing file: " << outFile << std::endl;
smart_ptr<Group> src_file =
new Group("annot:src_file",inputArg.c_str());
mg->children.push_back(src_file);
if(perlFlag) {
mg->dumpPerl(o);
} else {
mg->dump(o);
}
o.close();
} else {
mg->showError();
return 1;
}
return 0;
}
|