File: parser.h

package info (click to toggle)
mrd6 0.9.5-release-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,308 kB
  • ctags: 3,956
  • sloc: cpp: 25,728; perl: 462; makefile: 281; ansic: 142; sh: 67
file content (105 lines) | stat: -rw-r--r-- 2,485 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
/*
 * Multicast Routing Daemon (MRD)
 *   parser.h
 *
 * Copyright (C) 2004, 2005
 *   Universidade de Aveiro, Instituto Telecomunicacoes - Polo Aveiro
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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
 *
 * Authors:	Hugo Santos, <hsantos@av.it.pt>
 */

#ifndef _mrd_parser_h_
#define _mrd_parser_h_

#include <sys/types.h>

#include <map>
#include <vector>
#include <list>
#include <string>

class parser_context {
public:
	parser_context();
	parser_context(const char *input, bool partial = false);
	parser_context(const parser_context &);

	parser_context &operator = (const parser_context &);

	enum token_type {
		NONE,
		LCURLY, RCURLY,
		LPARENT, RPARENT,
		TERM,
		EQUAL,
		PLUSEQUAL,
		DOT, COMMA,

		/* identifier, integer, address, etc. */
		TOKEN,
		/* TOKEN directly followed by a '?' */
		PARTIAL_TOKEN,
		/* quoted string */
		STRING,
	};

	static const char *token_name(token_type);

	struct symbol {
		symbol();
		symbol(int, token_type, const std::string &);
		symbol(const symbol &);

		int line;
		token_type sym;
		std::string value;
	};

	int current_line_number() const { return m_current_line; }
	std::string current_line() const;
	const char *current_input() const { return m_input_pointer; }
	int current_column() const { return m_input_pointer - m_input_line_start; }

	// -1 parsing error
	//  0 no more symbols
	//  1 parsed with success
	int read();
	int eat();
	int eat(token_type);
	int eat(int, ...);

	const symbol &head() const { return m_current; }

private:
	int read_token(bool, int *, const char ** = 0, bool = true);

	int parse_one(const char *input, int state, int *sym, int *readnum) const;

	symbol m_current;
	int m_current_line;

	std::list<symbol> m_symq;

	const char *m_input;
	const char *m_input_pointer;
	const char *m_input_line_start;

	bool m_partial;
};

#endif