parser is cooler than a Pig Tail's!
[fedora-idea.git] / lang-impl / src / com / intellij / lang / pratt / MutableMarker.java
blobe1287e46023e183fda1375c73601b370fea7985b
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 private final PsiBuilder.Marker myStartMarker;
17 private IElementType myResultType;
18 private int myInitialPathLength;
19 private LinkedList<IElementType> myPath;
20 private boolean myFinished;
22 public MutableMarker(final LinkedList<IElementType> path, final PsiBuilder.Marker startMarker, final int initialPathLength) {
23 myPath = path;
24 myStartMarker = startMarker;
25 myInitialPathLength = initialPathLength;
28 public MutableMarker setResultType(final IElementType resultType) {
29 myResultType = resultType;
30 return this;
33 public IElementType getResultType() {
34 return myResultType;
37 public void finish() {
38 assert !myFinished;
39 myFinished = true;
41 if (myResultType == null) {
42 myStartMarker.drop();
43 } else {
44 myStartMarker.done(myResultType);
45 restorePath();
46 myPath.addLast(myResultType);
50 private void restorePath() {
51 while (myPath.size() > myInitialPathLength) {
52 myPath.removeLast();
56 public MutableMarker precede() {
57 return new MutableMarker(myPath, myStartMarker.precede(), myInitialPathLength);
60 public void finish(final IElementType type) {
61 setResultType(type);
62 finish();
65 public void drop() {
66 assert !myFinished;
67 myFinished = true;
68 myStartMarker.drop();
71 public void rollback() {
72 assert !myFinished;
73 myFinished = true;
74 restorePath();
75 myStartMarker.rollbackTo();
78 public void error(final String message) {
79 assert !myFinished;
80 myFinished = true;
81 myStartMarker.error(message);