1 /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2 *
3 * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>.
4 *
5 * "Hilbert II" is free software; you can redistribute
6 * it and/or modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 package org.qedeq.kernel.bo.parser;
17
18 import java.util.Stack;
19
20 import org.qedeq.base.io.TextInput;
21
22 /**
23 * Remember TextInput positions.
24 *
25 * @author Michael Meyling
26 */
27 public class MementoTextInput {
28
29 /** For remembering input positions. */
30 private final Stack stack = new Stack();
31
32 /** Input source to parse. */
33 private final TextInput input;
34
35 /**
36 * Constructor.
37 *
38 * @param input Input source to parse.
39 */
40 public MementoTextInput(final TextInput input) {
41 this.input = input;
42 }
43
44 /**
45 * Remember current position.
46 */
47 public void markPosition() {
48 stack.push(new Integer(input.getPosition()));
49 }
50
51 /**
52 * Rewind to previous marked position. Also clears the mark.
53 *
54 * @return Current position before pop.
55 */
56 public long rewindPosition() {
57 final long oldPosition = getPosition();
58 input.setPosition(((Integer) stack.pop()).intValue());
59 return oldPosition;
60 }
61
62 /**
63 * Forget last remembered position.
64 */
65 public void clearMark() {
66 stack.pop();
67 }
68
69 /**
70 * Get byte position.
71 *
72 * @return Position.
73 */
74 public long getPosition() {
75 return input.getPosition();
76 }
77
78 /**
79 * Reads a single character and does not change the reading
80 * position.
81 *
82 * @return character read, if there are no more chars
83 * <code>-1</code> is returned
84 */
85 public int getChar() {
86 return input.getChar();
87 }
88
89 /**
90 * Reads a single character and increments the reading position
91 * by one.
92 *
93 * @return character read, if there are no more chars
94 * <code>-1</code> is returned
95 */
96 public int read() {
97 return input.read();
98 }
99
100 /**
101 * Are there still any characters to read?
102 *
103 * @return Anything left for reading further?
104 */
105 public final boolean isEmpty() {
106 return input.isEmpty();
107 }
108
109 /**
110 * Get rewind stack size.
111 *
112 * @return Rewind stack size.
113 */
114 public int getRewindStackSize() {
115 return stack.size();
116 }
117
118 /**
119 * Returns the current column number.
120 *
121 * @return Current column number (starting with line 1).
122 */
123 public int getColumn() {
124 return input.getColumn();
125 }
126
127 /**
128 * Returns the current line number.
129 *
130 * @return Current line number (starting with line 1).
131 */
132 public int getRow() {
133 return input.getRow();
134 }
135
136 /**
137 * Returns the current line.
138 *
139 * @return Current line.
140 */
141 public String getLine() {
142 return input.getLine();
143 }
144
145 /**
146 * Decrements the reading position by one and reads a single character.
147 * If no characters are left, <code>-1</code> is returned.
148 * Otherwise a cast to <code>char</code> gives the character read.
149 *
150 * @return Character read, if there are no more chars
151 * <code>-1</code> is returned.
152 */
153 public int readInverse() {
154 return input.readInverse();
155 }
156
157 }