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;
49 struct token_hdr_pgm {
50 struct token_ops *ops;
53 typedef struct token_hdr_pgm parse_pgm_token_hdr_t;
56 * A token is defined by this structure.
58 * parse() takes the token as first argument, then the source buffer
59 * starting at the token we want to parse. The 3rd arg is a pointer
60 * where we store the parsed data (as binary). It returns the number of
61 * parsed chars on success and a negative value on error.
63 * complete_get_nb() returns the number of possible values for this
64 * token if completion is possible. If it is NULL or if it returns 0,
65 * no completion is possible.
67 * complete_get_elt() copy in dstbuf (the size is specified in the
68 * parameter) the i-th possible completion for this token. returns 0
69 * on success or and a negative value on error.
71 * get_help() fills the dstbuf with the help for the token. It returns
72 * -1 on error and 0 on success.
75 /** parse(token ptr, buf, res pts) */
76 int8_t (*parse)(parse_pgm_token_hdr_t *, const char *, void *);
77 /** return the num of possible choices for this token */
78 int8_t (*complete_get_nb)(parse_pgm_token_hdr_t *);
79 /** return the elt x for this token (token, idx, dstbuf, size) */
80 int8_t (*complete_get_elt)(parse_pgm_token_hdr_t *, int8_t, char *, uint8_t);
81 /** get help for this token (token, dstbuf, size) */
82 int8_t (*get_help)(parse_pgm_token_hdr_t *, char *, uint8_t);
86 * Store a instruction, which is a pointer to a callback function and
87 * its parameter that is called when the instruction is parsed, a help
88 * string, and a list of token composing this instruction.
91 /* f(parsed_struct, data) */
92 void (*f)(void *, void *);
97 typedef struct inst parse_inst_t;
99 /* f(parsed_struct, data) */
100 void (*f)(void *, void *);
103 prog_void * tokens[];
105 typedef struct inst_pgm parse_pgm_inst_t;
108 * A context is identified by its name, and contains a list of
112 typedef parse_pgm_inst_t * parse_ctx_t;
113 typedef PROGMEM parse_ctx_t parse_pgm_ctx_t;
116 * Try to parse a buffer according to the specified context. The
117 * argument buf must ends with "\n\0". The function returns
118 * PARSE_AMBIGUOUS, PARSE_NOMATCH or PARSE_BAD_ARGS on error. Else it
119 * calls the associated function (defined in the context) and returns
122 int8_t parse(parse_pgm_ctx_t ctx[], const char * buf);
125 * complete() must be called with *state==0.
126 * It returns < 0 on error.
129 * 2 on completion (one possible choice). In this case, the chars
130 * are appended in dst buffer.
131 * 1 if there is several possible choices. In this case, you must
132 * call the function again, keeping the value of state intact.
133 * 0 when the iteration is finished. The dst is not valid for this
136 * The returned dst buf ends with \0.
139 int8_t complete(parse_pgm_ctx_t ctx[], const char *buf, int16_t *state,
140 char *dst, uint8_t size);
143 /* true if(!c || iscomment(c) || isblank(c) || isendofline(c)) */
144 int isendoftoken(char c);
146 #endif /* _PARSE_H_ */