source: mainline/uspace/app/bdsh/tok.c@ 0662451

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0662451 was 0662451, checked in by Martin Sucha <sucha14@…>, 14 years ago

Extend bdsh tokenizer to include more information

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Sucha
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <str.h>
30#include <assert.h>
31#include <malloc.h>
32#include <stdlib.h>
33#include <errno.h>
34
35#include "tok.h"
36
37/* Forward declarations of static functions */
38static wchar_t tok_get_char(tokenizer_t *);
39static wchar_t tok_look_char(tokenizer_t *);
40static int tok_push_char(tokenizer_t *, wchar_t);
41static int tok_push_token(tokenizer_t *);
42static bool tok_pending_chars(tokenizer_t *);
43static int tok_finish_string(tokenizer_t *);
44static void tok_start_token(tokenizer_t *, token_type_t);
45
46/** Initialize the token parser
47 *
48 * @param tok the tokenizer structure to initialize
49 * @param input the input string to tokenize
50 * @param out_tokens array of strings where to store the result
51 * @param max_tokens number of elements of the out_tokens array
52 */
53int tok_init(tokenizer_t *tok, char *input, token_t *out_tokens,
54 size_t max_tokens)
55{
56 tok->in = input;
57 tok->in_offset = 0;
58 tok->last_in_offset = 0;
59 tok->in_char_offset = 0;
60 tok->last_in_char_offset = 0;
61
62 tok->outtok = out_tokens;
63 tok->outtok_offset = 0;
64 tok->outtok_size = max_tokens;
65
66 /* Prepare a buffer where all the token strings will be stored */
67 size_t len = str_size(input) + max_tokens + 1;
68 char *tmp = malloc(len);
69
70 if (tmp == NULL) {
71 return ENOMEM;
72 }
73
74 tok->outbuf = tmp;
75 tok->outbuf_offset = 0;
76 tok->outbuf_size = len;
77 tok->outbuf_last_start = 0;
78
79 return EOK;
80}
81
82/** Finalize the token parser */
83void tok_fini(tokenizer_t *tok)
84{
85 if (tok->outbuf != NULL) {
86 free(tok->outbuf);
87 }
88}
89
90/** Tokenize the input string into the tokens */
91int tok_tokenize(tokenizer_t *tok, size_t *tokens_length)
92{
93 int rc;
94 wchar_t cur_char;
95
96 /* Read the input line char by char and append tokens */
97 while ((cur_char = tok_get_char(tok)) != 0) {
98 if (cur_char == ' ') {
99 /* Push the token if there is any.
100 * There may not be any pending char for a token in case
101 * there are several spaces in the input.
102 */
103 if (tok_pending_chars(tok)) {
104 rc = tok_push_token(tok);
105 if (rc != EOK) {
106 return rc;
107 }
108 }
109 tok_start_token(tok, TOKTYPE_SPACE);
110 /* Eat all spaces */
111 while (tok_look_char(tok) == ' ') {
112 tok_push_char(tok, tok_get_char(tok));
113 }
114 tok_push_token(tok);
115
116 }
117 else if (cur_char == '|') {
118 /* Pipes are tokens that are delimiters and should be output
119 * as a separate token
120 */
121 if (tok_pending_chars(tok)) {
122 rc = tok_push_token(tok);
123 if (rc != EOK) {
124 return rc;
125 }
126 }
127
128 tok_start_token(tok, TOKTYPE_PIPE);
129
130 rc = tok_push_char(tok, '|');
131 if (rc != EOK) {
132 return rc;
133 }
134
135 rc = tok_push_token(tok);
136 if (rc != EOK) {
137 return rc;
138 }
139 }
140 else if (cur_char == '\'') {
141 /* A string starts with a quote (') and ends again with a quote.
142 * A literal quote is written as ''
143 */
144 tok_start_token(tok, TOKTYPE_TEXT);
145 rc = tok_finish_string(tok);
146 if (rc != EOK) {
147 return rc;
148 }
149 }
150 else {
151 if (!tok_pending_chars(tok)) {
152 tok_start_token(tok, TOKTYPE_TEXT);
153 }
154 /* If we are handling any other character, just append it to
155 * the current token.
156 */
157 rc = tok_push_char(tok, cur_char);
158 if (rc != EOK) {
159 return rc;
160 }
161 }
162 }
163
164 /* Push the last token */
165 if (tok_pending_chars(tok)) {
166 rc = tok_push_token(tok);
167 if (rc != EOK) {
168 return rc;
169 }
170 }
171
172 *tokens_length = tok->outtok_offset;
173
174 return EOK;
175}
176
177/** Finish tokenizing an opened string */
178int tok_finish_string(tokenizer_t *tok)
179{
180 int rc;
181 wchar_t cur_char;
182
183 while ((cur_char = tok_get_char(tok)) != 0) {
184 if (cur_char == '\'') {
185 if (tok_look_char(tok) == '\'') {
186 /* Encode a single literal quote */
187 rc = tok_push_char(tok, '\'');
188 if (rc != EOK) {
189 return rc;
190 }
191
192 /* Swallow the additional one in the input */
193 tok_get_char(tok);
194 }
195 else {
196 /* The string end */
197 return tok_push_token(tok);
198 }
199 }
200 else {
201 rc = tok_push_char(tok, cur_char);
202 if (rc != EOK) {
203 return rc;
204 }
205 }
206 }
207
208 /* If we are here, the string run to the end without being closed */
209 return EINVAL;
210}
211
212/** Get a char from input, advancing the input position */
213wchar_t tok_get_char(tokenizer_t *tok)
214{
215 tok->in_char_offset++;
216 return str_decode(tok->in, &tok->in_offset, STR_NO_LIMIT);
217}
218
219/** Get a char from input, while staying on the same input position */
220wchar_t tok_look_char(tokenizer_t *tok)
221{
222 unsigned int old_offset = tok->in_offset;
223 unsigned int old_char_offset = tok->in_char_offset;
224 wchar_t ret = tok_get_char(tok);
225 tok->in_offset = old_offset;
226 tok->in_char_offset = old_char_offset;
227 return ret;
228}
229
230/** Append a char to the end of the current token */
231int tok_push_char(tokenizer_t *tok, wchar_t ch)
232{
233 return chr_encode(ch, tok->outbuf, &tok->outbuf_offset, tok->outbuf_size);
234}
235
236void tok_start_token(tokenizer_t *tok, token_type_t type)
237{
238 tok->current_type = type;
239}
240
241/** Push the current token to the output array */
242int tok_push_token(tokenizer_t *tok)
243{
244 if (tok->outtok_offset >= tok->outtok_size) {
245 return EOVERFLOW;
246 }
247
248 if (tok->outbuf_offset >= tok->outbuf_size) {
249 return EOVERFLOW;
250 }
251
252 tok->outbuf[tok->outbuf_offset++] = 0;
253 token_t *tokinfo = &tok->outtok[tok->outtok_offset++];
254 tokinfo->type = tok->current_type;
255 tokinfo->text = tok->outbuf + tok->outbuf_last_start;
256 tokinfo->byte_start = tok->last_in_offset;
257 tokinfo->byte_length = tok->in_offset - tok->last_in_offset - 1;
258 tokinfo->char_start = tok->last_in_char_offset;
259 tokinfo->char_length = tok->in_char_offset - tok->last_in_char_offset
260 - 1;
261 tok->outbuf_last_start = tok->outbuf_offset;
262
263 /* We have consumed the first char of the next token already */
264 tok->last_in_offset = tok->in_offset-1;
265 tok->last_in_char_offset = tok->in_char_offset-1;
266
267 return EOK;
268}
269
270/** Return true, if the current token is not empty */
271bool tok_pending_chars(tokenizer_t *tok)
272{
273 assert(tok->outbuf_offset >= tok->outbuf_last_start);
274 return (tok->outbuf_offset != tok->outbuf_last_start);
275}
Note: See TracBrowser for help on using the repository browser.