home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / Jikes / Source / src / parser.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-24  |  5.6 KB  |  215 lines

  1. // $Id: parser.h,v 1.11 2001/01/05 09:13:20 mdejong Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #ifndef parser_INCLUDED
  11. #define parser_INCLUDED
  12.  
  13. #include "platform.h"
  14. #include "lpginput.h"
  15.  
  16. /*
  17. //FIXME: include stuff
  18. #include <ctype.h>
  19. #include <limits.h>
  20. #ifdef HAVE_WCHAR_H
  21. # include <wchar.h>
  22. #endif
  23. #include <string.h>
  24. #include <stdio.h>
  25. */
  26.  
  27.  
  28. #ifdef    HAVE_JIKES_NAMESPACE
  29. namespace Jikes {    // Open namespace Jikes block
  30. #endif
  31.  
  32.  
  33. class StoragePool;
  34. class AstPackageDeclaration;
  35. class AstCompilationUnit;
  36. class AstClassBody;
  37. class AstInterfaceDeclaration;
  38. class Ast;
  39. class AstListNode;
  40.  
  41. enum ParseErrorCode
  42. {
  43.     ERROR_CODE,
  44.     BEFORE_CODE,
  45.     INSERTION_CODE,
  46.     INVALID_CODE,
  47.     SUBSTITUTION_CODE,
  48.     DELETION_CODE,
  49.     MERGE_CODE,
  50.     MISPLACED_CODE,
  51.     SCOPE_CODE,
  52.     MANUAL_CODE,
  53.     SECONDARY_CODE,
  54.     EOF_CODE
  55. };
  56.  
  57. struct PrimaryRepairInfo
  58. {
  59.     ParseErrorCode code;
  60.     
  61.     int distance,
  62.         buffer_position,
  63.         misspell_index,
  64.         symbol;
  65. };
  66.  
  67. struct SecondaryRepairInfo
  68. {
  69.     ParseErrorCode code;
  70.     int distance,
  71.         buffer_position,
  72.         stack_position,
  73.         num_deletions,
  74.         symbol;
  75.  
  76.     bool recovery_on_next_stack;
  77. };
  78.  
  79. class Parser : public javaprs_table
  80. {
  81. public:
  82.  
  83.     Parser() : ast_pool(NULL),
  84.                parse_header_only(false),
  85.                parse_package_header_only(false),
  86.                location_stack(NULL),
  87.                parse_stack(NULL),
  88.                stack_length(0),
  89.                stack(NULL),
  90.                temp_stack(NULL)
  91.     {
  92.         InitRuleAction();
  93.         return;
  94.     }
  95.  
  96.     ~Parser()
  97.     {
  98.         delete [] stack;
  99.         delete [] location_stack;
  100.         delete [] parse_stack;
  101.         delete [] temp_stack;
  102.     }
  103.  
  104.     AstPackageDeclaration *PackageHeaderParse(LexStream *, StoragePool *);
  105.     AstCompilationUnit *HeaderParse(LexStream *, StoragePool * = NULL);
  106.     bool InitializerParse(LexStream *, AstClassBody *);
  107.     bool InitializerParse(LexStream *, AstInterfaceDeclaration *);
  108.     bool BodyParse(LexStream *, AstClassBody *);
  109.     bool BodyParse(LexStream *, AstInterfaceDeclaration *);
  110.  
  111. protected:
  112.  
  113.     TokenObject buffer[BUFF_SIZE];
  114.     TokenObject end_token;
  115.  
  116.     Ast *HeaderParse();
  117.     bool Initializer(AstClassBody *);
  118.     bool Initializer(AstInterfaceDeclaration *);
  119.     bool Body(AstClassBody *);
  120.     bool Body(AstInterfaceDeclaration *);
  121.     Ast *ParseSegment(TokenObject);
  122.  
  123. #define HEADERS
  124. #include "javaact.h"
  125.  
  126.     void (Parser::*rule_action[NUM_RULES + 1]) ();
  127.  
  128.     void InitRuleAction();
  129.  
  130.     //******************************************************************************
  131.     //
  132.     // Given a rule of the form     A ::= x1 x2 ... xn     n > 0
  133.     //
  134.     // the function Token(i) yields the symbol xi, if xi is a terminal
  135.     // or ti, if xi is a nonterminal that produced a string of the form
  136.     // xi => ti w.
  137.     //
  138.     //******************************************************************************
  139.     inline LexStream::TokenIndex Token(int i)
  140.     {
  141.         return location_stack[state_stack_top + (i - 1)];
  142.     }
  143.  
  144.     //******************************************************************************
  145.     //
  146.     // Given a rule of the form     A ::= x1 x2 ... xn     n > 0
  147.     //
  148.     // the function Sym(i) yields the AST subtree associated with symbol
  149.     // xi. NOTE that if xi is a terminal, Sym(i) is undefined !
  150.     //
  151.     //******************************************************************************
  152.     inline Ast*& Sym(int i) { return parse_stack[state_stack_top + (i - 1)]; }
  153.  
  154.     //******************************************************************************
  155.     //
  156.     // When a token is shifted, we also construct a null AST for
  157.     // it.  This is necessary in case we encounter an error and need to
  158.     // delete AST subtrees from the parse stack - those corresponding to
  159.     // shifted tokens should also have a valid subtree.
  160.     //
  161.     //******************************************************************************
  162.     inline void TokenAction(TokenObject curtok) { Sym(1) = NULL; }
  163.  
  164.     LexStream *lex_stream;
  165.  
  166.     StoragePool *ast_pool,
  167.                 *body_pool,
  168.                 *list_node_pool;
  169.  
  170.     AstListNode *free_list_nodes;
  171.     AstListNode *AllocateListNode();
  172.     void FreeCircularList(AstListNode *);
  173.  
  174.     bool parse_header_only,
  175.          parse_package_header_only;
  176.  
  177.     //
  178.     // LOCATION_STACK is a stack that is "parallel" to
  179.     // (STATE_)STACK that is used to keep
  180.     // track of the location of the first token on which an action
  181.     // was executed in the corresponding state.
  182.     //
  183.     Location *location_stack;
  184.     Ast **parse_stack;
  185.  
  186.     enum { STACK_INCREMENT = 256 };
  187.  
  188.     int stack_length,
  189.         state_stack_top,
  190.         *stack,
  191.  
  192.         temp_stack_top,
  193.         *temp_stack;
  194.  
  195.     static inline int Min(int x, int y) { return ((x) < (y) ? (x) : (y)); }
  196.     static inline int Max(int x, int y) { return ((x) > (y) ? (x) : (y)); }
  197.  
  198.     void AllocateErrorStacks();
  199.     void ReallocateStacks();
  200.     void ErrorRepair(TokenObject error_token);
  201.     void RepairParse(TokenObject);
  202.     SecondaryRepairInfo ErrorSurgery(int stack[],
  203.                                      int stack_top,
  204.                                      int last_index,
  205.                                      SecondaryRepairInfo repair);
  206.     int ParseCheck(int stack[], int stack_top, int first_token, int buffer_position);
  207. };
  208.  
  209. #ifdef    HAVE_JIKES_NAMESPACE
  210. }            // Close namespace Jikes block
  211. #endif
  212.  
  213. #endif
  214.  
  215.