summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bytecode.c108
-rw-r--r--src/compiler.cc2
-rw-r--r--src/pdarun.c2
-rw-r--r--src/pdarun.h2
-rw-r--r--src/struct.h11
-rw-r--r--src/tree.c42
6 files changed, 108 insertions, 59 deletions
diff --git a/src/bytecode.c b/src/bytecode.c
index b6458eec..6a983037 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -192,21 +192,23 @@ void undoStreamAppend( Program *prg, Tree **sp, StreamImpl *is, Tree *input, lon
}
}
-void undoStreamAppendStream( Program *prg, Tree **sp, StreamImpl *is, Tree *input, long length )
+void undoStreamAppendStream( Program *prg, Tree **sp, StreamImpl *is,
+ Tree *input, long length )
{
is->funcs->undoAppendStream( is );
}
-long parseFrag( Program *prg, Tree **sp, Parser *parser, long stopId, long entry )
+long parseFrag( Program *prg, Tree **sp, PdaRun *pdaRun,
+ Stream *input, long stopId, long entry )
{
switch ( entry ) {
case PcrStart:
- if ( ! parser->pdaRun->parseError ) {
- parser->pdaRun->stopTarget = stopId;
+ if ( ! pdaRun->parseError ) {
+ pdaRun->stopTarget = stopId;
- long pcr = parseLoop( prg, sp, parser->pdaRun,
- streamToImpl( parser->input ), entry );
+ long pcr = parseLoop( prg, sp, pdaRun,
+ streamToImpl( input ), entry );
while ( pcr != PcrDone ) {
@@ -216,8 +218,8 @@ case PcrGeneration:
case PcrPreEof:
case PcrReverse:
- pcr = parseLoop( prg, sp, parser->pdaRun,
- streamToImpl( parser->input ), entry );
+ pcr = parseLoop( prg, sp, pdaRun,
+ streamToImpl( input ), entry );
}
}
@@ -228,19 +230,19 @@ break; }
}
long parseFinish( Tree **result, Program *prg, Tree **sp,
- Parser *parser, int revertOn, long entry )
+ PdaRun *pdaRun, Stream *input , int revertOn, long entry )
{
StreamImpl *si;
switch ( entry ) {
case PcrStart:
- if ( parser->pdaRun->stopTarget <= 0 ) {
- si = streamToImpl( parser->input );
+ if ( pdaRun->stopTarget <= 0 ) {
+ si = streamToImpl( input );
si->funcs->setEof( si );
- if ( ! parser->pdaRun->parseError ) {
- si = streamToImpl( parser->input );
- long pcr = parseLoop( prg, sp, parser->pdaRun, si, entry );
+ if ( ! pdaRun->parseError ) {
+ si = streamToImpl( input );
+ long pcr = parseLoop( prg, sp, pdaRun, si, entry );
while ( pcr != PcrDone ) {
@@ -250,8 +252,8 @@ case PcrGeneration:
case PcrPreEof:
case PcrReverse:
- si = streamToImpl( parser->input );
- pcr = parseLoop( prg, sp, parser->pdaRun, si, entry );
+ si = streamToImpl( input );
+ pcr = parseLoop( prg, sp, pdaRun, si, entry );
}
}
}
@@ -259,12 +261,12 @@ case PcrReverse:
/* FIXME: need something here to check that we are not stopped waiting for
* more data when we are actually expected to finish. This check doesn't
* work (at time of writing). */
- //assert( (parser->pdaRun->stopTarget > 0 && parser->pdaRun->stopParsing) || streamToImpl( parser->input )->eofSent );
+ //assert( (pdaRun->stopTarget > 0 && pdaRun->stopParsing) || streamToImpl( input )->eofSent );
if ( !revertOn )
- commitFull( prg, sp, parser->pdaRun, 0 );
+ commitFull( prg, sp, pdaRun, 0 );
- Tree *tree = getParsedRoot( parser->pdaRun, parser->pdaRun->stopTarget > 0 );
+ Tree *tree = getParsedRoot( pdaRun, pdaRun->stopTarget > 0 );
treeUpref( tree );
*result = tree;
@@ -275,11 +277,12 @@ break; }
return PcrDone;
}
-long undoParseFrag( Program *prg, Tree **sp, Parser *parser, long steps, long entry )
+long undoParseFrag( Program *prg, Tree **sp, PdaRun *pdaRun,
+ Stream *input, long steps, long entry )
{
- PdaRun *pdaRun = parser->pdaRun;
-
- debug( prg, REALM_PARSE, "undo parse frag, target steps: %ld, pdarun steps: %ld\n", steps, pdaRun->steps );
+ debug( prg, REALM_PARSE,
+ "undo parse frag, target steps: %ld, pdarun steps: %ld\n",
+ steps, pdaRun->steps );
resetToken( pdaRun );
@@ -294,7 +297,7 @@ case PcrStart:
pdaRun->triggerUndo = 1;
/* The parse loop will recognise the situation. */
- long pcr = parseLoop( prg, sp, pdaRun, streamToImpl(parser->input), entry );
+ long pcr = parseLoop( prg, sp, pdaRun, streamToImpl(input), entry );
while ( pcr != PcrDone ) {
return pcr;
@@ -303,7 +306,7 @@ case PcrGeneration:
case PcrPreEof:
case PcrReverse:
- pcr = parseLoop( prg, sp, pdaRun, streamToImpl(parser->input), entry );
+ pcr = parseLoop( prg, sp, pdaRun, streamToImpl(input), entry );
}
/* Reset environment. */
@@ -2201,14 +2204,15 @@ again:
case IN_PARSE_SAVE_STEPS: {
debug( prg, REALM_BYTECODE, "IN_PARSE_SAVE_STEPS\n" );
- Parser *parser = (Parser*)vm_pop();
- long steps = parser->pdaRun->steps;
+ Struct *parser = (Struct*)vm_pop();
+ PdaRun *pdaRun = colm_struct_get_field_type( parser, PdaRun*, 6 );
+ long steps = pdaRun->steps;
vm_push( (SW)exec->parser );
vm_push( (SW)exec->pcr );
vm_push( (SW)exec->steps );
- exec->parser = parser;
+ exec->parser = (Parser*)parser;
exec->steps = steps;
exec->pcr = PcrStart;
break;
@@ -2293,7 +2297,10 @@ again:
debug( prg, REALM_BYTECODE, "IN_PARSE_FRAG_WC %hd\n", stopId );
- exec->pcr = parseFrag( prg, sp, exec->parser, stopId, exec->pcr );
+
+ PdaRun *pdaRun = colm_struct_get_field_type( exec->parser, PdaRun *, 6 );
+ Stream *input = colm_struct_get_field_type( exec->parser, Stream *, 7 );
+ exec->pcr = parseFrag( prg, sp, pdaRun, input, stopId, exec->pcr );
/* If done, jump to the terminating instruction, otherwise fall
* through to call some code, then jump back here. */
@@ -2325,7 +2332,10 @@ again:
debug( prg, REALM_BYTECODE, "IN_PARSE_FRAG_WV %hd\n", stopId );
- exec->pcr = parseFrag( prg, sp, exec->parser, stopId, exec->pcr );
+ PdaRun *pdaRun = colm_struct_get_field_type( exec->parser, PdaRun *, 6 );
+ Stream *input = colm_struct_get_field_type( exec->parser, Stream *, 7 );
+
+ exec->pcr = parseFrag( prg, sp, pdaRun, input, stopId, exec->pcr );
/* If done, jump to the terminating instruction, otherwise fall
* through to call some code, then jump back here. */
@@ -2366,7 +2376,10 @@ again:
debug( prg, REALM_BYTECODE, "IN_PARSE_FRAG_BKT %hd\n", stopId );
- exec->pcr = undoParseFrag( prg, sp, exec->parser, exec->steps, exec->pcr );
+ PdaRun *pdaRun = colm_struct_get_field_type( exec->parser, PdaRun *, 6 );
+ Stream *input = colm_struct_get_field_type( exec->parser, Stream *, 7 );
+
+ exec->pcr = undoParseFrag( prg, sp, pdaRun, input, exec->steps, exec->pcr );
if ( exec->pcr == PcrDone )
instr += SIZEOF_CODE;
@@ -2392,8 +2405,13 @@ again:
debug( prg, REALM_BYTECODE, "IN_PARSE_FINISH_WC %hd\n", stopId );
- exec->parser->result = 0;
- exec->pcr = parseFinish( &exec->parser->result, prg, sp, exec->parser, false, exec->pcr );
+ PdaRun *pdaRun = colm_struct_get_field_type( exec->parser, PdaRun *, 6 );
+ Stream *input = colm_struct_get_field_type( exec->parser, Stream *, 7 );
+ Tree *result = 0;
+
+ exec->pcr = parseFinish( &result, prg, sp, pdaRun, input, false, exec->pcr );
+
+ colm_struct_set_field_type( exec->parser, Tree*, 8, result );
/* If done, jump to the terminating instruction, otherwise fall
* through to call some code, then jump back here. */
@@ -2425,8 +2443,12 @@ again:
debug( prg, REALM_BYTECODE, "IN_PARSE_FINISH_WV %hd\n", stopId );
- exec->parser->result = 0;
- exec->pcr = parseFinish( &exec->parser->result, prg, sp, exec->parser, true, exec->pcr );
+ PdaRun *pdaRun = colm_struct_get_field_type( exec->parser, PdaRun *, 6 );
+ Stream *input = colm_struct_get_field_type( exec->parser, Stream *, 7 );
+ Tree *result = 0;
+
+ exec->pcr = parseFinish( &result, prg, sp, pdaRun, input, true, exec->pcr );
+ colm_struct_set_field_type( exec->parser, Tree*, 8, result );
if ( exec->pcr == PcrDone )
instr += SIZEOF_CODE;
@@ -2468,7 +2490,10 @@ again:
debug( prg, REALM_BYTECODE, "IN_PARSE_FINISH_BKT %hd\n", stopId );
- exec->pcr = undoParseFrag( prg, sp, exec->parser, exec->steps, exec->pcr );
+ PdaRun *pdaRun = colm_struct_get_field_type( exec->parser, PdaRun *, 6 );
+ Stream *input = colm_struct_get_field_type( exec->parser, Stream *, 7 );
+
+ exec->pcr = undoParseFrag( prg, sp, pdaRun, input, exec->steps, exec->pcr );
if ( exec->pcr == PcrDone )
instr += SIZEOF_CODE;
@@ -2634,8 +2659,11 @@ again:
debug( prg, REALM_BYTECODE, "IN_GET_INPUT\n" );
Parser *parser = (Parser*)vm_pop();
- vm_push( (Tree*)parser->input );
- treeDownref( prg, sp, (Tree*)parser );
+ Stream *stream = colm_struct_get_field_type(
+ (struct colm_struct *)parser, Stream*, 7 );
+ vm_push( (Tree*)stream );
+
+ //treeDownref( prg, sp, (Tree*)parser );
break;
}
case IN_SET_INPUT: {
@@ -2643,8 +2671,8 @@ again:
Parser *parser = (Parser*)vm_pop();
Stream *stream = (Stream*)vm_pop();
- parser->input = stream;
- treeDownref( prg, sp, (Tree*)parser );
+ colm_struct_set_field_type( (struct colm_struct *)parser,
+ Stream*, 7, stream );
break;
}
case IN_CONSTRUCT_TERM: {
diff --git a/src/compiler.cc b/src/compiler.cc
index 9703e88a..1d5d78f2 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -968,7 +968,7 @@ PdaRun *Compiler::parsePattern( Program *prg, Tree **sp, const InputLoc &loc,
StreamImpl *in = newSourceStreamGeneric( "<internal>" );
PdaRun *pdaRun = new PdaRun;
- initPdaRun( prg, pdaRun, pdaTables, parserId, 0, false, 0 );
+ colm_pda_init( prg, pdaRun, pdaTables, parserId, 0, false, 0 );
Tree *stream = constructStream( prg );
diff --git a/src/pdarun.c b/src/pdarun.c
index a9fd2845..c2404607 100644
--- a/src/pdarun.c
+++ b/src/pdarun.c
@@ -1417,7 +1417,7 @@ int isParserStopFinished( PdaRun *pdaRun )
return done;
}
-void initPdaRun( Program *prg, PdaRun *pdaRun, PdaTables *tables,
+void colm_pda_init( Program *prg, PdaRun *pdaRun, PdaTables *tables,
int parserId, long stopTarget, int revertOn, Tree *context )
{
memset( pdaRun, 0, sizeof(PdaRun) );
diff --git a/src/pdarun.h b/src/pdarun.h
index 9a517a4b..6312e249 100644
--- a/src/pdarun.h
+++ b/src/pdarun.h
@@ -346,7 +346,7 @@ typedef struct _PdaRun
FsmRun _fsmRun;
} PdaRun;
-void initPdaRun( struct colm_program *prg, PdaRun *pdaRun, PdaTables *tables,
+void colm_pda_init( struct colm_program *prg, PdaRun *pdaRun, PdaTables *tables,
int parserId, long stopTarget, int revertOn, Tree *context );
void clearPdaRun( struct colm_program *prg, struct colm_tree **sp, PdaRun *pdaRun );
void rtCodeVectReplace( RtCodeVect *vect, long pos, const Code *val, long len );
diff --git a/src/struct.h b/src/struct.h
index 392cd9f2..a5e49cb6 100644
--- a/src/struct.h
+++ b/src/struct.h
@@ -6,10 +6,17 @@ void colm_struct_delete( struct colm_program *prg, struct colm_tree **sp,
struct colm_struct *colm_struct_inbuilt( Program *prg, int size, void *destructor );
-#define colm_struct_get_field(obj, field) \
+#define colm_struct_get_field( obj, field ) \
((struct colm_tree**)(((struct colm_struct*)obj)+1))[field]
-#define colm_struct_set_field(obj, field, val) \
+#define colm_struct_set_field( obj, field, val ) \
((struct colm_tree**)(((struct colm_struct*)obj)+1))[field] = val
+#define colm_struct_get_field_type( obj, type, field ) \
+ ((type*)(((struct colm_struct*)obj)+1))[field]
+
+#define colm_struct_set_field_type( obj, type, field, val ) \
+ ((type*)(((struct colm_struct*)obj)+1))[field] = val
+
+
#endif
diff --git a/src/tree.c b/src/tree.c
index 3d89944a..66a259da 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -8,6 +8,8 @@
#include <colm/bytecode.h>
#include <colm/debug.h>
#include <colm/map.h>
+#include <colm/struct.h>
+
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
@@ -947,6 +949,20 @@ Tree *splitTree( Program *prg, Tree *tree )
return tree;
}
+Parser *colm_parser_construct( Program *prg, GenericInfo *gi )
+{
+ PdaRun *pdaRun = malloc( sizeof(PdaRun) );
+
+ /* Start off the parsing process. */
+ colm_pda_init( prg, pdaRun, prg->rtd->pdaTables,
+ gi->parserId, false, false, 0 );
+
+ struct colm_struct *s = colm_struct_inbuilt( prg, 16, 0 );
+ colm_struct_set_field_type( s, PdaRun*, 6, pdaRun );
+
+ return (Parser*) s;
+}
+
Tree *constructGeneric( Program *prg, long genericId )
{
GenericInfo *genericInfo = &prg->rtd->genericInfo[genericId];
@@ -967,15 +983,7 @@ Tree *constructGeneric( Program *prg, long genericId )
break;
}
case GEN_PARSER: {
- Parser *parser = (Parser*)mapElAllocate( prg );
- parser->id = genericInfo->langElId;
- parser->genericInfo = genericInfo;
- parser->pdaRun = malloc( sizeof(PdaRun) );
-
- /* Start off the parsing process. */
- initPdaRun( prg, parser->pdaRun, prg->rtd->pdaTables,
- genericInfo->parserId, false, false, 0 );
-
+ Parser *parser = colm_parser_construct( prg, genericInfo );
newGeneric = (Tree*) parser;
break;
}
@@ -1721,15 +1729,21 @@ Tree *getParserMem( Parser *parser, Word field )
{
Tree *result = 0;
switch ( field ) {
- case 0:
- result = parser->result;
+ case 0: {
+ result = colm_struct_get_field_type(
+ (struct colm_struct*)parser, Tree *, 8 );
break;
- case 1:
- result = parser->pdaRun->parseErrorText;
+ }
+ case 1: {
+ PdaRun *pdaRun = colm_struct_get_field_type(
+ (struct colm_struct*)parser, PdaRun *, 6 );
+ result = pdaRun->parseErrorText;
break;
- default:
+ }
+ default: {
assert( false );
break;
+ }
}
return result;
}