2005-05-12 15:51:20 +00:00
|
|
|
/*
|
2005-10-02 15:45:09 +00:00
|
|
|
* $Id: parser.c,v 1.78 2005-10-02 15:45:09 bacon Exp $
|
2005-05-12 15:51:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/stx/parser.h>
|
2005-07-05 06:26:33 +00:00
|
|
|
#include <xp/stx/object.h>
|
2005-06-29 12:02:39 +00:00
|
|
|
#include <xp/stx/class.h>
|
2005-07-05 09:02:13 +00:00
|
|
|
#include <xp/stx/method.h>
|
|
|
|
#include <xp/stx/symbol.h>
|
2005-07-05 11:38:01 +00:00
|
|
|
#include <xp/stx/bytecode.h>
|
2005-07-19 12:08:04 +00:00
|
|
|
#include <xp/stx/dict.h>
|
2005-07-05 06:26:33 +00:00
|
|
|
#include <xp/stx/misc.h>
|
2005-05-22 04:11:54 +00:00
|
|
|
|
2005-06-06 03:47:56 +00:00
|
|
|
static int __parse_method (
|
|
|
|
xp_stx_parser_t* parser,
|
2005-06-08 16:05:41 +00:00
|
|
|
xp_word_t method_class, void* input);
|
2005-07-04 11:32:41 +00:00
|
|
|
static int __finish_method (xp_stx_parser_t* parser);
|
2005-06-12 15:46:02 +00:00
|
|
|
|
2005-06-06 03:54:32 +00:00
|
|
|
static int __parse_message_pattern (xp_stx_parser_t* parser);
|
2005-06-12 15:46:02 +00:00
|
|
|
static int __parse_unary_pattern (xp_stx_parser_t* parser);
|
|
|
|
static int __parse_binary_pattern (xp_stx_parser_t* parser);
|
|
|
|
static int __parse_keyword_pattern (xp_stx_parser_t* parser);
|
|
|
|
|
2005-06-08 03:16:34 +00:00
|
|
|
static int __parse_temporaries (xp_stx_parser_t* parser);
|
2005-07-04 10:02:00 +00:00
|
|
|
static int __parse_primitive (xp_stx_parser_t* parser);
|
2005-06-08 03:16:34 +00:00
|
|
|
static int __parse_statements (xp_stx_parser_t* parser);
|
2005-06-29 10:56:42 +00:00
|
|
|
static int __parse_block_statements (xp_stx_parser_t* parser);
|
2005-06-23 04:55:44 +00:00
|
|
|
static int __parse_statement (xp_stx_parser_t* parser);
|
2005-06-11 18:01:25 +00:00
|
|
|
static int __parse_expression (xp_stx_parser_t* parser);
|
2005-06-06 03:47:56 +00:00
|
|
|
|
2005-06-19 16:16:33 +00:00
|
|
|
static int __parse_assignment (
|
|
|
|
xp_stx_parser_t* parser, const xp_char_t* target);
|
2005-06-23 04:55:44 +00:00
|
|
|
static int __parse_basic_expression (
|
|
|
|
xp_stx_parser_t* parser, const xp_char_t* ident);
|
|
|
|
static int __parse_primary (
|
2005-09-30 12:19:00 +00:00
|
|
|
xp_stx_parser_t* parser, const xp_char_t* ident, xp_bool_t* is_super);
|
2005-07-08 11:32:50 +00:00
|
|
|
static int __parse_primary_ident (
|
2005-09-30 12:19:00 +00:00
|
|
|
xp_stx_parser_t* parser, const xp_char_t* ident, xp_bool_t* is_super);
|
2005-07-08 11:32:50 +00:00
|
|
|
|
2005-06-23 04:55:44 +00:00
|
|
|
static int __parse_block_constructor (xp_stx_parser_t* parser);
|
2005-09-30 12:19:00 +00:00
|
|
|
static int __parse_message_continuation (
|
|
|
|
xp_stx_parser_t* parser, xp_bool_t is_super);
|
|
|
|
static int __parse_keyword_message (
|
|
|
|
xp_stx_parser_t* parser, xp_bool_t is_super);
|
|
|
|
static int __parse_binary_message (
|
|
|
|
xp_stx_parser_t* parser, xp_bool_t is_super);
|
|
|
|
static int __parse_unary_message (
|
|
|
|
xp_stx_parser_t* parser, xp_bool_t is_super);
|
2005-06-19 16:16:33 +00:00
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
static int __get_token (xp_stx_parser_t* parser);
|
2005-06-05 16:44:05 +00:00
|
|
|
static int __get_ident (xp_stx_parser_t* parser);
|
2005-06-07 16:09:58 +00:00
|
|
|
static int __get_numlit (xp_stx_parser_t* parser, xp_bool_t negated);
|
2005-06-05 16:44:05 +00:00
|
|
|
static int __get_charlit (xp_stx_parser_t* parser);
|
|
|
|
static int __get_strlit (xp_stx_parser_t* parser);
|
2005-06-08 15:49:35 +00:00
|
|
|
static int __get_binary (xp_stx_parser_t* parser);
|
2005-06-05 05:27:02 +00:00
|
|
|
static int __skip_spaces (xp_stx_parser_t* parser);
|
|
|
|
static int __skip_comment (xp_stx_parser_t* parser);
|
|
|
|
static int __get_char (xp_stx_parser_t* parser);
|
|
|
|
static int __unget_char (xp_stx_parser_t* parser, xp_cint_t c);
|
2005-06-06 03:47:56 +00:00
|
|
|
static int __open_input (xp_stx_parser_t* parser, void* input);
|
|
|
|
static int __close_input (xp_stx_parser_t* parser);
|
2005-06-05 05:27:02 +00:00
|
|
|
|
2005-06-08 03:19:31 +00:00
|
|
|
xp_stx_parser_t* xp_stx_parser_open (xp_stx_parser_t* parser, xp_stx_t* stx)
|
2005-05-22 04:11:54 +00:00
|
|
|
{
|
|
|
|
if (parser == XP_NULL) {
|
|
|
|
parser = (xp_stx_parser_t*)
|
2005-06-08 16:05:41 +00:00
|
|
|
xp_malloc (xp_sizeof(xp_stx_parser_t));
|
2005-05-22 04:11:54 +00:00
|
|
|
if (parser == XP_NULL) return XP_NULL;
|
|
|
|
parser->__malloced = xp_true;
|
|
|
|
}
|
|
|
|
else parser->__malloced = xp_false;
|
|
|
|
|
2005-06-12 16:22:47 +00:00
|
|
|
if (xp_stx_name_open (&parser->method_name, 0) == XP_NULL) {
|
|
|
|
if (parser->__malloced) xp_free (parser);
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-12 15:46:02 +00:00
|
|
|
if (xp_stx_token_open (&parser->token, 0) == XP_NULL) {
|
2005-06-12 16:22:47 +00:00
|
|
|
xp_stx_name_close (&parser->method_name);
|
2005-06-08 16:05:41 +00:00
|
|
|
if (parser->__malloced) xp_free (parser);
|
2005-05-30 15:27:31 +00:00
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-19 16:16:33 +00:00
|
|
|
if (xp_array_open (
|
2005-07-05 09:02:13 +00:00
|
|
|
&parser->bytecode, 256,
|
2005-06-19 16:16:33 +00:00
|
|
|
xp_sizeof(xp_byte_t), XP_NULL) == XP_NULL) {
|
|
|
|
xp_stx_name_close (&parser->method_name);
|
|
|
|
xp_stx_token_close (&parser->token);
|
|
|
|
if (parser->__malloced) xp_free (parser);
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-08 03:19:31 +00:00
|
|
|
parser->stx = stx;
|
2005-06-05 05:27:02 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_NONE;
|
2005-06-12 14:40:35 +00:00
|
|
|
|
2005-06-12 16:46:45 +00:00
|
|
|
parser->temporary_count = 0;
|
2005-07-07 16:32:37 +00:00
|
|
|
parser->argument_count = 0;
|
2005-07-04 11:47:25 +00:00
|
|
|
parser->literal_count = 0;
|
2005-06-12 14:40:35 +00:00
|
|
|
|
2005-06-08 16:05:41 +00:00
|
|
|
parser->curc = XP_CHAR_EOF;
|
2005-06-05 05:27:02 +00:00
|
|
|
parser->ungotc_count = 0;
|
2005-06-04 16:27:30 +00:00
|
|
|
|
2005-06-06 03:47:56 +00:00
|
|
|
parser->input_owner = XP_NULL;
|
|
|
|
parser->input_func = XP_NULL;
|
2005-05-22 04:11:54 +00:00
|
|
|
return parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xp_stx_parser_close (xp_stx_parser_t* parser)
|
|
|
|
{
|
2005-06-12 16:46:45 +00:00
|
|
|
while (parser->temporary_count > 0) {
|
2005-07-07 16:32:37 +00:00
|
|
|
xp_free (parser->temporaries[--parser->temporary_count]);
|
2005-06-12 16:46:45 +00:00
|
|
|
}
|
2005-07-07 16:32:37 +00:00
|
|
|
parser->argument_count = 0;
|
2005-06-12 15:46:02 +00:00
|
|
|
|
2005-07-05 09:02:13 +00:00
|
|
|
xp_array_close (&parser->bytecode);
|
2005-06-12 16:22:47 +00:00
|
|
|
xp_stx_name_close (&parser->method_name);
|
2005-06-04 07:01:57 +00:00
|
|
|
xp_stx_token_close (&parser->token);
|
2005-06-19 16:16:33 +00:00
|
|
|
|
2005-06-08 16:05:41 +00:00
|
|
|
if (parser->__malloced) xp_free (parser);
|
2005-05-22 04:11:54 +00:00
|
|
|
}
|
2005-05-12 15:51:20 +00:00
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
#define GET_CHAR(parser) \
|
|
|
|
do { if (__get_char(parser) == -1) return -1; } while (0)
|
|
|
|
#define UNGET_CHAR(parser,c) \
|
|
|
|
do { if (__unget_char(parser,c) == -1) return -1; } while (0)
|
2005-06-05 16:44:05 +00:00
|
|
|
#define GET_TOKEN(parser) \
|
|
|
|
do { if (__get_token(parser) == -1) return -1; } while (0)
|
2005-06-06 16:04:18 +00:00
|
|
|
#define ADD_TOKEN_CHAR(parser,c) \
|
|
|
|
do { \
|
|
|
|
if (xp_stx_token_addc (&(parser)->token, c) == -1) { \
|
|
|
|
(parser)->error_code = XP_STX_PARSER_ERROR_MEMORY; \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2005-06-05 16:44:05 +00:00
|
|
|
|
2005-06-12 12:33:31 +00:00
|
|
|
const xp_char_t* xp_stx_parser_error_string (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
static const xp_char_t* msg[] =
|
|
|
|
{
|
|
|
|
XP_TEXT("no error"),
|
|
|
|
|
|
|
|
XP_TEXT("input fucntion not ready"),
|
|
|
|
XP_TEXT("input function error"),
|
|
|
|
XP_TEXT("out of memory"),
|
|
|
|
|
|
|
|
XP_TEXT("invalid character"),
|
|
|
|
XP_TEXT("incomplete character literal"),
|
|
|
|
XP_TEXT("incomplete string literal"),
|
2005-06-23 04:55:44 +00:00
|
|
|
XP_TEXT("incomplete literal"),
|
2005-06-12 12:33:31 +00:00
|
|
|
|
|
|
|
XP_TEXT("message selector"),
|
|
|
|
XP_TEXT("invalid argument name"),
|
2005-06-12 14:40:35 +00:00
|
|
|
XP_TEXT("too many arguments"),
|
2005-07-04 10:02:00 +00:00
|
|
|
|
|
|
|
XP_TEXT("invalid primitive type"),
|
|
|
|
XP_TEXT("primitive number expected"),
|
2005-07-07 07:45:05 +00:00
|
|
|
XP_TEXT("primitive number out of range"),
|
2005-07-04 10:02:00 +00:00
|
|
|
XP_TEXT("primitive not closed"),
|
|
|
|
|
2005-06-12 16:46:45 +00:00
|
|
|
XP_TEXT("temporary list not closed"),
|
|
|
|
XP_TEXT("too many temporaries"),
|
2005-06-19 16:16:33 +00:00
|
|
|
XP_TEXT("cannot redefine pseudo variable"),
|
2005-06-23 04:55:44 +00:00
|
|
|
XP_TEXT("invalid primary/expression-start"),
|
2005-06-12 16:51:57 +00:00
|
|
|
|
2005-06-23 04:55:44 +00:00
|
|
|
XP_TEXT("no period at end of statement"),
|
2005-06-23 07:20:44 +00:00
|
|
|
XP_TEXT("no closing parenthesis"),
|
|
|
|
XP_TEXT("block argument name missing"),
|
|
|
|
XP_TEXT("block argument list not closed"),
|
2005-06-29 12:02:39 +00:00
|
|
|
XP_TEXT("block not closed"),
|
|
|
|
|
2005-07-11 13:41:59 +00:00
|
|
|
XP_TEXT("undeclared name"),
|
|
|
|
XP_TEXT("too many literals")
|
2005-06-12 12:33:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (parser->error_code >= 0 &&
|
|
|
|
parser->error_code < xp_countof(msg)) return msg[parser->error_code];
|
|
|
|
|
|
|
|
return XP_TEXT("unknown error");
|
|
|
|
}
|
|
|
|
|
2005-06-28 15:23:58 +00:00
|
|
|
static INLINE xp_bool_t __is_pseudo_variable (const xp_stx_token_t* token)
|
2005-06-19 16:16:33 +00:00
|
|
|
{
|
|
|
|
return token->type == XP_STX_TOKEN_IDENT &&
|
|
|
|
(xp_strcmp(token->name.buffer, XP_TEXT("self")) == 0 ||
|
|
|
|
xp_strcmp(token->name.buffer, XP_TEXT("super")) == 0 ||
|
|
|
|
xp_strcmp(token->name.buffer, XP_TEXT("nil")) == 0 ||
|
|
|
|
xp_strcmp(token->name.buffer, XP_TEXT("true")) == 0 ||
|
|
|
|
xp_strcmp(token->name.buffer, XP_TEXT("false")) == 0);
|
|
|
|
}
|
|
|
|
|
2005-06-28 15:23:58 +00:00
|
|
|
static INLINE xp_bool_t __is_vbar_token (const xp_stx_token_t* token)
|
2005-06-23 07:20:44 +00:00
|
|
|
{
|
|
|
|
return
|
|
|
|
token->type == XP_STX_TOKEN_BINARY &&
|
|
|
|
token->name.size == 1 &&
|
|
|
|
token->name.buffer[0] == XP_CHAR('|');
|
|
|
|
}
|
|
|
|
|
2005-07-04 10:02:00 +00:00
|
|
|
static INLINE xp_bool_t __is_primitive_opener (const xp_stx_token_t* token)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
token->type == XP_STX_TOKEN_BINARY &&
|
|
|
|
token->name.size == 1 &&
|
|
|
|
token->name.buffer[0] == XP_CHAR('<');
|
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE xp_bool_t __is_primitive_closer (const xp_stx_token_t* token)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
token->type == XP_STX_TOKEN_BINARY &&
|
|
|
|
token->name.size == 1 &&
|
|
|
|
token->name.buffer[0] == XP_CHAR('>');
|
|
|
|
}
|
|
|
|
|
2005-06-28 15:23:58 +00:00
|
|
|
static INLINE xp_bool_t __is_binary_char (xp_cint_t c)
|
2005-06-23 07:20:44 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* binaryCharacter ::=
|
|
|
|
* '!' | '%' | '&' | '*' | '+' | ',' |
|
|
|
|
* '/' | '<' | '=' | '>' | '?' | '@' |
|
|
|
|
* '\' | '~' | '|' | '-'
|
|
|
|
*/
|
|
|
|
|
|
|
|
return
|
|
|
|
c == XP_CHAR('!') || c == XP_CHAR('%') ||
|
|
|
|
c == XP_CHAR('&') || c == XP_CHAR('*') ||
|
|
|
|
c == XP_CHAR('+') || c == XP_CHAR(',') ||
|
|
|
|
c == XP_CHAR('/') || c == XP_CHAR('<') ||
|
|
|
|
c == XP_CHAR('=') || c == XP_CHAR('>') ||
|
|
|
|
c == XP_CHAR('?') || c == XP_CHAR('@') ||
|
|
|
|
c == XP_CHAR('\\') || c == XP_CHAR('|') ||
|
|
|
|
c == XP_CHAR('~') || c == XP_CHAR('-');
|
|
|
|
}
|
|
|
|
|
2005-06-28 15:23:58 +00:00
|
|
|
static INLINE xp_bool_t __is_closing_char (xp_cint_t c)
|
2005-06-23 07:20:44 +00:00
|
|
|
{
|
|
|
|
return
|
|
|
|
c == XP_CHAR('.') || c == XP_CHAR(']') ||
|
|
|
|
c == XP_CHAR(')') || c == XP_CHAR(';') ||
|
|
|
|
c == XP_CHAR('\"') || c == XP_CHAR('\'');
|
|
|
|
}
|
|
|
|
|
2005-07-10 09:21:46 +00:00
|
|
|
#define EMIT_CODE_TEST(parser,high,low) \
|
|
|
|
do { if (__emit_code_test(parser,high,low) == -1) return -1; } while (0)
|
|
|
|
|
|
|
|
#define EMIT_CODE(parser,code) \
|
|
|
|
do { if (__emit_code(parser,code) == -1) return -1; } while(0)
|
|
|
|
|
|
|
|
#define EMIT_PUSH_RECEIVER_VARIABLE(parser,pos) \
|
|
|
|
do { \
|
|
|
|
if (__emit_stack_positional ( \
|
|
|
|
parser, PUSH_RECEIVER_VARIABLE, pos) == -1) return -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_PUSH_TEMPORARY_LOCATION(parser,pos) \
|
|
|
|
do { \
|
|
|
|
if (__emit_stack_positional ( \
|
|
|
|
parser, PUSH_TEMPORARY_LOCATION, pos) == -1) return -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_PUSH_LITERAL_CONSTANT(parser,pos) \
|
|
|
|
do { \
|
|
|
|
if (__emit_stack_positional ( \
|
|
|
|
parser, PUSH_LITERAL_CONSTANT, pos) == -1) return -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
#define EMIT_PUSH_LITERAL_VARIABLE(parser,pos) \
|
|
|
|
do { \
|
|
|
|
if (__emit_stack_positional ( \
|
|
|
|
parser, PUSH_LITERAL_VARIABLE, pos) == -1) return -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_STORE_RECEIVER_VARIABLE(parser,pos) \
|
|
|
|
do { \
|
|
|
|
if (__emit_stack_positional ( \
|
|
|
|
parser, STORE_RECEIVER_VARIABLE, pos) == -1) return -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_STORE_TEMPORARY_LOCATION(parser,pos) \
|
|
|
|
do { \
|
|
|
|
if (__emit_stack_positional ( \
|
|
|
|
parser, STORE_TEMPORARY_LOCATION, pos) == -1) return -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
#define EMIT_POP_STACK_TOP(parser) EMIT_CODE(parser, POP_STACK_TOP)
|
|
|
|
#define EMIT_DUPLICATE_STACK_TOP(parser) EMIT_CODE(parser, DUPLICATE_STACK_TOP)
|
|
|
|
#define EMIT_PUSH_ACTIVE_CONTEXT(parser) EMIT_CODE(parser, PUSH_ACTIVE_CONTEXT)
|
|
|
|
#define EMIT_RETURN_FROM_MESSAGE(parser) EMIT_CODE(parser, RETURN_FROM_MESSAGE)
|
|
|
|
#define EMIT_RETURN_FROM_BLOCK(parser) EMIT_CODE(parser, RETURN_FROM_BLOCK)
|
|
|
|
|
2005-07-10 16:50:50 +00:00
|
|
|
#define EMIT_SEND_TO_SELF(parser,nargs,selector) \
|
2005-07-10 09:21:46 +00:00
|
|
|
do { \
|
2005-07-10 16:50:50 +00:00
|
|
|
if (__emit_send_to_self(parser,nargs,selector) == -1) return -1; \
|
2005-07-10 09:21:46 +00:00
|
|
|
} while (0)
|
|
|
|
|
2005-07-10 16:50:50 +00:00
|
|
|
#define EMIT_SEND_TO_SUPER(parser,nargs,selector) \
|
2005-07-10 09:21:46 +00:00
|
|
|
do { \
|
2005-07-10 16:50:50 +00:00
|
|
|
if (__emit_send_to_super(parser,nargs,selector) == -1) return -1; \
|
2005-07-10 09:21:46 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_DO_PRIMITIVE(parser,no) \
|
|
|
|
do { if (__emit_do_primitive(parser,no) == -1) return -1; } while(0)
|
|
|
|
|
2005-07-05 11:38:01 +00:00
|
|
|
static INLINE int __emit_code_test (
|
2005-07-05 11:15:51 +00:00
|
|
|
xp_stx_parser_t* parser, const xp_char_t* high, const xp_char_t* low)
|
|
|
|
{
|
|
|
|
xp_printf (XP_TEXT("CODE: %s %s\n"), high, low);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE int __emit_code (xp_stx_parser_t* parser, xp_byte_t code)
|
|
|
|
{
|
2005-07-07 07:45:05 +00:00
|
|
|
if (xp_array_add_datum(&parser->bytecode, &code) == XP_NULL) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-07-05 11:15:51 +00:00
|
|
|
}
|
|
|
|
|
2005-07-10 09:21:46 +00:00
|
|
|
static INLINE int __emit_stack_positional (
|
2005-07-10 03:16:40 +00:00
|
|
|
xp_stx_parser_t* parser, int opcode, int pos)
|
|
|
|
{
|
2005-07-10 16:50:50 +00:00
|
|
|
xp_assert (pos >= 0x0 && pos <= 0xFF);
|
2005-07-10 03:16:40 +00:00
|
|
|
|
2005-07-10 09:21:46 +00:00
|
|
|
if (pos <= 0x0F) {
|
|
|
|
EMIT_CODE (parser, (opcode & 0xF0) | (pos & 0x0F));
|
2005-07-10 03:16:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-07-10 09:21:46 +00:00
|
|
|
EMIT_CODE (parser, (opcode >> 4) & 0x6F);
|
|
|
|
EMIT_CODE (parser, pos & 0xFF);
|
2005-07-10 03:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-10 09:21:46 +00:00
|
|
|
static INLINE int __emit_send_to_self (
|
2005-07-10 16:50:50 +00:00
|
|
|
xp_stx_parser_t* parser, int nargs, int selector)
|
2005-07-10 03:16:40 +00:00
|
|
|
{
|
2005-07-10 16:50:50 +00:00
|
|
|
xp_assert (nargs >= 0x00 && nargs <= 0xFF);
|
2005-07-10 09:21:46 +00:00
|
|
|
xp_assert (selector >= 0x00 && selector <= 0xFF);
|
2005-07-10 16:50:50 +00:00
|
|
|
|
|
|
|
if (nargs <= 0x08 && selector <= 0x1F) {
|
|
|
|
EMIT_CODE (parser, SEND_TO_SELF);
|
|
|
|
EMIT_CODE (parser, (nargs << 5) | selector);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
EMIT_CODE (parser, SEND_TO_SELF_EXTENDED);
|
|
|
|
EMIT_CODE (parser, nargs);
|
|
|
|
EMIT_CODE (parser, selector);
|
|
|
|
}
|
2005-07-11 13:41:59 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-07-10 09:21:46 +00:00
|
|
|
}
|
2005-07-08 11:32:50 +00:00
|
|
|
|
2005-07-10 09:21:46 +00:00
|
|
|
static INLINE int __emit_send_to_super (
|
2005-10-02 10:44:49 +00:00
|
|
|
xp_stx_parser_t* parser, int nargs, int selector)
|
2005-07-10 09:21:46 +00:00
|
|
|
{
|
2005-07-10 16:50:50 +00:00
|
|
|
xp_assert (nargs >= 0x00 && nargs <= 0xFF);
|
|
|
|
xp_assert (selector >= 0x00 && selector <= 0xFF);
|
|
|
|
|
|
|
|
if (nargs <= 0x08 && selector <= 0x1F) {
|
|
|
|
EMIT_CODE (parser, SEND_TO_SUPER);
|
|
|
|
EMIT_CODE (parser, (nargs << 5) | selector);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
EMIT_CODE (parser, SEND_TO_SUPER_EXTENDED);
|
|
|
|
EMIT_CODE (parser, nargs);
|
|
|
|
EMIT_CODE (parser, selector);
|
|
|
|
}
|
2005-07-11 13:41:59 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-07-08 11:32:50 +00:00
|
|
|
}
|
|
|
|
|
2005-07-10 09:21:46 +00:00
|
|
|
static INLINE int __emit_do_primitive (xp_stx_parser_t* parser, int no)
|
|
|
|
{
|
|
|
|
xp_assert (no >= 0x0 && no <= 0xFFF);
|
|
|
|
|
2005-09-11 15:15:35 +00:00
|
|
|
EMIT_CODE (parser, DO_PRIMITIVE | ((no >> 8) & 0x0F));
|
2005-07-10 09:21:46 +00:00
|
|
|
EMIT_CODE (parser, no & 0xFF);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-07 07:45:05 +00:00
|
|
|
|
2005-07-12 16:16:42 +00:00
|
|
|
static int __add_literal (xp_stx_parser_t* parser, xp_word_t literal)
|
2005-07-11 13:41:59 +00:00
|
|
|
{
|
2005-07-12 16:16:42 +00:00
|
|
|
xp_word_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < parser->literal_count; i++) {
|
|
|
|
/*
|
|
|
|
* it would remove redundancy of symbols and small integers.
|
|
|
|
* more complex redundacy check may be done somewhere else
|
|
|
|
* like in __add_string_literal.
|
|
|
|
*/
|
|
|
|
if (parser->literals[i] == literal) return i;
|
|
|
|
}
|
|
|
|
|
2005-07-11 13:41:59 +00:00
|
|
|
if (parser->literal_count >= xp_countof(parser->literals)) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_TOO_MANY_LITERALS;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-07-12 16:16:42 +00:00
|
|
|
|
2005-07-11 13:41:59 +00:00
|
|
|
parser->literals[parser->literal_count++] = literal;
|
|
|
|
return parser->literal_count - 1;
|
|
|
|
}
|
|
|
|
|
2005-07-12 16:16:42 +00:00
|
|
|
static int __add_character_literal (xp_stx_parser_t* parser, xp_char_t ch)
|
|
|
|
{
|
|
|
|
xp_word_t i, c, literal;
|
|
|
|
xp_stx_t* stx = parser->stx;
|
|
|
|
|
|
|
|
for (i = 0; i < parser->literal_count; i++) {
|
|
|
|
c = XP_STX_IS_SMALLINT(parser->literals[i])?
|
|
|
|
stx->class_smallinteger: XP_STX_CLASS (stx, parser->literals[i]);
|
|
|
|
if (c != stx->class_character) continue;
|
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
if (ch == XP_STX_CHAR_AT(stx,parser->literals[i],0)) return i;
|
2005-07-12 16:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
literal = xp_stx_instantiate (
|
|
|
|
stx, stx->class_character, &ch, XP_NULL, 0);
|
|
|
|
return __add_literal (parser, literal);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __add_string_literal (
|
|
|
|
xp_stx_parser_t* parser, const xp_char_t* str, xp_word_t size)
|
|
|
|
{
|
|
|
|
xp_word_t i, c, literal;
|
|
|
|
xp_stx_t* stx = parser->stx;
|
|
|
|
|
|
|
|
for (i = 0; i < parser->literal_count; i++) {
|
|
|
|
c = XP_STX_IS_SMALLINT(parser->literals[i])?
|
|
|
|
stx->class_smallinteger: XP_STX_CLASS (stx, parser->literals[i]);
|
|
|
|
if (c != stx->class_string) continue;
|
|
|
|
|
|
|
|
if (xp_strxncmp (str, size,
|
|
|
|
XP_STX_DATA(stx,parser->literals[i]),
|
|
|
|
XP_STX_SIZE(stx,parser->literals[i])) == 0) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
literal = xp_stx_instantiate (
|
|
|
|
stx, stx->class_string, XP_NULL, str, size);
|
|
|
|
return __add_literal (parser, literal);
|
|
|
|
}
|
|
|
|
|
2005-09-11 17:01:56 +00:00
|
|
|
static int __add_symbol_literal (
|
|
|
|
xp_stx_parser_t* parser, const xp_char_t* str, xp_word_t size)
|
|
|
|
{
|
|
|
|
xp_stx_t* stx = parser->stx;
|
|
|
|
return __add_literal (parser, xp_stx_new_symbolx(stx, str, size));
|
|
|
|
}
|
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
int xp_stx_parser_parse_method (
|
2005-06-08 16:05:41 +00:00
|
|
|
xp_stx_parser_t* parser, xp_word_t method_class, void* input)
|
2005-06-05 05:27:02 +00:00
|
|
|
{
|
2005-06-06 03:47:56 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
if (parser->input_func == XP_NULL) {
|
2005-06-12 12:33:31 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_INPUT_FUNC;
|
2005-06-05 05:27:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-12 16:46:45 +00:00
|
|
|
parser->method_class = method_class;
|
|
|
|
if (__open_input(parser, input) == -1) return -1;
|
2005-06-06 03:47:56 +00:00
|
|
|
n = __parse_method (parser, method_class, input);
|
|
|
|
if (__close_input(parser) == -1) return -1;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __parse_method (
|
2005-06-08 16:05:41 +00:00
|
|
|
xp_stx_parser_t* parser, xp_word_t method_class, void* input)
|
2005-06-06 03:47:56 +00:00
|
|
|
{
|
2005-06-08 03:16:34 +00:00
|
|
|
/*
|
|
|
|
* <method definition> ::=
|
2005-07-04 10:02:00 +00:00
|
|
|
* <message pattern> [<temporaries>] [<primitive>] [<statements>]
|
2005-06-08 03:16:34 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
GET_CHAR (parser);
|
2005-06-05 16:44:05 +00:00
|
|
|
GET_TOKEN (parser);
|
2005-06-05 05:27:02 +00:00
|
|
|
|
2005-06-12 16:46:45 +00:00
|
|
|
xp_stx_name_clear (&parser->method_name);
|
2005-07-05 09:02:13 +00:00
|
|
|
xp_array_clear (&parser->bytecode);
|
2005-06-12 16:46:45 +00:00
|
|
|
|
|
|
|
while (parser->temporary_count > 0) {
|
2005-07-07 16:32:37 +00:00
|
|
|
xp_free (parser->temporaries[--parser->temporary_count]);
|
2005-06-12 16:46:45 +00:00
|
|
|
}
|
2005-07-07 16:32:37 +00:00
|
|
|
parser->argument_count = 0;
|
2005-07-04 11:47:25 +00:00
|
|
|
parser->literal_count = 0;
|
2005-06-12 16:46:45 +00:00
|
|
|
|
2005-06-23 07:20:44 +00:00
|
|
|
if (__parse_message_pattern(parser) == -1) return -1;
|
|
|
|
if (__parse_temporaries(parser) == -1) return -1;
|
2005-07-04 10:02:00 +00:00
|
|
|
if (__parse_primitive(parser) == -1) return -1;
|
2005-06-23 07:20:44 +00:00
|
|
|
if (__parse_statements(parser) == -1) return -1;
|
2005-07-04 11:32:41 +00:00
|
|
|
if (__finish_method (parser) == -1) return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __finish_method (xp_stx_parser_t* parser)
|
|
|
|
{
|
2005-07-05 06:26:33 +00:00
|
|
|
xp_stx_t* stx = parser->stx;
|
2005-07-04 11:32:41 +00:00
|
|
|
xp_stx_class_t* class_obj;
|
2005-07-05 09:02:13 +00:00
|
|
|
xp_stx_method_t* method_obj;
|
|
|
|
xp_word_t method, selector;
|
|
|
|
|
|
|
|
xp_assert (parser->bytecode.size != 0);
|
2005-07-04 11:32:41 +00:00
|
|
|
|
|
|
|
class_obj = (xp_stx_class_t*)
|
2005-07-07 07:45:05 +00:00
|
|
|
XP_STX_OBJECT(stx, parser->method_class);
|
2005-07-04 11:32:41 +00:00
|
|
|
|
2005-07-05 06:26:33 +00:00
|
|
|
if (class_obj->methods == stx->nil) {
|
2005-07-04 11:32:41 +00:00
|
|
|
/* TODO: reconfigure method dictionary size */
|
2005-07-05 06:26:33 +00:00
|
|
|
class_obj->methods = xp_stx_instantiate (
|
2005-07-19 12:08:04 +00:00
|
|
|
stx, stx->class_system_dictionary,
|
|
|
|
XP_NULL, XP_NULL, 64);
|
2005-07-04 11:32:41 +00:00
|
|
|
}
|
2005-07-05 06:26:33 +00:00
|
|
|
xp_assert (class_obj->methods != stx->nil);
|
2005-07-04 11:32:41 +00:00
|
|
|
|
2005-07-05 09:02:13 +00:00
|
|
|
selector = xp_stx_new_symbolx (
|
|
|
|
stx, parser->method_name.buffer, parser->method_name.size);
|
2005-07-04 11:32:41 +00:00
|
|
|
|
2005-07-05 09:02:13 +00:00
|
|
|
method = xp_stx_instantiate(stx, stx->class_method,
|
|
|
|
XP_NULL, parser->literals, parser->literal_count);
|
2005-07-07 07:45:05 +00:00
|
|
|
method_obj = (xp_stx_method_t*)XP_STX_OBJECT(stx, method);
|
2005-07-04 11:32:41 +00:00
|
|
|
|
2005-07-05 09:02:13 +00:00
|
|
|
/* TODO: text saving must be optional */
|
|
|
|
/*method_obj->text = xp_stx_instantiate (
|
|
|
|
stx, stx->class_string, XP_NULL,
|
|
|
|
parser->text, xp_strlen(parser->text));
|
|
|
|
*/
|
|
|
|
method_obj->selector = selector;
|
|
|
|
method_obj->bytecodes = xp_stx_instantiate (
|
|
|
|
stx, stx->class_bytearray, XP_NULL,
|
|
|
|
parser->bytecode.buffer, parser->bytecode.size);
|
2005-09-11 15:15:35 +00:00
|
|
|
|
|
|
|
/* TODO: better way to store argument count & temporary count */
|
|
|
|
method_obj->tmpcount =
|
2005-09-13 11:15:41 +00:00
|
|
|
XP_STX_TO_SMALLINT(parser->temporary_count - parser->argument_count);
|
|
|
|
method_obj->argcount = XP_STX_TO_SMALLINT(parser->argument_count);
|
2005-07-05 09:52:00 +00:00
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_stx_dict_put (stx, class_obj->methods, selector, method);
|
2005-06-06 03:54:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-06 15:46:48 +00:00
|
|
|
static int __parse_message_pattern (xp_stx_parser_t* parser)
|
2005-06-06 03:54:32 +00:00
|
|
|
{
|
2005-06-08 03:16:34 +00:00
|
|
|
/*
|
|
|
|
* <message pattern> ::=
|
|
|
|
* <unary pattern> | <binary pattern> | <keyword pattern>
|
|
|
|
* <unary pattern> ::= unarySelector
|
|
|
|
* <binary pattern> ::= binarySelector <method argument>
|
|
|
|
* <keyword pattern> ::= (keyword <method argument>)+
|
|
|
|
*/
|
2005-06-12 15:46:02 +00:00
|
|
|
int n;
|
2005-06-08 03:16:34 +00:00
|
|
|
|
|
|
|
if (parser->token.type == XP_STX_TOKEN_IDENT) {
|
2005-06-12 15:46:02 +00:00
|
|
|
n = __parse_unary_pattern (parser);
|
2005-06-07 16:09:58 +00:00
|
|
|
}
|
2005-06-08 03:16:34 +00:00
|
|
|
else if (parser->token.type == XP_STX_TOKEN_BINARY) {
|
2005-06-12 15:46:02 +00:00
|
|
|
n = __parse_binary_pattern (parser);
|
|
|
|
}
|
|
|
|
else if (parser->token.type == XP_STX_TOKEN_KEYWORD) {
|
|
|
|
n = __parse_keyword_pattern (parser);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MESSAGE_SELECTOR;
|
|
|
|
n = -1;
|
|
|
|
}
|
|
|
|
|
2005-07-07 16:32:37 +00:00
|
|
|
parser->temporary_count = parser->argument_count;
|
2005-06-12 15:46:02 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __parse_unary_pattern (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
/* TODO: check if the method name exists */
|
2005-06-12 16:46:45 +00:00
|
|
|
|
2005-06-12 16:07:23 +00:00
|
|
|
if (xp_stx_name_adds(
|
2005-06-12 16:22:47 +00:00
|
|
|
&parser->method_name, parser->token.name.buffer) == -1) {
|
2005-06-12 16:07:23 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-12 15:46:02 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __parse_binary_pattern (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
/* TODO: check if the method name exists */
|
2005-06-12 16:46:45 +00:00
|
|
|
|
2005-06-12 16:07:23 +00:00
|
|
|
if (xp_stx_name_adds(
|
2005-06-12 16:22:47 +00:00
|
|
|
&parser->method_name, parser->token.name.buffer) == -1) {
|
2005-06-12 16:07:23 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-06-12 15:46:02 +00:00
|
|
|
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
if (parser->token.type != XP_STX_TOKEN_IDENT) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_ARGUMENT_NAME;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-07-07 16:32:37 +00:00
|
|
|
if (parser->argument_count >= xp_countof(parser->temporaries)) {
|
2005-06-12 15:46:02 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_TOO_MANY_ARGUMENTS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-12 16:46:45 +00:00
|
|
|
/* TODO: check for duplicate entries...in instvars */
|
2005-07-07 16:32:37 +00:00
|
|
|
parser->temporaries[parser->argument_count] =
|
2005-06-12 15:46:02 +00:00
|
|
|
xp_stx_token_yield (&parser->token, 0);
|
2005-07-07 16:32:37 +00:00
|
|
|
if (parser->temporaries[parser->argument_count] == XP_NULL) {
|
2005-06-12 15:46:02 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
parser->argument_count++;
|
|
|
|
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __parse_keyword_pattern (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
do {
|
2005-06-12 16:07:23 +00:00
|
|
|
if (xp_stx_name_adds(
|
2005-06-12 16:22:47 +00:00
|
|
|
&parser->method_name, parser->token.name.buffer) == -1) {
|
2005-06-12 16:07:23 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-12 12:33:31 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
if (parser->token.type != XP_STX_TOKEN_IDENT) {
|
2005-06-12 14:40:35 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_ARGUMENT_NAME;
|
2005-06-12 12:33:31 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-19 16:16:33 +00:00
|
|
|
if (__is_pseudo_variable(&parser->token)) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_PSEUDO_VARIABLE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-07-07 16:32:37 +00:00
|
|
|
if (parser->argument_count >= xp_countof(parser->temporaries)) {
|
2005-06-12 15:46:02 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_TOO_MANY_ARGUMENTS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-07-07 16:32:37 +00:00
|
|
|
parser->temporaries[parser->argument_count] =
|
2005-06-12 15:46:02 +00:00
|
|
|
xp_stx_token_yield (&parser->token, 0);
|
2005-07-07 16:32:37 +00:00
|
|
|
if (parser->temporaries[parser->argument_count] == XP_NULL) {
|
2005-06-12 15:46:02 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-07-07 16:32:37 +00:00
|
|
|
|
|
|
|
/* TODO: check for duplicate entries...in instvars/arguments */
|
2005-06-12 15:46:02 +00:00
|
|
|
parser->argument_count++;
|
|
|
|
|
2005-06-12 12:33:31 +00:00
|
|
|
GET_TOKEN (parser);
|
2005-06-12 15:46:02 +00:00
|
|
|
} while (parser->token.type == XP_STX_TOKEN_KEYWORD);
|
|
|
|
|
|
|
|
/* TODO: check if the method name exists */
|
|
|
|
/* if it exists, collapse arguments */
|
2005-06-12 16:22:47 +00:00
|
|
|
xp_printf (XP_TEXT("METHOD NAME ==> [%s]\n"), parser->method_name.buffer);
|
2005-06-11 18:01:25 +00:00
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-08 03:16:34 +00:00
|
|
|
static int __parse_temporaries (xp_stx_parser_t* parser)
|
|
|
|
{
|
2005-06-12 16:46:45 +00:00
|
|
|
/*
|
|
|
|
* <temporaries> ::= '|' <temporary variable list> '|'
|
|
|
|
* <temporary variable list> ::= identifier*
|
|
|
|
*/
|
|
|
|
|
2005-06-11 18:01:25 +00:00
|
|
|
if (!__is_vbar_token(&parser->token)) return 0;
|
|
|
|
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
while (parser->token.type == XP_STX_TOKEN_IDENT) {
|
2005-07-07 16:32:37 +00:00
|
|
|
if (parser->temporary_count >= xp_countof(parser->temporaries)) {
|
2005-06-12 16:46:45 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_TOO_MANY_TEMPORARIES;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-19 16:16:33 +00:00
|
|
|
if (__is_pseudo_variable(&parser->token)) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_PSEUDO_VARIABLE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-07-07 16:32:37 +00:00
|
|
|
parser->temporaries[parser->temporary_count] =
|
2005-06-12 16:46:45 +00:00
|
|
|
xp_stx_token_yield (&parser->token, 0);
|
2005-07-07 16:32:37 +00:00
|
|
|
if (parser->temporaries[parser->temporary_count] == XP_NULL) {
|
2005-06-12 16:46:45 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-07-07 16:32:37 +00:00
|
|
|
/* TODO: check for duplicate entries...in instvars/arguments/temporaries */
|
2005-06-12 16:46:45 +00:00
|
|
|
parser->temporary_count++;
|
|
|
|
|
2005-06-11 18:01:25 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
}
|
|
|
|
if (!__is_vbar_token(&parser->token)) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_TEMPORARIES_NOT_CLOSED;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
return 0;
|
2005-06-08 03:16:34 +00:00
|
|
|
}
|
|
|
|
|
2005-07-04 10:02:00 +00:00
|
|
|
static int __parse_primitive (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* <primitive> ::= '<' 'primitive:' number '>'
|
|
|
|
*/
|
|
|
|
|
2005-07-07 07:45:05 +00:00
|
|
|
int prim_no;
|
|
|
|
|
2005-07-04 10:02:00 +00:00
|
|
|
if (!__is_primitive_opener(&parser->token)) return 0;
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
|
|
|
|
if (parser->token.type != XP_STX_TOKEN_KEYWORD ||
|
|
|
|
xp_strcmp (parser->token.name.buffer, XP_TEXT("primitive:")) != 0) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_PRIMITIVE_KEYWORD;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (parser); /* TODO: only integer */
|
|
|
|
if (parser->token.type != XP_STX_TOKEN_NUMLIT) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_PRIMITIVE_NUMBER;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-07-07 07:45:05 +00:00
|
|
|
/*TODO: more checks the validity of the primitive number */
|
|
|
|
if (!xp_stristype(parser->token.name.buffer, xp_isdigit)) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_PRIMITIVE_NUMBER;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
XP_STRTOI (prim_no, parser->token.name.buffer, XP_NULL, 10);
|
2005-07-10 09:21:46 +00:00
|
|
|
if (prim_no < 0 || prim_no > 0xFF) {
|
2005-07-07 07:45:05 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_PRIMITIVE_NUMBER_RANGE;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-07-05 11:15:51 +00:00
|
|
|
|
2005-07-10 09:21:46 +00:00
|
|
|
EMIT_DO_PRIMITIVE (parser, prim_no);
|
2005-07-05 11:15:51 +00:00
|
|
|
|
2005-07-04 10:02:00 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
if (!__is_primitive_closer(&parser->token)) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_PRIMITIVE_NOT_CLOSED;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-08 03:16:34 +00:00
|
|
|
static int __parse_statements (xp_stx_parser_t* parser)
|
2005-06-11 18:01:25 +00:00
|
|
|
{
|
|
|
|
/*
|
2005-06-23 04:55:44 +00:00
|
|
|
* <statements> ::= (ORIGINAL->maybe wrong)
|
2005-06-11 18:01:25 +00:00
|
|
|
* (<return statement> ['.'] ) |
|
|
|
|
* (<expression> ['.' [<statements>]])
|
2005-06-23 04:55:44 +00:00
|
|
|
* <statements> ::= (REVISED->correct?)
|
|
|
|
* <statement> ['. [<statements>]]
|
2005-06-11 18:01:25 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-15 16:07:14 +00:00
|
|
|
while (parser->token.type != XP_STX_TOKEN_END) {
|
2005-06-23 04:55:44 +00:00
|
|
|
if (__parse_statement (parser) == -1) return -1;
|
2005-06-15 16:07:14 +00:00
|
|
|
|
|
|
|
if (parser->token.type == XP_STX_TOKEN_PERIOD) {
|
|
|
|
GET_TOKEN (parser);
|
2005-06-23 04:55:44 +00:00
|
|
|
continue;
|
2005-06-15 16:07:14 +00:00
|
|
|
}
|
2005-06-23 04:55:44 +00:00
|
|
|
|
|
|
|
if (parser->token.type != XP_STX_TOKEN_END) {
|
2005-06-15 16:07:14 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_NO_PERIOD;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-06-12 12:33:31 +00:00
|
|
|
}
|
2005-06-15 16:07:14 +00:00
|
|
|
|
2005-10-01 05:33:06 +00:00
|
|
|
EMIT_CODE (parser, RETURN_RECEIVER);
|
2005-06-12 12:33:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-29 10:56:42 +00:00
|
|
|
static int __parse_block_statements (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
while (parser->token.type != XP_STX_TOKEN_RBRACKET &&
|
|
|
|
parser->token.type != XP_STX_TOKEN_END) {
|
|
|
|
|
|
|
|
if (__parse_statement(parser) == -1) return -1;
|
|
|
|
if (parser->token.type != XP_STX_TOKEN_PERIOD) break;
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-23 04:55:44 +00:00
|
|
|
static int __parse_statement (xp_stx_parser_t* parser)
|
2005-06-12 12:33:31 +00:00
|
|
|
{
|
2005-06-23 04:55:44 +00:00
|
|
|
/*
|
|
|
|
* <statement> ::= <return statement> | <expression>
|
|
|
|
* <return statement> ::= returnOperator <expression>
|
|
|
|
* returnOperator ::= '^'
|
|
|
|
*/
|
|
|
|
|
2005-06-11 18:01:25 +00:00
|
|
|
if (parser->token.type == XP_STX_TOKEN_RETURN) {
|
2005-06-12 12:33:31 +00:00
|
|
|
GET_TOKEN (parser);
|
2005-06-23 04:55:44 +00:00
|
|
|
if (__parse_expression(parser) == -1) return -1;
|
2005-07-10 09:21:46 +00:00
|
|
|
EMIT_RETURN_FROM_MESSAGE (parser);
|
2005-06-11 18:01:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-06-23 04:55:44 +00:00
|
|
|
if (__parse_expression(parser) == -1) return -1;
|
2005-06-12 12:33:31 +00:00
|
|
|
}
|
|
|
|
|
2005-06-11 18:01:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __parse_expression (xp_stx_parser_t* parser)
|
2005-06-08 03:16:34 +00:00
|
|
|
{
|
2005-06-12 12:33:31 +00:00
|
|
|
/*
|
|
|
|
* <expression> ::= <assignment> | <basic expression>
|
2005-06-23 04:55:44 +00:00
|
|
|
* <assignment> ::= <assignment target> assignmentOperator <expression>
|
2005-06-12 12:33:31 +00:00
|
|
|
* <basic expression> ::= <primary> [<messages> <cascaded messages>]
|
2005-06-23 04:55:44 +00:00
|
|
|
* <assignment target> ::= identifier
|
2005-06-12 12:33:31 +00:00
|
|
|
* assignmentOperator ::= ':='
|
|
|
|
*/
|
2005-10-02 15:45:09 +00:00
|
|
|
xp_stx_t* stx = parser->stx;
|
2005-06-12 12:33:31 +00:00
|
|
|
|
|
|
|
if (parser->token.type == XP_STX_TOKEN_IDENT) {
|
2005-06-23 04:55:44 +00:00
|
|
|
xp_char_t* ident = xp_stx_token_yield (&parser->token, 0);
|
2005-06-19 16:16:33 +00:00
|
|
|
if (ident == XP_NULL) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-06-16 16:42:02 +00:00
|
|
|
|
2005-06-12 12:33:31 +00:00
|
|
|
GET_TOKEN (parser);
|
2005-06-15 16:07:14 +00:00
|
|
|
if (parser->token.type == XP_STX_TOKEN_ASSIGN) {
|
|
|
|
GET_TOKEN (parser);
|
2005-06-19 16:16:33 +00:00
|
|
|
if (__parse_assignment(parser, ident) == -1) {
|
|
|
|
xp_free (ident);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-06-15 16:07:14 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-06-23 04:55:44 +00:00
|
|
|
if (__parse_basic_expression(parser, ident) == -1) {
|
2005-06-19 16:16:33 +00:00
|
|
|
xp_free (ident);
|
2005-06-22 15:02:41 +00:00
|
|
|
return -1;
|
2005-06-19 16:16:33 +00:00
|
|
|
}
|
2005-06-15 16:07:14 +00:00
|
|
|
}
|
2005-06-19 16:16:33 +00:00
|
|
|
|
|
|
|
xp_free (ident);
|
2005-06-12 12:33:31 +00:00
|
|
|
}
|
2005-06-22 15:02:41 +00:00
|
|
|
else {
|
2005-06-23 04:55:44 +00:00
|
|
|
if (__parse_basic_expression(parser, XP_NULL) == -1) return -1;
|
2005-06-22 15:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-04 10:02:00 +00:00
|
|
|
static int __parse_basic_expression (
|
|
|
|
xp_stx_parser_t* parser, const xp_char_t* ident)
|
2005-06-22 15:02:41 +00:00
|
|
|
{
|
|
|
|
/*
|
2005-06-23 04:55:44 +00:00
|
|
|
* <basic expression> ::= <primary> [<messages> <cascaded messages>]
|
2005-06-22 15:02:41 +00:00
|
|
|
*/
|
2005-09-30 12:19:00 +00:00
|
|
|
xp_bool_t is_super;
|
2005-06-22 15:02:41 +00:00
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_primary(parser, ident, &is_super) == -1) return -1;
|
2005-06-23 04:55:44 +00:00
|
|
|
if (parser->token.type != XP_STX_TOKEN_END &&
|
|
|
|
parser->token.type != XP_STX_TOKEN_PERIOD) {
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_message_continuation(parser, is_super) == -1) return -1;
|
2005-06-12 12:33:31 +00:00
|
|
|
}
|
2005-06-23 04:55:44 +00:00
|
|
|
return 0;
|
2005-06-08 03:16:34 +00:00
|
|
|
}
|
2005-06-22 15:02:41 +00:00
|
|
|
|
2005-06-19 16:16:33 +00:00
|
|
|
static int __parse_assignment (
|
|
|
|
xp_stx_parser_t* parser, const xp_char_t* target)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* <assignment> ::= <assignment target> assignmentOperator <expression>
|
|
|
|
*/
|
|
|
|
|
2005-06-29 16:01:32 +00:00
|
|
|
xp_word_t i;
|
2005-07-05 06:26:33 +00:00
|
|
|
xp_stx_t* stx = parser->stx;
|
2005-06-19 16:16:33 +00:00
|
|
|
|
2005-07-07 16:32:37 +00:00
|
|
|
for (i = parser->argument_count; i < parser->temporary_count; i++) {
|
|
|
|
if (xp_strcmp (target, parser->temporaries[i]) == 0) {
|
2005-06-19 16:16:33 +00:00
|
|
|
if (__parse_expression(parser) == -1) return -1;
|
2005-07-10 09:21:46 +00:00
|
|
|
EMIT_STORE_TEMPORARY_LOCATION (parser, i);
|
2005-06-19 16:16:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-29 16:01:32 +00:00
|
|
|
if (xp_stx_get_instance_variable_index (
|
2005-07-05 06:26:33 +00:00
|
|
|
stx, parser->method_class, target, &i) == 0) {
|
2005-06-29 16:01:32 +00:00
|
|
|
if (__parse_expression(parser) == -1) return -1;
|
2005-07-10 09:21:46 +00:00
|
|
|
EMIT_STORE_RECEIVER_VARIABLE (parser, i);
|
2005-06-29 16:01:32 +00:00
|
|
|
return 0;
|
2005-06-29 12:02:39 +00:00
|
|
|
}
|
2005-06-19 16:16:33 +00:00
|
|
|
|
2005-07-03 16:37:01 +00:00
|
|
|
if (xp_stx_lookup_class_variable (
|
2005-07-05 06:26:33 +00:00
|
|
|
stx, parser->method_class, target) != stx->nil) {
|
2005-07-03 16:37:01 +00:00
|
|
|
if (__parse_expression(parser) == -1) return -1;
|
2005-07-08 11:32:50 +00:00
|
|
|
|
|
|
|
/* TODO */
|
2005-07-05 11:38:01 +00:00
|
|
|
EMIT_CODE_TEST (parser, XP_TEXT("ASSIGN_CLASSVAR #"), target);
|
2005-07-10 09:21:46 +00:00
|
|
|
//EMIT_STORE_CLASS_VARIABLE (parser, target);
|
2005-07-03 16:37:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-19 16:16:33 +00:00
|
|
|
|
2005-07-04 10:02:00 +00:00
|
|
|
/* TODO: IMPLEMENT POOL DICTIONARIES */
|
|
|
|
|
|
|
|
/* TODO: IMPLEMENT GLOBLAS, but i don't like this idea */
|
2005-06-19 16:16:33 +00:00
|
|
|
|
2005-06-29 12:02:39 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_UNDECLARED_NAME;
|
2005-06-19 16:16:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
static int __parse_primary (
|
|
|
|
xp_stx_parser_t* parser, const xp_char_t* ident, xp_bool_t* is_super)
|
2005-06-23 04:55:44 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* <primary> ::=
|
|
|
|
* identifier | <literal> |
|
|
|
|
* <block constructor> | ( '('<expression>')' )
|
|
|
|
*/
|
|
|
|
|
2005-07-11 13:41:59 +00:00
|
|
|
xp_stx_t* stx = parser->stx;
|
|
|
|
|
2005-06-23 04:55:44 +00:00
|
|
|
if (ident == XP_NULL) {
|
2005-07-11 13:41:59 +00:00
|
|
|
int pos;
|
|
|
|
xp_word_t literal;
|
|
|
|
|
2005-09-30 16:49:39 +00:00
|
|
|
*is_super = xp_false;
|
|
|
|
|
2005-06-23 04:55:44 +00:00
|
|
|
if (parser->token.type == XP_STX_TOKEN_IDENT) {
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_primary_ident(parser,
|
|
|
|
parser->token.name.buffer, is_super) == -1) return -1;
|
2005-06-23 04:55:44 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
}
|
|
|
|
else if (parser->token.type == XP_STX_TOKEN_CHARLIT) {
|
2005-07-12 16:16:42 +00:00
|
|
|
pos = __add_character_literal(
|
|
|
|
parser, parser->token.name.buffer[0]);
|
2005-07-11 13:41:59 +00:00
|
|
|
if (pos == -1) return -1;
|
|
|
|
EMIT_PUSH_LITERAL_CONSTANT (parser, pos);
|
2005-06-23 04:55:44 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
}
|
|
|
|
else if (parser->token.type == XP_STX_TOKEN_STRLIT) {
|
2005-07-12 16:16:42 +00:00
|
|
|
pos = __add_string_literal (parser,
|
2005-07-11 13:41:59 +00:00
|
|
|
parser->token.name.buffer, parser->token.name.size);
|
|
|
|
if (pos == -1) return -1;
|
|
|
|
EMIT_PUSH_LITERAL_CONSTANT (parser, pos);
|
2005-06-23 04:55:44 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
}
|
|
|
|
else if (parser->token.type == XP_STX_TOKEN_NUMLIT) {
|
2005-07-12 16:16:42 +00:00
|
|
|
/* TODO: other types of numbers, negative numbers, etc */
|
|
|
|
xp_word_t tmp;
|
|
|
|
XP_STRTOI (tmp, parser->token.name.buffer, XP_NULL, 10);
|
|
|
|
literal = XP_STX_TO_SMALLINT(tmp);
|
|
|
|
pos = __add_literal(parser, literal);
|
|
|
|
if (pos == -1) return -1;
|
|
|
|
EMIT_PUSH_LITERAL_CONSTANT (parser, pos);
|
2005-06-23 04:55:44 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
}
|
|
|
|
else if (parser->token.type == XP_STX_TOKEN_SYMLIT) {
|
2005-09-11 17:01:56 +00:00
|
|
|
pos = __add_symbol_literal (parser,
|
|
|
|
parser->token.name.buffer, parser->token.name.size);
|
|
|
|
if (pos == -1) return -1;
|
|
|
|
EMIT_PUSH_LITERAL_CONSTANT (parser, pos);
|
2005-06-23 04:55:44 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
}
|
|
|
|
else if (parser->token.type == XP_STX_TOKEN_LBRACKET) {
|
|
|
|
GET_TOKEN (parser);
|
2005-06-29 10:56:42 +00:00
|
|
|
if (__parse_block_constructor(parser) == -1) return -1;
|
2005-06-23 04:55:44 +00:00
|
|
|
}
|
|
|
|
else if (parser->token.type == XP_STX_TOKEN_APAREN) {
|
|
|
|
/* TODO: array literal */
|
|
|
|
}
|
|
|
|
else if (parser->token.type == XP_STX_TOKEN_LPAREN) {
|
|
|
|
GET_TOKEN (parser);
|
2005-06-29 10:56:42 +00:00
|
|
|
if (__parse_expression(parser) == -1) return -1;
|
2005-06-23 04:55:44 +00:00
|
|
|
if (parser->token.type != XP_STX_TOKEN_RPAREN) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_NO_RPAREN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_PRIMARY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2005-08-11 16:16:04 +00:00
|
|
|
/*if (__parse_primary_ident(parser, parser->token.name.buffer) == -1) return -1;*/
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_primary_ident(parser, ident, is_super) == -1) return -1;
|
2005-06-23 04:55:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
static int __parse_primary_ident (
|
|
|
|
xp_stx_parser_t* parser, const xp_char_t* ident, xp_bool_t* is_super)
|
2005-07-08 11:32:50 +00:00
|
|
|
{
|
|
|
|
xp_word_t i;
|
2005-07-10 09:21:46 +00:00
|
|
|
xp_stx_t* stx = parser->stx;
|
2005-07-08 11:32:50 +00:00
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
*is_super = xp_false;
|
|
|
|
|
|
|
|
if (xp_strcmp(ident, XP_TEXT("self")) == 0) {
|
|
|
|
EMIT_CODE (parser, PUSH_RECEIVER);
|
|
|
|
return 0;
|
2005-08-11 11:18:30 +00:00
|
|
|
}
|
2005-09-30 12:19:00 +00:00
|
|
|
else if (xp_strcmp(ident, XP_TEXT("super")) == 0) {
|
|
|
|
*is_super = xp_true;
|
|
|
|
EMIT_CODE (parser, PUSH_RECEIVER);
|
|
|
|
return 0;
|
2005-08-11 11:18:30 +00:00
|
|
|
}
|
2005-09-30 12:19:00 +00:00
|
|
|
else if (xp_strcmp(ident, XP_TEXT("nil")) == 0) {
|
2005-08-11 16:16:04 +00:00
|
|
|
EMIT_CODE (parser, PUSH_NIL);
|
|
|
|
return 0;
|
2005-08-11 11:18:30 +00:00
|
|
|
}
|
2005-08-11 16:16:04 +00:00
|
|
|
else if (xp_strcmp(ident, XP_TEXT("true")) == 0) {
|
|
|
|
EMIT_CODE (parser, PUSH_TRUE);
|
|
|
|
return 0;
|
2005-08-11 11:18:30 +00:00
|
|
|
}
|
2005-08-11 16:16:04 +00:00
|
|
|
else if (xp_strcmp(ident, XP_TEXT("false")) == 0) {
|
|
|
|
EMIT_CODE (parser, PUSH_FALSE);
|
|
|
|
return 0;
|
2005-08-11 11:18:30 +00:00
|
|
|
}
|
|
|
|
|
2005-07-08 11:32:50 +00:00
|
|
|
/* Refer to __parse_assignment for identifier lookup */
|
|
|
|
|
|
|
|
for (i = 0; i < parser->temporary_count; i++) {
|
2005-07-10 09:21:46 +00:00
|
|
|
if (xp_strcmp(ident, parser->temporaries[i]) == 0) {
|
|
|
|
EMIT_PUSH_TEMPORARY_LOCATION (parser, i);
|
2005-07-08 11:32:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xp_stx_get_instance_variable_index (
|
2005-07-10 09:21:46 +00:00
|
|
|
stx, parser->method_class, ident, &i) == 0) {
|
|
|
|
EMIT_PUSH_RECEIVER_VARIABLE (parser, i);
|
2005-07-08 11:32:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-10 09:21:46 +00:00
|
|
|
/* TODO: what is the best way to look up a class variable? */
|
|
|
|
/* 1. Use the class containing it and using its position */
|
|
|
|
/* 2. Use a primitive method after pushing the name as a symbol */
|
|
|
|
/* 3. Implement a vm instruction to do it */
|
|
|
|
/*
|
2005-07-08 11:32:50 +00:00
|
|
|
if (xp_stx_lookup_class_variable (
|
2005-07-10 09:21:46 +00:00
|
|
|
stx, parser->method_class, ident) != stx->nil) {
|
|
|
|
//EMIT_LOOKUP_CLASS_VARIABLE (parser, ident);
|
2005-07-08 11:32:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-07-10 09:21:46 +00:00
|
|
|
*/
|
2005-07-08 11:32:50 +00:00
|
|
|
|
|
|
|
/* TODO: IMPLEMENT POOL DICTIONARIES */
|
|
|
|
|
|
|
|
/* TODO: IMPLEMENT GLOBLAS, but i don't like this idea */
|
|
|
|
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_UNDECLARED_NAME;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-23 04:59:00 +00:00
|
|
|
static int __parse_block_constructor (xp_stx_parser_t* parser)
|
2005-06-23 04:55:44 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* <block constructor> ::= '[' <block body> ']'
|
|
|
|
* <block body> ::= [<block argument>* '|']
|
|
|
|
* [<temporaries>] [<statements>]
|
|
|
|
* <block argument> ::= ':' identifier
|
|
|
|
*/
|
|
|
|
|
2005-06-23 07:20:44 +00:00
|
|
|
if (parser->token.type == XP_STX_TOKEN_COLON) {
|
|
|
|
do {
|
2005-06-29 10:56:42 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
|
2005-06-23 07:20:44 +00:00
|
|
|
if (parser->token.type != XP_STX_TOKEN_IDENT) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_BLOCK_ARGUMENT_NAME;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO : store block arguments */
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
} while (parser->token.type == XP_STX_TOKEN_COLON);
|
|
|
|
|
|
|
|
if (!__is_vbar_token(&parser->token)) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_BLOCK_ARGUMENT_LIST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: create a block closure */
|
2005-06-29 10:56:42 +00:00
|
|
|
if (__parse_temporaries(parser) == -1) return -1;
|
|
|
|
if (__parse_block_statements(parser) == -1) return -1;
|
2005-06-23 07:20:44 +00:00
|
|
|
|
|
|
|
if (parser->token.type != XP_STX_TOKEN_RBRACKET) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_BLOCK_NOT_CLOSED;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (parser);
|
|
|
|
|
|
|
|
/* TODO: do special treatment for block closures */
|
|
|
|
|
2005-06-23 04:55:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
static int __parse_message_continuation (
|
|
|
|
xp_stx_parser_t* parser, xp_bool_t is_super)
|
2005-06-19 16:16:33 +00:00
|
|
|
{
|
2005-06-22 15:02:41 +00:00
|
|
|
/*
|
|
|
|
* <messages> ::=
|
|
|
|
* (<unary message>+ <binary message>* [<keyword message>] ) |
|
|
|
|
* (<binary message>+ [<keyword message>] ) |
|
|
|
|
* <keyword message>
|
|
|
|
* <cascaded messages> ::= (';' <messages>)*
|
|
|
|
*/
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_keyword_message(parser, is_super) == -1) return -1;
|
2005-06-19 16:16:33 +00:00
|
|
|
|
|
|
|
while (parser->token.type == XP_STX_TOKEN_SEMICOLON) {
|
2005-07-05 11:38:01 +00:00
|
|
|
EMIT_CODE_TEST (parser, XP_TEXT("DoSpecial(DUP_RECEIVER(CASCADE))"), XP_TEXT(""));
|
2005-06-19 16:16:33 +00:00
|
|
|
GET_TOKEN (parser);
|
|
|
|
|
2005-10-01 16:48:44 +00:00
|
|
|
if (__parse_keyword_message(parser, xp_false) == -1) return -1;
|
2005-07-05 11:38:01 +00:00
|
|
|
EMIT_CODE_TEST (parser, XP_TEXT("DoSpecial(POP_TOP)"), XP_TEXT(""));
|
2005-06-19 16:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
static int __parse_keyword_message (xp_stx_parser_t* parser, xp_bool_t is_super)
|
2005-06-15 16:07:14 +00:00
|
|
|
{
|
2005-06-23 04:55:44 +00:00
|
|
|
/*
|
|
|
|
* <keyword message> ::= (keyword <keyword argument> )+
|
|
|
|
* <keyword argument> ::= <primary> <unary message>* <binary message>*
|
|
|
|
*/
|
|
|
|
|
|
|
|
xp_stx_name_t name;
|
2005-09-13 07:07:24 +00:00
|
|
|
xp_word_t pos;
|
2005-09-30 12:19:00 +00:00
|
|
|
xp_bool_t is_super2;
|
|
|
|
int nargs = 0, n;
|
2005-06-23 04:55:44 +00:00
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_binary_message (parser, is_super) == -1) return -1;
|
2005-07-10 16:50:50 +00:00
|
|
|
if (parser->token.type != XP_STX_TOKEN_KEYWORD) return 0;
|
2005-06-23 04:55:44 +00:00
|
|
|
|
|
|
|
if (xp_stx_name_open(&name, 0) == XP_NULL) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-07-10 16:50:50 +00:00
|
|
|
do {
|
2005-06-23 04:55:44 +00:00
|
|
|
if (xp_stx_name_adds(&name, parser->token.name.buffer) == -1) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
xp_stx_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (parser);
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_primary(parser, XP_NULL, &is_super2) == -1) {
|
2005-06-23 04:55:44 +00:00
|
|
|
xp_stx_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_binary_message(parser, is_super2) == -1) {
|
2005-06-23 04:55:44 +00:00
|
|
|
xp_stx_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-07-10 16:50:50 +00:00
|
|
|
nargs++;
|
2005-09-13 07:07:24 +00:00
|
|
|
/* TODO: check if it has too many arguments.. */
|
2005-07-10 16:50:50 +00:00
|
|
|
} while (parser->token.type == XP_STX_TOKEN_KEYWORD);
|
|
|
|
|
2005-09-13 07:07:24 +00:00
|
|
|
pos = __add_symbol_literal (parser, name.buffer, name.size);
|
|
|
|
if (pos == -1) {
|
|
|
|
xp_stx_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-09-30 12:19:00 +00:00
|
|
|
|
|
|
|
n = (is_super)?
|
|
|
|
__emit_send_to_super(parser,nargs,pos):
|
|
|
|
__emit_send_to_self(parser,nargs,pos);
|
|
|
|
if (n == -1) {
|
2005-09-13 07:07:24 +00:00
|
|
|
xp_stx_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-07-10 16:50:50 +00:00
|
|
|
|
2005-06-23 04:55:44 +00:00
|
|
|
xp_stx_name_close (&name);
|
2005-06-22 15:02:41 +00:00
|
|
|
return 0;
|
2005-06-15 16:07:14 +00:00
|
|
|
}
|
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
static int __parse_binary_message (xp_stx_parser_t* parser, xp_bool_t is_super)
|
2005-06-15 16:07:14 +00:00
|
|
|
{
|
2005-06-22 15:02:41 +00:00
|
|
|
/*
|
|
|
|
* <binary message> ::= binarySelector <binary argument>
|
|
|
|
* <binary argument> ::= <primary> <unary message>*
|
|
|
|
*/
|
2005-09-13 07:07:24 +00:00
|
|
|
xp_word_t pos;
|
2005-09-30 12:19:00 +00:00
|
|
|
xp_bool_t is_super2;
|
|
|
|
int n;
|
2005-06-22 15:02:41 +00:00
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_unary_message (parser, is_super) == -1) return -1;
|
2005-06-22 15:02:41 +00:00
|
|
|
|
|
|
|
while (parser->token.type == XP_STX_TOKEN_BINARY) {
|
2005-06-23 04:55:44 +00:00
|
|
|
xp_char_t* op = xp_stx_token_yield (&parser->token, 0);
|
|
|
|
if (op == XP_NULL) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-22 15:02:41 +00:00
|
|
|
GET_TOKEN (parser);
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_primary(parser, XP_NULL, &is_super2) == -1) {
|
2005-06-23 04:55:44 +00:00
|
|
|
xp_free (op);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
if (__parse_unary_message(parser, is_super2) == -1) {
|
2005-06-23 04:55:44 +00:00
|
|
|
xp_free (op);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-09-13 07:07:24 +00:00
|
|
|
pos = __add_symbol_literal (parser, op, xp_strlen(op));
|
|
|
|
if (pos == -1) {
|
|
|
|
xp_free (op);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-09-30 12:19:00 +00:00
|
|
|
|
|
|
|
n = (is_super)?
|
|
|
|
__emit_send_to_super(parser,2,pos):
|
|
|
|
__emit_send_to_self(parser,2,pos);
|
|
|
|
if (n == -1) {
|
2005-09-13 07:07:24 +00:00
|
|
|
xp_free (op);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-07-10 16:50:50 +00:00
|
|
|
|
2005-06-23 04:55:44 +00:00
|
|
|
xp_free (op);
|
2005-06-22 15:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-06-15 16:07:14 +00:00
|
|
|
}
|
|
|
|
|
2005-09-30 12:19:00 +00:00
|
|
|
static int __parse_unary_message (xp_stx_parser_t* parser, xp_bool_t is_super)
|
2005-06-19 16:16:33 +00:00
|
|
|
{
|
2005-06-22 15:02:41 +00:00
|
|
|
/* <unary message> ::= unarySelector */
|
2005-06-19 16:16:33 +00:00
|
|
|
|
2005-09-11 17:01:56 +00:00
|
|
|
xp_word_t pos;
|
2005-09-30 12:19:00 +00:00
|
|
|
int n;
|
2005-09-11 17:01:56 +00:00
|
|
|
|
2005-06-22 15:02:41 +00:00
|
|
|
while (parser->token.type == XP_STX_TOKEN_IDENT) {
|
2005-09-11 17:01:56 +00:00
|
|
|
pos = __add_symbol_literal (parser,
|
|
|
|
parser->token.name.buffer, parser->token.name.size);
|
|
|
|
if (pos == -1) return -1;
|
2005-09-30 12:19:00 +00:00
|
|
|
|
|
|
|
n = (is_super)?
|
|
|
|
__emit_send_to_super(parser,0,pos):
|
|
|
|
__emit_send_to_self(parser,0,pos);
|
|
|
|
if (n == -1) return -1;
|
|
|
|
|
2005-06-22 15:02:41 +00:00
|
|
|
GET_TOKEN (parser);
|
2005-06-19 16:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
static int __get_token (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
xp_cint_t c;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (__skip_spaces(parser) == -1) return -1;
|
2005-06-08 16:05:41 +00:00
|
|
|
if (parser->curc == XP_CHAR('"')) {
|
2005-06-05 05:27:02 +00:00
|
|
|
GET_CHAR (parser);
|
|
|
|
if (__skip_comment(parser) == -1) return -1;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
c = parser->curc;
|
2005-06-05 16:44:05 +00:00
|
|
|
xp_stx_token_clear (&parser->token);
|
2005-06-05 05:27:02 +00:00
|
|
|
|
2005-06-08 16:05:41 +00:00
|
|
|
if (c == XP_CHAR_EOF) {
|
2005-06-06 16:04:18 +00:00
|
|
|
parser->token.type = XP_STX_TOKEN_END;
|
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
else if (xp_isalpha(c)) {
|
2005-06-05 16:44:05 +00:00
|
|
|
if (__get_ident(parser) == -1) return -1;
|
2005-06-05 05:27:02 +00:00
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
else if (xp_isdigit(c)) {
|
2005-06-07 16:09:58 +00:00
|
|
|
if (__get_numlit(parser, xp_false) == -1) return -1;
|
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
else if (c == XP_CHAR('$')) {
|
2005-06-06 16:47:10 +00:00
|
|
|
GET_CHAR (parser);
|
2005-06-05 16:44:05 +00:00
|
|
|
if (__get_charlit(parser) == -1) return -1;
|
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
else if (c == XP_CHAR('\'')) {
|
2005-06-06 16:47:10 +00:00
|
|
|
GET_CHAR (parser);
|
2005-06-05 16:44:05 +00:00
|
|
|
if (__get_strlit(parser) == -1) return -1;
|
2005-06-05 05:27:02 +00:00
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
else if (c == XP_CHAR(':')) {
|
2005-06-07 16:09:58 +00:00
|
|
|
parser->token.type = XP_STX_TOKEN_COLON;
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
|
|
|
|
c = parser->curc;
|
2005-06-08 16:05:41 +00:00
|
|
|
if (c == XP_CHAR('=')) {
|
2005-06-07 16:09:58 +00:00
|
|
|
parser->token.type = XP_STX_TOKEN_ASSIGN;
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
else if (c == XP_CHAR('^')) {
|
2005-06-06 16:04:18 +00:00
|
|
|
parser->token.type = XP_STX_TOKEN_RETURN;
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
else if (c == XP_CHAR('[')) {
|
2005-06-07 16:09:58 +00:00
|
|
|
parser->token.type = XP_STX_TOKEN_LBRACKET;
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
else if (c == XP_CHAR(']')) {
|
2005-06-07 16:09:58 +00:00
|
|
|
parser->token.type = XP_STX_TOKEN_RBRACKET;
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
2005-06-12 12:33:31 +00:00
|
|
|
else if (c == XP_CHAR('(')) {
|
|
|
|
parser->token.type = XP_STX_TOKEN_LPAREN;
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
|
|
|
else if (c == XP_CHAR(')')) {
|
|
|
|
parser->token.type = XP_STX_TOKEN_RPAREN;
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
2005-06-23 04:55:44 +00:00
|
|
|
else if (c == XP_CHAR('#')) {
|
|
|
|
/*ADD_TOKEN_CHAR(parser, c);*/
|
|
|
|
GET_CHAR (parser);
|
|
|
|
|
|
|
|
c = parser->curc;
|
|
|
|
if (c == XP_CHAR_EOF) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_LITERAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (c == XP_CHAR('(')) {
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
parser->token.type = XP_STX_TOKEN_APAREN;
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
|
|
|
else if (c == XP_CHAR('\'')) {
|
|
|
|
GET_CHAR (parser);
|
|
|
|
if (__get_strlit(parser) == -1) return -1;
|
|
|
|
parser->token.type = XP_STX_TOKEN_SYMLIT;
|
|
|
|
}
|
|
|
|
else if (!__is_closing_char(c) && !xp_isspace(c)) {
|
|
|
|
do {
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
c = parser->curc;
|
|
|
|
} while (!__is_closing_char(c) && !xp_isspace(c));
|
|
|
|
|
|
|
|
parser->token.type = XP_STX_TOKEN_SYMLIT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_LITERAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
else if (c == XP_CHAR('.')) {
|
2005-06-07 16:09:58 +00:00
|
|
|
parser->token.type = XP_STX_TOKEN_PERIOD;
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
2005-06-19 16:16:33 +00:00
|
|
|
else if (c == XP_CHAR(';')) {
|
|
|
|
parser->token.type = XP_STX_TOKEN_SEMICOLON;
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
2005-06-08 15:49:35 +00:00
|
|
|
else if (__is_binary_char(c)) {
|
|
|
|
if (__get_binary(parser) == -1) return -1;
|
|
|
|
}
|
2005-06-05 05:27:02 +00:00
|
|
|
else {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_CHAR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-19 16:16:33 +00:00
|
|
|
//xp_printf (XP_TEXT("TOKEN: %s\n"), parser->token.name.buffer);
|
2005-06-05 05:27:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-05 16:44:05 +00:00
|
|
|
static int __get_ident (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* identifier ::= letter (letter | digit)*
|
|
|
|
* keyword ::= identifier ':'
|
|
|
|
*/
|
|
|
|
|
|
|
|
xp_cint_t c = parser->curc;
|
|
|
|
parser->token.type = XP_STX_TOKEN_IDENT;
|
|
|
|
|
|
|
|
do {
|
2005-06-06 16:04:18 +00:00
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
2005-06-05 16:44:05 +00:00
|
|
|
GET_CHAR (parser);
|
|
|
|
c = parser->curc;
|
2005-06-08 16:05:41 +00:00
|
|
|
} while (xp_isalnum(c));
|
2005-06-05 16:44:05 +00:00
|
|
|
|
2005-06-08 16:05:41 +00:00
|
|
|
if (c == XP_CHAR(':')) {
|
2005-06-23 04:55:44 +00:00
|
|
|
ADD_TOKEN_CHAR (parser, c);
|
2005-06-05 16:44:05 +00:00
|
|
|
parser->token.type = XP_STX_TOKEN_KEYWORD;
|
|
|
|
GET_CHAR (parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-07 16:09:58 +00:00
|
|
|
static int __get_numlit (xp_stx_parser_t* parser, xp_bool_t negated)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* <number literal> ::= ['-'] <number>
|
|
|
|
* <number> ::= integer | float | scaledDecimal
|
|
|
|
* integer ::= decimalInteger | radixInteger
|
|
|
|
* decimalInteger ::= digits
|
|
|
|
* digits ::= digit+
|
|
|
|
* radixInteger ::= radixSpecifier 'r' radixDigits
|
|
|
|
* radixSpecifier := digits
|
|
|
|
* radixDigits ::= (digit | uppercaseAlphabetic)+
|
|
|
|
* float ::= mantissa [exponentLetter exponent]
|
|
|
|
* mantissa ::= digits'.' digits
|
|
|
|
* exponent ::= ['-']decimalInteger
|
|
|
|
* exponentLetter ::= 'e' | 'd' | 'q'
|
|
|
|
* scaledDecimal ::= scaledMantissa 's' [fractionalDigits]
|
|
|
|
* scaledMantissa ::= decimalInteger | mantissa
|
|
|
|
* fractionalDigits ::= decimalInteger
|
|
|
|
*/
|
|
|
|
|
|
|
|
xp_cint_t c = parser->curc;
|
|
|
|
parser->token.type = XP_STX_TOKEN_NUMLIT;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
c = parser->curc;
|
2005-06-08 16:05:41 +00:00
|
|
|
} while (xp_isalnum(c));
|
2005-06-07 16:09:58 +00:00
|
|
|
|
|
|
|
/* TODO; more */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-05 16:44:05 +00:00
|
|
|
static int __get_charlit (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* character_literal ::= '$' character
|
|
|
|
* character ::= "Any character in the implementation-defined character set"
|
|
|
|
*/
|
|
|
|
|
2005-06-06 16:47:10 +00:00
|
|
|
xp_cint_t c = parser->curc; /* even a new-line or white space would be taken */
|
2005-06-08 16:05:41 +00:00
|
|
|
if (c == XP_CHAR_EOF) {
|
2005-06-05 16:44:05 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_CHARLIT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
parser->token.type = XP_STX_TOKEN_CHARLIT;
|
2005-06-06 16:04:18 +00:00
|
|
|
ADD_TOKEN_CHAR(parser, c);
|
2005-06-05 16:44:05 +00:00
|
|
|
GET_CHAR (parser);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __get_strlit (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* string_literal ::= stringDelimiter stringBody stringDelimiter
|
|
|
|
* stringBody ::= (nonStringDelimiter | (stringDelimiter stringDelimiter)*)
|
|
|
|
* stringDelimiter ::= ''' "a single quote"
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO: C-like string */
|
|
|
|
|
|
|
|
xp_cint_t c = parser->curc;
|
|
|
|
parser->token.type = XP_STX_TOKEN_STRLIT;
|
|
|
|
|
|
|
|
do {
|
|
|
|
do {
|
2005-06-06 16:04:18 +00:00
|
|
|
ADD_TOKEN_CHAR (parser, c);
|
2005-06-05 16:44:05 +00:00
|
|
|
GET_CHAR (parser);
|
|
|
|
c = parser->curc;
|
|
|
|
|
2005-06-08 16:05:41 +00:00
|
|
|
if (c == XP_CHAR_EOF) {
|
2005-06-05 16:44:05 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_STRLIT;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-06-08 16:05:41 +00:00
|
|
|
} while (c != XP_CHAR('\''));
|
2005-06-05 16:44:05 +00:00
|
|
|
|
|
|
|
GET_CHAR (parser);
|
|
|
|
c = parser->curc;
|
2005-06-08 16:05:41 +00:00
|
|
|
} while (c == XP_CHAR('\''));
|
2005-06-05 16:44:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-08 15:49:35 +00:00
|
|
|
static int __get_binary (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* binarySelector ::= binaryCharacter+
|
|
|
|
*/
|
|
|
|
|
|
|
|
xp_cint_t c = parser->curc;
|
|
|
|
ADD_TOKEN_CHAR (parser, c);
|
|
|
|
|
2005-06-11 18:01:25 +00:00
|
|
|
if (c == XP_CHAR('-')) {
|
2005-06-08 15:49:35 +00:00
|
|
|
GET_CHAR (parser);
|
|
|
|
c = parser->curc;
|
2005-06-08 16:05:41 +00:00
|
|
|
if (xp_isdigit(c)) return __get_numlit(parser,xp_true);
|
2005-06-08 15:49:35 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
GET_CHAR (parser);
|
|
|
|
c = parser->curc;
|
|
|
|
}
|
|
|
|
|
2005-06-11 18:01:25 +00:00
|
|
|
/* up to 2 characters only */
|
2005-06-08 15:49:35 +00:00
|
|
|
if (__is_binary_char(c)) {
|
|
|
|
ADD_TOKEN_CHAR (parser, c);
|
|
|
|
GET_CHAR (parser);
|
2005-06-11 18:01:25 +00:00
|
|
|
c = parser->curc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* or up to any occurrences */
|
|
|
|
/*
|
|
|
|
while (__is_binary_char(c)) {
|
|
|
|
ADD_TOKEN_CHAR (parser, c);
|
|
|
|
GET_CHAR (parser);
|
|
|
|
c = parser->curc;
|
2005-06-08 15:49:35 +00:00
|
|
|
}
|
2005-06-11 18:01:25 +00:00
|
|
|
*/
|
2005-06-08 15:49:35 +00:00
|
|
|
|
|
|
|
parser->token.type = XP_STX_TOKEN_BINARY;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
static int __skip_spaces (xp_stx_parser_t* parser)
|
2005-05-30 15:27:31 +00:00
|
|
|
{
|
2005-06-08 16:05:41 +00:00
|
|
|
while (xp_isspace(parser->curc)) GET_CHAR (parser);
|
2005-06-05 05:27:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-04 16:27:30 +00:00
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
static int __skip_comment (xp_stx_parser_t* parser)
|
|
|
|
{
|
2005-06-08 16:05:41 +00:00
|
|
|
while (parser->curc != XP_CHAR('"')) GET_CHAR (parser);
|
2005-06-05 05:27:02 +00:00
|
|
|
GET_CHAR (parser);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-05-30 15:27:31 +00:00
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
static int __get_char (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
xp_cint_t c;
|
2005-06-05 16:44:05 +00:00
|
|
|
|
|
|
|
if (parser->ungotc_count > 0) {
|
|
|
|
parser->curc = parser->ungotc[parser->ungotc_count--];
|
|
|
|
}
|
|
|
|
else {
|
2005-06-06 03:47:56 +00:00
|
|
|
if (parser->input_func (
|
|
|
|
XP_STX_PARSER_INPUT_CONSUME,
|
|
|
|
parser->input_owner, (void*)&c) == -1) {
|
2005-06-05 16:44:05 +00:00
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_INPUT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
parser->curc = c;
|
2005-06-05 05:27:02 +00:00
|
|
|
}
|
2005-05-30 15:27:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-04 07:01:57 +00:00
|
|
|
|
2005-06-05 05:27:02 +00:00
|
|
|
static int __unget_char (xp_stx_parser_t* parser, xp_cint_t c)
|
|
|
|
{
|
|
|
|
if (parser->ungotc_count >= xp_countof(parser->ungotc)) return -1;
|
|
|
|
parser->ungotc[parser->ungotc_count++] = c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-06 03:47:56 +00:00
|
|
|
static int __open_input (xp_stx_parser_t* parser, void* input)
|
2005-06-05 05:27:02 +00:00
|
|
|
{
|
2005-06-06 03:47:56 +00:00
|
|
|
if (parser->input_func(
|
|
|
|
XP_STX_PARSER_INPUT_OPEN,
|
|
|
|
(void*)&parser->input_owner, input) == -1) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_INPUT;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-06-05 05:27:02 +00:00
|
|
|
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_NONE;
|
2005-06-08 16:05:41 +00:00
|
|
|
parser->curc = XP_CHAR_EOF;
|
2005-06-05 05:27:02 +00:00
|
|
|
parser->ungotc_count = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-06 03:47:56 +00:00
|
|
|
static int __close_input (xp_stx_parser_t* parser)
|
|
|
|
{
|
|
|
|
if (parser->input_func(
|
|
|
|
XP_STX_PARSER_INPUT_CLOSE,
|
|
|
|
parser->input_owner, XP_NULL) == -1) {
|
|
|
|
parser->error_code = XP_STX_PARSER_ERROR_INPUT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-08 11:32:50 +00:00
|
|
|
|