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,16 +105,17 @@ bool parse_input(char * buff, int size)
cerr << "Unknown section name '" << sn << "'!" << endl; cerr << "Unknown section name '" << sn << "'!" << endl;
return false; return false;
} }
continue;
} }
else
{
switch (section) switch (section)
{ {
case none: case none:
cerr << "Unrecognized input on line " << lineno << endl; cerr << "Unrecognized input on line " << lineno << endl;
return false; return false;
case tokens: case tokens:
if (pcre_exec(token, NULL, line.c_str(), line.size(), 0, 0, if (pcre_exec(token, NULL, line.c_str(), line.size(),
ovector, ovec_size) == 0) 0, 0, ovector, ovec_size) >= 0)
{ {
string name(line, ovector[2], ovector[3] - ovector[2]); string name(line, ovector[2], ovector[3] - ovector[2]);
string definition(line, string definition(line,
@ -112,8 +135,8 @@ bool parse_input(char * buff, int size)
} }
break; break;
case rules: case rules:
if (pcre_exec(rule, NULL, line.c_str(), line.size(), 0, 0, if (pcre_exec(rule, NULL, line.c_str(), line.size(),
ovector, ovec_size) == 0) 0, 0, ovector, ovec_size) >= 0)
{ {
string name(line, ovector[2], ovector[3] - ovector[2]); string name(line, ovector[2], ovector[3] - ovector[2]);
string definition(line, string definition(line,
@ -127,7 +150,10 @@ bool parse_input(char * buff, int size)
} }
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