Extend the API to offer parsing strings
This commit is contained in:
parent
1a7ca5af5c
commit
0837e19285
|
@ -126,6 +126,7 @@ typedef enum _CppCode
|
||||||
|
|
||||||
/* functions */
|
/* functions */
|
||||||
Cpp * cpp_new(CppPrefs * prefs);
|
Cpp * cpp_new(CppPrefs * prefs);
|
||||||
|
Cpp * cpp_new_string(CppPrefs * prefs, char const * string);
|
||||||
void cpp_delete(Cpp * cpp);
|
void cpp_delete(Cpp * cpp);
|
||||||
|
|
||||||
/* accessors */
|
/* accessors */
|
||||||
|
|
37
src/cpp.c
37
src/cpp.c
|
@ -72,6 +72,43 @@ Cpp * cpp_new(CppPrefs * prefs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* cpp_new_string */
|
||||||
|
Cpp * cpp_new_string(CppPrefs * prefs, char const * string)
|
||||||
|
{
|
||||||
|
/* FIXME really implement (and refactor) */
|
||||||
|
Cpp * cpp;
|
||||||
|
String * p;
|
||||||
|
int r = 0;
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
fprintf(stderr, "DEBUG: %s()\n", __func__);
|
||||||
|
#endif
|
||||||
|
if((cpp = object_new(sizeof(*cpp))) == NULL)
|
||||||
|
return NULL;
|
||||||
|
memset(cpp, 0, sizeof(*cpp));
|
||||||
|
cpp->options = prefs->options;
|
||||||
|
cpp->parser = cppparser_new_string(cpp, NULL, string, prefs->filters);
|
||||||
|
if((p = string_new(prefs->filename)) != NULL)
|
||||||
|
{
|
||||||
|
r |= cpp_path_add(cpp, dirname(p)); /* FIXME inclusion order */
|
||||||
|
string_delete(p);
|
||||||
|
}
|
||||||
|
if((p = getcwd(NULL, 0)) != NULL)
|
||||||
|
{
|
||||||
|
r |= cpp_path_add(cpp, p);
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
error_set("%s%s", "getcwd: ", strerror(errno));
|
||||||
|
if(r != 0 || cpp->parser == NULL || cpp->paths_cnt != 2)
|
||||||
|
{
|
||||||
|
cpp_delete(cpp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return cpp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* cpp_delete */
|
/* cpp_delete */
|
||||||
void cpp_delete(Cpp * cpp)
|
void cpp_delete(Cpp * cpp)
|
||||||
{
|
{
|
||||||
|
|
56
src/parser.c
56
src/parser.c
|
@ -901,6 +901,62 @@ CppParser * cppparser_new(Cpp * cpp, CppParser * parent, char const * filename,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* cppparser_new_string */
|
||||||
|
CppParser * cppparser_new_string(Cpp * cpp, CppParser * parent,
|
||||||
|
char const * string, int filters)
|
||||||
|
{
|
||||||
|
/* FIXME refactor */
|
||||||
|
CppParser * cp;
|
||||||
|
|
||||||
|
if((cp = object_new(sizeof(*cp))) == NULL)
|
||||||
|
return NULL;
|
||||||
|
cp->cpp = cpp;
|
||||||
|
cp->parent = parent;
|
||||||
|
cp->parser = parser_new_string(string, strlen(string));
|
||||||
|
cp->filters = filters;
|
||||||
|
cp->inject = NULL;
|
||||||
|
cp->inject_first = 0;
|
||||||
|
cp->newlines_last = 0;
|
||||||
|
cp->newlines_last_cnt = 0;
|
||||||
|
cp->trigraphs_last = 0;
|
||||||
|
cp->trigraphs_last_cnt = 0;
|
||||||
|
cp->directive_newline = 1;
|
||||||
|
cp->directive_control = 0;
|
||||||
|
cp->queue_ready = 0;
|
||||||
|
cp->queue_code = CPP_CODE_NULL;
|
||||||
|
cp->queue_string = NULL;
|
||||||
|
cp->subparser = NULL;
|
||||||
|
if(cp->parser == NULL)
|
||||||
|
{
|
||||||
|
cppparser_delete(cp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
parser_add_filter(cp->parser, _cpp_filter_inject, cp);
|
||||||
|
parser_add_filter(cp->parser, _cpp_filter_newlines, cp);
|
||||||
|
if(cp->filters & CPP_FILTER_TRIGRAPH)
|
||||||
|
parser_add_filter(cp->parser, _cpp_filter_trigraphs, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_inject, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_dequeue, cp);
|
||||||
|
if(cp->filters & CPP_FILTER_WHITESPACE)
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_whitespace, cp);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_newline, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_otherspace, cp);
|
||||||
|
}
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_comment, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_header, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_control, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_comma, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_operator, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_quote, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_directive, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_word, cp);
|
||||||
|
parser_add_callback(cp->parser, _cpp_callback_unknown, cp);
|
||||||
|
return cp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* cppparser_delete */
|
/* cppparser_delete */
|
||||||
void cppparser_delete(CppParser * cp)
|
void cppparser_delete(CppParser * cp)
|
||||||
{
|
{
|
||||||
|
|
|
@ -28,6 +28,8 @@ typedef struct _CppParser CppParser;
|
||||||
/* functions */
|
/* functions */
|
||||||
CppParser * cppparser_new(Cpp * cpp, CppParser * parent, char const * filename,
|
CppParser * cppparser_new(Cpp * cpp, CppParser * parent, char const * filename,
|
||||||
int filters);
|
int filters);
|
||||||
|
CppParser * cppparser_new_string(Cpp * cpp, CppParser * parent,
|
||||||
|
char const * string, int filters);
|
||||||
void cppparser_delete(CppParser * cppparser);
|
void cppparser_delete(CppParser * cppparser);
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user