File: parser.cpp

package info (click to toggle)
teg 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,036 kB
  • sloc: cpp: 16,819; xml: 1,313; makefile: 268; sh: 195; ansic: 112
file content (185 lines) | stat: -rw-r--r-- 4,372 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2000 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <rquesada@core-sdi.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; only version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/*
 *	IN:	PPARSER  (->data tiene la cadena a parsear )
 *	OUT:	PPARSER->data = Puntero a la data
 *		PPARSER->token = 1er token
 *		PPARSER->valor = su valor
 *		PPARSER->hay_otro = si hay mas datos para leer
 *
 *	Separadores:  son pasados por PDELIM igualador
 *	Igualadores:  son pasados por PDELIM separador
 *	Fin: asciiz o \n o \r
 * Ejemplos validos:
 *	"Quiero un nuevo mundo=TRUE;Me gusta Linux=Si"
 *	"Hola;Como;Te;Va"
 * Ejemplos no validos:
 *	"Hola=343=534"
*/

#include "parser.h"
#include "parser_private.h"

#include <cstdlib>
#include <cstring>

bool parser_belongs_to_class(char ch, DELIM const& which)
{
	return which.valid && (which.accept == ch);
}

enum CharClass parser_character_class(char a,
                                      DELIM const& igualador,
                                      DELIM const& separador)
{
	for(char const ch: "\n\r") {
		if(ch == a) {
			return ccEnd;
		}
	}
	if(parser_belongs_to_class(a, igualador)) {
		return ccEquals;
	}
	if(parser_belongs_to_class(a, separador)) {
		return ccSeparators;
	}

	return ccData;
}

PARSER_VALUE parser_analyze_token(int *pos, char const *in, char *out,
                                  DELIM const& equals, DELIM const& separators, int maxlen)
{
	PARSER_VALUE pval=PARSER_DATA;
	int i, j;
	bool in_escape = false;

	// Analyzing the input string, copy data characters obeying escaped values.
	for(i=0, j=0; i<maxlen; i++) {
		if(in[i] == '"') {
			in_escape = !in_escape;
			continue;
		}

		if(!in_escape) {
			pval=(PARSER_VALUE)parser_character_class(in[i], equals, separators);
			if(pval != PARSER_DATA) {
				break;
			}
		}

		out[j++]=in[i];
	}
	out[j]=0;

	// Check if the input end was reached without a terminating symbol
	if(i==maxlen) {
		return PARSER_ERROR;
	}

	*pos=i; // signal the new input position to the caller
	return pval;
}

/* Unica funcion exportable */
bool PARSER::parse()
{
	PARSER *const p_in{this};
	PARSER_VALUE pval;
	int i;

	if(!can_continue()) {
		return false;
	}

	if((pval=parser_analyze_token(&i, p_in->data, p_in->token,
	                              p_in->equals, p_in->separators,
	                              PARSER_TOKEN_MAX)) == PARSER_ERROR) {
		return false;
	}

	p_in->value[0]=0;

	switch(pval) {
	case PARSER_FIN:
		p_in->data=NULL;
		p_in->m_can_continue = false;
		return true;

	case PARSER_SEPARADOR:
		p_in->data=&p_in->data[i+1];
		p_in->m_can_continue = true;
		return true;

	case PARSER_IGUAL: {
		int j;
		pval = parser_analyze_token(&j, &p_in->data[i+1], p_in->value,
		                            DELIM{.valid=false}, p_in->separators,
		                            PARSER_VALUE_MAX);

		if(pval==PARSER_IGUAL || pval==PARSER_ERROR) {
			return false;
		}

		if(pval==PARSER_SEPARADOR) {
			p_in->data = &p_in->data[j+1 + i+1];
			p_in->m_can_continue = true;
		} else { /* PARSER_FIN */
			p_in->data = NULL;
			p_in->m_can_continue = false;
		}
		return true;
	}
	default:
		return false;
	}
}

PARSER& operator >> (PARSER& source, int &value)
{
	if(source.parse_token()) {
		value=std::atoi(source.token);
	}
	return source;
}

PARSER& operator >> (PARSER& source, Limited&& limit)
{
	int dest;
	source >> dest;
	if(source.ok() && (dest <= limit.max) && (dest >= limit.min)) {
		limit.dest = dest;
	} else {
		source.fail();
		limit.dest = limit.reset_to;
	}
	return source;
}

PARSER& operator >> (PARSER& source, CString&& dest)
{
	if(source.ok() && source.parse_token()) {
		if(dest.count > 0) {
			strncpy(dest.dest, source.token, dest.count);
			dest.dest[dest.count-1] = 0;
		}
	}
	return source;
}