From 369167b630883cd276567d8fc0c99e6a28b10a79 Mon Sep 17 00:00:00 2001 From: Antoine Chavasse Date: Sun, 14 Jun 2009 00:56:38 +0200 Subject: [PATCH] Parser WIP. --- src/idl/parser/{grammar.h => actions_namespace.h} | 44 +++++++++----------- src/idl/parser/grammar.h | 1 + src/idl/parser/grammar_namespace.h | 7 +++- src/idl/parser/parser.cpp | 5 ++- src/idl/parser/{grammar.h => state.h} | 49 ++++++++++------------- 5 files changed, 49 insertions(+), 57 deletions(-) copy src/idl/parser/{grammar.h => actions_namespace.h} (54%) copy src/idl/parser/{grammar.h => state.h} (51%) diff --git a/src/idl/parser/grammar.h b/src/idl/parser/actions_namespace.h similarity index 54% copy from src/idl/parser/grammar.h copy to src/idl/parser/actions_namespace.h index b56fa74..b28d1c6 100644 --- a/src/idl/parser/grammar.h +++ b/src/idl/parser/actions_namespace.h @@ -16,38 +16,30 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifndef FAIL_IDL_PARSER_GRAMMAR_H -#define FAIL_IDL_PARSER_GRAMMAR_H - -#include "pegtl.hh" +#ifndef FAIL_IDL_PARSER_ACTIONS_NAMESPACE_H +#define FAIL_IDL_PARSER_ACTIONS_NAMESPACE_H namespace fail { namespace idlparser { - using namespace pegtl; + struct NewNamespace : action_base< NewNamespace > + { + static void apply( const std::string& str, State& s ) + { + shared_ptr< idlast::Namespace > pNewNamespace( + new idlast::Namespace( str.c_str(), s.CurrentFilename.c_str(), s.CurrentLine ) ); - enum CaptureIndices + s.pCurrentNamespace = s.pCurrentNamespace->addNamespace( pNewNamespace ); +// s.ClassFlags.clear(); + } + }; + + struct EndNamespace : action_base< NewNamespace > { - cap_ClassName, - capture_last + static void apply( const std::string& str, State& s ) + { + s.pCurrentNamespace = s.pCurrentNamespace->getParent(); + } }; }} -#include "grammar_lowlevel.h" -#include "grammar_tokens.h" -#include "grammar_flags.h" -#include "grammar_type.h" -#include "grammar_class.h" -#include "grammar_namespace.h" - -namespace fail { namespace idlparser -{ - // Top level rule - struct Grammar : - seq< - opt< Spacing >, - star< Namespace >, - eof - > {}; -}} - #endif diff --git a/src/idl/parser/grammar.h b/src/idl/parser/grammar.h index b56fa74..a97e065 100644 --- a/src/idl/parser/grammar.h +++ b/src/idl/parser/grammar.h @@ -20,6 +20,7 @@ #define FAIL_IDL_PARSER_GRAMMAR_H #include "pegtl.hh" +#include "state.h" namespace fail { namespace idlparser { diff --git a/src/idl/parser/grammar_namespace.h b/src/idl/parser/grammar_namespace.h index 7e997d5..4815a99 100644 --- a/src/idl/parser/grammar_namespace.h +++ b/src/idl/parser/grammar_namespace.h @@ -19,6 +19,8 @@ #ifndef FAIL_IDL_PARSER_GRAMMAR_NAMESPACE_H #define FAIL_IDL_PARSER_GRAMMAR_NAMESPACE_H +#include "actions_namespace.h" + namespace fail { namespace idlparser { struct NamespaceContent; @@ -28,8 +30,9 @@ namespace fail { namespace idlparser KWnamespace, seq< Spacing, - identifier, - enclose< LeftBrace, NamespaceContent, RightBrace > + ifapply< identifier, NewNamespace >, + enclose< LeftBrace, NamespaceContent, RightBrace >, + apply< EndNamespace > > > {}; diff --git a/src/idl/parser/parser.cpp b/src/idl/parser/parser.cpp index c4b1d2d..10f42d2 100644 --- a/src/idl/parser/parser.cpp +++ b/src/idl/parser/parser.cpp @@ -18,6 +18,7 @@ */ #include "parser.h" #include "grammar.h" +#include "state.h" using namespace fail::idlparser; @@ -27,8 +28,8 @@ shared_ptr< const fail::idlast::AST > fail::idlparser::Parse( const char* pFilen //pegtl::basic_parse_string< Grammar >( " namespace // ffff" ); - map< int, std::string > captures; - pegtl::trace_parse_file< Grammar >( false, pFilename, captures ); + State s( pAST ); + pegtl::trace_parse_file< Grammar >( false, pFilename, s ); //std::cout << "captured class name: " << captures[ cap_ClassName ] << std::endl; diff --git a/src/idl/parser/grammar.h b/src/idl/parser/state.h similarity index 51% copy from src/idl/parser/grammar.h copy to src/idl/parser/state.h index b56fa74..824b643 100644 --- a/src/idl/parser/grammar.h +++ b/src/idl/parser/state.h @@ -16,38 +16,33 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifndef FAIL_IDL_PARSER_GRAMMAR_H -#define FAIL_IDL_PARSER_GRAMMAR_H - -#include "pegtl.hh" +#ifndef FAIL_IDL_PARSER_STATE_H +#define FAIL_IDL_PARSER_STATE_H namespace fail { namespace idlparser { - using namespace pegtl; - - enum CaptureIndices + struct State : pegtl::capture_map { - cap_ClassName, - capture_last + State( shared_ptr< idlast::AST > pAST_ ) : + pAST( pAST_ ), + pCurrentNamespace( pAST_ ) + /*m_bConstPointer( false ), + m_pCurrentFlags( &m_ClassFlags ), + m_bVirtualMethod( false )*/ + { + // TODO: implement actions in the grammar to track this + // using the directives inserted by the preprocessor + // Also wrap eol into a rule that apply an action that increments the line number + CurrentFilename = "filename_nyi"; + CurrentLine = 1; + } + + shared_ptr< idlast::AST > pAST; + shared_ptr< idlast::Namespace > pCurrentNamespace; + + std::string CurrentFilename; + int CurrentLine; }; }} -#include "grammar_lowlevel.h" -#include "grammar_tokens.h" -#include "grammar_flags.h" -#include "grammar_type.h" -#include "grammar_class.h" -#include "grammar_namespace.h" - -namespace fail { namespace idlparser -{ - // Top level rule - struct Grammar : - seq< - opt< Spacing >, - star< Namespace >, - eof - > {}; -}} - #endif -- 2.11.4.GIT