summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@colm.net>2019-12-27 11:52:41 +0200
committerAdrian Thurston <thurston@colm.net>2019-12-27 11:52:41 +0200
commitba0569b4a80459e7d05f63115c89073eb9637537 (patch)
tree73738236d6d9554a92b45ca2ff8434a438b47475
parent7595234119adcda6e060fef31b3424589bf634df (diff)
downloadcolm-ba0569b4a80459e7d05f63115c89073eb9637537.tar.gz
Created a common superclass for the export classes
Every class representing Colm types has a program and tree pointer and some common functions. Moving this out allows us to pass the common base to the repeat iterator and eliminate the first argument to the iterator.
-rw-r--r--colm/colmex.h36
-rw-r--r--colm/exports.cc33
-rw-r--r--colm/loadfinal.cc38
-rw-r--r--colm/loadinit.cc2
-rw-r--r--test/rlparse.d/load.cc24
-rw-r--r--test/rlparse.d/reducer.cc2
6 files changed, 70 insertions, 65 deletions
diff --git a/colm/colmex.h b/colm/colmex.h
index 5786b0a7..2abc7b2f 100644
--- a/colm/colmex.h
+++ b/colm/colmex.h
@@ -8,13 +8,45 @@
#include <string>
#include <vector>
+inline void appendString( colm_print_args *args, const char *data, int length )
+{
+ std::string *str = (std::string*)args->arg;
+ *str += std::string( data, length );
+}
+
+inline std::string printTreeStr( colm_program *prg, colm_tree *tree, bool trim )
+{
+ std::string str;
+ struct indent_impl indent = { -1, 0 };
+ colm_print_args printArgs = { &str, 1, 0, trim, &indent, &appendString,
+ &colm_print_null, &colm_print_term_tree, &colm_print_null };
+ colm_print_tree_args( prg, colm_vm_root(prg), &printArgs, tree );
+ return str;
+}
+
+struct ExportTree
+{
+ ExportTree( colm_program *prg, colm_tree *tree )
+ : __prg(prg), __tree(tree) {}
+
+ std::string text() { return printTreeStr( __prg, __tree, true ); }
+ colm_location *loc() { return colm_find_location( __prg, __tree ); }
+ std::string text_notrim() { return printTreeStr( __prg, __tree, false ); }
+ std::string text_ws() { return printTreeStr( __prg, __tree, false ); }
+ colm_data *data() { return __tree->tokdata; }
+ operator colm_tree *() { return __tree; }
+
+ colm_program *__prg;
+ colm_tree *__tree;
+};
+
/* Non-recursive tree iterator. Runs an in-order traversal and when it finds a
* search target it yields it and then resumes searching the next child. It
* does not go into what it finds. This iterator can be used to search lists,
* regardless if they are left-recursive or right-recursive. */
-template <class RootType, class SearchType> struct RepeatIter
+template <class SearchType> struct RepeatIter
{
- RepeatIter( const RootType &root )
+ RepeatIter( const ExportTree &root )
:
prg(root.__prg),
search_id(SearchType::ID)
diff --git a/colm/exports.cc b/colm/exports.cc
index cc040d57..58482ad1 100644
--- a/colm/exports.cc
+++ b/colm/exports.cc
@@ -64,26 +64,6 @@ void Compiler::generateExports()
"#include <string>\n"
"\n";
- out <<
- "inline void appendString( colm_print_args *args, const char *data, int length )\n"
- "{\n"
- " std::string *str = (std::string*)args->arg;\n"
- " *str += std::string( data, length );\n"
- "}\n"
- "\n";
-
- out <<
- "inline std::string printTreeStr( colm_program *prg, colm_tree *tree, bool trim )\n"
- "{\n"
- " std::string str;\n"
- " struct indent_impl indent = { -1, 0 };\n"
- " colm_print_args printArgs = { &str, 1, 0, trim, &indent, &appendString, \n"
- " &colm_print_null, &colm_print_term_tree, &colm_print_null };\n"
- " colm_print_tree_args( prg, colm_vm_root(prg), &printArgs, tree );\n"
- " return str;\n"
- "}\n"
- "\n";
-
/* Declare. */
for ( LelList::Iter lel = langEls; lel.lte(); lel++ ) {
if ( lel->isEOF )
@@ -102,25 +82,18 @@ void Compiler::generateExports()
openNameSpace( out, lel->nspace );
out << "struct " << lel->fullName << "\n";
+ out << " : public ExportTree\n";
out << "{\n";
out << " static const int ID = " << lel->id << ";\n";
- out << " std::string text() { return printTreeStr( __prg, __tree, true ); }\n";
- out << " colm_location *loc() { return colm_find_location( __prg, __tree ); }\n";
- out << " std::string text_notrim() { return printTreeStr( __prg, __tree, false ); }\n";
- out << " std::string text_ws() { return printTreeStr( __prg, __tree, false ); }\n";
- out << " colm_data *data() { return __tree->tokdata; }\n";
- out << " operator colm_tree *() { return __tree; }\n";
- out << " colm_program *__prg;\n";
- out << " colm_tree *__tree;\n";
if ( mainReturnUT != 0 && mainReturnUT->langEl == lel ) {
out << " " << lel->fullName <<
- "( colm_program *prg ) : __prg(prg), __tree(returnVal(prg)) {\n";
+ "( colm_program *prg ) : ExportTree( prg, returnVal(prg) ) {\n";
out << " }\n";
}
out << " " << lel->fullName <<
- "( colm_program *prg, colm_tree *tree ) : __prg(prg), __tree(tree) {\n";
+ "( colm_program *prg, colm_tree *tree ) : ExportTree( prg, tree ) {\n";
out << "}\n";
diff --git a/colm/loadfinal.cc b/colm/loadfinal.cc
index 01101973..a6496f15 100644
--- a/colm/loadfinal.cc
+++ b/colm/loadfinal.cc
@@ -314,7 +314,7 @@ struct LoadColm
StmtList *retList = new StmtList;
/* Walk the list of statements. */
- RepeatIter<_repeat_statement, statement> ri( langStmtList.StmtList() );
+ RepeatIter<statement> ri( langStmtList.StmtList() );
while ( !ri.end() ) {
statement Statement = ri.value();
@@ -385,7 +385,7 @@ struct LoadColm
ObjectDef *objectDef = ObjectDef::cons( ObjectDef::UserType,
String(), pd->nextObjectId++ );
- RepeatIter<_repeat_var_def, var_def> varDefIter( varDefList );
+ RepeatIter<var_def> varDefIter( varDefList );
while ( !varDefIter.end() ) {
ObjectField *varDef = walkVarDef( varDefIter.value(),
@@ -534,7 +534,7 @@ struct LoadColm
{
PatternItemList *list = new PatternItemList;
- RepeatIter<_repeat_sq_cons_data, sq_cons_data> sqConsDataIter( sqConsDataList );
+ RepeatIter<sq_cons_data> sqConsDataIter( sqConsDataList );
while ( !sqConsDataIter.end() ) {
String consData = unescape( sqConsDataIter.value().text().c_str() );
@@ -561,7 +561,7 @@ struct LoadColm
{
ConsItemList *list = new ConsItemList;
- RepeatIter<_repeat_sq_cons_data, sq_cons_data> sqConsDataIter( sqConsDataList );
+ RepeatIter<sq_cons_data> sqConsDataIter( sqConsDataList );
while ( !sqConsDataIter.end() ) {
String consData = unescape( sqConsDataIter.value().text().c_str() );
@@ -589,7 +589,7 @@ struct LoadColm
{
PatternItemList *list = new PatternItemList;
- RepeatIter<_repeat_litpat_el, litpat_el> litpatElIter( litpatElList );
+ RepeatIter<litpat_el> litpatElIter( litpatElList );
while ( !litpatElIter.end() ) {
PatternItemList *tail = walkLitpatEl( litpatElIter.value(), patternVarRef );
@@ -613,7 +613,7 @@ struct LoadColm
{
PatternItemList *list = new PatternItemList;
- RepeatIter<_repeat_pattern_el, pattern_el> patternElIter( patternElList );
+ RepeatIter<pattern_el> patternElIter( patternElList );
while ( !patternElIter.end() ) {
PatternItemList *tail = walkPatternEl( patternElIter.value(), patternVarRef );
@@ -783,7 +783,7 @@ struct LoadColm
String lit = "";
_repeat_sq_cons_data sqConsDataList = Include.SqConsDataList();
- RepeatIter<_repeat_sq_cons_data, sq_cons_data> sqConsDataIter( sqConsDataList );
+ RepeatIter<sq_cons_data> sqConsDataIter( sqConsDataList );
while ( !sqConsDataIter.end() ) {
colm_data *data = sqConsDataIter.value().data();
@@ -1465,7 +1465,7 @@ struct LoadColm
{
ConsItemList *list = new ConsItemList;
- RepeatIter<_repeat_lit_cons_el, lit_cons_el> litConsElIter( litConsElList );
+ RepeatIter<lit_cons_el> litConsElIter( litConsElList );
while ( !litConsElIter.end() ) {
ConsItemList *extension = walkLitConsEl( litConsElIter.value(), consTypeRef );
@@ -1526,7 +1526,7 @@ struct LoadColm
{
ConsItemList *list = new ConsItemList;
- RepeatIter<_repeat_cons_el, cons_el> consElIter( consElList );
+ RepeatIter<cons_el> consElIter( consElList );
while ( !consElIter.end() ) {
ConsItemList *extension = walkConsEl( consElIter.value(), consTypeRef );
@@ -1601,7 +1601,7 @@ struct LoadColm
{
ConsItemList *list = new ConsItemList;
- RepeatIter<_repeat_lit_string_el, lit_string_el> litStringElIter( litStringElList );
+ RepeatIter<lit_string_el> litStringElIter( litStringElList );
while ( !litStringElIter.end() ) {
ConsItemList *extension = walkLitStringEl( litStringElIter.value() );
@@ -1657,7 +1657,7 @@ struct LoadColm
{
ConsItemList *list = new ConsItemList;
- RepeatIter<_repeat_string_el, string_el> stringElIter( stringElList );
+ RepeatIter<string_el> stringElIter( stringElList );
while ( !stringElIter.end() ) {
ConsItemList *extension = walkStringEl( stringElIter.value() );
@@ -1733,7 +1733,7 @@ struct LoadColm
{
ConsItemList *list = new ConsItemList;
- RepeatIter<_repeat_lit_accum_el, lit_accum_el> litAccumElIter( litAccumElList );
+ RepeatIter<lit_accum_el> litAccumElIter( litAccumElList );
while ( !litAccumElIter.end() ) {
ConsItemList *extension = walkLitAccumEl( litAccumElIter.value() );
@@ -1789,7 +1789,7 @@ struct LoadColm
{
ConsItemList *list = new ConsItemList;
- RepeatIter<_repeat_accum_el, accum_el> accumElIter( accumElList );
+ RepeatIter<accum_el> accumElIter( accumElList );
while ( !accumElIter.end() ) {
ConsItemList *extension = walkAccumEl( accumElIter.value() );
@@ -1857,7 +1857,7 @@ struct LoadColm
{
FieldInitVect *list = new FieldInitVect;
- RepeatIter<_repeat_field_init, field_init> fieldInitIter( fieldInitList );
+ RepeatIter<field_init> fieldInitIter( fieldInitList );
while ( !fieldInitIter.end() ) {
walkFieldInit( list, fieldInitIter.value() );
@@ -2517,7 +2517,7 @@ struct LoadColm
_repeat_struct_item structItemList = structDef.ItemList();
- RepeatIter<_repeat_struct_item, struct_item> structItemIter( structItemList );
+ RepeatIter<struct_item> structItemIter( structItemList );
while ( !structItemIter.end() ) {
walkStructItem( structItemIter.value() );
@@ -2608,7 +2608,7 @@ struct LoadColm
void walkRedItemList( _repeat_host_item itemList, ReduceTextItemList &list )
{
- RepeatIter<_repeat_host_item, host_item> itemIter( itemList );
+ RepeatIter<host_item> itemIter( itemList );
while ( !itemIter.end() ) {
walkRedItem( itemIter.value(), list );
@@ -2659,7 +2659,7 @@ struct LoadColm
void walkReductionList( _repeat_reduction_item itemList )
{
- RepeatIter<_repeat_reduction_item, reduction_item> itemIter( itemList );
+ RepeatIter<reduction_item> itemIter( itemList );
while ( !itemIter.end() ) {
walkReductionItem( itemIter.value() );
@@ -2855,7 +2855,7 @@ struct LoadColm
void walkNamespaceItemList( _repeat_namespace_item itemList, StmtList *stmtList )
{
/* Walk the list of items. */
- RepeatIter<_repeat_namespace_item, namespace_item> itemIter( itemList );
+ RepeatIter<namespace_item> itemIter( itemList );
while ( !itemIter.end() ) {
walkNamespaceItem( itemIter.value(), stmtList );
itemIter.next();
@@ -2867,7 +2867,7 @@ struct LoadColm
StmtList *stmtList = new StmtList;
/* Walk the list of items. */
- RepeatIter<_repeat_root_item, root_item> rootItemIter( rootItemList );
+ RepeatIter<root_item> rootItemIter( rootItemList );
while ( !rootItemIter.end() ) {
walkRootItem( rootItemIter.value(), stmtList );
rootItemIter.next();
diff --git a/colm/loadinit.cc b/colm/loadinit.cc
index 027a1018..65e7e062 100644
--- a/colm/loadinit.cc
+++ b/colm/loadinit.cc
@@ -394,7 +394,7 @@ void LoadInit::go( long activeRealm )
/* Walk the list of items. */
_repeat_item ItemList = Start.ItemList();
- RepeatIter<_repeat_item, item> itemIter( ItemList );
+ RepeatIter<item> itemIter( ItemList );
while ( !itemIter.end() ) {
item Item = itemIter.value();
diff --git a/test/rlparse.d/load.cc b/test/rlparse.d/load.cc
index 3d6e297b..234c3f7a 100644
--- a/test/rlparse.d/load.cc
+++ b/test/rlparse.d/load.cc
@@ -319,7 +319,7 @@ struct LoadRagel
{
ruby_inline::_repeat_block_item BlockItemList = RubyInlineBlock._repeat_block_item();
- RepeatIter<ruby_inline::_repeat_block_item, ruby_inline::block_item> BlockItemIter( BlockItemList );
+ RepeatIter<ruby_inline::block_item> BlockItemIter( BlockItemList );
while ( !BlockItemIter.end() ) {
loadBlockItem( inlineList, BlockItemIter.value() );
BlockItemIter.next();
@@ -364,7 +364,7 @@ struct LoadRagel
{
InlineList *inlineList = new InlineList;
ruby_inline::_repeat_expr_item ExprItemList = InlineExpr._repeat_expr_item();
- RepeatIter<ruby_inline::_repeat_expr_item, ruby_inline::expr_item> ExprItemIter( ExprItemList );
+ RepeatIter<ruby_inline::expr_item> ExprItemIter( ExprItemList );
while ( !ExprItemIter.end() ) {
InlineItem *inlineItem = loadExprItem( ExprItemIter.value() );
inlineList->append( inlineItem );
@@ -533,7 +533,7 @@ struct LoadRagel
{
ocaml_inline::_repeat_block_item BlockItemList = OCamlInlineBlock._repeat_block_item();
- RepeatIter<ocaml_inline::_repeat_block_item, ocaml_inline::block_item> BlockItemIter( BlockItemList );
+ RepeatIter<ocaml_inline::block_item> BlockItemIter( BlockItemList );
while ( !BlockItemIter.end() ) {
loadBlockItem( inlineList, BlockItemIter.value() );
BlockItemIter.next();
@@ -579,7 +579,7 @@ struct LoadRagel
InlineList *inlineList = new InlineList;
ocaml_inline::_repeat_expr_item ExprItemList = InlineExpr._repeat_expr_item();
- RepeatIter<ocaml_inline::_repeat_expr_item, ocaml_inline::expr_item> ExprItemIter( ExprItemList );
+ RepeatIter<ocaml_inline::expr_item> ExprItemIter( ExprItemList );
while ( !ExprItemIter.end() ) {
InlineItem *inlineItem = loadExprItem( ExprItemIter.value() );
inlineList->append( inlineItem );
@@ -748,7 +748,7 @@ struct LoadRagel
{
crack_inline::_repeat_block_item BlockItemList = CrackInlineBlock._repeat_block_item();
- RepeatIter<crack_inline::_repeat_block_item, crack_inline::block_item> BlockItemIter( BlockItemList );
+ RepeatIter<crack_inline::block_item> BlockItemIter( BlockItemList );
while ( !BlockItemIter.end() ) {
loadBlockItem( inlineList, BlockItemIter.value() );
BlockItemIter.next();
@@ -794,7 +794,7 @@ struct LoadRagel
InlineList *inlineList = new InlineList;
crack_inline::_repeat_expr_item ExprItemList = InlineExpr._repeat_expr_item();
- RepeatIter<crack_inline::_repeat_expr_item, crack_inline::expr_item> ExprItemIter( ExprItemList );
+ RepeatIter<crack_inline::expr_item> ExprItemIter( ExprItemList );
while ( !ExprItemIter.end() ) {
InlineItem *inlineItem = loadExprItem( ExprItemIter.value() );
inlineList->append( inlineItem );
@@ -2298,7 +2298,7 @@ struct LoadRagel
inputItem->writeArgs.push_back( Cmd.text() );
- RepeatIter<ragel::_repeat_write_arg, ragel::write_arg> WordIter( WordList );
+ RepeatIter<ragel::write_arg> WordIter( WordList );
while ( !WordIter.end() ) {
inputItem->writeArgs.push_back( WordIter.value().text() );
WordIter.next();
@@ -2472,7 +2472,7 @@ struct LoadRagel
void loadImportList( _repeat_import ImportList )
{
- RepeatIter<_repeat_import, import> ImportIter( ImportList );
+ RepeatIter<import> ImportIter( ImportList );
while ( !ImportIter.end() ) {
loadImport( ImportIter.value() );
ImportIter.next();
@@ -2602,7 +2602,7 @@ struct LoadRagel
ragel::_repeat_statement StmtList = RagelStart._repeat_statement();
- RepeatIter<ragel::_repeat_statement, ragel::statement> StmtIter( StmtList );
+ RepeatIter<ragel::statement> StmtIter( StmtList );
while ( !StmtIter.end() ) {
bool stop = loadStatement( StmtIter.value() );
if ( stop )
@@ -2706,7 +2706,7 @@ struct LoadRagel
c_host::_repeat_section SectionList = Start.SectionList();
if ( SectionList ) {
- RepeatIter<c_host::_repeat_section, c_host::section> SectionIter( SectionList );
+ RepeatIter<c_host::section> SectionIter( SectionList );
while ( !SectionIter.end() ) {
loadSection( SectionIter.value(), targetMachine, searchMachine );
SectionIter.next();
@@ -2715,7 +2715,7 @@ struct LoadRagel
else if ( Start.RSectionList() ) {
ruby_host::_repeat_section RSectionList = Start.RSectionList();
if ( RSectionList ) {
- RepeatIter<ruby_host::_repeat_section, ruby_host::section> RSectionIter( RSectionList );
+ RepeatIter<ruby_host::section> RSectionIter( RSectionList );
while ( !RSectionIter.end() ) {
loadSection( RSectionIter.value(), targetMachine, searchMachine );
RSectionIter.next();
@@ -2725,7 +2725,7 @@ struct LoadRagel
else {
ocaml_host::_repeat_section OSectionList = Start.OSectionList();
if ( OSectionList ) {
- RepeatIter<ocaml_host::_repeat_section, ocaml_host::section> OSectionIter( OSectionList );
+ RepeatIter<ocaml_host::section> OSectionIter( OSectionList );
while ( !OSectionIter.end() ) {
loadSection( OSectionIter.value(), targetMachine, searchMachine );
OSectionIter.next();
diff --git a/test/rlparse.d/reducer.cc b/test/rlparse.d/reducer.cc
index b3bd32ba..21bee96e 100644
--- a/test/rlparse.d/reducer.cc
+++ b/test/rlparse.d/reducer.cc
@@ -248,7 +248,7 @@ void TopLevel::loadImport( std::string fileName )
return;
}
- RepeatIter<_repeat_import, import> ImportIter( ImportList );
+ RepeatIter<import> ImportIter( ImportList );
while ( !ImportIter.end() ) {
import Import = ImportIter.value();