2002-08-02 04:07:25 +00:00
|
|
|
/*
|
2002-08-08 09:20:00 +00:00
|
|
|
gib_parse.c
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
GIB parser functions
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
Copyright (C) 2002 Brian Koropoff
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
Author: Brian Koropoff
|
2002-08-02 04:07:25 +00:00
|
|
|
Date: #DATE#
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
static __attribute__ ((unused))
|
|
|
|
const char rcsid[] = "$Id$";
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2002-12-10 00:04:15 +00:00
|
|
|
#include <stdlib.h>
|
2002-08-02 04:07:25 +00:00
|
|
|
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/dstring.h"
|
2002-12-10 00:04:15 +00:00
|
|
|
#include "QF/va.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
#include "QF/cmd.h"
|
2002-08-02 04:07:25 +00:00
|
|
|
#include "QF/cbuf.h"
|
|
|
|
#include "QF/gib_buffer.h"
|
|
|
|
#include "QF/gib_process.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
#include "QF/gib_builtin.h"
|
|
|
|
#include "QF/gib_function.h"
|
2002-08-08 09:20:00 +00:00
|
|
|
#include "QF/gib_vars.h"
|
2002-08-22 22:43:47 +00:00
|
|
|
#include "QF/gib_parse.h"
|
2002-08-03 06:04:00 +00:00
|
|
|
|
|
|
|
|
2002-10-16 04:59:34 +00:00
|
|
|
/*
|
|
|
|
GIB_Escaped
|
|
|
|
|
|
|
|
Returns true if character i in str is
|
|
|
|
escaped with a backslash (and the backslash
|
|
|
|
is not itself escaped).
|
|
|
|
*/
|
2002-08-22 22:43:47 +00:00
|
|
|
inline qboolean
|
|
|
|
GIB_Escaped (const char *str, int i)
|
|
|
|
{
|
|
|
|
int n, c;
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
for (n = i - 1, c = 0; n >= 0 && str[n] == '\\'; n--, c++);
|
|
|
|
return c & 1;
|
|
|
|
}
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
/*
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Parse_Match_*
|
|
|
|
|
|
|
|
These are the workhorses of the GIB parser. They iterate
|
|
|
|
an index variable through a string until an appropriate
|
|
|
|
matching character is found, calling themselves and their
|
|
|
|
neighbors recursively to handle sections of string that they
|
|
|
|
are uninterested in.
|
2002-08-08 09:20:00 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
FIXME: Make sure everything is calling everything else it might
|
|
|
|
need to. Make appropriate functions intolerant of newlines.
|
2002-08-08 09:20:00 +00:00
|
|
|
*/
|
2003-01-28 21:16:21 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static char
|
2002-08-02 04:07:25 +00:00
|
|
|
GIB_Parse_Match_Dquote (const char *str, unsigned int *i)
|
|
|
|
{
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
2002-08-03 06:04:00 +00:00
|
|
|
if (str[*i] == '\n')
|
2002-11-19 04:15:36 +00:00
|
|
|
break;
|
2002-08-22 22:43:47 +00:00
|
|
|
else if (str[*i] == '\"' && !GIB_Escaped (str, *i))
|
2002-08-02 04:07:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-02 04:07:25 +00:00
|
|
|
return '\"';
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:01:06 +00:00
|
|
|
char
|
2002-08-02 04:07:25 +00:00
|
|
|
GIB_Parse_Match_Brace (const char *str, unsigned int *i)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char c;
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-02 04:07:25 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
|
|
|
if (str[*i] == '\"') {
|
|
|
|
if ((c = GIB_Parse_Match_Dquote (str, i)))
|
|
|
|
return c;
|
2002-08-06 08:15:20 +00:00
|
|
|
} else if (str[*i] == '{') {
|
2002-08-02 04:07:25 +00:00
|
|
|
if ((c = GIB_Parse_Match_Brace (str, i)))
|
|
|
|
return c;
|
2002-08-06 08:15:20 +00:00
|
|
|
} else if (str[*i] == '}')
|
2003-02-14 22:42:11 +00:00
|
|
|
return 0;
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-02 04:07:25 +00:00
|
|
|
return '{';
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:01:06 +00:00
|
|
|
char
|
2002-08-03 06:04:00 +00:00
|
|
|
GIB_Parse_Match_Paren (const char *str, unsigned int *i)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char c;
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-03 06:04:00 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
|
|
|
if (str[*i] == '(') {
|
|
|
|
if ((c = GIB_Parse_Match_Paren (str, i)))
|
|
|
|
return c;
|
2002-11-19 04:15:36 +00:00
|
|
|
} else if (str[*i] == '\"') {
|
|
|
|
if ((c = GIB_Parse_Match_Dquote (str, i)))
|
|
|
|
return c;
|
2002-08-06 08:15:20 +00:00
|
|
|
} else if (str[*i] == ')')
|
2002-08-03 06:04:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-03 06:04:00 +00:00
|
|
|
return '(';
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:01:06 +00:00
|
|
|
char
|
2002-08-07 06:17:50 +00:00
|
|
|
GIB_Parse_Match_Backtick (const char *str, unsigned int *i)
|
2002-08-05 00:01:06 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char c;
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-05 00:01:06 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
2002-08-07 06:17:50 +00:00
|
|
|
if (str[*i] == '`')
|
2002-08-05 00:01:06 +00:00
|
|
|
return 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
else if (str[*i] == '\"') { // Skip over strings as usual
|
2002-08-05 00:01:06 +00:00
|
|
|
if ((c = GIB_Parse_Match_Dquote (str, i)))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-07 06:17:50 +00:00
|
|
|
return '`';
|
2002-08-05 00:01:06 +00:00
|
|
|
}
|
|
|
|
|
2002-08-14 05:28:07 +00:00
|
|
|
char
|
|
|
|
GIB_Parse_Match_Index (const char *str, unsigned int *i)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char c;
|
2002-08-29 22:12:51 +00:00
|
|
|
unsigned int n = *i;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-14 05:28:07 +00:00
|
|
|
for ((*i)++; str[*i]; (*i)++) {
|
2003-01-28 21:16:21 +00:00
|
|
|
if (str[*i] == '[' && (c = GIB_Parse_Match_Index (str, i)))
|
|
|
|
return c;
|
|
|
|
else if (str[*i] == ']')
|
2002-08-14 05:28:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-08-29 22:12:51 +00:00
|
|
|
*i = n;
|
2002-08-14 05:28:07 +00:00
|
|
|
return '[';
|
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
char
|
|
|
|
GIB_Parse_Match_Var (const char *str, unsigned int *i)
|
2002-09-10 01:26:02 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
char c;
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
(*i)++;
|
|
|
|
if (str[*i] == '{' && (c = GIB_Parse_Match_Brace (str, i)))
|
|
|
|
return c;
|
|
|
|
else {
|
2003-02-14 22:42:11 +00:00
|
|
|
for (; isalnum ((byte) str[*i]) || str[*i] == '_'; (*i)++);
|
2003-01-28 21:16:21 +00:00
|
|
|
if (str[*i] == '[') {
|
|
|
|
if ((c = GIB_Parse_Match_Index (str, i)))
|
|
|
|
return c;
|
|
|
|
(*i)++;
|
2002-09-10 01:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
return 0;
|
2002-09-10 01:26:02 +00:00
|
|
|
}
|
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
qboolean gib_parse_error;
|
2003-02-14 08:06:01 +00:00
|
|
|
unsigned int gib_parse_error_pos;
|
|
|
|
const char *gib_parse_error_msg;
|
|
|
|
|
|
|
|
static void
|
|
|
|
GIB_Parse_Error (const char *msg, unsigned int pos)
|
|
|
|
{
|
|
|
|
gib_parse_error = true;
|
|
|
|
gib_parse_error_msg = msg;
|
|
|
|
gib_parse_error_pos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
GIB_Parse_ErrorMsg (void)
|
|
|
|
{
|
|
|
|
return gib_parse_error_msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
GIB_Parse_ErrorPos (void)
|
|
|
|
{
|
|
|
|
return gib_parse_error_pos;
|
|
|
|
}
|
2002-12-13 23:36:05 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
// FIXME: Concatenation in stupid circumstances should generate errors
|
2002-08-29 22:12:51 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
static gib_tree_t *
|
2003-02-25 06:52:27 +00:00
|
|
|
GIB_Parse_Tokens (const char *program, unsigned int *i, unsigned int pofs, gib_tree_t ** embedded)
|
2003-01-28 21:16:21 +00:00
|
|
|
{
|
2003-02-25 08:35:31 +00:00
|
|
|
char c = 0, delim, *str;
|
2003-02-14 08:06:01 +00:00
|
|
|
unsigned int tstart, start;
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_tree_t *nodes = 0, *cur, *new, *embs = 0, *tmp;
|
|
|
|
gib_tree_t **node = &nodes;
|
2003-02-25 08:04:48 +00:00
|
|
|
enum {CAT_NORMAL = 0, CAT_DISALLOW, CAT_CONCAT} cat = CAT_DISALLOW;
|
|
|
|
const char *catestr = "Comma found before first argument, nothing to concatenate to.";
|
2002-08-29 22:12:51 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_parse_error = false;
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
while (1) {
|
|
|
|
// Skip whitespace
|
2003-02-14 22:42:11 +00:00
|
|
|
while (program[*i] != '\n' && isspace ((byte) program[*i]))
|
2003-01-28 21:16:21 +00:00
|
|
|
(*i)++;
|
|
|
|
// Check for concatenation, skip comma and any more whitespace
|
|
|
|
if (program[*i] == ',') {
|
2003-02-25 08:04:48 +00:00
|
|
|
if (cat == CAT_DISALLOW) {
|
|
|
|
GIB_Parse_Error(catestr, *i + pofs);
|
|
|
|
goto ERROR;
|
|
|
|
}
|
|
|
|
cat = CAT_CONCAT;
|
2003-01-28 21:16:21 +00:00
|
|
|
(*i)++;
|
2003-02-26 02:51:40 +00:00
|
|
|
while (program[*i] != '\n' && isspace ((byte) program[*i]))
|
|
|
|
(*i)++;
|
|
|
|
if (program[*i] == ',') {
|
|
|
|
GIB_Parse_Error("Double comma error.", *i+pofs);
|
|
|
|
goto ERROR;
|
|
|
|
}
|
2003-02-25 08:04:48 +00:00
|
|
|
} else
|
|
|
|
cat = CAT_NORMAL;
|
2003-01-28 21:16:21 +00:00
|
|
|
// New line/command?
|
|
|
|
if (!program[*i] || program[*i] == '\n' || program[*i] == ';')
|
|
|
|
break;
|
|
|
|
// Save our start position
|
2003-02-14 08:06:01 +00:00
|
|
|
start = *i;
|
|
|
|
tstart = start + 1;
|
2003-01-28 21:16:21 +00:00
|
|
|
delim = program[*i];
|
|
|
|
switch (delim) {
|
|
|
|
case '{':
|
|
|
|
if ((c = GIB_Parse_Match_Brace (program, i)))
|
|
|
|
goto ERROR;
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
if ((c = GIB_Parse_Match_Dquote (program, i)))
|
|
|
|
goto ERROR;
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
if ((c = GIB_Parse_Match_Paren (program, i)))
|
|
|
|
goto ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Find the end of a "normal" token
|
|
|
|
delim = ' ';
|
|
|
|
tstart = *i;
|
2003-02-14 22:42:11 +00:00
|
|
|
for (;
|
|
|
|
program[*i] && !isspace ((byte) program[*i])
|
|
|
|
&& program[*i] != ',' && program[*i] != ';'; (*i)++) {
|
2003-01-28 21:16:21 +00:00
|
|
|
if (program[*i] == '{') {
|
|
|
|
if ((c = GIB_Parse_Match_Brace (program, i)))
|
|
|
|
goto ERROR;
|
|
|
|
} else if (program[*i] == '(') {
|
|
|
|
if ((c = GIB_Parse_Match_Paren (program, i)))
|
|
|
|
goto ERROR;
|
|
|
|
} else if (program[*i] == '`') {
|
|
|
|
if ((c = GIB_Parse_Match_Backtick (program, i)))
|
|
|
|
goto ERROR;
|
2003-02-14 22:42:11 +00:00
|
|
|
// Handle comments
|
|
|
|
} else if (program[*i] == '/' && program[*i + 1] == '/') {
|
|
|
|
for ((*i) += 2; program[*i] && program[*i] != '\n';
|
|
|
|
(*i)++);
|
2003-01-29 04:34:23 +00:00
|
|
|
goto DONE;
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
2002-11-19 04:15:36 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
2003-02-14 08:06:01 +00:00
|
|
|
c = 0;
|
2003-02-25 06:52:27 +00:00
|
|
|
cur = *node = GIB_Tree_New (TREE_T_ARG);
|
2003-02-14 08:06:01 +00:00
|
|
|
cur->start = start + pofs;
|
|
|
|
cur->end = *i + pofs;
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->delim = delim;
|
2003-02-14 22:42:11 +00:00
|
|
|
str = calloc (*i - tstart + 1, sizeof (char));
|
2003-02-26 07:44:34 +00:00
|
|
|
cur->str = str;
|
2003-02-14 22:42:11 +00:00
|
|
|
memcpy (str, program + tstart, *i - tstart);
|
2003-02-25 06:52:27 +00:00
|
|
|
if (cur->delim == '{') {
|
2003-02-25 08:04:48 +00:00
|
|
|
if (cat == CAT_CONCAT) {
|
|
|
|
GIB_Parse_Error ("Program blocks may not be concatenated with other arguments.", start + pofs);
|
|
|
|
goto ERROR;
|
|
|
|
}
|
|
|
|
catestr = "Program blocks may not be concatenated with other arguments.";
|
|
|
|
cat = CAT_DISALLOW;
|
2003-01-28 21:16:21 +00:00
|
|
|
// Try to parse sub-program
|
2003-02-25 06:52:27 +00:00
|
|
|
if (!(new = GIB_Parse_Lines (str, tstart + pofs)))
|
2003-01-28 21:16:21 +00:00
|
|
|
goto ERROR;
|
|
|
|
cur->children = new;
|
2003-02-14 22:42:11 +00:00
|
|
|
// Check for embedded commands/variables
|
2003-01-28 21:16:21 +00:00
|
|
|
} else if (cur->delim == ' ' || cur->delim == '(') {
|
2003-02-14 22:42:11 +00:00
|
|
|
if (!
|
|
|
|
(cur->children =
|
2003-02-25 06:52:27 +00:00
|
|
|
GIB_Parse_Embedded (str, tstart + pofs, &new))) {
|
2003-02-14 22:42:11 +00:00
|
|
|
// There could be no embedded elements, so check for a real
|
|
|
|
// error
|
2003-01-28 21:16:21 +00:00
|
|
|
if (gib_parse_error)
|
|
|
|
goto ERROR;
|
|
|
|
} else {
|
|
|
|
// Link/set flags
|
2003-02-25 06:52:27 +00:00
|
|
|
cur->flags |= TREE_A_EMBED;
|
2003-01-28 21:16:21 +00:00
|
|
|
// Add any embedded commands to top of chain
|
|
|
|
if (new) {
|
2003-01-29 04:34:23 +00:00
|
|
|
for (tmp = new; tmp->next; tmp = tmp->next);
|
2003-01-28 21:16:21 +00:00
|
|
|
tmp->next = embs;
|
|
|
|
embs = new;
|
2002-08-08 09:20:00 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
// Check for array splitting
|
|
|
|
// Concatenating this onto something else is non-sensical
|
2003-02-25 08:04:48 +00:00
|
|
|
if (cur->delim == ' ' && (str[0] == '@' || str[0] == '%')) {
|
|
|
|
if (cat == CAT_CONCAT) {
|
|
|
|
GIB_Parse_Error ("Variable expansions may not be concatenated with other arguments.", start + pofs);
|
|
|
|
goto ERROR;
|
|
|
|
}
|
|
|
|
catestr = "Variable expansions may not be concatenated with other arguments.";
|
|
|
|
cat = CAT_DISALLOW;
|
2003-02-25 06:52:27 +00:00
|
|
|
cur->flags |= TREE_A_EXPAND;
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
// We can handle escape characters now
|
2003-01-28 21:16:21 +00:00
|
|
|
} else if (cur->delim == '\"')
|
|
|
|
GIB_Process_Escapes (str);
|
2003-01-31 03:43:56 +00:00
|
|
|
|
2003-02-25 08:04:48 +00:00
|
|
|
if (cat == CAT_CONCAT)
|
2003-02-25 06:52:27 +00:00
|
|
|
cur->flags |= TREE_A_CONCAT;
|
2003-01-28 21:16:21 +00:00
|
|
|
// Nothing left to parse?
|
|
|
|
if (!program[*i])
|
|
|
|
break;
|
|
|
|
// On non-normal tokens, move past the delimeter
|
|
|
|
if (cur->delim != ' ')
|
|
|
|
(*i)++;
|
|
|
|
node = &cur->next;
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
DONE:
|
2003-01-28 21:16:21 +00:00
|
|
|
*embedded = embs;
|
|
|
|
return nodes;
|
2003-02-14 22:42:11 +00:00
|
|
|
ERROR:
|
2003-02-14 08:06:01 +00:00
|
|
|
if (c)
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Parse_Error (va ("Could not find match for '%c'.", c), *i + pofs);
|
2003-01-31 05:22:20 +00:00
|
|
|
if (nodes)
|
2003-02-26 07:44:34 +00:00
|
|
|
GIB_Tree_Unref (&nodes);
|
|
|
|
if (embs)
|
|
|
|
GIB_Tree_Unref (&embs);
|
2003-01-28 21:16:21 +00:00
|
|
|
return 0;
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
static gib_tree_t *
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Parse_Semantic_Preprocess (gib_tree_t * line)
|
2002-08-02 04:07:25 +00:00
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_tree_t *p, *start = line;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-02-26 07:44:34 +00:00
|
|
|
// If second token is concatenated, than the first can't possibly mean anything
|
|
|
|
if (line->children->next && line->children->next->flags & TREE_A_CONCAT)
|
|
|
|
return line;
|
2003-02-14 22:42:11 +00:00
|
|
|
while (!strcmp (line->children->str, "if")
|
|
|
|
|| !strcmp (line->children->str, "ifnot")) {
|
2003-01-28 21:16:21 +00:00
|
|
|
// Sanity checking
|
2003-02-14 08:06:01 +00:00
|
|
|
if (!line->children->next || !line->children->next->next) {
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Parse_Error ("Not enough arguments to 'if' statement.",
|
|
|
|
line->start);
|
2003-02-14 08:06:01 +00:00
|
|
|
return line;
|
2003-02-14 22:42:11 +00:00
|
|
|
} else if (!line->children->next->next->children
|
|
|
|
|| line->children->next->next->delim != '{') {
|
|
|
|
GIB_Parse_Error
|
|
|
|
("First program block in 'if' statement not enclosed in braces or invalid.",
|
|
|
|
line->start);
|
2003-02-14 08:06:01 +00:00
|
|
|
return line;
|
2003-02-25 06:52:27 +00:00
|
|
|
} else if (line->flags & TREE_L_EMBED) {
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Parse_Error
|
|
|
|
("'if' statements may not be used in embedded commands.",
|
|
|
|
line->start);
|
2003-01-28 21:16:21 +00:00
|
|
|
return line;
|
|
|
|
}
|
2003-02-25 06:52:27 +00:00
|
|
|
// Set as conditional
|
|
|
|
line->type = TREE_T_COND;
|
2003-01-31 03:43:56 +00:00
|
|
|
if (line->children->str[2])
|
2003-02-25 06:52:27 +00:00
|
|
|
line->flags |= TREE_L_NOT;
|
2003-01-28 21:16:21 +00:00
|
|
|
// Save our spot
|
|
|
|
p = line;
|
|
|
|
// Move subprogram inline
|
|
|
|
line->next = line->children->next->next->children;
|
|
|
|
line->children->next->next->children = 0;
|
|
|
|
// Find end of subprogram
|
2003-02-14 22:42:11 +00:00
|
|
|
while (line->next)
|
|
|
|
line = line->next;
|
2003-01-28 21:16:21 +00:00
|
|
|
// Handle "else"
|
2003-02-14 22:42:11 +00:00
|
|
|
if (p->children->next->next->next
|
|
|
|
&& !strcmp (p->children->next->next->next->str, "else")) {
|
2003-01-28 21:16:21 +00:00
|
|
|
// Sanity checking
|
|
|
|
if (!p->children->next->next->next->next) {
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Parse_Error
|
|
|
|
("'if' statement contains 'else' but no secondary program block or command.",
|
|
|
|
line->start);
|
2003-01-28 21:16:21 +00:00
|
|
|
return line;
|
|
|
|
}
|
2003-02-25 06:52:27 +00:00
|
|
|
// On 'true' first block must jump past this
|
|
|
|
// We will figure out jump target later
|
|
|
|
line->next = GIB_Tree_New (TREE_T_JUMPPLUS);
|
|
|
|
line = line->next;
|
|
|
|
// Jump to else block on 'false'
|
|
|
|
p->jump = line;
|
2003-01-28 21:16:21 +00:00
|
|
|
// Is "else" followed by a subprogram?
|
|
|
|
if (p->children->next->next->next->next->delim == '{') {
|
|
|
|
// Move subprogram inline
|
|
|
|
line->next = p->children->next->next->next->next->children;
|
|
|
|
p->children->next->next->next->next->children = 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
while (line->next)
|
|
|
|
line = line->next;
|
2003-01-28 21:16:21 +00:00
|
|
|
} else {
|
|
|
|
// Push rest of tokens into a new line
|
2003-02-25 06:52:27 +00:00
|
|
|
line->next = GIB_Tree_New (TREE_T_CMD);
|
2003-01-28 21:16:21 +00:00
|
|
|
line->next->children = p->children->next->next->next->next;
|
|
|
|
p->children->next->next->next->next = 0;
|
|
|
|
line = line->next;
|
|
|
|
}
|
2003-02-25 06:52:27 +00:00
|
|
|
} else {
|
|
|
|
// Jump past block on 'false'
|
|
|
|
p->jump = line;
|
|
|
|
break; // Don't touch if statements in the sub program
|
|
|
|
}
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
2003-02-25 06:52:27 +00:00
|
|
|
// Now we know exit point from if-else if chain, set our jumps
|
2003-01-28 21:16:21 +00:00
|
|
|
while (start) {
|
2003-02-25 06:52:27 +00:00
|
|
|
if (start->type == TREE_T_JUMPPLUS && !start->jump)
|
2003-01-28 21:16:21 +00:00
|
|
|
start->jump = line;
|
|
|
|
start = start->next;
|
|
|
|
}
|
|
|
|
// Nothing expanded from a line remains, exit now
|
|
|
|
if (!line->children)
|
|
|
|
return line;
|
|
|
|
// If we have a while loop, handle that
|
|
|
|
if (!strcmp (line->children->str, "while")) {
|
|
|
|
// Sanity checks
|
2003-02-14 08:06:01 +00:00
|
|
|
if (!line->children->next || !line->children->next->next) {
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Parse_Error ("Not enough arguments to 'while' statement.",
|
|
|
|
line->start);
|
2003-02-14 08:06:01 +00:00
|
|
|
return line;
|
2003-02-14 22:42:11 +00:00
|
|
|
} else if (!line->children->next->next->children
|
|
|
|
|| line->children->next->next->delim != '{') {
|
|
|
|
GIB_Parse_Error
|
|
|
|
("Program block in 'while' statement not enclosed in braces or invalid.",
|
|
|
|
line->start);
|
2003-02-14 08:06:01 +00:00
|
|
|
return line;
|
2003-02-25 06:52:27 +00:00
|
|
|
} else if (line->flags & TREE_L_EMBED) {
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Parse_Error
|
|
|
|
("'while' statements may not be used in embedded commands.",
|
|
|
|
line->start);
|
2003-01-28 21:16:21 +00:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
// Set conditional flag
|
2003-02-25 06:52:27 +00:00
|
|
|
line->type = TREE_T_COND;
|
2003-01-28 21:16:21 +00:00
|
|
|
// Save our spot
|
|
|
|
p = line;
|
|
|
|
// Move subprogram inline
|
|
|
|
line->next = line->children->next->next->children;
|
|
|
|
line->children->next->next->children = 0;
|
2003-02-25 06:52:27 +00:00
|
|
|
// Find end of subprogram
|
2003-01-28 21:16:21 +00:00
|
|
|
for (; line->next; line = line->next)
|
2003-02-25 06:52:27 +00:00
|
|
|
if (!line->jump && line->children) {
|
|
|
|
if (!strcmp (line->children->str, "continue")) {
|
|
|
|
line->type = TREE_T_JUMP;
|
|
|
|
line->jump = p;
|
|
|
|
} else if (!strcmp (line->children->str, "break"))
|
|
|
|
line->type = TREE_T_JUMPPLUS;
|
|
|
|
}
|
|
|
|
line->next = GIB_Tree_New (TREE_T_JUMP);
|
2003-01-28 21:16:21 +00:00
|
|
|
line->next->jump = p;
|
|
|
|
line = line->next;
|
|
|
|
// Mark jump point out of loop
|
|
|
|
p->jump = line;
|
2003-02-25 06:52:27 +00:00
|
|
|
// Set jumps out of loop for "break" commands;
|
|
|
|
while (p) {
|
|
|
|
if (p->type == TREE_T_JUMPPLUS && !p->jump)
|
|
|
|
p->jump = line;
|
|
|
|
p = p->next;
|
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
} else if (!strcmp (line->children->str, "for")) {
|
|
|
|
gib_tree_t *tmp;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
// Sanity checks
|
2003-02-14 22:42:11 +00:00
|
|
|
if (!line->children->next || !line->children->next->next
|
|
|
|
|| strcmp (line->children->next->next->str, "in")
|
|
|
|
|| !line->children->next->next->next
|
|
|
|
|| !line->children->next->next->next->next) {
|
2003-02-14 08:06:01 +00:00
|
|
|
GIB_Parse_Error ("Malformed 'for' statement.", line->start);
|
2003-01-28 21:16:21 +00:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
// Find last token in line (contains program block)
|
2003-02-14 22:42:11 +00:00
|
|
|
for (tmp = line->children->next->next->next->next; tmp->next;
|
|
|
|
tmp = tmp->next);
|
2003-01-28 21:16:21 +00:00
|
|
|
// More sanity
|
2003-01-29 04:34:23 +00:00
|
|
|
if (tmp->delim != '{' || !tmp->children) {
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Parse_Error
|
|
|
|
("Program block in 'for' statement not enclosed in braces or invalid.",
|
|
|
|
line->start);
|
2003-01-28 21:16:21 +00:00
|
|
|
return line;
|
|
|
|
}
|
2003-02-25 06:52:27 +00:00
|
|
|
// Add instruction to fetch next argument (this is the true loop start)
|
|
|
|
line->next = GIB_Tree_New (TREE_T_FORNEXT);
|
|
|
|
line = line->next;
|
2003-01-28 21:16:21 +00:00
|
|
|
p = line;
|
|
|
|
// Move subprogram inline
|
|
|
|
line->next = tmp->children;
|
2003-02-14 08:06:01 +00:00
|
|
|
tmp->children = 0;
|
2003-02-25 06:52:27 +00:00
|
|
|
// Find end of subprogram
|
2003-01-28 21:16:21 +00:00
|
|
|
for (; line->next; line = line->next)
|
2003-02-25 06:52:27 +00:00
|
|
|
if (!line->jump && line->children) {
|
|
|
|
if (!strcmp (line->children->str, "continue")) {
|
|
|
|
line->type = TREE_T_JUMP;
|
|
|
|
line->jump = p;
|
|
|
|
} else if (!strcmp (line->children->str, "break"))
|
|
|
|
line->type = TREE_T_JUMPPLUS;
|
|
|
|
}
|
|
|
|
line->next = GIB_Tree_New (TREE_T_JUMP);
|
2003-01-28 21:16:21 +00:00
|
|
|
line->next->jump = p;
|
|
|
|
line = line->next;
|
|
|
|
// Mark jump point out of loop
|
|
|
|
p->jump = line;
|
2003-02-25 06:52:27 +00:00
|
|
|
// Mark jump point out of loop for break command
|
|
|
|
while (p) {
|
|
|
|
if (p->type == TREE_T_JUMPPLUS && !p->jump)
|
|
|
|
p->jump = line;
|
|
|
|
p = p->next;
|
|
|
|
}
|
2003-02-26 07:44:34 +00:00
|
|
|
} else if (
|
|
|
|
line->children->next &&
|
|
|
|
!(line->children->next->flags & TREE_A_CONCAT) &&
|
|
|
|
line->children->next->delim == ' ' &&
|
|
|
|
!strcmp (line->children->next->str, "=")
|
|
|
|
)
|
|
|
|
line->type = TREE_T_ASSIGN;
|
2003-01-28 21:16:21 +00:00
|
|
|
return line;
|
2003-02-14 22:42:11 +00:00
|
|
|
}
|
2002-08-02 04:07:25 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_tree_t *
|
2003-02-25 06:52:27 +00:00
|
|
|
GIB_Parse_Lines (const char *program, unsigned int pofs)
|
2002-08-03 06:04:00 +00:00
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
unsigned int i = 0, lstart;
|
|
|
|
gib_tree_t *lines = 0, *cur, *tokens, **line = &lines, *embs;
|
2003-02-14 22:42:11 +00:00
|
|
|
char *str;
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
while (1) {
|
2003-02-14 22:42:11 +00:00
|
|
|
while (isspace ((byte) program[i]) || program[i] == ';')
|
2002-08-02 04:07:25 +00:00
|
|
|
i++;
|
2003-01-28 21:16:21 +00:00
|
|
|
if (!program[i])
|
2002-08-02 04:07:25 +00:00
|
|
|
break;
|
2003-01-28 21:16:21 +00:00
|
|
|
lstart = i;
|
|
|
|
// If we parse something useful...
|
2003-02-25 06:52:27 +00:00
|
|
|
if ((tokens = GIB_Parse_Tokens (program, &i, pofs, &embs))) {
|
2003-01-28 21:16:21 +00:00
|
|
|
// Link it in
|
2003-02-25 06:52:27 +00:00
|
|
|
cur = GIB_Tree_New (TREE_T_CMD);
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->delim = '\n';
|
2003-02-14 22:42:11 +00:00
|
|
|
str = calloc (i - lstart + 1, sizeof (char));
|
|
|
|
memcpy (str, program + lstart, i - lstart);
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->str = str;
|
2003-02-14 08:06:01 +00:00
|
|
|
cur->start = lstart + pofs;
|
|
|
|
cur->end = i + pofs;
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->children = tokens;
|
|
|
|
// Line contains embedded commands?
|
|
|
|
if (embs) {
|
|
|
|
// Add them to chain before actual line
|
|
|
|
*line = embs;
|
|
|
|
for (; embs->next; embs = embs->next);
|
|
|
|
embs->next = cur;
|
|
|
|
} else
|
|
|
|
*line = cur;
|
|
|
|
// Do preprocessing
|
|
|
|
line = &(GIB_Parse_Semantic_Preprocess (cur))->next;
|
2002-08-07 06:17:50 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
if (gib_parse_error)
|
|
|
|
goto ERROR;
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
return lines;
|
2003-02-14 22:42:11 +00:00
|
|
|
ERROR:
|
2003-01-31 05:22:20 +00:00
|
|
|
if (lines)
|
2003-02-26 07:44:34 +00:00
|
|
|
GIB_Tree_Unref (&lines);
|
2003-01-28 21:16:21 +00:00
|
|
|
return 0;
|
2002-08-02 04:07:25 +00:00
|
|
|
}
|
2002-08-03 06:04:00 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_tree_t *
|
2003-02-25 06:52:27 +00:00
|
|
|
GIB_Parse_Embedded (const char *program, unsigned int pofs, gib_tree_t ** embedded)
|
2002-12-10 00:04:15 +00:00
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
unsigned int i, n, t;
|
2003-02-14 22:42:11 +00:00
|
|
|
char c, d, *str;
|
2003-02-26 07:44:34 +00:00
|
|
|
gib_tree_t *lines = 0, **line = &lines, *cur, *tokens, *emb, *tmp, **embfirst;
|
2003-01-28 21:16:21 +00:00
|
|
|
unsigned int start, end;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 23:55:21 +00:00
|
|
|
gib_parse_error = false;
|
2003-02-26 07:44:34 +00:00
|
|
|
embfirst = embedded;
|
2003-01-28 21:16:21 +00:00
|
|
|
*embedded = 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
for (i = 0; program[i]; i++) {
|
2003-02-14 22:42:11 +00:00
|
|
|
if (program[i] == '`' || (program[i] == '$' && program[i + 1] == '(')) {
|
2003-01-28 21:16:21 +00:00
|
|
|
// Extract the embedded command
|
|
|
|
start = i;
|
|
|
|
if (program[i] == '`') {
|
2003-02-14 22:42:11 +00:00
|
|
|
n = i + 1;
|
2003-01-28 21:16:21 +00:00
|
|
|
if ((c = GIB_Parse_Match_Backtick (program, &i)))
|
|
|
|
goto ERROR;
|
|
|
|
} else {
|
2003-02-14 22:42:11 +00:00
|
|
|
n = ++i + 1;
|
2003-01-28 21:16:21 +00:00
|
|
|
if ((c = GIB_Parse_Match_Paren (program, &i)))
|
|
|
|
goto ERROR;
|
|
|
|
}
|
2003-02-14 22:42:11 +00:00
|
|
|
end = i + 1;
|
2003-01-28 21:16:21 +00:00
|
|
|
// Construct the actual line to be executed
|
2003-02-25 06:52:27 +00:00
|
|
|
cur = GIB_Tree_New (TREE_T_CMD);
|
|
|
|
cur->flags |= TREE_L_EMBED;
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->delim = '`';
|
|
|
|
str = calloc (i - n + 1, sizeof (char));
|
2003-02-14 22:42:11 +00:00
|
|
|
memcpy (str, program + n, i - n);
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->str = str;
|
2003-02-14 08:06:01 +00:00
|
|
|
cur->start = start + pofs;
|
|
|
|
cur->end = end + pofs;
|
|
|
|
c = 0;
|
2003-01-28 21:16:21 +00:00
|
|
|
t = 0;
|
2003-02-26 07:44:34 +00:00
|
|
|
if (!(tokens = GIB_Parse_Tokens (cur->str, &t, start + pofs, &emb))) {
|
|
|
|
GIB_Tree_Unref (&cur);
|
2003-01-28 21:16:21 +00:00
|
|
|
goto ERROR;
|
2003-02-26 07:44:34 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->children = tokens;
|
|
|
|
GIB_Parse_Semantic_Preprocess (cur)->next = *embedded;
|
2003-02-26 07:44:34 +00:00
|
|
|
if (gib_parse_error) {
|
|
|
|
GIB_Tree_Unref (&cur);
|
2003-01-28 21:16:21 +00:00
|
|
|
goto ERROR;
|
2003-02-26 07:44:34 +00:00
|
|
|
}
|
2003-01-28 21:16:21 +00:00
|
|
|
// Did this have embedded commands of it's own?
|
|
|
|
if (emb) {
|
|
|
|
// Link them in first
|
|
|
|
for (tmp = emb; tmp->next; tmp = tmp->next);
|
|
|
|
tmp->next = cur;
|
|
|
|
*embedded = emb;
|
|
|
|
} else
|
|
|
|
*embedded = cur;
|
2003-02-14 22:42:11 +00:00
|
|
|
// Create a representative child node for GIB_Process_Embedded to
|
|
|
|
// use
|
2003-02-25 06:52:27 +00:00
|
|
|
cur = GIB_Tree_New (TREE_T_META);
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->delim = '`';
|
|
|
|
// Save start/end indices
|
|
|
|
cur->start = start;
|
|
|
|
cur->end = end;
|
|
|
|
*line = cur;
|
|
|
|
line = &cur->next;
|
2003-02-14 22:42:11 +00:00
|
|
|
// Check for variable substitution
|
2003-01-28 21:16:21 +00:00
|
|
|
} else if (program[i] == '$' || program[i] == '#') {
|
|
|
|
// Extract variable name
|
|
|
|
start = i;
|
|
|
|
end = 0;
|
|
|
|
d = program[i];
|
2003-02-14 22:42:11 +00:00
|
|
|
if (program[i + 1] == '{') {
|
|
|
|
n = i + 2;
|
2003-01-28 21:16:21 +00:00
|
|
|
end++;
|
|
|
|
} else
|
2003-02-14 22:42:11 +00:00
|
|
|
n = i + 1;
|
2003-01-28 21:16:21 +00:00
|
|
|
if ((c = GIB_Parse_Match_Var (program, &i)))
|
|
|
|
goto ERROR;
|
|
|
|
end += i;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-02-25 06:52:27 +00:00
|
|
|
cur = GIB_Tree_New (TREE_T_META);
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->delim = d;
|
2003-02-14 22:42:11 +00:00
|
|
|
str = calloc (i - n + 1, sizeof (char));
|
|
|
|
memcpy (str, program + n, i - n);
|
2003-01-28 21:16:21 +00:00
|
|
|
cur->str = str;
|
2003-02-14 22:42:11 +00:00
|
|
|
// Can we use the name as is, or must processing be done at
|
|
|
|
// runtime?
|
2003-01-28 21:16:21 +00:00
|
|
|
if (strchr (str, '$') || strchr (str, '#'))
|
2003-02-25 06:52:27 +00:00
|
|
|
cur->flags |= TREE_A_EMBED;
|
2003-01-28 21:16:21 +00:00
|
|
|
// Save start/end indices
|
|
|
|
cur->start = start;
|
|
|
|
cur->end = end;
|
|
|
|
*line = cur;
|
|
|
|
line = &cur->next;
|
|
|
|
// Don't skip anything important
|
2003-02-14 22:42:11 +00:00
|
|
|
if (program[n - 1] != '{')
|
2003-01-28 21:16:21 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines;
|
2003-02-14 22:42:11 +00:00
|
|
|
ERROR:
|
2003-02-14 08:06:01 +00:00
|
|
|
if (c)
|
|
|
|
GIB_Parse_Error (va ("Could not find match for '%c'.", c), i + pofs);
|
2003-01-31 05:22:20 +00:00
|
|
|
if (lines)
|
2003-02-26 07:44:34 +00:00
|
|
|
GIB_Tree_Unref (&lines);
|
|
|
|
if (*embfirst)
|
|
|
|
GIB_Tree_Unref (embfirst);
|
2003-01-28 21:16:21 +00:00
|
|
|
return 0;
|
2002-08-03 06:04:00 +00:00
|
|
|
}
|