-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFortranAttributes.java
203 lines (193 loc) · 8.35 KB
/
FortranAttributes.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.util.HashSet;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Vector;
import java.util.List;
import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.AbstractMap.SimpleEntry;
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Dr.scient. Frank Thomas Tveter, 2016
public class FortranAttributes {
//
static final int noChild=RegexNode.noSubLevels;
static final int allChild=RegexNode.allSubLevels;
static int maxcnt=0;
//
public FortranAttributes() {
}
static public void addAttributes(RegexNode regex,String... nodes) {
addAttributes(regex, null,nodes);
}
static private void addAttributes(RegexNode regex, HashMap<String,String> globalTypeMap,String... nodes) {
// assign location array attribute to all nodes in the node-tree
//regex.debugOff();
RegexNode subNode=regex.getNode(nodes);
while (subNode != null) {
//System.out.format("Looping addAttributes subNode: %s\n",subNode.getIdentification());
subNode.setLocationAll();
// assign "name-attribute" to all parents of "name"
RegexNode node=subNode.getNode("name");
while (node != null) {
String varName=node.getText();
RegexNode parent=node.getParent();
parent.setAttribute("name",varName);
node=subNode.getNode("name");
}
// assign "Number of arguments" to all "arguments" nodes
node=subNode.getNode("arguments");
while (node != null) {
Integer nArguments=node.countChildren();
RegexNode parent=node.getParent();
parent.setAttribute("narguments",nArguments);
node=subNode.getNode("arguments");
}
//System.out.format("Debug:\n===============\n%s\n===============\n",subNode.toString());
HashMap<String,String> typeMap = new HashMap<String,String>();
if (globalTypeMap != null) {
for (Map.Entry<String, String> entry : globalTypeMap.entrySet()) {
String addressName = entry.getKey();
String addressType = entry.getValue();
typeMap.put(addressName,addressType);
}
}
// process references, identify functions and arrays...
RegexNode mainNode=subNode.getNode("$","*","Main"); // loop over all "main" nodes...
while (mainNode != null) {
//System.out.format("Looping addAttributes MainNode: %s\n",mainNode.getIdentification());
if (subNode.getNodeName().equals("Function")) {
String name=mainNode.getText("$","Main","start","name");
typeMap.put(name,"function");
}
RegexNode declarationsNode=mainNode.getNode("$","Main","Declarations");
// make hash of arrays and external function declarations...
while(declarationsNode != null ) {
RegexNode declarationNode=declarationsNode.getNode("$","Declarations","declaration");
while ( declarationNode!=null) {
String type=declarationNode.getText("type");
RegexNode referenceNode=declarationNode.getNode("$","declaration","references","reference");
while (referenceNode != null) {
String name=canonical(referenceNode.getText("$","reference","name"));
name=canonical(name);
// find number of arguments...
Integer nArguments=0;
RegexNode argumentsNode=referenceNode.getFirstNode("$","reference","arguments");
if (argumentsNode != null) {
nArguments=argumentsNode.countChildren();
}
if (nArguments == 0) {
typeMap.put(name,"variable");
referenceNode.setAttribute("type",new String("scalar"));
} else {
typeMap.put(name,"array");
referenceNode.setAttribute("type",new String("array"));
}
referenceNode=declarationNode.getNode("$","declaration","references","reference");
}
declarationNode=declarationsNode.getNode("$","Declarations","declaration");
}
RegexNode externalNode=declarationsNode.getNode("$","Declarations","external");
while ( externalNode!=null) {
RegexNode referenceNode=externalNode.getNode("$","external","arguments","argument","reference");
while (referenceNode != null) {
String name=canonical(referenceNode.getText("$","reference","name"));
name=canonical(name);
// store function name...
typeMap.put(name,"function");
referenceNode.setAttribute("type",new String("function"));
referenceNode=externalNode.getNode("$","external","arguments","argument","reference");
}
externalNode=declarationsNode.getNode("$","Declarations","external");
}
RegexNode dimensionNode=declarationsNode.getNode("$","Declarations","dimension");
while ( dimensionNode!=null) {
RegexNode referenceNode=dimensionNode.getNode("$","dimension","arguments","argument","reference");
while (referenceNode != null) {
String name=canonical(referenceNode.getText("$","reference","name"));
name=canonical(name);
// store reference name...
typeMap.put(name,"array");
referenceNode.setAttribute("type",new String("array"));
referenceNode=dimensionNode.getNode("$","dimension","arguments","argument","reference");
}
dimensionNode=declarationsNode.getNode("$","Declarations","dimension");
}
declarationsNode=mainNode.getNode("$","Main","Declarations");
}
RegexNode statementsNode=mainNode.getNode("$","Main","Statements");
while(statementsNode != null ) {
// loop over references,
RegexNode referenceNode=statementsNode.getNode("reference");
while (referenceNode != null ) {
String name=canonical(referenceNode.getText("$","reference","name"));
name=canonical(name);
String type=typeMap.get(name);
if (type != null ) {
if (type.equals("function")) {
referenceNode.setAttribute("type",new String("function"));
} else if (type.equals("array")) {
referenceNode.setAttribute("type",new String("array"));
}
} else {
// check if reference has dimensions => check if it is external or declared array or, otherwise its a "functionCall"
// if it has no dimensions , check if it is external, then it is a function... change nodeName to "functionCall"
// find number of arguments...
Integer nArguments=0;
RegexNode argumentsNode=referenceNode.getFirstNode("$","reference","arguments");
if (argumentsNode != null) {
nArguments=argumentsNode.countChildren();
}
if (nArguments > 0) { // this is a function (else it is an implicit reference)
referenceNode.setAttribute("type",new String("function"));
} else {
referenceNode.setAttribute("type",new String("implicit"));
}
}
referenceNode=statementsNode.getNode("reference");
}
statementsNode=mainNode.getNode("$","Main","Statements");
}
mainNode=subNode.getNode("$","*","Main");
}
String s="";
for (String ss : nodes) {
s=s+":"+ss;
}
RegexNode subroutineNode=subNode.getNode("$","*","Contains","Subroutine");
while (subroutineNode != null) {
//System.out.format(" Loop addAttributes subroutineNode: %s\n",subroutineNode.getIdentification());
addAttributes(subroutineNode, typeMap,"$","Subroutine");
//System.out.format(" Done addAttributes subroutineNode: %s\n",subroutineNode.getIdentification());
subroutineNode=subNode.getNode("$","*","Contains","Subroutine");
}
RegexNode functionNode=subNode.getNode("$","*","Contains","Function");
while (functionNode != null) {
//System.out.format("Looping addAttributes functionNode: %s\n",functionNode.getIdentification());
addAttributes(functionNode, typeMap,"$","Function");
functionNode=subNode.getNode("$","*","Contains","Function");
}
subNode=regex.getNode(nodes);
}
// process contains subroutines (may use global variables)...
}
public static String canonical (String name) {
if (name != null) {
return name.toLowerCase();
} else {
return name;
}
}
}