NetBSD/sys/ddb/db_lex.c

315 lines
5.7 KiB
C
Raw Normal View History

/* $NetBSD: db_lex.c,v 1.13 2000/06/06 18:50:56 soren Exp $ */
1993-03-21 12:45:37 +03:00
/*
* Mach Operating System
* Copyright (c) 1991,1990 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
1993-03-21 12:45:37 +03:00
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie the
* rights to redistribute these changes.
1993-05-20 07:39:04 +04:00
*
1993-03-21 12:45:37 +03:00
* Author: David B. Golub, Carnegie Mellon University
* Date: 7/90
*/
1993-12-18 07:46:25 +03:00
1993-03-21 12:45:37 +03:00
/*
* Lexical analyzer.
*/
1994-10-09 11:19:29 +03:00
#include <sys/param.h>
#include <sys/systm.h>
1994-10-09 11:19:29 +03:00
1996-02-05 04:56:47 +03:00
#include <machine/db_machdep.h>
1993-03-21 12:45:37 +03:00
#include <ddb/db_lex.h>
1996-02-05 04:56:47 +03:00
#include <ddb/db_output.h>
#include <ddb/db_command.h>
#include <ddb/db_sym.h>
#include <ddb/db_extern.h>
1993-03-21 12:45:37 +03:00
char db_line[120];
char * db_lp, *db_endlp;
int
db_read_line()
{
int i;
i = db_readline(db_line, sizeof(db_line));
if (i == 0)
return (0); /* EOI */
db_lp = db_line;
db_endlp = db_lp + i;
return (i);
}
void
db_flush_line()
{
db_lp = db_line;
db_endlp = db_line;
}
int db_look_char = 0;
int
db_read_char()
{
int c;
if (db_look_char != 0) {
c = db_look_char;
db_look_char = 0;
}
else if (db_lp >= db_endlp)
c = -1;
else
c = *db_lp++;
return (c);
}
void
db_unread_char(c)
1994-10-09 11:19:29 +03:00
int c;
1993-03-21 12:45:37 +03:00
{
db_look_char = c;
}
int db_look_token = 0;
void
db_unread_token(t)
int t;
{
db_look_token = t;
}
int
db_read_token()
{
int t;
if (db_look_token) {
t = db_look_token;
db_look_token = 0;
}
else
t = db_lex();
return (t);
}
int db_radix = 16;
/*
* Convert the number to a string in the current radix.
* This replaces the non-standard %n printf() format.
*/
char *
db_num_to_str(val)
db_expr_t val;
{
/*
* 2 chars for "0x", 1 for a sign ("-")
* up to 21 chars for a 64-bit number:
* % echo 2^64 | bc | wc -c
* 21
* and 1 char for a terminal NUL
* 2+1+21+1 => 25
*/
static char buf[25];
char format[] = "%#10lZ";
if (db_radix == 16)
format[5] = 'x'; /* convert to %lx */
else if (db_radix == 8)
format[5] = 'o'; /* convert to %lo */
else
format[5] = 'u'; /* convert to %lu */
snprintf(buf, sizeof(buf), format, val);
return buf;
}
1993-03-21 12:45:37 +03:00
void
db_flush_lex()
{
db_flush_line();
db_look_char = 0;
db_look_token = 0;
}
int
db_lex()
{
int c;
c = db_read_char();
while (c <= ' ' || c > '~') {
if (c == '\n' || c == -1)
return (tEOL);
c = db_read_char();
}
if (c >= '0' && c <= '9') {
/* number */
1997-02-03 22:56:03 +03:00
db_expr_t r, digit = 0;
1993-03-21 12:45:37 +03:00
if (c > '0')
r = db_radix;
else {
c = db_read_char();
if (c == 'O' || c == 'o')
r = 8;
else if (c == 'T' || c == 't')
r = 10;
else if (c == 'X' || c == 'x')
r = 16;
else {
r = db_radix;
db_unread_char(c);
}
c = db_read_char();
}
db_tok_number = 0;
for (;;) {
if (c >= '0' && c <= ((r == 8) ? '7' : '9'))
digit = c - '0';
else if (r == 16 && ((c >= 'A' && c <= 'F') ||
(c >= 'a' && c <= 'f'))) {
if (c >= 'a')
digit = c - 'a' + 10;
else if (c >= 'A')
digit = c - 'A' + 10;
}
else
break;
db_tok_number = db_tok_number * r + digit;
c = db_read_char();
}
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c == '_'))
{
db_error("Bad character in number\n");
1994-10-09 11:56:23 +03:00
/*NOTREACHED*/
1993-03-21 12:45:37 +03:00
}
db_unread_char(c);
return (tNUMBER);
}
if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
c == '_' || c == '\\')
{
/* string */
char *cp;
cp = db_tok_string;
if (c == '\\') {
c = db_read_char();
1994-10-09 11:56:23 +03:00
if (c == '\n' || c == -1) {
1993-03-21 12:45:37 +03:00
db_error("Bad escape\n");
1994-10-09 11:56:23 +03:00
/*NOTREACHED*/
}
1993-03-21 12:45:37 +03:00
}
*cp++ = c;
while (1) {
c = db_read_char();
if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '_' || c == '\\' || c == ':')
{
if (c == '\\') {
c = db_read_char();
1994-10-09 11:56:23 +03:00
if (c == '\n' || c == -1) {
1993-03-21 12:45:37 +03:00
db_error("Bad escape\n");
1994-10-09 11:56:23 +03:00
/*NOTREACHED*/
}
1993-03-21 12:45:37 +03:00
}
*cp++ = c;
if (cp == db_tok_string+sizeof(db_tok_string)) {
db_error("String too long\n");
1994-10-09 11:56:23 +03:00
/*NOTREACHED*/
1993-03-21 12:45:37 +03:00
}
continue;
}
else {
*cp = '\0';
break;
}
}
db_unread_char(c);
return (tIDENT);
}
switch (c) {
case '+':
return (tPLUS);
case '-':
return (tMINUS);
case '.':
c = db_read_char();
if (c == '.')
return (tDOTDOT);
db_unread_char(c);
return (tDOT);
case '*':
return (tSTAR);
case '/':
return (tSLASH);
case '=':
return (tEQ);
case '%':
return (tPCT);
case '#':
return (tHASH);
case '(':
return (tLPAREN);
case ')':
return (tRPAREN);
case ',':
return (tCOMMA);
case '"':
return (tDITTO);
case '$':
return (tDOLLAR);
case '!':
return (tEXCL);
case '<':
c = db_read_char();
if (c == '<')
return (tSHIFT_L);
db_unread_char(c);
break;
case '>':
c = db_read_char();
if (c == '>')
return (tSHIFT_R);
db_unread_char(c);
break;
case -1:
return (tEOF);
}
db_printf("Bad character\n");
db_flush_lex();
return (tEOF);
}