aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/antlr3_cpp_runtime/include/antlr3treeparser.inl
blob: 8ad71d6474415b59e9c9740e909e3ff1c9f373ea (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
namespace antlr3 {

template< class ImplTraits >
TreeParser<ImplTraits>::TreeParser( ANTLR_UINT32 sizeHint, TreeNodeStreamType* ctnstream,
											RecognizerSharedStateType* state)
											:RecognizerType( sizeHint, state )
{
	/* Install the tree node stream
	*/
	this->setTreeNodeStream(ctnstream);

}

template< class ImplTraits >
TreeParser<ImplTraits>::~TreeParser()
{
	this->get_rec()->get_state()->get_following().clear();
}

template< class ImplTraits >
typename TreeParser<ImplTraits>::TreeNodeStreamType* TreeParser<ImplTraits>::get_ctnstream() const
{
	return m_ctnstream;
}

template< class ImplTraits >
typename TreeParser<ImplTraits>::IntStreamType* TreeParser<ImplTraits>::get_istream() const
{
	return m_ctnstream;
}

template< class ImplTraits >
typename TreeParser<ImplTraits>::IntStreamType* TreeParser<ImplTraits>::get_parser_istream() const
{
	return m_ctnstream;
}

template< class ImplTraits >
typename TreeParser<ImplTraits>::RecognizerType* TreeParser<ImplTraits>::get_rec()
{
	return this;
}

template< class ImplTraits >
void TreeParser<ImplTraits>::fillExceptionData( ExceptionBaseType* ex )
{
	auto xxx = m_ctnstream->LT(1);
	//ex->set_token( m_ctnstream->LT(1) );	    /* Current input tree node */
	ex->set_line( ex->get_token()->get_line() );
	ex->set_charPositionInLine( ex->get_token()->get_charPositionInLine() );
	ex->set_index( m_ctnstream->index() );

	// Are you ready for this? Deep breath now...
	//
	{
		TreeTypePtr tnode;

		tnode		= ex->get_token();

		if	(tnode->get_token()    == NULL)
		{
			ex->set_streamName("-unknown source-" );
		}
		else
		{
			if	( tnode->get_token()->get_input() == NULL)
			{
				ex->set_streamName("");
			}
			else
			{
				ex->set_streamName(	tnode->get_token()->get_input()->get_fileName() );
			}
		}
		ex->set_message("Unexpected node");
	}
}

template< class ImplTraits >
void TreeParser<ImplTraits>::displayRecognitionError( ANTLR_UINT8** /*tokenNames*/, ExceptionBaseType* ex )
{
	typename ImplTraits::StringStreamType errtext;
	// See if there is a 'filename' we can use
	//
	if( ex->get_streamName().empty() )
	{
		if(ex->get_token()->get_type() == ImplTraits::CommonTokenType::TOKEN_EOF)
		{
			errtext << "-end of input-(";
		}
		else
		{
			errtext << "-unknown source-(";
		}
	}
	else
	{
		errtext << ex->get_streamName() << "(";
	}

	// Next comes the line number
	//
	errtext << this->get_rec()->get_state()->get_exception()->get_line() << ") ";
	errtext << " : error " << this->get_rec()->get_state()->get_exception()->getType()
							<< " : "
							<< this->get_rec()->get_state()->get_exception()->get_message();

	IntStreamType* is			= this->get_istream();
	TreeTypePtr theBaseTree	= this->get_rec()->get_state()->get_exception()->get_token();
	StringType ttext		= theBaseTree->toStringTree();

	if  (theBaseTree != NULL)
	{
		TreeTypePtr  theCommonTree	=  static_cast<TreeTypePtr>(theBaseTree);
		if	(theCommonTree != NULL)
		{
			CommonTokenType* theToken	= theBaseTree->getToken();
		}
		errtext << ", at offset "
			    << theBaseTree->getCharPositionInLine();
		errtext << ", near " << ttext;
	}
	ex->displayRecognitionError( errtext );
	ImplTraits::displayRecognitionError( errtext.str() );
}

template< class ImplTraits >
void	TreeParser<ImplTraits>::setTreeNodeStream(TreeNodeStreamType* input)
{
	m_ctnstream = input;
    this->get_rec()->reset();
    m_ctnstream->reset();
}

template< class ImplTraits >
typename TreeParser<ImplTraits>::TreeNodeStreamType* TreeParser<ImplTraits>::getTreeNodeStream()
{
	return m_ctnstream;
}

template< class ImplTraits >
void TreeParser<ImplTraits>::exConstruct()
{
	new ANTLR_Exception<ImplTraits, MISMATCHED_TREE_NODE_EXCEPTION, TreeNodeStreamType>( this->get_rec(), "" );
}

template< class ImplTraits >
void TreeParser<ImplTraits>::mismatch(ANTLR_UINT32 ttype, BitsetListType* follow)
{
	this->exConstruct();
    this->recoverFromMismatchedToken(ttype, follow);
}

template< class ImplTraits >
typename TreeParser<ImplTraits>::TokenType*
TreeParser<ImplTraits>::getMissingSymbol( IntStreamType* istream, ExceptionBaseType*		/*e*/,
					  ANTLR_UINT32	 expectedTokenType, BitsetListType*	/*follow*/)
{
	TreeNodeStreamType*		tns;
	TreeTypePtr				node;
	TreeTypePtr				current;
	CommonTokenType*		token;
	StringType				text;

	// Dereference the standard pointers
	//
    tns	    = static_cast<TreeNodeStreamType*>(istream);

	// Create a new empty node, by stealing the current one, or the previous one if the current one is EOF
	//
	current	= tns->LT(1);

	if	(current == tns->get_EOF_NODE_p())
	{
		current = tns->LT(-1);
	}
	node	= current->dupNode();

	// Find the newly dupicated token
	//
	token	= node->getToken();

	// Create the token text that shows it has been inserted
	//
	token->setText("<missing ");
	text = token->getText();
	text.append((const char *)this->get_rec()->get_state()->get_tokenName(expectedTokenType));
	text.append((const char *)">");

	// Finally return the pointer to our new node
	//
	return	node;
}


}