summaryrefslogtreecommitdiff
path: root/src/parser.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/parser.cpp34
1 files changed, 29 insertions, 5 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
index e4e8ae5..4ad4ff9 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -22,16 +22,25 @@ void Bu::Parser::popLexer()
22 delete sLexer.peekPop(); 22 delete sLexer.peekPop();
23} 23}
24 24
25Lexer::Token *Bu::Parser::popToken()
26{
27 return sToken.peekPop();
28}
29
30void Bu::Parser::pushToken( Lexer::Token *pTok )
31{
32 sToken.push( pTok );
33}
34
25void Bu::Parser::parse() 35void Bu::Parser::parse()
26{ 36{
27 int iCurNt = iRootNonTerminal; 37 int iCurNt = iRootNonTerminal;
28 Lexer::Token *ptCur = sLexer.peek()->nextToken(); 38 Lexer::Token *ptCur = sLexer.peek()->nextToken();
29 sio << "Token: " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; 39 sio << "Token(a): " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl;
30 selectProduction( iCurNt, ptCur ); 40 selectProduction( iCurNt, ptCur );
31 41
32 while( !sState.isEmpty() ) 42 while( !sState.isEmpty() )
33 { 43 {
34 sio << "Currently: " << *sState.peek() << sio.nl;
35 switch( (*sState.peek()).eType ) 44 switch( (*sState.peek()).eType )
36 { 45 {
37 case State::typeTerminal: 46 case State::typeTerminal:
@@ -42,7 +51,7 @@ void Bu::Parser::parse()
42 advanceState(); 51 advanceState();
43 delete ptCur; 52 delete ptCur;
44 ptCur = sLexer.peek()->nextToken(); 53 ptCur = sLexer.peek()->nextToken();
45 sio << "Token: " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; 54 sio << "Token(b): " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl;
46 } 55 }
47 else 56 else
48 { 57 {
@@ -59,7 +68,7 @@ void Bu::Parser::parse()
59 sToken.push( ptCur ); 68 sToken.push( ptCur );
60 69
61 ptCur = sLexer.peek()->nextToken(); 70 ptCur = sLexer.peek()->nextToken();
62 sio << "Token: " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; 71 sio << "Token(c): " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl;
63 } 72 }
64 else 73 else
65 { 74 {
@@ -72,7 +81,7 @@ void Bu::Parser::parse()
72 << (*sState.peek()).iIndex << sio.nl; 81 << (*sState.peek()).iIndex << sio.nl;
73 { 82 {
74 int iNt = (*sState.peek()).iIndex; 83 int iNt = (*sState.peek()).iIndex;
75 advanceState(); 84 sio << "Current state: " << *sState.peek() << sio.nl;
76 if( !selectProduction( iNt, ptCur ) ) 85 if( !selectProduction( iNt, ptCur ) )
77 { 86 {
78 throw Bu::ExceptionBase("Error parsing code."); 87 throw Bu::ExceptionBase("Error parsing code.");
@@ -132,7 +141,11 @@ bool Bu::Parser::selectProduction( int iNt, Lexer::Token *ptCur )
132 } 141 }
133 } 142 }
134 if( nt.bCanSkip ) 143 if( nt.bCanSkip )
144 {
145 sio << "Nothing matches, skipping non-terminal." << sio.nl;
146 advanceState();
135 return true; 147 return true;
148 }
136 sio << "-->(Found nothing)" << sio.nl; 149 sio << "-->(Found nothing)" << sio.nl;
137 return false; 150 return false;
138} 151}
@@ -148,6 +161,7 @@ void Bu::Parser::advanceState()
148 sio.decIndent(); 161 sio.decIndent();
149 sState.pop(); 162 sState.pop();
150 sio << "State advanced, End of production." << sio.nl; 163 sio << "State advanced, End of production." << sio.nl;
164 advanceState();
151 return; 165 return;
152 } 166 }
153 sio << "State advanced, now: " << *(sState.peek()) << sio.nl; 167 sio << "State advanced, now: " << *(sState.peek()) << sio.nl;
@@ -191,6 +205,11 @@ int Bu::Parser::getNonTerminalId( const Bu::FString &sName )
191 return hNonTerminalName.get( sName ); 205 return hNonTerminalName.get( sName );
192} 206}
193 207
208bool Bu::Parser::hasNonTerminal( const Bu::FString &sName )
209{
210 return hNonTerminalName.has( sName );
211}
212
194int Bu::Parser::addReduction( const Bu::FString &sName, const Reduction &r ) 213int Bu::Parser::addReduction( const Bu::FString &sName, const Reduction &r )
195{ 214{
196 int iId = aReduction.getSize(); 215 int iId = aReduction.getSize();
@@ -217,6 +236,11 @@ int Bu::Parser::getReductionId( const Bu::FString &sName )
217 return hReductionName.get( sName ); 236 return hReductionName.get( sName );
218} 237}
219 238
239bool Bu::Parser::hasReduction( const Bu::FString &sName )
240{
241 return hReductionName.has( sName );
242}
243
220// 244//
221// Bu::Parser::State 245// Bu::Parser::State
222// 246//