diff options
author | Mike Buland <eichlan@xagasoft.com> | 2010-10-17 05:50:57 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2010-10-17 05:50:57 +0000 |
commit | 00ecd458ced768b3b8752cdd421a22244b7adc01 (patch) | |
tree | a0b2ad00db46b61b269be6090a386cb88e6bf7f2 /src/parser.cpp | |
parent | 9031e2af7dd4e65ec70890ee78a7cf600d1b2cc5 (diff) | |
download | libbu++-00ecd458ced768b3b8752cdd421a22244b7adc01.tar.gz libbu++-00ecd458ced768b3b8752cdd421a22244b7adc01.tar.bz2 libbu++-00ecd458ced768b3b8752cdd421a22244b7adc01.tar.xz libbu++-00ecd458ced768b3b8752cdd421a22244b7adc01.zip |
Hey! The parser parses now! It's actually a little stupid, I didn't implement
lookahead or precedence, but I should be able to do that easily with the next
version. I'm treating this more as a proof of concept than a real working
model. Although it can handle +, -, (), and = :)
Diffstat (limited to 'src/parser.cpp')
-rw-r--r-- | src/parser.cpp | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/parser.cpp b/src/parser.cpp index e4e8ae5..3ce73b6 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 | ||
25 | Lexer::Token *Bu::Parser::popToken() | ||
26 | { | ||
27 | return sToken.peekPop(); | ||
28 | } | ||
29 | |||
30 | void Bu::Parser::pushToken( Lexer::Token *pTok ) | ||
31 | { | ||
32 | sToken.push( pTok ); | ||
33 | } | ||
34 | |||
25 | void Bu::Parser::parse() | 35 | void 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; |