summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/promela/PromelaParser.h
blob: 0cca36544d99d9a72b70cec0192506dced958346 (plain)
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
// bison -v -d promela-expr.ypp && flex promela-expr.l
// bison promela-expr.ypp && flex promela-expr.l

#ifndef PROMELA_H_9AB78YB1
#define PROMELA_H_9AB78YB1

#include <stdlib.h>
#include <stdarg.h>

#include "uscxml/Message.h"

#define GRAMMAR_COMMON(name, uc_name) \
struct yy_buffer_state; \
typedef yy_buffer_state *YY_BUFFER_STATE; \
extern YY_BUFFER_STATE promela_##name##__scan_buffer(char *, size_t, void*); \
extern int promela_##name##_lex (PROMELA_##uc_name##_STYPE* yylval_param, void* yyscanner); \
int promela_##name##_lex_init (void**); \
int promela_##name##_lex_destroy (void*); \

namespace uscxml {

class PromelaParser;
	
struct PromelaParserNode {
	PromelaParserNode() : type(0) {}
	int type;
	std::string value;
	std::list<PromelaParserNode*> operands;
	
	void merge(PromelaParserNode* node) {
		for (std::list<PromelaParserNode*>::iterator iter = node->operands.begin();
				 iter != node->operands.end(); iter++) {
			operands.push_back(*iter);
		}
	}

	void push(PromelaParserNode* node) {
		operands.push_back(node);
	}

	void dump(int indent = 0) {
		std::string padding;
		for (int i = 0; i < indent; i++) {
			padding += "  ";
		}
		std::cout << padding << typeToDesc(type) << ": " << value << std::endl;
		for (std::list<PromelaParserNode*>::iterator iter = operands.begin();
				 iter != operands.end(); iter++) {
			(*iter)->dump(indent + 1);
		}
	}
	
	static std::string typeToDesc(int type);

};

class PromelaParser {
public:
	enum Type {
		PROMELA_EXPR,
		PROMELA_DECL,
		PROMELA_STMNT
	};
	
	static std::string typeToDesc(int type);

	PromelaParser(const std::string& expr);
	PromelaParser(const std::string& expr, Type expectedType);
	virtual ~PromelaParser();
	
	virtual PromelaParserNode* node(int type, int nrArgs, ...) {
		PromelaParserNode* newNode = new PromelaParserNode();
		newNode->type = type;
		va_list ap;
    va_start(ap, nrArgs);
    for(int i = 1; i <= nrArgs; i++) {
			newNode->operands.push_back(va_arg(ap, PromelaParserNode*));
		}
		return newNode;
	}

	virtual PromelaParserNode* value(int type, const char* value) {
		PromelaParserNode* newNode = new PromelaParserNode();
		newNode->value = value;
		newNode->type = type;
		return newNode;
	}

	void dump() {
		switch (type) {
		case PROMELA_EXPR:
			std::cout << "Promela Expression" << std::endl;
			break;
		case PROMELA_DECL:
			std::cout << "Promela Declarations" << std::endl;
			break;
		case PROMELA_STMNT:
			std::cout << "Promela Statement" << std::endl;
			break;
		}
		ast->dump();
	}
	
	PromelaParserNode* ast;
	Type type;

protected:
	
	void init(const std::string& expr);
	
	void* scanner;
	char* input;
	size_t input_length;
};

}

void promela_error (uscxml::PromelaParser* ctx, void* yyscanner, const char* err);

#endif /* end of include guard: PROMELA_H_9AB78YB1 */