Added a CppParser class
This commit is contained in:
parent
021ddc8633
commit
5ec29c6031
2
Makefile
2
Makefile
|
@ -25,10 +25,12 @@ dist:
|
||||||
$(PACKAGE)-$(VERSION)/include/Makefile \
|
$(PACKAGE)-$(VERSION)/include/Makefile \
|
||||||
$(PACKAGE)-$(VERSION)/include/project.conf \
|
$(PACKAGE)-$(VERSION)/include/project.conf \
|
||||||
$(PACKAGE)-$(VERSION)/src/cpp.c \
|
$(PACKAGE)-$(VERSION)/src/cpp.c \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/parser.c \
|
||||||
$(PACKAGE)-$(VERSION)/src/scanner.c \
|
$(PACKAGE)-$(VERSION)/src/scanner.c \
|
||||||
$(PACKAGE)-$(VERSION)/src/main.c \
|
$(PACKAGE)-$(VERSION)/src/main.c \
|
||||||
$(PACKAGE)-$(VERSION)/src/Makefile \
|
$(PACKAGE)-$(VERSION)/src/Makefile \
|
||||||
$(PACKAGE)-$(VERSION)/src/common.h \
|
$(PACKAGE)-$(VERSION)/src/common.h \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/parser.h \
|
||||||
$(PACKAGE)-$(VERSION)/src/project.conf \
|
$(PACKAGE)-$(VERSION)/src/project.conf \
|
||||||
$(PACKAGE)-$(VERSION)/Makefile \
|
$(PACKAGE)-$(VERSION)/Makefile \
|
||||||
$(PACKAGE)-$(VERSION)/COPYING \
|
$(PACKAGE)-$(VERSION)/COPYING \
|
||||||
|
|
|
@ -19,7 +19,7 @@ INSTALL = install
|
||||||
|
|
||||||
all: $(TARGETS)
|
all: $(TARGETS)
|
||||||
|
|
||||||
libcpp_OBJS = cpp.o scanner.o
|
libcpp_OBJS = cpp.o parser.o scanner.o
|
||||||
libcpp_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) -fPIC
|
libcpp_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) -fPIC
|
||||||
libcpp_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) -L $(LIBDIR) -Wl,-rpath $(LIBDIR) -l System
|
libcpp_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) -L $(LIBDIR) -Wl,-rpath $(LIBDIR) -l System
|
||||||
|
|
||||||
|
@ -40,6 +40,9 @@ cpp: $(cpp_OBJS) libcpp.so
|
||||||
cpp.o: cpp.c common.h ../include/cpp.h
|
cpp.o: cpp.c common.h ../include/cpp.h
|
||||||
$(CC) $(libcpp_CFLAGS) -c cpp.c
|
$(CC) $(libcpp_CFLAGS) -c cpp.c
|
||||||
|
|
||||||
|
parser.o: parser.c parser.h ../include/cpp.h
|
||||||
|
$(CC) $(libcpp_CFLAGS) -c parser.c
|
||||||
|
|
||||||
scanner.o: scanner.c common.h ../include/cpp.h
|
scanner.o: scanner.c common.h ../include/cpp.h
|
||||||
$(CC) $(libcpp_CFLAGS) -c scanner.c
|
$(CC) $(libcpp_CFLAGS) -c scanner.c
|
||||||
|
|
||||||
|
|
24
src/common.h
24
src/common.h
|
@ -18,6 +18,7 @@
|
||||||
#ifndef _CPP_COMMON_H
|
#ifndef _CPP_COMMON_H
|
||||||
# define _CPP_COMMON_H
|
# define _CPP_COMMON_H
|
||||||
|
|
||||||
|
# include "parser.h"
|
||||||
# include "cpp.h"
|
# include "cpp.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,27 +36,10 @@ typedef enum _CppScope
|
||||||
CPP_SCOPE_TAKEN
|
CPP_SCOPE_TAKEN
|
||||||
} CppScope;
|
} CppScope;
|
||||||
|
|
||||||
/* FIXME make a subtype for the actual parser instead of the "toplevel" hack */
|
|
||||||
struct _Cpp
|
struct _Cpp
|
||||||
{
|
{
|
||||||
int filters;
|
|
||||||
Parser * parser;
|
|
||||||
/* for cpp_filter_newlines */
|
|
||||||
int newlines_last;
|
|
||||||
int newlines_last_cnt;
|
|
||||||
/* for cpp_filter_trigraphs */
|
|
||||||
int trigraphs_last;
|
|
||||||
int trigraphs_last_cnt;
|
|
||||||
/* to queue a token */
|
|
||||||
int queue_ready;
|
|
||||||
TokenCode queue_code;
|
|
||||||
String * queue_string;
|
|
||||||
/* for cpp_callback_directive */
|
|
||||||
int directive_newline;
|
|
||||||
int directive_control;
|
|
||||||
/* for include directives */
|
/* for include directives */
|
||||||
Cpp * toplevel;
|
CppParser * parser;
|
||||||
Cpp * subparser;
|
|
||||||
char ** paths;
|
char ** paths;
|
||||||
size_t paths_cnt;
|
size_t paths_cnt;
|
||||||
/* for substitutions */
|
/* for substitutions */
|
||||||
|
@ -66,4 +50,8 @@ struct _Cpp
|
||||||
size_t scopes_cnt;
|
size_t scopes_cnt;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* functions */
|
||||||
|
char * cpp_path_lookup(Cpp * cpp, char const * filename);
|
||||||
|
|
||||||
#endif /* !_CPP_COMMON_H */
|
#endif /* !_CPP_COMMON_H */
|
||||||
|
|
1016
src/parser.c
Normal file
1016
src/parser.c
Normal file
File diff suppressed because it is too large
Load Diff
41
src/parser.h
Normal file
41
src/parser.h
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/* $Id$ */
|
||||||
|
/* Copyright (c) 2009 Pierre Pronchery <khorben@defora.org> */
|
||||||
|
/* This file is part of DeforaOS Devel cpp */
|
||||||
|
/* cpp is not free software; you can redistribute it and/or modify it under the
|
||||||
|
* terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0
|
||||||
|
* Unported as published by the Creative Commons organization.
|
||||||
|
*
|
||||||
|
* cpp 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 Creative Commons Attribution-NonCommercial-
|
||||||
|
* ShareAlike 3.0 Unported license for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the Creative Commons Attribution-
|
||||||
|
* NonCommercial-ShareAlike 3.0 along with cpp; if not, browse to
|
||||||
|
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _CPP_PARSER_H
|
||||||
|
# define _CPP_PARSER_H
|
||||||
|
|
||||||
|
# include "cpp.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* types */
|
||||||
|
typedef struct _CppParser CppParser;
|
||||||
|
|
||||||
|
|
||||||
|
/* functions */
|
||||||
|
CppParser * cppparser_new(Cpp * cpp, CppParser * parent, char const * filename,
|
||||||
|
int filters);
|
||||||
|
void cppparser_delete(CppParser * cppparser);
|
||||||
|
|
||||||
|
|
||||||
|
/* accessors */
|
||||||
|
char const * cppparser_get_filename(CppParser * cppparser);
|
||||||
|
|
||||||
|
|
||||||
|
/* useful */
|
||||||
|
int cppparser_scan(CppParser * cppparser, Token ** token);
|
||||||
|
|
||||||
|
#endif /* !_CPP_PARSER_H */
|
|
@ -2,11 +2,11 @@ targets=libcpp,cpp
|
||||||
cppflags=-I ../include
|
cppflags=-I ../include
|
||||||
cflags_force=-W
|
cflags_force=-W
|
||||||
cflags=-Wall -g -O2
|
cflags=-Wall -g -O2
|
||||||
dist=Makefile,common.h
|
dist=Makefile,common.h,parser.h
|
||||||
|
|
||||||
[libcpp]
|
[libcpp]
|
||||||
type=library
|
type=library
|
||||||
sources=cpp.c,scanner.c
|
sources=cpp.c,parser.c,scanner.c
|
||||||
cflags=-fPIC
|
cflags=-fPIC
|
||||||
ldflags=-L $(LIBDIR) -Wl,-rpath $(LIBDIR) -l System
|
ldflags=-L $(LIBDIR) -Wl,-rpath $(LIBDIR) -l System
|
||||||
install=$(LIBDIR)
|
install=$(LIBDIR)
|
||||||
|
@ -14,6 +14,9 @@ install=$(LIBDIR)
|
||||||
[cpp.c]
|
[cpp.c]
|
||||||
depends=common.h,../include/cpp.h
|
depends=common.h,../include/cpp.h
|
||||||
|
|
||||||
|
[parser.c]
|
||||||
|
depends=parser.h,../include/cpp.h
|
||||||
|
|
||||||
[scanner.c]
|
[scanner.c]
|
||||||
depends=common.h,../include/cpp.h
|
depends=common.h,../include/cpp.h
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,6 @@ static int _cpp_scope_push(Cpp * cpp, CppScope scope)
|
||||||
{
|
{
|
||||||
CppScope * p;
|
CppScope * p;
|
||||||
|
|
||||||
cpp = cpp->toplevel;
|
|
||||||
if(_cpp_scope_get(cpp) != CPP_SCOPE_TAKING)
|
if(_cpp_scope_get(cpp) != CPP_SCOPE_TAKING)
|
||||||
scope = CPP_SCOPE_TAKEN;
|
scope = CPP_SCOPE_TAKEN;
|
||||||
if((p = realloc(cpp->scopes, sizeof(*p) * (cpp->scopes_cnt + 1)))
|
if((p = realloc(cpp->scopes, sizeof(*p) * (cpp->scopes_cnt + 1)))
|
||||||
|
@ -67,7 +66,6 @@ static int _cpp_scope_push(Cpp * cpp, CppScope scope)
|
||||||
/* cpp_scope_get */
|
/* cpp_scope_get */
|
||||||
static CppScope _cpp_scope_get(Cpp * cpp)
|
static CppScope _cpp_scope_get(Cpp * cpp)
|
||||||
{
|
{
|
||||||
cpp = cpp->toplevel;
|
|
||||||
return (cpp->scopes_cnt == 0) ? CPP_SCOPE_TAKING
|
return (cpp->scopes_cnt == 0) ? CPP_SCOPE_TAKING
|
||||||
: cpp->scopes[cpp->scopes_cnt - 1];
|
: cpp->scopes[cpp->scopes_cnt - 1];
|
||||||
}
|
}
|
||||||
|
@ -76,14 +74,13 @@ static CppScope _cpp_scope_get(Cpp * cpp)
|
||||||
/* cpp_scope_get_count */
|
/* cpp_scope_get_count */
|
||||||
static size_t _cpp_scope_get_count(Cpp * cpp)
|
static size_t _cpp_scope_get_count(Cpp * cpp)
|
||||||
{
|
{
|
||||||
return cpp->toplevel->scopes_cnt;
|
return cpp->scopes_cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* cpp_scope_set */
|
/* cpp_scope_set */
|
||||||
static void _cpp_scope_set(Cpp * cpp, CppScope scope)
|
static void _cpp_scope_set(Cpp * cpp, CppScope scope)
|
||||||
{
|
{
|
||||||
cpp = cpp->toplevel;
|
|
||||||
assert(cpp->scopes_cnt > 0);
|
assert(cpp->scopes_cnt > 0);
|
||||||
cpp->scopes[cpp->scopes_cnt - 1] = scope;
|
cpp->scopes[cpp->scopes_cnt - 1] = scope;
|
||||||
}
|
}
|
||||||
|
@ -94,7 +91,6 @@ static int _cpp_scope_pop(Cpp * cpp)
|
||||||
{
|
{
|
||||||
CppScope * p;
|
CppScope * p;
|
||||||
|
|
||||||
cpp = cpp->toplevel;
|
|
||||||
assert(cpp->scopes_cnt > 0);
|
assert(cpp->scopes_cnt > 0);
|
||||||
if(cpp->scopes_cnt == 1)
|
if(cpp->scopes_cnt == 1)
|
||||||
{
|
{
|
||||||
|
@ -112,7 +108,6 @@ static int _cpp_scope_pop(Cpp * cpp)
|
||||||
|
|
||||||
/* public */
|
/* public */
|
||||||
/* cpp_scan */
|
/* cpp_scan */
|
||||||
static int _scan_get_next(Cpp * cpp, Token ** token);
|
|
||||||
static int _scan_ifdef(Cpp * cpp, Token ** token);
|
static int _scan_ifdef(Cpp * cpp, Token ** token);
|
||||||
static int _scan_ifndef(Cpp * cpp, Token ** token);
|
static int _scan_ifndef(Cpp * cpp, Token ** token);
|
||||||
static int _scan_if(Cpp * cpp, Token ** token);
|
static int _scan_if(Cpp * cpp, Token ** token);
|
||||||
|
@ -127,7 +122,8 @@ int cpp_scan(Cpp * cpp, Token ** token)
|
||||||
int ret;
|
int ret;
|
||||||
TokenCode code;
|
TokenCode code;
|
||||||
|
|
||||||
for(; (ret = _scan_get_next(cpp, token)) == 0; token_delete(*token))
|
for(; (ret = cppparser_scan(cpp->parser, token)) == 0;
|
||||||
|
token_delete(*token))
|
||||||
{
|
{
|
||||||
if(*token == NULL) /* end of file */
|
if(*token == NULL) /* end of file */
|
||||||
break;
|
break;
|
||||||
|
@ -163,20 +159,6 @@ int cpp_scan(Cpp * cpp, Token ** token)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _scan_get_next(Cpp * cpp, Token ** token)
|
|
||||||
{
|
|
||||||
if(cpp->subparser != NULL)
|
|
||||||
{
|
|
||||||
if(_scan_get_next(cpp->subparser, token) != 0)
|
|
||||||
return 1;
|
|
||||||
if(*token != NULL)
|
|
||||||
return 0;
|
|
||||||
cpp_delete(cpp->subparser); /* end of file */
|
|
||||||
cpp->subparser = NULL;
|
|
||||||
}
|
|
||||||
return parser_get_token(cpp->parser, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int _scan_ifdef(Cpp * cpp, Token ** token)
|
static int _scan_ifdef(Cpp * cpp, Token ** token)
|
||||||
{
|
{
|
||||||
char * name;
|
char * name;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user