/
R2.xs
4888 lines (4541 loc) · 116 KB
/
R2.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2013 Jeffrey Kegler
* This file is part of Marpa::R2. Marpa::R2 is free software: you can
* redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* Marpa::R2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser
* General Public License along with Marpa::R2. If not, see
* http://www.gnu.org/licenses/.
*/
#define PERL_NO_GET_CONTEXT
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#include "config.h"
#include "marpa.h"
typedef SV* SVREF;
#undef Dim
#define Dim(x) (sizeof(x)/sizeof(*x))
typedef struct marpa_g Grammar;
/* The error_code member should usually be ignored in favor of
* getting a fresh error code from Libmarpa. Essentially it
* acts as an optional return value for marpa_g_error()
*/
typedef struct {
Marpa_Grammar g;
char *message_buffer;
int libmarpa_error_code;
const char *libmarpa_error_string;
unsigned int throw:1;
unsigned int message_is_marpa_thin_error:1;
} G_Wrapper;
typedef struct marpa_r Recce;
typedef struct {
Marpa_Recce r;
Marpa_Symbol_ID* terminals_buffer;
SV* base_sv;
G_Wrapper* base;
unsigned int ruby_slippers:1;
} R_Wrapper;
typedef struct {
G_Wrapper* g0_wrapper;
/* Need to keep a copy of the G0 SV in order to properly "wrap"
* a recognizer.
*/
SV* g0_sv;
Marpa_Recce r0;
SV* r0_sv;
STRLEN perl_pos; /* character position, taking into account Unicode
Equivalent to Perl pos()
*/
STRLEN input_offset; /* byte position, ignoring Unicode */
SV* input;
Marpa_Symbol_ID input_symbol_id;
UV codepoint; /* For error returns */
HV* per_codepoint_ops;
IV ignore_rejection;
/* The minimum number of tokens that must
be accepted at an earleme */
IV minimum_accepted;
IV trace_g0; /* trace level */
AV* event_queue;
} Unicode_Stream;
typedef struct {
SV* g0_sv;
SV* g1_sv;
G_Wrapper* g0_wrapper;
G_Wrapper* g1_wrapper;
Marpa_Grammar g0;
Marpa_Grammar g1;
Marpa_Symbol_ID* g0_rule_to_g1_lexeme;
} Scanless_G;
typedef struct {
SV* slg_sv;
SV* r1_sv;
SV* stream_sv;
Scanless_G* slg;
Unicode_Stream* stream;
R_Wrapper* r1_wrapper;
Marpa_Recce r1;
G_Wrapper* g0_wrapper;
G_Wrapper* g1_wrapper;
AV* event_queue;
int trace_level;
int trace_terminals;
STRLEN start_of_lexeme;
STRLEN end_of_lexeme;
int please_start_lex_recce;
int stream_read_result;
int r1_earleme_complete_result;
} Scanless_R;
typedef struct marpa_b Bocage;
typedef struct {
Marpa_Bocage b;
SV* base_sv;
G_Wrapper* base;
} B_Wrapper;
typedef struct marpa_o Order;
typedef struct {
Marpa_Order o;
SV* base_sv;
G_Wrapper* base;
} O_Wrapper;
typedef struct marpa_t Tree;
typedef struct {
Marpa_Tree t;
SV* base_sv;
G_Wrapper* base;
} T_Wrapper;
typedef struct marpa_v Value;
typedef struct
{
Marpa_Value v;
SV *base_sv;
G_Wrapper *base;
AV *event_queue;
AV *token_values;
AV *stack;
IV trace_values;
int mode; /* 'raw' or 'stack' */
int result; /* stack location to which to write result */
AV *constants;
AV *rule_semantics;
AV *token_semantics;
AV *nulling_semantics;
Scanless_R* slr;
} V_Wrapper;
#define MARPA_XS_V_MODE_IS_INITIAL 0
#define MARPA_XS_V_MODE_IS_RAW 1
#define MARPA_XS_V_MODE_IS_STACK 2
static const char grammar_c_class_name[] = "Marpa::R2::Thin::G";
static const char recce_c_class_name[] = "Marpa::R2::Thin::R";
static const char bocage_c_class_name[] = "Marpa::R2::Thin::B";
static const char unicode_stream_class_name[] = "Marpa::R2::Thin::U";
static const char order_c_class_name[] = "Marpa::R2::Thin::O";
static const char tree_c_class_name[] = "Marpa::R2::Thin::T";
static const char value_c_class_name[] = "Marpa::R2::Thin::V";
static const char scanless_g_class_name[] = "Marpa::R2::Thin::SLG";
static const char scanless_r_class_name[] = "Marpa::R2::Thin::SLR";
#include "codes.h"
#include "codes.c"
static const char *
event_type_to_string (Marpa_Event_Type event_code)
{
const char *event_name = NULL;
if (event_code >= 0 && event_code < MARPA_ERROR_COUNT) {
event_name = marpa_event_description[event_code].name;
}
return event_name;
}
static const char *
step_type_to_string (const Marpa_Step_Type step_type)
{
const char *step_type_name = NULL;
if (step_type >= 0 && step_type < MARPA_STEP_COUNT) {
step_type_name = marpa_step_type_description[step_type].name;
}
return step_type_name;
}
/* This routine is for the handling exceptions
from libmarpa. It is used when in the general
cases, for those exception which are not singled
out for special handling by the XS logic.
It returns a buffer which must be Safefree()'d.
*/
static char *
error_description_generate (G_Wrapper *g_wrapper)
{
dTHX;
const int error_code = g_wrapper->libmarpa_error_code;
const char *error_string = g_wrapper->libmarpa_error_string;
const char *suggested_description = NULL;
/*
* error_name should always be set when suggested_description is,
* so this initialization should never be used.
*/
const char *error_name = "not libmarpa error";
const char *output_string;
switch (error_code)
{
case MARPA_ERR_DEVELOPMENT:
output_string = form ("(development) %s",
(error_string ? error_string : "(null)"));
goto COPY_STRING;
case MARPA_ERR_INTERNAL:
output_string = form ("Internal error (%s)",
(error_string ? error_string : "(null)"));
goto COPY_STRING;
}
if (error_code >= 0 && error_code < MARPA_ERROR_COUNT) {
suggested_description = marpa_error_description[error_code].suggested;
error_name = marpa_error_description[error_code].name;
}
if (!suggested_description)
{
if (error_string)
{
output_string = form ("libmarpa error %d %s: %s",
error_code, error_name, error_string);
goto COPY_STRING;
}
output_string = form ("libmarpa error %d %s", error_code, error_name);
goto COPY_STRING;
}
if (error_string)
{
output_string = form ("%s%s%s", suggested_description, "; ", error_string);
goto COPY_STRING;
}
output_string = suggested_description;
COPY_STRING:
{
char* buffer = g_wrapper->message_buffer;
if (buffer) Safefree(buffer);
return g_wrapper->message_buffer = savepv(output_string);
}
}
/* Argument must be something that can be Safefree()'d */
static const char *
set_error_from_string (G_Wrapper * g_wrapper, char *string)
{
dTHX;
Marpa_Grammar g = g_wrapper->g;
char *buffer = g_wrapper->message_buffer;
if (buffer) Safefree(buffer);
g_wrapper->message_buffer = string;
g_wrapper->message_is_marpa_thin_error = 1;
marpa_g_error_clear(g);
g_wrapper->libmarpa_error_code = MARPA_ERR_NONE;
g_wrapper->libmarpa_error_string = NULL;
return buffer;
}
/* Return value must be Safefree()'d */
static const char *
xs_g_error (G_Wrapper * g_wrapper)
{
Marpa_Grammar g = g_wrapper->g;
g_wrapper->libmarpa_error_code =
marpa_g_error (g, &g_wrapper->libmarpa_error_string);
g_wrapper->message_is_marpa_thin_error = 0;
return error_description_generate (g_wrapper);
}
/* Wrapper to use vwarn with libmarpa */
static int marpa_r2_warn(const char* format, ...)
{
dTHX;
va_list args;
va_start (args, format);
vwarn (format, &args);
va_end (args);
return 1;
}
enum marpa_op
{
op_end_marker = 0,
op_alternative,
op_bless,
op_callback,
op_earleme_complete,
op_ignore_rejection,
op_noop,
op_push_one,
op_push_undef,
op_push_sequence,
op_push_values,
op_push_slr_range,
op_report_rejection,
op_result_is_array,
op_result_is_constant,
op_result_is_rhs_n,
op_result_is_n_of_sequence,
op_result_is_token_value,
op_result_is_undef
};
typedef struct { enum marpa_op op; const char *name; } Marpa_XS_OP_Data;
static Marpa_XS_OP_Data marpa_op_data[] = {
{ op_alternative, "alternative" },
{ op_bless, "bless" },
{ op_callback, "callback" },
{ op_earleme_complete, "earleme_complete" },
{ op_end_marker, "end_marker" },
{ op_ignore_rejection, "ignore_rejection" },
{ op_noop, "noop" },
{ op_push_one, "push_one" },
{ op_push_sequence, "push_sequence" },
{ op_push_slr_range, "push_slr_range" },
{ op_push_undef, "push_undef" },
{ op_push_values, "push_values" },
{ op_report_rejection, "report_rejection" },
{ op_result_is_array, "result_is_array" },
{ op_result_is_constant, "result_is_constant" },
{ op_result_is_rhs_n, "result_is_rhs_n" },
{ op_result_is_n_of_sequence, "result_is_n_of_sequence" },
{ op_result_is_token_value, "result_is_token_value" },
{ op_result_is_undef, "result_is_undef" },
{ -1, (char *)NULL}
};
static const char* op_to_op_name(enum marpa_op op)
{
Marpa_XS_OP_Data *op_data = marpa_op_data;
for (op_data = marpa_op_data; op_data->name; op_data++)
{
if (op == op_data->op)
{
return op_data->name;
}
}
return "unknown";
}
/* Static grammar methods */
#define SET_G_WRAPPER_FROM_G_SV(g_wrapper, g_sv) { \
IV tmp = SvIV ((SV *) SvRV (g_sv)); \
(g_wrapper) = INT2PTR (G_Wrapper *, tmp); \
}
/* Static recognizer methods */
#define SET_R_WRAPPER_FROM_R_SV(r_wrapper, r_sv) { \
IV tmp = SvIV ((SV *) SvRV (r_sv)); \
(r_wrapper) = INT2PTR (R_Wrapper *, tmp); \
}
/* Maybe inline some of these */
/* Assumes caller has checked that g_sv is blessed into right type.
Assumes caller holds a ref to the recce.
*/
static R_Wrapper*
r_wrap( Marpa_Recce r, SV* g_sv)
{
dTHX;
int highest_symbol_id;
R_Wrapper *r_wrapper;
G_Wrapper *g_wrapper;
Marpa_Grammar g;
SET_G_WRAPPER_FROM_G_SV(g_wrapper, g_sv);
g = g_wrapper->g;
highest_symbol_id = marpa_g_highest_symbol_id (g);
if (highest_symbol_id < 0)
{
if (!g_wrapper->throw)
{
return 0;
}
croak ("failure in marpa_g_highest_symbol_id: %s",
xs_g_error (g_wrapper));
};
Newx (r_wrapper, 1, R_Wrapper);
r_wrapper->r = r;
Newx (r_wrapper->terminals_buffer, highest_symbol_id + 1, Marpa_Symbol_ID);
r_wrapper->ruby_slippers = 0;
SvREFCNT_inc (g_sv);
r_wrapper->base_sv = g_sv;
r_wrapper->base = g_wrapper;
return r_wrapper;
}
/* It is up to the caller to deal with the Libmarpa recce's
* reference count
*/
static Marpa_Recce
r_unwrap (R_Wrapper * r_wrapper)
{
dTHX;
Marpa_Recce r = r_wrapper->r;
/* The wrapper should always have had a ref to its base grammar's SV */
SvREFCNT_dec (r_wrapper->base_sv);
Safefree (r_wrapper->terminals_buffer);
Safefree (r_wrapper);
/* The wrapper should always have had a ref to the Libmarpa recce */
return r;
}
/* Static Stream methods */
/* The caller must ensure that g_sv is an SV of the correct type */
static Unicode_Stream* u_new(SV* g_sv)
{
dTHX;
Unicode_Stream *stream;
IV tmp = SvIV ((SV *) SvRV (g_sv));
G_Wrapper *g_wrapper = INT2PTR (G_Wrapper *, tmp);
Newx (stream, 1, Unicode_Stream);
stream->g0_wrapper = g_wrapper;
stream->r0 = NULL;
/* Hold a ref to the grammar SV we were called with --
* it will have to exist for our lifetime
*/
SvREFCNT_inc (g_sv);
stream->g0_sv = g_sv;
stream->r0_sv = NULL;
stream->input = newSVpvn ("", 0);
stream->perl_pos = 0;
stream->input_offset = 0;
stream->input_symbol_id = -1;
stream->per_codepoint_ops = newHV ();
stream->ignore_rejection = 1;
stream->minimum_accepted = 1;
stream->trace_g0 = 0;
stream->event_queue = newAV ();
return stream;
}
static void u_destroy(Unicode_Stream *stream)
{
dTHX;
const Marpa_Recce r0 = stream->r0;
if (r0)
{
marpa_r_unref (r0);
}
SvREFCNT_dec (stream->event_queue);
SvREFCNT_dec (stream->input);
SvREFCNT_dec (stream->per_codepoint_ops);
SvREFCNT_dec (stream->g0_sv);
if (stream->r0_sv)
{
SvREFCNT_dec (stream->r0_sv);
}
Safefree (stream);
}
static void
u_r0_clear (Unicode_Stream * stream)
{
dTHX;
SV *r0_sv;
Marpa_Recce r0 = stream->r0;
if (!r0)
return;
r0_sv = stream->r0_sv;
if (r0_sv)
{
SvREFCNT_dec (r0_sv);
stream->r0_sv = NULL;
}
marpa_r_unref (r0);
stream->r0 = NULL;
}
static Marpa_Recce
u_r0_new (Unicode_Stream * stream)
{
dTHX;
G_Wrapper *g0_wrapper = stream->g0_wrapper;
Marpa_Recce r0 = marpa_r_new (g0_wrapper->g);
if (!r0)
{
if (!g0_wrapper->throw)
return 0;
croak ("failure in marpa_r_new(): %s", xs_g_error (g0_wrapper));
};
{
int gp_result = marpa_r_start_input (r0);
if (gp_result == -1)
return 0;
if (gp_result < 0)
{
if (g0_wrapper->throw)
{
croak ("Problem in r->start_input(): %s", xs_g_error (g0_wrapper));
}
return 0;
}
}
stream->r0 = r0;
return r0;
}
/* Return values:
* 1 or greater: an event count, as returned by earleme complete.
* 0: success: a full reading of the input, with nothing to report.
* -1: a character was rejected, when rejection is not being ignored
* -2: an unregistered character was found
* -3: earleme_complete() reported an exhausted parse.
* -4: we are tracing, character by character
*/
static int
u_read(Unicode_Stream *stream)
{
dTHX;
U8* input;
STRLEN len;
int input_is_utf8;
const IV trace_g0 = stream->trace_g0;
Marpa_Recognizer r = stream->r0;
if (!r) {
r = u_r0_new(stream);
if (!r)
croak ("Problem in u_read(): %s", xs_g_error (stream->g0_wrapper));
}
input_is_utf8 = SvUTF8 (stream->input);
input = (U8*)SvPV (stream->input, len);
for (;;)
{
int return_value = 0;
UV codepoint;
STRLEN codepoint_length = 1;
STRLEN op_ix;
STRLEN op_count;
IV *ops;
IV ignore_rejection = stream->ignore_rejection;
IV minimum_accepted = stream->minimum_accepted;
int tokens_accepted = 0;
if (stream->input_offset >= len)
break;
if (input_is_utf8)
{
/* utf8_to_uvchr is deprecated in 5.16, but
* utf8_to_uvchr_buf is not available before 5.16
* If I need to get fancier, I should look at Dumper.xs
* in Data::Dumper
*/
#if PERL_VERSION <= 15 && ! defined(utf8_to_uvchr_buf)
codepoint = utf8_to_uvchr(input+stream->input_offset, &codepoint_length);
#else
codepoint =
utf8_to_uvchr_buf (input + stream->input_offset, input + len,
&codepoint_length);
#endif
/* Perl API documents that return value is 0 and length is -1 on error,
* "if possible". length can be, and is, in fact unsigned.
* I deal with this by noting that 0 is a valid UTF8 char but should
* have a length of 1, when valid.
*/
if (codepoint == 0 && codepoint_length != 1) {
croak ("Problem in r->read_string(): invalid UTF8 character");
}
}
else
{
codepoint = (UV) input[stream->input_offset];
codepoint_length = 1;
}
{
STRLEN dummy;
SV **p_ops_sv =
hv_fetch (stream->per_codepoint_ops, (char *) &codepoint,
(I32)sizeof (codepoint), 0);
if (!p_ops_sv)
{
stream->codepoint = codepoint;
return -2;
}
ops = (IV *)SvPV (*p_ops_sv, dummy);
}
if (trace_g0 >= 1) {
AV *event;
SV *event_data[3];
event_data[0] = newSVpv ("g0 reading codepoint", 0);
event_data[1] = newSViv ((IV)codepoint);
event_data[2] = newSViv ((IV)stream->perl_pos);
event = av_make (Dim (event_data), event_data);
av_push (stream->event_queue, newRV_noinc ((SV *) event));
}
/* ops[0] is codepoint */
op_count = ops[1];
for (op_ix = 2; op_ix < op_count; op_ix++)
{
IV op_code = ops[op_ix];
switch (op_code)
{
case op_report_rejection:
{
ignore_rejection = 0;
break;
}
case op_ignore_rejection:
{
ignore_rejection = 1;
break;
}
case op_alternative:
{
int result;
int symbol_id;
int length;
int value;
op_ix++;
if (op_ix >= op_count)
{
croak
("Missing operand for op code (0x%lx); codepoint=0x%lx, op_ix=0x%lx",
(unsigned long) op_code, (unsigned long) codepoint,
(unsigned long) op_ix);
}
symbol_id = (int) ops[op_ix];
if (op_ix + 2 >= op_count)
{
croak
("Missing operand for op code (0x%lx); codepoint=0x%lx, op_ix=0x%lx",
(unsigned long) op_code, (unsigned long) codepoint,
(unsigned long) op_ix);
}
value = (int) ops[++op_ix];
length = (int) ops[++op_ix];
result = marpa_r_alternative (r, symbol_id, value, length);
switch (result)
{
case MARPA_ERR_UNEXPECTED_TOKEN_ID:
/* This guarantees that later, if we fall below
* the minimum number of tokens accepted,
* we have one of them as an example
*/
stream->input_symbol_id = symbol_id;
if (trace_g0 >= 1) {
AV *event;
SV *event_data[4];
event_data[0] = newSVpv ("g0 rejected codepoint", 0);
event_data[1] = newSViv ((IV)codepoint);
event_data[2] = newSViv ((IV)stream->perl_pos);
event_data[3] = newSViv ((IV)symbol_id);
event = av_make (Dim (event_data), event_data);
av_push (stream->event_queue, newRV_noinc ((SV *) event));
}
if (!ignore_rejection)
{
stream->codepoint = codepoint;
return -1;
}
break;
case MARPA_ERR_NONE:
if (trace_g0 >= 1) {
AV *event;
SV *event_data[4];
event_data[0] = newSVpv ("g0 accepted codepoint", 0);
event_data[1] = newSViv ((IV)codepoint);
event_data[2] = newSViv ((IV)stream->perl_pos);
event_data[3] = newSViv ((IV)symbol_id);
event = av_make (Dim (event_data), event_data);
av_push (stream->event_queue, newRV_noinc ((SV *) event));
}
tokens_accepted++;
break;
default:
stream->codepoint = codepoint;
stream->input_symbol_id = symbol_id;
croak
("Problem alternative() failed at char ix %d; symbol id %d; codepoint 0x%lx\n"
"Problem in r->input_string_read(), alternative() failed: %s",
(int)stream->perl_pos, symbol_id, (unsigned long)codepoint,
xs_g_error (stream->g0_wrapper));
}
}
break;
case op_earleme_complete:
{
int result;
if (tokens_accepted < minimum_accepted) {
stream->codepoint = codepoint;
return -1;
}
marpa_r_latest_earley_set_value_set (r, (int)codepoint);
result = marpa_r_earleme_complete (r);
if (result > 0)
{
return_value = result;
/* Advance one character before returning */
goto ADVANCE_ONE_CHAR;
}
if (result == -2)
{
const Marpa_Error_Code error =
marpa_g_error (stream->g0_wrapper->g, NULL);
if (error == MARPA_ERR_PARSE_EXHAUSTED)
{
return -3;
}
}
if (result < 0)
{
croak
("Problem in r->input_string_read(), earleme_complete() failed: %s",
xs_g_error (stream->g0_wrapper));
}
}
break;
default:
croak ("Unknown op code (0x%lx); codepoint=0x%lx, op_ix=0x%lx",
(unsigned long) op_code, (unsigned long) codepoint,
(unsigned long) op_ix);
}
}
ADVANCE_ONE_CHAR:;
stream->input_offset += codepoint_length;
stream->perl_pos++;
/* This logic does not allow a return value of 0,
* which is reserved for a indicating a full
* read of the input string without event
*/
if (return_value)
{
return return_value;
}
if (trace_g0) {
return -4;
}
}
return 0;
}
static STRLEN
u_pos_set(Unicode_Stream* stream, STRLEN new_pos)
{
/* *SAFELY* change the position
* This requires care in UTF8
* Returns the position *BEFORE* the call
*/
dTHX;
U8 *input;
int input_is_utf8;
STRLEN len;
const STRLEN old_pos = stream->perl_pos;
/* Zero position is easy special case */
if (new_pos == 0) {
stream->input_offset = new_pos;
stream->perl_pos = new_pos;
return old_pos;
}
/* Same as current position is another easy special case */
if (new_pos == old_pos) {
return old_pos;
}
input_is_utf8 = SvUTF8 (stream->input);
input = (U8 *) SvPV (stream->input, len);
if (input_is_utf8)
{
/* I am required to *know* that the "hop" is inside the string,
* which apparently can have Perl extensions -- it is *Perl* utf8, not
* standard UTF8. The only safe thing
* to use the Perl API to hop one codepoint at a time,
* which is basically how utf8_hop() does it anyway.
*/
IV hop = new_pos - old_pos;
U8 *p_current = input + stream->input_offset;
U8 *end_of_input = input + len;
while (hop > 0)
{
if (p_current >= end_of_input)
{
croak
("Problem in stream->pos_set(): Attempt to set position after end of utf8 string");
}
p_current = utf8_hop (p_current, 1);
hop--;
}
while (hop < 0)
{
if (p_current <= input)
{
croak
("Problem in stream->pos_set(): Attempt to set position before start of utf8 string");
}
p_current = utf8_hop (p_current, -1);
hop++;
}
stream->input_offset = p_current - input;
stream->perl_pos = new_pos;
}
else
{
/* STRLEN is assumed to be unsigned so no check for less than zero */
if (new_pos >= len)
{
croak
("Problem in stream->pos_set(): new pos = %ld, but length = %ld",
(long) new_pos, (long) len);
}
stream->input_offset = new_pos;
stream->perl_pos = new_pos;
}
return old_pos;
}
/* Static valuator methods */
/* Return -1 on failure due to wrong mode */
static IV
v_create_stack(V_Wrapper* v_wrapper)
{
dTHX;
if (v_wrapper->mode == MARPA_XS_V_MODE_IS_RAW)
{
return -1;
}
v_wrapper->stack = newAV ();
av_extend (v_wrapper->stack, 1023);
v_wrapper->mode = MARPA_XS_V_MODE_IS_STACK;
return 0;
}
static void slr_locations (Scanless_R * slr, Marpa_Earley_Set_ID earley_set,
int *p_start, int *p_end);
static int
v_do_stack_ops (V_Wrapper * v_wrapper, SV ** stack_results)
{
dTHX;
AV *stack = v_wrapper->stack;
const Marpa_Value v = v_wrapper->v;
const Marpa_Step_Type step_type = marpa_v_step_type (v);
IV result_ix = marpa_v_result (v);
IV *ops;
int op_ix;
UV blessing = 0;
/* values_av is created when needed.
* It is created mortal, so it automatically goes
* away unless it is de-mortalized.
*/
AV *values_av = NULL;
v_wrapper->result = result_ix;
switch (step_type)
{
STRLEN dummy;
case MARPA_STEP_RULE:
{
SV **p_ops_sv =
av_fetch (v_wrapper->rule_semantics, marpa_v_rule (v), 0);
if (!p_ops_sv)
{
croak ("Problem in v->stack_step: rule %d is not registered",
marpa_v_rule (v));
}
ops = (IV *) SvPV (*p_ops_sv, dummy);
}
break;
case MARPA_STEP_TOKEN:
{
SV **p_ops_sv =
av_fetch (v_wrapper->token_semantics, marpa_v_token (v), 0);
if (!p_ops_sv)
{
croak ("Problem in v->stack_step: token %d is not registered",
marpa_v_token (v));
}
ops = (IV *) SvPV (*p_ops_sv, dummy);
}
break;
case MARPA_STEP_NULLING_SYMBOL:
{
SV **p_ops_sv =
av_fetch (v_wrapper->nulling_semantics, marpa_v_token (v), 0);
if (!p_ops_sv)
{
croak
("Problem in v->stack_step: nulling symbol %d is not registered",
marpa_v_token (v));
}
ops = (IV *) SvPV (*p_ops_sv, dummy);
}
break;
default:
/* Never reached -- turns off warning about uninitialized ops */
ops = NULL;
}
op_ix = 0;
while (1)
{
IV op_code = ops[op_ix++];
if (v_wrapper->trace_values >= 2)
{
AV *event;
SV *event_data[3];
const char *result_string = step_type_to_string (step_type);
if (!result_string)
result_string = "valuator unknown step";
event_data[0] = newSVpvs ("starting op");
event_data[1] = newSVpv (result_string, 0);
event_data[2] = newSVpv (op_to_op_name (op_code), 0);
event = av_make (Dim (event_data), event_data);
av_push (v_wrapper->event_queue, newRV_noinc ((SV *) event));
}
switch (op_code)
{
case 0:
return -1;
case op_result_is_undef:
{
av_fill (stack, -1 + result_ix);
}
return -1;
case op_result_is_constant:
{
IV constant_ix = ops[op_ix++];
SV **p_constant_sv;
p_constant_sv = av_fetch (v_wrapper->constants, constant_ix, 0);
if (p_constant_sv)
{
SV *constant_sv = newSVsv (*p_constant_sv);
SV **stored_sv = av_store (stack, result_ix, constant_sv);
if (!stored_sv)
{
SvREFCNT_dec (constant_sv);
}
}
else
{
av_store (stack, result_ix, &PL_sv_undef);
}
if (v_wrapper->trace_values && step_type == MARPA_STEP_TOKEN)
{
AV *event;
SV *event_data[3];
const char *result_string = step_type_to_string (step_type);
if (!result_string)
result_string = "valuator unknown step";
event_data[0] = newSVpv (result_string, 0);
event_data[1] = newSViv (marpa_v_token (v));
event_data[2] = newSViv (result_ix);
event = av_make (Dim (event_data), event_data);
av_push (v_wrapper->event_queue, newRV_noinc ((SV *) event));
}
}
return -1;
case op_result_is_rhs_n:
case op_result_is_n_of_sequence:
{
SV **stored_av;
SV **p_sv;
IV stack_offset = ops[op_ix++];
IV fetch_ix;
if (step_type != MARPA_STEP_RULE)
{
av_fill (stack, result_ix - 1);
return -1;
}
if (stack_offset == 0)
{
/* Special-cased for 4 reasons --
* it's common, it's reference count handling is
* a special case and it can be easily and highly optimized.
*/
av_fill (stack, result_ix);
return -1;
}
/* Determine index of SV to fetch */
if (op_code == op_result_is_rhs_n)
{
if (stack_offset > 0)
{
fetch_ix = result_ix + stack_offset;