2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
5 package com
.intellij
.lang
.pratt
;
7 import com
.intellij
.lang
.PsiBuilder
;
8 import com
.intellij
.psi
.tree
.IElementType
;
10 import java
.util
.LinkedList
;
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
;
24 public MutableMarker(final LinkedList
<IElementType
> path
, final PsiBuilder
.Marker startMarker
, final int initialPathLength
) {
26 myStartMarker
= startMarker
;
27 myInitialPathLength
= initialPathLength
;
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
;
44 public IElementType
getResultType() {
48 public void finish() {
49 assert myMode
== Mode
.READY
;
50 if (myResultType
== null) {
51 myMode
= Mode
.DROPPED
;
54 myMode
= Mode
.COMMITTED
;
55 myStartMarker
.done(myResultType
);
57 myPath
.addLast(myResultType
);
61 private void restorePath() {
62 while (myPath
.size() > myInitialPathLength
) {
67 public MutableMarker
precede() {
68 return new MutableMarker(myPath
, myStartMarker
.precede(), myInitialPathLength
);
71 public void finish(final IElementType type
) {
77 assert myMode
== Mode
.READY
;
78 myMode
= Mode
.DROPPED
;
82 public void rollback() {
83 assert myMode
== Mode
.READY
;
84 myMode
= Mode
.DROPPED
;
86 myStartMarker
.rollbackTo();
89 public void error(final String message
) {
90 assert myMode
== Mode
.READY
;
92 myStartMarker
.error(message
);