diff options
Diffstat (limited to '')
-rw-r--r-- | src/experimental/parser.h | 218 |
1 files changed, 109 insertions, 109 deletions
diff --git a/src/experimental/parser.h b/src/experimental/parser.h index 953202d..50b6afb 100644 --- a/src/experimental/parser.h +++ b/src/experimental/parser.h | |||
@@ -18,115 +18,115 @@ | |||
18 | 18 | ||
19 | namespace Bu | 19 | namespace Bu |
20 | { | 20 | { |
21 | /** | 21 | /** |
22 | * The base framework for a LR(1) grammar parser. Provided a proper set of | 22 | * The base framework for a LR(1) grammar parser. Provided a proper set of |
23 | * ParserStates this will prase any input the lexer can provide. | 23 | * ParserStates this will prase any input the lexer can provide. |
24 | */ | 24 | */ |
25 | class Parser | 25 | class Parser |
26 | { | 26 | { |
27 | public: | 27 | public: |
28 | Parser(); | 28 | Parser(); |
29 | virtual ~Parser(); | 29 | virtual ~Parser(); |
30 | 30 | ||
31 | /** | 31 | /** |
32 | * When a Lexer is pushed onto the stack it becomes the source for | 32 | * When a Lexer is pushed onto the stack it becomes the source for |
33 | * future tokens read by the parser until it is popped off the stack. | 33 | * future tokens read by the parser until it is popped off the stack. |
34 | * The Parser takes ownership of every Lexer pushed onto the stack, | 34 | * The Parser takes ownership of every Lexer pushed onto the stack, |
35 | * and will delete it when it is popped off the stack. | 35 | * and will delete it when it is popped off the stack. |
36 | */ | 36 | */ |
37 | void pushLexer( Lexer *pLex ); | 37 | void pushLexer( Lexer *pLex ); |
38 | 38 | ||
39 | /** | 39 | /** |
40 | * Pop a lexer off the stack, and delete it. | 40 | * Pop a lexer off the stack, and delete it. |
41 | */ | 41 | */ |
42 | void popLexer(); | 42 | void popLexer(); |
43 | 43 | ||
44 | Lexer::Token *popToken(); | 44 | Lexer::Token *popToken(); |
45 | void pushToken( Lexer::Token *pTok ); | 45 | void pushToken( Lexer::Token *pTok ); |
46 | 46 | ||
47 | /** | 47 | /** |
48 | * Execute a parse. | 48 | * Execute a parse. |
49 | */ | 49 | */ |
50 | void parse(); | 50 | void parse(); |
51 | 51 | ||
52 | void setRootNonTerminal( int iRoot ); | 52 | void setRootNonTerminal( int iRoot ); |
53 | void setRootNonTerminal( const Bu::String &sRoot ); | 53 | void setRootNonTerminal( const Bu::String &sRoot ); |
54 | 54 | ||
55 | typedef Bu::Signal1<void, Parser &> Reduction; | 55 | typedef Bu::Signal1<void, Parser &> Reduction; |
56 | 56 | ||
57 | /** | 57 | /** |
58 | * Represents a possible state, either a terminal or non-terminal symbol | 58 | * Represents a possible state, either a terminal or non-terminal symbol |
59 | * in a Production. | 59 | * in a Production. |
60 | */ | 60 | */ |
61 | class State | 61 | class State |
62 | { | 62 | { |
63 | public: | 63 | public: |
64 | enum Type | 64 | enum Type |
65 | { | 65 | { |
66 | typeTerminal, | 66 | typeTerminal, |
67 | typeTerminalPush, | 67 | typeTerminalPush, |
68 | typeNonTerminal, | 68 | typeNonTerminal, |
69 | typeReduction | 69 | typeReduction |
70 | }; | 70 | }; |
71 | 71 | ||
72 | State( Type eType, int iIndex ); | 72 | State( Type eType, int iIndex ); |
73 | virtual ~State(); | 73 | virtual ~State(); |
74 | 74 | ||
75 | //private: | 75 | //private: |
76 | Type eType; | 76 | Type eType; |
77 | int iIndex; | 77 | int iIndex; |
78 | }; | 78 | }; |
79 | 79 | ||
80 | typedef Bu::List<State> Production; | 80 | typedef Bu::List<State> Production; |
81 | 81 | ||
82 | class NonTerminal | 82 | class NonTerminal |
83 | { | 83 | { |
84 | public: | 84 | public: |
85 | NonTerminal(); | 85 | NonTerminal(); |
86 | virtual ~NonTerminal(); | 86 | virtual ~NonTerminal(); |
87 | 87 | ||
88 | void addProduction( Production p ); | 88 | void addProduction( Production p ); |
89 | void setCanSkip(); | 89 | void setCanSkip(); |
90 | 90 | ||
91 | // private: | 91 | // private: |
92 | typedef Bu::List<Production> ProductionList; | 92 | typedef Bu::List<Production> ProductionList; |
93 | ProductionList lProduction; | 93 | ProductionList lProduction; |
94 | bool bCanSkip; | 94 | bool bCanSkip; |
95 | }; | 95 | }; |
96 | 96 | ||
97 | int addNonTerminal( const Bu::String &sName, NonTerminal &nt ); | 97 | int addNonTerminal( const Bu::String &sName, NonTerminal &nt ); |
98 | int addNonTerminal( const Bu::String &sName ); | 98 | int addNonTerminal( const Bu::String &sName ); |
99 | void setNonTerminal( const Bu::String &sName, NonTerminal &nt ); | 99 | void setNonTerminal( const Bu::String &sName, NonTerminal &nt ); |
100 | int getNonTerminalId( const Bu::String &sName ); | 100 | int getNonTerminalId( const Bu::String &sName ); |
101 | bool hasNonTerminal( const Bu::String &sName ); | 101 | bool hasNonTerminal( const Bu::String &sName ); |
102 | 102 | ||
103 | int addReduction( const Bu::String &sName, const Reduction &r ); | 103 | int addReduction( const Bu::String &sName, const Reduction &r ); |
104 | int addReduction( const Bu::String &sName ); | 104 | int addReduction( const Bu::String &sName ); |
105 | void setReduction( const Bu::String &sName, const Reduction &r ); | 105 | void setReduction( const Bu::String &sName, const Reduction &r ); |
106 | int getReductionId( const Bu::String &sName ); | 106 | int getReductionId( const Bu::String &sName ); |
107 | bool hasReduction( const Bu::String &sName ); | 107 | bool hasReduction( const Bu::String &sName ); |
108 | 108 | ||
109 | private: | 109 | private: |
110 | bool selectProduction( int iNt, Lexer::Token *ptCur ); | 110 | bool selectProduction( int iNt, Lexer::Token *ptCur ); |
111 | void advanceState(); | 111 | void advanceState(); |
112 | 112 | ||
113 | private: | 113 | private: |
114 | typedef Bu::List<Lexer *> LexerStack; | 114 | typedef Bu::List<Lexer *> LexerStack; |
115 | typedef Bu::List<Lexer::Token *> TokenStack; | 115 | typedef Bu::List<Lexer::Token *> TokenStack; |
116 | typedef Bu::List<Production::const_iterator> StateStack; | 116 | typedef Bu::List<Production::const_iterator> StateStack; |
117 | typedef Bu::Array<Reduction> ReductionArray; | 117 | typedef Bu::Array<Reduction> ReductionArray; |
118 | typedef Bu::Hash<Bu::String,int> NameIndexHash; | 118 | typedef Bu::Hash<Bu::String,int> NameIndexHash; |
119 | typedef Bu::Array<NonTerminal> NonTerminalArray; | 119 | typedef Bu::Array<NonTerminal> NonTerminalArray; |
120 | 120 | ||
121 | LexerStack sLexer; | 121 | LexerStack sLexer; |
122 | TokenStack sToken; | 122 | TokenStack sToken; |
123 | StateStack sState; | 123 | StateStack sState; |
124 | ReductionArray aReduction; | 124 | ReductionArray aReduction; |
125 | NameIndexHash hReductionName; | 125 | NameIndexHash hReductionName; |
126 | NonTerminalArray aNonTerminal; | 126 | NonTerminalArray aNonTerminal; |
127 | NameIndexHash hNonTerminalName; | 127 | NameIndexHash hNonTerminalName; |
128 | int iRootNonTerminal; | 128 | int iRootNonTerminal; |
129 | }; | 129 | }; |
130 | Bu::Formatter &operator<<( Bu::Formatter &f, Bu::Parser::State::Type t ); | 130 | Bu::Formatter &operator<<( Bu::Formatter &f, Bu::Parser::State::Type t ); |
131 | Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::Parser::State &s ); | 131 | Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::Parser::State &s ); |
132 | }; | 132 | }; |