2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2013-10-14 04:21:15 -07:00
|
|
|
#pragma once
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
#include "cf-lexer.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* C-family parser
|
|
|
|
*
|
|
|
|
* Handles preprocessing/lexing/errors when parsing a file, and provides a
|
|
|
|
* set of parsing functions to be able to go through all the resulting tokens
|
|
|
|
* more easily.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PARSE_SUCCESS 0
|
|
|
|
#define PARSE_CONTINUE -1
|
|
|
|
#define PARSE_BREAK -2
|
|
|
|
#define PARSE_UNEXPECTED_CONTINUE -3
|
|
|
|
#define PARSE_UNEXPECTED_BREAK -4
|
|
|
|
#define PARSE_EOF -5
|
|
|
|
|
|
|
|
struct cf_parser {
|
|
|
|
struct cf_lexer lex;
|
|
|
|
struct cf_preprocessor pp;
|
|
|
|
struct error_data error_list;
|
|
|
|
|
|
|
|
struct cf_token *cur_token;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void cf_parser_init(struct cf_parser *parser)
|
|
|
|
{
|
|
|
|
cf_lexer_init(&parser->lex);
|
|
|
|
cf_preprocessor_init(&parser->pp);
|
|
|
|
error_data_init(&parser->error_list);
|
|
|
|
|
|
|
|
parser->cur_token = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void cf_parser_free(struct cf_parser *parser)
|
|
|
|
{
|
|
|
|
cf_lexer_free(&parser->lex);
|
|
|
|
cf_preprocessor_free(&parser->pp);
|
|
|
|
error_data_free(&parser->error_list);
|
|
|
|
|
|
|
|
parser->cur_token = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool cf_parser_parse(struct cf_parser *parser,
|
|
|
|
const char *str, const char *file)
|
|
|
|
{
|
|
|
|
if (!cf_lexer_lex(&parser->lex, str, file))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!cf_preprocess(&parser->pp, &parser->lex, &parser->error_list))
|
|
|
|
return false;
|
|
|
|
|
2014-08-05 13:38:24 -07:00
|
|
|
parser->cur_token = cf_preprocessor_get_tokens(&parser->pp);
|
2013-09-30 19:37:13 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT void cf_adderror(struct cf_parser *parser, const char *error,
|
|
|
|
int level, const char *val1, const char *val2,
|
|
|
|
const char *val3);
|
|
|
|
|
|
|
|
static inline void cf_adderror_expecting(struct cf_parser *p,
|
|
|
|
const char *expected)
|
|
|
|
{
|
2014-03-01 04:54:55 -08:00
|
|
|
cf_adderror(p, "Expected '$1'", LEX_ERROR, expected, NULL, NULL);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void cf_adderror_unexpected_eof(struct cf_parser *p)
|
|
|
|
{
|
2014-03-01 04:54:55 -08:00
|
|
|
cf_adderror(p, "Unexpected EOF", LEX_ERROR,
|
2013-09-30 19:37:13 -07:00
|
|
|
NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void cf_adderror_syntax_error(struct cf_parser *p)
|
|
|
|
{
|
2014-03-01 04:54:55 -08:00
|
|
|
cf_adderror(p, "Syntax error", LEX_ERROR,
|
2013-09-30 19:37:13 -07:00
|
|
|
NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline bool cf_next_token(struct cf_parser *p)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
if (p->cur_token->type != CFTOKEN_SPACETAB &&
|
|
|
|
p->cur_token->type != CFTOKEN_NEWLINE &&
|
|
|
|
p->cur_token->type != CFTOKEN_NONE)
|
|
|
|
p->cur_token++;
|
|
|
|
|
|
|
|
while (p->cur_token->type == CFTOKEN_SPACETAB ||
|
|
|
|
p->cur_token->type == CFTOKEN_NEWLINE)
|
|
|
|
p->cur_token++;
|
|
|
|
|
|
|
|
return p->cur_token->type != CFTOKEN_NONE;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline bool cf_next_valid_token(struct cf_parser *p)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2014-03-01 00:25:41 -08:00
|
|
|
if (!cf_next_token(p)) {
|
2013-09-30 19:37:13 -07:00
|
|
|
cf_adderror_unexpected_eof(p);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
EXPORT bool cf_pass_pair(struct cf_parser *p, char in, char out);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline bool cf_go_to_token(struct cf_parser *p,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *str1, const char *str2)
|
|
|
|
{
|
2014-03-01 00:25:41 -08:00
|
|
|
while (cf_next_token(p)) {
|
2013-09-30 19:37:13 -07:00
|
|
|
if (strref_cmp(&p->cur_token->str, str1) == 0) {
|
|
|
|
return true;
|
|
|
|
} else if (str2 && strref_cmp(&p->cur_token->str, str2) == 0) {
|
|
|
|
return true;
|
|
|
|
} else if (*p->cur_token->str.array == '{') {
|
2014-03-01 00:25:41 -08:00
|
|
|
if (!cf_pass_pair(p, '{', '}'))
|
2013-09-30 19:37:13 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline bool cf_go_to_valid_token(struct cf_parser *p,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *str1, const char *str2)
|
|
|
|
{
|
2014-03-01 00:25:41 -08:00
|
|
|
if (!cf_go_to_token(p, str1, str2)) {
|
2013-09-30 19:37:13 -07:00
|
|
|
cf_adderror_unexpected_eof(p);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline bool cf_go_to_token_type(struct cf_parser *p,
|
2013-09-30 19:37:13 -07:00
|
|
|
enum cf_token_type type)
|
|
|
|
{
|
|
|
|
while (p->cur_token->type != CFTOKEN_NONE &&
|
2013-10-17 17:21:42 -07:00
|
|
|
p->cur_token->type != type)
|
2013-09-30 19:37:13 -07:00
|
|
|
p->cur_token++;
|
|
|
|
|
|
|
|
return p->cur_token->type != CFTOKEN_NONE;
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:54:55 -08:00
|
|
|
static inline int cf_token_should_be(struct cf_parser *p,
|
|
|
|
const char *str, const char *goto1, const char *goto2)
|
|
|
|
{
|
|
|
|
if (strref_cmp(&p->cur_token->str, str) == 0)
|
|
|
|
return PARSE_SUCCESS;
|
|
|
|
|
|
|
|
if (goto1) {
|
|
|
|
if (!cf_go_to_token(p, goto1, goto2))
|
|
|
|
return PARSE_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
cf_adderror_expecting(p, str);
|
|
|
|
return PARSE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline int cf_next_token_should_be(struct cf_parser *p,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *str, const char *goto1, const char *goto2)
|
|
|
|
{
|
2014-03-01 00:25:41 -08:00
|
|
|
if (!cf_next_token(p)) {
|
2013-09-30 19:37:13 -07:00
|
|
|
cf_adderror_unexpected_eof(p);
|
|
|
|
return PARSE_EOF;
|
|
|
|
} else if (strref_cmp(&p->cur_token->str, str) == 0) {
|
|
|
|
return PARSE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:54:55 -08:00
|
|
|
if (goto1) {
|
|
|
|
if (!cf_go_to_token(p, goto1, goto2))
|
|
|
|
return PARSE_EOF;
|
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
cf_adderror_expecting(p, str);
|
|
|
|
return PARSE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline bool cf_peek_token(struct cf_parser *p, struct cf_token *peek)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
struct cf_token *cur_token = p->cur_token;
|
2014-03-01 00:25:41 -08:00
|
|
|
bool success = cf_next_token(p);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
*peek = *p->cur_token;
|
|
|
|
p->cur_token = cur_token;
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline bool cf_peek_valid_token(struct cf_parser *p,
|
2013-09-30 19:37:13 -07:00
|
|
|
struct cf_token *peek)
|
|
|
|
{
|
2014-03-01 00:25:41 -08:00
|
|
|
bool success = cf_peek_token(p, peek);
|
2013-09-30 19:37:13 -07:00
|
|
|
if (!success)
|
|
|
|
cf_adderror_unexpected_eof(p);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline bool cf_token_is(struct cf_parser *p, const char *val)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
return strref_cmp(&p->cur_token->str, val) == 0;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline int cf_token_is_type(struct cf_parser *p,
|
2013-09-30 19:37:13 -07:00
|
|
|
enum cf_token_type type, const char *type_expected,
|
|
|
|
const char *goto_token)
|
|
|
|
{
|
|
|
|
if (p->cur_token->type != type) {
|
|
|
|
cf_adderror_expecting(p, type_expected);
|
|
|
|
if (goto_token) {
|
2014-03-01 00:25:41 -08:00
|
|
|
if (!cf_go_to_valid_token(p, goto_token, NULL))
|
2013-09-30 19:37:13 -07:00
|
|
|
return PARSE_EOF;
|
|
|
|
}
|
|
|
|
return PARSE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PARSE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline void cf_copy_token(struct cf_parser *p, char **dst)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
*dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline int cf_get_name(struct cf_parser *p, char **dst,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, const char *goto_token)
|
|
|
|
{
|
|
|
|
int errcode;
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
|
2013-09-30 19:37:13 -07:00
|
|
|
if (errcode != PARSE_SUCCESS)
|
|
|
|
return errcode;
|
|
|
|
|
|
|
|
*dst = bstrdup_n(p->cur_token->str.array, p->cur_token->str.len);
|
|
|
|
return PARSE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline int cf_next_name(struct cf_parser *p, char **dst,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, const char *goto_token)
|
|
|
|
{
|
2014-03-01 00:25:41 -08:00
|
|
|
if (!cf_next_valid_token(p))
|
2013-09-30 19:37:13 -07:00
|
|
|
return PARSE_EOF;
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
return cf_get_name(p, dst, name, goto_token);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline int cf_get_name_ref(struct cf_parser *p, struct strref *dst,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, const char *goto_token)
|
|
|
|
{
|
|
|
|
int errcode;
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
errcode = cf_token_is_type(p, CFTOKEN_NAME, name, goto_token);
|
2013-09-30 19:37:13 -07:00
|
|
|
if (errcode != PARSE_SUCCESS)
|
|
|
|
return errcode;
|
|
|
|
|
|
|
|
strref_copy(dst, &p->cur_token->str);
|
|
|
|
return PARSE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
static inline int cf_next_name_ref(struct cf_parser *p, struct strref *dst,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, const char *goto_token)
|
|
|
|
{
|
2014-03-01 00:25:41 -08:00
|
|
|
if (!cf_next_valid_token(p))
|
2013-09-30 19:37:13 -07:00
|
|
|
return PARSE_EOF;
|
|
|
|
|
2014-03-01 00:25:41 -08:00
|
|
|
return cf_get_name_ref(p, dst, name, goto_token);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|