home *** CD-ROM | disk | FTP | other *** search
- #ifndef __PARSER__
- #define __PARSER__ 1
-
- #ifndef __STACK__
- #include "Stack.h"
- #endif
-
- #ifndef __STDFILE__
- #include "StdFile.h"
- #endif
-
- #ifndef __SYNTACTIC__
- #include "Syntactic.h"
- #endif
-
- #ifndef __TYPES__
- #include <types.h>
- #endif
-
-
- #pragma segment Parser
-
-
-
- /*
- ** Class which does parsing of the input
- */
- class Formatting;
- class SyntacticPrs;
- class Parser : public SingleObject {
- public:
- Parser();
- ~Parser();
-
- short IParser();
- short IParser(const Parser *aParser); // Clone the parser
-
- Formatting *GetFormatting() const;
- void SetFormatting(Formatting *aFormat);
- // Manipulate the Formatting object
-
- Boolean DebugFormatting() const;
- Boolean DebugParser() const;
- void DebugFormatting(Boolean aFlag);
- void DebugParser(Boolean aFlag);
-
- Boolean Parse();
- Boolean Parse(Syntactic *aToken);
- /*
- ** Parse by applying the token to the current state of the parse.
- ** Return true if the parse succeeded, false if it didn't. The
- ** argument free form is called to make sure that all tokens that
- ** can be parsed have been parsed. The parser does not immediately
- ** use a token, identifiers in particular can be held up until the
- ** type of the identifier can be determined (such as class::member,
- ** or ::new(xxx)).
- */
-
- short FindHandle(short aTokenType, const short *aStackDescription) const;
- /*
- ** FindHandle is used by parsing objects to map the state recorded
- ** on the parse stack to a single value. The "aStackDescription"
- ** variable defines the mapping from stack contents to value. Its
- ** format is:
- ** {
- ** <value>, <tokenType>, <nStackItems>, <type of TOS-n>…<type of TOS>,
- ** <value>, <tokenType>, <nStackItems>, <type of TOS-n>…<type of TOS>,
- ** ...
- ** (v < 0), ???, 0
- ** }
- ** where the final entry (v: v < 0) is the value to return when nothing
- ** matches. Note that the stack description has the top of the stack
- ** on the right, with deeper values on the left. The list is terminated
- ** when <value> < 0 and nStackItems == 0. In that case, the <tokenType>
- ** is ignored. If <tokenType> < 0, then <tokenType> matches aTokenType
- ** If <type of TOS-n> < 0, then that matches any type on the stack
- */
-
-
- Syntactic *TopItem() const;
- void TopItem(Syntactic *anItem);
- Syntactic *Pick(int i) const;
- Syntactic *At(int i) const;
- void Drop(int n);
- int Depth() const;
- // Access the parse stack entry values
-
- int ItemIndex() const;
- // Retturn the index of the object currently doing the parsing
-
- void Shift(Syntactic *token);
- Boolean Reduce(Syntactic *newItem, int n);
- // Apply a shift and reduce operations to the parser
-
- void DumpStack(StdFile &aFile);
- /*
- ** Dump the parse stack to the specified file
- */
-
- static const char *NameOf(short aType);
- /*
- ** Yield the name of a particular lexical/syntactic type. Used for
- ** debugging
- */
-
- private:
- Stack fStack; // Parse stack of Syntactic*
- Formatting *fFormat; // How and what to display
- short fItemIndex; // Index of the item parsing
- short fRecursionCount; // Internal error check
- short fDepth; // Depth at last call to Parse()
- Boolean fDebugParser; // True when DumpStack on ok.
- Boolean fDebugFormatting; // True when debugging formatting.
- };
-
-
- //µ Parser::Parser
- #pragma segment Parser
- inline Parser::Parser()
- {
- }
-
-
- //µ Parser::GetFormatting
- #pragma segment Parser
- inline Formatting *Parser::GetFormatting() const
- {
- return (fFormat);
- }
-
-
- //µ Parser::SetFormatting
- #pragma segment Parser
- inline void Parser::SetFormatting(Formatting *aFormat)
- {
- fFormat = aFormat;
- }
-
-
- //µ Parser::DebugFormatting
- #pragma segment Parser
- inline Boolean Parser::DebugFormatting() const
- {
- return (fDebugFormatting);
- }
-
-
- //µ Parser::DebugParser
- #pragma segment Parser
- inline Boolean Parser::DebugParser() const
- {
- return (fDebugParser);
- }
-
-
- //µ Parser::DebugFormatting
- #pragma segment Parser
- inline void Parser::DebugFormatting(Boolean aFlag)
- {
- fDebugFormatting = aFlag;
- }
-
-
- //µ Parser::DebugParser
- #pragma segment Parser
- inline void Parser::DebugParser(Boolean aFlag)
- {
- fDebugParser = aFlag;
- }
-
-
- //µ Parser::TopItem
- #pragma segment Parser
- inline Syntactic *Parser::TopItem() const
- {
- return ((Syntactic *)fStack.Top());
- }
-
-
- //µ Parser::TopItem
- #pragma segment Parser
- inline void Parser::TopItem(Syntactic *anItem)
- {
- fStack.Top(anItem);
- }
-
-
- //µ Parser::Pick
- #pragma segment Parser
- inline Syntactic *Parser::Pick(int i) const
- {
- return ((Syntactic *)fStack.Pick(i));
- }
-
-
- //µ Parser::At
- #pragma segment Parser
- inline Syntactic *Parser::At(int i) const
- {
- return ((Syntactic *)fStack.At(i));
- }
-
-
- //µ Parser::Drop
- #pragma segment Parser
- inline void Parser::Drop(int n)
- {
- fStack.Drop(n);
- }
-
-
- //µ Parser::Depth
- #pragma segment Parser
- inline int Parser::Depth() const
- {
- return (fStack.Depth());
- }
-
-
- //µ Parser::ItemIndex
- #pragma segment Parser
- inline int Parser::ItemIndex() const
- {
- return (fItemIndex);
- }
-
-
- //µ Parser::Shift
- #pragma segment Parser
- inline void Parser::Shift(Syntactic *token)
- {
- fStack.Push(token);
- }
-
-
- //µ Parser::Reduce
- #pragma segment Parser
- inline Boolean Parser::Reduce(Syntactic *newItem, int n)
- {
- Drop(n);
- return (Parse(newItem));
- }
-
-
-
- #endif
-
-
-