CtplToken

CtplToken — Language token

Synopsis

#include <ctpl/token.h>

enum                CtplOperator;
enum                CtplTokenType;
union               CtplTokenValue;
struct              CtplToken;
struct              CtplTokenFor;
struct              CtplTokenIf;
enum                CtplTokenExprType;
union               CtplTokenExprValue;
struct              CtplTokenExpr;
struct              CtplTokenExprOperator;
CtplToken *         ctpl_token_new_data                 (const gchar *data,
                                                         gssize len);
CtplToken *         ctpl_token_new_expr                 (CtplTokenExpr *expr);
CtplToken *         ctpl_token_new_for                  (const gchar *array,
                                                         const gchar *iterator,
                                                         CtplToken *children);
CtplToken *         ctpl_token_new_if                   (CtplTokenExpr *condition,
                                                         CtplToken *if_children,
                                                         CtplToken *else_children);
void                ctpl_token_free                     (CtplToken *token,
                                                         gboolean chain);
void                ctpl_token_append                   (CtplToken *token,
                                                         CtplToken *brother);
void                ctpl_token_prepend                  (CtplToken *token,
                                                         CtplToken *brother);
#define             ctpl_token_get_type                 (token)
void                ctpl_token_dump                     (const CtplToken *token,
                                                         gboolean chain);
CtplTokenExpr *     ctpl_token_expr_new_operator        (CtplOperator operator,
                                                         CtplTokenExpr *loperand,
                                                         CtplTokenExpr *roperand);
CtplTokenExpr *     ctpl_token_expr_new_integer         (glong integer);
CtplTokenExpr *     ctpl_token_expr_new_float           (gdouble real);
CtplTokenExpr *     ctpl_token_expr_new_symbol          (const gchar *symbol,
                                                         gssize len);
void                ctpl_token_expr_free                (CtplTokenExpr *token,
                                                         gboolean recurse);
void                ctpl_token_expr_dump                (const CtplTokenExpr *token);

Description

Represents a CTPL language token.

A CtplToken is created with ctpl_token_new_data(), ctpl_token_new_expr(), ctpl_token_new_for() or ctpl_token_new_if(), and freed with ctpl_token_free(). You can append or prepend tokens to others with ctpl_token_append() and ctpl_token_prepend(). To dump a CtplToken, use ctpl_token_dump().

A CtplTokenExpr is created with ctpl_token_expr_new_operator(), ctpl_token_expr_new_integer(), ctpl_token_expr_new_float() or ctpl_token_expr_new_symbol(), and freed with ctpl_token_expr_free(). To dump a CtplTokenExpr, use ctpl_token_expr_dump().

Details

enum CtplOperator

typedef enum {
  CTPL_OPERATOR_PLUS,
  CTPL_OPERATOR_MINUS,
  CTPL_OPERATOR_DIV,
  CTPL_OPERATOR_MUL,
  CTPL_OPERATOR_EQUAL,
  CTPL_OPERATOR_INF,
  CTPL_OPERATOR_SUP,
  CTPL_OPERATOR_MODULO,
  CTPL_OPERATOR_SUPEQ,
  CTPL_OPERATOR_INFEQ,
  CTPL_OPERATOR_NEQ,
  CTPL_OPERATOR_AND,
  CTPL_OPERATOR_OR,
  /* must be last */
  CTPL_OPERATOR_NONE
} CtplOperator;

Operators constants.

See also ctpl_operator_to_string() and ctpl_operator_from_string().

CTPL_OPERATOR_PLUS

Addition operator

CTPL_OPERATOR_MINUS

Subtraction operator

CTPL_OPERATOR_DIV

Division operator

CTPL_OPERATOR_MUL

Multiplication operator

CTPL_OPERATOR_EQUAL

Equality test operator

CTPL_OPERATOR_INF

Inferiority test operator

CTPL_OPERATOR_SUP

Superiority test operator

CTPL_OPERATOR_MODULO

Modulo operator

CTPL_OPERATOR_SUPEQ

CTPL_OPERATOR_SUP || CTPL_OPERATOR_EQUAL

CTPL_OPERATOR_INFEQ

CTPL_OPERATOR_INF || CTPL_OPERATOR_EQUAL

CTPL_OPERATOR_NEQ

Non-equality test operator (! CTPL_OPERATOR_EQUAL)

CTPL_OPERATOR_AND

Boolean AND operator

CTPL_OPERATOR_OR

Boolean OR operator

CTPL_OPERATOR_NONE

Not an operator, denoting no operator

enum CtplTokenType

typedef enum _CtplTokenType
{
  CTPL_TOKEN_TYPE_DATA,
  CTPL_TOKEN_TYPE_FOR,
  CTPL_TOKEN_TYPE_IF,
  CTPL_TOKEN_TYPE_EXPR
} CtplTokenType;

Possible types of a token.

CTPL_TOKEN_TYPE_DATA

Data flow, not an language token

CTPL_TOKEN_TYPE_FOR

A loop through an array of values

CTPL_TOKEN_TYPE_IF

A conditional branching

CTPL_TOKEN_TYPE_EXPR

An expression

union CtplTokenValue

union _CtplTokenValue
{
  gchar          *t_data;
  CtplTokenExpr  *t_expr;
  CtplTokenFor   *t_for;
  CtplTokenIf    *t_if;
};

Represents the possible values of a token (see CtplToken).

gchar *t_data;

The data of a data token

CtplTokenExpr *t_expr;

The value of an expression token

CtplTokenFor *t_for;

The value of a for token

CtplTokenIf *t_if;

The value of an if token

struct CtplToken

struct CtplToken {
  CtplTokenType   type;
  CtplTokenValue  token;
  CtplToken      *next;
  CtplToken      *last;
};

The CtplToken structure. The fields in this structure should be considered private and are documented only for internal usage.

CtplTokenType type;

Type of the token

CtplTokenValue token;

Union holding the corresponding token (according to type)

CtplToken *next;

Next token

CtplToken *last;

Last token

struct CtplTokenFor

struct CtplTokenFor {
  gchar      *array;
  gchar      *iter;
  CtplToken  *children;
};

Holds information about a for statement.

gchar *array;

The symbol of the array

gchar *iter;

The symbol of the iterator

CtplToken *children;

Tree to repeat on iterations

struct CtplTokenIf

struct CtplTokenIf {
  CtplTokenExpr  *condition;
  CtplToken      *if_children;
  CtplToken      *else_children;
};

Holds information about an if statement.

CtplTokenExpr *condition;

The condition string

CtplToken *if_children;

Branching if condition evaluate to true

CtplToken *else_children;

Branching if condition evaluate to false

enum CtplTokenExprType

typedef enum _CtplTokenExprType
{
  CTPL_TOKEN_EXPR_TYPE_OPERATOR,
  CTPL_TOKEN_EXPR_TYPE_INTEGER,
  CTPL_TOKEN_EXPR_TYPE_FLOAT,
  CTPL_TOKEN_EXPR_TYPE_SYMBOL
} CtplTokenExprType;

Possibles types of an expression token.

CTPL_TOKEN_EXPR_TYPE_OPERATOR

An operator (CTPL_OPERATOR_*)

CTPL_TOKEN_EXPR_TYPE_INTEGER

An integer

CTPL_TOKEN_EXPR_TYPE_FLOAT

A floating-point value

CTPL_TOKEN_EXPR_TYPE_SYMBOL

A symbol (a name to be found in the environ)

union CtplTokenExprValue

union _CtplTokenExprValue
{
  CtplTokenExprOperator  *t_operator;
  glong                   t_integer;
  gdouble                 t_float;
  gchar                  *t_symbol;
};

Represents the possible values of an expression token (see CtplTokenExpr).

CtplTokenExprOperator *t_operator;

The value of an operator token

glong t_integer;

The value of an integer token

gdouble t_float;

The value of a floating-point token

gchar *t_symbol;

The name of a symbol token

struct CtplTokenExpr

struct CtplTokenExpr {
  CtplTokenExprType   type;
  CtplTokenExprValue  token;
};

Represents an expression token. The fields in this structure should be considered private and are documented only for internal usage.

CtplTokenExprType type;

The type of the expression token

CtplTokenExprValue token;

The value of the token

struct CtplTokenExprOperator

struct CtplTokenExprOperator {
  CtplOperator    operator;
  CtplTokenExpr  *loperand;
  CtplTokenExpr  *roperand;
};

Represents a operator token in an expression.

CtplOperator operator;

The operator

CtplTokenExpr *loperand;

The left operand

CtplTokenExpr *roperand;

The right operand

ctpl_token_new_data ()

CtplToken *         ctpl_token_new_data                 (const gchar *data,
                                                         gssize len);

Creates a new token holding raw data.

data :

Buffer containing token value (raw data)

len :

length of the data or -1 if 0-terminated

Returns :

A new CtplToken that should be freed with ctpl_token_free() when no longer needed.

ctpl_token_new_expr ()

CtplToken *         ctpl_token_new_expr                 (CtplTokenExpr *expr);

Creates a new token holding an expression. Such tokens are used to represent any expression that will be simply replaced, including simple reference to variables or constants, as of complex expressions with or without variable or expression references.

expr :

The expression

Returns :

A new CtplToken that should be freed with ctpl_token_free() when no longer needed.

ctpl_token_new_for ()

CtplToken *         ctpl_token_new_for                  (const gchar *array,
                                                         const gchar *iterator,
                                                         CtplToken *children);

Creates a new token holding a for statement.

array :

String containing the name of the array to iterate over

iterator :

String containing the name of the array iterator

children :

Sub-tree that should be computed on each loop iteration

Returns :

A new CtplToken that should be freed with ctpl_token_free() when no longer needed.

ctpl_token_new_if ()

CtplToken *         ctpl_token_new_if                   (CtplTokenExpr *condition,
                                                         CtplToken *if_children,
                                                         CtplToken *else_children);

Creates a new token holding an if statement.

condition :

The expression condition

if_children :

Branching if condition evaluate to true

else_children :

Branching if condition evaluate to false, or NULL

Returns :

A new CtplToken that should be freed with ctpl_token_free() when no longer needed.

ctpl_token_free ()

void                ctpl_token_free                     (CtplToken *token,
                                                         gboolean chain);

Frees all memory used by a CtplToken. If chain is TRUE, all tokens attached at the right of token (appended ones) will be freed too. Then, if this function is called with chain set to TRUE on the root token of a tree, all the tree will be freed. Otherwise, if chain is FALSE, token is freed and detached from its neighbours.

token :

A CtplToken to free

chain :

Whether all next tokens should be freed too or not.

ctpl_token_append ()

void                ctpl_token_append                   (CtplToken *token,
                                                         CtplToken *brother);

Appends (link at the end) brother to token.

token :

A CtplToken

brother :

Another CtplToken

ctpl_token_prepend ()

void                ctpl_token_prepend                  (CtplToken *token,
                                                         CtplToken *brother);

Prepends (link at the start) btother to token.

token :

A CtplToken

brother :

Another CtplToken

ctpl_token_get_type()

#define ctpl_token_get_type(token) ((token)->type)

Gets the type of a CtplToken.

token :

A CtplToken

Returns :

The type of token.

ctpl_token_dump ()

void                ctpl_token_dump                     (const CtplToken *token,
                                                         gboolean chain);

Dumps a token with g_print(). If chain is TRUE, all next tokens will be dumped too.

token :

A CtplToken

chain :

Whether to dump all next tokens

ctpl_token_expr_new_operator ()

CtplTokenExpr *     ctpl_token_expr_new_operator        (CtplOperator operator,
                                                         CtplTokenExpr *loperand,
                                                         CtplTokenExpr *roperand);

Creates a new CtplTokenExpr holding an operator.

operator :

A binary operator (one of the CTPL_OPERATOR_*)

loperand :

The left operand of the operator

roperand :

The right operand of the operator

Returns :

A new CtplTokenExpr that should be freed with ctpl_token_expr_free() when no longer needed.

ctpl_token_expr_new_integer ()

CtplTokenExpr *     ctpl_token_expr_new_integer         (glong integer);

Creates a new CtplTokenExpr holding an integer.

integer :

The value of the integer token

Returns :

A new CtplTokenExpr that should be freed with ctpl_token_expr_free() when no longer needed.

ctpl_token_expr_new_float ()

CtplTokenExpr *     ctpl_token_expr_new_float           (gdouble real);

Creates a new CtplTokenExpr holding a floating-point value.

real :

The value of the floating-point token

Returns :

A new CtplTokenExpr that should be freed with ctpl_token_expr_free() when no longer needed.

ctpl_token_expr_new_symbol ()

CtplTokenExpr *     ctpl_token_expr_new_symbol          (const gchar *symbol,
                                                         gssize len);

Creates a new CtplTokenExpr holding a symbol.

symbol :

String holding the symbol name

len :

Length to read from symbol or -1 to read the whole string.

Returns :

A new CtplTokenExpr that should be freed with ctpl_token_expr_free() when no longer needed.

ctpl_token_expr_free ()

void                ctpl_token_expr_free                (CtplTokenExpr *token,
                                                         gboolean recurse);

Frees all memory used by a CtplTokenExpr.

token :

A CtplTokenExpr to free

recurse :

Whether to free sub-tokens too.

ctpl_token_expr_dump ()

void                ctpl_token_expr_dump                (const CtplTokenExpr *token);

Dumps the given CtplTokenExpr using g_print().

token :

A CtplTokenExpr