Sat, 02 May 2015 18:44:25 +0200
added clang config + fixed control flow in two parser functions
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2015 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef DAVQLPARSER_H #define DAVQLPARSER_H #ifdef __cplusplus extern "C" { #endif #include <stdint.h> #include "ucx/string.h" #include "ucx/list.h" /** * Enumeration of possible statement types. */ typedef enum {DAVQL_ERROR, DAVQL_GET, DAVQL_SET} davqltype_t; /** * Enumeration of possible expression types. */ typedef enum { DAVQL_UNDEFINED_TYP, DAVQL_NUMBER, DAVQL_STRING, DAVQL_TIMESTAMP, DAVQL_IDENTIFIER, DAVQL_UNARY, DAVQL_BINARY, DAVQL_LOGICAL, DAVQL_FUNCCALL } davqlexprtype_t; /** * Enumeration of possible expression operators. */ typedef enum { DAVQL_NOOP, DAVQL_CALL, DAVQL_ARGLIST, // internal representations DAVQL_ADD, DAVQL_SUB, DAVQL_MUL, DAVQL_DIV, DAVQL_AND, DAVQL_OR, DAVQL_XOR, DAVQL_NEG, // airthmetic DAVQL_NOT, DAVQL_LAND, DAVQL_LOR, DAVQL_LXOR, // logical DAVQL_EQ, DAVQL_NEQ, DAVQL_LT, DAVQL_GT, DAVQL_LE, DAVQL_GE, DAVQL_LIKE, DAVQL_UNLIKE // comparisons } davqloperator_t; /** * An expression within a DAVQL query. */ typedef struct _davqlexpr DavQLExpression; /** * The structure for type DavQLExpression. */ struct _davqlexpr { /** * The original expression text. * Contains the literal value, if type is LITERAL. */ sstr_t srctext; /** * The expression type. */ davqlexprtype_t type; /** * Operator. */ davqloperator_t op; /** * Left or single operand. * <code>NULL</code> for literals or identifiers. */ DavQLExpression *left; /** * Right operand. * <code>NULL</code> for literals, identifiers or unary expressions. */ DavQLExpression *right; }; /** * A tuple representing an order criterion. */ typedef struct { /** * The column. */ DavQLExpression *column; /** * True, if the result shall be sorted descending, false otherwise. * Default is false (ascending). */ _Bool descending; } DavQLOrderCriterion; /** * A tuple representing a field. */ typedef struct { /** * The field name. * <ul> * <li>GET: the identifier or an alias name</li> * <li>SET: the identifier</li> * </ul> */ sstr_t name; /** * The field expression. * <ul> * <li>GET: the queried property (identifier) or an expression</li> * <li>SET: the expression for the value to be set</li> * </ul> */ DavQLExpression *expr; } DavQLField; /** * Query statement object. * Contains the binary information about the parsed query. * * The grammar for a DavQLStatement is: * * <pre> * Expression = AddExpression; * AddExpression = MultExpression, [AddOperator, AddExpression]; * MultExpression = BitwiseExpression, [MultOperator, MultExpression]; * BitwiseExpression = UnaryExpression, [BitwiseOperator, BitwiseExpression]; * UnaryExpression = [UnaryOperator], (AtomicExpression | ParExpression); * AtomicExpression = FunctionCall | Identifier | Literal; * ParExpression = "(", Expression, ")"; * * BitwiseOperator = "&" | "|" | "^"; * MultOperator = "*" | "/"; * AddOperator = "+" | "-"; * UnaryOperator = "+" | "-" | "~"; * * FunctionCall = Identifier, "(", ArgumentList, ")"; * ArgumentList = Expression, {",", Expression}; * Identifier = IdentifierChar - ?Digit?, {IdentifierChar} * | "`", ?Character? - "`", {?Character? - "`"}, "`"; * IdentifierChar = ?Character? - (" "|","); * Literal = Number | String | Timestamp; * Number = ?Digit?, {?Digit?} | "%d"; * String = "'", {?Character? - "'" | "'''"} , "'" | "%s"; * Timestamp = "%t"; // TODO: maybe introduce a real literal * * LogicalExpression = "not ", LogicalExpression * | "(", LogicalExpression, ")" * | BooleanExpression, [LogicalOperator, LogicalExpression]; * BooleanExpression = Expression, Comparison, Expression * | Expression, (" like " | " unlike "), String * | FunctionCall | Identifier; * * LogicalOperator = " and " | " or " | " xor "; * Comparison = | "=" | "<" | ">" | "<=" | ">=" | "!="; * * FieldExpressions = "*", {",", Expression, " as ", Identifier} * | FieldExpression, {",", FieldExpression} * | "-"; * FieldExpression = Identifier * | Expression, " as ", Identifier; * SetExpressions = SetExpression, {",", SetExpression}; * SetExpression = Identifier, "=", Expression; * * Path = "%s" * | "/", {?Character? - " "} * | "'/", {?Character?}, "'"; * * WithClause = "depth", "=", (Number | "infinity"); * * OrderByClause = OrderByCriterion, {",", OrderByCriterion}; * OrderByCriterion = (Identifier | Number), [" asc"|" desc"]; * * </pre> * * Note: mandatory spaces are part of the grammar. But you may also insert an * arbitrary amount of optional spaces between two symbols if they are not part * of an literal or identifier. * * <b>GET:</b> * <pre> * GetStatement = "get ", FieldExpressions, * " from ", Path, * [" with ", WithClause], * [" where ", LogicalExpression], * [" order by ", OrderByClause]; * </pre> * * <b>SET:</b> * <pre> * "set ",SetExpressions, * " at ", Path, * [" with ", WithClause], * (" where ", LogicalExpression) | " anywhere"; * </pre> * */ typedef struct { /** * The original query text. */ sstr_t srctext; /** * The statement type. */ davqltype_t type; /** * Error code, if any error occurred. Zero otherwise. */ int errorcode; /** * Error message, if any error occurred. */ char* errormessage; /** * The list of DavQLFields. */ UcxList* fields; /** * A string that denotes the queried path. */ sstr_t path; /** * Logical expression for selection. * <code>NULL</code>, if there is no where clause. */ DavQLExpression* where; /** * The list of DavQLOrderCriterions. * This is <code>NULL</code> for SET queries and may be <code>NULL</code> * if the result doesn't need to be sorted. */ UcxList* orderby; /** * The recursion depth for the statement. * Defaults to 1. * Magic numbers are DAV_DEPTH_INFINITY for infinity and * DAV_DEPTH_PLACEHOLDER for a placeholder. */ int depth; } DavQLStatement; /** Infinity recursion depth for a DavQLStatement. */ #define DAV_DEPTH_INFINITY -1 /** Depth needs to be specified at runtime. */ #define DAV_DEPTH_PLACEHOLDER -2 /** Invalid path. */ #define DAVQL_ERROR_INVALID_PATH 1 /** Expected an identifier, but found something else. */ #define DAVQL_ERROR_IDENTIFIER_EXPECTED 10 /** Expected an identifier or literal, but found something else. */ #define DAVQL_ERROR_IDORLIT_EXPECTED 11 /** Expected an identifier or number, but found something else. */ #define DAVQL_ERROR_IDORNUM_EXPECTED 12 /** Expected an identifier or string, but found something else. */ #define DAVQL_ERROR_IDORSTR_EXPECTED 13 /** Expected an identifier or timestamp, but found something else. */ #define DAVQL_ERROR_IDORTS_EXPECTED 14 /** The with-clause contains an unknown attribute. */ #define DAVQL_ERROR_UNKNOWN_ATTRIBUTE 20 /** Depth must be greater than zero or infinity. */ #define DAVQL_ERROR_INVALID_DEPTH 21 /** The with-clause contains an attribute more than once. */ #define DAVQL_ERROR_DUPLICATED_ATTRIBUTE 29 /** The format specifier is missing. */ #define DAVQL_ERROR_MISSING_FMTSPEC 30 /** The format specifier is unknown. */ #define DAVQL_ERROR_UNKNOWN_FMTSPEC 31 /** The format specifier is invalid. */ #define DAVQL_ERROR_INVALID_FMTSPEC 39 /** A quote symbol (' or `) is missing. */ #define DAVQL_ERROR_MISSING_QUOTE 50 /** No more tokens to parse, but the parser expected more. */ #define DAVQL_ERROR_UNEXPECTED_END 100 /** A token was found, which has not been expected. */ #define DAVQL_ERROR_UNEXPECTED_TOKEN 101 /** Nothing about the statement seems legit. */ #define DAVQL_ERROR_INVALID -1 /** * Starts an interactive debugger for a DavQLStatement. * * @param stmt the statement to debug */ void dav_debug_statement(DavQLStatement *stmt); /** * Parses a statement. * @param stmt the sstr_t containing the statement * @return a DavQLStatement object */ DavQLStatement* dav_parse_statement(sstr_t stmt); /** * Implicitly converts a cstr to a sstr_t and calls dav_parse_statement. */ #define dav_parse_cstr_statement(stmt) dav_parse_statement(S(stmt)) /** * Frees a DavQLStatement. * @param stmt the statement object to free */ void dav_free_statement(DavQLStatement *stmt); #ifdef __cplusplus } #endif #endif /* DAVQLPARSER_H */