~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/ada/adasupport.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <ctype.h>
 
2
#include <string.h>
 
3
#include <stdio.h>
 
4
#include <stdlib.h>
 
5
#include <stack>
 
6
#include <string>
 
7
#include <antlr/AST.hpp>
 
8
#include "AdaParser.hpp"
 
9
#include "AdaTokenTypes.hpp"
 
10
#include "adasupport.hpp"
 
11
 
 
12
#define eq !strcmp
 
13
 
 
14
using namespace std;
 
15
 
 
16
const RefAdaAST AdaAST::nullAdaAST(antlr::nullAST.get() );
 
17
 
 
18
using namespace std;
 
19
 
 
20
string text (const RefAdaAST& n)
 
21
{
 
22
  if (n == 0 || n == AdaAST::nullAdaAST)
 
23
    return "";
 
24
  string retval;
 
25
  int type = n->getType();
 
26
  if (type == AdaTokenTypes::DOT) {
 
27
    const RefAdaAST& sibs = n->down ();
 
28
    retval = text (sibs);
 
29
    retval.append (".");
 
30
    retval.append (text (sibs->right()));
 
31
  } else {
 
32
    retval = n->getText();
 
33
  }
 
34
  /*
 
35
  const RefAdaAST& r = n->right();
 
36
  if (r != 0 && r->getType () == AdaTokenTypes::DOT) {
 
37
    retval.append (".");
 
38
    retval.append (text (r->right()));
 
39
  }
 
40
   */
 
41
  return retval;
 
42
}
 
43
 
 
44
int txteq (RefAdaAST n1, RefAdaAST n2)
 
45
{
 
46
  if (!n1 || !n2 || n1 == antlr::nullAST || n2 == antlr::nullAST)
 
47
    return 0;
 
48
  const char* s1 = n1->getText().c_str();
 
49
  const char* s2 = n2->getText().c_str();
 
50
  if (strcasecmp (s1, s2) != 0)
 
51
    return 0;
 
52
  n1 = n1->right ();
 
53
  n2 = n2->right ();
 
54
  if (!n1 || !n2 || n1 == antlr::nullAST || n2 == antlr::nullAST)
 
55
    return 1;
 
56
  if (n1->getType () == AdaTokenTypes::DOT)
 
57
    if (n2->getType () == AdaTokenTypes::DOT)
 
58
      return txteq (n1->right (), n2->right ());
 
59
    else
 
60
      return 0;
 
61
  else if (n2->getType () == AdaTokenTypes::DOT)
 
62
    return 0;
 
63
  return 1;
 
64
}
 
65
 
 
66
std::stack<RefAdaAST> defid_stack;
 
67
 
 
68
void AdaParser::push_def_id (const RefAdaAST& defid)
 
69
{
 
70
#ifdef __DEBUG__
 
71
  string txt (text (defid));
 
72
  printf ("push_def_id: pushing %s\n", txt.c_str());
 
73
#endif
 
74
  defid_stack.push (defid);
 
75
}
 
76
 
 
77
const RefAdaAST& AdaParser::pop_def_id ()
 
78
{
 
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;
 
83
  }
 
84
  RefAdaAST& top = defid_stack.top ();
 
85
#ifdef __DEBUG__
 
86
  string txt (text (top));
 
87
  printf ("pop_def_id: popping %s\n", txt.c_str());
 
88
#endif
 
89
  defid_stack.pop ();
 
90
  return top;
 
91
}
 
92
 
 
93
bool AdaParser::end_id_matches_def_id (const RefAdaAST& endid)
 
94
{
 
95
  if (defid_stack.size() == 0)
 
96
    return false;
 
97
  RefAdaAST& top = defid_stack.top ();
 
98
  string defid (text (top));
 
99
  defid_stack.pop();
 
100
  if (endid == 0 || endid == antlr::nullAST)
 
101
    return false;
 
102
  string txt (text (endid));
 
103
  if (strcasecmp (defid.c_str (), txt.c_str ()) != 0) {
 
104
    string errtxt ("End id ");
 
105
    errtxt.append (txt);
 
106
    errtxt.append (" does not match ");
 
107
    errtxt.append (defid);
 
108
    reportError (errtxt);
 
109
    return false;
 
110
  }
 
111
#ifdef __DEBUG__
 
112
  printf ("end_id_matches_def_id: popped %s\n", txt.c_str());
 
113
#endif
 
114
  return true;
 
115
}
 
116
 
 
117
char * strtolower (char *string)
 
118
{
 
119
  char *p = string;
 
120
  if (!p)
 
121
    return NULL;
 
122
  while (*p)
 
123
  {
 
124
    if (isupper (*p))
 
125
      *p = tolower (*p);
 
126
    p++;
 
127
  }
 
128
  return string;
 
129
}
 
130
 
 
131
char * extracted_operator (const char *string)
 
132
{
 
133
  int len = strlen (string);
 
134
  static char op[10];
 
135
 
 
136
  if (len < 4 && len > 5 || *string != '"' || *(string + len - 1) != '"')
 
137
    return NULL;
 
138
 
 
139
  strcpy (op, string + 1);
 
140
  op[len - 2] = '\0';  /* discard ending quotation mark */
 
141
  strtolower (op);
 
142
  return op;
 
143
}
 
144
 
 
145
bool AdaParser::definable_operator (const char *string)
 
146
{                                 // operator_symbol sans "/="
 
147
  char *op = extracted_operator (string);
 
148
  if (op == NULL)
 
149
    return false;
 
150
  return
 
151
     (eq (op, "=") ||
 
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"));
 
158
}
 
159
 
 
160
bool AdaParser::is_operator_symbol (const char *string)
 
161
{
 
162
  char *op;
 
163
  if (definable_operator (string))
 
164
    return true;
 
165
  op = extracted_operator (string);
 
166
  return (eq (op, "/="));
 
167
}
 
168