aboutsummaryrefslogtreecommitdiff
path: root/src/builder.h
blob: d7c08913900f90a80a64f51e3a4a00f0c911be0b (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef BUILDER_H
#define BUILDER_H

#include <string>
#include <list>
#include <map>
#include "build.tab.h"
#include "exceptionbase.h"
#include "staticstring.h"
#include "regexp.h"

subExceptionDecl( BuildException )

class Builder;
class Action;
class Command;
class Rule;
class Target;

#define YY_DECL int yylex( YYSTYPE *yylval_param, YYLTYPE *yylloc_param, Builder &bld )
YY_DECL;

class Builder
{
	struct ltstr
	{
		bool operator()(const char* s1, const char* s2) const
		{
			return strcmp(s1, s2) < 0;
		}
	};

public:
	Builder();
	virtual ~Builder();

	void load( const char *sFN );
	void build( const char *sAct=NULL );
	void execute( Action *pAct );

	//void error( const yy::location &l, const std::string &m );
	//void error( const std::string &m );

	std::string file;

	void add( Action *pAct );
	void add( Command *pCmd );
	void add( Rule *pRule );
	void add( Target *pTarg );
	void varSet( const char *sName, const char *sValue );
	void varAddSet( const char *sName, const char *sValue );
	Rule *getRule( const char *sName );
	std::list<Rule *> findRuleChain( Rule *pRule );
	void processRequires( std::list<std::string> &lInput );
	void requires( const char *sBase, const char *sReq );
	void requiresFromCommand( const char *sBase, const char *sReq );
	void requiresRegexp( bool on )
	{
		bReqRegexp = on;
	}
	bool isRequiresRegexp()
	{
		return bReqRegexp;
	}
	void setContext( const char *sCont );
	void setContext();

	bool hasDefaultAction()
	{
		return pDefaultAction != NULL;
	}

	void debug();

	Rule *lastRule()
	{
		return pLastAddedRule;
	}

	Target *lastTarget()
	{
		return pLastAddedTarget;
	}

	void setTmp( const char *s )
	{
		sTmp = s;
	}

	const char *getTmp()
	{
		return sTmp;
	}

	Target *getTarget( const char *sName )
	{
		if( mTarget.find( sName ) == mTarget.end() )
			throw BuildException("Target %s not found.", sName );

		return mTarget[sName];
	}

private:
	typedef std::map<std::string, std::string> varmap;

	void requiresNormal( const char *sBase, const char *sReq );
	void requiresRegexp( const char *sBase, const char *sReq );
	void checkVar( const char *cont, const char *sName );
	void scanBegin();
	void scanEnd();
	varmap *regexVars( RegExp *re );

	bool hasVar( varmap *pMap, std::string &var );
	std::string varRepl( const char *sSrc, const char *cont, varmap *mExtra );

	Action *pDefaultAction;
	Action *pLastAddedAction;
	std::map<const char *, Action *, ltstr> mAction;

	Rule *pLastAddedRule;
	std::map<const char *, Rule *, ltstr> mRule;

	Target *pLastAddedTarget;
	std::map<const char *, Target *, ltstr> mTarget;

	varmap mVar;

	std::map<std::string, std::list<std::string> *> mRequires;

	typedef std::list<std::pair<RegExp *, std::string> > regreqlist;
	regreqlist lRequiresRegexp;
	regreqlist lRequiresRegexpCommand;

	std::map<std::string, varmap> mContVar;
	StaticString sContext;

	StaticString sTmp;

	bool bReqRegexp;
};

#endif