diff options
author | Adrian Thurston <thurston@complang.org> | 2012-07-02 09:22:10 -0400 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2012-07-02 09:22:10 -0400 |
commit | 1ec17c55a3bd0085c3ec38c7fbd344d425562660 (patch) | |
tree | 3bf9f1005dd84d149432d0cf1322f4006d515d6f | |
parent | 5bdf8bfcb05dde0f89d291533378d1db38d9b975 (diff) | |
download | colm-1ec17c55a3bd0085c3ec38c7fbd344d425562660.tar.gz |
more static member functions for consing parse tree elements
-rw-r--r-- | src/lmparse.kl | 37 | ||||
-rw-r--r-- | src/parsetree.h | 116 |
2 files changed, 104 insertions, 49 deletions
diff --git a/src/lmparse.kl b/src/lmparse.kl index 8ef15a79..26aad053 100644 --- a/src/lmparse.kl +++ b/src/lmparse.kl @@ -972,13 +972,12 @@ literal_item: opt_no_ignore TK_Literal opt_no_ignore Namespace *nspace = namespaceStack.top(); TokenRegion *region = regionStack.top(); - LiteralDictEl *ldel = nspace->literalDict.find( interp ); if ( ldel != 0 ) error( $2->loc ) << "literal already defined in this namespace" << endp; else { - Join *join = new Join( Expression::cons( new Term( new FactorWithAug( - new FactorWithRep( $2->loc, new FactorWithNeg( $2->loc, new Factor( + Join *join = new Join( Expression::cons( Term::cons( new FactorWithAug( + new FactorWithRep( $2->loc, FactorWithNeg::cons( $2->loc, Factor::cons( new Literal( $2->loc, $2->data, Literal::LitString ) ) ) ) ) ) ) ); @@ -2133,28 +2132,28 @@ nonterm rl_term rl_term: rl_term factor_with_label final { - $$->term = new Term( $1->term, $2->factorWithAug ); + $$->term = Term::cons( $1->term, $2->factorWithAug ); }; rl_term: rl_term '.' factor_with_label final { - $$->term = new Term( $1->term, $3->factorWithAug ); + $$->term = Term::cons( $1->term, $3->factorWithAug ); }; rl_term: rl_term TK_ColonGt factor_with_label final { - $$->term = new Term( $1->term, $3->factorWithAug, Term::RightStartType ); + $$->term = Term::cons( $1->term, $3->factorWithAug, Term::RightStartType ); }; rl_term: rl_term TK_ColonGtGt factor_with_label final { - $$->term = new Term( $1->term, $3->factorWithAug, Term::RightFinishType ); + $$->term = Term::cons( $1->term, $3->factorWithAug, Term::RightFinishType ); }; rl_term: rl_term TK_LtColon factor_with_label final { - $$->term = new Term( $1->term, + $$->term = Term::cons( $1->term, $3->factorWithAug, Term::LeftType ); }; rl_term: factor_with_label final { - $$->term = new Term( $1->factorWithAug ); + $$->term = Term::cons( $1->factorWithAug ); }; nonterm factor_with_label @@ -2303,17 +2302,17 @@ nonterm factor_with_neg factor_with_neg: '!' factor_with_neg final { - $$->factorWithNeg = new FactorWithNeg( $1->loc, + $$->factorWithNeg = FactorWithNeg::cons( $1->loc, $2->factorWithNeg, FactorWithNeg::NegateType ); }; factor_with_neg: '^' factor_with_neg final { - $$->factorWithNeg = new FactorWithNeg( $1->loc, + $$->factorWithNeg = FactorWithNeg::cons( $1->loc, $2->factorWithNeg, FactorWithNeg::CharNegateType ); }; factor_with_neg: rl_factor final { - $$->factorWithNeg = new FactorWithNeg( $1->factor->loc, $1->factor ); + $$->factorWithNeg = FactorWithNeg::cons( $1->factor->loc, $1->factor ); }; nonterm rl_factor @@ -2324,12 +2323,12 @@ nonterm rl_factor rl_factor: TK_Literal final { /* Create a new factor node going to a concat literal. */ - $$->factor = new Factor( new Literal( $1->loc, $1->data, Literal::LitString ) ); + $$->factor = Factor::cons( new Literal( $1->loc, $1->data, Literal::LitString ) ); }; rl_factor: alphabet_num final { /* Create a new factor node going to a literal number. */ - $$->factor = new Factor( new Literal( $1->loc, + $$->factor = Factor::cons( new Literal( $1->loc, $1->data, Literal::Number ) ); }; rl_factor: @@ -2348,7 +2347,7 @@ rl_factor: } else { /* Create a factor node that is a lookup of an expression. */ - $$->factor = new Factor( $1->loc, gdNode->value ); + $$->factor = Factor::cons( $1->loc, gdNode->value ); } break; } @@ -2365,22 +2364,22 @@ rl_factor: rl_factor: TK_SqOpen regular_expr_or_data TK_SqClose final { /* Create a new factor node going to an OR expression. */ - $$->factor = new Factor( new ReItem( $1->loc, $2->reOrBlock, ReItem::OrBlock ) ); + $$->factor = Factor::cons( new ReItem( $1->loc, $2->reOrBlock, ReItem::OrBlock ) ); }; rl_factor: TK_SqOpenNeg regular_expr_or_data TK_SqClose final { /* Create a new factor node going to a negated OR expression. */ - $$->factor = new Factor( new ReItem( $1->loc, $2->reOrBlock, ReItem::NegOrBlock ) ); + $$->factor = Factor::cons( new ReItem( $1->loc, $2->reOrBlock, ReItem::NegOrBlock ) ); }; rl_factor: range_lit TK_DotDot range_lit final { /* Create a new factor node going to a range. */ - $$->factor = new Factor( new Range( $1->literal, $3->literal ) ); + $$->factor = Factor::cons( new Range( $1->literal, $3->literal ) ); }; rl_factor: '(' rl_join ')' final { /* Create a new factor going to a parenthesized join. */ - $$->factor = new Factor( $2->join ); + $$->factor = Factor::cons( $2->join ); }; nonterm range_lit diff --git a/src/parsetree.h b/src/parsetree.h index aa8b7f4c..ee329108 100644 --- a/src/parsetree.h +++ b/src/parsetree.h @@ -876,28 +876,19 @@ struct Term FactorWithAugType }; - Term( Term *term, FactorWithAug *factorWithAug ) : - term(term), factorWithAug(factorWithAug), type(ConcatType) { } - - Term( Term *term, FactorWithAug *factorWithAug, Type type ) : - term(term), factorWithAug(factorWithAug), type(type) { } - - Term( FactorWithAug *factorWithAug ) : - term(0), factorWithAug(factorWithAug), type(FactorWithAugType) { } - Term() : term(0), factorWithAug(0), type((Type)-1) { } - Term *cons( Term *term, FactorWithAug *factorWithAug ) + static Term *cons( Term *term, FactorWithAug *factorWithAug ) { Term *ret = new Term; + ret->type = ConcatType; ret->term = term; ret->factorWithAug = factorWithAug; - ret->type = ConcatType; return ret; } - Term *cons( Term *term, FactorWithAug *factorWithAug, Type type ) + static Term *cons( Term *term, FactorWithAug *factorWithAug, Type type ) { Term *ret = new Term; ret->type = type; @@ -906,12 +897,12 @@ struct Term return ret; } - Term *cons( FactorWithAug *factorWithAug ) + static Term *cons( FactorWithAug *factorWithAug ) { Term *ret = new Term; ret->type = FactorWithAugType; ret->factorWithAug = factorWithAug; - return term; + return ret; } ~Term(); @@ -1004,11 +995,32 @@ struct FactorWithNeg FactorType }; - FactorWithNeg( const InputLoc &loc, FactorWithNeg *factorWithNeg, Type type) : - loc(loc), factorWithNeg(factorWithNeg), factor(0), type(type) { } + FactorWithNeg() + : + factorWithNeg(0), + factor(0), + type((Type)-1) + {} + + static FactorWithNeg *cons( const InputLoc &loc, FactorWithNeg *factorWithNeg, Type type ) + { + FactorWithNeg *f = new FactorWithNeg; + f->type = (type); + f->loc = (loc); + f->factorWithNeg = (factorWithNeg); + f->factor = (0); + return f; + } - FactorWithNeg( const InputLoc &loc, Factor *factor ) : - loc(loc), factorWithNeg(0), factor(factor), type(FactorType) { } + static FactorWithNeg *cons( const InputLoc &loc, Factor *factor ) + { + FactorWithNeg *f = new FactorWithNeg; + f->type = (FactorType); + f->loc = (loc); + f->factorWithNeg = (0); + f->factor = (factor); + return f; + } ~FactorWithNeg(); @@ -1036,30 +1048,74 @@ struct Factor ReferenceType, ParenType, }; + + Factor() + : + literal(0), + range(0), + reItem(0), + regExp(0), + varDef(0), + join(0), + lower(0), + upper(0), + type((Type)-1) + {} /* Construct with a literal fsm. */ - Factor( Literal *literal ) : - literal(literal), type(LiteralType) { } + static Factor *cons( Literal *literal ) + { + Factor *f = new Factor; + f->type = LiteralType; + f->literal = literal; + return f; + } /* Construct with a range. */ - Factor( Range *range ) : - range(range), type(RangeType) { } + static Factor *cons( Range *range ) + { + Factor *f = new Factor; + f->type = RangeType; + f->range = range; + return f; + } /* Construct with the or part of a regular expression. */ - Factor( ReItem *reItem ) : - reItem(reItem), type(OrExprType) { } + static Factor *cons( ReItem *reItem ) + { + Factor *f = new Factor; + f->type = OrExprType; + f->reItem = reItem; + return f; + } /* Construct with a regular expression. */ - Factor( RegExpr *regExp ) : - regExp(regExp), type(RegExprType) { } + static Factor *cons( RegExpr *regExp ) + { + Factor *f = new Factor; + f->type = RegExprType; + f->regExp = regExp; + return f; + } /* Construct with a reference to a var def. */ - Factor( const InputLoc &loc, VarDef *varDef ) : - loc(loc), varDef(varDef), type(ReferenceType) {} + static Factor *cons( const InputLoc &loc, VarDef *varDef ) + { + Factor *f = new Factor; + f->type = ReferenceType; + f->loc = loc; + f->varDef = varDef; + return f; + } /* Construct with a parenthesized join. */ - Factor( Join *join ) : - join(join), type(ParenType) {} + static Factor *cons( Join *join ) + { + Factor *f = new Factor; + f->type = ParenType; + f->join = join; + return f; + } /* Cleanup. */ ~Factor(); |