File: parser.c

package info (click to toggle)
sgrep 1.94a-4
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 880 kB
  • ctags: 1,074
  • sloc: ansic: 10,566; sh: 3,433; makefile: 134
file content (1108 lines) | stat: -rw-r--r-- 28,161 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
/*
	System: Structured text retrieval tool sgrep.
	Module: parser.c
	Author: Pekka Kilpel�inen & Jani Jaakkola
	Description: Parses expression string ( which has been gathered in
	             main.c and preprocessed in preprocess.c )
	             Returns operator tree.
	             Used through fuction parse_tree()
	Version history: Original version February 1995 by JJ & PK
	Copyright: University of Helsinki, Dept. of Computer Science
*/

#include <ctype.h> /* FIXME: get rid of ctype.h */
#include <string.h>

#define SGREP_LIBRARY
#include "sgrep.h"


/* Maximum length of one phrase. */
#define MAX_PHRASE_LENGTH 512
/* Maximum length of a reserved word. Could also be smaller */
#define MAX_R_WORD 20
#define ELLENGTH 79
#define SHOWR 5

/* 
 * Tokens of sgrep query language
 */
enum Token { 
    /* Tokens for reserved words */
    W_IN,W_NOT,W_CONTAINING,W_OR,
    W_ORDERED,W_L_ORDERED,W_R_ORDERED,W_LR_ORDERED,
    W_EXTRACTING,
    W_QUOTE,W_L_QUOTE,W_R_QUOTE,W_LR_QUOTE,
    W_EQUAL,		/* PK Febr 12 '96 */
    W_PARENTING,W_CHILDRENING,
    W_NEAR, W_NEAR_BEFORE,
    W_OUTER,W_INNER,W_CONCAT,
    /* number functions */
    W_JOIN, W_FIRST, W_LAST,
    W_FIRST_BYTES,W_LAST_BYTES,
    /* FIXME: what to do with "chars" */
    W_CHARS,
    /* The automagical phrases */
    W_START,W_END,
    /* Phrase types */
    W_PROLOG,
    W_ELEMENTS,
    W_FILE,
    W_STRING, W_REGEX,
    W_DOCTYPE, W_DOCTYPE_PID, W_DOCTYPE_SID,
    W_PI, W_ATTRIBUTE, W_ATTVALUE,
    W_STAG, W_ETAG, W_COMMENT, W_COMMENT_WORD,
    W_WORD, W_CDATA,
    W_ENTITY, W_ENTITY_DECLARATION, W_ENTITY_LITERAL,
    W_ENTITY_PID, W_ENTITY_SID, W_ENTITY_NDATA,
    W_RAW,
    R_WORDS, 
    /* And the rest */
    W_LPAREN,W_RPAREN,W_LBRACK,W_RBRACK,W_COMMA,W_PHRASE,W_NUMBER,
    /* End of file */
    W_EOF,
    /* In sgrep, parse error while scanning is just a kind of token */
    W_PARSE_ERROR,
    W_END_TOKENS
};

enum ScannerState {
    SCANNER_START,
    SCANNER_STRING,
    SCANNER_NAME,
    SCANNER_NUMBER,
    SCANNER_COMMENT,
    SCANNER_LINE_START_COMMENT,
    SCANNER_LINE_NUMBER_COMMENT,
    SCANNER_FILE_NAME_COMMENT_START,
    SCANNER_FILE_NAME_COMMENT
};

/*
 * Parser object
 */
typedef struct {
    SgrepData *sgrep;
    /* The command string and a index to it */
    const char *expr_str;
    int expr_ind;

    /* Current character, token and string of current string token */
    int ch;
    enum Token token;
    SgrepString *unexpanded_string_token;
    SgrepString *string_token;
    
    /* This points to first phrase in the phrase list */
    struct PHRASE_NODE *first_phrase;

    /* for telling where errors occurred */
    int line,column;
    SgrepString *file;
    int errline,errcol,errind;

    /* Scanners state */
    enum ScannerState scanner_state;
    
    /* Needed for freeing data after parse error */
    int nodes;
    ParseTreeNode *node_array[5000];
} Parser;



void real_parse_error(Parser *parser,char *);
int get_next_char(Parser *parser);
enum Token next_token(Parser *parser);

ParseTreeNode *create_tree_node(Parser *parser,enum Oper oper);
ParseTreeNode *parse_reg_expr(Parser *parser);
ParseTreeNode *parse_basic_expr(Parser *parser);
ParseTreeNode *parse_oper_expr(Parser *parser,ParseTreeNode *);
ParseTreeNode *parse_phrase(Parser *,const char *);
ParseTreeNode *parse_cons_list(Parser *parser);
ParseTreeNode *new_string_phrase(Parser *, SgrepString *, 
					   const char *);
#ifdef DEBUG
void print_tree(ParseTreeNode *);
#endif

#define parse_error(E) \
  do { real_parse_error(parser,(E));return NULL; } while (0)
#define token (parser->token)
#define NEXT_TOKEN \
    do { token=next_token(parser); if (token==W_PARSE_ERROR) return NULL; } while (0);


/*
 * When looking for reserved words they are matched to these strings 
 */
struct {
    const char *word;
    enum Token t;
} reserved_words[]=
{ 
    {"in",W_IN},
    {"not",W_NOT},
    {"containing",W_CONTAINING},
    {"or",W_OR},
    {"..",W_ORDERED},
    {"_.",W_L_ORDERED},
    {"._",W_R_ORDERED},
    {"__",W_LR_ORDERED},
    {"extracting", W_EXTRACTING},
    {"quote",W_QUOTE},
    {"_quote",W_L_QUOTE},
    {"quote_",W_R_QUOTE},
    {"_quote_",W_LR_QUOTE },
    {"equal",W_EQUAL},	 /* PK Febr 12 '96 */
    {"parenting",W_PARENTING},
    {"childrening",W_CHILDRENING},
    {"near",W_NEAR},
    {"near_before",W_NEAR_BEFORE},
    {"outer",W_OUTER},
    {"inner",W_INNER},
    {"concat",W_CONCAT},
    /* Constant lists */
    {"chars",W_CHARS},
    /* Number functions */
    {"first",W_FIRST},
    {"last",W_LAST},
    {"first_bytes",W_FIRST_BYTES},
    {"last_bytes",W_LAST_BYTES},
    {"join",W_JOIN},
    /* Automagical phrases */
    {"start",W_START},
    {"end",W_END},
    /* Different phrases */
    {"file",W_FILE},
    {"string", W_STRING},
    {"regex",W_REGEX},
    {"pi", W_PI},
    {"attribute", W_ATTRIBUTE},
    {"attvalue",W_ATTVALUE},
    {"stag", W_STAG},
    {"etag", W_ETAG},
    {"comments", W_COMMENT},
    {"elements", W_ELEMENTS},
    {"comment_word",W_COMMENT_WORD},
    {"word", W_WORD},
    {"cdata",W_CDATA},
    {"entity",W_ENTITY},

    /* Stuff from DTD */
    {"prologs", W_PROLOG},
    {"doctype", W_DOCTYPE},
    {"doctype_pid", W_DOCTYPE_PID},
    {"doctype_sid",W_DOCTYPE_SID},
    {"entity_declaration",W_ENTITY_DECLARATION },
    {"entity_literal",W_ENTITY_LITERAL },
    {"entity_pid",W_ENTITY_PID },
    {"entity_sid",W_ENTITY_SID },
    {"entity_ndata",W_ENTITY_NDATA },
    /* And the raw phrase */
    {"raw", W_RAW },
    {NULL,W_END_TOKENS}
};

/*
 * Gives user readable operation name from given opernum
 */
const char *give_oper_name(int oper)
{
	switch (oper) {
	case IN:		return "in";break;
	case NOT_IN:		return "not in";break;
	case CONTAINING:	return "containing";break;
	case NOT_CONTAINING:	return "not containing";break;
	case EQUAL:		return "equal";break; /* PK Febr'95 */
	case NOT_EQUAL:		return "not equal";break; /* PK Febr'95 */
	case OR:		return "or";break;
	case ORDERED:		return "..";break;
	case L_ORDERED:		return "_.";break;
	case R_ORDERED:		return "._";break;
	case LR_ORDERED:	return "__";break;
	case EXTRACTING:	return "extracting";break;
	case OUTER:		return "outer";break;
	case INNER:		return "inner";break;
	case CONCAT:		return "concat";break;
	case JOIN:		return "join";break;
	case PHRASE:		return "phrase";break;
	case INVALID:		return "invalid";break;
	default:		return "unknown";break;
	}
}


/*
 * Gets next character from input expression. Counts also lines and columns
 * return character must be unsigned to keep sgrep is 8-bit clean
 */

int get_next_char(Parser *parser)
{
	switch (parser->expr_str[parser->expr_ind]) {
		case '\n':
			parser->column=-1;
			parser->line++;
			break;
	}
	parser->column++;
	return parser->ch=((unsigned char *)parser->expr_str)[parser->expr_ind++];	
}
	
/*
 * Scans next token from input string. If token was a phrase sets **phrase 
 */
enum Token next_token(Parser *parser)
{
    char str[MAX_PHRASE_LENGTH+1];
    int i=0,j=0;
    int start;
    int ch;
    SgrepString **phrase;
    SGREPDATA(parser);

/* if we already have next character in ch then ch!=-1. Otherwise we have to
   get one */
    ch=parser->ch;
    phrase=&parser->string_token;
    if (ch==-1) ch=get_next_char(parser);
 
/* These show the place where possible error started */
    parser->errline=parser->line;
    parser->errcol=parser->column;
    parser->errind=parser->expr_ind;

/* Finite automaton for recognizing next word */	
    do {
	switch (parser->scanner_state) {
	
	    /* This is the start state */
	case SCANNER_START: 
	    /* skip the whitespace */
	    while (isspace(ch)) ch=get_next_char(parser);

	    /* These show the place where possible error started */
	    parser->errline=parser->line;
	    parser->errcol=parser->column;
	    parser->errind=parser->expr_ind;
		
	    switch(ch) {
	    case 0:		        
		return W_EOF;
	    case '(':
		parser->ch=-1;
		return W_LPAREN;
	    case ')': 
		parser->ch=-1;
		return W_RPAREN;
	    case '[':
		parser->ch=-1;
		return W_LBRACK;
	    case ']':
		parser->ch=-1;
		return W_RBRACK;
	    case ',':
		parser->ch=-1;
		return W_COMMA;
	    case '\"': 
		i=0;
		parser->scanner_state=SCANNER_STRING;
		break;
	    case '#':
		parser->scanner_state=SCANNER_COMMENT;
		break;
	    }
	    if ( isalpha(ch) || ch=='.' || ch=='_' )
	    {
		i=1;
		str[0]=tolower(ch);
		parser->scanner_state=SCANNER_NAME;
		start=parser->column;
	    }
	    if (ch>='0' && ch<='9')
	    {
		i=1;
		str[0]=ch;
		start=parser->column;
		parser->scanner_state=SCANNER_NUMBER;
	    }
	    if ( parser->scanner_state==SCANNER_START )
	    {
#ifdef DEBUG
		fprintf(stderr,"Character %c ascii(%d)\n",ch,ch);
#endif
		real_parse_error(parser,"Invalid character");
		return W_PARSE_ERROR;
	    }
	    break;
		
	    /* This state reads a phrase string */
	case SCANNER_STRING:
	    if (ch==0 || ch=='\n') 
	    {
		real_parse_error(parser,"Unterminated phrase string");
		return W_PARSE_ERROR;
	    }
	    if ( ch<' ' ) {
		real_parse_error(parser,"Unprintable character in phrase");
		return W_PARSE_ERROR;
	    }
	    if (ch=='\"' && (i==0 || str[i-1]!='\\')) 
	    {
		str[i]=0;
		*phrase=expand_backslashes(sgrep,str);
		if (!(*phrase) || string_len(*phrase)==0) {
		    real_parse_error(parser,"Empty phrase");		    
		    return W_PARSE_ERROR;
		}		    
		parser->scanner_state=SCANNER_START;
		parser->ch=-1;
		return W_PHRASE;
	    }
	    if ( i==MAX_PHRASE_LENGTH )
	    {
		sgrep_error(sgrep,"%s ( > %d ) %s %d\n%s\n%s\n",
			    "Phrase length exceeds",MAX_PHRASE_LENGTH,"on line",parser->line,
			    "Either you have forgotten the quote  at the end of phrase or",
			    "you have to recompile with greater MAX_PHRASE_LENGTH.");
		real_parse_error(parser,"");
		return W_PARSE_ERROR;
	    }
	    if ( parser->scanner_state==SCANNER_STRING ) str[i++]=ch;
	    break;	    
	    /* This state reads a reserved word (operator) */
	case SCANNER_NAME:
	    if ( ch==0 || ( !isalpha(ch) && ch!='.' && ch!='_' ) )
	    {
		parser->scanner_state=SCANNER_START;
		str[i]=0;
		for(j=0;reserved_words[j].word;j++)
		    if (strcmp(reserved_words[j].word,str)==0) 
			return reserved_words[j].t;
		real_parse_error(parser,"Unknown word");
		return W_PARSE_ERROR;
	    }
	    if (i==MAX_R_WORD)
	    {
		real_parse_error(parser,"Unknown word");
		return W_PARSE_ERROR;
	    }
	    str[i++]=tolower(ch);
	    break;
	    /* This state skips comments */
	case SCANNER_LINE_START_COMMENT:
	    if (parser->column<=6 && 
		("#line ")[parser->column-1]==ch) {
		break;
	    } else if (parser->column==7 && ch>='0' && ch<='9') {
		parser->line=ch-'0'-1;
		parser->scanner_state=SCANNER_LINE_NUMBER_COMMENT;
	    } else {
		parser->scanner_state=SCANNER_COMMENT;
	    }
	    /* Fall through */		
	case SCANNER_COMMENT:
	    if (ch==0) {
		parser->scanner_state=SCANNER_START;
		return W_EOF;
	    } else if (ch=='\n') {
		parser->scanner_state=SCANNER_START;
	    } else if (ch=='l' && parser->column==2) {
		parser->scanner_state=SCANNER_LINE_START_COMMENT;
	    }
	    break;
	case SCANNER_LINE_NUMBER_COMMENT:	       
	    if (ch>='0' && ch<='9') {
		parser->line=parser->line*10+(ch-'0');    
	    } else if (ch==' ') {
		parser->scanner_state=SCANNER_FILE_NAME_COMMENT_START;
	    } else if (ch=='\n') {
		parser->scanner_state=SCANNER_START;
	    } else if (ch==0) {
		parser->scanner_state=SCANNER_START;
		return W_EOF;
	    } else {
		sgrep_error(sgrep,"Warning: '%s':%d: Malformed #line directive.\n",
			    string_to_char(parser->file),parser->errline);
		parser->line=parser->errline;
		parser->scanner_state=SCANNER_COMMENT;
	    }
	    break;
	case SCANNER_FILE_NAME_COMMENT_START:
	    if (ch=='\"') {
		string_clear(parser->file);
		parser->scanner_state=SCANNER_FILE_NAME_COMMENT;
		break;
	    }		
	    sgrep_error(sgrep,"Warning: '%s':%d: Malformed #line directive.\n",
			string_to_char(parser->file),parser->errline);
	    parser->line=parser->errline;
	    if (ch==0) {
		parser->scanner_state=SCANNER_START;
		return W_EOF;
		break;
	    }
	    if (ch=='\n') {
		parser->scanner_state=SCANNER_START;
		break;
	    }
	    parser->scanner_state=SCANNER_COMMENT;
	    break;		
	case SCANNER_FILE_NAME_COMMENT:
	    if (ch=='\"') {
		parser->scanner_state=SCANNER_COMMENT;
		break;
	    }
	    if (ch=='\n' || ch==0) {
		string_clear(parser->file);
		string_cat(parser->file,"[unknown]");
		sgrep_error(sgrep,"Warning: '%s':%d: Malformed #line directive.\n",
			    string_to_char(parser->file),parser->errline);
		parser->line=parser->errline;
		parser->scanner_state=SCANNER_START;
		if (ch==0) return W_EOF;
		break;
	    }
	    string_push(parser->file,ch);
	    break;	        
	    /* We read a number */
	case SCANNER_NUMBER:
	    if ( ch<'0'|| ch>'9' )
	    {
		parser->scanner_state=SCANNER_START;
		str[i]=0;
		*phrase=init_string(sgrep,i,str);
		return W_NUMBER;
	    }
	    if (i==9)
	    {
		real_parse_error(parser,"Too big number");
		return W_PARSE_ERROR;
	    }
	    str[i++]=ch;
	    break;
	}
	
	ch=get_next_char(parser);
    } while (1);

}

/* 
 * Shows a given error message & where it occurred 
 */
void real_parse_error(Parser *parser, char *error)
{
    	char erlin[ELLENGTH+1];
	int i;
	SGREPDATA(parser);
	
	if (parser->errcol-ELLENGTH+SHOWR>0)
		parser->errind-=ELLENGTH-SHOWR;
	else parser->errind-=parser->errcol;
	for(i=0;i<ELLENGTH && 
		parser->expr_str[i+parser->errind] 
		&& parser->expr_str[i+parser->errind]!='\n';
	    i++) {
	    erlin[i]=parser->expr_str[i+parser->errind];
	    if (erlin[i]=='\t') erlin[i]=' ';
	}
	
	erlin[i]=0;
	if (string_len(parser->file)==0) {
	    sgrep_error(parser->sgrep,"Parse error in command line expression");
	} else if (string_to_char(parser->file)[0]=='-') {
	    sgrep_error(parser->sgrep,"Parse error in stdin line %d",
			parser->errline);
	} else {
	    sgrep_error(parser->sgrep,"Parse error in file '%s' line %d",
			string_to_char(parser->file),parser->errline);
	}
        sgrep_error(parser->sgrep," column %d :\n\t%s\n%s\n",
		    parser->errcol,
		    error,erlin); 
	if ( parser->errcol>ELLENGTH-SHOWR ) parser->errcol=ELLENGTH-SHOWR; 
        for (i=0;i<parser->errcol-1;i++) sgrep_error(sgrep," "); 
        sgrep_error(sgrep,"^\n");
}

/*
 * Creates and initializez an operator node to parse tree
 */
ParseTreeNode *create_tree_node(Parser *parser, enum Oper oper)
{
	ParseTreeNode *n;
	SgrepData *sgrep=parser->sgrep;

	assert(parser->nodes>=0);
	if (parser->nodes==sizeof(parser->node_array)/
	    sizeof(ParseTreeNode *)) {
	    parse_error("Suspiciously many tree nodes\n");
	    return NULL;
	}

	n=sgrep_new(ParseTreeNode);
	n->left=NULL;
	n->right=NULL;
	n->parent=NULL;
	n->oper=oper;
	n->number=-1;
	n->leaf=NULL;
	n->label_left=LABEL_NOTKNOWN;
	n->label_right=LABEL_NOTKNOWN;
	n->refcount=0;
	n->result=NULL;

	parser->node_array[parser->nodes++]=n;
	return n;
}
	
/* 
 * Creates and initializes a leaf node to parse tree 
 */
ParseTreeNode *create_leaf_node(Parser *parser, 
				   enum Oper oper, 
				   SgrepString *phrase, int phrase_label)
{
    ParseTreeNode *n;
    SgrepData *sgrep=parser->sgrep;

    /* Create a leaf node */
    n=create_tree_node(parser,oper);
    if (!n) return NULL;
    n->label_left=phrase_label;
    /* Now create a phrase node for the phrase */
    n->leaf=sgrep_new(struct PHRASE_NODE);
    n->leaf->phrase=phrase;
    n->leaf->regions=NULL;
    n->result=NULL;
    return n;
}

ParseTreeNode *new_string_phrase(Parser *parser,
					   SgrepString *phrase, 
					   const char *typestr) {
    ParseTreeNode *n;
    SGREPDATA(parser);

    if (phrase==NULL) {
	phrase=init_string(sgrep,strlen(typestr),typestr);
    } else {
	push_front(phrase,typestr);
    }
    n=create_leaf_node(parser,PHRASE,phrase,LABEL_PHRASE);
    if (!n) {
	delete_string(phrase);
	return NULL;
    }

    /* Put the new phrase node to phrase list */
    n->leaf->next=parser->first_phrase;
    parser->first_phrase=n->leaf;
    return n;
}


/* 
 * Here starts recursive parser 
 */

ParseTreeNode *parse_phrase(Parser *parser, const char *typestr) {
    ParseTreeNode *n;
    NEXT_TOKEN;
    if (token!=W_LPAREN) parse_error("Expecting '('");
    NEXT_TOKEN;
    if (token!=W_PHRASE) parse_error("Expecting phrase string");
    n=new_string_phrase(parser,parser->string_token,typestr);
    if (!n) return NULL;
#if DEBUG
    fprintf(stderr,"parse_phrase '%s'\n",n->leaf->phrase->s);
#endif
    NEXT_TOKEN;
    if (token!=W_RPAREN) parse_error("Expecting ')'");
    NEXT_TOKEN;
    return n;
}


/* production basic_expr->constant_list */
ParseTreeNode *parse_cons_list(Parser *parser)
{
    int s,e,ps,pe;
    char *cons_err="invalid constant region list";
    ParseTreeNode *n;
    RegionList *c_list;
    SGREPDATA(parser);
    
    n=create_leaf_node(parser,PHRASE,NULL,LABEL_CONS);
    if (!n) return NULL;
    NEXT_TOKEN;
    c_list=n->leaf->regions=new_region_list(sgrep);
    
    ps=-1;pe=-1;
    while (token!=W_RBRACK) /* We can fall out here immediately,
			       which means we have empty list */
    {
	if (token!=W_LPAREN)
	    parse_error(cons_err);
	NEXT_TOKEN;
	if (token!=W_NUMBER)
	    parse_error(cons_err);
	s=atoi(parser->string_token->s);
	NEXT_TOKEN;
	if (token!=W_COMMA)
	    parse_error(cons_err);
	NEXT_TOKEN;
	if (token!=W_NUMBER)
	    parse_error(cons_err);
	e=atoi((char *)(parser->string_token->s));
	NEXT_TOKEN;
	if (token!=W_RPAREN)
	    parse_error(cons_err);
	if (e<s)
	    parse_error("region end point must be greater than start point");
	NEXT_TOKEN;
	if (s<ps || (s==ps && e<=pe) )
	    parse_error("constant gc list must be sorted");
	if (e<=pe || s==ps)
	{
	    /* nesting detected */
	    c_list->nested=1;
	}
	add_region(c_list,s,e);
	ps=s;pe=e;
    }
    parser->sgrep->statistics.constant_lists++;
    NEXT_TOKEN;
    return n;
}

/* Parses function(integer,expression) */
ParseTreeNode *parse_integer_function(Parser *parser,
				      enum Oper oper,
				      const char *name) {
    ParseTreeNode *n=create_tree_node(parser, oper);
    if (!n) return NULL;
    /* Let's parse the parameters */
    NEXT_TOKEN;
    if (token!=W_LPAREN)
	parse_error("( expected");
    NEXT_TOKEN;
    if (token!=W_NUMBER) {
	char buf[1000];
	sprintf(buf,"integer expected: %s(integer,expression)",name);
	parse_error(buf);
    }
    n->number=atoi((char *)parser->string_token->s);
    delete_string(parser->string_token);
    parser->string_token=NULL;
    if (n->number<0)
	parse_error("Negative ints not implemented for last");
    NEXT_TOKEN;
    if (token!=W_COMMA) {
	char buf[100];
	sprintf(buf,"',' expected: %s(integer, expression)",name);
	parse_error(buf);
	return NULL;
    }
    NEXT_TOKEN;
    n->left=parse_reg_expr(parser);
    if (n->left==NULL) return NULL;
    n->right=NULL;
    if (token!=W_RPAREN)
	parse_error(") expected");
    NEXT_TOKEN;
    return n;
}

ParseTreeNode *parse_int_oper_argument(Parser *parser,
				       enum Oper oper) {
    ParseTreeNode *n=create_tree_node(parser,oper);
    if (!n) return NULL;
    NEXT_TOKEN;
    if (token!=W_LPAREN) {
	parse_error("Expecting '(' starting integer argument for operator");
	return NULL;    
    }
    NEXT_TOKEN;
    if (token!=W_NUMBER) {
	parse_error("Expecting integer argument for operator");
	return NULL;
    }
    n->number=atoi(parser->string_token->s);
    if (n->number<0) {
	parse_error("Expecting integer value >=0");
	return NULL;
    }
    delete_string(parser->string_token);
    NEXT_TOKEN;
    if (token!=W_RPAREN) {
	parse_error("')' expected");
    }
    return n;    
}

/* Which kind of basic expressions */			
ParseTreeNode *parse_basic_expr(Parser *parser) {
    ParseTreeNode *n;
    
    switch (token) {
    case W_LBRACK:
	/* We got a constant region list */
	return parse_cons_list(parser);	
    case W_CHARS:
	/* reserved wors 'chars' was found */
	parse_error("'chars' disabled until I figure out how to fix it (JJ)");
	n=create_leaf_node(parser,PHRASE,NULL,LABEL_CHARS);
	if (!n) return NULL;
	/* we use already created list (see create_constant_lists() )*/
	n->leaf->regions=parser->sgrep->chars_list;
	NEXT_TOKEN;
	return n;
    case W_LPAREN:
	NEXT_TOKEN;
	n=parse_reg_expr(parser);
	if (n==NULL) return NULL;
	if (token!=W_RPAREN)
	    parse_error(") expected");
	NEXT_TOKEN;
	return n;
    case W_OUTER:
    case W_INNER:
    case W_CONCAT:
	/* So we have function. Let's create a tree node for it */
	n=create_tree_node(parser, 
			   (token==W_OUTER) ? OUTER:
			   ((token==W_INNER) ? INNER:CONCAT) ); 
	if (!n) return NULL;
	/* Let's parse the parameter */
	NEXT_TOKEN;
	if (token!=W_LPAREN)
	    parse_error("( expected");
	NEXT_TOKEN;
	n->left=parse_reg_expr(parser);
	if (n->left==NULL) return NULL;
	n->right=NULL; /* Function has only one parameter */
	if (token!=W_RPAREN)
	    parse_error(") expected");
	NEXT_TOKEN;
	return n;
    case W_JOIN:
	return parse_integer_function(parser,JOIN,"join");
    case W_FIRST:
	return parse_integer_function(parser,FIRST,"first");
    case W_LAST:
	return parse_integer_function(parser,LAST,"last");
    case W_FIRST_BYTES:
	return parse_integer_function(parser,FIRST_BYTES,"firs_bytes");
    case W_LAST_BYTES:
	return parse_integer_function(parser,LAST_BYTES,"last_bytes");
	/* phrases */
    case W_PHRASE:
	/* A simple phrase */
	n=new_string_phrase(parser,parser->string_token, "n");
	if (!n) return NULL;
	NEXT_TOKEN;
	return n;
    case W_START:
	/* "start", first of possible regions */
	n=new_string_phrase(parser,NULL,"#start");
	if (!n) return NULL;
	NEXT_TOKEN;
	return n;
    case W_END:
	/* "end", last of possible regions */
	n=new_string_phrase(parser,NULL, "#end");
	if (!n) return NULL;
	NEXT_TOKEN;
	return n;
    case W_COMMENT: 
	n=new_string_phrase(parser,NULL,"-");
	if (!n) return NULL;
	NEXT_TOKEN;
	return n;
    case W_ELEMENTS:
	n=new_string_phrase(parser,NULL,"@elements");
	if (!n) return NULL;
	NEXT_TOKEN;
	return n;
    case W_PROLOG:
	n=new_string_phrase(parser,NULL,"d!");
	if (!n) return NULL;
	NEXT_TOKEN;
	return n;
    case W_CDATA:
	n=new_string_phrase(parser,NULL,"[CDATA");
	if (!n) return NULL;
	NEXT_TOKEN;
	return n;
	/* phrases with argument */
    case W_RAW:
	return parse_phrase(parser,"");
    case W_FILE:
	return parse_phrase(parser,"f");
    case W_STRING: 
	return parse_phrase(parser,"n");
    case W_REGEX:
	return parse_phrase(parser,"r");
    case W_DOCTYPE:
	return parse_phrase(parser, "dn");
    case W_DOCTYPE_PID: 
	return parse_phrase(parser, "dp");
    case W_DOCTYPE_SID:
	return parse_phrase(parser,"ds");
    case W_ENTITY_DECLARATION:
	return parse_phrase(parser,"!ed");
    case W_ENTITY_LITERAL:
	return parse_phrase(parser,"!el");
    case W_ENTITY_PID:
	return parse_phrase(parser,"!ep");
    case W_ENTITY_SID:
	return parse_phrase(parser,"!es");
    case W_ENTITY_NDATA:
	return parse_phrase(parser,"!en");
    case W_PI:
	return parse_phrase(parser,"?");	    
    case W_ATTRIBUTE:
	return parse_phrase(parser,"a");
    case W_ATTVALUE:
	return parse_phrase(parser,"v");	    
    case W_STAG: 
	return parse_phrase(parser,"s");
    case W_ETAG:
	return parse_phrase(parser,"e");
    case W_COMMENT_WORD:
	return parse_phrase(parser,"c");	    
    case W_WORD: 
	return parse_phrase(parser,"w");
    case W_ENTITY:
	return parse_phrase(parser,"&");

	
    default:
	parse_error("Basic expression expected\n");
    }

    /* Dummy return for keeping c++ compilers quiet */
    return NULL;
}

/* Which kind region_expression */
ParseTreeNode *parse_reg_expr(Parser *parser)
{
	ParseTreeNode *left;
	if (token==W_EOF)
		parse_error("Unexpected end of expression");

	left=parse_basic_expr(parser);
	if (left==NULL) return NULL;
	if ( token==W_EOF || token==W_RPAREN ) return left;
	return parse_oper_expr(parser,left);
}	

/* Which kind of operator expression */
ParseTreeNode *parse_oper_expr(Parser *parser,ParseTreeNode *left)
{
	ParseTreeNode *o=NULL;
	switch (token)
	{
	case W_NEAR:
	        o=parse_int_oper_argument(parser,NEAR);
		break;
	case W_NEAR_BEFORE:
	        o=parse_int_oper_argument(parser,NEAR_BEFORE);
		break;
	case W_IN:		
		o=create_tree_node(parser,IN);
		break;
	case W_CONTAINING:	
		o=create_tree_node(parser,CONTAINING);
		break;
/* PK Febr 95 */
	case W_EQUAL:	
		o=create_tree_node(parser,EQUAL);
		break;
/* PK Febr 95 */
	case W_PARENTING:
	        o=create_tree_node(parser,PARENTING);
                break;
	case W_CHILDRENING:
	        o=create_tree_node(parser,CHILDRENING);
		break;
	case W_OR: 		
		o=create_tree_node(parser,OR);
		break;
	case W_ORDERED: 
		o=create_tree_node(parser,ORDERED);
		break;
	case W_L_ORDERED:
		o=create_tree_node(parser,L_ORDERED);
		break;
	case W_R_ORDERED:
		o=create_tree_node(parser,R_ORDERED);
		break;
	case W_LR_ORDERED:
		o=create_tree_node(parser,LR_ORDERED);
		break;
	case W_EXTRACTING:
		o=create_tree_node(parser,EXTRACTING);
		break;
	case W_QUOTE:
		o=create_tree_node(parser,QUOTE);
		break;
	case W_R_QUOTE:
		o=create_tree_node(parser,R_QUOTE);
		break;
	case W_L_QUOTE:
		o=create_tree_node(parser,L_QUOTE);
		break;
	case W_LR_QUOTE:
		o=create_tree_node(parser,LR_QUOTE);
		break;
	case W_NOT:
		NEXT_TOKEN;
		if (token==W_CONTAINING) o=create_tree_node(parser,NOT_CONTAINING);
		else if (token==W_IN) o=create_tree_node(parser,NOT_IN);
		else if (token==W_EQUAL) o=create_tree_node(parser,NOT_EQUAL);
		else parse_error("'not' must be followed by 'in', 'containing' or 'equal'");
		break;
	default:
		parse_error("Operator expected");
	}
	if (!o) return NULL;
	NEXT_TOKEN;
	o->right=parse_basic_expr(parser);
	if (o->right==NULL) return NULL;
	o->left=left;
	if (token==W_EOF || token==W_RPAREN) return o;
	return parse_oper_expr(parser,o);
}		

/* End of recursive parser */

/*
 * Parses the given command string. Returns root node of the parse tree.
 * phrase_list returns pointer to phrase list 
 */
ParseTreeNode *parse_string(SgrepData *sgrep,const char *str, 
	struct PHRASE_NODE **phrase_list)
{
	ParseTreeNode *root;
	Parser parser_l;
	Parser *parser=&parser_l;
	
	parser->sgrep=sgrep;
	parser->line=1;
	parser->column=0;
	parser->file=new_string(sgrep,8);
	parser->expr_str=str;
	parser->expr_ind=0;
	parser->ch=-1;
	parser->first_phrase=NULL;
	parser->scanner_state=SCANNER_START;
	parser->nodes=0;

	NEXT_TOKEN;
	root=parse_reg_expr(parser);

	if (token==W_RPAREN && root!=NULL)
	{
		real_parse_error(parser,"Too many ')'s");
		root=NULL;
	}
	delete_string(parser->file);
	
	if (token!=W_EOF)
	{
	    root=NULL;
	}
	if (root==NULL) {
	    /* Free whatever was parsed before parse error */
	    while(parser->nodes) {
		ParseTreeLeaf *leaf;
		
		parser->nodes--;
		leaf=parser->node_array[parser->nodes]->leaf;
		if (leaf) {
		    if (leaf->phrase) {
			delete_string(leaf->phrase);
		    }
		    sgrep_free(leaf);
		}
		sgrep_free(parser->node_array[parser->nodes]);
	    }
	}
	

#ifdef DEBUG
	print_tree(root);
	fprintf(stderr,"\n");
#endif	
	*phrase_list=parser->first_phrase;
	return root;	
}


void free_parse_tree(SgrepData *sgrep, ParseTreeNode *root) {
    assert(root->oper!=INVALID && root->refcount!=0);
    if (root->refcount==-1) {
	assert(root->oper==PHRASE && root->leaf && root->leaf->phrase==NULL);
	root->refcount=0;
	sgrep_free(root->leaf);
	sgrep_free(root);
    } else {
	root->refcount--;
	if (root->refcount==0) {
	    if (root->left) free_parse_tree(sgrep,root->left);
	    if (root->right) free_parse_tree(sgrep,root->right);
	    if (root->oper==PHRASE) {
		assert(root->leaf);
		delete_string(root->leaf->phrase);
		sgrep_free(root->leaf);
		root->leaf=NULL;
	    }
	    root->oper=INVALID;
	    sgrep_free(root);
	}
    }
}
	
	
#ifdef DEBUG
void print_tree(ParseTreeNode *root)
{
	if ( root==NULL )
	{
		fprintf(stderr,"\nprint_tree: got NULL node\n");
		exit(3);
	}
	if ( root->oper==PHRASE )
	{
		fprintf(stderr," \"%s\"",root->leaf->phrase->s);
		return;
	}
	if (root->oper<0 || root->oper>R_WORDS)
	{
		printf("\nprint tree: got invalid oper (%d)\n",root->oper);
		exit(3);
	}
	
	if (root->right!=NULL)
	{
		fprintf(stderr," (");
		print_tree(root->left);
	
		fprintf(stderr," %s",r_word_str[root->oper]);
	
		print_tree(root->right);
		fprintf(stderr," )");
	} else
	{
		fprintf(stderr," %s(",r_word_str[root->oper]);
		print_tree(root->left);
		fprintf(stderr," )");
	}
}
#endif