-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.c
1268 lines (1204 loc) · 32.1 KB
/
parser.c
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 (C) 1999-2012 Core Technologies.
//
// This file is part of tpasm.
//
// tpasm is free software; you can redistribute it and/or modify
// it under the terms of the tpasm LICENSE AGREEMENT.
//
// tpasm 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
// tpasm LICENSE AGREEMENT for more details.
//
// You should have received a copy of the tpasm LICENSE AGREEMENT
// along with tpasm; see the file "LICENSE.TXT".
// Parse a line in various ways
// NOTE: All of the "Parse..." functions here will skip whitespace before
// they start looking for whatever it is they are meant to find. The
// caller can rely on this behavior. Even if a Parse function returns
// false, it is will have modified the line index to point past any
// whitespace that was at the start.
#include "include.h"
static inline bool IsWhiteChar(char character)
// return true if character is white-space, false if not
{
return(character==' '||character=='\t');
}
static inline bool IsCommaSeparator(char character)
// see if character is a list separator
{
return(character==',');
}
bool IsStartLabelChar(char character)
// return true if character is a character that can be used at the start of a label
{
return((character>='A'&&character<='Z')||(character>='a'&&character<='z')||(character=='_'));
}
bool IsLabelChar(char character)
// return true if character is a character that can be used anywhwere in a label
{
return(IsStartLabelChar(character)||(character>='0'&&character<='9'));
}
static inline bool IsNameChar(char character)
// return true if character is alpha, numeric, dot, or underscore), false if not
{
return((character>='A'&&character<='Z')||(character>='a'&&character<='z')||(character>='0'&&character<='9')||character=='.'||character=='_');
}
const TOKEN *MatchBuriedToken(const char *string,unsigned int *index,const TOKEN *list)
// Try to match characters of string starting at index against the list of tokens,
// if a match is made, move index to the end of the match in string,
// and return the matched token.
// if nothing is matched, return NULL
{
unsigned int
i,j;
const char
*stringA,
*stringB;
const TOKEN
*match;
match=NULL;
i=0;
while(!match&&(list[i].token[0]!='\0')) // if nothing found yet, and not at end of list
{
stringA=&string[*index];
stringB=&list[i].token[0];
j=0;
while((*stringA)&&((*stringA)==(*stringB)))
{
stringA++;
stringB++;
j++;
}
if(!(*stringB)) // got to end of token?
{
(*index)+=j; // push forward in the string past the matched data
match=&list[i];
}
i++;
}
return(match);
}
bool SkipWhiteSpace(const char *line,unsigned int *lineIndex)
// push lineIndex past any white space in line
// If there was no white space, return false
{
if(IsWhiteChar(line[*lineIndex]))
{
while(IsWhiteChar(line[++(*lineIndex)]))
;
return(true);
}
return(false);
}
static inline bool IsCommentStart(const char *line,unsigned int *lineIndex)
// see if a comment is starting at the given location, or if the line is ending
{
return(line[*lineIndex]=='\0'||line[*lineIndex]==';'||(line[*lineIndex]=='/'&&line[(*lineIndex)+1]=='/')); // comment starting, or end of line?
}
bool ParseComment(const char *line,unsigned int *lineIndex)
// Return true if line is empty, or contains a comment.
// Do not push lineIndex past the start of the comment.
{
SkipWhiteSpace(line,lineIndex);
return(IsCommentStart(line,lineIndex));
}
bool ParseNonWhiteSpace(const char *line,unsigned int *lineIndex,char *string)
// Extract the next non-white, non-comment characters from the line and return them in string.
{
unsigned int
outputIndex;
unsigned int
localIndex;
SkipWhiteSpace(line,lineIndex);
localIndex=*lineIndex; // keep index here for a while
outputIndex=0;
while(!IsWhiteChar(line[localIndex])&&!IsCommentStart(line,&localIndex)) // get everything non-white and non-comment
{
string[outputIndex++]=line[localIndex++];
}
if(outputIndex)
{
string[outputIndex]='\0';
*lineIndex=localIndex;
return(true);
}
return(false); // nothing
}
bool ParseName(const char *line,unsigned int *lineIndex,char *string)
// Parse sequences of name characters
{
unsigned int
outputIndex;
unsigned int
localIndex;
SkipWhiteSpace(line,lineIndex);
localIndex=*lineIndex; // keep index here for a while
outputIndex=0;
while(IsNameChar(line[localIndex])&&!IsCommentStart(line,&localIndex))
{
string[outputIndex++]=line[localIndex++];
}
if(outputIndex)
{
string[outputIndex]='\0';
*lineIndex=localIndex;
return(true);
}
return(false); // nothing found
}
bool ParseNonName(const char *line,unsigned int *lineIndex,char *string)
// Extract the next non-white, non-comment, non-name sequence of characters.
{
unsigned int
outputIndex;
unsigned int
localIndex;
SkipWhiteSpace(line,lineIndex);
localIndex=*lineIndex; // keep index here for a while
outputIndex=0;
while(!IsWhiteChar(line[localIndex])&&!IsNameChar(line[localIndex])&&!IsCommentStart(line,&localIndex))
{
string[outputIndex++]=line[localIndex++];
}
if(outputIndex)
{
string[outputIndex]='\0';
*lineIndex=localIndex;
return(true);
}
return(false); // nothing found
}
bool ParseFunction(const char *line,unsigned int *lineIndex,char *functionName)
// Attempt to parse a something which looks like a function
// call (label characters followed by an '(')
// leave lineIndex pushed past the last thing successfully parsed.
// If this finds something, it pushes lineIndex past the open paren,
// and collects the function name in functionName.
{
unsigned int
outputIndex;
unsigned int
localIndex;
SkipWhiteSpace(line,lineIndex);
localIndex=*lineIndex; // keep index here for a while
outputIndex=0;
if(IsStartLabelChar(line[localIndex])) // verify that the first character looks like a label
{
do
{
functionName[outputIndex++]=line[localIndex++];
}
while(IsLabelChar(line[localIndex]));
functionName[outputIndex]='\0'; // terminate the function name just collected
SkipWhiteSpace(line,&localIndex); // move past any white space
if(line[localIndex]=='(') // open paren (function starting?)
{
localIndex++;
*lineIndex=localIndex; // update output index
return(true); // return the label
}
}
return(false);
}
bool ParseLabelString(const char *line,unsigned int *lineIndex,char *label,bool *isLocal)
// Attempt to parse a label from line at lineIndex.
// Leave lineIndex pushed past the last thing successfully parsed
// If the label is recognized as local (because it begins with a '.' or an '@'),
// this will adjust it for the current scope.
{
unsigned int
outputIndex;
unsigned int
localIndex;
SkipWhiteSpace(line,lineIndex);
localIndex=*lineIndex; // keep index here for a while
outputIndex=0;
*isLocal=false;
if(line[localIndex]=='.') // see if local label
{
sprintf(label,"%s@",scope);
outputIndex=strlen(label); // move to the end
localIndex++;
*isLocal=true;
}
else if(line[localIndex]=='@') // see if macro local label
{
sprintf(label,"%s@%d@",scope,scopeValue);
outputIndex=strlen(label); // move to the end
localIndex++;
*isLocal=true;
}
if(IsStartLabelChar(line[localIndex])) // verify that the first character looks like a label
{
do
{
label[outputIndex++]=line[localIndex++];
}
while(IsLabelChar(line[localIndex]));
label[outputIndex]='\0'; // terminate the label we just collected
*lineIndex=localIndex; // update output index
return(true); // return the label
}
return(false); // no label found
}
bool ParseLabel(const char *line,unsigned int *lineIndex,PARSED_LABEL *parsedLabel)
// Attempt to parse a label from line at lineIndex, filling in parsedLabel.
// Leave lineIndex pushed past the last thing successfully parsed
// If the label is recognized as local (because it begins with a '.' or an '@'),
// this will adjust it for the current scope.
{
return(ParseLabelString(line,lineIndex,&parsedLabel->name[0],&parsedLabel->isLocal));
}
static bool GetRadixDigitValue(char digit,unsigned int radix,int *value)
// Return value as a binary representation of the value of digit given in
// radix
// NOTE: if digit is not valid for radix, return false
{
if(digit>='a')
{
digit-='a'-'A'; // pull back to upper case
}
if((digit>='0'&&digit<='9')||(digit>='A'&&digit<='Z'))
{
digit-='0'; // make binary
if(digit>9)
{
digit-='A'-'0'-10;
}
if(digit<(int)radix)
{
*value=digit;
return(true);
}
}
return(false);
}
static bool ParseRadixNumber(const char *line,unsigned int *lineIndex,unsigned int radix,int *value)
// read at least one digit in the given radix, assemble it into outValue
// if no digits are located, return false
{
unsigned int
numDigits;
int
outDigit;
char
inChar;
SkipWhiteSpace(line,lineIndex);
*value=0;
numDigits=0;
while((inChar=line[*lineIndex])&&GetRadixDigitValue(inChar,radix,&outDigit))
{
(*lineIndex)++;
(*value)=((*value)*radix)+outDigit;
numDigits++;
}
if(numDigits)
{
return(true);
}
return(false);
}
static bool ParseQuotedNumber(const char *line,unsigned int *lineIndex,unsigned int radix,int *value)
// a quoted number with a given radix is arriving, so parse it into element
// if there is a problem, return false
{
unsigned int
inputIndex;
SkipWhiteSpace(line,lineIndex);
inputIndex=*lineIndex;
if(line[inputIndex]=='\'')
{
inputIndex++;
if(ParseRadixNumber(line,&inputIndex,radix,value))
{
if(line[inputIndex]=='\'')
{
*lineIndex=inputIndex+1;
return(true);
}
}
}
return(false);
}
static bool ParseDecimalOrPostfixNumber(const char *line,unsigned int *lineIndex,int *value)
// The next thing on the line should be interpreted as a decimal number,
// or a number with a postfix radix specifier.
// parse it into value
// if there is a problem (does not look like a proper decimal or postfix number), return false
{
unsigned int
inputIndex;
bool
done;
unsigned int
i;
unsigned int
radix;
SkipWhiteSpace(line,lineIndex);
inputIndex=*lineIndex;
// first run to the end of the string of digits and letters, attempting to locate
// a radix specifier
i=*lineIndex;
done=false;
while(!done)
{
if((line[i]>='0'&&line[i]<='9')|| // decimal digit
((line[i]>='A'&&line[i]<='F')||(line[i]>='a'&&line[i]<='f'))|| // hex digit, or binary or decimal specifier
(line[i]=='H'||line[i]=='h'||line[i]=='O'||line[i]=='o')) // hex specifier, or octal specifier
{
i++; // step over it
}
else
{
done=true; // something which is none of the above
}
}
if(line[i-1]>='0'&&line[i-1]<='9') // raw decimal number
{
if(ParseRadixNumber(line,&inputIndex,10,value)) // fetch in the decimal number
{
if(inputIndex==i) // verify we have read all the digits (nothing funky in-between)
{
*lineIndex=inputIndex; // push to the end of the number
return(true);
}
}
}
else
{
switch(line[i-1]) // look at the specifier
{
case 'b':
case 'B':
radix=2;
break;
case 'o':
case 'O':
radix=8;
break;
case 'd':
case 'D':
radix=10;
break;
case 'h':
case 'H':
radix=16;
break;
default:
return(false); // something other than a digit or specifier at the end of the string
break;
}
if(ParseRadixNumber(line,&inputIndex,radix,value)) // fetch in the number
{
if(inputIndex==(i-1)) // verify we have read up until the radix specifier (nothing funky in between)
{
*lineIndex=inputIndex+1; // push past the specifier
return(true);
}
}
}
return(false);
}
bool ParseQuotedString(const char *line,unsigned int *lineIndex,char startQuote,char endQuote,char *string,unsigned int *stringLength)
// Parse a quoted string (if none can be found, return false)
// NOTE: if a quoted string contains \'s, they will quote the next character, and possibly interpret it
// \a, \b, \f, \n, \r, \t, \v. \x## and \0## are interpreted
// NOTE: the outer quotes are not returned in string
// NOTE: since the string could contain NULs, the length is returned
{
unsigned int
outputIndex;
unsigned int
localIndex;
int
digitValue,
value;
SkipWhiteSpace(line,lineIndex);
outputIndex=0;
if(line[*lineIndex]==startQuote)
{
localIndex=(*lineIndex)+1; // skip over the start quote
while((line[localIndex]!=endQuote)&&line[localIndex])
{
if(line[localIndex]!='\\')
{
string[outputIndex++]=line[localIndex++];
}
else
{
localIndex++; // skip over the '\'
if(line[localIndex])
{
switch(line[localIndex])
{
case 'a': // alert
string[outputIndex++]='\a';
localIndex++;
break;
case 'b': // backspace
string[outputIndex++]='\b';
localIndex++;
break;
case 'f': // form feed
string[outputIndex++]='\f';
localIndex++;
break;
case 'n': // newline
string[outputIndex++]='\n';
localIndex++;
break;
case 'r': // return
string[outputIndex++]='\r';
localIndex++;
break;
case 't': // tab
string[outputIndex++]='\t';
localIndex++;
break;
case 'v': // vertical tab
string[outputIndex++]='\v';
localIndex++;
break;
case 'x': // hex number
localIndex++; // skip the introduction
value=0;
while(line[localIndex]&&GetRadixDigitValue(line[localIndex],16,&digitValue))
{
localIndex++;
value=value*16+digitValue;
}
string[outputIndex++]=value&0xFF;
break;
case '0': // octal number
localIndex++; // skip the introduction
value=0;
while(line[localIndex]&&GetRadixDigitValue(line[localIndex],8,&digitValue))
{
localIndex++;
value=value*8+digitValue;
}
string[outputIndex++]=value&0xFF;
break;
default:
string[outputIndex++]=line[localIndex++];
break;
}
}
}
}
if(line[localIndex]==endQuote) // make sure it ended
{
string[outputIndex]='\0'; // terminate the string
*stringLength=outputIndex; // return the length (excluding the termination we added)
*lineIndex=localIndex+1; // skip over the terminating quote
return(true);
}
}
return(false); // none found
}
bool ParseQuotedStringVerbatim(const char *line,unsigned int *lineIndex,char startQuote,char endQuote,char *string)
// Parse a quoted string (if none can be found, return false)
// NOTE: if a quoted string contains \'s, they will quote the next character, but will be left in
// the output, along with an unmodified next character
// NOTE: the outer quotes are not returned in string
{
unsigned int
outputIndex;
unsigned int
localIndex;
SkipWhiteSpace(line,lineIndex);
outputIndex=0;
if(line[*lineIndex]==startQuote)
{
localIndex=(*lineIndex)+1; // skip over the start quote
while((line[localIndex]!=endQuote)&&line[localIndex])
{
string[outputIndex++]=line[localIndex++];
if(line[localIndex-1]=='\\')
{
if(line[localIndex])
{
string[outputIndex++]=line[localIndex++];
}
}
}
if(line[localIndex]==endQuote) // make sure it ended
{
string[outputIndex]='\0'; // terminate the string
*lineIndex=localIndex+1; // skip over the terminating quote
return(true);
}
}
return(false); // none found
}
static bool ParseASCIIConstant(const char *line,unsigned int *lineIndex,int *value)
// parse ascii data into a number as an ascii constant
// if there is a problem, return false
{
unsigned int
inputIndex;
unsigned int
i;
char
string[MAX_STRING];
unsigned int
stringLength;
inputIndex=*lineIndex;
if(ParseQuotedString(line,&inputIndex,'\'','\'',string,&stringLength))
{
(*value)=0;
i=0;
while(i<stringLength)
{
(*value)<<=8;
(*value)|=string[i];
i++;
}
*lineIndex=inputIndex;
return(true);
}
return(false);
}
bool ParseNumber(const char *line,unsigned int *lineIndex,int *value)
// Skip white space and attempt to parse a number from the line
// The following forms are supported:
//
// ### decimal
// 'cccc' for ascii
//
// C style
// 0b### binary
// 0o### octal
// 0d### decimal
// 0x### hex
//
// Microchip style
// A'###' ascii
// B'###' binary
// O'###' octal
// D'###' decimal
// H'###' hex
//
// Intel style
// ###b binary
// ###B binary
// ###o octal
// ###O octal
// ###d decimal
// ###D decimal
// ###h hex (first digit must be 0-9)
// ###H hex (first digit must be 0-9)
//
// Motorola style
// %### binary
// .### decimal
// $### hex
//
// return the number found with lineIndex updated, or false if none located
{
unsigned int
localIndex;
SkipWhiteSpace(line,lineIndex);
switch(line[*lineIndex])
{
case '0': // leading 0?
switch(line[(*lineIndex)+1])
{
case 'b': // could be 0b (0 in binary), OR 0b1101 (binary 1101) OR 0b3cFh (0b3cf in hex)
if(ParseDecimalOrPostfixNumber(line,lineIndex,value))
{
return(true);
}
else
{
localIndex=(*lineIndex)+2; // skip over the prefix
if(ParseRadixNumber(line,&localIndex,2,value)) // try it as binary
{
(*lineIndex)=localIndex;
return(true);
}
else
{
return(false);
}
}
break;
case 'o': // could be 0o (0 in octal), OR 0o777 (octal 777)
if(ParseDecimalOrPostfixNumber(line,lineIndex,value))
{
return(true);
}
else
{
localIndex=(*lineIndex)+2; // skip over the prefix
if(ParseRadixNumber(line,&localIndex,8,value)) // try it as octal
{
(*lineIndex)=localIndex;
return(true);
}
else
{
return(false);
}
}
break;
case 'd': // could be 0d (0 in decimal), OR 0d1234 (decimal 1234) OR 0d45h (0d45 in hex)
if(ParseDecimalOrPostfixNumber(line,lineIndex,value))
{
return(true);
}
else
{
localIndex=(*lineIndex)+2; // skip over the prefix
if(ParseRadixNumber(line,&localIndex,10,value)) // try it as decimal
{
(*lineIndex)=localIndex;
return(true);
}
else
{
return(false);
}
}
break;
case 'x':
localIndex=(*lineIndex)+2; // skip over the prefix
if(ParseRadixNumber(line,&localIndex,16,value)) // try it as hex
{
(*lineIndex)=localIndex;
return(true);
}
else
{
return(false);
}
break;
default:
return(ParseDecimalOrPostfixNumber(line,lineIndex,value));
break;
}
break;
case 'A':
if(line[(*lineIndex)+1]=='\'')
{
localIndex=(*lineIndex)+1; // skip over the prefix
if(ParseASCIIConstant(line,&localIndex,value)) // see if we actually got a constant
{
*lineIndex=localIndex;
return(true);
}
return(false);
}
break;
case 'B':
localIndex=(*lineIndex)+1; // skip over the prefix
if(ParseQuotedNumber(line,&localIndex,2,value))
{
*lineIndex=localIndex;
return(true);
}
else
{
return(false);
}
break;
case 'O':
localIndex=(*lineIndex)+1; // skip over the prefix
if(ParseQuotedNumber(line,&localIndex,8,value))
{
*lineIndex=localIndex;
return(true);
}
else
{
return(false);
}
break;
case 'D':
localIndex=(*lineIndex)+1; // skip over the prefix
if(ParseQuotedNumber(line,&localIndex,10,value))
{
*lineIndex=localIndex;
return(true);
}
else
{
return(false);
}
break;
case 'H':
localIndex=(*lineIndex)+1; // skip over the prefix
if(ParseQuotedNumber(line,&localIndex,16,value))
{
*lineIndex=localIndex;
return(true);
}
else
{
return(false);
}
break;
case '$':
localIndex=(*lineIndex)+1; // skip over the prefix
if(ParseRadixNumber(line,&localIndex,16,value)) // try it as hex
{
(*lineIndex)=localIndex;
return(true);
}
else
{
return(false);
}
break;
case '.':
localIndex=(*lineIndex)+1; // skip over the prefix
if(ParseRadixNumber(line,&localIndex,10,value)) // try it as decimal
{
(*lineIndex)=localIndex;
return(true);
}
else
{
return(false);
}
break;
case '%':
localIndex=(*lineIndex)+1; // skip over the prefix
if(ParseRadixNumber(line,&localIndex,2,value)) // try it as binary
{
(*lineIndex)=localIndex;
return(true);
}
else
{
return(false);
}
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return(ParseDecimalOrPostfixNumber(line,lineIndex,value));
break;
case '\'':
return(ParseASCIIConstant(line,lineIndex,value));
break;
}
return(false);
}
bool ParseParentheticString(const char *line,unsigned int *lineIndex,char *string)
// Parse out a string which is enclosed in parenthesis
// line must begin with an open paren, and will be
// placed into string until a matching close paren is seen
// if a comment, this will stop parsing, and
// return false
// NOTE: this understands that there may be quoted material between the
// parenthesis, and abides by the quotes
// NOTE: the parenthesis that enclose the string are not included in
// the parsed output
{
bool
fail;
unsigned int
initialIndex,
workingIndex;
unsigned int
parenDepth;
unsigned int
stringLength;
fail=false;
parenDepth=0;
SkipWhiteSpace(line,lineIndex);
if(line[*lineIndex]=='(') // open paren?
{
parenDepth++;
initialIndex=workingIndex=(*lineIndex)+1;
while(parenDepth&&!ParseComment(line,&workingIndex))
{
switch(line[workingIndex])
{
case '(':
parenDepth++;
break;
case ')':
parenDepth--;
break;
case '\'':
fail=!ParseQuotedString(line,&workingIndex,'\'','\'',string,&stringLength);
break;
case '"':
fail=!ParseQuotedString(line,&workingIndex,'"','"',string,&stringLength);
break;
}
workingIndex++;
}
if(!parenDepth) // finished?
{
*lineIndex=workingIndex;
workingIndex--; // step back off the last parenthesis
strncpy(string,&line[initialIndex],workingIndex-initialIndex);
string[workingIndex-initialIndex]='\0';
}
else
{
fail=true;
}
}
else
{
fail=true;
}
return(!fail);
}
bool ParseLabelDefinition(const char *line,unsigned int *lineIndex,PARSED_LABEL *parsedLabel)
// Attempt to parse a label from line at lineIndex, filling in parsedLabel.
// leave lineIndex pushed past the last thing successfully parsed
// If the label is recognized as local, this will adjust it for the
// current scope.
{
if(ParseLabel(line,lineIndex,parsedLabel))
{
if(line[*lineIndex]==':') // if there is a colon after the label, move past it
{
(*lineIndex)++;
}
return(true); // return the label
}
return(false); // no label found
}
bool ParseCommaSeparator(const char *line,unsigned int *lineIndex)
// Expect a comma, step over one if found
{
SkipWhiteSpace(line,lineIndex);
if(IsCommaSeparator(line[*lineIndex])) // does this look like a separator?
{
(*lineIndex)++; // step over it
return(true);
}
return(false);
}
bool ParseFirstListElement(const char *line,unsigned int *lineIndex,char *element)
// Parse the first element from a comma separated list
// NOTE: a list element is anything which is non-white at the start,
// non-white at the end, and which is not a comma, or a comment character
// NOTE: this will pay attention to quotes in the list element, and WILL
// allow comment characters or comma's to be embedded in elements
// as long as they are quoted
// false is returned if no element could be located
// NOTE: if the list separator is encountered before any list
// element characters are seen, an empty element is returned
// NOTE: the list separator which marks the end of the element is NOT used up by
// this routine, and is left around for calls to ParseNextListElement
{
bool
done;
char
character;
unsigned int
outputIndex;
outputIndex=0;
if(!ParseComment(line,lineIndex))
{
done=false;
while(!done&&!IsCommentStart(line,lineIndex))
{
character=line[*lineIndex];
if(!IsCommaSeparator(character))
{
switch(character)
{
case '"': // start of a double quoted string
case '\'': // start of a single quoted string
element[outputIndex++]=character;
if(ParseQuotedStringVerbatim(line,lineIndex,character,character,&element[outputIndex]))
{
outputIndex+=strlen(&element[outputIndex]); // push forward
element[outputIndex++]=character;
}
else
{
// no final matching quote, so just get it all
(*lineIndex)++;
while(line[*lineIndex])
{
element[outputIndex++]=line[(*lineIndex)++];
}
}
break;
case '\\': // quoting the next single character?
(*lineIndex)++;
if(line[*lineIndex]) // if not end of line, then pull into the element
{
element[outputIndex++]=line[*lineIndex];
(*lineIndex)++;
}
break;
default:
element[outputIndex++]=character;
(*lineIndex)++;
break;
}
}
else
{
done=true;
}
}
// remove white space from the end