![]() |
![]() |
![]() |
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 (CtplInputStream *stream
,GError **error
); CtplTokenExpr * ctpl_lexer_expr_lex_full (CtplInputStream *stream
,gboolean lex_all
,GError **error
); CtplTokenExpr * ctpl_lexer_expr_lex_string (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 that |
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, from the CTPL_LEXER_EXPR_ERROR
domain.
CtplTokenExpr * ctpl_lexer_expr_lex (CtplInputStream *stream
,GError **error
);
Tries to lex the expression in stream
.
If you want to lex a CtplInputStream that (may) hold other data after the
expression, see ctpl_lexer_expr_lex_full()
.
|
A CtplInputStream from where read the expression |
|
Return location for errors, or NULL to ignore them.
|
Returns : |
A new CtplTokenExpr or NULL on error.
|
CtplTokenExpr * ctpl_lexer_expr_lex_full (CtplInputStream *stream
,gboolean lex_all
,GError **error
);
Tries to lex the expression in stream
.
|
A CtplInputStream |
|
Whether to lex stream until EOF or until the end of a valid
expression. This is useful for expressions inside other data.
|
|
Return location for errors, or NULL to ignore them.
|
Returns : |
A new CtplTokenExpr or NULL on error.
|
CtplTokenExpr * ctpl_lexer_expr_lex_string (const gchar *expr
,gssize len
,GError **error
);
Tries to lex the expression in expr
.
See ctpl_lexer_expr_lex()
.
|
An expression |
|
Length of 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.
|