00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 __version__ = '3.1.1'
00143
00144 def version_str_to_tuple(version_str):
00145 import re
00146 import sys
00147
00148 if version_str == 'HEAD':
00149 return (sys.maxint, sys.maxint, sys.maxint, sys.maxint)
00150
00151 m = re.match(r'(\d+)\.(\d+)(\.(\d+))?(b(\d+))?', version_str)
00152 if m is None:
00153 raise ValueError("Bad version string %r" % version_str)
00154
00155 major = int(m.group(1))
00156 minor = int(m.group(2))
00157 patch = int(m.group(4) or 0)
00158 beta = int(m.group(6) or sys.maxint)
00159
00160 return (major, minor, patch, beta)
00161
00162
00163 runtime_version_str = __version__
00164 runtime_version = version_str_to_tuple(runtime_version_str)
00165
00166
00167 from constants import *
00168 from dfa import *
00169 from exceptions import *
00170 from recognizers import *
00171 from streams import *
00172 from tokens import *
00173 """ANTLR3 exception hierarchy"""
00174
00175
00176 from antlr3.constants import INVALID_TOKEN_TYPE
00177
00178
00179
00180
00181 class BacktrackingFailed(Exception):
00182
00183 pass
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 class RecognitionException(Exception):
00219
00220 def __init__(self, input=None):
00221 Exception.__init__(self)
00222
00223
00224 self.input = None
00225
00226
00227
00228 self.index = None
00229
00230
00231
00232
00233 self.token = None
00234
00235
00236
00237 self.node = None
00238
00239
00240 self.c = None
00241
00242
00243
00244
00245 self.line = None
00246
00247 self.charPositionInLine = None
00248
00249
00250
00251
00252
00253 self.approximateLineInfo = False
00254
00255
00256 if input is not None:
00257 self.input = input
00258 self.index = input.index()
00259
00260
00261 from antlr3.streams import TokenStream, CharStream
00262 from antlr3.tree import TreeNodeStream
00263
00264 if isinstance(self.input, TokenStream):
00265 self.token = self.input.LT(1)
00266 self.line = self.token.line
00267 self.charPositionInLine = self.token.charPositionInLine
00268
00269 if isinstance(self.input, TreeNodeStream):
00270 self.extractInformationFromTreeNodeStream(self.input)
00271
00272 else:
00273 if isinstance(self.input, CharStream):
00274 self.c = self.input.LT(1)
00275 self.line = self.input.line
00276 self.charPositionInLine = self.input.charPositionInLine
00277
00278 else:
00279 self.c = self.input.LA(1)
00280
00281 def extractInformationFromTreeNodeStream(self, nodes):
00282 from antlr3.tree import Tree, CommonTree
00283 from antlr3.tokens import CommonToken
00284
00285 self.node = nodes.LT(1)
00286 adaptor = nodes.adaptor
00287 payload = adaptor.getToken(self.node)
00288 if payload is not None:
00289 self.token = payload
00290 if payload.line <= 0:
00291
00292 i = -1
00293 priorNode = nodes.LT(i)
00294 while priorNode is not None:
00295 priorPayload = adaptor.getToken(priorNode)
00296 if priorPayload is not None and priorPayload.line > 0:
00297
00298 self.line = priorPayload.line
00299 self.charPositionInLine = priorPayload.charPositionInLine
00300 self.approximateLineInfo = True
00301 break
00302
00303 i -= 1
00304 priorNode = nodes.LT(i)
00305
00306 else:
00307 self.line = payload.line
00308 self.charPositionInLine = payload.charPositionInLine
00309
00310 elif isinstance(self.node, Tree):
00311 self.line = self.node.line
00312 self.charPositionInLine = self.node.charPositionInLine
00313 if isinstance(self.node, CommonTree):
00314 self.token = self.node.token
00315
00316 else:
00317 type = adaptor.getType(self.node)
00318 text = adaptor.getText(self.node)
00319 self.token = CommonToken(type=type, text=text)
00320
00321
00322
00323
00324 def getUnexpectedType(self):
00325
00326 from antlr3.streams import TokenStream
00327 from antlr3.tree import TreeNodeStream
00328
00329 if isinstance(self.input, TokenStream):
00330 return self.token.type
00331
00332 elif isinstance(self.input, TreeNodeStream):
00333 adaptor = self.input.treeAdaptor
00334 return adaptor.getType(self.node)
00335
00336 else:
00337 return self.c
00338
00339 unexpectedType = property(getUnexpectedType)
00340
00341
00342
00343
00344 class MismatchedTokenException(RecognitionException):
00345
00346 def __init__(self, expecting, input):
00347 RecognitionException.__init__(self, input)
00348 self.expecting = expecting
00349
00350
00351 def __str__(self):
00352
00353 return "MismatchedTokenException(%r!=%r)" % (
00354 self.getUnexpectedType(), self.expecting
00355 )
00356 __repr__ = __str__
00357
00358
00359
00360
00361 class UnwantedTokenException(MismatchedTokenException):
00362
00363 def getUnexpectedToken(self):
00364 return self.token
00365
00366
00367 def __str__(self):
00368 exp = ", expected %s" % self.expecting
00369 if self.expecting == INVALID_TOKEN_TYPE:
00370 exp = ""
00371
00372 if self.token is None:
00373 return "UnwantedTokenException(found=%s%s)" % (None, exp)
00374
00375 return "UnwantedTokenException(found=%s%s)" % (self.token.text, exp)
00376 __repr__ = __str__
00377
00378
00379
00380
00381
00382
00383
00384 class MissingTokenException(MismatchedTokenException):
00385
00386 def __init__(self, expecting, input, inserted):
00387 MismatchedTokenException.__init__(self, expecting, input)
00388
00389 self.inserted = inserted
00390
00391
00392 def getMissingType(self):
00393 return self.expecting
00394
00395
00396 def __str__(self):
00397 if self.inserted is not None and self.token is not None:
00398 return "MissingTokenException(inserted %r at %r)" % (
00399 self.inserted, self.token.text)
00400
00401 if self.token is not None:
00402 return "MissingTokenException(at %r)" % self.token.text
00403
00404 return "MissingTokenException"
00405 __repr__ = __str__
00406
00407
00408
00409
00410 class MismatchedRangeException(RecognitionException):
00411
00412 def __init__(self, a, b, input):
00413 RecognitionException.__init__(self, input)
00414
00415 self.a = a
00416 self.b = b
00417
00418
00419 def __str__(self):
00420 return "MismatchedRangeException(%r not in [%r..%r])" % (
00421 self.getUnexpectedType(), self.a, self.b
00422 )
00423 __repr__ = __str__
00424
00425
00426
00427
00428 class MismatchedSetException(RecognitionException):
00429
00430 def __init__(self, expecting, input):
00431 RecognitionException.__init__(self, input)
00432
00433 self.expecting = expecting
00434
00435
00436 def __str__(self):
00437 return "MismatchedSetException(%r not in %r)" % (
00438 self.getUnexpectedType(), self.expecting
00439 )
00440 __repr__ = __str__
00441
00442
00443
00444
00445 class MismatchedNotSetException(MismatchedSetException):
00446
00447 def __str__(self):
00448 return "MismatchedNotSetException(%r!=%r)" % (
00449 self.getUnexpectedType(), self.expecting
00450 )
00451 __repr__ = __str__
00452
00453
00454
00455
00456 class NoViableAltException(RecognitionException):
00457
00458 def __init__(
00459 self, grammarDecisionDescription, decisionNumber, stateNumber, input
00460 ):
00461 RecognitionException.__init__(self, input)
00462
00463 self.grammarDecisionDescription = grammarDecisionDescription
00464 self.decisionNumber = decisionNumber
00465 self.stateNumber = stateNumber
00466
00467
00468 def __str__(self):
00469 return "NoViableAltException(%r!=[%r])" % (
00470 self.unexpectedType, self.grammarDecisionDescription
00471 )
00472 __repr__ = __str__
00473
00474
00475
00476
00477 class EarlyExitException(RecognitionException):
00478
00479 def __init__(self, decisionNumber, input):
00480 RecognitionException.__init__(self, input)
00481
00482 self.decisionNumber = decisionNumber
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493 class FailedPredicateException(RecognitionException):
00494
00495 def __init__(self, input, ruleName, predicateText):
00496 RecognitionException.__init__(self, input)
00497
00498 self.ruleName = ruleName
00499 self.predicateText = predicateText
00500
00501
00502 def __str__(self):
00503 return "FailedPredicateException("+self.ruleName+",{"+self.predicateText+"}?)"
00504 __repr__ = __str__
00505
00506
00507
00508
00509 class MismatchedTreeNodeException(RecognitionException):
00510
00511 def __init__(self, expecting, input):
00512 RecognitionException.__init__(self, input)
00513
00514 self.expecting = expecting
00515
00516 def __str__(self):
00517 return "MismatchedTreeNodeException(%r!=%r)" % (
00518 self.getUnexpectedType(), self.expecting
00519 )
00520 __repr__ = __str__
00521 """ANTLR3 runtime package"""
00522
00523
00524 EOF = -1
00525
00526
00527
00528
00529 DEFAULT_CHANNEL = 0
00530
00531
00532
00533 HIDDEN_CHANNEL = 99
00534
00535
00536 EOR_TOKEN_TYPE = 1
00537
00538
00539
00540 DOWN = 2
00541
00542
00543 UP = 3
00544
00545 MIN_TOKEN_TYPE = UP+1
00546
00547 INVALID_TOKEN_TYPE = 0
00548
00549 """ANTLR3 runtime package"""
00550
00551 """ANTLR3 runtime package"""
00552
00553
00554 from antlr3.constants import EOF, DEFAULT_CHANNEL, INVALID_TOKEN_TYPE
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564 class Token(object):
00565
00566
00567
00568
00569
00570
00571 def getText(self):
00572 raise NotImplementedError
00573
00574
00575
00576
00577
00578
00579 def setText(self, text):
00580 raise NotImplementedError
00581
00582
00583
00584
00585
00586
00587 def getType(self):
00588
00589 raise NotImplementedError
00590
00591
00592
00593
00594
00595 def setType(self, ttype):
00596
00597 raise NotImplementedError
00598
00599
00600
00601
00602
00603
00604
00605
00606 def getLine(self):
00607
00608 raise NotImplementedError
00609
00610
00611
00612
00613
00614 def setLine(self, line):
00615
00616 raise NotImplementedError
00617
00618
00619
00620
00621
00622
00623
00624
00625 def getCharPositionInLine(self):
00626
00627 raise NotImplementedError
00628
00629
00630
00631
00632
00633 def setCharPositionInLine(self, pos):
00634
00635 raise NotImplementedError
00636
00637
00638
00639
00640
00641
00642 def getChannel(self):
00643
00644 raise NotImplementedError
00645
00646
00647
00648
00649
00650 def setChannel(self, channel):
00651
00652 raise NotImplementedError
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662 def getTokenIndex(self):
00663
00664 raise NotImplementedError
00665
00666
00667
00668
00669
00670 def setTokenIndex(self, index):
00671
00672 raise NotImplementedError
00673
00674
00675
00676
00677
00678
00679
00680 def getInputStream(self):
00681
00682 raise NotImplementedError
00683
00684
00685
00686
00687
00688
00689 def setInputStream(self, input):
00690
00691 raise NotImplementedError
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712 class CommonToken(Token):
00713
00714 def __init__(self, type=None, channel=DEFAULT_CHANNEL, text=None,
00715 input=None, start=None, stop=None, oldToken=None):
00716 Token.__init__(self)
00717
00718 if oldToken is not None:
00719 self.type = oldToken.type
00720 self.line = oldToken.line
00721 self.charPositionInLine = oldToken.charPositionInLine
00722 self.channel = oldToken.channel
00723 self.index = oldToken.index
00724 self._text = oldToken._text
00725 if isinstance(oldToken, CommonToken):
00726 self.input = oldToken.input
00727 self.start = oldToken.start
00728 self.stop = oldToken.stop
00729
00730 else:
00731 self.type = type
00732 self.input = input
00733 self.charPositionInLine = -1
00734 self.line = 0
00735 self.channel = channel
00736
00737
00738 self.index = -1
00739
00740
00741
00742
00743 self._text = text
00744
00745
00746 self.start = start
00747
00748
00749
00750 self.stop = stop
00751
00752
00753 def getText(self):
00754 if self._text is not None:
00755 return self._text
00756
00757 if self.input is None:
00758 return None
00759
00760 return self.input.substring(self.start, self.stop)
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770 def setText(self, text):
00771 self._text = text
00772
00773 text = property(getText, setText)
00774
00775
00776 def getType(self):
00777 return self.type
00778
00779 def setType(self, ttype):
00780 self.type = ttype
00781
00782
00783 def getLine(self):
00784 return self.line
00785
00786 def setLine(self, line):
00787 self.line = line
00788
00789
00790 def getCharPositionInLine(self):
00791 return self.charPositionInLine
00792
00793 def setCharPositionInLine(self, pos):
00794 self.charPositionInLine = pos
00795
00796
00797 def getChannel(self):
00798 return self.channel
00799
00800 def setChannel(self, channel):
00801 self.channel = channel
00802
00803
00804 def getTokenIndex(self):
00805 return self.index
00806
00807 def setTokenIndex(self, index):
00808 self.index = index
00809
00810
00811 def getInputStream(self):
00812 return self.input
00813
00814 def setInputStream(self, input):
00815 self.input = input
00816
00817
00818 def __str__(self):
00819 if self.type == EOF:
00820 return "<EOF>"
00821
00822 channelStr = ""
00823 if self.channel > 0:
00824 channelStr = ",channel=" + str(self.channel)
00825
00826 txt = self.text
00827 if txt is not None:
00828 txt = txt.replace("\n","\\\\n")
00829 txt = txt.replace("\r","\\\\r")
00830 txt = txt.replace("\t","\\\\t")
00831 else:
00832 txt = "<no text>"
00833
00834 return "[@%d,%d:%d=%r,<%d>%s,%d:%d]" % (
00835 self.index,
00836 self.start, self.stop,
00837 txt,
00838 self.type, channelStr,
00839 self.line, self.charPositionInLine
00840 )
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853 class ClassicToken(Token):
00854
00855 def __init__(self, type=None, text=None, channel=DEFAULT_CHANNEL,
00856 oldToken=None
00857 ):
00858 Token.__init__(self)
00859
00860 if oldToken is not None:
00861 self.text = oldToken.text
00862 self.type = oldToken.type
00863 self.line = oldToken.line
00864 self.charPositionInLine = oldToken.charPositionInLine
00865 self.channel = oldToken.channel
00866
00867 self.text = text
00868 self.type = type
00869 self.line = None
00870 self.charPositionInLine = None
00871 self.channel = channel
00872 self.index = None
00873
00874
00875 def getText(self):
00876 return self.text
00877
00878 def setText(self, text):
00879 self.text = text
00880
00881
00882 def getType(self):
00883 return self.type
00884
00885 def setType(self, ttype):
00886 self.type = ttype
00887
00888
00889 def getLine(self):
00890 return self.line
00891
00892 def setLine(self, line):
00893 self.line = line
00894
00895
00896 def getCharPositionInLine(self):
00897 return self.charPositionInLine
00898
00899 def setCharPositionInLine(self, pos):
00900 self.charPositionInLine = pos
00901
00902
00903 def getChannel(self):
00904 return self.channel
00905
00906 def setChannel(self, channel):
00907 self.channel = channel
00908
00909
00910 def getTokenIndex(self):
00911 return self.index
00912
00913 def setTokenIndex(self, index):
00914 self.index = index
00915
00916
00917 def getInputStream(self):
00918 return None
00919
00920 def setInputStream(self, input):
00921 pass
00922
00923
00924 def toString(self):
00925 channelStr = ""
00926 if self.channel > 0:
00927 channelStr = ",channel=" + str(self.channel)
00928
00929 txt = self.text
00930 if txt is None:
00931 txt = "<no text>"
00932
00933 return "[@%r,%r,<%r>%s,%r:%r]" % (self.index,
00934 txt,
00935 self.type,
00936 channelStr,
00937 self.line,
00938 self.charPositionInLine
00939 )
00940
00941
00942 __str__ = toString
00943 __repr__ = toString
00944
00945
00946
00947 EOF_TOKEN = CommonToken(type=EOF)
00948
00949 INVALID_TOKEN = CommonToken(type=INVALID_TOKEN_TYPE)
00950
00951
00952
00953 SKIP_TOKEN = CommonToken(type=INVALID_TOKEN_TYPE)
00954
00955
00956 """ANTLR3 runtime package"""
00957
00958
00959 import codecs
00960 from StringIO import StringIO
00961
00962 from antlr3.constants import DEFAULT_CHANNEL, EOF
00963 from antlr3.tokens import Token, EOF_TOKEN
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984 class IntStream(object):
00985
00986 def consume(self):
00987 raise NotImplementedError
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997 def LA(self, i):
00998
00999 raise NotImplementedError
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011 def mark(self):
01012
01013 raise NotImplementedError
01014
01015
01016
01017
01018
01019
01020
01021
01022 def index(self):
01023
01024 raise NotImplementedError
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047 def rewind(self, marker=None):
01048
01049 raise NotImplementedError
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062 def release(self, marker=None):
01063
01064 raise NotImplementedError
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090 def seek(self, index):
01091
01092 raise NotImplementedError
01093
01094
01095
01096
01097
01098
01099
01100
01101 def size(self):
01102
01103 raise