got parsing working using PCRE; updated testinput.i

This commit is contained in:
Josh Holtrop 2010-04-08 11:15:55 -04:00
parent 42ada6ee17
commit 1ae6878fb1
2 changed files with 78 additions and 52 deletions

View File

@ -43,6 +43,8 @@ bool parse_input(char * buff, int size)
sections["tokens"] = tokens; sections["tokens"] = tokens;
sections["rules"] = rules; sections["rules"] = rules;
Section section = none; Section section = none;
string line;
bool append_line = false;
for (int i = 0; i < sizeof(exprs)/sizeof(exprs[0]); i++) for (int i = 0; i < sizeof(exprs)/sizeof(exprs[0]); i++)
{ {
@ -64,16 +66,36 @@ bool parse_input(char * buff, int size)
{ {
line_length--; line_length--;
} }
string line(input, line_length); if (append_line)
if (pcre_exec(empty, NULL, line.c_str(), line.size(), 0, 0,
ovector, ovec_size) == 0)
continue;
if (pcre_exec(comment, NULL, line.c_str(), line.size(), 0, 0,
ovector, ovec_size) == 0)
continue;
if (pcre_exec(section_name, NULL, line.c_str(), line.size(), 0, 0,
ovector, ovec_size) == 0)
{ {
line += string(input, line_length);
}
else
{
line = string(input, line_length);
}
if (line.size() > 0 && line[line.size()-1] == '\\')
{
line[line.size()-1] = ' ';
append_line = true;
}
else
{
append_line = false;
}
if ( append_line
|| (pcre_exec(empty, NULL, line.c_str(), line.size(),
0, 0, ovector, ovec_size) >= 0)
|| (pcre_exec(comment, NULL, line.c_str(), line.size(),
0, 0, ovector, ovec_size) >= 0)
)
{
/* nothing */;
}
else if (pcre_exec(section_name, NULL, line.c_str(), line.size(),
0, 0, ovector, ovec_size) >= 0)
{
sn = string(input, ovector[2], ovector[3] - ovector[2]);
if (sections.find(sn) != sections.end()) if (sections.find(sn) != sections.end())
{ {
section = sections[sn]; section = sections[sn];
@ -83,51 +105,55 @@ bool parse_input(char * buff, int size)
cerr << "Unknown section name '" << sn << "'!" << endl; cerr << "Unknown section name '" << sn << "'!" << endl;
return false; return false;
} }
continue;
} }
switch (section) else
{ {
case none: switch (section)
cerr << "Unrecognized input on line " << lineno << endl; {
return false; case none:
case tokens: cerr << "Unrecognized input on line " << lineno << endl;
if (pcre_exec(token, NULL, line.c_str(), line.size(), 0, 0, return false;
ovector, ovec_size) == 0) case tokens:
{ if (pcre_exec(token, NULL, line.c_str(), line.size(),
string name(line, ovector[2], ovector[3] - ovector[2]); 0, 0, ovector, ovec_size) >= 0)
string definition(line,
ovector[4], ovector[5] - ovector[4]);
string flags;
if (ovector[6] >= 0 && ovector[7] >= 0)
{ {
flags = string(line, string name(line, ovector[2], ovector[3] - ovector[2]);
ovector[6], ovector[7] - ovector[6]); string definition(line,
ovector[4], ovector[5] - ovector[4]);
string flags;
if (ovector[6] >= 0 && ovector[7] >= 0)
{
flags = string(line,
ovector[6], ovector[7] - ovector[6]);
}
/* TODO: process token */
} }
/* TODO: process token */ else
} {
else cerr << "Unrecognized input on line " << lineno << endl;
{ return false;
cerr << "Unrecognized input on line " << lineno << endl; }
return false; break;
} case rules:
break; if (pcre_exec(rule, NULL, line.c_str(), line.size(),
case rules: 0, 0, ovector, ovec_size) >= 0)
if (pcre_exec(rule, NULL, line.c_str(), line.size(), 0, 0, {
ovector, ovec_size) == 0) string name(line, ovector[2], ovector[3] - ovector[2]);
{ string definition(line,
string name(line, ovector[2], ovector[3] - ovector[2]); ovector[4], ovector[5] - ovector[4]);
string definition(line, /* TODO: process rule */
ovector[4], ovector[5] - ovector[4]); }
/* TODO: process rule */ else
} {
else cerr << "Unrecognized input on line " << lineno << endl;
{ return false;
cerr << "Unrecognized input on line " << lineno << endl; }
return false; break;
} }
break;
} }
input = newline + 1; input = newline + 1;
lineno++; lineno++;
} }
return true;
} }

View File

@ -1,13 +1,13 @@
[tokens] [tokens]
ASSIGN := ":=" ASSIGN :=
DASSIGN := ":==" DASSIGN :==
IDENTIFIER := "[a-zA-Z_][a-zA-Z_0-9]*" IDENTIFIER [a-zA-Z_][a-zA-Z_0-9]*
[rules] [rules]
Assignment := IDENTIFIER ASSIGN Expression Assignment := IDENTIFIER ASSIGN Expression
Expression := IDENTIFIER Expression := IDENTIFIER \
| Assignment | Assignment