Skip to content

Commit 17cdf6f

Browse files
committed
Validate break() and continue()
Validate that `break()` and `continue()` are used within breakable / continuable code blocks during static analysis.
1 parent 4575c83 commit 17cdf6f

File tree

10 files changed

+389
-16
lines changed

10 files changed

+389
-16
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.laytonsmith.core.compiler.analysis;
2+
3+
import com.laytonsmith.core.NodeModifiers;
4+
import com.laytonsmith.core.constructs.Target;
5+
6+
/**
7+
* Represents a breakable (for(), while(), switch(), etc) bound declaration in a scope graph.
8+
* This indicates a boundary where lookup for a breakable should stop.
9+
* @author P.J.S. Kools
10+
*/
11+
public class BreakableBoundDeclaration extends Declaration {
12+
13+
/**
14+
* Creates a new {@link BreakableBoundDeclaration} in the {@link Namespace#BREAKABLE} namespace.
15+
* @param modifiers The node modifiers.
16+
* @param t The breakable target.
17+
*/
18+
public BreakableBoundDeclaration(NodeModifiers modifiers, Target t) {
19+
super(Namespace.BREAKABLE, null, null, modifiers, t);
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.laytonsmith.core.compiler.analysis;
2+
3+
import com.laytonsmith.core.NodeModifiers;
4+
import com.laytonsmith.core.constructs.Target;
5+
6+
/**
7+
* Represents a breakable (for(), while(), switch(), etc) declaration in a scope graph.
8+
* @author P.J.S. Kools
9+
*/
10+
public class BreakableDeclaration extends Declaration {
11+
12+
private final Scope parentScope;
13+
14+
/**
15+
* Creates a new {@link BreakableDeclaration} in the {@link Namespace#BREAKABLE} namespace.
16+
* @param parentScope The parent scope of the breakable (that does not include this declaration).
17+
* @param modifiers The node modifiers.
18+
* @param t The breakable target.
19+
*/
20+
public BreakableDeclaration(Scope parentScope, NodeModifiers modifiers, Target t) {
21+
super(Namespace.BREAKABLE, null, null, modifiers, t);
22+
this.parentScope = parentScope;
23+
}
24+
25+
public Scope getParentScope() {
26+
return this.parentScope;
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.laytonsmith.core.compiler.analysis;
2+
3+
import com.laytonsmith.core.constructs.Target;
4+
5+
/**
6+
* Represents a reference to a breakable (by the break() function) in a scope graph.
7+
* @author P.J.S. Kools
8+
*/
9+
public class BreakableReference extends Reference {
10+
11+
/**
12+
* Creates a new {@link BreakableReference} in the {@link Namespace#BREAKABLE} scope.
13+
* @param t - The target of the reference.
14+
*/
15+
public BreakableReference(Target t) {
16+
super(Namespace.BREAKABLE, null, t);
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.laytonsmith.core.compiler.analysis;
2+
3+
import com.laytonsmith.core.NodeModifiers;
4+
import com.laytonsmith.core.constructs.Target;
5+
6+
/**
7+
* Represents a continuable (for(), while(), etc) bound declaration in a scope graph.
8+
* This indicates a boundary where lookup for a continuable should stop.
9+
* @author P.J.S. Kools
10+
*/
11+
public class ContinuableBoundDeclaration extends Declaration {
12+
13+
/**
14+
* Creates a new {@link ContinuableBoundDeclaration} in the {@link Namespace#CONTINUABLE} namespace.
15+
* @param modifiers The node modifiers.
16+
* @param t The continuable target.
17+
*/
18+
public ContinuableBoundDeclaration(NodeModifiers modifiers, Target t) {
19+
super(Namespace.CONTINUABLE, null, null, modifiers, t);
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.laytonsmith.core.compiler.analysis;
2+
3+
import com.laytonsmith.core.NodeModifiers;
4+
import com.laytonsmith.core.constructs.Target;
5+
6+
/**
7+
* Represents a continuable (for(), while(), etc) declaration in a scope graph.
8+
* @author P.J.S. Kools
9+
*/
10+
public class ContinuableDeclaration extends Declaration {
11+
12+
/**
13+
* Creates a new {@link ContinuableDeclaration} in the {@link Namespace#CONTINUABLE} namespace.
14+
* @param modifiers The node modifiers.
15+
* @param t The continuable target.
16+
*/
17+
public ContinuableDeclaration(NodeModifiers modifiers, Target t) {
18+
super(Namespace.CONTINUABLE, null, null, modifiers, t);
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.laytonsmith.core.compiler.analysis;
2+
3+
import com.laytonsmith.core.constructs.Target;
4+
5+
/**
6+
* Represents a reference to a continuable (by the continue()) function in a scope graph.
7+
* @author P.J.S. Kools
8+
*/
9+
public class ContinuableReference extends Reference {
10+
11+
/**
12+
* Creates a new {@link ContinuableReference} in the {@link Namespace#CONTINUABLE} scope.
13+
* @param t - The target of the reference.
14+
*/
15+
public ContinuableReference(Target t) {
16+
super(Namespace.CONTINUABLE, null, t);
17+
}
18+
}

src/main/java/com/laytonsmith/core/compiler/analysis/Namespace.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ public enum Namespace {
1010
IVARIABLE_ASSIGN,
1111
PROCEDURE,
1212
INCLUDE,
13-
RETURNABLE
13+
RETURNABLE,
14+
BREAKABLE,
15+
CONTINUABLE
1416
}

0 commit comments

Comments
 (0)