home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++Source Code Fmtr Folder / Src / Parser.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  5.0 KB  |  249 lines  |  [TEXT/MPS ]

  1. #ifndef __PARSER__
  2. #define __PARSER__    1
  3.  
  4. #ifndef __STACK__
  5. #include "Stack.h"
  6. #endif
  7.  
  8. #ifndef __STDFILE__
  9. #include "StdFile.h"
  10. #endif
  11.  
  12. #ifndef __SYNTACTIC__
  13. #include "Syntactic.h"
  14. #endif
  15.  
  16. #ifndef __TYPES__
  17. #include <types.h>
  18. #endif
  19.  
  20.  
  21. #pragma segment Parser
  22.  
  23.  
  24.  
  25. /*
  26. ** Class which does parsing of the input
  27. */
  28. class Formatting;
  29. class SyntacticPrs;
  30. class Parser : public SingleObject {
  31. public:
  32.     Parser();
  33.     ~Parser();
  34.  
  35.     short IParser();
  36.     short IParser(const Parser *aParser);        // Clone the parser
  37.  
  38.     Formatting *GetFormatting() const;
  39.     void SetFormatting(Formatting *aFormat);
  40.     // Manipulate the Formatting object
  41.  
  42.     Boolean DebugFormatting() const;
  43.     Boolean DebugParser() const;
  44.     void DebugFormatting(Boolean aFlag);
  45.     void DebugParser(Boolean aFlag);
  46.  
  47.     Boolean Parse();
  48.     Boolean Parse(Syntactic *aToken);
  49.     /*
  50.     ** Parse by applying the token to the current state of the parse.
  51.     ** Return true if the parse succeeded, false if it didn't.  The
  52.     ** argument free form is called to make sure that all tokens that
  53.     ** can be parsed have been parsed.  The parser does not immediately
  54.     ** use a token, identifiers in particular can be held up until the
  55.     ** type of the identifier can be determined (such as class::member,
  56.     ** or ::new(xxx)).
  57.     */
  58.  
  59.     short FindHandle(short aTokenType, const short *aStackDescription) const;
  60.     /*
  61.     ** FindHandle is used by parsing objects to map the state recorded
  62.     ** on the parse stack to a single value.  The "aStackDescription"
  63.     ** variable defines the mapping from stack contents to value.  Its
  64.     ** format is:
  65.     **        {
  66.     **         <value>, <tokenType>, <nStackItems>, <type of TOS-n>…<type of TOS>,
  67.     **         <value>, <tokenType>, <nStackItems>, <type of TOS-n>…<type of TOS>,
  68.     **            ...
  69.     **         (v < 0), ???,         0
  70.     **        }
  71.     ** where the final entry (v: v < 0) is the value to return when nothing
  72.     ** matches.  Note that the stack description has the top of the stack
  73.     ** on the right, with deeper values on the left.  The list is terminated
  74.     ** when <value> < 0 and nStackItems == 0.  In that case, the <tokenType>
  75.     ** is ignored.  If <tokenType> < 0, then <tokenType> matches aTokenType
  76.     ** If <type of TOS-n> < 0, then that matches any type on the stack
  77.     */
  78.  
  79.  
  80.     Syntactic *TopItem() const;
  81.     void TopItem(Syntactic *anItem);
  82.     Syntactic *Pick(int i) const;
  83.     Syntactic *At(int i) const;
  84.     void Drop(int n);
  85.     int Depth() const;
  86.     // Access the parse stack entry values
  87.  
  88.     int ItemIndex() const;
  89.     // Retturn the index of the object currently doing the parsing
  90.  
  91.     void Shift(Syntactic *token);
  92.     Boolean Reduce(Syntactic *newItem, int n);
  93.     // Apply a shift and reduce operations to the parser
  94.  
  95.     void DumpStack(StdFile &aFile);
  96.     /*
  97.     ** Dump the parse stack to the specified file
  98.     */
  99.  
  100.     static const char *NameOf(short aType);
  101.     /*
  102.     ** Yield the name of a particular lexical/syntactic type.  Used for
  103.     ** debugging
  104.     */
  105.  
  106. private:
  107.     Stack fStack;                                // Parse stack of Syntactic*
  108.     Formatting *fFormat;                        // How and what to display
  109.     short fItemIndex;                            // Index of the item parsing
  110.     short fRecursionCount;                        // Internal error check
  111.     short fDepth;                                // Depth at last call to Parse()
  112.     Boolean fDebugParser;                        // True when DumpStack on ok.
  113.     Boolean fDebugFormatting;                    // True when debugging formatting.
  114. };
  115.  
  116.  
  117. //µ   Parser::Parser
  118. #pragma segment Parser
  119. inline Parser::Parser()
  120. {
  121. }
  122.  
  123.  
  124. //µ   Parser::GetFormatting
  125. #pragma segment Parser
  126. inline Formatting *Parser::GetFormatting() const
  127. {
  128.     return (fFormat);
  129. }
  130.  
  131.  
  132. //µ   Parser::SetFormatting
  133. #pragma segment Parser
  134. inline void Parser::SetFormatting(Formatting *aFormat)
  135. {
  136.     fFormat = aFormat;
  137. }
  138.  
  139.  
  140. //µ   Parser::DebugFormatting
  141. #pragma segment Parser
  142. inline Boolean Parser::DebugFormatting() const
  143. {
  144.     return (fDebugFormatting);
  145. }
  146.  
  147.  
  148. //µ   Parser::DebugParser
  149. #pragma segment Parser
  150. inline Boolean Parser::DebugParser() const
  151. {
  152.     return (fDebugParser);
  153. }
  154.  
  155.  
  156. //µ   Parser::DebugFormatting
  157. #pragma segment Parser
  158. inline void Parser::DebugFormatting(Boolean aFlag)
  159. {
  160.     fDebugFormatting = aFlag;
  161. }
  162.  
  163.  
  164. //µ   Parser::DebugParser
  165. #pragma segment Parser
  166. inline void Parser::DebugParser(Boolean aFlag)
  167. {
  168.     fDebugParser = aFlag;
  169. }
  170.  
  171.  
  172. //µ   Parser::TopItem
  173. #pragma segment Parser
  174. inline Syntactic *Parser::TopItem() const
  175. {
  176.     return ((Syntactic *)fStack.Top());
  177. }
  178.  
  179.  
  180. //µ   Parser::TopItem
  181. #pragma segment Parser
  182. inline void Parser::TopItem(Syntactic *anItem)
  183. {
  184.     fStack.Top(anItem);
  185. }
  186.  
  187.  
  188. //µ   Parser::Pick
  189. #pragma segment Parser
  190. inline Syntactic *Parser::Pick(int i) const
  191. {
  192.     return ((Syntactic *)fStack.Pick(i));
  193. }
  194.  
  195.  
  196. //µ   Parser::At
  197. #pragma segment Parser
  198. inline Syntactic *Parser::At(int i) const
  199. {
  200.     return ((Syntactic *)fStack.At(i));
  201. }
  202.  
  203.  
  204. //µ   Parser::Drop
  205. #pragma segment Parser
  206. inline void Parser::Drop(int n)
  207. {
  208.     fStack.Drop(n);
  209. }
  210.  
  211.  
  212. //µ   Parser::Depth
  213. #pragma segment Parser
  214. inline int Parser::Depth() const
  215. {
  216.     return (fStack.Depth());
  217. }
  218.  
  219.  
  220. //µ   Parser::ItemIndex
  221. #pragma segment Parser
  222. inline int Parser::ItemIndex() const
  223. {
  224.     return (fItemIndex);
  225. }
  226.  
  227.  
  228. //µ   Parser::Shift
  229. #pragma segment Parser
  230. inline void Parser::Shift(Syntactic *token)
  231. {
  232.     fStack.Push(token);
  233. }
  234.  
  235.  
  236. //µ   Parser::Reduce
  237. #pragma segment Parser
  238. inline Boolean Parser::Reduce(Syntactic *newItem, int n)
  239. {
  240.     Drop(n);
  241.     return (Parse(newItem));
  242. }
  243.  
  244.  
  245.  
  246. #endif
  247.  
  248.  
  249.