mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
Merge branch 'master' of supraverse.net:libpsyc
Conflicts: d/include/psyc/parser.d
This commit is contained in:
commit
b8b8083d9b
6 changed files with 7 additions and 4 deletions
112
d/include/psyc/common.d
Normal file
112
d/include/psyc/common.d
Normal file
|
@ -0,0 +1,112 @@
|
|||
/** @file d/psyc/common.d
|
||||
*
|
||||
* @brief Main PSYC interface providing crucial functionality.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
module psyc.common;
|
||||
|
||||
const EPOCH = 1440444041; // 2015-08-24 21:20:41 CET (Monday)
|
||||
|
||||
extern (C):
|
||||
|
||||
enum Bool
|
||||
{
|
||||
FALSE = 0,
|
||||
TRUE = 1,
|
||||
}
|
||||
|
||||
/**
|
||||
* PSYC packet parts.
|
||||
*/
|
||||
enum Part
|
||||
{
|
||||
RESET = -1,
|
||||
ROUTING = 0,
|
||||
LENGTH = 1,
|
||||
CONTENT = 2,
|
||||
METHOD = 3,
|
||||
DATA = 4,
|
||||
END = 5,
|
||||
}
|
||||
|
||||
/**
|
||||
* Different types that a variable can have.
|
||||
*
|
||||
* This enum lists PSYC variable types that
|
||||
* this library is capable of checking for
|
||||
* validity. Other variable types are treated
|
||||
* as opaque data.
|
||||
*/
|
||||
enum Type
|
||||
{
|
||||
UNKNOWN,
|
||||
AMOUNT,
|
||||
COLOR,
|
||||
DATE,
|
||||
DEGREE,
|
||||
ENTITY,
|
||||
FLAG,
|
||||
LANGUAGE,
|
||||
LIST,
|
||||
NICK,
|
||||
PAGE,
|
||||
UNIFORM,
|
||||
TIME,
|
||||
}
|
||||
|
||||
/**
|
||||
* List types.
|
||||
* Possible types are text and binary.
|
||||
*/
|
||||
enum ListType
|
||||
{
|
||||
LIST_TEXT = 1,
|
||||
LIST_BINARY = 2,
|
||||
}
|
||||
|
||||
struct String
|
||||
{
|
||||
size_t length;
|
||||
ubyte *ptr;
|
||||
}
|
||||
|
||||
struct MatchVar
|
||||
{
|
||||
String name;
|
||||
int value;
|
||||
}
|
||||
|
||||
int psyc_version()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// Routing vars in alphabetical order.
|
||||
extern (C) String routingVars[];
|
||||
extern (C) MatchVar varTypes[];
|
||||
|
||||
/**
|
||||
* Get the type of variable name.
|
||||
*/
|
||||
Bool isRoutingVar(char *name, size_t len);
|
||||
|
||||
/**
|
||||
* Get the type of variable name.
|
||||
*/
|
||||
Type getVarType(char *name, size_t len);
|
||||
|
||||
/**
|
||||
* Checks if long keyword string inherits from short keyword string.
|
||||
*/
|
||||
int inherits(char *sho, size_t slen,
|
||||
char *lon, size_t llen);
|
||||
|
||||
/**
|
||||
* Checks if short keyword string matches long keyword string.
|
||||
*/
|
||||
int matches(char *sho, size_t slen,
|
||||
char *lon, size_t llen);
|
||||
|
||||
|
310
d/include/psyc/parser.d
Normal file
310
d/include/psyc/parser.d
Normal file
|
@ -0,0 +1,310 @@
|
|||
module psyc.parser;
|
||||
|
||||
import psyc.common;
|
||||
|
||||
extern (C):
|
||||
|
||||
enum ParseFlag
|
||||
{
|
||||
ROUTING_ONLY = 1,
|
||||
START_AT_CONTENT = 2,
|
||||
}
|
||||
|
||||
/**
|
||||
* The return value definitions for the packet parsing function.
|
||||
*
|
||||
* See_Also:
|
||||
* parse()
|
||||
*/
|
||||
enum ParseRC
|
||||
{
|
||||
/// Error, packet is not ending with a valid delimiter.
|
||||
ERROR_END = -8,
|
||||
/// Error, expected NL after the method.
|
||||
ERROR_METHOD = -7,
|
||||
/// Error, expected NL after a modifier.
|
||||
ERROR_MOD_NL = -6,
|
||||
/// Error, modifier length is not numeric.
|
||||
ERROR_MOD_LEN = -5,
|
||||
/// Error, expected TAB before modifier value.
|
||||
ERROR_MOD_TAB = -4,
|
||||
/// Error, modifier name is missing.
|
||||
ERROR_MOD_NAME = -3,
|
||||
/// Error, expected NL after the content length.
|
||||
ERROR_LENGTH = -2,
|
||||
/// Error in packet.
|
||||
ERROR = -1,
|
||||
// Success, used internally.
|
||||
SUCCESS = 0,
|
||||
/// 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.
|
||||
INSUFFICIENT = 1,
|
||||
/// Routing modifier parsing done.
|
||||
/// Operator, name & value contains the respective parts.
|
||||
ROUTING = 2,
|
||||
/// Start of an incomplete entity modifier.
|
||||
/// Operator & name are complete, value is incomplete.
|
||||
ENTITY_START = 3,
|
||||
/// Continuation of an incomplete entity modifier.
|
||||
ENTITY_CONT = 4,
|
||||
/// End of an incomplete entity modifier.
|
||||
ENTITY_END = 5,
|
||||
/// Entity modifier parsing done in one go.
|
||||
/// Operator, name & value contains the respective parts.
|
||||
ENTITY = 6,
|
||||
/// Start of an incomplete body.
|
||||
/// Name contains method, value contains part of the body.
|
||||
BODY_START = 7,
|
||||
/// Continuation of an incomplete body.
|
||||
BODY_CONT = 8,
|
||||
/// End of an incomplete body.
|
||||
BODY_END = 9,
|
||||
/// Body parsing done in one go, name contains method, value contains body.
|
||||
BODY = 10,
|
||||
/// Start of an incomplete content, value contains part of content.
|
||||
/// Used when ROUTING_ONLY is set.
|
||||
CONTENT_START = 7,
|
||||
/// Continuation of an incomplete body.
|
||||
/// Used when ROUTING_ONLY is set.
|
||||
CONTENT_CONT = 8,
|
||||
/// End of an incomplete body.
|
||||
/// Used when ROUTING_ONLY is set.
|
||||
CONTENT_END = 9,
|
||||
/// Content parsing done in one go, value contains the whole content.
|
||||
/// Used when ROUTING_ONLY is set.
|
||||
CONTENT = 10,
|
||||
// Binary value parsing complete, used internally.
|
||||
COMPLETE = 11,
|
||||
// Binary value parsing incomplete, used internally.
|
||||
INCOMPLETE = 12,
|
||||
}
|
||||
|
||||
/**
|
||||
* The return value definitions for the list parsing function.
|
||||
* See_Also: parseList()
|
||||
*/
|
||||
enum ParseListRC
|
||||
{
|
||||
LIST_ERROR_DELIM = -5,
|
||||
LIST_ERROR_LEN = -4,
|
||||
LIST_ERROR_TYPE = -3,
|
||||
LIST_ERROR_NAME = -2,
|
||||
LIST_ERROR = -1,
|
||||
/// Completed parsing a list element.
|
||||
LIST_ELEM = 1,
|
||||
/// Reached end of buffer.
|
||||
LIST_END = 2,
|
||||
/// Binary list is incomplete.
|
||||
LIST_INCOMPLETE = 3,
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
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.
|
||||
*/
|
||||
static ParseState opCall ( ubyte flags )
|
||||
{
|
||||
ParseState inst;
|
||||
inst.flags = flags;
|
||||
|
||||
if (flags & ParseFlag.ROUTING_ONLY)
|
||||
inst.part = Part.CONTENT;
|
||||
|
||||
return inst;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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()];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
/**
|
||||
* Sets a new buffer with data to be parsed
|
||||
*
|
||||
* Params:
|
||||
* buffer = the buffer to be parsed
|
||||
*/
|
||||
void setBuffer ( String buffer )
|
||||
{
|
||||
this.buffer = buffer;
|
||||
this.cursor = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new buffer with data to be parsed
|
||||
*
|
||||
* Params:
|
||||
* buffer = the buffer to be parsed
|
||||
*/
|
||||
void setBuffer ( ubyte[] buffer )
|
||||
{
|
||||
this.setBuffer(*(cast(String*) &buffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new buffer with data to be parsed
|
||||
*
|
||||
* Params:
|
||||
* buffer = pointer to the buffer to be parsed
|
||||
* length = size of the buffer
|
||||
*/
|
||||
void setBuffer ( ubyte* buffer, size_t length )
|
||||
{
|
||||
this.setBuffer(String(length, buffer));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue