summaryrefslogtreecommitdiff
path: root/src/parser.cpp
blob: e8e8ff2c40e4550352773d30dea6c02d0d3ac1dc (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
#include "bu/parser.h"
#include "bu/lexer.h"

#include "bu/sio.h"
using namespace Bu;

Bu::Parser::Parser()
{
}

Bu::Parser::~Parser()
{
}

void Bu::Parser::pushLexer( Lexer *pLex )
{
	sLexer.push( pLex );
}

void Bu::Parser::popLexer()
{
	delete sLexer.peekPop();
}

void Bu::Parser::parse()
{
	for(;;)
	{
		Bu::Lexer::Token *pToken = sLexer.peek()->nextToken();
		sio << sLexer.peek()->tokenToString( *pToken ) << sio.nl;
		if( pToken->iToken < 0 )
		{
			delete sLexer.peekPop();
			if( sLexer.isEmpty() )
			{
				delete pToken;
				return;
			}
		}
		delete pToken;
	}	
}

void Bu::Parser::setRootNonTerminal( int iRoot )
{
	iRootNonTerminal = iRoot;
}

void Bu::Parser::setRootNonTerminal( const Bu::FString &sRoot )
{
	setRootNonTerminal( hNonTerminalName.get( sRoot ) );
}

int Bu::Parser::addNonTerminal( const Bu::FString &sName, NonTerminal &nt )
{
	int iId = aNonTerminal.getSize();
	aNonTerminal.append( nt );
	hNonTerminalName.insert( sName, iId );
	return iId;
}

int Bu::Parser::addNonTerminal( const Bu::FString &sName )
{
	int iId = aNonTerminal.getSize();
	aNonTerminal.append( NonTerminal() );
	hNonTerminalName.insert( sName, iId );
	return iId;
}

void Bu::Parser::setNonTerminal( const Bu::FString &sName, NonTerminal &nt )
{
	aNonTerminal[hNonTerminalName.get(sName)] = nt;
}

int Bu::Parser::getNonTerminalId( const Bu::FString &sName )
{
	return hNonTerminalName.get( sName );
}

int Bu::Parser::addReduction( const Bu::FString &sName, const Reduction &r )
{
	int iId = aReduction.getSize();
	aReduction.append( r );
	hReductionName.insert( sName, iId );
	return iId;
}

int Bu::Parser::addReduction( const Bu::FString &sName )
{
	int iId = aReduction.getSize();
	aReduction.append( Reduction() );
	hReductionName.insert( sName, iId );
	return iId;
}

void Bu::Parser::setReduction( const Bu::FString &sName, const Reduction &r )
{
	aReduction[hReductionName.get(sName)] = r;
}

int Bu::Parser::getReductionId( const Bu::FString &sName )
{
	return hReductionName.get( sName );
}

//
// Bu::Parser::State
//

Bu::Parser::State::State( Bu::Parser::State::Type eType, int iIndex ) :
	eType( eType ),
	iIndex( iIndex )
{
}

Bu::Parser::State::~State()
{
}

//
// Bu::Parser::NonTerminal
//

Bu::Parser::NonTerminal::NonTerminal()
{
}

Bu::Parser::NonTerminal::~NonTerminal()
{
}

void Bu::Parser::NonTerminal::addProduction( Production p )
{
	lProduction.append( p );
}