![]() |
![]() |
![]() |
CTPL Reference Manual | ![]() |
---|---|---|---|---|
Top | Description |
#include <ctpl/lexer-expr.h> #define CTPL_EXPR_CHARS #define CTPL_OPERAND_CHARS #define CTPL_OPERATOR_CHARS #define CTPL_LEXER_EXPR_ERROR enum CtplLexerExprError; CtplTokenExpr * ctpl_lexer_expr_lex (const gchar *expr, gssize len, GError **error); const gchar * ctpl_operator_to_string (CtplOperator op); CtplOperator ctpl_operator_from_string (const gchar *str, gssize len, gsize *operator_len);
Syntax analyser for mathematical or test expressions creating a token tree from an expression.
To analyse an expression, use ctpl_lexer_expr_lex()
. The resulting expression
should be freed with ctpl_token_expr_free()
when no longer needed.
An expression is something like a mathematical expression that can include references to variables. The allowed things are:
Binary operators |
addition ( The boolean operators results to the integer 0 if their expression evaluates to false, or to the positive integer 1 if their expression evaluates to true. This result might be used as a plain integer. The operators' priority is very common: boolean operators have the higher priority, followed by division, modulo and multiplication, and finally addition and subtraction which have the lower priority. When two operators have the same priority, the left one is prior over the right one. |
Unary operators |
The unary operators plus ( |
Operands |
Any numeric constant written in C notation (period ( |
Parentheses |
Parentheses may be placed to delimit sub-expressions, allowing a fine control over operator priority. |
Of course, the latter example supposes that the environment contains the two
variables foo
and bar
.
#define CTPL_LEXER_EXPR_ERROR (ctpl_lexer_expr_error_quark ())
Error domain of CtplLexerExprError.
typedef enum _CtplLexerExprError { CTPL_LEXER_EXPR_ERROR_MISSING_OPERAND, CTPL_LEXER_EXPR_ERROR_MISSING_OPERATOR, CTPL_LEXER_EXPR_ERROR_SYNTAX_ERROR, CTPL_LEXER_EXPR_ERROR_FAILED } CtplLexerExprError;
Error codes that lexing functions can throw.
CtplTokenExpr * ctpl_lexer_expr_lex (const gchar *expr, gssize len, GError **error);
Tries to lex expr
.
|
An expression to lex |
|
The length to read from expr , or -1 to read the whole string
|
|
Return location for errors, or NULL to ignore them.
|
Returns : |
A new CtplTokenExpr or NULL on error.
|
const gchar * ctpl_operator_to_string (CtplOperator op);
Gets the string representation of an operator.
This representation is understood by the lexer if op
is valid.
|
A CtplOperator |
Returns : |
A string representing the operator. This string should not be modified or freed. |
CtplOperator ctpl_operator_from_string (const gchar *str, gssize len, gsize *operator_len);
Tries to convert a string to an operator, as the lexer may do.
|
A string starting with an operator |
|
length to read from str , or -1 to read the whole string.
|
|
Return location for the length of the read operator, or NULL .
|
Returns : |
The read operator or CTPL_OPERATOR_NONE if none successfully read.
|