2 * Copyright Droids Corporation (2007)
3 * Olivier MATZ <zer0@droids-corp.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Revision : $Id: parse.h,v 1.1.2.9 2009-01-03 16:25:13 zer0 Exp $
27 #include <aversive/pgmspace.h>
28 #include <aversive/types.h>
31 #define offsetof(type, field) ((size_t) &( ((type *)0)->field) )
34 #define PARSE_SUCCESS 0
35 #define PARSE_AMBIGUOUS -1
36 #define PARSE_NOMATCH -2
37 #define PARSE_BAD_ARGS -3
40 * Stores a pointer to the ops struct, and the offset: the place to
41 * write the parsed result in the destination structure.
44 struct token_ops *ops;
47 typedef struct token_hdr parse_token_hdr_t;
50 * A token is defined by this structure.
52 * parse() takes the token as first argument, then the source buffer
53 * starting at the token we want to parse. The 3rd arg is a pointer
54 * where we store the parsed data (as binary). It returns the number of
55 * parsed chars on success and a negative value on error.
57 * complete_get_nb() returns the number of possible values for this
58 * token if completion is possible. If it is NULL or if it returns 0,
59 * no completion is possible.
61 * complete_get_elt() copy in dstbuf (the size is specified in the
62 * parameter) the i-th possible completion for this token. returns 0
63 * on success or and a negative value on error.
65 * get_help() fills the dstbuf with the help for the token. It returns
66 * -1 on error and 0 on success.
69 /** parse(token ptr, buf, res pts) */
70 int8_t (*parse)(PGM_P, const char *, void *);
71 /** return the num of possible choices for this token */
72 int8_t (*complete_get_nb)(PGM_P);
73 /** return the elt x for this token (token, idx, dstbuf, size) */
74 int8_t (*complete_get_elt)(PGM_P, int8_t, char *, uint8_t);
75 /** get help for this token (token, dstbuf, size) */
76 int8_t (*get_help)(PGM_P, char *, uint8_t);
80 * Store a instruction, which is a pointer to a callback function and
81 * its parameter that is called when the instruction is parsed, a help
82 * string, and a list of token composing this instruction.
85 /* f(parsed_struct, data) */
86 void (*f)(void *, void *);
88 const char * help_str;
91 typedef struct inst parse_inst_t;
94 * A context is identified by its name, and contains a list of
98 typedef const parse_inst_t * parse_ctx_t;
101 * Try to parse a buffer according to the specified context. The
102 * argument buf must ends with "\n\0". The function returns
103 * PARSE_AMBIGUOUS, PARSE_NOMATCH or PARSE_BAD_ARGS on error. Else it
104 * calls the associated function (defined in the context) and returns
107 int8_t parse(PGM_P ctx, const char * buf);
110 * complete() must be called with *state==0.
111 * It returns < 0 on error.
114 * 2 on completion (one possible choice). In this case, the chars
115 * are appended in dst buffer.
116 * 1 if there is several possible choices. In this case, you must
117 * call the function again, keeping the value of state intact.
118 * 0 when the iteration is finished. The dst is not valid for this
121 * The returned dst buf ends with \0.
124 int8_t complete(PGM_P ctx, const char *buf, int16_t *state,
125 char *dst, uint8_t size);
128 /* true if(!c || iscomment(c) || isblank(c) || isendofline(c)) */
129 int isendoftoken(char c);
131 #endif /* _PARSE_H_ */