Break out the debug and string functions from qc-lex.l.

This commit is contained in:
Bill Currie 2011-01-06 14:57:40 +09:00
parent 695f71eeb6
commit a09f02db1a
5 changed files with 255 additions and 248 deletions

View file

@ -34,6 +34,9 @@
#include "QF/pr_debug.h"
void push_source_file (void);
void pop_source_file (void);
void line_info (char *text);
pr_auxfunction_t *new_auxfunction (void);
pr_lineno_t *new_lineno (void);
struct ddef_s *new_local (void);

View file

@ -120,6 +120,8 @@ const char *strip_path (const char *filename);
*/
const char *save_string (const char *str);
const char *make_string (char *token, char **end);
void clear_frame_macros (void);
extern FILE *yyin;
int yyparse (void);

View file

@ -41,12 +41,78 @@ static __attribute__ ((used)) const char rcsid[] =
# include <strings.h>
#endif
#include <stdlib.h>
#include <ctype.h>
#include "QF/pr_comp.h"
#include "debug.h"
#include "expr.h"
#include "immediate.h"
#include "qfcc.h"
static srcline_t *free_srclines;
void
push_source_file (void)
{
srcline_t *srcline;
ALLOC (16, srcline_t, srclines, srcline);
srcline->source_file = pr.source_file;
srcline->source_line = pr.source_line;
srcline->next = pr.srcline_stack;
pr.srcline_stack = srcline;
}
void
pop_source_file (void)
{
srcline_t *tmp;
if (!pr.srcline_stack) {
notice (0, "unbalanced #includes. bug in preprocessor?");
return;
}
tmp = pr.srcline_stack;
pr.srcline_stack = tmp->next;
tmp->next = free_srclines;
free_srclines = tmp;
}
void
line_info (char *text)
{
char *p;
char *s;
const char *str;
int line;
int flags;
p = text;
line = strtol (p, &s, 10);
p = s;
while (isspace ((unsigned char)*p))
p++;
if (!*p)
error (0, "Unexpected end of file");
str = make_string (p, &s); // grab the filename
p = s;
while (isspace ((unsigned char) *p))
p++;
flags = strtol (p, &s, 10);
switch (flags) {
case 1:
push_source_file ();
break;
case 2:
pop_source_file ();
break;
}
while (*p && *p != '\n') // ignore rest
p++;
pr.source_line = line - 1;
pr.source_file = ReuseString (strip_path (str));
}
pr_auxfunction_t *
new_auxfunction (void)
{

View file

@ -49,12 +49,14 @@ static __attribute__ ((used)) const char rcsid[] =
#include <QF/sys.h>
#include "qfcc.h"
#include "debug.h"
#include "expr.h"
#include "class.h"
#include "immediate.h"
#include "options.h"
#include "struct.h"
#include "type.h"
#include "qc-parse.h"
#ifndef YY_PROTO
@ -82,10 +84,6 @@ YY_DECL;
int type_or_name (char *token);
int do_grab (char *token);
void add_frame_macro (char *token);
const char *make_string (char *token, char **end);
void push_source_file (void);
void pop_source_file (void);
void line_info (char *text);
extern YYSTYPE yylval;
@ -472,250 +470,6 @@ clear_frame_macros (void)
Hash_FlushTable (frame_tab);
}
const char *
make_string (char *token, char **end)
{
char s[2];
int c;
int i;
int mask;
int boldnext;
int quote;
static dstring_t *str;
if (!str)
str = dstring_newstr ();
dstring_clearstr (str);
s[1] = 0;
mask = 0x00;
boldnext = 0;
quote = *token++;
do {
c = *token++;
if (!c)
error (0, "EOF inside quote");
if (c == '\n')
error (0, "newline inside quote");
if (c == '\\') { // escape char
c = *token++;
if (!c)
error (0, "EOF inside quote");
switch (c) {
case '\\':
c = '\\';
break;
case 'n':
c = '\n';
break;
case '"':
c = '\"';
break;
case '\'':
c = '\'';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
if (!options.qccx_escapes) {
for (i = c = 0; i < 3
&& *token >= '0'
&& *token <= '7'; i++, token++) {
c *= 8;
c += *token - '0';
}
if (!*token)
error (0, "EOF inside quote");
break;
}
case '8':
case '9':
c = 18 + c - '0';
break;
case 'x':
c = 0;
while (*token && isxdigit ((unsigned char)*token)) {
c *= 16;
if (*token <= '9')
c += *token - '0';
else if (*token <= 'F')
c += *token - 'A' + 10;
else
c += *token - 'a' + 10;
token++;
}
if (!*token)
error (0, "EOF inside quote");
break;
case 'a':
c = '\a';
break;
case 'b':
if (options.qccx_escapes)
mask ^= 0x80;
else
c = '\b';
break;
case 'e':
c = '\033';
break;
case 'f':
c = '\f';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = '\v';
break;
case '^':
if (*token == '\"')
error (0, "Unexpected end of string after \\^");
boldnext = 1;
continue;
case '[':
c = 0x90; // gold [
break;
case ']':
c = 0x91; // gold ]
break;
case '.':
c = 28; // center dot
break;
case '<':
if (options.qccx_escapes)
c = 29; // brown left end
else
mask = 0x80;
continue;
case '-':
c = 30; // brown center bit
break;
case '>':
if (options.qccx_escapes)
c = 31; // broun right end
else
mask = 0x00;
continue;
case '(':
c = 128; // left slider end
break;
case '=':
c = 129; // slider center
break;
case ')':
c = 130; // right slider end
break;
case '{':
c = 0;
while (*token && *token != '}'
&& isdigit ((unsigned char)*token)) {
c *= 10;
c += *token++ - '0';
}
if (!*token)
error (0, "EOF inside quote");
if (*token != '}')
error (0, "non-digit inside \\{}");
else
token++;
if (c > 255)
warning (0, "\\{%d} > 255", c);
break;
default:
error (0, "Unknown escape char");
break;
}
} else if (c == quote) {
break;
}
if (boldnext)
c = c ^ 0x80;
boldnext = 0;
c = c ^ mask;
s[0] = c;
dstring_appendstr (str, s);
} while (1);
if (end)
*end = token;
return save_string (str->str);
}
static srcline_t *free_srclines;
void
push_source_file (void)
{
srcline_t *srcline;
ALLOC (16, srcline_t, srclines, srcline);
srcline->source_file = pr.source_file;
srcline->source_line = pr.source_line;
srcline->next = pr.srcline_stack;
pr.srcline_stack = srcline;
}
void
pop_source_file (void)
{
srcline_t *tmp;
if (!pr.srcline_stack) {
notice (0, "unbalanced #includes. bug in preprocessor?");
return;
}
tmp = pr.srcline_stack;
pr.srcline_stack = tmp->next;
tmp->next = free_srclines;
free_srclines = tmp;
}
void
line_info (char *text)
{
char *p;
char *s;
const char *str;
int line;
int flags;
p = text;
line = strtol (p, &s, 10);
p = s;
while (isspace ((unsigned char)*p))
p++;
if (!*p)
error (0, "Unexpected end of file");
str = make_string (p, &s); // grab the filename
p = s;
while (isspace ((unsigned char) *p))
p++;
flags = strtol (p, &s, 10);
switch (flags) {
case 1:
push_source_file ();
break;
case 2:
pop_source_file ();
break;
}
while (*p && *p != '\n') // ignore rest
p++;
pr.source_line = line - 1;
pr.source_file = ReuseString (strip_path (str));
}
#ifdef YY_FLEX_REALLOC_HACK
static __attribute__ ((used)) void *(*const yy_flex_realloc_hack)(void *,yy_size_t) = yy_flex_realloc;
#else

View file

@ -56,6 +56,7 @@ static __attribute__ ((used)) const char rcsid[] =
#endif
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <QF/cbuf.h>
@ -127,6 +128,187 @@ save_string (const char *str)
return s;
}
const char *
make_string (char *token, char **end)
{
char s[2];
int c;
int i;
int mask;
int boldnext;
int quote;
static dstring_t *str;
if (!str)
str = dstring_newstr ();
dstring_clearstr (str);
s[1] = 0;
mask = 0x00;
boldnext = 0;
quote = *token++;
do {
c = *token++;
if (!c)
error (0, "EOF inside quote");
if (c == '\n')
error (0, "newline inside quote");
if (c == '\\') { // escape char
c = *token++;
if (!c)
error (0, "EOF inside quote");
switch (c) {
case '\\':
c = '\\';
break;
case 'n':
c = '\n';
break;
case '"':
c = '\"';
break;
case '\'':
c = '\'';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
if (!options.qccx_escapes) {
for (i = c = 0; i < 3
&& *token >= '0'
&& *token <= '7'; i++, token++) {
c *= 8;
c += *token - '0';
}
if (!*token)
error (0, "EOF inside quote");
break;
}
case '8':
case '9':
c = 18 + c - '0';
break;
case 'x':
c = 0;
while (*token && isxdigit ((unsigned char)*token)) {
c *= 16;
if (*token <= '9')
c += *token - '0';
else if (*token <= 'F')
c += *token - 'A' + 10;
else
c += *token - 'a' + 10;
token++;
}
if (!*token)
error (0, "EOF inside quote");
break;
case 'a':
c = '\a';
break;
case 'b':
if (options.qccx_escapes)
mask ^= 0x80;
else
c = '\b';
break;
case 'e':
c = '\033';
break;
case 'f':
c = '\f';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = '\v';
break;
case '^':
if (*token == '\"')
error (0, "Unexpected end of string after \\^");
boldnext = 1;
continue;
case '[':
c = 0x90; // gold [
break;
case ']':
c = 0x91; // gold ]
break;
case '.':
c = 28; // center dot
break;
case '<':
if (options.qccx_escapes)
c = 29; // brown left end
else
mask = 0x80;
continue;
case '-':
c = 30; // brown center bit
break;
case '>':
if (options.qccx_escapes)
c = 31; // broun right end
else
mask = 0x00;
continue;
case '(':
c = 128; // left slider end
break;
case '=':
c = 129; // slider center
break;
case ')':
c = 130; // right slider end
break;
case '{':
c = 0;
while (*token && *token != '}'
&& isdigit ((unsigned char)*token)) {
c *= 10;
c += *token++ - '0';
}
if (!*token)
error (0, "EOF inside quote");
if (*token != '}')
error (0, "non-digit inside \\{}");
else
token++;
if (c > 255)
warning (0, "\\{%d} > 255", c);
break;
default:
error (0, "Unknown escape char");
break;
}
} else if (c == quote) {
break;
}
if (boldnext)
c = c ^ 0x80;
boldnext = 0;
c = c ^ mask;
s[0] = c;
dstring_appendstr (str, s);
} while (1);
if (end)
*end = token;
return save_string (str->str);
}
#ifdef _WIN32
char *
fix_backslash (char *path)