1 // $Id: diagnose.h,v 1.18 2004/03/25 13:32:27 ericb Exp $ -*- c++ -*-
2 //
3 // This software is subject to the terms of the IBM Jikes Compiler
4 // License Agreement available at the following URL:
5 // http://ibm.com/developerworks/opensource/jikes.
6 // Copyright (C) 1996, 2004 IBM Corporation and others.  All Rights Reserved.
7 // You must accept the terms of that agreement to use this software.
8 //
9 #ifndef diagnose_INCLUDED
10 #define diagnose_INCLUDED
11 
12 #include "platform.h"
13 #include "parser.h"
14 #include "jikesapi.h"
15 #include "tuple.h"
16 
17 #ifdef HAVE_JIKES_NAMESPACE
18 namespace Jikes { // Open namespace Jikes block
19 #endif
20 
21 class Control;
22 
23 struct RepairCandidate
24 {
25     int symbol;
26     Location location;
27 };
28 
29 struct StateInfo
30 {
31     int state,
32         next;
33 };
34 
35 class ParseErrorInfo: public JikesError
36 {
37     friend class ParseError;
38 public:
39     virtual const wchar_t* getErrorMessage();
40     virtual const wchar_t* getErrorReport();
41 
42     virtual JikesErrorSeverity getSeverity();
43     virtual const char* getFileName();
44 
45     virtual int getLeftLineNo();
46     virtual int getLeftColumnNo();
47     virtual int getRightLineNo();
48     virtual int getRightColumnNo();
49 
50 private:
51     int left_line_no;
52     int left_column_no;
53     int right_line_no;
54     int right_column_no;
55 
56     static bool emacs_style_report;
57     LexStream* lex_stream;
58 
59     void Initialize(LexStream*);
60 
61     const wchar_t* regularErrorString();
62     const wchar_t* emacsErrorString();
63 
64     TokenIndex left_token;
65     TokenIndex right_token;
66 
67     int name_index;
68     int num;
69     unsigned char msg_level;
70     ParseErrorCode msg_code;
71     unsigned scope_name_index;
72 };
73 
74 
75 class ParseError : public javaprs_table
76 {
77 public:
78 
79     void Report(int msg_level, ParseErrorCode, int name_index,
80                 TokenIndex left_token, TokenIndex right_token,
81                 int scope_name_index = 0);
82 
83     void SortMessages();
84 
85     ParseError(Control& control_, LexStream* lex_stream_);
86     void PrintMessages();
87 
88 private:
89 
90     Control& control;
91     LexStream* lex_stream;
92 
93     Tuple<ParseErrorInfo> errors;
94 
95     void PrintMessage(int k);
96 };
97 
98 
99 class DiagnoseParser : public Parser
100 {
101 public:
102 
DiagnoseParser(Control & control_,LexStream * lex_stream_)103     DiagnoseParser(Control& control_, LexStream* lex_stream_)
104         : next_stack(NULL)
105         , prev_stack(NULL)
106         , scope_index(NULL)
107         , scope_position(NULL)
108         , state_pool(256)
109         , error(control_, lex_stream_)
110     {
111         lex_stream = lex_stream_;
112         memset(list, 0, NUM_SYMBOLS * sizeof(int));
113         DiagnoseParse();
114     }
115 
~DiagnoseParser()116     ~DiagnoseParser()
117     {
118         delete [] next_stack;
119         delete [] prev_stack;
120         delete [] scope_index;
121         delete [] scope_position;
122     }
123 
124 private:
125 
126     int next_stack_top;
127     int* next_stack;
128 
129     int prev_stack_top;
130     int* prev_stack;
131 
132     int scope_stack_top;
133     int* scope_index;
134     int* scope_position;
135 
136     int list[NUM_SYMBOLS + 1];
137 
138     enum { NIL = -1 };
139     Tuple<StateInfo> state_pool;
140     // this variable is managed entirely by the function "scope_trial"
141     int* state_seen;
142 
143     ParseError error;
144 
145     void DiagnoseParse();
146 
147     void ReallocateStacks();
148 
149     RepairCandidate ErrorRecovery(TokenObject error_token);
150     RepairCandidate PrimaryPhase(TokenObject error_token);
151     int MergeCandidate(int state, int buffer_position);
152     PrimaryRepairInfo CheckPrimaryDistance(int stack[],
153                                            int stack_top,
154                                            PrimaryRepairInfo repair);
155     RepairCandidate PrimaryDiagnosis(PrimaryRepairInfo repair);
156     int GetTermIndex(int stack[], int stack_top,
157                      int tok, int buffer_position);
158     int GetNtermIndex(int start, int sym, int buffer_position);
159     int Misspell(int sym, TokenObject tok);
160     RepairCandidate SecondaryPhase(TokenObject error_token);
161     SecondaryRepairInfo MisplacementRecovery
162              (int stack[],
163               int stack_top,
164               int last_index,
165               SecondaryRepairInfo misplaced, bool stack_flag);
166     SecondaryRepairInfo SecondaryRecovery
167              (int stack[],
168               int stack_top,
169               int last_index,
170               SecondaryRepairInfo repair, bool stack_flag);
171     void SecondaryDiagnosis(SecondaryRepairInfo repair);
172 
173     void RepairParse(TokenObject);
174 
175     PrimaryRepairInfo ScopeTrial(int stack[], int stack_top,
176                                  PrimaryRepairInfo repair);
177     void ScopeTrialCheck(int stack[], int stack_top,
178                          PrimaryRepairInfo& repair, int indx);
179     bool SecondaryCheck(int stack[], int stack_top,
180                         int buffer_position, int distance);
181 };
182 
183 #ifdef HAVE_JIKES_NAMESPACE
184 } // Close namespace Jikes block
185 #endif
186 
187 #endif // diagnose_INCLUDED
188 
189