#Dzen language parser This library provides a parser to interpret the Dzen in-text formating and control language. See https://github.com/robm/dzen for more information about Dzen.
#How-to use ###The beginning Currently there is just one entry point which accepts a cstring and returns a token list.
dzen_list *list = dzen_parse("^fg(red)I'm red text ^fg(blue)I am blue");
###Token iteration
To iterate through a dzen_list
you can use a dzen_list_iterator
which is created with the help of dzen_list_iterator_create(dzen_list *list)
.
dzen_list_iterator *it = dzen_list_iterator_create(list);
Now you can iterate from the first token to the last or backward.
// iterate forward
while (dzen_list_iterate_forward(it)) {
char *string_val = it->token->string_value;
}
// iterate backward
while (dzen_list_iterate_backward(it)) {
char *string_val = it->token->string_value;
}
###The end
After working with the token list and an iterator you have to de-allocated these. Whenever you use an iterator you can use dzen_list_iterator_destroy(dzen_list_iterator *iterator)
.
This function frees the iterator only. So it is required to destroy the list also with the help of 'dzen_list_destroy(dzen_list *list)'.
// free list first
dzen_list_destroy(it->list);
// free iterator
dzen_list_iterator_destroy(it);
#Dzen language token
A language token represents a logical unit of the input string like a function, a text or a parameter. So the following example input "^fg(red)I'm red text ^fg(blue)I am blue"
results in a list with 4 token:
- Function >>
DZEN_TOKEN_TYPE_FUNCTION
>>^fg(red)
- Text >>
DZEN_TOKEN_TYPE_TEXT
>>I'm red text
- Function >>
DZEN_TOKEN_TYPE_FUNCTION
>>^fg(blue)
- Text >>
DZEN_TOKEN_TYPE_TEXT
>>I am blue
##Token structure A token consists of the following information.
struct dzen_lang_token {
dzen_token_type type;
dzen_token_value_modifier *value_modifier;
char *value;
struct dzen_token *parameter_list;
};
type
- The type of the token like a function or a text
value_modifier
- A list of value modifier. Could be
NULL
- For example a modifier is used to represent the
+
or-
in the^p(+-X)
function
- A list of value modifier. Could be
value
- The value of the token, like the name of a function. Could be
NULL
- The value of the token, like the name of a function. Could be
parameter_list
- A list of parameter. Could be
NULL
- This field is usually used for parameter with multiple info like the size parameter
2x3
where2
is the first token in this list and3
the second one
- A list of parameter. Could be
##Token types ###Function A function consists of a name and a parameter list.