2005-11-27 16:05:28 +03:00
|
|
|
/* $NetBSD: db_lex.c,v 1.20 2005/11/27 13:05:28 yamt Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2002-02-15 10:33:48 +03:00
|
|
|
/*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Mach Operating System
|
|
|
|
* Copyright (c) 1991,1990 Carnegie Mellon University
|
|
|
|
* All Rights Reserved.
|
2002-02-15 10:33:48 +03:00
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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.
|
2002-02-15 10:33:48 +03:00
|
|
|
*
|
1999-04-13 00:38:17 +04:00
|
|
|
* 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.
|
2002-02-15 10:33:48 +03:00
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Carnegie Mellon requests users of this software to return to
|
2002-02-15 10:33:48 +03:00
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
2002-02-15 10:33:48 +03:00
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2001-11-13 01:54:03 +03:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2005-11-27 16:05:28 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: db_lex.c,v 1.20 2005/11/27 13:05:28 yamt Exp $");
|
2001-11-13 01:54:03 +03:00
|
|
|
|
1994-10-09 11:19:29 +03:00
|
|
|
#include <sys/param.h>
|
2000-06-06 22:50:56 +04:00
|
|
|
#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>
|
2003-05-17 13:58:03 +04:00
|
|
|
#include <ddb/db_interface.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-02-15 10:33:48 +03:00
|
|
|
db_expr_t db_tok_number;
|
|
|
|
char db_tok_string[TOK_STRING_SIZE];
|
|
|
|
|
2005-11-27 16:05:28 +03:00
|
|
|
static char db_line[DB_LINE_MAXLEN];
|
2005-11-26 15:16:44 +03:00
|
|
|
static const char *db_lp;
|
|
|
|
static const char *db_endlp;
|
2002-02-15 10:33:48 +03:00
|
|
|
|
|
|
|
static int db_look_char = 0;
|
|
|
|
static int db_look_token = 0;
|
|
|
|
|
|
|
|
static void db_flush_line(void);
|
|
|
|
static int db_read_char(void);
|
|
|
|
static void db_unread_char(int);
|
|
|
|
static int db_lex(void);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
int
|
2002-02-15 10:33:48 +03:00
|
|
|
db_read_line(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = db_readline(db_line, sizeof(db_line));
|
|
|
|
if (i == 0)
|
2002-02-15 10:33:48 +03:00
|
|
|
return (0); /* EOI */
|
2005-11-26 15:16:44 +03:00
|
|
|
db_set_line(db_line, db_line + i);
|
1993-03-21 12:45:37 +03:00
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
2005-11-26 15:16:44 +03:00
|
|
|
void
|
|
|
|
db_set_line(const char *sp, const char *ep)
|
|
|
|
{
|
|
|
|
|
|
|
|
db_lp = sp;
|
|
|
|
db_endlp = ep;
|
|
|
|
}
|
|
|
|
|
2002-02-15 10:33:48 +03:00
|
|
|
static void
|
|
|
|
db_flush_line(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-02-15 10:33:48 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
db_lp = db_line;
|
|
|
|
db_endlp = db_line;
|
|
|
|
}
|
|
|
|
|
2002-02-15 10:33:48 +03:00
|
|
|
static int
|
|
|
|
db_read_char(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (db_look_char != 0) {
|
2002-02-15 10:33:48 +03:00
|
|
|
c = db_look_char;
|
|
|
|
db_look_char = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
else if (db_lp >= db_endlp)
|
2002-02-15 10:33:48 +03:00
|
|
|
c = -1;
|
|
|
|
else
|
|
|
|
c = *db_lp++;
|
1993-03-21 12:45:37 +03:00
|
|
|
return (c);
|
|
|
|
}
|
|
|
|
|
2002-02-15 10:33:48 +03:00
|
|
|
static void
|
|
|
|
db_unread_char(int c)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-02-15 10:33:48 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
db_look_char = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-02-15 10:33:48 +03:00
|
|
|
db_unread_token(int t)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-02-15 10:33:48 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
db_look_token = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-02-15 10:33:48 +03:00
|
|
|
db_read_token(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
if (db_look_token) {
|
2002-02-15 10:33:48 +03:00
|
|
|
t = db_look_token;
|
|
|
|
db_look_token = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
else
|
2002-02-15 10:33:48 +03:00
|
|
|
t = db_lex();
|
1993-03-21 12:45:37 +03:00
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
int db_radix = 16;
|
|
|
|
|
2000-06-06 09:06:25 +04:00
|
|
|
/*
|
|
|
|
* Convert the number to a string in the current radix.
|
|
|
|
* This replaces the non-standard %n printf() format.
|
|
|
|
*/
|
2002-02-15 10:33:48 +03:00
|
|
|
|
2000-06-06 09:06:25 +04:00
|
|
|
char *
|
2002-02-15 10:33:48 +03:00
|
|
|
db_num_to_str(db_expr_t val)
|
2000-06-06 09:06:25 +04:00
|
|
|
{
|
2003-03-02 16:39:40 +03:00
|
|
|
|
2002-02-15 10:33:48 +03:00
|
|
|
/*
|
2000-06-06 09:06:25 +04:00
|
|
|
* 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];
|
|
|
|
|
|
|
|
if (db_radix == 16)
|
2003-05-17 13:58:03 +04:00
|
|
|
snprintf(buf, sizeof(buf), DB_EXPR_T_IS_QUAD ? "%#qx" : "%#lx",
|
|
|
|
val);
|
2000-06-06 09:06:25 +04:00
|
|
|
else if (db_radix == 8)
|
2003-05-17 13:58:03 +04:00
|
|
|
snprintf(buf, sizeof(buf), DB_EXPR_T_IS_QUAD ? "%#qo" : "%#lo",
|
|
|
|
val);
|
2000-06-06 09:06:25 +04:00
|
|
|
else
|
2003-05-17 13:58:03 +04:00
|
|
|
snprintf(buf, sizeof(buf), DB_EXPR_T_IS_QUAD ? "%qu" : "%lu",
|
|
|
|
val);
|
|
|
|
|
2003-03-02 16:39:40 +03:00
|
|
|
return (buf);
|
2000-06-06 09:06:25 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
void
|
2002-02-15 10:33:48 +03:00
|
|
|
db_flush_lex(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-02-15 10:33:48 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
db_flush_line();
|
|
|
|
db_look_char = 0;
|
|
|
|
db_look_token = 0;
|
|
|
|
}
|
|
|
|
|
2002-02-15 10:33:48 +03:00
|
|
|
static int
|
|
|
|
db_lex(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
c = db_read_char();
|
|
|
|
while (c <= ' ' || c > '~') {
|
2002-02-15 10:33:48 +03:00
|
|
|
if (c == '\n' || c == -1)
|
|
|
|
return (tEOL);
|
|
|
|
c = db_read_char();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c >= '0' && c <= '9') {
|
2002-02-15 10:33:48 +03:00
|
|
|
/* number */
|
|
|
|
db_expr_t r, digit = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-02-15 10:33:48 +03:00
|
|
|
if (c > '0')
|
|
|
|
r = db_radix;
|
1993-03-21 12:45:37 +03:00
|
|
|
else {
|
2002-02-15 10:33:48 +03:00
|
|
|
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();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-02-15 10:33:48 +03:00
|
|
|
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();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-02-15 10:33:48 +03:00
|
|
|
if ((c >= '0' && c <= '9') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
|
|
|
(c == '_')) {
|
|
|
|
db_error("Bad character in number\n");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
db_unread_char(c);
|
|
|
|
return (tNUMBER);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if ((c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
2002-02-15 10:33:48 +03:00
|
|
|
c == '_' || c == '\\') {
|
|
|
|
/* string */
|
|
|
|
char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-02-15 10:33:48 +03:00
|
|
|
cp = db_tok_string;
|
|
|
|
if (c == '\\') {
|
1993-03-21 12:45:37 +03:00
|
|
|
c = db_read_char();
|
1994-10-09 11:56:23 +03:00
|
|
|
if (c == '\n' || c == -1) {
|
2002-02-15 10:33:48 +03:00
|
|
|
db_error("Bad escape\n");
|
|
|
|
/*NOTREACHED*/
|
1994-10-09 11:56:23 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-02-15 10:33:48 +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();
|
|
|
|
if (c == '\n' || c == -1) {
|
|
|
|
db_error("Bad escape\n");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*cp++ = c;
|
|
|
|
if (cp == db_tok_string+sizeof(db_tok_string)) {
|
|
|
|
db_error("String too long\n");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
*cp = '\0';
|
|
|
|
break;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-02-15 10:33:48 +03:00
|
|
|
db_unread_char(c);
|
|
|
|
return (tIDENT);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (c) {
|
2002-02-15 10:33:48 +03:00
|
|
|
case '+':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tPLUS);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '-':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tMINUS);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '.':
|
1993-03-21 12:45:37 +03:00
|
|
|
c = db_read_char();
|
|
|
|
if (c == '.')
|
2002-02-15 10:33:48 +03:00
|
|
|
return (tDOTDOT);
|
1993-03-21 12:45:37 +03:00
|
|
|
db_unread_char(c);
|
|
|
|
return (tDOT);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '*':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tSTAR);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '/':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tSLASH);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '=':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tEQ);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '%':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tPCT);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '#':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tHASH);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '(':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tLPAREN);
|
2002-02-15 10:33:48 +03:00
|
|
|
case ')':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tRPAREN);
|
2002-02-15 10:33:48 +03:00
|
|
|
case ',':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tCOMMA);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '"':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tDITTO);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '$':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tDOLLAR);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '!':
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tEXCL);
|
2002-02-15 10:33:48 +03:00
|
|
|
case '<':
|
1993-03-21 12:45:37 +03:00
|
|
|
c = db_read_char();
|
|
|
|
if (c == '<')
|
2002-02-15 10:33:48 +03:00
|
|
|
return (tSHIFT_L);
|
1993-03-21 12:45:37 +03:00
|
|
|
db_unread_char(c);
|
|
|
|
break;
|
2002-02-15 10:33:48 +03:00
|
|
|
case '>':
|
1993-03-21 12:45:37 +03:00
|
|
|
c = db_read_char();
|
|
|
|
if (c == '>')
|
2002-02-15 10:33:48 +03:00
|
|
|
return (tSHIFT_R);
|
1993-03-21 12:45:37 +03:00
|
|
|
db_unread_char(c);
|
|
|
|
break;
|
2002-02-15 10:33:48 +03:00
|
|
|
case -1:
|
1993-03-21 12:45:37 +03:00
|
|
|
return (tEOF);
|
|
|
|
}
|
|
|
|
db_printf("Bad character\n");
|
|
|
|
db_flush_lex();
|
|
|
|
return (tEOF);
|
|
|
|
}
|