protobuf-c-text
Library to generate & parse text format protobufs in C.
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups
Data Structures | Macros | Enumerations | Functions | Variables
Routines that define a simple finite state machine
Collaboration diagram for Routines that define a simple finite state machine:

Data Structures

struct  State
 Maintain state for the FSM. More...
 

Macros

#define STATE_ERROR_STR_MAX   160
 Max size of an error message. More...
 

Enumerations

enum  StateId { STATE_OPEN, STATE_ASSIGNMENT, STATE_VALUE, STATE_DONE }
 StateId enumeration. More...
 

Functions

static int state_init (State *state, Scanner *scanner, const ProtobufCMessageDescriptor *descriptor, ProtobufCAllocator *allocator)
 Initialise a State struct. More...
 
static void state_free (State *state)
 Free internal data in a State struct. More...
 
static StateId state_error (State *state, Token *t, char *error_fmt,...) __attribute__((format(printf
 Handle an error in the FSM. More...
 
static StateId state_open (State *state, Token *t)
 Expect an element name (bareword) or a closing brace. More...
 
static StateId state_assignment (State *state, Token *t)
 Expect a colon or opening brace. More...
 
static StateId state_value (State *state, Token *t)
 Expect a quoted string, enum (bareword) or boolean. More...
 

Variables

static StateId(* states [])(State *, Token *)
 Table of states and actions. More...
 

Detailed Description

Macro Definition Documentation

#define STATE_ERROR_STR_MAX   160

Max size of an error message.

Definition at line 478 of file parse.re.

Referenced by state_error(), and state_init().

Enumeration Type Documentation

enum StateId

StateId enumeration.

A list of states for the FSM.

Enumerator
STATE_OPEN 

Ready to start a new statement or close a nested message.

STATE_ASSIGNMENT 

Ready to assign a scalar or a nested message.

STATE_VALUE 

Assign the scalar.

STATE_DONE 

Nothing more to read or there's been an error.

Definition at line 469 of file parse.re.

Function Documentation

static StateId state_assignment ( State state,
Token t 
)
static

Expect a colon or opening brace.

The state where we expect an assignment.

Parameters
[in,out]stateA state struct pointer.
[in]tThe Token to process.
Returns
A StateID value.

Definition at line 663 of file parse.re.

References State::allocator, State::current_msg, State::field, Token::id, local_realloc(), State::max_msg, State::msgs, ST_ALLOC, state_error(), STATE_OPEN, STATE_VALUE, STRUCT_MEMBER, TOK_COLON, TOK_OBRACE, and token2txt().

Here is the call graph for this function:

static StateId static StateId state_error ( State state,
Token t,
char *  error_fmt,
  ... 
)
static

Handle an error in the FSM.

At any point in the FSM if an error is encounters, call this function with an explination of the error - and return the resulting value.

Parameters
[in,out]stateA state struct pointer.
[in]tThe Token to process.
[in]error_fmtprintf style format string for the error message.
[in]...Arguments for error_fmt .
Returns
This will always return STATE_DONE .

Definition at line 575 of file parse.re.

References STATE_DONE, and STATE_ERROR_STR_MAX.

Referenced by protobuf_c_text_parse(), state_assignment(), state_open(), and state_value().

Here is the caller graph for this function:

static void state_free ( State state)
static

Free internal data in a State struct.

Frees allocated data within the State instance. Note that the State instance itself is not freed and that the State instance contains a pointer to the ProtobufCAllocator allocator that was passed in state_init().

Note also that the error_str element is only freed if there hasn't been an error. If there has been an error, the responsibility falls on the caller to free error_str .

Parameters
[in,out]stateA state struct pointer.

Definition at line 550 of file parse.re.

References State::error, State::error_str, State::msgs, and ST_FREE.

Referenced by protobuf_c_text_parse().

Here is the caller graph for this function:

static int state_init ( State state,
Scanner scanner,
const ProtobufCMessageDescriptor *  descriptor,
ProtobufCAllocator *  allocator 
)
static

Initialise a State struct.

Parameters
[in,out]stateA state struct pointer - the is the state for the FSM.
[in,out]scannerThe state struct for the scanner.
[in]descriptorMessage descriptor.
[in]allocatorAllocator functions.
Returns
Success (1) or failure (0). Failure is due to out of memory errors.

Definition at line 510 of file parse.re.

References State::allocator, State::error_str, State::max_msg, State::msgs, State::scanner, ST_ALLOC, ST_FREE, and STATE_ERROR_STR_MAX.

Referenced by protobuf_c_text_parse().

Here is the caller graph for this function:

static StateId state_open ( State state,
Token t 
)
static

Expect an element name (bareword) or a closing brace.

Initial state, and state after each assignment completes (or a message assignment starts. If a bareword is found, go into STATE_ASSIGNMENT and if a closing brace is found, go into STATE_DONE.

If something else is found or if there are no more messages on the stack (in other words, we're already at the base message), call and return with state_error().

Parameters
[in,out]stateA state struct pointer.
[in]tThe Token to process.
Returns
A StateID value.

Definition at line 610 of file parse.re.

References Token::bareword, State::current_msg, State::field, Token::id, State::msgs, STATE_ASSIGNMENT, STATE_DONE, state_error(), STATE_OPEN, TOK_BAREWORD, TOK_CBRACE, TOK_EOF, and token2txt().

Here is the call graph for this function:

static StateId state_value ( State state,
Token t 
)
static

Expect a quoted string, enum (bareword) or boolean.

Assign the value in Token to the field we identified in the state_open() call. This function is huge in order to handle the variety of data types and the struct offset math required to manipulate them.

Parameters
[in,out]stateA state struct pointer.
[in]tThe Token to process.
Returns
A StateID value.

Definition at line 771 of file parse.re.

References State::allocator, Token::bareword, Token::boolean, State::current_msg, State::field, Token::id, local_realloc(), State::msgs, Token::number, Token::qs, ST_ALLOC, state_error(), STATE_OPEN, STRUCT_MEMBER, STRUCT_MEMBER_PTR, TOK_BAREWORD, TOK_BOOLEAN, TOK_NUMBER, TOK_QUOTED, and token2txt().

Here is the call graph for this function:

Variable Documentation

StateId(* states[])(State *, Token *)
static
Initial value:
= {
}
static StateId state_value(State *state, Token *t)
Expect a quoted string, enum (bareword) or boolean.
Definition: parse.re:771
Ready to start a new statement or close a nested message.
Definition: parse.re:470
static StateId state_assignment(State *state, Token *t)
Expect a colon or opening brace.
Definition: parse.re:663
Assign the scalar.
Definition: parse.re:473
static StateId state_open(State *state, Token *t)
Expect an element name (bareword) or a closing brace.
Definition: parse.re:610
Ready to assign a scalar or a nested message.
Definition: parse.re:472

Table of states and actions.

This is a table of each state and the action to take when in it.

Definition at line 1152 of file parse.re.

Referenced by protobuf_c_text_parse().