• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include "STreeParser.h"
3 
4 void STreeParser::
MATCH(SORASTBase * _t,int tok)5 MATCH(SORASTBase *_t,int tok)
6 {
7 	if ( _t->type()!=tok )
8 	{
9 		if ( guessing ) _GUESS_FAIL;
10 		else mismatched_token(tok, _t);
11 	}
12 }
13 
14 void STreeParser::
MATCHRANGE(SORASTBase * _t,int tok,int tok2)15 MATCHRANGE(SORASTBase *_t,int tok,int tok2)
16 {
17 	if ( _t->type()<tok || _t->type()>tok2 )
18 	{
19 		if ( guessing ) _GUESS_FAIL;
20 		else mismatched_range(tok, tok2, _t);
21 	}
22 }
23 
24 void STreeParser::
WILDCARD(SORASTBase * _t)25 WILDCARD(SORASTBase *_t)
26 {
27 	if ( _t==NULL )
28 	{
29 		if ( guessing ) _GUESS_FAIL;
30 		else missing_wildcard();
31 	}
32 }
33 
34 void STreeParser::
mismatched_range(int looking_for,int upper_token,SORASTBase * found)35 mismatched_range(int looking_for, int upper_token, SORASTBase *found)
36 {
37 	if ( found!=NULL ) {
38 		fprintf(stderr,
39 				"parse error: expected token range %d..%d found token %d\n",
40 				looking_for, upper_token,
41 				found->type());
42 	}
43 	else {
44 		fprintf(stderr,
45 				"parse error: expected token range %d..%d found NULL tree\n",
46 				looking_for, upper_token);
47 	}
48 }
49 
50 void STreeParser::
missing_wildcard()51 missing_wildcard()
52 {
53 	fprintf(stderr, "parse error: expected any token/tree found found NULL tree\n");
54 }
55 
56 void STreeParser::
mismatched_token(int looking_for,SORASTBase * found)57 mismatched_token(int looking_for, SORASTBase *found)
58 {
59 	if ( found!=NULL ) {
60 		fprintf(stderr,
61 				"parse error: expected token %d found token %d\n",
62 				looking_for,
63 				found->type());
64 	}
65 	else {
66 		fprintf(stderr,
67 				"parse error: expected token %d found NULL tree\n",
68 				looking_for);
69 	}
70 }
71 
72 void STreeParser::
no_viable_alt(char * rulename,SORASTBase * root)73 no_viable_alt(char *rulename, SORASTBase *root)
74 {
75 	if ( root==NULL )
76 		fprintf(stderr,
77 				"parse error: in rule %s, no viable alternative for NULL tree\n",
78 				rulename);
79 	else
80 		fprintf(stderr,
81 				"parse error: in rule %s, no viable alternative for tree\n",
82 				rulename);
83 }
84 
85 void STreeParser::
panic(char * err)86 panic(char *err)
87 {
88 	fprintf(stderr, "panic: %s\n", err);
89 	exit(-1);
90 }
91 
92 void STreeParser::
save_state(STreeParser * buf)93 save_state(STreeParser *buf)
94 {
95 	buf->try_ok = this->try_ok;
96 	buf->sjrv = this->sjrv;
97 	buf->guessing = this->guessing;
98 	buf->startofguess = this->startofguess;
99 }
100 
101 void STreeParser::
restore_state(STreeParser * buf)102 restore_state(STreeParser *buf)
103 {
104 	this->try_ok = buf->try_ok;
105 	this->sjrv = buf->sjrv;
106 	this->guessing = buf->guessing;
107 	this->startofguess = buf->startofguess;
108 }
109 
110 void STreeParser::
_mkroot(SORASTBase ** r,SORASTBase ** s,SORASTBase ** e,SORASTBase * t)111 _mkroot(SORASTBase **r, SORASTBase **s, SORASTBase **e, SORASTBase *t)
112 {
113 	*r = t;
114 }
115 
116 void STreeParser::
_mkchild(SORASTBase ** r,SORASTBase ** s,SORASTBase ** e,SORASTBase * t)117 _mkchild(SORASTBase **r, SORASTBase **s, SORASTBase **e, SORASTBase *t)
118 {
119 #ifdef BEFORE_GARYS_FIX
120 	/* if no sibling list, must attach to any existing root */
121 	if ( *s==NULL )
122 	{
123 		*s = *e = t;
124 		/* If r is NULL, then there was no root defined--must be sibling list */
125 		if ( *r==NULL ) *r = *s;
126 		else (*r)->setDown(t);
127 	}
128 	else { (*e)->setRight(t); *e = t; }
129 #endif
130 /*
131 	should do nothing if asked to add a NULL argument.  NULL's come up
132 	when a rule wants to return "nothing".
133 */
134 	/* if no sibling list, must attach to any existing root */
135 	if (*s == NULL)
136 	{
137 		*s = *e = t;
138 		// If r is NULL then there was no root defined--must be sibling list
139 		if (*r == NULL)	*r = *s;
140 		else (*r)->setDown(t);
141 	}
142 	else if (*e != NULL)
143 	{
144 		(*e)->setRight(t);
145 		*e = t;
146 	}
147 	if (*e != NULL) {
148 		while ((*e)->right() != NULL) *e = (SORASTBase *)(*e)->right();
149 	}
150 }
151 
152