7
#include <antlr/AST.hpp>
8
#include "AdaParser.hpp"
9
#include "AdaTokenTypes.hpp"
10
#include "adasupport.hpp"
16
const RefAdaAST AdaAST::nullAdaAST(antlr::nullAST.get() );
20
string text (const RefAdaAST& n)
22
if (n == 0 || n == AdaAST::nullAdaAST)
25
int type = n->getType();
26
if (type == AdaTokenTypes::DOT) {
27
const RefAdaAST& sibs = n->down ();
30
retval.append (text (sibs->right()));
32
retval = n->getText();
35
const RefAdaAST& r = n->right();
36
if (r != 0 && r->getType () == AdaTokenTypes::DOT) {
38
retval.append (text (r->right()));
44
int txteq (RefAdaAST n1, RefAdaAST n2)
46
if (!n1 || !n2 || n1 == antlr::nullAST || n2 == antlr::nullAST)
48
const char* s1 = n1->getText().c_str();
49
const char* s2 = n2->getText().c_str();
50
if (strcasecmp (s1, s2) != 0)
54
if (!n1 || !n2 || n1 == antlr::nullAST || n2 == antlr::nullAST)
56
if (n1->getType () == AdaTokenTypes::DOT)
57
if (n2->getType () == AdaTokenTypes::DOT)
58
return txteq (n1->right (), n2->right ());
61
else if (n2->getType () == AdaTokenTypes::DOT)
66
std::stack<RefAdaAST> defid_stack;
68
void AdaParser::push_def_id (const RefAdaAST& defid)
71
string txt (text (defid));
72
printf ("push_def_id: pushing %s\n", txt.c_str());
74
defid_stack.push (defid);
77
const RefAdaAST& AdaParser::pop_def_id ()
79
if (defid_stack.size() == 0) {
80
fprintf (stderr, "pop_def_id() called on empty stack\n");
81
// return static_cast<RefAdaAST>(antlr::nullAST);
82
return AdaAST::nullAdaAST;
84
RefAdaAST& top = defid_stack.top ();
86
string txt (text (top));
87
printf ("pop_def_id: popping %s\n", txt.c_str());
93
bool AdaParser::end_id_matches_def_id (const RefAdaAST& endid)
95
if (defid_stack.size() == 0)
97
RefAdaAST& top = defid_stack.top ();
98
string defid (text (top));
100
if (endid == 0 || endid == antlr::nullAST)
102
string txt (text (endid));
103
if (strcasecmp (defid.c_str (), txt.c_str ()) != 0) {
104
string errtxt ("End id ");
106
errtxt.append (" does not match ");
107
errtxt.append (defid);
108
reportError (errtxt);
112
printf ("end_id_matches_def_id: popped %s\n", txt.c_str());
117
char * strtolower (char *string)
131
char * extracted_operator (const char *string)
133
int len = strlen (string);
136
if (len < 4 && len > 5 || *string != '"' || *(string + len - 1) != '"')
139
strcpy (op, string + 1);
140
op[len - 2] = '\0'; /* discard ending quotation mark */
145
bool AdaParser::definable_operator (const char *string)
146
{ // operator_symbol sans "/="
147
char *op = extracted_operator (string);
152
eq (op, "<") || eq (op, ">") ||
153
eq (op, "<=") || eq (op, ">=") ||
154
eq (op, "&") || eq (op, "**") ||
155
eq (op, "*") || eq (op, "/") || eq (op, "+") || eq (op, "-") ||
156
eq (op, "abs") || eq (op, "rem") || eq (op, "mod") ||
157
eq (op, "and") || eq (op, "or") || eq (op, "xor") || eq (op, "not"));
160
bool AdaParser::is_operator_symbol (const char *string)
163
if (definable_operator (string))
165
op = extracted_operator (string);
166
return (eq (op, "/="));