converted from pcrecpp to pcre

This commit is contained in:
Josh Holtrop 2010-04-07 20:59:28 -04:00
parent 095a2009af
commit 1907029d56
2 changed files with 44 additions and 20 deletions

View File

@ -5,7 +5,7 @@ CXXDEPS := $(CXXOBJS:.o=.dep)
CXXFLAGS := -O2 CXXFLAGS := -O2
DEPS := $(CXXDEPS) DEPS := $(CXXDEPS)
OBJS := $(CXXOBJS) OBJS := $(CXXOBJS)
LDFLAGS := -lpcrecpp LDFLAGS := -lpcre
all: $(TARGET) all: $(TARGET)

View File

@ -5,30 +5,51 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <map> #include <map>
#include <pcrecpp.h> #include <pcre.h>
#include "parse-input.h" #include "parse-input.h"
using namespace std; using namespace std;
using namespace pcrecpp;
enum Section{ none, tokens, rules }; enum Section{ none, tokens, rules };
bool parse_input(char * buff, int size) bool parse_input(char * buff, int size)
{ {
RE empty("\\s*"); pcre * empty;
RE comment("\\s*#.*"); pcre * comment;
RE section_name("\\s*\\[([^\\]]+?)\\]\\s*"); pcre * section_name;
RE token("\\s*" /* possible leading ws */ pcre * token;
"([a-zA-Z_][a-zA-Z_0-9]*)" /* token name */ pcre * rule;
"\\s+" /* required whitespace */ struct { pcre ** re; const char * pattern; } exprs[] = {
"((?:[^\\\\\\s]|\\\\.)+)" /* token regular expression */ {&empty, "^\\s*$"},
"(?:\\s+\\[([^\\]]+)\\])?" /* optional token flags */ {&comment, "^\\s*#"},
"\\s*"); /* possible trailing ws */ {&section_name, "^\\s*\\[([^\\]]+?)\\]\\s*$"},
RE rule("\\s*(\\S+)\\s*:=(.*)"); {&token, "^\\s*" /* possible leading ws */
"([a-zA-Z_][a-zA-Z_0-9]*)" /* token name */
"\\s+" /* required whitespace */
"((?:[^\\\\\\s]|\\\\.)+)" /* token RE */
"(?:\\s+\\[([^\\]]+)\\])?" /* optional token flags */
"\\s*$"}, /* possible trailing ws */
{&rule, "^\\s*(\\S+)\\s*:=(.*)$"}
};
Section section = none; Section section = none;
for (int i = 0; i < sizeof(exprs)/sizeof(exprs[0]); i++)
{
const char * errptr;
int erroffset;
*exprs[i].re = pcre_compile(exprs[i].pattern, 0,
&errptr, &erroffset, NULL);
if (*exprs[i].re == NULL)
{
cerr << "Error compiling regex '" << exprs[i].pattern <<
"': " << errptr << " at position " << erroffset << endl;
}
}
const int ovec_size = 30;
int ovector[ovec_size];
int lineno = 1; int lineno = 1;
char * newline; char * newline;
char * input = buff; char * input = buff;
@ -45,11 +66,14 @@ bool parse_input(char * buff, int size)
line_length--; line_length--;
} }
string line(input, line_length); string line(input, line_length);
if (empty.FullMatch(line)) if (pcre_exec(empty, NULL, line.c_str(), line.size(), 0, 0,
ovector, ovec_size) == 0)
continue; continue;
if (comment.FullMatch(line)) if (pcre_exec(comment, NULL, line.c_str(), line.size(), 0, 0,
ovector, ovec_size) == 0)
continue; continue;
if (section_name.FullMatch(line, &sn)) if (pcre_exec(section_name, NULL, line.c_str(), line.size(), 0, 0,
ovector, ovec_size) == 0)
{ {
if (sections.find(sn) != sections.end()) if (sections.find(sn) != sections.end())
{ {
@ -68,8 +92,8 @@ bool parse_input(char * buff, int size)
break; break;
case tokens: case tokens:
{ {
string name, definition, flags; if (pcre_exec(token, NULL, line.c_str(), line.size(), 0, 0,
if (token.FullMatch(line, &name, &definition, &flags)) ovector, ovec_size) == 0)
{ {
/* TODO: process token */ /* TODO: process token */
} }
@ -77,8 +101,8 @@ bool parse_input(char * buff, int size)
break; break;
case rules: case rules:
{ {
string name, definition; if (pcre_exec(rule, NULL, line.c_str(), line.size(), 0, 0,
if (rule.FullMatch(line, &name, &definition)) ovector, ovec_size) == 0)
{ {
/* TODO: process rule */ /* TODO: process rule */
} }