Wed, 15 Apr 2015 08:57:46 +0200
removed ultrabreak gotos
/* * 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_LITERAL, 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; }; /** * Query statement object. * Contains the binary information about the parsed query. * * The grammar for a DavQLStatement is: * * <pre> * Expression = Expression, BinaryOperator, Expression * | UnaryOperator, Expression * | FunctionCall | Identifier | Literal * | "(", Expression, ")"; * * FunctionCall = Identifier, "(", ArgumentList, ")"; * ArgumentList = Expression, {",", Expression}; * Identifier = IdentifierChar - ?Digit?, {IdentifierChar} * | "`", ?Character?, {?Character?}, "`"; * IdentifierChar = ?Character - (" "|",")?; * Literal = Number | String; * Number = ?Digit?, {?Digit?} | "%d"; * String = "'", {?Character - "'"? | "'''"} , "'"; * * LogicalExpression = LogicalExpression, LogicalOperator, LogicalExpression * | "not ", LogicalExpression * | Expression, Comparison, Expression * | Expression, (" like " | " unlike "), String * | "(", LogicalExpression, ")"; * * UnaryOperator = "-" | "~"; * BinaryOperator = "+" | "-" | "*" | "/" | "&" | "|" | "^"; * LogicalOperator = " and " | " or " | " xor "; * Comparison = | "=" | "<" | ">" | "<=" | ">=" | "!="; * * FieldExpressions = "*", {",", Expression, " as ", String} * | FieldExpression, {",", FieldExpression} * | "-"; * FieldExpression = Identifier * | Expression, " as ", String; * SetExpressions = SetExpression, {",", SetExpressions}; * SetExpression = Identifier, "=", Expression; * * 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 ", Identifier, * [" with ", WithClause], * [" where ", LogicalExpression], * [" order by ", OrderByClause]; * </pre> * * <b>SET:</b> * <pre> * "set ",SetExpressions, * " at ", Identifier, * [" 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 field expressions. */ UcxList* fields; /** * The list of DavQLExpressions for the new DAV property values. * This is <code>NULL</code> for GET queries. */ UcxList* setvalues; /** * 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 recursion depth for the statement. * Defaults to 1. */ int depth; } DavQLStatement; /** Infinity recursion depth for a DavQLStatement. */ #define DAV_DEPTH_INFINITY -1 /** Expected an identifier, but found something else. */ #define DAVQL_ERROR_IDENTIFIER_EXPECTED 10 /** 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 /** 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 */