01: /*
02: * $Id: BreakStatement.java,v 1.10 2002/09/16 08:05:06 jkl Exp $
03: *
04: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
05: *
06: * Use is subject to license terms, as defined in
07: * Anvil Sofware License, Version 1.1. See LICENSE
08: * file, or http://njet.org/license-1.1.txt
09: */
10: package anvil.script.statements;
11:
12: import anvil.Location;
13: import anvil.codec.Code;
14: import anvil.parser.Tag;
15: import anvil.ErrorListener;
16: import anvil.script.compiler.ByteCompiler;
17: import anvil.script.Context;
18: import anvil.script.parser.TemplateParser;
19: import java.io.IOException;
20:
21: /**
22: * class BreakStatement
23: *
24: * @author: Jani Lehtimäki
25: */
26: public class BreakStatement extends Statement {
27:
28: private String _label = null;
29: private int _depth = 0;
30:
31: public BreakStatement(Statement parent, Location location) {
32: super (parent, location);
33: }
34:
35: public BreakStatement(Statement parent, Location location,
36: String label, int depth) {
37: super (parent, location);
38: _label = label;
39: _depth = depth;
40: }
41:
42: public int typeOf() {
43: return Statement.ST_BREAK;
44: }
45:
46: public String name() {
47: return "break";
48: }
49:
50: public void parse(TemplateParser parser, Tag tag) {
51: String label = tag.getValue("to");
52: if (label != null) {
53: _depth = getLabelDepth(label);
54: if (_depth == -1) {
55: parser.error(getLocation(), "Label '" + label
56: + "' is not declared");
57: return;
58: }
59: }
60: _label = label;
61: }
62:
63: public void check(ErrorListener context) {
64: }
65:
66: public Jumps eliminate(ErrorListener context) {
67: Jumps jumps = new Jumps();
68: if (_label != null) {
69: jumps.setBreak(_depth).setBlocked(true);
70: } else {
71: jumps.setBreak().setBlocked(true);
72: }
73: //jumps.print("break");
74: return jumps;
75: }
76:
77: public void compile(ByteCompiler context) {
78: boolean blocked = false;
79: Code code = context.getCode();
80: Labeled target = getLabeled(_label, false);
81: Statement stmt = this ;
82: while (!blocked && stmt != target) {
83: blocked = stmt.callFinalizer();
84: stmt = stmt.getParentStatement();
85: }
86: if (!blocked) {
87: code.go_to(target.getEndOfScope());
88: }
89: }
90:
91: public boolean isBlocked() {
92: return true;
93: }
94:
95: }
|