summaryrefslogtreecommitdiff
path: root/colm
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2012-05-21 12:16:16 -0400
committerAdrian Thurston <thurston@complang.org>2012-05-21 12:16:16 -0400
commitd7360a8e4cdae572b06563eac5d6c21d57621d65 (patch)
tree494873fb15839255dff6e40057557bc593dd9de2 /colm
parent7487fbd6ff688e109a2d64aebaaf34cde28adde7 (diff)
downloadcolm-d7360a8e4cdae572b06563eac5d6c21d57621d65.tar.gz
more clone removal surrounding ignore handling
Diffstat (limited to 'colm')
-rw-r--r--colm/pdarun.c60
-rw-r--r--colm/tree.c88
-rw-r--r--colm/tree.h10
3 files changed, 88 insertions, 70 deletions
diff --git a/colm/pdarun.c b/colm/pdarun.c
index 206d2df4..a806ac5e 100644
--- a/colm/pdarun.c
+++ b/colm/pdarun.c
@@ -587,7 +587,7 @@ static void attachRightIgnore( Program *prg, Tree **sp, PdaRun *pdaRun, ParseTre
Tree *pushTo = parseTree->shadow->tree;
- pushTo = pushRightIgnore2( prg, sp, pushTo, rightIgnore );
+ pushTo = pushRightIgnore( prg, sp, pushTo, rightIgnore );
parseTree->shadow->tree = pushTo;
@@ -596,8 +596,6 @@ static void attachRightIgnore( Program *prg, Tree **sp, PdaRun *pdaRun, ParseTre
}
}
-
-
static void attachLeftIgnore( Program *prg, Tree **sp, PdaRun *pdaRun, ParseTree *parseTree )
{
/* Reset. */
@@ -644,12 +642,11 @@ static void attachLeftIgnore( Program *prg, Tree **sp, PdaRun *pdaRun, ParseTree
Tree *pushTo = parseTree->shadow->tree;
- pushTo = pushLeftIgnore2( prg, sp, pushTo, leftIgnore );
+ pushTo = pushLeftIgnore( prg, sp, pushTo, leftIgnore );
parseTree->shadow->tree = pushTo;
parseTree->flags |= PF_LEFT_IL_ATTACHED;
-
}
}
@@ -660,26 +657,11 @@ static void detachRightIgnore( Program *prg, Tree **sp, PdaRun *pdaRun, ParseTre
* left-ignores. */
Tree *rightIgnore = 0;
if ( parseTree->flags & PF_RIGHT_IL_ATTACHED ) {
- /* Modifying the tree we are detaching from. */
- parseTree->shadow->tree = splitTree( prg, parseTree->shadow->tree );
-
- Kid *riKid = treeRightIgnoreKid( prg, parseTree->shadow->tree );
-
- /* If the right ignore has a left ignore, then that was the original
- * right ignore. */
- Kid *li = treeLeftIgnoreKid( prg, riKid->tree );
- if ( li != 0 ) {
- treeUpref( li->tree );
- popLeftIgnore( prg, sp, riKid->tree );
- rightIgnore = riKid->tree;
- treeUpref( rightIgnore );
- riKid->tree = li->tree;
- }
- else {
- rightIgnore = riKid->tree;
- treeUpref( rightIgnore );
- popRightIgnore( prg, sp, parseTree->shadow->tree );
- }
+ Tree *popFrom = parseTree->shadow->tree;
+
+ popFrom = popRightIgnore( prg, sp, popFrom, &rightIgnore );
+
+ parseTree->shadow->tree = popFrom;
parseTree->flags &= ~PF_RIGHT_IL_ATTACHED;
}
@@ -726,27 +708,13 @@ static void detachLeftIgnore( Program *prg, Tree **sp, PdaRun *pdaRun, FsmRun *f
/* Detach left. */
Tree *leftIgnore = 0;
if ( parseTree->flags & PF_LEFT_IL_ATTACHED ) {
- /* Modifying, make the write safe. */
- parseTree->shadow->tree = splitTree( prg, parseTree->shadow->tree );
-
- Kid *liKid = treeLeftIgnoreKid( prg, parseTree->shadow->tree );
-
- /* If the left ignore has a right ignore, then that was the original
- * left ignore. */
- Kid *ri = treeRightIgnoreKid( prg, liKid->tree );
- if ( ri != 0 ) {
- treeUpref( ri->tree );
- popRightIgnore( prg, sp, liKid->tree );
- leftIgnore = liKid->tree;
- treeUpref( leftIgnore );
- liKid->tree = ri->tree;
- }
- else {
- leftIgnore = liKid->tree;
- treeUpref( leftIgnore );
- popLeftIgnore( prg, sp, parseTree->shadow->tree );
- parseTree->flags &= ~PF_LEFT_IL_ATTACHED;
- }
+ Tree *popFrom = parseTree->shadow->tree;
+
+ popFrom = popLeftIgnore( prg, sp, popFrom, &leftIgnore );
+
+ parseTree->shadow->tree = popFrom;
+
+ parseTree->flags &= ~PF_LEFT_IL_ATTACHED;
}
if ( parseTree->leftIgnore != 0 ) {
diff --git a/colm/tree.c b/colm/tree.c
index 4e93be31..b6d8b90f 100644
--- a/colm/tree.c
+++ b/colm/tree.c
@@ -366,7 +366,7 @@ static Kid *constructRightIgnoreList( Program *prg, long pat )
return constructIgnoreList( prg, nodes[pat].rightIgnore );
}
-static void pushLeftIgnore( Program *prg, Tree *tree, Tree *ignoreList )
+static void insLeftIgnore( Program *prg, Tree *tree, Tree *ignoreList )
{
assert( ! (tree->flags & AF_LEFT_IGNORE) );
@@ -382,7 +382,7 @@ static void pushLeftIgnore( Program *prg, Tree *tree, Tree *ignoreList )
tree->flags |= AF_LEFT_IGNORE;
}
-static void pushRightIgnore( Program *prg, Tree *tree, Tree *ignoreList )
+static void insRightIgnore( Program *prg, Tree *tree, Tree *ignoreList )
{
assert( ! (tree->flags & AF_RIGHT_IGNORE) );
@@ -404,7 +404,7 @@ static void pushRightIgnore( Program *prg, Tree *tree, Tree *ignoreList )
tree->flags |= AF_RIGHT_IGNORE;
}
-Tree *pushRightIgnore2( Program *prg, Tree **sp, Tree *pushTo, Tree *rightIgnore )
+Tree *pushRightIgnore( Program *prg, Tree **sp, Tree *pushTo, Tree *rightIgnore )
{
/* About to alter the data tree. Split first. */
pushTo = splitTree( prg, pushTo );
@@ -413,7 +413,7 @@ Tree *pushRightIgnore2( Program *prg, Tree **sp, Tree *pushTo, Tree *rightIgnore
/* The previous token already has a right ignore. Merge by
* attaching it as a left ignore of the new list. */
Kid *curIgnore = treeRightIgnoreKid( prg, pushTo );
- pushLeftIgnore( prg, rightIgnore, curIgnore->tree );
+ insLeftIgnore( prg, rightIgnore, curIgnore->tree );
/* Replace the current ignore. */
treeDownref( prg, sp, curIgnore->tree );
@@ -422,13 +422,13 @@ Tree *pushRightIgnore2( Program *prg, Tree **sp, Tree *pushTo, Tree *rightIgnore
}
else {
/* Attach The ignore list. */
- pushRightIgnore( prg, pushTo, rightIgnore );
+ insRightIgnore( prg, pushTo, rightIgnore );
}
return pushTo;
}
-Tree *pushLeftIgnore2( Program *prg, Tree **sp, Tree *pushTo, Tree *leftIgnore )
+Tree *pushLeftIgnore( Program *prg, Tree **sp, Tree *pushTo, Tree *leftIgnore )
{
pushTo = splitTree( prg, pushTo );
@@ -437,7 +437,7 @@ Tree *pushLeftIgnore2( Program *prg, Tree **sp, Tree *pushTo, Tree *leftIgnore )
/* The token already has a left-ignore. Merge by attaching it as a
* right ignore of the new list. */
Kid *curIgnore = treeLeftIgnoreKid( prg, pushTo );
- pushRightIgnore( prg, leftIgnore, curIgnore->tree );
+ insRightIgnore( prg, leftIgnore, curIgnore->tree );
/* Replace the current ignore. */
treeDownref( prg, sp, curIgnore->tree );
@@ -446,14 +446,14 @@ Tree *pushLeftIgnore2( Program *prg, Tree **sp, Tree *pushTo, Tree *leftIgnore )
}
else {
/* Attach the ignore list. */
- pushLeftIgnore( prg, pushTo, leftIgnore );
+ insLeftIgnore( prg, pushTo, leftIgnore );
}
return pushTo;
}
-void popLeftIgnore( Program *prg, Tree **sp, Tree *tree )
+static void remLeftIgnore( Program *prg, Tree **sp, Tree *tree )
{
assert( tree->flags & AF_LEFT_IGNORE );
@@ -465,7 +465,7 @@ void popLeftIgnore( Program *prg, Tree **sp, Tree *tree )
tree->flags &= ~AF_LEFT_IGNORE;
}
-void popRightIgnore( Program *prg, Tree **sp, Tree *tree )
+static void remRightIgnore( Program *prg, Tree **sp, Tree *tree )
{
assert( tree->flags & AF_RIGHT_IGNORE );
@@ -485,6 +485,58 @@ void popRightIgnore( Program *prg, Tree **sp, Tree *tree )
tree->flags &= ~AF_RIGHT_IGNORE;
}
+Tree *popRightIgnore( Program *prg, Tree **sp, Tree *popFrom, Tree **rightIgnore )
+{
+ /* Modifying the tree we are detaching from. */
+ popFrom = splitTree( prg, popFrom );
+
+ Kid *riKid = treeRightIgnoreKid( prg, popFrom );
+
+ /* If the right ignore has a left ignore, then that was the original
+ * right ignore. */
+ Kid *li = treeLeftIgnoreKid( prg, riKid->tree );
+ if ( li != 0 ) {
+ treeUpref( li->tree );
+ remLeftIgnore( prg, sp, riKid->tree );
+ *rightIgnore = riKid->tree;
+ treeUpref( *rightIgnore );
+ riKid->tree = li->tree;
+ }
+ else {
+ *rightIgnore = riKid->tree;
+ treeUpref( *rightIgnore );
+ remRightIgnore( prg, sp, popFrom );
+ }
+
+ return popFrom;
+}
+
+Tree *popLeftIgnore( Program *prg, Tree **sp, Tree *popFrom, Tree **leftIgnore )
+{
+ /* Modifying, make the write safe. */
+ popFrom = splitTree( prg, popFrom );
+
+ Kid *liKid = treeLeftIgnoreKid( prg, popFrom );
+
+ /* If the left ignore has a right ignore, then that was the original
+ * left ignore. */
+ Kid *ri = treeRightIgnoreKid( prg, liKid->tree );
+ if ( ri != 0 ) {
+ treeUpref( ri->tree );
+ remRightIgnore( prg, sp, liKid->tree );
+ *leftIgnore = liKid->tree;
+ treeUpref( *leftIgnore );
+ liKid->tree = ri->tree;
+ }
+ else {
+ *leftIgnore = liKid->tree;
+ treeUpref( *leftIgnore );
+ remLeftIgnore( prg, sp, popFrom );
+ }
+
+ return popFrom;
+}
+
/* Returns an uprefed tree. Saves us having to downref and bindings to zero to
* return a zero-ref tree. */
@@ -513,7 +565,7 @@ Tree *constructReplacementTree( Kid *kid, Tree **bindings, Program *prg, long pa
/* The token already has a left-ignore. Merge by attaching it as a
* right ignore of the new list. */
Kid *curIgnore = treeLeftIgnoreKid( prg, tree );
- pushRightIgnore( prg, leftIgnore, curIgnore->tree );
+ insRightIgnore( prg, leftIgnore, curIgnore->tree );
/* Replace the current ignore. */
curIgnore->tree->refs -= 1;
@@ -522,7 +574,7 @@ Tree *constructReplacementTree( Kid *kid, Tree **bindings, Program *prg, long pa
}
else {
/* Attach the ignore list. */
- pushLeftIgnore( prg, tree, leftIgnore );
+ insLeftIgnore( prg, tree, leftIgnore );
}
}
@@ -541,7 +593,7 @@ Tree *constructReplacementTree( Kid *kid, Tree **bindings, Program *prg, long pa
/* The token already has a right-ignore. Merge by attaching it as a
* right ignore of the new list. */
Kid *curIgnore = treeRightIgnoreKid( prg, tree );
- pushLeftIgnore( prg, rightIgnore, curIgnore->tree );
+ insLeftIgnore( prg, rightIgnore, curIgnore->tree );
/* Replace the current ignore. */
curIgnore->tree->refs -= 1;
@@ -550,7 +602,7 @@ Tree *constructReplacementTree( Kid *kid, Tree **bindings, Program *prg, long pa
}
else {
/* Attach the ignore list. */
- pushRightIgnore( prg, tree, rightIgnore );
+ insRightIgnore( prg, tree, rightIgnore );
}
}
}
@@ -2051,7 +2103,7 @@ Tree *treeTrim( struct ColmProgram *prg, Tree **sp, Tree *tree )
/* The token already has a left-ignore. Merge by attaching it as a
* right ignore of the new list. */
Kid *curIgnore = treeLeftIgnoreKid( prg, tree );
- pushRightIgnore( prg, leftIgnore, curIgnore->tree );
+ insRightIgnore( prg, leftIgnore, curIgnore->tree );
/* Replace the current ignore. */
treeDownref( prg, sp, curIgnore->tree );
@@ -2060,7 +2112,7 @@ Tree *treeTrim( struct ColmProgram *prg, Tree **sp, Tree *tree )
}
else {
/* Attach the ignore list. */
- pushLeftIgnore( prg, tree, leftIgnore );
+ insLeftIgnore( prg, tree, leftIgnore );
}
debug( REALM_PARSE, "attaching ignore right\n" );
@@ -2081,7 +2133,7 @@ Tree *treeTrim( struct ColmProgram *prg, Tree **sp, Tree *tree )
/* The previous token already has a right ignore. Merge by
* attaching it as a left ignore of the new list. */
Kid *curIgnore = treeRightIgnoreKid( prg, tree );
- pushLeftIgnore( prg, rightIgnore, curIgnore->tree );
+ insLeftIgnore( prg, rightIgnore, curIgnore->tree );
/* Replace the current ignore. */
treeDownref( prg, sp, curIgnore->tree );
@@ -2090,7 +2142,7 @@ Tree *treeTrim( struct ColmProgram *prg, Tree **sp, Tree *tree )
}
else {
/* Attach The ignore list. */
- pushRightIgnore( prg, tree, rightIgnore );
+ insRightIgnore( prg, tree, rightIgnore );
}
return tree;
diff --git a/colm/tree.h b/colm/tree.h
index ced24a78..fc197462 100644
--- a/colm/tree.h
+++ b/colm/tree.h
@@ -262,12 +262,10 @@ void treeUpref( Tree *tree );
void treeDownref( struct ColmProgram *prg, Tree **sp, Tree *tree );
long cmpTree( struct ColmProgram *prg, const Tree *tree1, const Tree *tree2 );
-Tree *pushRightIgnore2( struct ColmProgram *prg, Tree **sp, Tree *pushTo, Tree *rightIgnore );
-Tree *pushLeftIgnore2( struct ColmProgram *prg, Tree **sp, Tree *pushTo, Tree *leftIgnore );
-//void pushLeftIgnore( struct ColmProgram *prg, Tree *tree, Tree *ignoreList );
-//void pushRightIgnore( struct ColmProgram *prg, Tree *tree, Tree *ignoreList );
-void popLeftIgnore( struct ColmProgram *prg, Tree **sp, Tree *tree );
-void popRightIgnore( struct ColmProgram *prg, Tree **sp, Tree *tree );
+Tree *pushRightIgnore( struct ColmProgram *prg, Tree **sp, Tree *pushTo, Tree *rightIgnore );
+Tree *pushLeftIgnore( struct ColmProgram *prg, Tree **sp, Tree *pushTo, Tree *leftIgnore );
+Tree *popRightIgnore( struct ColmProgram *prg, Tree **sp, Tree *popFrom, Tree **rightIgnore );
+Tree *popLeftIgnore( struct ColmProgram *prg, Tree **sp, Tree *popFrom, Tree **leftIgnore );
Tree *treeLeftIgnore( struct ColmProgram *prg, Tree *tree );
Tree *treeRightIgnore( struct ColmProgram *prg, Tree *tree );
Kid *treeLeftIgnoreKid( struct ColmProgram *prg, Tree *tree );