began new parse_input() based on pcrecpp

This commit is contained in:
Josh Holtrop 2010-04-07 11:57:46 -04:00
parent a78b3af9f8
commit 095a2009af
3 changed files with 85 additions and 8 deletions

View File

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

View File

@ -1,15 +1,91 @@
#include <stdio.h> #include <stdio.h>
#include <ctype.h> /* isspace() */ #include <string.h>
#include <iostream>
#include <string>
#include <map>
#include <pcrecpp.h>
#include "parse-input.h" #include "parse-input.h"
using namespace std; using namespace std;
using namespace pcrecpp;
#define SET_ERROR(err, args...) \ enum Section{ none, tokens, rules };
do { \
error = true; \
sprintf(errstr, err " at line %d, column %d", ##args, lineno, colno); \
} while(0)
void parse_input(char * buff, int size) bool parse_input(char * buff, int size)
{ {
RE empty("\\s*");
RE comment("\\s*#.*");
RE section_name("\\s*\\[([^\\]]+?)\\]\\s*");
RE token("\\s*" /* possible leading ws */
"([a-zA-Z_][a-zA-Z_0-9]*)" /* token name */
"\\s+" /* required whitespace */
"((?:[^\\\\\\s]|\\\\.)+)" /* token regular expression */
"(?:\\s+\\[([^\\]]+)\\])?" /* optional token flags */
"\\s*"); /* possible trailing ws */
RE rule("\\s*(\\S+)\\s*:=(.*)");
Section section = none;
int lineno = 1;
char * newline;
char * input = buff;
string sn;
map<string, Section> sections;
sections["none"] = none;
sections["tokens"] = tokens;
sections["rules"] = rules;
while ((newline = strstr(input, "\n")) != NULL)
{
int line_length = newline - input;
if (newline[-1] == '\r')
{
line_length--;
}
string line(input, line_length);
if (empty.FullMatch(line))
continue;
if (comment.FullMatch(line))
continue;
if (section_name.FullMatch(line, &sn))
{
if (sections.find(sn) != sections.end())
{
section = sections[sn];
}
else
{
cerr << "Unknown section name '" << sn << "'!" << endl;
return false;
}
continue;
}
switch (section)
{
case none:
break;
case tokens:
{
string name, definition, flags;
if (token.FullMatch(line, &name, &definition, &flags))
{
/* TODO: process token */
}
}
break;
case rules:
{
string name, definition;
if (rule.FullMatch(line, &name, &definition))
{
/* TODO: process rule */
}
}
break;
}
input = newline + 1;
lineno++;
}
} }

View File

@ -5,6 +5,6 @@
#include <vector> #include <vector>
#include "refptr/refptr.h" #include "refptr/refptr.h"
void parse_input(char * buff, int size); bool parse_input(char * buff, int size);
#endif #endif