Classes | Defines | Typedefs | Functions
rparecord.h File Reference

The public interface for working with Abstract Syntax Tree (AST), produced by rpa_stat_parse. More...

Go to the source code of this file.

Classes

struct  rparecord_s

Defines

#define RPA_RECORD_NONE   (0)
#define RPA_RECORD_START   (1 << 0)
#define RPA_RECORD_END   (1 << 1)

Typedefs

typedef struct rparecord_s rparecord_t
typedef long(* rpa_recordtree_callback )(rarray_t *records, long rec, rpointer userdata)

Functions

long rpa_recordtree_get (rarray_t *records, long rec, unsigned long type)
long rpa_recordtree_firstchild (rarray_t *records, long rec, unsigned long type)
long rpa_recordtree_lastchild (rarray_t *records, long rec, unsigned long type)
long rpa_recordtree_next (rarray_t *records, long rec, unsigned long type)
long rpa_recordtree_prev (rarray_t *records, long rec, unsigned long type)
long rpa_recordtree_parent (rarray_t *records, long rec, unsigned long type)
long rpa_recordtree_size (rarray_t *records, long rec)
long rpa_recordtree_copy (rarray_t *dst, rarray_t *src, long rec)
rparecord_trpa_record_get (rarray_t *records, long rec)
rarray_t * rpa_records_create ()
void rpa_records_destroy (rarray_t *records)

Detailed Description

The public interface for working with Abstract Syntax Tree (AST), produced by rpa_stat_parse.

Synopsis

Upon a successful call to rpa_stat_parse, the parser produces a stack of rparecord_t records. There are two kinds of records: RPA_RECORD_START and RPA_RECORD_END. RPA_RECORD_START marks the beginning of a branch and RPA_RECORD_END marks the end of that branch. Empty branches are specified by a record RPA_RECORD_START followed immediately by RPA_RECORD_END (no child records in between). Empty branches are considered leaves.

Example: Consider parsing a person name:

John M. Smith 

with the following BNF:

 first  ::= [A-Za-z]+
 middle ::= [A-Za-z]+ '.'?
 last   ::= [A-Za-z]+
 name   ::= <first> ' ' <middle> ' ' <last>

The records produced by rpa_stat_parse would look like this:

 [record offset]    [record type]       [rule name]  [input offset]  [input size]  [input]
  0                  RPA_RECORD_START    name         0               13            John M. Smith
  1                  RPA_RECORD_START    first        0                4            John
  2                  RPA_RECORD_END      first        0                4            John
  3                  RPA_RECORD_START    middle       5                2            M.
  4                  RPA_RECORD_END      middle       5                2            M.
  5                  RPA_RECORD_START    last         8                5            Smith
  6                  RPA_RECORD_END      last         8                5            Smith
  7                  RPA_RECORD_END      name         0               13            John M. Smith

Note: first, middle and last are enclosed within name's RPA_RECORD_START and RPA_RECORD_END


Define Documentation

#define RPA_RECORD_END   (1 << 1)

End record - the parser generates this record after evaluating the rule and the rule matched some input.

Examples:
personname.c.
#define RPA_RECORD_NONE   (0)

No record type - the record type is not initialized

#define RPA_RECORD_START   (1 << 0)

Start record - the parser generates this record before evaluating the rule.

Examples:
personname.c.

Typedef Documentation

typedef long(* rpa_recordtree_callback)(rarray_t *records, long rec, rpointer userdata)

Tree walk callback

typedef struct rparecord_s rparecord_t

Abstract Syntax Tree (AST) construction element


Function Documentation

rparecord_t* rpa_record_get ( rarray_t *  records,
long  rec 
)

Return a pointer to a record at offset rec from the records array.

Parameters:
recordsAn array of records populated by a rpa_stat_parse operation.
recrecord offset.
rarray_t* rpa_records_create ( )

Create array for rparecord_t elements.

Returns:
Return empty array or NULL if creation failed.
Examples:
personname.c.
void rpa_records_destroy ( rarray_t *  records)

Destroy array created with rpa_records_create

Parameters:
recordsSpecifies the records array to be destroyed.
Examples:
personname.c.
long rpa_recordtree_copy ( rarray_t *  dst,
rarray_t *  src,
long  rec 
)

Copy a branch to another array.

Parameters:
dstDestination array.
srcSource array.
recSource branch.
Returns:
Return the number of records copied.
long rpa_recordtree_firstchild ( rarray_t *  records,
long  rec,
unsigned long  type 
)

Return the first child.

Parameters:
recordsarray of AST records.
recrecord offset in the records array.
typeThe type of the requested record.
Returns:
first child record of the specified type. If there are no children return -1.
long rpa_recordtree_get ( rarray_t *  records,
long  rec,
unsigned long  type 
)

Return record offset for the corresponding type. Use this function to locate the start or end record for a given record. For example if you have a record r1 (of type RPA_RECORD_START) marking the beginning of a branch and you need to locate its counterpart, you can do it with this function by specifying RPA_RECORD_END for type parameter. If the rec type is the same as the specified type parameter this function will return rec. In the example above: rpa_recordtree_get(records, 0, RPA_RECORD_END) will return 7. rpa_recordtree_get(records, 7, RPA_RECORD_START) will return 0. rpa_recordtree_get(records, 1, RPA_RECORD_END) will return 2. rpa_recordtree_get(records, 1, RPA_RECORD_START) will return 1.

Parameters:
recordsarray of AST records (populated by rpa_stat_parse in most cases).
recrecord offset in the records array.
typeThe type of the requested record.
Returns:
Return the counterpart record of rec if type is different than the rec type, or the specified rec if the type is the same as the rec type.
long rpa_recordtree_lastchild ( rarray_t *  records,
long  rec,
unsigned long  type 
)

Return the last child.

Parameters:
recordsarray of AST records.
recrecord offset in the records array.
typeThe type of the requested record.
Returns:
last child record of the specified type. If there are no children return -1.
long rpa_recordtree_next ( rarray_t *  records,
long  rec,
unsigned long  type 
)

Return the next sibling.

Parameters:
recordsarray of AST records.
recrecord offset in the records array.
typeThe type of the requested record.
Returns:
next sibling record of the specified type. If rec is the last sibling, return -1.
long rpa_recordtree_parent ( rarray_t *  records,
long  rec,
unsigned long  type 
)

Return the parent node.

Parameters:
recordsarray of AST records.
recrecord offset in the records array.
typeThe type of the requested record.
Returns:
Return the parent of rec. If rec has no parent return -1.
long rpa_recordtree_prev ( rarray_t *  records,
long  rec,
unsigned long  type 
)

Return the prev sibling.

Parameters:
recordsarray of AST records.
recrecord offset in the records array.
typeThe type of the requested record.
Returns:
previous sibling record of the specified type. If rec is the first sibling, return -1.
long rpa_recordtree_size ( rarray_t *  records,
long  rec 
)

Return the number of records in the branch specified by rec.

Parameters:
recordsarray of AST records.
recrecord offset in the records array.
Returns:
Return the records count in the specified branch.