[go: up one dir, main page]

Menu

[r20]: / trunk / VTS3 / ScriptBase.cpp  Maximize  Restore  History

Download this file

1184 lines (980 with data), 24.4 kB

   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
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
// ScriptBase.cpp: implementation of the ScriptBase class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "VTS.h"
#include "ScriptBase.h"
#include "ScriptKeywords.h"
#include "ScriptParmList.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Define the following as '1' if signed constants other than integers
// and floating point values are allowed. For example, if '-0xF' is an
// invalid form of '-15', then define it as '1'.
//
#define InvalidSignedConst 0
//
// ScriptBase::ScriptBase
//
ScriptBase::ScriptBase()
: baseType( scriptNull )
, baseLineStart(0), baseLineCount(0)
, baseImage(0), baseStatus(0)
, baseParent(0)
{
baseLabel = _T("");
}
//
// ScriptBase::~ScriptBase
//
// When a parent is deleted, destroy all of the component elements.
// These components will not be a part of any other datastructures.
//
ScriptBase::~ScriptBase()
{
for (POSITION pos = GetHeadPosition(); pos; )
delete (ScriptBasePtr)GetNext( pos );
}
//
// ScriptBase::Append
//
void ScriptBase::Append( ScriptBasePtr sbp )
{
// add to the end of the list
AddTail( sbp );
// link the item to the parent
sbp->baseParent = this;
}
//
// ScriptBase::Remove
//
void ScriptBase::Remove( int indx )
{
POSITION pos = FindIndex( indx )
;
ASSERT( pos != NULL );
delete (ScriptBasePtr)GetAt( pos );
RemoveAt( pos );
}
//
// ScriptBase::Length
//
int ScriptBase::Length( void )
{
return CList<ScriptBasePtr,ScriptBasePtr>::GetCount();
}
//
// ScriptBase::Child
//
ScriptBasePtr ScriptBase::Child( int indx )
{
POSITION pos = FindIndex( indx )
;
if (pos != NULL )
return (ScriptBasePtr)GetAt( pos );
else
return 0;
}
//
// ScriptToken::ScriptToken
//
ScriptToken::ScriptToken( void )
: tokenType( scriptEOF )
, tokenEnc( scriptIntegerEnc )
, tokenLine( -1 )
, tokenOffset(0), tokenLength(0)
, tokenValue(""), tokenSymbol(0)
{
}
//
// ScriptToken::ScriptToken
//
ScriptToken::ScriptToken( const ScriptToken& cpy )
: tokenType( cpy.tokenType )
, tokenEnc( cpy.tokenEnc )
, tokenLine( cpy.tokenLine )
, tokenOffset( cpy.tokenOffset) , tokenLength( cpy.tokenLength )
, tokenValue( cpy.tokenValue ), tokenSymbol( cpy.tokenSymbol )
{
}
//
// ScriptToken::operator =
//
ScriptToken::operator =( const ScriptToken& cpy )
{
tokenType = cpy.tokenType;
tokenLine = cpy.tokenLine;
tokenOffset = cpy.tokenOffset;
tokenLength = cpy.tokenLength;
tokenValue = cpy.tokenValue;
tokenSymbol = cpy.tokenSymbol;
}
//
// ScriptToken::~ScriptToken
//
ScriptToken::~ScriptToken( void )
{
}
//
// ScriptToken::IsInteger
//
bool ScriptToken::IsInteger( int &valu, ScriptParmListPtr parms ) const
{
LPCTSTR src = tokenValue
;
ScriptEncodingType encType = tokenEnc
;
bool negValue = false
;
int t
;
// if it is a keyword, lookup the value
if (tokenType == scriptKeyword) {
ScriptParmPtr spp = (parms ? parms->LookupParm( tokenSymbol ) : 0);
if (!spp)
return false;
src = spp->parmValue;
// figure out what encoding to use
if ((src[0] == 'D') && (src[1] == '\''))
encType = scriptDecimalEnc;
else
if ((src[0] == '0') && (src[1] == 'x'))
encType = scriptHexEnc;
else
if ((src[0] == 'X') && (src[1] == '\''))
encType = scriptHexEnc;
else
if ((src[0] == '&') && (src[1] == 'x'))
encType = scriptHexEnc;
else
if ((src[0] == '0') && (src[1] == 'o'))
encType = scriptOctalEnc;
else
if ((src[0] == 'O') && (src[1] == '\''))
encType = scriptOctalEnc;
else
if ((src[0] == '&') && (src[1] == 'O'))
encType = scriptOctalEnc;
else
if ((src[0] == '0') && (src[1] == 'b'))
encType = scriptBinaryEnc;
else
if ((src[0] == 'B') && (src[1] == '\''))
encType = scriptBinaryEnc;
else
if ((src[0] == '&') && (src[1] == 'B'))
encType = scriptBinaryEnc;
else
return false;
} else
if (tokenType != scriptValue)
return false;
// look for a sign
if (*src == '-') {
negValue = true;
src += 1;
} else
if (*src == '+')
src += 1;
// now parse
switch (encType) {
case scriptIntegerEnc:
for (valu = 0; *src; src++)
valu = (valu * 10) + (*src - '0');
break;
case scriptDecimalEnc: // D'nnn'
src += 2;
if (((strlen(src) - 1) % 3) != 0) // must be triplet
return false;
for (valu = 0; *src != '\''; ) {
t = (*src++ - '0');
t = (t * 10) + (*src++ - '0');
t = (t * 10) + (*src++ - '0');
valu = (valu * 256) + t;
}
break;
case scriptHexEnc: // 0xFF, X'FF', &xFF
src += 2;
for (valu = 0; *src && (*src != '\''); src++)
valu = (valu * 16) + (isdigit(*src) ? (*src - '0') : (*src - 'A' + 10));
break;
case scriptOctalEnc: // 0o377, O'377', &O377
src += 2;
for (valu = 0; *src && (*src != '\''); src++)
valu = (valu * 8) + (*src - '0');
break;
case scriptBinaryEnc: // 0b11111111, B'11111111', &B11111111
src += 2;
for (valu = 0; *src && (*src != '\''); src++)
valu = (valu * 2) + (*src - '0');
break;
default:
return false;
}
// update for sign
if (negValue)
valu = (valu * -1);
// success
return true;
}
//
// ScriptToken::IsInteger
//
// This function is used to see if the token can be parsed as an integer. If it is a
// keyword, then look it up in the table provided.
//
bool ScriptToken::IsInteger( int &valu, ScriptTranslateTablePtr tp ) const
{
int rslt
;
// if it is a keyword, lookup the value
if (tokenType == scriptKeyword) {
rslt = Lookup( tokenSymbol, tp );
if (rslt < 0)
return false;
// success
valu = rslt;
return true;
}
// try default processing
return IsInteger( valu );
}
//
// ScriptToken::IsPropertyReference
//
// This function is used to see if the token is a reference to a property. If it is
// the valu will be the property identifier. Only standard property names are supported,
// because otherwise there is no mechanism to match up the type.
//
bool ScriptToken::IsPropertyReference( int &prop ) const
{
int rslt
;
// if it is a keyword, lookup the value
if (tokenType == scriptReference) {
rslt = Lookup( tokenSymbol, ScriptPropertyMap );
if (rslt < 0)
return false;
// success
prop = rslt;
return true;
}
// sadly, no chance
return false;
}
//
// ScriptToken::IsEncodeable
//
// This function checks to see if the token can be translated into an encodeable type,
// one of the BACnet atomic types.
//
bool ScriptToken::IsEncodeable( BACnetEncodeable &enc ) const
{
try {
enc.Decode( tokenValue );
}
catch (...) {
return false;
}
// success
return true;
}
//
// ScriptToken::HashCode
//
// This hash function is used to encode a keyword into some unique value
// that can be used in switch statements and looked up in tables very
// quickly. There may be two keywords that map to the same hash value,
// btu I haven't found any yet!
//
int ScriptToken::HashCode( const char *key )
{
#define LARGENUMBER 6293815
int index = 1
;
DWORD sum = 0
, multiple = LARGENUMBER
;
while (*key != '\0') {
sum += multiple * index++ * (*key++);
multiple *= LARGENUMBER;
}
return sum;
}
//
// ScriptToken::Lookup
//
// This function looks through a translate table for the current token
// symbol. It is used to map keywords into property values, for example.
// This will return -1 if the value was not found.
//
int ScriptToken::Lookup( int code, ScriptTranslateTablePtr tp )
{
while (tp->scriptKeyword)
if (tp->scriptKeyword == code)
return tp->scriptValue;
else
tp += 1;
return -1;
}
//
// ScriptTokenList::ScriptTokenList
//
ScriptTokenList::ScriptTokenList( void )
{
}
//
// ScriptTokenList::~ScriptTokenList
//
ScriptTokenList::~ScriptTokenList( void )
{
for (POSITION pos = GetHeadPosition(); pos; )
delete (ScriptTokenPtr)GetNext( pos );
}
//
// ScriptTokenList::Append
//
void ScriptTokenList::Append( const ScriptToken& tok )
{
// add a copy to the end of the list
AddTail( new ScriptToken( tok ) );
}
//
// ScriptTokenList::Length
//
int ScriptTokenList::Length( void )
{
return CList<ScriptTokenPtr,ScriptTokenPtr>::GetCount();
}
//
// ScriptTokenList::operator []
//
const ScriptToken& ScriptTokenList::operator []( int i )
{
POSITION pos = FindIndex( i )
;
ASSERT( pos != NULL );
return *(ScriptTokenPtr)GetAt( pos );
}
//
// ScriptScanner::ScriptScanner
//
// This scanner allows scanning from CEdit text, usually the script source
// window.
//
ScriptScanner::ScriptScanner( CEdit* ep )
: scanSource( ep )
, scanLine( -1 )
, scanSrc( scanLineBuffer )
{
scanLineBuffer[0] = 0;
scanValueBuffer[0] = 0;
}
//
// ScriptScanner::ScriptScanner
//
// This form of the ctor allows scanning from any string.
//
ScriptScanner::ScriptScanner( const char *src )
: scanSource( 0 )
, scanLine( -1 )
, scanSrc( src )
{
}
//
// ScriptScanner::~ScriptScanner
//
ScriptScanner::~ScriptScanner( void )
{
}
//
// ScriptScanner::FormatError
//
void ScriptScanner::FormatError( ScriptToken &tok, char *msg )
{
// set up the token to point to the bad char
if (scanSource) {
tok.tokenLine = scanLine;
tok.tokenOffset = scanSource->LineIndex( scanLine ) + (scanSrc - scanLineBuffer);
tok.tokenLength = (*scanSrc ? 1 : 0);
}
// here it goes
throw msg;
}
//
// ScriptScanner::Next
//
#include "ScriptKeywords.h"
void ScriptScanner::Next( ScriptToken& tok )
{
const char *tStart
;
char c, *dst
;
bool signFound = false
;
// prepare for token value
tok.tokenLength = 0;
tok.tokenSymbol = 0;
dst = scanValueBuffer;
*dst = 0;
// deblank
while (*scanSrc && isspace(*scanSrc))
scanSrc += 1;
// keep track of the start for computing length
tStart = scanSrc;
// compute the offset into the source text (unless working from a buffer)
if (scanSource) {
tok.tokenLine = scanLine;
tok.tokenOffset = scanSource->LineIndex( scanLine ) + (scanSrc - scanLineBuffer);
}
// check for end-of-line
if (!*scanSrc) {
tok.tokenType = scriptEOL;
tok.tokenLength = 0;
tok.tokenValue = "";
return;
}
// look for an 'ignoring' comment
if (*scanSrc == ';') {
// skip to end of line
while (*scanSrc) scanSrc++;
// end-of-line
tok.tokenType = scriptEOL;
tok.tokenLength = (scanSrc - tStart);
tok.tokenValue = "";
return;
}
// look for a 'describing' comment
if ((*scanSrc == '-') && (*(scanSrc+1) == '-')) {
// skip the comment chars
while (*scanSrc == '-')
scanSrc++;
// deblank
while (*scanSrc && isspace(*scanSrc))
scanSrc += 1;
// look for a word
while (*scanSrc) {
// copy the word
while (*scanSrc && (!isspace(*scanSrc)))
*dst++ = *scanSrc++;
// deblank
while (*scanSrc && isspace(*scanSrc))
scanSrc += 1;
// copy a blank before the next word
if (*scanSrc)
*dst++ = ' ';
}
// null terminate the end
*dst = 0;
// return an end-of-line token
tok.tokenType = scriptEOL;
tok.tokenLength = (scanSrc - tStart); // actual parsed length, not result length
tok.tokenValue = scanValueBuffer;
return;
}
// set up for possible error
tok.tokenType = scriptError;
// copy at least the first char
*dst++ = c = toupper(*scanSrc++);
*dst = 0;
// special chars are the next easiest
switch (c) {
case '=':
case '?':
case ',':
case '/':
case ':':
case '(':
case ')':
tok.tokenType = scriptSymbol;
tok.tokenSymbol = c;
break;
case '<':
if (*scanSrc == '=') {
scanSrc += 1;
tok.tokenType = scriptSymbol;
tok.tokenSymbol = '<=';
} else
if (*scanSrc == '>') {
scanSrc += 1;
tok.tokenType = scriptSymbol;
tok.tokenSymbol = '!=';
} else {
tok.tokenType = scriptSymbol;
tok.tokenSymbol = c;
}
break;
case '>':
if (*scanSrc == '=') {
scanSrc += 1;
tok.tokenType = scriptSymbol;
tok.tokenSymbol = '>=';
} else {
tok.tokenType = scriptSymbol;
tok.tokenSymbol = c;
}
break;
case '!':
if (*scanSrc == '=') {
scanSrc += 1;
tok.tokenType = scriptSymbol;
tok.tokenSymbol = '!=';
}
break;
case '+':
case '-':
signFound = true;
if (!isdigit(*scanSrc))
FormatError( tok, "Invalid character in numeric constant" );
break;
case '&':
if (*scanSrc == 'D') {
tok.tokenType = scriptValue;
tok.tokenEnc = scriptDecimalEnc;
*dst++ = *scanSrc++;
while (isdigit(*scanSrc))
*dst++ = *scanSrc++;
} else
if (*scanSrc == 'H') {
tok.tokenType = scriptValue;
tok.tokenEnc = scriptHexEnc;
*dst++ = *scanSrc++;
while (isxdigit(*scanSrc))
*dst++ = *scanSrc++;
} else
if (*scanSrc == 'B') {
tok.tokenType = scriptValue;
tok.tokenEnc = scriptBinaryEnc;
*dst++ = *scanSrc++;
while ((*scanSrc == '0') || (*scanSrc == '1'))
*dst++ = *scanSrc++;
} else
if (*scanSrc == 'O') {
tok.tokenType = scriptValue;
tok.tokenEnc = scriptOctalEnc;
*dst++ = *scanSrc++;
while ((*scanSrc >= '0') && (*scanSrc <= '7'))
*dst++ = *scanSrc++;
} else
;
if (isalnum(*scanSrc))
FormatError( tok, "Invalid character in constant" );
*dst = 0;
break;
case '\"':
case '\'':
tok.tokenType = scriptValue;
tok.tokenEnc = scriptASCIIEnc;
while (*scanSrc && (*scanSrc != c)) {
if ((*scanSrc == '\\') && (*(scanSrc+1)))
scanSrc += 1;
*dst++ = *scanSrc++;
}
if (*scanSrc != c)
FormatError( tok, "Missing close quote" );
*dst++ = *scanSrc++;
*dst = 0;
break;
case '{':
tok.tokenType = scriptReference;
// deblank
while (*scanSrc && isspace(*scanSrc))
scanSrc += 1;
// get the name
scanValueBuffer[1] = 0;
while (isalpha(*scanSrc) || isdigit(*scanSrc) || (*scanSrc == '-') || (*scanSrc == '_')) {
if ((*scanSrc == '-') && (*(scanSrc+1) == '-'))
break;
c = toupper(*scanSrc++);
*dst++ = (c == '_' ? '-' : c);
}
if (!scanValueBuffer[1])
FormatError( tok, "Missing reference" );
*dst = 0;
// compute the hash code
tok.tokenSymbol = ScriptToken::HashCode( scanValueBuffer + 1 );
// deblank
while (*scanSrc && isspace(*scanSrc))
scanSrc += 1;
if ((*dst++ = *scanSrc++) != '}')
FormatError( tok, "Missing close brace" );
*dst = 0;
break;
}
// clean up if a token was found
if (tok.tokenType != scriptError) {
tok.tokenLength = (scanSrc - tStart);
tok.tokenValue = scanValueBuffer;
return;
}
// C-style hex constant
if ((c == '0') && ((*scanSrc == 'x') || (*scanSrc == 'X'))) {
#if InvalidSignedConst
if (signFound)
throw "Invalid signed value";
#endif
tok.tokenType = scriptValue;
tok.tokenEnc = scriptHexEnc;
*dst++ = 'x';
scanSrc += 1;
while (isxdigit(*scanSrc))
*dst++ = toupper(*scanSrc++);
*dst = 0;
tok.tokenLength = (scanSrc - tStart);
if (scanValueBuffer[2] == 0)
FormatError( tok, "Hex digit expected" );
if (isalnum(*scanSrc))
FormatError( tok, "Invalid character in hex constant" );
tok.tokenValue = scanValueBuffer;
return;
}
// C-style binary constant
if ((c == '0') && ((*scanSrc == 'b') || (*scanSrc == 'B'))) {
#if InvalidSignedConst
if (signFound)
throw "Invalid signed value";
#endif
tok.tokenType = scriptValue;
tok.tokenEnc = scriptBinaryEnc;
*dst++ = 'b';
scanSrc += 1;
while ((*scanSrc == '0') || (*scanSrc == '1'))
*dst++ = *scanSrc++;
*dst = 0;
tok.tokenLength = (scanSrc - tStart);
if (scanValueBuffer[2] == 0)
FormatError( tok, "Binary digit expected" );
if (isalnum(*scanSrc))
FormatError( tok, "Invalid character in binary constant" );
tok.tokenValue = scanValueBuffer;
return;
}
// C-style octal constant
if ((c == '0') && ((*scanSrc == 'o') || (*scanSrc == 'O'))) {
#if InvalidSignedConst
if (signFound)
throw "Invalid signed value";
#endif
tok.tokenType = scriptValue;
tok.tokenEnc = scriptOctalEnc;
*dst++ = 'o';
scanSrc += 1;
while ((*scanSrc >= '0') && (*scanSrc <= '7'))
*dst++ = *scanSrc++;
*dst = 0;
tok.tokenLength = (scanSrc - tStart);
if (scanValueBuffer[2] == 0)
FormatError( tok, "Octal digit expected" );
if (isalnum(*scanSrc))
FormatError( tok, "Invalid character in octal constant" );
tok.tokenValue = scanValueBuffer;
return;
}
// check for an IP address
if (isdigit(c)) {
int valu = (c - '0')
;
const char *saveSrc = scanSrc
;
char *dst = scanValueBuffer
;
bool validIP = true
;
// assume the best
tok.tokenType = scriptValue;
tok.tokenEnc = scriptIPEnc;
*dst++ = c;
// check for four dotted octets
for (int i = 0; i < 4; i++, valu = 0) {
// copy the integer portion
while (isdigit(*scanSrc)) {
valu = (valu * 10) + (*scanSrc - '0');
*dst++ = *scanSrc++;
}
if (valu > 255) {
// nope!
validIP = false;
break;
}
if ((i != 3) && ((*dst++ = *scanSrc++) != '.')) {
validIP = false;
break;
}
}
// check for network size
if (validIP && (*scanSrc == '/')) {
*dst++ = *scanSrc++;
while (isdigit(*scanSrc))
*dst++ = *scanSrc++;
}
// check for port (allow 0xBAC0 encoding)
if (validIP && (*scanSrc == ':')) {
*dst++ = *scanSrc++;
if ((*scanSrc == '0') && (tolower(*(scanSrc+1)) == 'x')) {
*dst++ = *scanSrc++;
*dst++ = *scanSrc++;
while (isxdigit(*scanSrc))
*dst++ = *scanSrc++;
} else
while (isdigit(*scanSrc))
*dst++ = *scanSrc++;
}
// null terminate the results
*dst = 0;
// if we made it this far, run with it
if (validIP) {
tok.tokenLength = (scanSrc - tStart);
tok.tokenValue = scanValueBuffer;
return;
}
// rewind
scanSrc = saveSrc;
}
// look for a number
if (isdigit(c) || (signFound && isdigit(*scanSrc))) {
tok.tokenType = scriptValue;
tok.tokenEnc = scriptIntegerEnc;
// copy the rest of the integer portion
while (isdigit(*scanSrc))
*dst++ = *scanSrc++;
// check for float
if (*scanSrc == '.') {
tok.tokenEnc = scriptFloatEnc;
*dst++ = *scanSrc++;
if (!isdigit(*scanSrc))
FormatError( tok, "Invalid number format" );
while (isdigit(*scanSrc))
*dst++ = *scanSrc++;
}
// check for exponential
if ((*scanSrc == 'e') || (*scanSrc == 'E')) {
tok.tokenEnc = scriptFloatEnc;
*dst++ = *scanSrc++;
if (*scanSrc == '-')
*dst++ = *scanSrc++;
if (!isdigit(*scanSrc))
FormatError( tok, "Invalid number format" );
while (isdigit(*scanSrc))
*dst++ = *scanSrc++;
}
// null terminate the results
*dst = 0;
// no other alpha chars please
if (isalpha(*scanSrc))
FormatError( tok, "Invalid number format" );
tok.tokenLength = (scanSrc - tStart);
tok.tokenValue = scanValueBuffer;
return;
}
// return '+' and '-' as symbols, they might be operators
if (signFound) {
tok.tokenType = scriptSymbol;
tok.tokenSymbol = c;
return;
}
// toss all non-alphas
if (!isalpha(c)) {
scanSrc -= 1;
FormatError( tok, "Invalid character" );
}
// copy the rest and normalize
tok.tokenType = scriptKeyword;
while (isalpha(*scanSrc) || isdigit(*scanSrc) || (*scanSrc == '-') || (*scanSrc == '_')) {
if ((*scanSrc == '-') && (*(scanSrc+1) == '-'))
break;
c = toupper(*scanSrc++);
*dst++ = (c == '_' ? '-' : c);
}
*dst = 0;
tok.tokenLength = (scanSrc - tStart);
tok.tokenValue = scanValueBuffer;
// compute the hash code
tok.tokenSymbol = ScriptToken::HashCode( scanValueBuffer );
// check for COMMENT keyword
if (tok.tokenSymbol == kwCOMMENT) {
dst = scanValueBuffer;
// deblank
while (*scanSrc && isspace(*scanSrc))
scanSrc += 1;
// look for a word
while (*scanSrc) {
// copy the word
while (*scanSrc && (!isspace(*scanSrc)))
*dst++ = *scanSrc++;
// deblank
while (*scanSrc && isspace(*scanSrc))
scanSrc += 1;
// copy a blank before the next word
if (*scanSrc)
*dst++ = ' ';
}
// null terminate the end
*dst = 0;
// return an end-of-line token
tok.tokenType = scriptEOL;
tok.tokenLength = (scanSrc - tStart);
tok.tokenValue = scanValueBuffer;
return;
}
// check for hex const string
if ((tok.tokenSymbol == kwX) && (*scanSrc == '\'')) {
// normalize
scanValueBuffer[0] = 'X';
tok.tokenType = scriptValue;
tok.tokenEnc = scriptHexEnc;
*dst++ = *scanSrc++;
while (isxdigit(*scanSrc))
*dst++ = toupper(*scanSrc++);
// set the length and check for close quote
if (dst == scanValueBuffer + 2)
FormatError( tok, "Hex digit expected" );
if (*scanSrc != '\'')
FormatError( tok, "Invalid hex constant form" );
*dst++ = *scanSrc++;
*dst = 0;
tok.tokenLength = (scanSrc - tStart);
tok.tokenValue = scanValueBuffer;
return;
}
// check for binary const string
if ((tok.tokenSymbol == kwB) && (*scanSrc == '\'')) {
// normalize
scanValueBuffer[0] = 'B';
tok.tokenType = scriptValue;
tok.tokenEnc = scriptBinaryEnc;
*dst++ = *scanSrc++;
while ((*scanSrc == '0') || (*scanSrc == '1'))
*dst++ = *scanSrc++;
// set the length and check for close quote
if (dst == scanValueBuffer + 2)
FormatError( tok, "Binary digit expected" );
if (*scanSrc != '\'')
FormatError( tok, "Invalid binary constant form" );
*dst++ = *scanSrc++;
*dst = 0;
tok.tokenLength = (scanSrc - tStart);
tok.tokenValue = scanValueBuffer;
return;
}
// check for octal const string
if ((tok.tokenSymbol == kwO) && (*scanSrc == '\'')) {
// normalize
scanValueBuffer[0] = 'O';
tok.tokenType = scriptValue;
tok.tokenEnc = scriptBinaryEnc;
*dst++ = *scanSrc++;
while ((*scanSrc >= '0') && (*scanSrc <= '7'))
*dst++ = *scanSrc++;
*dst = 0;
// set the length and check for close quote
if (dst == scanValueBuffer + 2)
FormatError( tok, "Octal digit expected" );
if (*scanSrc != '\'')
FormatError( tok, "Invalid octal constant form" );
*dst++ = *scanSrc++;
*dst = 0;
tok.tokenLength = (scanSrc - tStart);
tok.tokenValue = scanValueBuffer;
return;
}
// no more special cases
return;
}
//
// ScriptScanner::Peek
//
// This function saves a copy of the current source and restores it after calling
// Next(). It is used to look ahead into the stream for a potential token.
//
void ScriptScanner::Peek( ScriptToken& tok )
{
const char *reserveSrc = scanSrc
;
// scan the token
Next( tok );
// restore the pointer to the next token
scanSrc = reserveSrc;
}
//
// ScriptScanner::NextTitle
//
// This special parsing function is called to group all of the information to the
// end-of-line into the parse token. Initial and trailing blanks will be removed,
// and multiple blanks in the data will be reduced to a single blank.
//
void ScriptScanner::NextTitle( ScriptToken& tok )
{
const char *tStart
;
char *dst = scanValueBuffer
;
// copy the line number
tok.tokenLine = scanLine;
// deblank
while (*scanSrc && isspace(*scanSrc))
scanSrc += 1;
// check for end-of-line
if ((*scanSrc == ';') || ((*scanSrc == '-') && (*(scanSrc+1) == '-'))) {
tok.tokenType = scriptEOL;
tok.tokenLength = 0;
tok.tokenValue = "";
return;
}
// keep track of the start for computing length
tStart = scanSrc;
// compute the offset into the source text
tok.tokenOffset = scanSource->LineIndex( scanLine ) + (scanSrc - scanLineBuffer);
// look for a word
while (*scanSrc) {
// copy the word
while (*scanSrc && (!isspace(*scanSrc)))
*dst++ = *scanSrc++;
// deblank
while (*scanSrc && isspace(*scanSrc))
scanSrc += 1;
// check for end-of-line
if ((*scanSrc == ';') || ((*scanSrc == '-') && (*(scanSrc+1) == '-')))
break;
// copy a blank before the next word
if (*scanSrc)
*dst++ = ' ';
}
// null terminate the end
*dst = 0;
// return an end-of-line token
tok.tokenType = scriptTitle;
tok.tokenLength = (scanSrc - tStart); // actual parsed length, not result length
tok.tokenValue = scanValueBuffer;
return;
}
//
// ScriptScanner::NextLine
//
// This member function loads the next line from the script and calls Next() to parse
// the first token.
//
void ScriptScanner::NextLine( ScriptToken& tok )
{
int len
;
// if there's no source, bail
if (!scanSource)
throw (-1);
// get the next line
scanLine += 1;
if (scanLine >= scanSource->GetLineCount()) {
tok.tokenType = scriptEOF;
return;
}
// suck the line into a private buffer and null-terminate it
len = scanSource->GetLine( scanLine, scanLineBuffer, kTokenBufferSize );
scanLineBuffer[len] = 0;
// load the next token
scanSrc = scanLineBuffer;
Next( tok );
}