1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

parser: next*Buffer -> set*Buffer, added setParse*Buffer2, added PSYC_ prefix to macro

This commit is contained in:
tg(x) 2011-05-03 22:24:50 +02:00
parent 56fdfe2cd7
commit 5bbcda7da0
4 changed files with 70 additions and 42 deletions

View file

@ -139,6 +139,13 @@ inline void psyc_initParseState (psycParseState* state);
*/
inline void psyc_initParseState2 (psycParseState* state, uint8_t flags);
/**
* Sets a new buffer in the parser state struct with data to be parsed.
*/
inline void psyc_setParseBuffer (psycParseState* state, psycString buffer);
inline void psyc_setParseBuffer2 (psycParseState* state, char *buffer, size_t length);
/**
* Initiates the list state struct.
*
@ -146,9 +153,12 @@ inline void psyc_initParseState2 (psycParseState* state, uint8_t flags);
*/
inline void psyc_initParseListState (psycParseListState* state);
inline void psyc_nextParseBuffer (psycParseState* state, psycString newBuf);
/**
* Sets a new buffer in the list parser state struct with data to be parsed.
*/
inline void psyc_setParseListBuffer (psycParseListState* state, psycString buffer);
inline void psyc_nextParseListBuffer (psycParseListState* state, psycString newBuf);
inline void psyc_setParseListBuffer2 (psycParseListState* state, char *buffer, size_t length);
inline size_t psyc_getContentLength (psycParseState* s);
@ -165,12 +175,12 @@ inline size_t psyc_getContentLength (psycParseState* s);
* @param value A pointer to a psycString. It will point to the
* value/body the variable/method and its length will be set accordingly
*/
psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psycString* value);
psycParseRC psyc_parse (psycParseState* state, char* oper, psycString* name, psycString* value);
/**
* List value parser.
*/
psycParseListRC psyc_parseList(psycParseListState* state, psycString *name, psycString* value, psycString* elem);
psycParseListRC psyc_parseList (psycParseListState* state, psycString *name, psycString* value, psycString* elem);
#endif // PSYC_PARSER_H

View file

@ -8,11 +8,11 @@
#include <psyc/lib.h>
#include <psyc/parser.h>
#define ADVANCE_CURSOR_OR_RETURN(ret) \
#define PSYC_ADVANCE_CURSOR_OR_RETURN(ret) \
if (++(state->cursor) >= state->buffer.length) \
{ \
state->cursor = state->startc; \
return ret; \
{ \
state->cursor = state->startc; \
return ret; \
}
inline void psyc_initParseState (psycParseState* state)
@ -29,27 +29,37 @@ inline void psyc_initParseState2 (psycParseState* state, uint8_t flags)
state->part = PSYC_PART_CONTENT;
}
inline void psyc_setParseBuffer (psycParseState* state, psycString buffer)
{
state->buffer = buffer;
state->cursor = 0;
if (state->flags & PSYC_PARSE_START_AT_CONTENT)
{
state->contentLength = buffer.length;
state->contentLengthFound = PSYC_TRUE;
}
}
inline void psyc_setParseBuffer2 (psycParseState* state, char *buffer, size_t length)
{
psyc_setParseBuffer(state, psyc_newString(buffer, length));
}
inline void psyc_initParseListState (psycParseListState* state)
{
memset(state, 0, sizeof(psycParseListState));
}
inline void psyc_nextParseBuffer (psycParseState* state, psycString newBuf)
inline void psyc_setParseListBuffer (psycParseListState* state, psycString buffer)
{
if (state->flags & PSYC_PARSE_START_AT_CONTENT)
{
state->contentLength = newBuf.length;
state->contentLengthFound = PSYC_TRUE;
}
state->buffer = newBuf;
state->buffer = buffer;
state->cursor = 0;
}
inline void psyc_nextParseListBuffer (psycParseListState* state, psycString newBuf)
inline void psyc_setParseListBuffer2 (psycParseListState* state, char *buffer, size_t length)
{
state->buffer = newBuf;
state->cursor = 0;
psyc_setParseListBuffer(state, psyc_newString(buffer, length));
}
inline size_t psyc_getContentLength (psycParseState* s)
@ -80,7 +90,7 @@ inline char isGlyph(uint8_t g)
/**
* Determines if the argument is numeric.
*/
inline char isNumeric(uint8_t c)
inline char isNumeric (uint8_t c)
{
return c >= '0' && c <= '9';
}
@ -88,7 +98,7 @@ inline char isNumeric(uint8_t c)
/**
* Determines if the argument is alphanumeric.
*/
inline char isAlphaNumeric(uint8_t c)
inline char isAlphaNumeric (uint8_t c)
{
return
(c >= 'a' && c <= 'z') ||
@ -100,7 +110,7 @@ inline char isAlphaNumeric(uint8_t c)
* Determines if the argument is a keyword character.
* Keyword characters are: alphanumeric and _
*/
inline char isKwChar(uint8_t c)
inline char isKwChar (uint8_t c)
{
return isAlphaNumeric(c) || c == '_';
}
@ -110,7 +120,7 @@ inline char isKwChar(uint8_t c)
* It should contain one or more keyword characters.
* @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS
*/
inline psycParseRC psyc_parseName(psycParseState* state, psycString* name)
inline psycParseRC psyc_parseName (psycParseState* state, psycString* name)
{
name->ptr = state->buffer.ptr + state->cursor;
name->length = 0;
@ -118,7 +128,7 @@ inline psycParseRC psyc_parseName(psycParseState* state, psycString* name)
while (isKwChar(state->buffer.ptr[state->cursor]))
{
name->length++; // was a valid char, increase length
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
}
return name->length > 0 ? PSYC_PARSE_SUCCESS : PSYC_PARSE_ERROR;
@ -134,7 +144,7 @@ inline psycParseRC psyc_parseName(psycParseState* state, psycString* name)
*
* @return PSYC_PARSE_COMPLETE or PSYC_PARSE_INCOMPLETE
*/
inline psycParseRC psyc_parseBinaryValue(psycParseState* state, psycString* value, size_t* length, size_t* parsed)
inline psycParseRC psyc_parseBinaryValue (psycParseState* state, psycString* value, size_t* length, size_t* parsed)
{
size_t remaining = *length - *parsed;
value->ptr = state->buffer.ptr + state->cursor;
@ -159,10 +169,10 @@ inline psycParseRC psyc_parseBinaryValue(psycParseState* state, psycString* valu
* Parse simple or binary variable.
* @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS
*/
inline psycParseRC psyc_parseModifier(psycParseState* state, char* oper, psycString* name, psycString* value)
inline psycParseRC psyc_parseModifier (psycParseState* state, char* oper, psycString* name, psycString* value)
{
*oper = *(state->buffer.ptr + state->cursor);
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
psycParseRC ret = psyc_parseName(state, name);
if (ret == PSYC_PARSE_ERROR)
@ -179,14 +189,14 @@ inline psycParseRC psyc_parseModifier(psycParseState* state, char* oper, psycStr
// If we're in the content part check if it's a binary var.
if (state->part == PSYC_PART_CONTENT && state->buffer.ptr[state->cursor] == ' ') // binary arg
{ // After SP the length follows.
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
if (isNumeric(state->buffer.ptr[state->cursor]))
{
do
{
length = 10 * length + state->buffer.ptr[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
}
while (isNumeric(state->buffer.ptr[state->cursor]));
state->valueLength = length;
@ -209,13 +219,13 @@ inline psycParseRC psyc_parseModifier(psycParseState* state, char* oper, psycStr
}
else if (state->buffer.ptr[state->cursor] == '\t') // simple arg
{
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
value->ptr = state->buffer.ptr + state->cursor;
while (state->buffer.ptr[state->cursor] != '\n')
{
value->length++;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
}
return PSYC_PARSE_SUCCESS;
@ -263,7 +273,7 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
{
if (state->buffer.ptr[state->cursor] != '\n')
return PSYC_PARSE_ERROR_MOD_NL;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
}
// Each line of the header starts with a glyph,
@ -292,7 +302,7 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
do
{
state->contentLength = 10 * state->contentLength + state->buffer.ptr[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
}
while (isNumeric(state->buffer.ptr[state->cursor]));
}
@ -322,7 +332,7 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
}
state->startc = state->cursor + 1;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
// fall thru
case PSYC_PART_CONTENT:
@ -344,7 +354,7 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
{
if (state->buffer.ptr[state->cursor] != '\n')
return PSYC_PARSE_ERROR_MOD_NL;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
}
// Each line of the header starts with a glyph,
@ -391,7 +401,7 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
state->part = PSYC_PART_DATA;
}
else // otherwise keep it at the beginning of method
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
// fall thru
}
@ -447,7 +457,7 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
}
}
value->length++;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
}
}
@ -530,7 +540,7 @@ psycParseListRC psyc_parseList(psycParseListState* state, psycString *name, psyc
do
{
state->elemLength = 10 * state->elemLength + state->buffer.ptr[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_LIST_INCOMPLETE);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_LIST_INCOMPLETE);
}
while (isNumeric(state->buffer.ptr[state->cursor]));
}

View file

@ -23,11 +23,13 @@ int main(int argc, char **argv)
write(1, buffer, idx);
write(1, ">> PARSE\n", 9);
}
if (routing_only)
psyc_initParseState2(&state, PSYC_PARSE_ROUTING_ONLY);
else
psyc_initParseState(&state);
psyc_nextParseBuffer(&state, psyc_newString(buffer, idx));
psyc_setParseBuffer(&state, psyc_newString(buffer, idx));
// try parsing that now
// while ((ret = psyc_parse(&state, &oper, &name, &value)))
@ -58,8 +60,10 @@ int main(int argc, char **argv)
{
if (verbose)
write(1, ">>> LIST START\n", 15);
psyc_initParseListState(&listState);
psyc_nextParseListBuffer(&listState, value);
psyc_setParseListBuffer(&listState, value);
while ((ret = psyc_parseList(&listState, &name, &value, &elem)))
{
switch (ret)

View file

@ -161,6 +161,7 @@ int main(int argc, char **argv)
psyc_initParseState2(&parsers[newfd], PSYC_PARSE_ROUTING_ONLY);
else
psyc_initParseState(&parsers[newfd]);
memset(&packets[newfd], 0, sizeof(psycPacket));
memset(&routing[newfd], 0, sizeof(psycModifier) * ROUTING_LINES);
memset(&entity[newfd], 0, sizeof(psycModifier) * ENTITY_LINES);
@ -188,7 +189,7 @@ int main(int argc, char **argv)
} else {
// we got some data from a client
parsebuf = recvbuf - contbytes;
psyc_nextParseBuffer(&parsers[i], psyc_newString(parsebuf, contbytes + nbytes));
psyc_setParseBuffer(&parsers[i], psyc_newString(parsebuf, contbytes + nbytes));
contbytes = 0;
oper = 0;
name.length = 0;
@ -198,6 +199,7 @@ int main(int argc, char **argv)
ret = psyc_parse(&parsers[i], &oper, &name, &value);
if (verbose)
printf("# ret = %d\n", ret);
switch (ret) {
case PSYC_PARSE_ROUTING:
assert(packets[i].routing.lines < ROUTING_LINES);
@ -299,8 +301,10 @@ int main(int argc, char **argv)
if (pname->length >= 5 && memcmp(pname->ptr, "_list", 5) == 0) {
if (verbose)
printf("## LIST START\n");
psyc_initParseListState(&listState);
psyc_nextParseListBuffer(&listState, *pvalue);
psyc_setParseListBuffer(&listState, *pvalue);
do {
retl = psyc_parseList(&listState, pname, pvalue, &elem);
switch (retl) {