blob: 84cb9da7f65833430dade7027ecd023bf8519d15 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef FORTRAN_PARSER_USER_STATE_H_
#define FORTRAN_PARSER_USER_STATE_H_
// Instances of ParseState (parse-state.h) incorporate instances of this
// UserState class, which encapsulates any semantic information necessary for
// parse tree construction so as to avoid any need for representing
// state in static data.
#include "char-block.h"
#include "features.h"
#include "parse-tree.h"
#include "../common/idioms.h"
#include <cinttypes>
#include <optional>
#include <ostream>
#include <set>
#include <unordered_set>
namespace Fortran::parser {
class CookedSource;
class ParsingLog;
class ParseState;
class Success {}; // for when one must return something that's present
class UserState {
public:
UserState(const CookedSource &cooked, LanguageFeatureControl features)
: cooked_{cooked}, features_{features} {}
const CookedSource &cooked() const { return cooked_; }
const LanguageFeatureControl &features() const { return features_; }
std::ostream *debugOutput() const { return debugOutput_; }
UserState &set_debugOutput(std::ostream *out) {
debugOutput_ = out;
return *this;
}
ParsingLog *log() const { return log_; }
UserState &set_log(ParsingLog *log) {
log_ = log;
return *this;
}
bool instrumentedParse() const { return instrumentedParse_; }
UserState &set_instrumentedParse(bool yes) {
instrumentedParse_ = yes;
return *this;
}
void NewSubprogram() {
doLabels_.clear();
nonlabelDoConstructNestingDepth_ = 0;
oldStructureComponents_.clear();
}
using Label = std::uint64_t;
bool IsDoLabel(Label label) const {
return doLabels_.find(label) != doLabels_.end();
}
bool InNonlabelDoConstruct() const {
return nonlabelDoConstructNestingDepth_ > 0;
}
void NewDoLabel(Label label) { doLabels_.insert(label); }
void EnterNonlabelDoConstruct() { ++nonlabelDoConstructNestingDepth_; }
void LeaveDoConstruct() {
if (nonlabelDoConstructNestingDepth_ > 0) {
--nonlabelDoConstructNestingDepth_;
}
}
void NoteOldStructureComponent(const CharBlock &name) {
oldStructureComponents_.insert(name);
}
bool IsOldStructureComponent(const CharBlock &name) const {
return oldStructureComponents_.find(name) != oldStructureComponents_.end();
}
private:
const CookedSource &cooked_;
std::ostream *debugOutput_{nullptr};
ParsingLog *log_{nullptr};
bool instrumentedParse_{false};
std::unordered_set<Label> doLabels_;
int nonlabelDoConstructNestingDepth_{0};
std::set<CharBlock> oldStructureComponents_;
LanguageFeatureControl features_;
};
// Definitions of parser classes that manipulate the UserState.
struct StartNewSubprogram {
using resultType = Success;
static std::optional<Success> Parse(ParseState &);
};
struct CapturedLabelDoStmt {
using resultType = Statement<common::Indirection<LabelDoStmt>>;
static std::optional<resultType> Parse(ParseState &);
};
struct EndDoStmtForCapturedLabelDoStmt {
using resultType = Statement<common::Indirection<EndDoStmt>>;
static std::optional<resultType> Parse(ParseState &);
};
struct EnterNonlabelDoConstruct {
using resultType = Success;
static std::optional<Success> Parse(ParseState &);
};
struct LeaveDoConstruct {
using resultType = Success;
static std::optional<Success> Parse(ParseState &);
};
struct OldStructureComponentName {
using resultType = Name;
static std::optional<Name> Parse(ParseState &);
};
struct StructureComponents {
using resultType = DataComponentDefStmt;
static std::optional<DataComponentDefStmt> Parse(ParseState &);
};
} // namespace Fortran::parser
#endif // FORTRAN_PARSER_USER_STATE_H_
|