diff options
author | Adrian Thurston <thurston@colm.net> | 2019-12-29 13:35:05 +0200 |
---|---|---|
committer | Adrian Thurston <thurston@colm.net> | 2019-12-29 14:30:40 +0200 |
commit | a0c5737f8452816c9a2ca2c91f4a3d889334ac97 (patch) | |
tree | dcb36b50a80925271bfda96d419d310c7318311d | |
parent | eec9b98d955cd3641216537f1d948559ed040d45 (diff) | |
download | colm-a0c5737f8452816c9a2ca2c91f4a3d889334ac97.tar.gz |
colm: fixes for backtracking in constructors and patterns
Backtracking in across literal blocks had a minor fault in constructors and was
fully missing from patterns. Both fixed with this commit. refs #86
-rw-r--r-- | colm/ctinput.cc | 25 | ||||
-rw-r--r-- | test/colm.d/btpat1.lm | 44 |
2 files changed, 66 insertions, 3 deletions
diff --git a/colm/ctinput.cc b/colm/ctinput.cc index bca2080a..efc2c69d 100644 --- a/colm/ctinput.cc +++ b/colm/ctinput.cc @@ -217,8 +217,25 @@ int pat_consume_data( struct colm_program *prg, struct input_impl_ct *ss, int le int pat_undo_consume_data( struct colm_program *prg, struct input_impl_ct *ss, const char *data, int length ) { - ss->offset -= length; - return length; + int origLen = length; + while ( true ) { + int avail = ss->offset; + + /* Okay to go up to the front of the buffer. */ + if ( length > avail ) { + ss->pat_item = ss->pat_item == 0 ? + ss->pattern->list->tail : + ss->pat_item->prev; + ss->offset = ss->pat_item->data.length(); + length -= avail; + } + else { + ss->offset -= length; + break; + } + } + + return origLen; } LangEl *pat_consume_lang_el( struct colm_program *prg, struct input_impl_ct *ss, long *bindId, @@ -459,7 +476,9 @@ int repl_undo_consume_data( struct colm_program *prg, struct input_impl_ct *ss, /* Okay to go up to the front of the buffer. */ if ( length > avail ) { - ss->cons_item = ss->cons_item->prev; + ss->cons_item = ss->cons_item == 0 ? + ss->constructor->list->tail : + ss->cons_item->prev; ss->offset = ss->cons_item->data.length(); length -= avail; } diff --git a/test/colm.d/btpat1.lm b/test/colm.d/btpat1.lm new file mode 100644 index 00000000..0c87832c --- /dev/null +++ b/test/colm.d/btpat1.lm @@ -0,0 +1,44 @@ +lex + token DEF / 'def' / + token id / ( 'a' .. 'z' ) + / + token SQOPEN /'['/ + token SQCLOSE /']'/ + token COLON /':'/ + token FOIL /'!'/ + ignore / ( '\n' | ' ' )+ / +end + +def opt_prod_el_name + [id COLON] +| [] + +def prod_el + [opt_prod_el_name id] + +def prod + [SQOPEN prod_el SQCLOSE] + +def E [] + +def item + [DEF] | [id] | [SQOPEN] | [SQCLOSE] | [COLON] + +def cfl_def + [E item* FOIL] +| [DEF id prod] + +parse Id: id + [stdin] + +cons Def: cfl_def + "def x \[[Id]\]" + +match Def + "def x \[[id]\] + +print [xml(Def) "\n"] + +##### IN ##### +y +##### EXP #### +<cfl_def><DEF>def</DEF><id>x</id><prod><SQOPEN>[</SQOPEN><prod_el><opt_prod_el_name></opt_prod_el_name><id>y</id></prod_el><SQCLOSE>]</SQCLOSE></prod></cfl_def> |