marker impro
[fedora-idea.git] / lang-impl / src / com / intellij / lang / pratt / MutableMarker.java
blob39e8940fe83eec80bbbadab761209f9c717aaa85
1 /*
2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
4 */
5 package com.intellij.lang.pratt;
7 import com.intellij.lang.PsiBuilder;
8 import com.intellij.psi.tree.IElementType;
10 import java.util.LinkedList;
12 /**
13 * @author peter
15 public class MutableMarker {
16 enum Mode { READY, DROPPED, COMMITTED, ERROR }
18 private final PsiBuilder.Marker myStartMarker;
19 private IElementType myResultType;
20 private int myInitialPathLength;
21 private LinkedList<IElementType> myPath;
22 private Mode myMode;
24 public MutableMarker(final LinkedList<IElementType> path, final PsiBuilder.Marker startMarker, final int initialPathLength) {
25 myPath = path;
26 myStartMarker = startMarker;
27 myInitialPathLength = initialPathLength;
28 myMode = Mode.READY;
31 public boolean isCommitted() {
32 return myMode == Mode.COMMITTED;
35 public boolean isDropped() {
36 return myMode == Mode.DROPPED || myMode == Mode.ERROR;
39 public MutableMarker setResultType(final IElementType resultType) {
40 myResultType = resultType;
41 return this;
44 public IElementType getResultType() {
45 return myResultType;
48 public void finish() {
49 assert myMode == Mode.READY;
50 if (myResultType == null) {
51 myMode = Mode.DROPPED;
52 myStartMarker.drop();
53 } else {
54 myMode = Mode.COMMITTED;
55 myStartMarker.done(myResultType);
56 restorePath();
57 myPath.addLast(myResultType);
61 private void restorePath() {
62 while (myPath.size() > myInitialPathLength) {
63 myPath.removeLast();
67 public MutableMarker precede() {
68 return new MutableMarker(myPath, myStartMarker.precede(), myInitialPathLength);
71 public void finish(final IElementType type) {
72 setResultType(type);
73 finish();
76 public void drop() {
77 assert myMode == Mode.READY;
78 myMode = Mode.DROPPED;
79 myStartMarker.drop();
82 public void rollback() {
83 assert myMode == Mode.READY;
84 myMode = Mode.DROPPED;
85 restorePath();
86 myStartMarker.rollbackTo();
89 public void error(final String message) {
90 assert myMode == Mode.READY;
91 myMode = Mode.ERROR;
92 myStartMarker.error(message);