Skip to main content
summaryrefslogtreecommitdiffstats
blob: 498b7760e5846a7bc2d6c2baeb73f9b957776137 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.dom.lrparser.lpgextensions;

import java.util.LinkedList;

import lpg.lpgjavaruntime.ConfigurationStack;
import lpg.lpgjavaruntime.IntTuple;
import lpg.lpgjavaruntime.TokenStream;

class ParserState {
	private static final int STACK_INCREMENT = 1024;

	public int lastToken;
	public int currentAction;
	public IntTuple tokens;
	public int actionStack[];
	public int stateStackTop;
	public int[] stateStack;
	public int actionCount;
	public int totalCommits;

	public int[] parserLocationStack;
	public int[] undoStack;
	
	// Error recovery
	public int[] locationStack;
	public int repair_token;

	/**
	 * The number of trial actions that have been executed since the last backtrackable point was encountered.
	 */
	public int trialActionCount;
	
	/**
	 * A stack that contains the number of trial actions that were executed at different backtrackable points.
	 */
	public LinkedList<Integer> trialActionStack;

	/**
	 * Trial actions that have been executed but not yet committed.
	 */
	@SuppressWarnings("unchecked")
	public LinkedList pendingCommits;

	public ConfigurationStack configurationStack;

	public int act;

	public int curtok;

	public ParserState(int startState, TokenStream tokStream) {
		reallocateStateStack();
		stateStackTop = 0;
		stateStack[0] = startState;

		//
		// The tuple tokens will eventually contain the sequence
		// of tokens that resulted in a successful parse. We leave
		// it up to the "Stream" implementer to define the predecessor
		// of the first token as he sees fit.
		//
		tokStream.reset(); // Position at first token.
		tokens = new IntTuple(tokStream.getStreamLength());
		tokens.add(tokStream.getPrevious(tokStream.peek()));
	}

	public void allocateOtherStacks() {
		locationStack = new int[stateStack.length];
	}

	public void reallocateStateStack() {
		int old_stack_length = (stateStack == null ? 0 : stateStack.length), stack_length = old_stack_length + STACK_INCREMENT;
		if (stateStack == null)
			stateStack = new int[stack_length];
		else
			System.arraycopy(stateStack, 0, stateStack = new int[stack_length], 0, old_stack_length);
		return;
	}

	//
	// Allocate or reallocate all the stacks. Their sizes should always be the
	// same.
	//
	public void reallocateOtherStacks(int start_token_index) {
		// assert(stateStack != null);
		if (this.actionStack == null) {
			this.actionStack = new int[stateStack.length];
			locationStack = new int[stateStack.length];

			actionStack[0] = 0;
			undoStack = new int[stateStack.length];
			
			locationStack[0] = start_token_index;
			
			parserLocationStack = new int[stateStack.length];
			parserLocationStack[0] = start_token_index;
			
			
		} else if (this.actionStack.length < stateStack.length) {
			int old_length = this.actionStack.length;

			System.arraycopy(this.actionStack, 0, this.actionStack = new int[stateStack.length], 0, old_length);
			System.arraycopy(this.undoStack, 0, this.undoStack = new int[stateStack.length], 0, old_length);
			System.arraycopy(locationStack, 0, locationStack = new int[stateStack.length], 0, old_length);
		}
		return;
	}
	
	
	@SuppressWarnings("nls")
	public void dumpState() {
		System.out.print(curtok);
		System.out.print("\t");
		System.out.print(act);
		System.out.print("\t");
		dump(stateStack, stateStackTop);
		System.out.print("\t");
		dump(parserLocationStack, stateStackTop);
		System.out.println();
	}
	
	@SuppressWarnings("nls")
	private void dump(int[] array, int limit) {
		System.out.print("[");
		for (int i = 0; i < limit; i++) {
			if (i > 0) {
				System.out.print(", ");
			}
			System.out.print(array[i]);
		}
		System.out.print("]");
	}
}

Back to the top