-
Notifications
You must be signed in to change notification settings - Fork 598
/
VariableVisitor.groovy
142 lines (117 loc) · 4.25 KB
/
VariableVisitor.groovy
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
/*
* Copyright 2013-2024, Seqera Labs
*
* 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.
*/
package nextflow.ast
import groovy.transform.CompileStatic
import nextflow.script.TokenValRef
import org.codehaus.groovy.ast.ClassCodeVisitorSupport
import org.codehaus.groovy.ast.expr.ClosureExpression
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.DeclarationExpression
import org.codehaus.groovy.ast.expr.PropertyExpression
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.syntax.SyntaxException
/**
* Visit a closure and collect all referenced variable names
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@CompileStatic
class VariableVisitor extends ClassCodeVisitorSupport {
final Map<String, TokenValRef> fAllVariables = [:]
final Set<String> localDef = []
final SourceUnit sourceUnit
private boolean declaration
private int deep
VariableVisitor( SourceUnit unit ) {
this.sourceUnit = unit
}
protected boolean scopeEnabled() { true }
protected boolean isNormalized(PropertyExpression expr) {
if( !(expr.getProperty() instanceof ConstantExpression) )
return false
def target = expr.getObjectExpression()
while( target instanceof PropertyExpression) {
target = (target as PropertyExpression).getObjectExpression()
}
return target instanceof VariableExpression
}
@Override
void visitClosureExpression(ClosureExpression expression) {
if( deep++ == 0 )
super.visitClosureExpression(expression)
}
@Override
void visitDeclarationExpression(DeclarationExpression expr) {
declaration = true
try {
super.visitDeclarationExpression(expr)
}
finally {
declaration = false
}
}
@Override
void visitPropertyExpression(PropertyExpression expr) {
if( isNormalized(expr)) {
final name = expr.text.replace('?','')
final line = expr.lineNumber
final coln = expr.columnNumber
if( !name.startsWith('this.') && !fAllVariables.containsKey(name) && scopeEnabled() ) {
fAllVariables[name] = new TokenValRef(name,line,coln)
}
}
else
super.visitPropertyExpression(expr)
}
@Override
void visitVariableExpression(VariableExpression var) {
final name = var.name
final line = var.lineNumber
final coln = var.columnNumber
if( name == 'this' )
return
if( declaration ) {
if( fAllVariables.containsKey(name) )
sourceUnit.addError( new SyntaxException("Variable `$name` already defined in the process scope", line, coln))
else
localDef.add(name)
}
// Note: variable declared in the process scope are not added
// to the set of referenced variables. Only global ones are tracked
else if( !localDef.contains(name) && !fAllVariables.containsKey(name) && scopeEnabled() ) {
fAllVariables[name] = new TokenValRef(name,line,coln)
}
}
@Override
protected SourceUnit getSourceUnit() {
return sourceUnit
}
/**
* @return The set of all variables referenced in the script.
* NOTE: it includes properties in the form {@code object.propertyName}
*/
Set<TokenValRef> getAllVariables() {
new HashSet<TokenValRef>(fAllVariables.values())
}
Set<String> getAllVariableNames() {
def all = getAllVariables()
def result = new HashSet(all.size())
for( TokenValRef ref : all )
result.add(ref.name)
return result
}
}