-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathTSql80ParserBaseInternal.cs
More file actions
2400 lines (2208 loc) · 103 KB
/
TSql80ParserBaseInternal.cs
File metadata and controls
2400 lines (2208 loc) · 103 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
//------------------------------------------------------------------------------
// <copyright file="TSql80ParserBaseInternal.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using antlr;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.SqlServer.TransactSql.ScriptDom
{
internal abstract class TSql80ParserBaseInternal : antlr.LLkParser
{
private readonly TSqlFragmentFactory _fragmentFactory = new TSqlFragmentFactory();
private IList<ParseError> _parseErrors;
private bool _phaseOne;
protected TSqlWhitespaceTokenFilter _tokenSource;
private bool _initialQuotedIdentifiersOn = true;
// Indicates the previous statement level error location in phase one parsing mode.
// Start from -1 which is not a valid line/column. 0 would be ambiguous.
private int _phaseOnePreviousStatementLevelErrorLine = -1;
private int _phaseOnePreviousStatementLevelErrorColumn = -1;
private static readonly antlr.collections.impl.BitSet _statementLevelRecoveryTokens = new antlr.collections.impl.BitSet(4);
private static readonly antlr.collections.impl.BitSet _phaseOneBatchLevelRecoveryTokens = new antlr.collections.impl.BitSet(3);
private static readonly antlr.collections.impl.BitSet _ddlStatementBeginnerTokens = new antlr.collections.impl.BitSet(2);
const int LookAhead = 2;
//private static HashSet<string> _languageString = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
//{
// CodeGenerationSupporter.ChineseMacaoSar,
// CodeGenerationSupporter.ChineseSingapore,
// CodeGenerationSupporter.SerbianCyrillic,
// CodeGenerationSupporter.Spanish,
// CodeGenerationSupporter.ChineseHongKong,
// CodeGenerationSupporter.SerbianLatin,
// CodeGenerationSupporter.Portuegese,
// CodeGenerationSupporter.BritishEnglish,
// CodeGenerationSupporter.SimplifiedChinese,
// CodeGenerationSupporter.Marathi,
// CodeGenerationSupporter.Malayalam,
// CodeGenerationSupporter.Kannada,
// CodeGenerationSupporter.Telugu,
// CodeGenerationSupporter.Tamil,
// CodeGenerationSupporter.Gujarati,
// CodeGenerationSupporter.Punjabi,
// CodeGenerationSupporter.BengaliIndia,
// CodeGenerationSupporter.MalayMalaysia,
// CodeGenerationSupporter.Hindi,
// CodeGenerationSupporter.Vietnamese,
// CodeGenerationSupporter.Lithuanian,
// CodeGenerationSupporter.Latvian,
// CodeGenerationSupporter.Slovenian,
// CodeGenerationSupporter.Ukrainian,
// CodeGenerationSupporter.Indonesian,
// CodeGenerationSupporter.Urdu,
// CodeGenerationSupporter.Thai,
// CodeGenerationSupporter.Swedish,
// CodeGenerationSupporter.Slovak,
// CodeGenerationSupporter.Croatian,
// CodeGenerationSupporter.Russian,
// CodeGenerationSupporter.Romanian,
// CodeGenerationSupporter.Brazilian,
// CodeGenerationSupporter.NorwegianBokmal,
// CodeGenerationSupporter.Dutch,
// CodeGenerationSupporter.Korean,
// CodeGenerationSupporter.Japanese,
// CodeGenerationSupporter.Italian,
// CodeGenerationSupporter.Icelandic,
// CodeGenerationSupporter.Hebrew,
// CodeGenerationSupporter.French,
// CodeGenerationSupporter.English,
// CodeGenerationSupporter.German,
// CodeGenerationSupporter.TraditionalChinese,
// CodeGenerationSupporter.Catalan,
// CodeGenerationSupporter.Bulgarian,
// CodeGenerationSupporter.Arabic,
// CodeGenerationSupporter.Neutral,
//};
//private static HashSet<string> _languageIdentifier = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
//{
// CodeGenerationSupporter.Spanish,
// CodeGenerationSupporter.Portuegese,
// CodeGenerationSupporter.Marathi,
// CodeGenerationSupporter.Malayalam,
// CodeGenerationSupporter.Kannada,
// CodeGenerationSupporter.Telugu,
// CodeGenerationSupporter.Tamil,
// CodeGenerationSupporter.Gujarati,
// CodeGenerationSupporter.Punjabi,
// CodeGenerationSupporter.Hindi,
// CodeGenerationSupporter.Vietnamese,
// CodeGenerationSupporter.Lithuanian,
// CodeGenerationSupporter.Latvian,
// CodeGenerationSupporter.Slovenian,
// CodeGenerationSupporter.Ukrainian,
// CodeGenerationSupporter.Indonesian,
// CodeGenerationSupporter.Urdu,
// CodeGenerationSupporter.Thai,
// CodeGenerationSupporter.Swedish,
// CodeGenerationSupporter.Slovak,
// CodeGenerationSupporter.Croatian,
// CodeGenerationSupporter.Russian,
// CodeGenerationSupporter.Romanian,
// CodeGenerationSupporter.Brazilian,
// CodeGenerationSupporter.Dutch,
// CodeGenerationSupporter.Korean,
// CodeGenerationSupporter.Japanese,
// CodeGenerationSupporter.Italian,
// CodeGenerationSupporter.Icelandic,
// CodeGenerationSupporter.Hebrew,
// CodeGenerationSupporter.French,
// CodeGenerationSupporter.English,
// CodeGenerationSupporter.German,
// CodeGenerationSupporter.Catalan,
// CodeGenerationSupporter.Bulgarian,
// CodeGenerationSupporter.Arabic,
// CodeGenerationSupporter.Neutral,
//};
//private static HashSet<int> _languageInteger = new HashSet<int>()
//{
// 5124,
// 4100,
// 3098,
// 3082,
// 3076,
// 2074,
// 2070,
// 2057,
// 2052,
// 1102,
// 1100,
// 1099,
// 1098,
// 1097,
// 1095,
// 1094,
// 1093,
// 1086,
// 1081,
// 1066,
// 1063,
// 1062,
// 1060,
// 1058,
// 1057,
// 1056,
// 1054,
// 1053,
// 1051,
// 1050,
// 1049,
// 1048,
// 1046,
// 1044,
// 1043,
// 1042,
// 1041,
// 1040,
// 1039,
// 1037,
// 1036,
// 1033,
// 1031,
// 1028,
// 1027,
// 1026,
// 1025,
// 0,
//};
private static HashSet<SqlDataTypeOption> _possibleSingleParameterDataTypes = new HashSet<SqlDataTypeOption>()
{
SqlDataTypeOption.Char, SqlDataTypeOption.VarChar, SqlDataTypeOption.NChar,
SqlDataTypeOption.NVarChar, SqlDataTypeOption.Decimal, SqlDataTypeOption.Float,
SqlDataTypeOption.Numeric, SqlDataTypeOption.Binary, SqlDataTypeOption.VarBinary,
SqlDataTypeOption.Time, SqlDataTypeOption.DateTime2, SqlDataTypeOption.DateTimeOffset,
SqlDataTypeOption.Vector
};
#region Constructors
// Not really needed, here only because ANTLR generates call to this one in derived classes
protected TSql80ParserBaseInternal(TokenBuffer tokenBuf, int k)
: base(tokenBuf, k)
{
}
// Not really needed, here only because ANTLR generates call to this one in derived classes
protected TSql80ParserBaseInternal(ParserSharedInputState state, int k)
: base(state, k)
{
}
// Not really needed, here only because ANTLR generates call to this one in derived classes
protected TSql80ParserBaseInternal(TokenStream lexer, int k)
: base(lexer, k)
{
}
/// <summary>
/// Real constructor (the one which is used)
/// </summary>
/// <param name="initialQuotedIdentifiersOn">if set to <c>true</c> initial quoted identifiers will be on.</param>
public TSql80ParserBaseInternal(bool initialQuotedIdentifiersOn)
: base(LookAhead)
{
_initialQuotedIdentifiersOn = initialQuotedIdentifiersOn;
}
public void InitializeForNewInput(IList<TSqlParserToken> tokens, IList<ParseError> errors, bool phaseOne)
{
_tokenSource = new TSqlWhitespaceTokenFilter(_initialQuotedIdentifiersOn, tokens);
_parseErrors = errors;
_fragmentFactory.SetTokenStream(tokens);
PhaseOne = phaseOne;
setTokenBuffer(new TokenBuffer(_tokenSource));
resetState();
}
static TSql80ParserBaseInternal()
{
_ddlStatementBeginnerTokens.add(TSql80ParserInternal.Create);
_ddlStatementBeginnerTokens.add(TSql80ParserInternal.Alter);
_statementLevelRecoveryTokens.add(TSql80ParserInternal.Go);
_statementLevelRecoveryTokens.add(TSql80ParserInternal.Semicolon);
_statementLevelRecoveryTokens.orInPlace(_ddlStatementBeginnerTokens);
_phaseOneBatchLevelRecoveryTokens.add(TSql80ParserInternal.Go);
_phaseOneBatchLevelRecoveryTokens.orInPlace(_ddlStatementBeginnerTokens);
}
#endregion
protected void ResetQuotedIdentifiersSettingToInitial()
{
_tokenSource.QuotedIdentifier = _initialQuotedIdentifiersOn;
}
/// <summary>
/// Updates token offsets for given fragment
/// </summary>
/// <param name="fragment">The fragment.</param>
/// <param name="token">The token.</param>
internal static void UpdateTokenInfo(TSqlFragment fragment, antlr.IToken token)
{
TSqlWhitespaceTokenFilter.TSqlParserTokenProxyWithIndex proxy = (TSqlWhitespaceTokenFilter.TSqlParserTokenProxyWithIndex)token;
int tokenIndex = proxy.TokenIndex;
if (tokenIndex != TSqlFragment.Uninitialized)
fragment.UpdateTokenInfo(tokenIndex, tokenIndex);
}
/// <summary>
/// Creates an identifier from a label token and adds it to the multipart identifier.
/// </summary>
/// <param name="token"></param>
/// <param name="identifier"></param>
/// <param name="multiPartIdentifier"></param>
internal static void CreateIdentifierFromLabel(antlr.IToken token, Identifier identifier, MultiPartIdentifier multiPartIdentifier)
{
var tokenText = token?.getText();
if (string.IsNullOrEmpty(tokenText))
{
throw GetUnexpectedTokenErrorException(token);
}
var identifierName = tokenText?.EndsWith(":") == true ? tokenText.Substring(0, tokenText.Length - 1) : tokenText;
identifier.SetIdentifier(identifierName);
UpdateTokenInfo(identifier, token);
AddAndUpdateTokenInfo(multiPartIdentifier, multiPartIdentifier.Identifiers, identifier);
}
protected static void AddAndUpdateTokenInfo<TFragmentType>(TSqlFragment node, IList<TFragmentType> collection, TFragmentType item)
where TFragmentType : TSqlFragment
{
collection.Add(item);
node.UpdateTokenInfo(item);
}
protected static void AddAndUpdateTokenInfo<TFragmentType>(TSqlFragment node, IList<TFragmentType> collection, IList<TFragmentType> otherCollection)
where TFragmentType : TSqlFragment
{
foreach (TFragmentType item in otherCollection)
{
AddAndUpdateTokenInfo(node, collection, item);
}
}
protected static string DecodeAsciiStringLiteral(string encodedValue)
{
int length = encodedValue.Length;
Debug.Assert(length > 1);
string valueWithoutQuotes = encodedValue.Substring(1, length - 2);
if (encodedValue[0] == '"') // Quoted identifier when quoted identifiers disabled
return valueWithoutQuotes.Replace("\"\"", "\"");
else
return valueWithoutQuotes.Replace("''", "'");
}
protected static string DecodeUnicodeStringLiteral(string encodedValue)
{
int length = encodedValue.Length;
Debug.Assert(length > 2);
return encodedValue.Substring(2, length - 3).Replace("''", "'");
}
protected static bool IsAsciiStringLob(string asciiValue)
{
return asciiValue.Length > 8000;
}
protected static bool IsUnicodeStringLob(string unicodeValue)
{
return unicodeValue.Length > 8000;
}
protected static bool IsBinaryLiteralLob(string binaryValue)
{
//Binary value string includes 0x
//
return binaryValue.Length - 2 > 16000;
}
/// <summary>
/// Used in instantiating fragments.
/// </summary>
public TSqlFragmentFactory FragmentFactory
{
get
{
return _fragmentFactory;
}
}
/// <summary>
/// Indicates if the parser is in Phase One parsing mode.
/// </summary>
public bool PhaseOne
{
get
{
return _phaseOne;
}
set
{
_phaseOne = value;
}
}
/// <summary>
/// Adds a parse error to the list/
/// </summary>
/// <param name="parseError">The error to be added.</param>
protected void AddParseError(ParseError parseError)
{
_parseErrors.Add(parseError);
}
/// <summary>
/// Recovers from an error at the statement level by
/// consuming (skipping over) tokens until one of the tokens in _errorRecoveryTokens.
/// </summary>
/// <param name="statementStartLine">The statement start line.</param>
/// <param name="statementStartColumn">The statement start column.</param>
protected void RecoverAtStatementLevel(int statementStartLine, int statementStartColumn)
{
consumeUntil(_statementLevelRecoveryTokens);
int nextTokenLine = LT(1).getLine();
int nextTokenColumn = LT(1).getColumn();
if (nextTokenLine == statementStartLine &&
nextTokenColumn == statementStartColumn)
{
if (PhaseOne &&
_phaseOnePreviousStatementLevelErrorLine != nextTokenLine &&
_phaseOnePreviousStatementLevelErrorColumn != nextTokenColumn)
{
_phaseOnePreviousStatementLevelErrorLine = nextTokenLine;
_phaseOnePreviousStatementLevelErrorColumn = nextTokenColumn;
// We have recovered at a possible DDL statement, re-enter the script.
throw new PhaseOneBatchException();
}
else
{
consume();
}
}
}
/// <summary>
/// If the first token is something we expect as a DDL statement than skip
/// that one in order to avoid infinite recursion (it would have never
/// caused an exception if it could be parsed).
/// </summary>
protected void SkipInitialDdlTokens()
{
if (_ddlStatementBeginnerTokens.member(LA(1)))
{
consume();
}
}
/// <summary>
/// Recovers from an error at the batch level by
/// consuming (skipping over) tokens until a GO.
/// </summary>
protected void RecoverAtBatchLevel()
{
if (PhaseOne == true)
{
// The initial DDL tokens are skipped, if they could be understood, we would not hit an error
// at the batch level.
SkipInitialDdlTokens();
// Consume until the end of the batch or one of the P1 statement starters.
consumeUntil(_phaseOneBatchLevelRecoveryTokens);
if ((LA(1) != TSql80ParserInternal.Go) && (LA(1) != TSql80ParserInternal.EOF))
{
// We have recovered at a possible DDL statement, re-enter the script.
throw new PhaseOneBatchException();
}
}
else
{
// Recover at GO, or EOF(implicit).
consumeUntil(TSql80ParserInternal.Go);
}
}
/// <summary>
/// If the parser is in phase one mode, the statement is wrapped in PhaseOnePartialAstException
/// and thrown.
/// </summary>
/// <param name="statement">The partial AST.</param>
protected void ThrowPartialAstIfPhaseOne(TSqlStatement statement)
{
if (PhaseOne == true)
throw new PhaseOnePartialAstException(statement);
}
/// <summary>
/// If the parser is in phase one mode, the constraint is wrapped in PhaseOneConstraintException
/// and thrown.
/// </summary>
/// <param name="constraint">The constraint.</param>
protected void ThrowConstraintIfPhaseOne(ConstraintDefinition constraint)
{
if (PhaseOne == true)
throw new PhaseOneConstraintException(constraint);
}
/// <summary>
/// Checks if the next tokens text is the same as the keyword in a case insensitive way.
/// </summary>
/// <param name="keyword">The keyword to check against.</param>
/// <returns></returns>
protected bool NextTokenMatches(string keyword)
{
return (LA(1) != TSql80ParserInternal.EOF) && (String.Equals(LT(1).getText(), keyword, StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// Checks if the asked tokens text is the same as the keyword in a case insensitive way.
/// </summary>
/// <param name="keyword">The keyword to check against.</param>
/// <param name="which">The token to look at.</param>
/// <returns></returns>
protected bool NextTokenMatches(string keyword, int which)
{
return (LA(which) != TSql80ParserInternal.EOF) && (String.Equals(LT(which).getText(), keyword, StringComparison.OrdinalIgnoreCase));
}
protected bool NextTokenMatchesOneOf(params string[] keywords)
{
if (LA(1) == TSql80ParserInternal.EOF)
return false;
string text = LT(1).getText();
foreach (string keyword in keywords)
{
if (String.Equals(keyword, text, StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
/// <summary>
/// Throws an exception that won't be logged if reached the end of a batch or file.
/// </summary>
protected void ThrowIfEndOfFileOrBatch()
{
if ((LA(1) == TSql80ParserInternal.EOF) || (LA(1) == TSql80ParserInternal.Go))
{
throw new TSqlParseErrorException(null, true);
}
}
/// <summary>
/// Updates the result with a new expression with left associativity.
/// </summary>
/// <param name="result">Expression to be updated.</param>
/// <param name="expression">New expression to be inserted</param>
/// <param name="type">The type of the new expression.</param>
protected void AddBinaryExpression(ref ScalarExpression result, ScalarExpression expression, BinaryExpressionType type)
{
Debug.Assert(result != null);
BinaryExpression binaryExpression = this.FragmentFactory.CreateFragment<BinaryExpression>();
binaryExpression.FirstExpression = result;
binaryExpression.SecondExpression = expression;
binaryExpression.BinaryExpressionType = type;
result = binaryExpression;
}
/// <summary>
/// Updates the result with a new expression with left associativity.
/// </summary>
/// <param name="result">Expression to be updated.</param>
/// <param name="expression">New expression to be inserted</param>
/// <param name="type">The type of the new expression.</param>
protected void AddBinaryExpression(ref BooleanExpression result, BooleanExpression expression, BooleanBinaryExpressionType type)
{
Debug.Assert(result != null);
BooleanBinaryExpression binaryExpression = this.FragmentFactory.CreateFragment<BooleanBinaryExpression>();
binaryExpression.FirstExpression = result;
binaryExpression.SecondExpression = expression;
binaryExpression.BinaryExpressionType = type;
result = binaryExpression;
}
/// <summary>
/// Creates an empty identifier at token.
/// </summary>
/// <param name="token">The token to get the location information.</param>
/// <returns>The created identifier.</returns>
protected Identifier GetEmptyIdentifier(antlr.IToken token)
{
Identifier identifier = this.FragmentFactory.CreateFragment<Identifier>();
UpdateTokenInfo(identifier, token);
identifier.SetIdentifier(string.Empty);
return identifier;
}
/// <summary>
/// Checks for duplicates in XML FOR clause options
/// </summary>
/// <param name="current">The enum that the newOption will be added.</param>
/// <param name="newOption">The new option to be added.</param>
/// <param name="token">The token that was parsed for the new option.</param>
/// <returns>The aggregated value for options.</returns>
protected static void CheckXmlForClauseOptionDuplication(XmlForClauseOptions current, XmlForClauseOptions newOption, antlr.IToken token)
{
if ((current & newOption) != 0)
throw GetUnexpectedTokenErrorException(token);
if ((newOption & XmlForClauseOptions.ElementsAll) != 0 &&
(current & XmlForClauseOptions.ElementsAll) != 0)
{
throw GetUnexpectedTokenErrorException(token);
}
}
/// <summary>
/// Adds the item to the list, checking with the max. If max is smaller than zero there is no limit. Error is thrown if max is exceeded.
/// </summary>
/// <param name="list">List.</param>
/// <param name="item">Item.</param>
/// <param name="max">The max items allowed.</param>
protected static void AddIdentifierToListWithCheck(List<Identifier> list, Identifier item, int max)
{
if (list.Count == max)
{
throw GetUnexpectedTokenErrorException(item);
}
list.Add(item);
}
protected static void CheckOptionDuplication(ref long encountered, int newOption, TSqlFragment vOption)
{
CheckOptionDuplication(ref encountered, newOption, GetFirstToken(vOption));
}
protected static void CheckOptionDuplication(ref long encountered, int newOption, IToken token)
{
long newOptionBit = (1L << newOption);
if ((encountered & newOptionBit) == newOptionBit)
{
ThrowParseErrorException("SQL46049", token, TSqlParserResource.SQL46049Message, token.getText());
}
encountered |= newOptionBit;
}
protected static void CheckOptionDuplication(ref ulong encountered, int newOption, TSqlFragment vOption)
{
CheckOptionDuplication(ref encountered, newOption, GetFirstToken(vOption));
}
protected static void CheckOptionDuplication(ref ulong encountered, int newOption, IToken token)
{
ulong newOptionBit = (1ul << newOption);
if ((encountered & newOptionBit) == newOptionBit)
{
ThrowParseErrorException("SQL46049", token, TSqlParserResource.SQL46049Message, token.getText());
}
encountered |= newOptionBit;
}
protected IdentifierOrValueExpression IdentifierOrValueExpression(Identifier identifier)
{
IdentifierOrValueExpression vIdentifierOrValueExpression = this.FragmentFactory.CreateFragment<IdentifierOrValueExpression>();
vIdentifierOrValueExpression.Identifier = identifier;
return vIdentifierOrValueExpression;
}
protected IdentifierOrValueExpression IdentifierOrValueExpression(ValueExpression valueExpression)
{
IdentifierOrValueExpression vIdentifierOrValueExpression = this.FragmentFactory.CreateFragment<IdentifierOrValueExpression>();
vIdentifierOrValueExpression.ValueExpression = valueExpression;
return vIdentifierOrValueExpression;
}
/// <summary>
/// Parses the input to determine the literal type.
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
protected static OdbcLiteralType ParseOdbcLiteralType(antlr.IToken token)
{
if (TryMatch(token, CodeGenerationSupporter.T))
{
return OdbcLiteralType.Time;
}
else if (TryMatch(token, CodeGenerationSupporter.D))
{
return OdbcLiteralType.Date;
}
else if (TryMatch(token, CodeGenerationSupporter.TS))
{
return OdbcLiteralType.Timestamp;
}
else if (TryMatch(token, CodeGenerationSupporter.Guid))
{
return OdbcLiteralType.Guid;
}
else
{
throw GetUnexpectedTokenErrorException(token);
}
}
/// <summary>
/// Parses Join optimizer hint
/// </summary>
/// <param name="token">A token that should represent "MERGE", "HASH" or "LOOP",
/// otherwise an invalid token exception will be generated</param>
/// <returns>The hint specified by token.</returns>
protected static OptimizerHintKind ParseJoinOptimizerHint(antlr.IToken token)
{
Debug.Assert(token.Type == TSql80ParserInternal.Identifier);
switch (token.getText().ToUpperInvariant())
{
case CodeGenerationSupporter.Merge:
return OptimizerHintKind.MergeJoin;
case CodeGenerationSupporter.Hash:
return OptimizerHintKind.HashJoin;
case CodeGenerationSupporter.Loop:
return OptimizerHintKind.LoopJoin;
default:
throw GetUnexpectedTokenErrorException(token);
}
}
/// <summary>
/// Parses Union optimizer hint
/// </summary>
/// <param name="token">A token that should represent "CONCAT", "HASH", "MERGE" or "KEEP",
/// otherwise an invalid token exception will be generated</param>
/// <returns>The hint specified by token.</returns>
protected static OptimizerHintKind ParseUnionOptimizerHint(antlr.IToken token)
{
Debug.Assert(token.Type == TSql80ParserInternal.Identifier);
switch (token.getText().ToUpperInvariant())
{
case CodeGenerationSupporter.Concat:
return OptimizerHintKind.ConcatUnion;
case CodeGenerationSupporter.Hash:
return OptimizerHintKind.HashUnion;
case CodeGenerationSupporter.Merge:
return OptimizerHintKind.MergeUnion;
case CodeGenerationSupporter.Keep:
return OptimizerHintKind.KeepUnion;
default:
throw GetUnexpectedTokenErrorException(token);
}
}
/// <summary>
/// Checks if the next rule is a Select Parenthesis.
/// The logic is if there is a Join, Inner, Full, Cross, Union, Except, Intersect
/// when one parenthesis is open, the first parenthesis belongs to a select.
/// </summary>
/// <returns>True if the next rule is a join.</returns>
protected bool IsNextRuleSelectParenthesis()
{
bool matches = false;
if (LA(1) == TSql80ParserInternal.LeftParenthesis && LA(2) == TSql80ParserInternal.Select)
return true;
// So it starts with a paranthesis, mark and enter the main algorithm.
int markSpot = mark();
consume();
int openParens = 1;
for (bool loop = true; loop == true; consume())
{
switch (LA(1))
{
case TSql80ParserInternal.LeftParenthesis:
++openParens;
break;
case TSql80ParserInternal.RightParenthesis:
--openParens;
if (openParens == 0)
{
// we ended without matching.
loop = false;
}
break;
case TSql80ParserInternal.EOF:
// we ended without matching.
loop = false;
break;
case TSql80ParserInternal.Join:
case TSql80ParserInternal.Inner:
case TSql80ParserInternal.Full:
case TSql80ParserInternal.Cross:
case TSql80ParserInternal.Outer:
case TSql80ParserInternal.Union:
case TSql80ParserInternal.Except:
case TSql80ParserInternal.Intersect:
// Only if we are at the top level, # of open paranthesis should be one.
if (openParens == 1)
{
matches = true;
loop = false;
}
break;
default:
// Anything else is ignored.
break;
}
}
rewind(markSpot);
return matches;
}
/// <summary>
/// Checks if the next rule is a boolean parenthesis.
/// The logic is if one of the tokens that is used in boolean expression is
/// seen the expression is a boolean expression.
/// Boolean expressions can appear in case expressions, so if we enter a case
/// expression, the whole expression will not be classified boolean until
/// we exit from the case expression.
/// Boolean expressions can appear in select, so if there is a select in the open parans level or below, seeing a
/// boolean operand does not matter.
/// </summary>
/// <returns>True if the next rule is a boolean parenthesis.</returns>
protected bool IsNextRuleBooleanParenthesis()
{
// Fail fast
if (LA(1) != TSql80ParserInternal.LeftParenthesis)
{
return false;
}
bool matches = false;
// So it starts with a paranthesis, mark and enter the main algorithm.
int markSpot = mark();
consume();
int openParens = 1;
int caseDepth = 0;
// 0 means there was no select
int topmostSelect = 0;
int insideIIf = 0;
for (bool loop = true; loop == true; consume())
{
switch (LA(1))
{
case TSql80ParserInternal.Identifier:
// if identifier is IIF
if(NextTokenMatches(CodeGenerationSupporter.IIf))
{
++insideIIf;
}
break;
case TSql80ParserInternal.LeftParenthesis:
++openParens;
break;
case TSql80ParserInternal.RightParenthesis:
if (openParens == topmostSelect)
{
topmostSelect = 0;
}
--openParens;
if (openParens == 0)
{
// we ended without matching.
loop = false;
}
break;
case TSql80ParserInternal.EOF:
// we ended without matching.
loop = false;
break;
case TSql80ParserInternal.And:
case TSql80ParserInternal.Or:
case TSql80ParserInternal.Not:
case TSql80ParserInternal.EqualsSign:
case TSql80ParserInternal.GreaterThan:
case TSql80ParserInternal.LessThan:
case TSql80ParserInternal.Bang:
case TSql80ParserInternal.MultiplyEquals:
case TSql80ParserInternal.RightOuterJoin:
case TSql80ParserInternal.Is:
case TSql80ParserInternal.In:
case TSql80ParserInternal.Like:
case TSql80ParserInternal.Between:
case TSql80ParserInternal.Contains:
case TSql80ParserInternal.FreeText:
case TSql80ParserInternal.Exists:
case TSql80ParserInternal.TSEqual:
case TSql80ParserInternal.Update:
if (caseDepth == 0 && topmostSelect == 0 && insideIIf == 0)
{
// The number of open paranthesis are not important.
// Unless inside an iff
matches = true;
loop = false;
}
else if (insideIIf > 0)
{
// Found the operator inside IIF
--insideIIf;
}
break;
case TSql80ParserInternal.Case:
++caseDepth;
break;
case TSql80ParserInternal.End:
--caseDepth;
break;
case TSql80ParserInternal.Select:
if (topmostSelect == 0)
{
topmostSelect = openParens;
}
break;
default:
// Anything else is ignored.
break;
}
}
rewind(markSpot);
return matches;
}
#region Lookahead Utilities
/// <summary>
/// In the guessing mode (i.e. within a syntactic predicate), returns the current
/// lookahead token; otherwise, does nothing.
/// <para>
/// Use it like the following:
/// <code>
/// your_rule { IToken marker = null; } :
/// (
/// ({ if (!SkipGuessing(marker)) } : your_rule ({ SaveGuessing(out marker); } : ))=>
/// ({ if (!SkipGuessing(marker)) } : your_rule)
/// | ...
/// </code>
/// </para>
/// </summary>
/// <param name="marker">When the method returns, contains the current lookahead token
/// or the null value, if called in the non-guessing mode (i.e. not within a syntactic
/// predicate).</param>
/// <returns>true in the guessing mode; otherwise, false.</returns>
/// <seealso cref="SkipGuessing"/>
protected bool SaveGuessing(out IToken marker)
{
marker = null;
// Do not proceed if called in the non-guessing mode
if (inputState.guessing == 0)
return false;
// Return the current lookahead token
marker = LT(1);
return true;
}
/// <summary>
/// In the guessing mode (i.e. within a syntactic predicate), advances the input position
/// to the specified token, which becomes the current lookahead token; otherwise, does
/// nothing.
/// <para>
/// Use it like the following:
/// <code>
/// your_rule { IToken marker = null; } :
/// (
/// ({ if (!SkipGuessing(marker)) } : your_rule ({ SaveGuessing(out marker); } : ))=>
/// ({ if (!SkipGuessing(marker)) } : your_rule)
/// | ...
/// </code>
/// </para>
/// </summary>
/// <param name="marker">The token to advance the input position to.</param>
/// <returns>true in the guessing mode; otherwise, false.</returns>
/// <remarks>
/// <para>
/// The primary usage of this method is to break recursive exponential lookaheads. It is
/// a rare case when a recursive rule is ambiguous and a conflict is resolved by the full
/// syntactic predicate (aka lookahead) for the rule.
/// </para>
/// <para>
/// Consider the following example:
/// <code>
/// outer: '(' ((outer)=> outer | inner) ')'
/// </code>
/// Each time "outer" is parsed, the nested one is parsed twice, first for the lookahead
/// and then for real. This happens at each level of nesting, so the overall complexity
/// becomes O(2^n), where n is the number of nested "outer".
/// </para>
/// <para>
/// Sometimes, it is possible to redesign the grammar and avoid such a lookahead (e.g.
/// using left factoring). But there are cases when we do not want to significantly change
/// the grammar or the language is just non-LL(k). When so, this method provides a way to
/// improve the performance to O(n^2), which is suitable for most practical cases. The idea
/// it is to skip redundant lookahead branches. Let's consider the above example. ANTLR
/// first parses "outer" for the lookahead, i.e. actions are turned off. Then, if no errors,
/// it parsers "outer" again, now with actions turned on. However, if this is happening
/// within an upper level lookahead, actions are actually turned off for both parsings and
/// there is no any difference between them. So, we can skip the second parsing and simply
/// consume the needed number of tokens. Here is how it looks like when expanded (outer_LA
/// means parsing for lookahead, actions turned off):
/// <code>
/// outer
/// outer_LA
/// outer_LA
/// outer_LA
/// ...
/// outer
/// ...
/// outer
/// outer_LA
/// ...
/// outer
/// ...
/// outer
/// outer_LA
/// ...
/// outer
/// ...
/// </code>
/// Each time "outer" appears under "outer_LA" it is redundant and can be skipped. And here
/// is a code snippet doing this:
/// <code>
/// outer { IToken marker = null; } :
/// (
/// (outer
/// // Save the token following "outer".
/// ({ SaveGuessing(out marker); } : )) =>
/// // If within a lookahead, simply consume tokens until the one following "outer";
/// // otherwise, do a regular parsing
/// ({ if (!SkipGuessing(marker)) } :
/// outer)
/// |
/// ...
/// </code>
/// Note that all the processing is done within rule/subrule initialization blocks instead
/// of regular actions. As mentioned, actions are ignored within a lookahead; however,
/// initialization blocks are always executed.