began new parse_input() based on pcrecpp
This commit is contained in:
parent
a78b3af9f8
commit
095a2009af
1
Makefile
1
Makefile
@ -5,6 +5,7 @@ CXXDEPS := $(CXXOBJS:.o=.dep)
|
||||
CXXFLAGS := -O2
|
||||
DEPS := $(CXXDEPS)
|
||||
OBJS := $(CXXOBJS)
|
||||
LDFLAGS := -lpcrecpp
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
|
@ -1,15 +1,91 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h> /* isspace() */
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <pcrecpp.h>
|
||||
|
||||
#include "parse-input.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace pcrecpp;
|
||||
|
||||
#define SET_ERROR(err, args...) \
|
||||
do { \
|
||||
error = true; \
|
||||
sprintf(errstr, err " at line %d, column %d", ##args, lineno, colno); \
|
||||
} while(0)
|
||||
enum Section{ none, tokens, rules };
|
||||
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,6 @@
|
||||
#include <vector>
|
||||
#include "refptr/refptr.h"
|
||||
|
||||
void parse_input(char * buff, int size);
|
||||
bool parse_input(char * buff, int size);
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user