belr-parse.cc 1.96 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1
#include <iostream>
Simon Morlat's avatar
Simon Morlat committed
2 3 4 5
#include <fstream>
#include <sstream>
#include <chrono>
#include <ctime>
Simon Morlat's avatar
Simon Morlat committed
6
#include <cstring>
7
#include <cstdlib>
Simon Morlat's avatar
Simon Morlat committed
8

Ronan's avatar
Ronan committed
9 10 11
#include "belr/grammarbuilder.h"
#include "belr/abnf.h"

12 13
using namespace belr;
using namespace std;
Simon Morlat's avatar
Simon Morlat committed
14 15

int main(int argc, char *argv[]){
Ronan's avatar
Ronan committed
16
	const char *file=nullptr,*message_file=nullptr;
Simon Morlat's avatar
Simon Morlat committed
17 18 19
	int rules_first=0;
	int i;
	int repeat_count=1;
Simon Morlat's avatar
Simon Morlat committed
20
	if (argc<2){
21 22
		cerr<<argv[0]<< " [--repeat <count>] <grammarfile-to-load> - test an abnf and instanciate the parser"<<endl;
		cerr<<argv[0]<< " [--repeat <count>] <grammarfile-to-load> <input file to parse> <entry rule> [rule1] [rule2]..."<<endl;
Simon Morlat's avatar
Simon Morlat committed
23 24
		return -1;
	}
Simon Morlat's avatar
Simon Morlat committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
	for(i=1;i<argc;++i){
		if (strcmp(argv[i],"--repeat")==0){
			++i;
			if (i<argc){
				repeat_count=atoi(argv[i]);
			}
		}else{
			file=argv[i];
			++i;
			if (i<argc)
				message_file=argv[i];
			break;
		}
	}
	rules_first=i+1;
Ronan's avatar
Ronan committed
40

41
	ABNFGrammarBuilder builder;
Simon Morlat's avatar
Simon Morlat committed
42
	shared_ptr<Grammar> grammar=make_shared<Grammar>(file);
Simon Morlat's avatar
Simon Morlat committed
43
	grammar->include(make_shared<CoreRules>());
Sylvain Berfini's avatar
Sylvain Berfini committed
44
	grammar = builder.createFromAbnfFile(file,grammar);
Ronan's avatar
Ronan committed
45

Simon Morlat's avatar
Simon Morlat committed
46 47
	if (message_file){
		ifstream istr(message_file);
Simon Morlat's avatar
Simon Morlat committed
48
		if (!istr.is_open()){
Simon Morlat's avatar
Simon Morlat committed
49
			cerr<<"Could not open "<<message_file<<endl;
Simon Morlat's avatar
Simon Morlat committed
50 51 52 53 54 55
			return -1;
		}
		stringstream str;
		str<<istr.rdbuf();
		DebugParser parser(grammar);
		list<string> rules;
Simon Morlat's avatar
Simon Morlat committed
56
		for(int i=rules_first; i<argc; ++i){
Simon Morlat's avatar
Simon Morlat committed
57 58 59 60
			rules.push_back(argv[i]);
		}
		parser.setObservedRules(rules);
		size_t parsed;
Simon Morlat's avatar
Simon Morlat committed
61
		shared_ptr<DebugElement> ret;
Simon Morlat's avatar
Simon Morlat committed
62
		auto t_start = std::chrono::high_resolution_clock::now();
Simon Morlat's avatar
Simon Morlat committed
63 64 65
		for(int r=0;r<repeat_count;++r){
			ret=parser.parseInput(argv[rules_first],str.str(),&parsed);
		}
Simon Morlat's avatar
Simon Morlat committed
66 67 68 69
		auto t_end = std::chrono::high_resolution_clock::now();
		if (parsed<str.str().size()){
			cerr<<"Parsing ended prematuraly at pos "<<parsed<<endl;
		}else{
Simon Morlat's avatar
Simon Morlat committed
70
			cout<<"Parsing done in "<<std::chrono::duration<double, std::milli>(t_end-t_start).count()/repeat_count<<" milliseconds"<<endl;
Simon Morlat's avatar
Simon Morlat committed
71 72 73 74 75 76 77 78
		}
		if (ret){
			ret->tostream(0,cout);
		}else{
			cerr<<"Parsing failed."<<endl;
			return -1;
		}
	}
Simon Morlat's avatar
Simon Morlat committed
79 80
	return 0;
};