Previous: Feeding, Up: User manual [Contents][Index]
The external interface specifies how your program invokes the pacc parser. The internal interface has considerations for the snippets of C code that appear in a ‘.pacc’ file, both in the preamble, and the value expressions of parsing rules.
For a parser with a return type of ‘result’, the external functions available (and defined in the header file written with the ‘-d’ flag) will look like this:
#define PACC_TYPE /* type of start rule */ struct pacc_parser; int pacc_trace; struct pacc_parser *pacc_new(void); void pacc_input(struct pacc_parser *, char *input, size_t length); void pacc_destroy(struct pacc_parser *); int pacc_parse(struct pacc_parser *); PACC_TYPE pacc_result(struct pacc_parser *); char *pacc_error(struct pacc_parser *); char *pacc_pos(struct pacc_parser *, const char *); int pacc_wrap(const char *, char *, size_t, PACC_TYPE *result);
The ‘pacc’ prefix on each function name is the default. You can specify a different prefix with the ‘--name’ option. The structure will still be called ‘pacc_parser’ though.
The interface is an object-oriented one:
struct pacc_parser *pacc_new(void)returns a new parser object.
void pacc_input(struct pacc_parser *p, char *input, size_t length)prepares the parser to parse the given ‘input’ of given ‘length’.
int pacc_parse(struct pacc_parser *p)performs the parse, returning ‘1’ if it was successful, ‘0’ otherwise.
char *pacc_error(struct pacc_parser *p)if pacc_parse() returned ‘0’, returns a newly-allocated
string describing the error that the parser found.
char *pacc_pos(struct pacc_parser *p, const char *s)returns a newly allocated string containing the coordinates of the furthest error found, e.g. ‘3:17: ’, followed by ‘s’.
PACC_TYPE pacc_result(struct pacc_parser *p)if pacc_parse() returned ‘1’, returns the result of the parse.
void pacc_destroy(pacc_parser *p)destroys the parser.
int pacc_wrap(const char *n, char *in, size_t l, PACC_TYPE *result_pointer)is a convenience function that creates a parser and invokes it on the
given input ‘in’ of length ‘l’. If the input was successfully
parsed, the result is evaluated into result_pointer and ‘1’
is returned. Otherwise, the parse error is written to stderr
prefixed by ‘name’, and ‘0’ is returned. In either case, the
parser is destroyed.
There are some utility functions defined by the parser engine that you may use in the C code snippets in a ‘.pacc’ file.
int PACC_LINEint PACC_COLare a pair of macros that return ints, representing the line
number and column of the first character matched by the current rule.
void pacc_nomem(void)prints the message ‘pacc: panic: out of memory’ to stderr,
and exits. The parser engine calls pacc_nomem() whenever a call
to realloc() fails.
void pacc_panic(const char *error)prints ‘pacc: panic: ’ followed by error to stderr
and exits.
Within the C code snippets in a ‘.pacc’ file, you must avoid using names that are used by the parser engine itself. All names beginning with ‘pacc_’, ‘PACC_’ and ‘_pacc_’ are reserved to the parser engine.
In addition, the name ‘ref’, and all names beginning ‘ref_’ are reserved.
Bug: currently the parser engine uses some other names, such as ‘cur’ and ‘_status’.
All allocation of memory done by the pacc parser engine uses only
realloc() and free(). Thus, an alternative memory manager
can be used with ‘#define realloc(x,s) ... #define free(x) ...’ in
the preamble.
Previous: Feeding, Up: User manual [Contents][Index]
Last updated: 2016-08-03 21:39:50 UTC
One thing pacc needs is more users. And, perhaps, one way to get more users is to reduce the friction in getting started with pacc. An obvious lubricant is packaging. Read More...
Looking at _pacc_coords(), I noticed that it seemed to have the same realloc() bug that I'd just fixed in _pacc_result(). However, the "list of arrays" trick really wasn't going to work here. Read More...