libidav/davqlparser.h

Tue, 31 Mar 2015 09:43:42 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 31 Mar 2015 09:43:42 +0200
changeset 80
a2832c054c98
parent 79
59c518ae0641
child 82
0567444f2d76
permissions
-rw-r--r--

added path examination to debugger + changed field names of expression subtrees

/*
 * 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 {GET, SET} davqltype_t;

/**
 * Enumeration of possible expression types.
 */
typedef enum {
    LITERAL, IDENTIFIER,
    UNARY, BINARY, LOGICAL, FUNCCALL
} davqlexprtype_t;

/**
 * Enumeration of possible expression operators.
 */
typedef enum {
    ADD, SUB, MUL, DIV,
    AND, OR, XOR, NEG,
    NOT, LAND, LOR, LXOR,
    EQ, NEQ, LT, GT, LE, GE,
    LIKE, UNLIKE
} 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, "(", Expression, ")";
 * Identifier   = ?Character?, {?Character?};
 * Literal      = ?Digit?, {?Digit?} | String;
 * 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", "=", Expression;
 * 
 * </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,
 * [" where ", LogicalExpression],
 * [" with ", WithClause];
  * </pre>
 * 
 * <b>SET:</b>
 * <pre>
 * "set ",SetExpressions,
 * " at ", Identifier,
 * (" where ", LogicalExpression) | " anywhere",
 * [" with ", WithClause];
 * </pre>
 * 
 */
typedef struct {
    /**
     * The original query text.
     */
    sstr_t srctext;
    /**
     * The statement type.
     */
    davqltype_t type;
    /**
     * 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 DavQLExpression that denotes the queried path.
     */
    DavQLExpression path;
    /**
     * Logical expression for selection.
     * <code>NULL</code>, if there is no where clause.
     */
    DavQLExpression* where;
    /**
     * The recursion depth for the statement.
     * Defaults to SIZE_MAX (unbound recursion).
     */
    size_t depth;
} DavQLStatement;


/**
 * Starts an interactive debugger for a DavQLStatement.
 * 
 * @param stmt the statement to debug
 */
void dav_debug_ql_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))

#ifdef	__cplusplus
}
#endif

#endif	/* DAVQLPARSER_H */

mercurial