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

346 lines
8.1 KiB
D
Raw Normal View History

2011-05-09 07:06:52 +00:00
module psyc.parse;
2011-04-30 12:30:01 +00:00
2011-04-30 12:39:04 +00:00
import psyc.common;
2011-04-30 12:30:01 +00:00
extern (C):
enum ParseFlag
{
2011-05-15 19:13:10 +00:00
ALL = 0,
2011-05-08 22:15:03 +00:00
ROUTING_ONLY = 1,
START_AT_CONTENT = 2,
2011-04-30 12:30:01 +00:00
}
/**
* The return value definitions for the packet parsing function.
*
* See_Also:
* parse()
2011-04-30 12:30:01 +00:00
*/
enum ParseRC
{
2011-05-15 22:01:13 +00:00
/// Error, packet is not ending with a valid delimiter.
2011-05-08 22:15:03 +00:00
ERROR_END = -8,
2011-05-15 22:01:13 +00:00
/// Error, expected NL after the method.
2011-05-08 22:15:03 +00:00
ERROR_METHOD = -7,
2011-05-15 22:01:13 +00:00
/// Error, expected NL after a modifier.
2011-05-08 22:15:03 +00:00
ERROR_MOD_NL = -6,
2011-05-15 22:01:13 +00:00
/// Error, modifier length is not numeric.
2011-05-08 22:15:03 +00:00
ERROR_MOD_LEN = -5,
2011-05-15 22:01:13 +00:00
/// Error, expected TAB before modifier value.
2011-05-08 22:15:03 +00:00
ERROR_MOD_TAB = -4,
2011-05-15 22:01:13 +00:00
/// Error, modifier name is missing.
2011-05-08 22:15:03 +00:00
ERROR_MOD_NAME = -3,
2011-05-15 22:01:13 +00:00
/// Error, expected NL after the content length.
2011-05-08 22:15:03 +00:00
ERROR_LENGTH = -2,
2011-05-15 22:01:13 +00:00
/// Error in packet.
2011-05-08 22:15:03 +00:00
ERROR = -1,
2011-05-15 22:01:13 +00:00
/// Buffer contains insufficient amount of data.
/// Fill another buffer and concatenate it with the end of the current buffer,
/// from the cursor position to the end.
2011-05-08 22:15:03 +00:00
INSUFFICIENT = 1,
2011-05-15 22:01:13 +00:00
/// Routing modifier parsing done.
/// Operator, name & value contains the respective parts.
2011-05-08 22:15:03 +00:00
ROUTING = 2,
2011-05-15 22:01:13 +00:00
/// Start of an incomplete entity modifier.
/// Operator & name are complete, value is incomplete.
2011-05-08 22:15:03 +00:00
ENTITY_START = 3,
2011-05-15 22:01:13 +00:00
/// Continuation of an incomplete entity modifier.
2011-05-08 22:15:03 +00:00
ENTITY_CONT = 4,
2011-05-15 22:01:13 +00:00
/// End of an incomplete entity modifier.
2011-05-08 22:15:03 +00:00
ENTITY_END = 5,
2011-05-15 22:01:13 +00:00
/// Entity modifier parsing done in one go.
/// Operator, name & value contains the respective parts.
2011-05-08 22:15:03 +00:00
ENTITY = 6,
2011-05-15 22:01:13 +00:00
/// Start of an incomplete body.
/// Name contains method, value contains part of the body.
2011-05-08 22:15:03 +00:00
BODY_START = 7,
2011-05-15 22:01:13 +00:00
/// Continuation of an incomplete body.
2011-05-08 22:15:03 +00:00
BODY_CONT = 8,
2011-05-15 22:01:13 +00:00
/// End of an incomplete body.
2011-05-08 22:15:03 +00:00
BODY_END = 9,
2011-05-15 22:01:13 +00:00
/// Body parsing done in one go, name contains method, value contains body.
2011-05-08 22:15:03 +00:00
BODY = 10,
2011-05-15 22:01:13 +00:00
/// Start of an incomplete content, value contains part of content.
/// Used when ROUTING_ONLY is set.
2011-05-08 22:15:03 +00:00
CONTENT_START = 7,
2011-05-15 22:01:13 +00:00
/// Continuation of an incomplete body.
/// Used when ROUTING_ONLY is set.
2011-05-08 22:15:03 +00:00
CONTENT_CONT = 8,
2011-05-15 22:01:13 +00:00
/// End of an incomplete body.
/// Used when ROUTING_ONLY is set.
2011-05-08 22:15:03 +00:00
CONTENT_END = 9,
2011-05-15 22:01:13 +00:00
/// Content parsing done in one go, value contains the whole content.
/// Used when ROUTING_ONLY is set.
2011-05-08 22:15:03 +00:00
CONTENT = 10,
2011-05-15 22:01:13 +00:00
/// Finished parsing packet.
COMPLETE = 11
2011-04-30 12:30:01 +00:00
}
/**
* The return value definitions for the list parsing function.
* See_Also: parseList()
2011-04-30 12:30:01 +00:00
*/
enum ParseListRC
{
2011-05-08 22:15:03 +00:00
LIST_ERROR_DELIM = -5,
LIST_ERROR_LEN = -4,
LIST_ERROR_TYPE = -3,
LIST_ERROR_NAME = -2,
LIST_ERROR = -1,
2011-04-30 12:30:01 +00:00
/// Completed parsing a list element.
2011-05-08 22:15:03 +00:00
LIST_ELEM = 1,
2011-04-30 12:30:01 +00:00
/// Reached end of buffer.
2011-05-08 22:15:03 +00:00
LIST_END = 2,
2011-04-30 12:30:01 +00:00
/// Binary list is incomplete.
2011-05-08 22:15:03 +00:00
LIST_INCOMPLETE = 3,
2011-04-30 12:30:01 +00:00
}
/**
* Struct for keeping parser state.
*/
struct ParseState
{
size_t cursor; ///< current position in buffer
size_t startc; ///< position where the parsing would be resumed
String buffer; ///< buffer with data to be parsed
ubyte flags; ///< flags for the parser, see ParseFlag
Part part; ///< part of the packet being parsed currently
size_t routingLength; ///< length of routing part parsed so far
size_t contentParsed; ///< number of bytes parsed from the content so far
size_t contentLength; ///< expected length of the content
Bool contentLengthFound; ///< is there a length given for this packet?
size_t valueParsed; ///< number of bytes parsed from the value so far
size_t valueLength; ///< expected length of the value
2011-05-08 22:15:03 +00:00
Bool valueLengthFound; ///< is there a length given for this modifier?
/**
* Initiates the state struct.
*/
static ParseState opCall ( )
{
ParseState inst;
return inst;
}
/**
* Initializes the state struct with flags.
*
* Params:
* flags = Flags to be set for the parser, see ParseFlag.
2011-05-08 22:15:03 +00:00
*/
2011-05-15 18:36:16 +00:00
static ParseState opCall ( ParseFlag flags )
2011-05-08 22:15:03 +00:00
{
ParseState inst;
inst.flags = flags;
2011-05-15 18:36:16 +00:00
if (flags & ParseFlag.START_AT_CONTENT)
2011-05-08 22:15:03 +00:00
inst.part = Part.CONTENT;
return inst;
}
/**
* Parse PSYC packets.
*
* Generalized line-based packet parser.
*
* Params:
* oper = A reference to a character. In case of a variable, it will
* be set to the operator of that variable
* name = A reference to a String. It will point to the name of
* the variable or method and its length will be set accordingly
* value = A reference to a String. It will point to the
* value/body the variable/method and its length will be set accordingly
*/
ParseRC parse ( ref char oper, ref char[] name, ref char[] value )
{
return psyc_parse(this, &oper, cast(String*) &name, cast(String*) &value);
}
2011-05-15 18:36:16 +00:00
/**
* Change parse flags in state
*
* Params:
* state = Pointer to the state struct that should be initialized.
* flags = Flags to be set for the parser, see psycParseFlag.
*
* See_Also: psyc_initParseState psycParseFlag
*/
void setParseFlags (ParseFlag flags)
{
this.flags = flags;
if (flags & ParseFlag.START_AT_CONTENT)
this.part = Part.CONTENT;
2011-05-15 18:37:14 +00:00
else
this.part = Part.ROUTING;
2011-05-15 18:36:16 +00:00
}
2011-05-08 22:15:03 +00:00
/**
* Sets a new buffer in the parser state struct with data to be parsed.
*
* Params:
* buffer the buffer that should be parsed now
*
* See_Also: String
2011-05-08 22:15:03 +00:00
*/
void setParseBuffer ( String buffer )
{
this.buffer = buffer;
this.cursor = 0;
if (this.flags & ParseFlag.START_AT_CONTENT)
{
this.contentLength = buffer.length;
this.contentLengthFound = Bool.TRUE;
}
}
/**
* Sets a new buffer in the parser state struct with data to be parsed.
*
* Params:
* buffer = the buffer that should be parsed now
*
* See_Also: String
2011-05-08 22:15:03 +00:00
*/
void setParseBuffer ( ubyte[] buffer )
{
this.setParseBuffer(*(cast(String*) &buffer));
}
/**
* Sets a new buffer in the parser state struct with data to be parsed.
*
* Params:
* buffer = pointer to the buffer that should be parsed now
* length = length of the buffer
2011-05-08 22:15:03 +00:00
*/
void setParseBuffer ( ubyte* buffer, size_t length )
{
this.setParseBuffer(String(length, cast(ubyte*)buffer));
}
size_t getContentLength ( )
{
return this.contentLength;
}
bool isContentLengthFound ( )
{
return this.contentLengthFound == Bool.TRUE;
}
size_t getValueLength ( )
{
return this.valueLength;
}
bool isValueLengthFound ( )
{
return this.valueLengthFound == Bool.TRUE;
}
size_t getCursor ( )
{
return this.cursor;
}
size_t getBufferLength ( )
{
return this.buffer.length;
}
size_t getRemainingLength ( )
{
return this.buffer.length - this.cursor;
}
ubyte* getRemainingBuffer ( )
{
return this.buffer.ptr + this.cursor;
}
void getRemainingBuffer ( ref ubyte[] buf )
{
buf = this.buffer.ptr[cursor .. getRemainingLength()];
}
2011-04-30 12:30:01 +00:00
}
/**
* Struct for keeping list parser state.
*/
struct ParseListState
{
size_t cursor; ///< current position in buffer
size_t startc; ///< line start position
String buffer;
ListType type; ///< list type
size_t elemParsed; ///< number of bytes parsed from the elem so far
size_t elemLength; ///< expected length of the elem
2011-05-08 22:15:03 +00:00
/**
* Sets a new buffer with data to be parsed
*
* Params:
* buffer = the buffer to be parsed
2011-05-08 22:15:03 +00:00
*/
void setBuffer ( String buffer )
{
this.buffer = buffer;
this.cursor = 0;
}
2011-04-30 12:30:01 +00:00
2011-05-08 22:15:03 +00:00
/**
* Sets a new buffer with data to be parsed
*
* Params:
* buffer = the buffer to be parsed
2011-05-08 22:15:03 +00:00
*/
void setBuffer ( ubyte[] buffer )
{
this.setBuffer(*(cast(String*) &buffer));
}
2011-04-30 12:30:01 +00:00
2011-05-08 22:15:03 +00:00
/**
* Sets a new buffer with data to be parsed
*
* Params:
* buffer = pointer to the buffer to be parsed
* length = size of the buffer
2011-05-08 22:15:03 +00:00
*/
void setBuffer ( ubyte* buffer, size_t length )
{
this.setBuffer(String(length, buffer));
}
}
2011-04-30 12:30:01 +00:00
private:
2011-04-30 12:30:01 +00:00
/**
* Parse PSYC packets.
*
* Generalized line-based packet parser.
*
* Params:
* state = An initialized ParseState
* oper = A pointer to a character. In case of a variable, it will
* be set to the operator of that variable
* name = A pointer to a String. It will point to the name of
* the variable or method and its length will be set accordingly
* value = A pointer to a String. It will point to the
* value/body the variable/method and its length will be set accordingly
2011-04-30 12:30:01 +00:00
*/
ParseRC psyc_parse(ParseState* state, char* oper, String* name, String* value);
/**
* List value parser.
*/
ParseListRC psyc_parseList(ParseListState* state, String *name, String* value, String* elem);
/** @} */ // end of parsing group