comparison anagram/agcore/token.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children 607e3be6bad8
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Programming
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * token.cpp
7 */
8
9 #include "q1glbl.h"
10 #include "rule.h"
11 #include "token.h"
12
13 //#define INCLUDE_LOGGING
14 #include "log.h"
15
16
17 Token::Map map_token_number;
18 const int Token::first = 1;
19
20 token_number_map &Token::Map::operator [] (unsigned x) {
21 //LOGSECTION("Token::Map::operator []");
22 //LOGV(x) LCV(Token::descriptorList.size());
23 assert(x < Token::descriptorList.size());
24 return Token::descriptorList[x];
25 }
26
27 token_number_map::token_number_map()
28 : key()
29 , vp_prod_number(0)
30 , value_type(0)
31 //, value_type(void_token_type)
32 , token_set_id(0)
33 , part_number(0)
34 , precedence_level(0)
35 , sticky(0)
36 , zero_length_flag(0)
37 , non_terminal_flag(0)
38 , sem_det_flag(0)
39 , fine_structure(0)
40 , left_associative(0)
41 , right_associative(0)
42 , rp_arg(0)
43 , pure(0)
44 , junky(0)
45 , reserve(0)
46 , reserved_word(0)
47 , subgrammar(0)
48 , lexeme(0)
49 , lexical(0)
50 , immediate_action(0)
51 , operatorCandidate(0)
52 , disregard(0)
53 {
54 LOGSECTION("token_number_map::token_number_map");
55 }
56
57 Token::Token(int x) : ObjectRegister<token_number_map>(x) {}
58 Token::Token(const Token &t) : ObjectRegister<token_number_map>(t) {}
59
60
61 Token Token::create() {
62 LOGSECTION("Token::create");
63 ntkns = descriptorList.size();
64 descriptorList.push(token_number_map());
65 //LOGV(netAllocations());
66 Token token(ntkns);
67 //LOGV(netAllocations());
68 LOGV(token) LCV(ntkns) LCV(descriptorList.size())
69 LCV(token->value_type) LCV(token->token_name);
70 return token;
71 }
72
73 void Token::reset() {
74 LOGSECTION("Token::reset");
75 ntkns = descriptorList.size();
76 descriptorList.reset().push(token_number_map());
77 LOGV(ntkns) LCV(descriptorList.size());
78 }
79
80 Rule Token::rule(int x) const {
81 //return descriptorList[index].rule(x);
82 return descriptor->ruleList[x];
83 }
84
85 Rule Token::expansionRule(int x) const {
86 //return descriptorList[index].expansionRule(x);
87 return descriptor->expansionRuleList[x];
88 }
89
90 Token Token::follower(int x) const {
91 return descriptor->followerList[x];
92 }
93
94 Token Token::leadingToken(int x) const {
95 return descriptor->leadingTokenList[x];
96 }
97
98 int Token::isLeadingToken(const Token candidate) const {
99 LOGSECTION("Token::isLeadingToken");
100 LOGV(index) LCV(candidate.index);
101
102 int nt;
103
104 if (index == candidate.index) {
105 return 1;
106 }
107
108 AgArray<Token> leadingTokenList = descriptor->leadingTokenList;
109 nt = leadingTokenList.size();
110 LOGV(nt);
111
112 while (nt--) {
113 LOGV(index) LCV(leadingTokenList[nt].index);
114 if (candidate.index == leadingTokenList[nt].index) {
115 return 1;
116 }
117 }
118
119 return 0;
120 }
121
122 int Token::isExpansionToken(const Token candidate) const {
123 int nf;
124
125 if (index == candidate.index) {
126 return 1;
127 }
128
129 //token_number_map &descriptor = descriptorList[index];
130 nf = descriptor->expansionRuleList.size();
131 for (int i = 0; i < nf; i++) {
132 Rule rule = descriptor->expansionRuleList[i];
133 int k = rule->length();
134 if (k == 0) {
135 continue;
136 }
137 if (candidate == rule.token(0)) {
138 return 1;
139 }
140 }
141 return 0;
142 }
143
144 int Token::isExpansionRule(const Rule rule) const {
145 //token_number_map &descriptor = descriptorList[index];
146 unsigned nf = descriptor->expansionRuleList.size();
147
148 while (nf--) {
149 if (descriptor->expansionRuleList[nf] == rule) {
150 return 1;
151 }
152 }
153
154 return 0;
155 }