summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2013-04-04 20:27:00 -0400
committerAdrian Thurston <thurston@complang.org>2013-04-04 20:27:00 -0400
commit52ac774a4c4dcc6beb0b87e708e738efbe0e915d (patch)
tree4051a0b1acb75c8f3ddef2c75bdd7795cb01847b
parent6355aa3155a050d1a84212625dcc55fc407fbf79 (diff)
downloadcolm-52ac774a4c4dcc6beb0b87e708e738efbe0e915d.tar.gz
moving position updates into the consume operation
-rw-r--r--colm/input.c38
-rw-r--r--colm/input.h3
-rw-r--r--colm/pdarun.c44
-rw-r--r--colm/pdarun.h5
4 files changed, 37 insertions, 53 deletions
diff --git a/colm/input.c b/colm/input.c
index 8c496e40..19fe5b18 100644
--- a/colm/input.c
+++ b/colm/input.c
@@ -94,6 +94,37 @@ StreamImpl *newSourceStreamFd( long fd )
return ss;
}
+/* Keep the position up to date after consuming text. */
+void updatePosition( StreamImpl *is, const char *data, long length )
+{
+ int i;
+ for ( i = 0; i < length; i++ ) {
+ if ( data[i] != '\n' )
+ is->column += 1;
+ else {
+ is->line += 1;
+ is->column = 1;
+ }
+ }
+
+ is->byte += length;
+}
+
+/* Keep the position up to date after sending back text. */
+void undoPosition( StreamImpl *is, const char *data, long length )
+{
+ /* FIXME: this needs to fetch the position information from the parsed
+ * token and restore based on that.. */
+ int i;
+ for ( i = 0; i < length; i++ ) {
+ if ( data[i] == '\n' )
+ is->line -= 1;
+ }
+
+ is->byte -= length;
+}
+
+
static RunBuf *sourceStreamPopHead( StreamImpl *ss )
{
RunBuf *ret = ss->queue;
@@ -241,8 +272,6 @@ int fdGetData( StreamImpl *ss, char *dest, int length )
int fdConsumeData( StreamImpl *ss, int length )
{
- //debug( REALM_INPUT, "source consuming %ld bytes\n", length );
-
int consumed = 0;
/* Move over skip bytes. */
@@ -262,9 +291,9 @@ int fdConsumeData( StreamImpl *ss, int length )
if ( avail > 0 ) {
/* The source data from the current buffer. */
int slen = avail <= length ? avail : length;
- //debug( REALM_INPUT, "consumed: %.*s\n", slen, buf->data + buf->offset );
consumed += slen;
length -= slen;
+ updatePosition( ss, buf->data + buf->offset, slen );
buf->offset += slen;
}
}
@@ -281,12 +310,11 @@ int fdConsumeData( StreamImpl *ss, int length )
int fdUndoConsumeData( StreamImpl *ss, const char *data, int length )
{
- //debug( REALM_INPUT, "undoing consume of %ld bytes\n", length );
-
RunBuf *newBuf = newRunBuf();
newBuf->length = length;
memcpy( newBuf->data, data, length );
sourceStreamPrepend( ss, newBuf );
+ undoPosition( ss, data, length );
return length;
}
diff --git a/colm/input.h b/colm/input.h
index 2c46ec7a..44db61be 100644
--- a/colm/input.h
+++ b/colm/input.h
@@ -160,6 +160,9 @@ StreamImpl *newSourceStreamCons( struct Constructor *constructor );
StreamImpl *newSourceStreamFile( FILE *file );
StreamImpl *newSourceStreamFd( long fd );
+void updatePosition( StreamImpl *inputStream, const char *data, long length );
+void undoPosition( StreamImpl *inputStream, const char *data, long length );
+
#ifdef __cplusplus
}
#endif
diff --git a/colm/pdarun.c b/colm/pdarun.c
index 915da819..2bd37c07 100644
--- a/colm/pdarun.c
+++ b/colm/pdarun.c
@@ -81,36 +81,6 @@ void clearFsmRun( Program *prg, FsmRun *fsmRun )
}
}
-/* Keep the position up to date after consuming text. */
-void updatePosition( StreamImpl *is, const char *data, long length )
-{
- int i;
- for ( i = 0; i < length; i++ ) {
- if ( data[i] != '\n' )
- is->column += 1;
- else {
- is->line += 1;
- is->column = 1;
- }
- }
-
- is->byte += length;
-}
-
-/* Keep the position up to date after sending back text. */
-void undoPosition( StreamImpl *is, const char *data, long length )
-{
- /* FIXME: this needs to fetch the position information from the parsed
- * token and restore based on that.. */
- int i;
- for ( i = 0; i < length; i++ ) {
- if ( data[i] == '\n' )
- is->line -= 1;
- }
-
- is->byte -= length;
-}
-
void incrementSteps( PdaRun *pdaRun )
{
pdaRun->steps += 1;
@@ -211,7 +181,6 @@ Head *streamPull( Program *prg, PdaRun *pdaRun, StreamImpl *is, long length )
fsmRun->toklen = 0;
Head *tokdata = stringAllocPointer( prg, dest, length );
- updatePosition( is, dest, length );
return tokdata;
}
@@ -222,7 +191,6 @@ Head *streamPull( Program *prg, PdaRun *pdaRun, StreamImpl *is, long length )
is->funcs->getData( is, dest, length );
is->funcs->consumeData( is, length );
- updatePosition( is, dest, length );
return head;
}
}
@@ -286,7 +254,6 @@ static void sendBackText( FsmRun *fsmRun, StreamImpl *is, const char *data, long
// (int)length, data );
is->funcs->undoConsumeData( is, data, length );
- undoPosition( is, data, length );
}
void sendBackTree( StreamImpl *is, Tree *tree )
@@ -364,10 +331,6 @@ static void sendBack( Program *prg, Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun,
debug( prg, REALM_PARSE, "sending back: %s\n", prg->rtd->lelInfo[parseTree->id].name );
if ( parseTree->flags & PF_NAMED ) {
- ///* Send back anything in the buffer that has not been parsed. */
- //if ( fsmRun->p == fsmRun->runBuf->data )
- // sendBackRunBufHead( fsmRun, is );
-
/* Send the named lang el back first, then send back any leading
* whitespace. */
is->funcs->undoConsumeLangEl( is );
@@ -821,8 +784,7 @@ void sendIgnore( Program *prg, Tree **sp, StreamImpl *is, FsmRun *fsmRun, PdaRun
/* Make the ignore string. */
Head *ignoreStr = extractMatch( prg, fsmRun, is );
- updatePosition( is, ignoreStr->data, ignoreStr->length );
-
+
debug( prg, REALM_PARSE, "ignoring: %.*s\n", ignoreStr->length, ignoreStr->data );
Tree *tree = treeAllocate( prg );
@@ -846,8 +808,6 @@ static void sendToken( Program *prg, Tree **sp, StreamImpl *is, FsmRun *fsmRun,
prg->rtd->lelInfo[id].name,
stringLength(tokdata), stringData(tokdata) );
- updatePosition( is, stringData(tokdata), stringLength(tokdata) );
-
Kid *input = makeTokenWithData( prg, pdaRun, fsmRun, is, id, tokdata );
incrementSteps( pdaRun );
@@ -903,8 +863,6 @@ static void sendCi( Program *prg, Tree **sp, StreamImpl *is, FsmRun *fsmRun, Pda
prg->rtd->lelInfo[id].name,
stringLength(tokdata), stringData(tokdata) );
- updatePosition( is, stringData(tokdata), stringLength(tokdata) );
-
Kid *input = makeTokenWithData( prg, pdaRun, fsmRun, is, id, tokdata );
incrementSteps( pdaRun );
diff --git a/colm/pdarun.h b/colm/pdarun.h
index 9087ca5e..c9d6285a 100644
--- a/colm/pdarun.h
+++ b/colm/pdarun.h
@@ -92,9 +92,6 @@ typedef struct _FsmRun
long matchedToken;
} FsmRun;
-void updatePosition( StreamImpl *inputStream, const char *data, long length );
-void undoPosition( StreamImpl *inputStream, const char *data, long length );
-void sendBackRunBufHead( FsmRun *fsmRun, StreamImpl *inputStream );
void undoStreamPull( StreamImpl *inputStream, const char *data, long length );
#if SIZEOF_LONG != 4 && SIZEOF_LONG != 8
@@ -445,8 +442,6 @@ void pushBinding( PdaRun *pdaRun, ParseTree *parseTree );
void executeGenerationAction( struct ColmProgram *prg, Tree **sp, FsmRun *fsmRun, PdaRun *pdaRun,
StreamImpl *inputStream, int frameId, Code *code, long id, Head *tokdata );
Kid *extractIgnore( PdaRun *pdaRun );
-long sendBackQueuedIgnore( struct ColmProgram *prg, Tree **sp, StreamImpl *inputStream,
- FsmRun *fsmRun, PdaRun *pdaRun, long entry );
void clearIgnoreList( struct ColmProgram *prg, Tree **sp, Kid *kid );
Head *extractMatch( struct ColmProgram *prg, FsmRun *fsmRun, StreamImpl *inputStream );
Head *extractMatch( struct ColmProgram *prg, FsmRun *fsmRun, StreamImpl *inputStream );