Added a CppParser class

This commit is contained in:
Pierre Pronchery 2009-07-28 11:22:32 +00:00
parent 021ddc8633
commit 5ec29c6031
8 changed files with 1130 additions and 978 deletions

View File

@ -25,10 +25,12 @@ dist:
$(PACKAGE)-$(VERSION)/include/Makefile \
$(PACKAGE)-$(VERSION)/include/project.conf \
$(PACKAGE)-$(VERSION)/src/cpp.c \
$(PACKAGE)-$(VERSION)/src/parser.c \
$(PACKAGE)-$(VERSION)/src/scanner.c \
$(PACKAGE)-$(VERSION)/src/main.c \
$(PACKAGE)-$(VERSION)/src/Makefile \
$(PACKAGE)-$(VERSION)/src/common.h \
$(PACKAGE)-$(VERSION)/src/parser.h \
$(PACKAGE)-$(VERSION)/src/project.conf \
$(PACKAGE)-$(VERSION)/Makefile \
$(PACKAGE)-$(VERSION)/COPYING \

View File

@ -19,7 +19,7 @@ INSTALL = install
all: $(TARGETS)
libcpp_OBJS = cpp.o scanner.o
libcpp_OBJS = cpp.o parser.o scanner.o
libcpp_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) -fPIC
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
$(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
$(CC) $(libcpp_CFLAGS) -c scanner.c

View File

@ -18,6 +18,7 @@
#ifndef _CPP_COMMON_H
# define _CPP_COMMON_H
# include "parser.h"
# include "cpp.h"
@ -35,27 +36,10 @@ typedef enum _CppScope
CPP_SCOPE_TAKEN
} CppScope;
/* FIXME make a subtype for the actual parser instead of the "toplevel" hack */
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 */
Cpp * toplevel;
Cpp * subparser;
CppParser * parser;
char ** paths;
size_t paths_cnt;
/* for substitutions */
@ -66,4 +50,8 @@ struct _Cpp
size_t scopes_cnt;
};
/* functions */
char * cpp_path_lookup(Cpp * cpp, char const * filename);
#endif /* !_CPP_COMMON_H */

989
src/cpp.c

File diff suppressed because it is too large Load Diff

1016
src/parser.c Normal file

File diff suppressed because it is too large Load Diff

41
src/parser.h Normal file
View 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 */

View File

@ -2,11 +2,11 @@ targets=libcpp,cpp
cppflags=-I ../include
cflags_force=-W
cflags=-Wall -g -O2
dist=Makefile,common.h
dist=Makefile,common.h,parser.h
[libcpp]
type=library
sources=cpp.c,scanner.c
sources=cpp.c,parser.c,scanner.c
cflags=-fPIC
ldflags=-L $(LIBDIR) -Wl,-rpath $(LIBDIR) -l System
install=$(LIBDIR)
@ -14,6 +14,9 @@ install=$(LIBDIR)
[cpp.c]
depends=common.h,../include/cpp.h
[parser.c]
depends=parser.h,../include/cpp.h
[scanner.c]
depends=common.h,../include/cpp.h

View File

@ -52,7 +52,6 @@ static int _cpp_scope_push(Cpp * cpp, CppScope scope)
{
CppScope * p;
cpp = cpp->toplevel;
if(_cpp_scope_get(cpp) != CPP_SCOPE_TAKING)
scope = CPP_SCOPE_TAKEN;
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 */
static CppScope _cpp_scope_get(Cpp * cpp)
{
cpp = cpp->toplevel;
return (cpp->scopes_cnt == 0) ? CPP_SCOPE_TAKING
: cpp->scopes[cpp->scopes_cnt - 1];
}
@ -76,14 +74,13 @@ static CppScope _cpp_scope_get(Cpp * cpp)
/* cpp_scope_get_count */
static size_t _cpp_scope_get_count(Cpp * cpp)
{
return cpp->toplevel->scopes_cnt;
return cpp->scopes_cnt;
}
/* cpp_scope_set */
static void _cpp_scope_set(Cpp * cpp, CppScope scope)
{
cpp = cpp->toplevel;
assert(cpp->scopes_cnt > 0);
cpp->scopes[cpp->scopes_cnt - 1] = scope;
}
@ -94,7 +91,6 @@ static int _cpp_scope_pop(Cpp * cpp)
{
CppScope * p;
cpp = cpp->toplevel;
assert(cpp->scopes_cnt > 0);
if(cpp->scopes_cnt == 1)
{
@ -112,7 +108,6 @@ static int _cpp_scope_pop(Cpp * cpp)
/* public */
/* cpp_scan */
static int _scan_get_next(Cpp * cpp, Token ** token);
static int _scan_ifdef(Cpp * cpp, Token ** token);
static int _scan_ifndef(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;
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 */
break;
@ -163,20 +159,6 @@ int cpp_scan(Cpp * cpp, Token ** token)
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)
{
char * name;