antlr.py 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864
  1. from __future__ import print_function
  2. ## This file is part of PyANTLR. See LICENSE.txt for license
  3. ## details..........Copyright (C) Wolfgang Haefelinger, 2004.
  4. ## This file was copied for use with xlwt from the 2.7.7 ANTLR distribution. Yes, it
  5. ## says 2.7.5 below. The 2.7.5 distribution version didn't have a
  6. ## version in it.
  7. ## Here is the contents of the ANTLR 2.7.7 LICENSE.txt referred to above.
  8. # SOFTWARE RIGHTS
  9. #
  10. # ANTLR 1989-2006 Developed by Terence Parr
  11. # Partially supported by University of San Francisco & jGuru.com
  12. #
  13. # We reserve no legal rights to the ANTLR--it is fully in the
  14. # public domain. An individual or company may do whatever
  15. # they wish with source code distributed with ANTLR or the
  16. # code generated by ANTLR, including the incorporation of
  17. # ANTLR, or its output, into commerical software.
  18. #
  19. # We encourage users to develop software with ANTLR. However,
  20. # we do ask that credit is given to us for developing
  21. # ANTLR. By "credit", we mean that if you use ANTLR or
  22. # incorporate any source code into one of your programs
  23. # (commercial product, research project, or otherwise) that
  24. # you acknowledge this fact somewhere in the documentation,
  25. # research report, etc... If you like ANTLR and have
  26. # developed a nice tool with the output, please mention that
  27. # you developed it using ANTLR. In addition, we ask that the
  28. # headers remain intact in our source code. As long as these
  29. # guidelines are kept, we expect to continue enhancing this
  30. # system and expect to make other tools available as they are
  31. # completed.
  32. #
  33. # The primary ANTLR guy:
  34. #
  35. # Terence Parr
  36. # parrt@cs.usfca.edu
  37. # parrt@antlr.org
  38. ## End of contents of the ANTLR 2.7.7 LICENSE.txt ########################
  39. ## get sys module
  40. import sys
  41. from .compat import long, basestring, int_types, xrange
  42. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  43. ### global symbols ###
  44. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  45. ### ANTLR Standard Tokens
  46. SKIP = -1
  47. INVALID_TYPE = 0
  48. EOF_TYPE = 1
  49. EOF = 1
  50. NULL_TREE_LOOKAHEAD = 3
  51. MIN_USER_TYPE = 4
  52. ### ANTLR's EOF Symbol
  53. EOF_CHAR = ''
  54. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  55. ### general functions ###
  56. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  57. ## Version should be automatically derived from configure.in. For now,
  58. ## we need to bump it ourselfs. Don't remove the <version> tags.
  59. ## <version>
  60. def version():
  61. r = {
  62. 'major' : '2',
  63. 'minor' : '7',
  64. 'micro' : '5',
  65. 'patch' : '' ,
  66. 'version': '2.7.5'
  67. }
  68. return r
  69. ## </version>
  70. def error(fmt,*args):
  71. if fmt:
  72. print("error: ", fmt % tuple(args))
  73. def ifelse(cond,_then,_else):
  74. if cond :
  75. r = _then
  76. else:
  77. r = _else
  78. return r
  79. def is_string_type(x):
  80. # return (isinstance(x,str) or isinstance(x,unicode))
  81. # Simplify; xlwt doesn't support Python < 2.3
  82. return isinstance(basestring)
  83. def assert_string_type(x):
  84. assert is_string_type(x)
  85. pass
  86. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  87. ### ANTLR Exceptions ###
  88. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  89. class ANTLRException(Exception):
  90. def __init__(self, *args):
  91. Exception.__init__(self, *args)
  92. class RecognitionException(ANTLRException):
  93. def __init__(self, *args):
  94. ANTLRException.__init__(self, *args)
  95. self.fileName = None
  96. self.line = -1
  97. self.column = -1
  98. if len(args) >= 2:
  99. self.fileName = args[1]
  100. if len(args) >= 3:
  101. self.line = args[2]
  102. if len(args) >= 4:
  103. self.column = args[3]
  104. def __str__(self):
  105. buf = ['']
  106. if self.fileName:
  107. buf.append(self.fileName + ":")
  108. if self.line != -1:
  109. if not self.fileName:
  110. buf.append("line ")
  111. buf.append(str(self.line))
  112. if self.column != -1:
  113. buf.append(":" + str(self.column))
  114. buf.append(":")
  115. buf.append(" ")
  116. return str('').join(buf)
  117. __repr__ = __str__
  118. class NoViableAltException(RecognitionException):
  119. def __init__(self, *args):
  120. RecognitionException.__init__(self, *args)
  121. self.token = None
  122. self.node = None
  123. if isinstance(args[0],AST):
  124. self.node = args[0]
  125. elif isinstance(args[0],Token):
  126. self.token = args[0]
  127. else:
  128. raise TypeError("NoViableAltException requires Token or AST argument")
  129. def __str__(self):
  130. if self.token:
  131. line = self.token.getLine()
  132. col = self.token.getColumn()
  133. text = self.token.getText()
  134. return "unexpected symbol at line %s (column %s): \"%s\"" % (line,col,text)
  135. if self.node == ASTNULL:
  136. return "unexpected end of subtree"
  137. assert self.node
  138. ### hackish, we assume that an AST contains method getText
  139. return "unexpected node: %s" % (self.node.getText())
  140. __repr__ = __str__
  141. class NoViableAltForCharException(RecognitionException):
  142. def __init__(self, *args):
  143. self.foundChar = None
  144. if len(args) == 2:
  145. self.foundChar = args[0]
  146. scanner = args[1]
  147. RecognitionException.__init__(self, "NoViableAlt",
  148. scanner.getFilename(),
  149. scanner.getLine(),
  150. scanner.getColumn())
  151. elif len(args) == 4:
  152. self.foundChar = args[0]
  153. fileName = args[1]
  154. line = args[2]
  155. column = args[3]
  156. RecognitionException.__init__(self, "NoViableAlt",
  157. fileName, line, column)
  158. else:
  159. RecognitionException.__init__(self, "NoViableAlt",
  160. '', -1, -1)
  161. def __str__(self):
  162. mesg = "unexpected char: "
  163. if self.foundChar >= ' ' and self.foundChar <= '~':
  164. mesg += "'" + self.foundChar + "'"
  165. elif self.foundChar:
  166. mesg += "0x" + hex(ord(self.foundChar)).upper()[2:]
  167. else:
  168. mesg += "<None>"
  169. return mesg
  170. __repr__ = __str__
  171. class SemanticException(RecognitionException):
  172. def __init__(self, *args):
  173. RecognitionException.__init__(self, *args)
  174. class MismatchedCharException(RecognitionException):
  175. NONE = 0
  176. CHAR = 1
  177. NOT_CHAR = 2
  178. RANGE = 3
  179. NOT_RANGE = 4
  180. SET = 5
  181. NOT_SET = 6
  182. def __init__(self, *args):
  183. self.args = args
  184. if len(args) == 5:
  185. # Expected range / not range
  186. if args[3]:
  187. self.mismatchType = MismatchedCharException.NOT_RANGE
  188. else:
  189. self.mismatchType = MismatchedCharException.RANGE
  190. self.foundChar = args[0]
  191. self.expecting = args[1]
  192. self.upper = args[2]
  193. self.scanner = args[4]
  194. RecognitionException.__init__(self, "Mismatched char range",
  195. self.scanner.getFilename(),
  196. self.scanner.getLine(),
  197. self.scanner.getColumn())
  198. elif len(args) == 4 and is_string_type(args[1]):
  199. # Expected char / not char
  200. if args[2]:
  201. self.mismatchType = MismatchedCharException.NOT_CHAR
  202. else:
  203. self.mismatchType = MismatchedCharException.CHAR
  204. self.foundChar = args[0]
  205. self.expecting = args[1]
  206. self.scanner = args[3]
  207. RecognitionException.__init__(self, "Mismatched char",
  208. self.scanner.getFilename(),
  209. self.scanner.getLine(),
  210. self.scanner.getColumn())
  211. elif len(args) == 4 and isinstance(args[1], BitSet):
  212. # Expected BitSet / not BitSet
  213. if args[2]:
  214. self.mismatchType = MismatchedCharException.NOT_SET
  215. else:
  216. self.mismatchType = MismatchedCharException.SET
  217. self.foundChar = args[0]
  218. self.set = args[1]
  219. self.scanner = args[3]
  220. RecognitionException.__init__(self, "Mismatched char set",
  221. self.scanner.getFilename(),
  222. self.scanner.getLine(),
  223. self.scanner.getColumn())
  224. else:
  225. self.mismatchType = MismatchedCharException.NONE
  226. RecognitionException.__init__(self, "Mismatched char")
  227. ## Append a char to the msg buffer. If special,
  228. # then show escaped version
  229. #
  230. def appendCharName(self, sb, c):
  231. if not c or c == 65535:
  232. # 65535 = (char) -1 = EOF
  233. sb.append("'<EOF>'")
  234. elif c == '\n':
  235. sb.append("'\\n'")
  236. elif c == '\r':
  237. sb.append("'\\r'");
  238. elif c == '\t':
  239. sb.append("'\\t'")
  240. else:
  241. sb.append('\'' + c + '\'')
  242. ##
  243. # Returns an error message with line number/column information
  244. #
  245. def __str__(self):
  246. sb = ['']
  247. sb.append(RecognitionException.__str__(self))
  248. if self.mismatchType == MismatchedCharException.CHAR:
  249. sb.append("expecting ")
  250. self.appendCharName(sb, self.expecting)
  251. sb.append(", found ")
  252. self.appendCharName(sb, self.foundChar)
  253. elif self.mismatchType == MismatchedCharException.NOT_CHAR:
  254. sb.append("expecting anything but '")
  255. self.appendCharName(sb, self.expecting)
  256. sb.append("'; got it anyway")
  257. elif self.mismatchType in [MismatchedCharException.RANGE, MismatchedCharException.NOT_RANGE]:
  258. sb.append("expecting char ")
  259. if self.mismatchType == MismatchedCharException.NOT_RANGE:
  260. sb.append("NOT ")
  261. sb.append("in range: ")
  262. self.appendCharName(sb, self.expecting)
  263. sb.append("..")
  264. self.appendCharName(sb, self.upper)
  265. sb.append(", found ")
  266. self.appendCharName(sb, self.foundChar)
  267. elif self.mismatchType in [MismatchedCharException.SET, MismatchedCharException.NOT_SET]:
  268. sb.append("expecting ")
  269. if self.mismatchType == MismatchedCharException.NOT_SET:
  270. sb.append("NOT ")
  271. sb.append("one of (")
  272. for i in range(len(self.set)):
  273. self.appendCharName(sb, self.set[i])
  274. sb.append("), found ")
  275. self.appendCharName(sb, self.foundChar)
  276. return str().join(sb).strip()
  277. __repr__ = __str__
  278. class MismatchedTokenException(RecognitionException):
  279. NONE = 0
  280. TOKEN = 1
  281. NOT_TOKEN = 2
  282. RANGE = 3
  283. NOT_RANGE = 4
  284. SET = 5
  285. NOT_SET = 6
  286. def __init__(self, *args):
  287. self.args = args
  288. self.tokenNames = []
  289. self.token = None
  290. self.tokenText = ''
  291. self.node = None
  292. if len(args) == 6:
  293. # Expected range / not range
  294. if args[3]:
  295. self.mismatchType = MismatchedTokenException.NOT_RANGE
  296. else:
  297. self.mismatchType = MismatchedTokenException.RANGE
  298. self.tokenNames = args[0]
  299. self.expecting = args[2]
  300. self.upper = args[3]
  301. self.fileName = args[5]
  302. elif len(args) == 4 and isinstance(args[2], int):
  303. # Expected token / not token
  304. if args[3]:
  305. self.mismatchType = MismatchedTokenException.NOT_TOKEN
  306. else:
  307. self.mismatchType = MismatchedTokenException.TOKEN
  308. self.tokenNames = args[0]
  309. self.expecting = args[2]
  310. elif len(args) == 4 and isinstance(args[2], BitSet):
  311. # Expected BitSet / not BitSet
  312. if args[3]:
  313. self.mismatchType = MismatchedTokenException.NOT_SET
  314. else:
  315. self.mismatchType = MismatchedTokenException.SET
  316. self.tokenNames = args[0]
  317. self.set = args[2]
  318. else:
  319. self.mismatchType = MismatchedTokenException.NONE
  320. RecognitionException.__init__(self, "Mismatched Token: expecting any AST node", "<AST>", -1, -1)
  321. if len(args) >= 2:
  322. if isinstance(args[1],Token):
  323. self.token = args[1]
  324. self.tokenText = self.token.getText()
  325. RecognitionException.__init__(self, "Mismatched Token",
  326. self.fileName,
  327. self.token.getLine(),
  328. self.token.getColumn())
  329. elif isinstance(args[1],AST):
  330. self.node = args[1]
  331. self.tokenText = str(self.node)
  332. RecognitionException.__init__(self, "Mismatched Token",
  333. "<AST>",
  334. self.node.getLine(),
  335. self.node.getColumn())
  336. else:
  337. self.tokenText = "<empty tree>"
  338. RecognitionException.__init__(self, "Mismatched Token",
  339. "<AST>", -1, -1)
  340. def appendTokenName(self, sb, tokenType):
  341. if tokenType == INVALID_TYPE:
  342. sb.append("<Set of tokens>")
  343. elif tokenType < 0 or tokenType >= len(self.tokenNames):
  344. sb.append("<" + str(tokenType) + ">")
  345. else:
  346. sb.append(self.tokenNames[tokenType])
  347. ##
  348. # Returns an error message with line number/column information
  349. #
  350. def __str__(self):
  351. sb = ['']
  352. sb.append(RecognitionException.__str__(self))
  353. if self.mismatchType == MismatchedTokenException.TOKEN:
  354. sb.append("expecting ")
  355. self.appendTokenName(sb, self.expecting)
  356. sb.append(", found " + self.tokenText)
  357. elif self.mismatchType == MismatchedTokenException.NOT_TOKEN:
  358. sb.append("expecting anything but '")
  359. self.appendTokenName(sb, self.expecting)
  360. sb.append("'; got it anyway")
  361. elif self.mismatchType in [MismatchedTokenException.RANGE, MismatchedTokenException.NOT_RANGE]:
  362. sb.append("expecting token ")
  363. if self.mismatchType == MismatchedTokenException.NOT_RANGE:
  364. sb.append("NOT ")
  365. sb.append("in range: ")
  366. self.appendTokenName(sb, self.expecting)
  367. sb.append("..")
  368. self.appendTokenName(sb, self.upper)
  369. sb.append(", found " + self.tokenText)
  370. elif self.mismatchType in [MismatchedTokenException.SET, MismatchedTokenException.NOT_SET]:
  371. sb.append("expecting ")
  372. if self.mismatchType == MismatchedTokenException.NOT_SET:
  373. sb.append("NOT ")
  374. sb.append("one of (")
  375. for i in range(len(self.set)):
  376. self.appendTokenName(sb, self.set[i])
  377. sb.append("), found " + self.tokenText)
  378. return str().join(sb).strip()
  379. __repr__ = __str__
  380. class TokenStreamException(ANTLRException):
  381. def __init__(self, *args):
  382. ANTLRException.__init__(self, *args)
  383. # Wraps an Exception in a TokenStreamException
  384. class TokenStreamIOException(TokenStreamException):
  385. def __init__(self, *args):
  386. if args and isinstance(args[0], Exception):
  387. io = args[0]
  388. TokenStreamException.__init__(self, str(io))
  389. self.io = io
  390. else:
  391. TokenStreamException.__init__(self, *args)
  392. self.io = self
  393. # Wraps a RecognitionException in a TokenStreamException
  394. class TokenStreamRecognitionException(TokenStreamException):
  395. def __init__(self, *args):
  396. if args and isinstance(args[0], RecognitionException):
  397. recog = args[0]
  398. TokenStreamException.__init__(self, str(recog))
  399. self.recog = recog
  400. else:
  401. raise TypeError("TokenStreamRecognitionException requires RecognitionException argument")
  402. def __str__(self):
  403. return str(self.recog)
  404. __repr__ = __str__
  405. class TokenStreamRetryException(TokenStreamException):
  406. def __init__(self, *args):
  407. TokenStreamException.__init__(self, *args)
  408. class CharStreamException(ANTLRException):
  409. def __init__(self, *args):
  410. ANTLRException.__init__(self, *args)
  411. # Wraps an Exception in a CharStreamException
  412. class CharStreamIOException(CharStreamException):
  413. def __init__(self, *args):
  414. if args and isinstance(args[0], Exception):
  415. io = args[0]
  416. CharStreamException.__init__(self, str(io))
  417. self.io = io
  418. else:
  419. CharStreamException.__init__(self, *args)
  420. self.io = self
  421. class TryAgain(Exception):
  422. pass
  423. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  424. ### Token ###
  425. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  426. class Token(object):
  427. SKIP = -1
  428. INVALID_TYPE = 0
  429. EOF_TYPE = 1
  430. EOF = 1
  431. NULL_TREE_LOOKAHEAD = 3
  432. MIN_USER_TYPE = 4
  433. def __init__(self,**argv):
  434. try:
  435. self.type = argv['type']
  436. except:
  437. self.type = INVALID_TYPE
  438. try:
  439. self.text = argv['text']
  440. except:
  441. self.text = "<no text>"
  442. def isEOF(self):
  443. return (self.type == EOF_TYPE)
  444. def getColumn(self):
  445. return 0
  446. def getLine(self):
  447. return 0
  448. def getFilename(self):
  449. return None
  450. def setFilename(self,name):
  451. return self
  452. def getText(self):
  453. return "<no text>"
  454. def setText(self,text):
  455. if is_string_type(text):
  456. pass
  457. else:
  458. raise TypeError("Token.setText requires string argument")
  459. return self
  460. def setColumn(self,column):
  461. return self
  462. def setLine(self,line):
  463. return self
  464. def getType(self):
  465. return self.type
  466. def setType(self,type):
  467. if isinstance(type,int):
  468. self.type = type
  469. else:
  470. raise TypeError("Token.setType requires integer argument")
  471. return self
  472. def toString(self):
  473. ## not optimal
  474. type_ = self.type
  475. if type_ == 3:
  476. tval = 'NULL_TREE_LOOKAHEAD'
  477. elif type_ == 1:
  478. tval = 'EOF_TYPE'
  479. elif type_ == 0:
  480. tval = 'INVALID_TYPE'
  481. elif type_ == -1:
  482. tval = 'SKIP'
  483. else:
  484. tval = type_
  485. return '["%s",<%s>]' % (self.getText(),tval)
  486. __str__ = toString
  487. __repr__ = toString
  488. ### static attribute ..
  489. Token.badToken = Token( type=INVALID_TYPE, text="<no text>")
  490. if __name__ == "__main__":
  491. print("testing ..")
  492. T = Token.badToken
  493. print(T)
  494. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  495. ### CommonToken ###
  496. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  497. class CommonToken(Token):
  498. def __init__(self,**argv):
  499. Token.__init__(self,**argv)
  500. self.line = 0
  501. self.col = 0
  502. try:
  503. self.line = argv['line']
  504. except:
  505. pass
  506. try:
  507. self.col = argv['col']
  508. except:
  509. pass
  510. def getLine(self):
  511. return self.line
  512. def getText(self):
  513. return self.text
  514. def getColumn(self):
  515. return self.col
  516. def setLine(self,line):
  517. self.line = line
  518. return self
  519. def setText(self,text):
  520. self.text = text
  521. return self
  522. def setColumn(self,col):
  523. self.col = col
  524. return self
  525. def toString(self):
  526. ## not optimal
  527. type_ = self.type
  528. if type_ == 3:
  529. tval = 'NULL_TREE_LOOKAHEAD'
  530. elif type_ == 1:
  531. tval = 'EOF_TYPE'
  532. elif type_ == 0:
  533. tval = 'INVALID_TYPE'
  534. elif type_ == -1:
  535. tval = 'SKIP'
  536. else:
  537. tval = type_
  538. d = {
  539. 'text' : self.text,
  540. 'type' : tval,
  541. 'line' : self.line,
  542. 'colm' : self.col
  543. }
  544. fmt = '["%(text)s",<%(type)s>,line=%(line)s,col=%(colm)s]'
  545. return fmt % d
  546. __str__ = toString
  547. __repr__ = toString
  548. if __name__ == '__main__' :
  549. T = CommonToken()
  550. print(T)
  551. T = CommonToken(col=15,line=1,text="some text", type=5)
  552. print(T)
  553. T = CommonToken()
  554. T.setLine(1).setColumn(15).setText("some text").setType(5)
  555. print(T)
  556. print(T.getLine())
  557. print(T.getColumn())
  558. print(T.getText())
  559. print(T.getType())
  560. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  561. ### CommonHiddenStreamToken ###
  562. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  563. class CommonHiddenStreamToken(CommonToken):
  564. def __init__(self,*args):
  565. CommonToken.__init__(self,*args)
  566. self.hiddenBefore = None
  567. self.hiddenAfter = None
  568. def getHiddenAfter(self):
  569. return self.hiddenAfter
  570. def getHiddenBefore(self):
  571. return self.hiddenBefore
  572. def setHiddenAfter(self,t):
  573. self.hiddenAfter = t
  574. def setHiddenBefore(self, t):
  575. self.hiddenBefore = t
  576. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  577. ### Queue ###
  578. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  579. ## Shall be a circular buffer on tokens ..
  580. class Queue(object):
  581. def __init__(self):
  582. self.buffer = [] # empty list
  583. def append(self,item):
  584. self.buffer.append(item)
  585. def elementAt(self,index):
  586. return self.buffer[index]
  587. def reset(self):
  588. self.buffer = []
  589. def removeFirst(self):
  590. self.buffer.pop(0)
  591. def length(self):
  592. return len(self.buffer)
  593. def __str__(self):
  594. return str(self.buffer)
  595. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  596. ### InputBuffer ###
  597. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  598. class InputBuffer(object):
  599. def __init__(self):
  600. self.nMarkers = 0
  601. self.markerOffset = 0
  602. self.numToConsume = 0
  603. self.queue = Queue()
  604. def __str__(self):
  605. return "(%s,%s,%s,%s)" % (
  606. self.nMarkers,
  607. self.markerOffset,
  608. self.numToConsume,
  609. self.queue)
  610. def __repr__(self):
  611. return str(self)
  612. def commit(self):
  613. self.nMarkers -= 1
  614. def consume(self) :
  615. self.numToConsume += 1
  616. ## probably better to return a list of items
  617. ## because of unicode. Or return a unicode
  618. ## string ..
  619. def getLAChars(self) :
  620. i = self.markerOffset
  621. n = self.queue.length()
  622. s = ''
  623. while i<n:
  624. s += self.queue.elementAt(i)
  625. return s
  626. ## probably better to return a list of items
  627. ## because of unicode chars
  628. def getMarkedChars(self) :
  629. s = ''
  630. i = 0
  631. n = self.markerOffset
  632. while i<n:
  633. s += self.queue.elementAt(i)
  634. return s
  635. def isMarked(self) :
  636. return self.nMarkers != 0
  637. def fill(self,k):
  638. ### abstract method
  639. raise NotImplementedError()
  640. def LA(self,k) :
  641. self.fill(k)
  642. return self.queue.elementAt(self.markerOffset + k - 1)
  643. def mark(self) :
  644. self.syncConsume()
  645. self.nMarkers += 1
  646. return self.markerOffset
  647. def rewind(self,mark) :
  648. self.syncConsume()
  649. self.markerOffset = mark
  650. self.nMarkers -= 1
  651. def reset(self) :
  652. self.nMarkers = 0
  653. self.markerOffset = 0
  654. self.numToConsume = 0
  655. self.queue.reset()
  656. def syncConsume(self) :
  657. while self.numToConsume > 0:
  658. if self.nMarkers > 0:
  659. # guess mode -- leave leading characters and bump offset.
  660. self.markerOffset += 1
  661. else:
  662. # normal mode -- remove first character
  663. self.queue.removeFirst()
  664. self.numToConsume -= 1
  665. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  666. ### CharBuffer ###
  667. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  668. class CharBuffer(InputBuffer):
  669. def __init__(self,reader):
  670. ##assert isinstance(reader,file)
  671. super(CharBuffer,self).__init__()
  672. ## a reader is supposed to be anything that has
  673. ## a method 'read(int)'.
  674. self.input = reader
  675. def __str__(self):
  676. base = super(CharBuffer,self).__str__()
  677. return "CharBuffer{%s,%s" % (base,str(input))
  678. def fill(self,amount):
  679. try:
  680. self.syncConsume()
  681. while self.queue.length() < (amount + self.markerOffset) :
  682. ## retrieve just one char - what happend at end
  683. ## of input?
  684. c = self.input.read(1)
  685. ### python's behaviour is to return the empty string on
  686. ### EOF, ie. no exception whatsoever is thrown. An empty
  687. ### python string has the nice feature that it is of
  688. ### type 'str' and "not ''" would return true. Contrary,
  689. ### one can't do this: '' in 'abc'. This should return
  690. ### false, but all we get is then a TypeError as an
  691. ### empty string is not a character.
  692. ### Let's assure then that we have either seen a
  693. ### character or an empty string (EOF).
  694. assert len(c) == 0 or len(c) == 1
  695. ### And it shall be of type string (ASCII or UNICODE).
  696. assert is_string_type(c)
  697. ### Just append EOF char to buffer. Note that buffer may
  698. ### contain then just more than one EOF char ..
  699. ### use unicode chars instead of ASCII ..
  700. self.queue.append(c)
  701. except Exception as e:
  702. raise CharStreamIOException(e)
  703. ##except: # (mk) Cannot happen ...
  704. ##error ("unexpected exception caught ..")
  705. ##assert 0
  706. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  707. ### LexerSharedInputState ###
  708. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  709. class LexerSharedInputState(object):
  710. def __init__(self,ibuf):
  711. assert isinstance(ibuf,InputBuffer)
  712. self.input = ibuf
  713. self.column = 1
  714. self.line = 1
  715. self.tokenStartColumn = 1
  716. self.tokenStartLine = 1
  717. self.guessing = 0
  718. self.filename = None
  719. def reset(self):
  720. self.column = 1
  721. self.line = 1
  722. self.tokenStartColumn = 1
  723. self.tokenStartLine = 1
  724. self.guessing = 0
  725. self.filename = None
  726. self.input.reset()
  727. def LA(self,k):
  728. return self.input.LA(k)
  729. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  730. ### TokenStream ###
  731. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  732. class TokenStream(object):
  733. def nextToken(self):
  734. pass
  735. def __iter__(self):
  736. return TokenStreamIterator(self)
  737. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  738. ### TokenStreamIterator ###
  739. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  740. class TokenStreamIterator(object):
  741. def __init__(self,inst):
  742. if isinstance(inst,TokenStream):
  743. self.inst = inst
  744. return
  745. raise TypeError("TokenStreamIterator requires TokenStream object")
  746. def next(self):
  747. assert self.inst
  748. item = self.inst.nextToken()
  749. if not item or item.isEOF():
  750. raise StopIteration()
  751. return item
  752. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  753. ### TokenStreamSelector ###
  754. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  755. class TokenStreamSelector(TokenStream):
  756. def __init__(self):
  757. self._input = None
  758. self._stmap = {}
  759. self._stack = []
  760. def addInputStream(self,stream,key):
  761. self._stmap[key] = stream
  762. def getCurrentStream(self):
  763. return self._input
  764. def getStream(self,sname):
  765. try:
  766. stream = self._stmap[sname]
  767. except:
  768. raise ValueError("TokenStream " + sname + " not found");
  769. return stream;
  770. def nextToken(self):
  771. while 1:
  772. try:
  773. return self._input.nextToken()
  774. except TokenStreamRetryException:
  775. ### just retry "forever"
  776. pass
  777. def pop(self):
  778. stream = self._stack.pop();
  779. self.select(stream);
  780. return stream;
  781. def push(self,arg):
  782. self._stack.append(self._input);
  783. self.select(arg)
  784. def retry(self):
  785. raise TokenStreamRetryException()
  786. def select(self,arg):
  787. if isinstance(arg,TokenStream):
  788. self._input = arg
  789. return
  790. if is_string_type(arg):
  791. self._input = self.getStream(arg)
  792. return
  793. raise TypeError("TokenStreamSelector.select requires " +
  794. "TokenStream or string argument")
  795. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  796. ### TokenStreamBasicFilter ###
  797. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  798. class TokenStreamBasicFilter(TokenStream):
  799. def __init__(self,input):
  800. self.input = input;
  801. self.discardMask = BitSet()
  802. def discard(self,arg):
  803. if isinstance(arg,int):
  804. self.discardMask.add(arg)
  805. return
  806. if isinstance(arg,BitSet):
  807. self.discardMark = arg
  808. return
  809. raise TypeError("TokenStreamBasicFilter.discard requires" +
  810. "integer or BitSet argument")
  811. def nextToken(self):
  812. tok = self.input.nextToken()
  813. while tok and self.discardMask.member(tok.getType()):
  814. tok = self.input.nextToken()
  815. return tok
  816. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  817. ### TokenStreamHiddenTokenFilter ###
  818. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  819. class TokenStreamHiddenTokenFilter(TokenStreamBasicFilter):
  820. def __init__(self,input):
  821. TokenStreamBasicFilter.__init__(self,input)
  822. self.hideMask = BitSet()
  823. self.nextMonitoredToken = None
  824. self.lastHiddenToken = None
  825. self.firstHidden = None
  826. def consume(self):
  827. self.nextMonitoredToken = self.input.nextToken()
  828. def consumeFirst(self):
  829. self.consume()
  830. p = None;
  831. while self.hideMask.member(self.LA(1).getType()) or \
  832. self.discardMask.member(self.LA(1).getType()):
  833. if self.hideMask.member(self.LA(1).getType()):
  834. if not p:
  835. p = self.LA(1)
  836. else:
  837. p.setHiddenAfter(self.LA(1))
  838. self.LA(1).setHiddenBefore(p)
  839. p = self.LA(1)
  840. self.lastHiddenToken = p
  841. if not self.firstHidden:
  842. self.firstHidden = p
  843. self.consume()
  844. def getDiscardMask(self):
  845. return self.discardMask
  846. def getHiddenAfter(self,t):
  847. return t.getHiddenAfter()
  848. def getHiddenBefore(self,t):
  849. return t.getHiddenBefore()
  850. def getHideMask(self):
  851. return self.hideMask
  852. def getInitialHiddenToken(self):
  853. return self.firstHidden
  854. def hide(self,m):
  855. if isinstance(m,int):
  856. self.hideMask.add(m)
  857. return
  858. if isinstance(m.BitMask):
  859. self.hideMask = m
  860. return
  861. def LA(self,i):
  862. return self.nextMonitoredToken
  863. def nextToken(self):
  864. if not self.LA(1):
  865. self.consumeFirst()
  866. monitored = self.LA(1)
  867. monitored.setHiddenBefore(self.lastHiddenToken)
  868. self.lastHiddenToken = None
  869. self.consume()
  870. p = monitored
  871. while self.hideMask.member(self.LA(1).getType()) or \
  872. self.discardMask.member(self.LA(1).getType()):
  873. if self.hideMask.member(self.LA(1).getType()):
  874. p.setHiddenAfter(self.LA(1))
  875. if p != monitored:
  876. self.LA(1).setHiddenBefore(p)
  877. p = self.lastHiddenToken = self.LA(1)
  878. self.consume()
  879. return monitored
  880. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  881. ### StringBuffer ###
  882. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  883. class StringBuffer:
  884. def __init__(self,string=None):
  885. if string:
  886. self.text = list(string)
  887. else:
  888. self.text = []
  889. def setLength(self,sz):
  890. if not sz :
  891. self.text = []
  892. return
  893. assert sz>0
  894. if sz >= self.length():
  895. return
  896. ### just reset to empty buffer
  897. self.text = self.text[0:sz]
  898. def length(self):
  899. return len(self.text)
  900. def append(self,c):
  901. self.text.append(c)
  902. ### return buffer as string. Arg 'a' is used as index
  903. ## into the buffer and 2nd argument shall be the length.
  904. ## If 2nd args is absent, we return chars till end of
  905. ## buffer starting with 'a'.
  906. def getString(self,a=None,length=None):
  907. if not a :
  908. a = 0
  909. assert a>=0
  910. if a>= len(self.text) :
  911. return ""
  912. if not length:
  913. ## no second argument
  914. L = self.text[a:]
  915. else:
  916. assert (a+length) <= len(self.text)
  917. b = a + length
  918. L = self.text[a:b]
  919. s = ""
  920. for x in L : s += x
  921. return s
  922. toString = getString ## alias
  923. def __str__(self):
  924. return str(self.text)
  925. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  926. ### Reader ###
  927. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  928. ## When reading Japanese chars, it happens that a stream returns a
  929. ## 'char' of length 2. This looks like a bug in the appropriate
  930. ## codecs - but I'm rather unsure about this. Anyway, if this is
  931. ## the case, I'm going to split this string into a list of chars
  932. ## and put them on hold, ie. on a buffer. Next time when called
  933. ## we read from buffer until buffer is empty.
  934. ## wh: nov, 25th -> problem does not appear in Python 2.4.0.c1.
  935. class Reader(object):
  936. def __init__(self,stream):
  937. self.cin = stream
  938. self.buf = []
  939. def read(self,num):
  940. assert num==1
  941. if len(self.buf):
  942. return self.buf.pop()
  943. ## Read a char - this may return a string.
  944. ## Is this a bug in codecs/Python?
  945. c = self.cin.read(1)
  946. if not c or len(c)==1:
  947. return c
  948. L = list(c)
  949. L.reverse()
  950. for x in L:
  951. self.buf.append(x)
  952. ## read one char ..
  953. return self.read(1)
  954. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  955. ### CharScanner ###
  956. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  957. class CharScanner(TokenStream):
  958. ## class members
  959. NO_CHAR = 0
  960. EOF_CHAR = '' ### EOF shall be the empty string.
  961. def __init__(self, *argv, **kwargs):
  962. super(CharScanner, self).__init__()
  963. self.saveConsumedInput = True
  964. self.tokenClass = None
  965. self.caseSensitive = True
  966. self.caseSensitiveLiterals = True
  967. self.literals = None
  968. self.tabsize = 8
  969. self._returnToken = None
  970. self.commitToPath = False
  971. self.traceDepth = 0
  972. self.text = StringBuffer()
  973. self.hashString = hash(self)
  974. self.setTokenObjectClass(CommonToken)
  975. self.setInput(*argv)
  976. def __iter__(self):
  977. return CharScannerIterator(self)
  978. def setInput(self,*argv):
  979. ## case 1:
  980. ## if there's no arg we default to read from
  981. ## standard input
  982. if not argv:
  983. import sys
  984. self.setInput(sys.stdin)
  985. return
  986. ## get 1st argument
  987. arg1 = argv[0]
  988. ## case 2:
  989. ## if arg1 is a string, we assume it's a file name
  990. ## and open a stream using 2nd argument as open
  991. ## mode. If there's no 2nd argument we fall back to
  992. ## mode '+rb'.
  993. if is_string_type(arg1):
  994. f = open(arg1,"rb")
  995. self.setInput(f)
  996. self.setFilename(arg1)
  997. return
  998. ## case 3:
  999. ## if arg1 is a file we wrap it by a char buffer (
  1000. ## some additional checks?? No, can't do this in
  1001. ## general).
  1002. if isinstance(arg1,file):
  1003. self.setInput(CharBuffer(arg1))
  1004. return
  1005. ## case 4:
  1006. ## if arg1 is of type SharedLexerInputState we use
  1007. ## argument as is.
  1008. if isinstance(arg1,LexerSharedInputState):
  1009. self.inputState = arg1
  1010. return
  1011. ## case 5:
  1012. ## check whether argument type is of type input
  1013. ## buffer. If so create a SharedLexerInputState and
  1014. ## go ahead.
  1015. if isinstance(arg1,InputBuffer):
  1016. self.setInput(LexerSharedInputState(arg1))
  1017. return
  1018. ## case 6:
  1019. ## check whether argument type has a method read(int)
  1020. ## If so create CharBuffer ...
  1021. try:
  1022. if arg1.read:
  1023. rd = Reader(arg1)
  1024. cb = CharBuffer(rd)
  1025. ss = LexerSharedInputState(cb)
  1026. self.inputState = ss
  1027. return
  1028. except:
  1029. pass
  1030. ## case 7:
  1031. ## raise wrong argument exception
  1032. raise TypeError(argv)
  1033. def setTabSize(self,size) :
  1034. self.tabsize = size
  1035. def getTabSize(self) :
  1036. return self.tabsize
  1037. def setCaseSensitive(self,t) :
  1038. self.caseSensitive = t
  1039. def setCommitToPath(self,commit) :
  1040. self.commitToPath = commit
  1041. def setFilename(self,f) :
  1042. self.inputState.filename = f
  1043. def setLine(self,line) :
  1044. self.inputState.line = line
  1045. def setText(self,s) :
  1046. self.resetText()
  1047. self.text.append(s)
  1048. def getCaseSensitive(self) :
  1049. return self.caseSensitive
  1050. def getCaseSensitiveLiterals(self) :
  1051. return self.caseSensitiveLiterals
  1052. def getColumn(self) :
  1053. return self.inputState.column
  1054. def setColumn(self,c) :
  1055. self.inputState.column = c
  1056. def getCommitToPath(self) :
  1057. return self.commitToPath
  1058. def getFilename(self) :
  1059. return self.inputState.filename
  1060. def getInputBuffer(self) :
  1061. return self.inputState.input
  1062. def getInputState(self) :
  1063. return self.inputState
  1064. def setInputState(self,state) :
  1065. assert isinstance(state,LexerSharedInputState)
  1066. self.inputState = state
  1067. def getLine(self) :
  1068. return self.inputState.line
  1069. def getText(self) :
  1070. return str(self.text)
  1071. def getTokenObject(self) :
  1072. return self._returnToken
  1073. def LA(self,i) :
  1074. c = self.inputState.input.LA(i)
  1075. if not self.caseSensitive:
  1076. ### E0006
  1077. c = c.__class__.lower(c)
  1078. return c
  1079. def makeToken(self,type) :
  1080. try:
  1081. ## dynamically load a class
  1082. assert self.tokenClass
  1083. tok = self.tokenClass()
  1084. tok.setType(type)
  1085. tok.setColumn(self.inputState.tokenStartColumn)
  1086. tok.setLine(self.inputState.tokenStartLine)
  1087. return tok
  1088. except:
  1089. self.panic("unable to create new token")
  1090. return Token.badToken
  1091. def mark(self) :
  1092. return self.inputState.input.mark()
  1093. def _match_bitset(self,b) :
  1094. if b.member(self.LA(1)):
  1095. self.consume()
  1096. else:
  1097. raise MismatchedCharException(self.LA(1), b, False, self)
  1098. def _match_string(self,s) :
  1099. for c in s:
  1100. if self.LA(1) == c:
  1101. self.consume()
  1102. else:
  1103. raise MismatchedCharException(self.LA(1), c, False, self)
  1104. def match(self,item):
  1105. if is_string_type(item):
  1106. return self._match_string(item)
  1107. else:
  1108. return self._match_bitset(item)
  1109. def matchNot(self,c) :
  1110. if self.LA(1) != c:
  1111. self.consume()
  1112. else:
  1113. raise MismatchedCharException(self.LA(1), c, True, self)
  1114. def matchRange(self,c1,c2) :
  1115. if self.LA(1) < c1 or self.LA(1) > c2 :
  1116. raise MismatchedCharException(self.LA(1), c1, c2, False, self)
  1117. else:
  1118. self.consume()
  1119. def newline(self) :
  1120. self.inputState.line += 1
  1121. self.inputState.column = 1
  1122. def tab(self) :
  1123. c = self.getColumn()
  1124. nc = ( ((c-1)/self.tabsize) + 1) * self.tabsize + 1
  1125. self.setColumn(nc)
  1126. def panic(self,s='') :
  1127. print("CharScanner: panic: " + s)
  1128. sys.exit(1)
  1129. def reportError(self,s) :
  1130. if not self.getFilename():
  1131. print("error: " + str(s))
  1132. else:
  1133. print(self.getFilename() + ": error: " + str(s))
  1134. def reportWarning(self,s) :
  1135. if not self.getFilename():
  1136. print("warning: " + str(s))
  1137. else:
  1138. print(self.getFilename() + ": warning: " + str(s))
  1139. def resetText(self) :
  1140. self.text.setLength(0)
  1141. self.inputState.tokenStartColumn = self.inputState.column
  1142. self.inputState.tokenStartLine = self.inputState.line
  1143. def rewind(self,pos) :
  1144. self.inputState.input.rewind(pos)
  1145. def setTokenObjectClass(self,cl):
  1146. self.tokenClass = cl
  1147. def testForLiteral(self,token):
  1148. if not token:
  1149. return
  1150. assert isinstance(token,Token)
  1151. _type = token.getType()
  1152. ## special tokens can't be literals
  1153. if _type in [SKIP,INVALID_TYPE,EOF_TYPE,NULL_TREE_LOOKAHEAD] :
  1154. return
  1155. _text = token.getText()
  1156. if not _text:
  1157. return
  1158. assert is_string_type(_text)
  1159. _type = self.testLiteralsTable(_text,_type)
  1160. token.setType(_type)
  1161. return _type
  1162. def testLiteralsTable(self,*args):
  1163. if is_string_type(args[0]):
  1164. s = args[0]
  1165. i = args[1]
  1166. else:
  1167. s = self.text.getString()
  1168. i = args[0]
  1169. ## check whether integer has been given
  1170. if not isinstance(i,int):
  1171. assert isinstance(i,int)
  1172. ## check whether we have a dict
  1173. assert isinstance(self.literals,dict)
  1174. try:
  1175. ## E0010
  1176. if not self.caseSensitiveLiterals:
  1177. s = s.__class__.lower(s)
  1178. i = self.literals[s]
  1179. except:
  1180. pass
  1181. return i
  1182. def toLower(self,c):
  1183. return c.__class__.lower()
  1184. def traceIndent(self):
  1185. print(' ' * self.traceDepth)
  1186. def traceIn(self,rname):
  1187. self.traceDepth += 1
  1188. self.traceIndent()
  1189. print("> lexer %s c== %s" % (rname,self.LA(1)))
  1190. def traceOut(self,rname):
  1191. self.traceIndent()
  1192. print("< lexer %s c== %s" % (rname,self.LA(1)))
  1193. self.traceDepth -= 1
  1194. def uponEOF(self):
  1195. pass
  1196. def append(self,c):
  1197. if self.saveConsumedInput :
  1198. self.text.append(c)
  1199. def commit(self):
  1200. self.inputState.input.commit()
  1201. def consume(self):
  1202. if not self.inputState.guessing:
  1203. c = self.LA(1)
  1204. if self.caseSensitive:
  1205. self.append(c)
  1206. else:
  1207. # use input.LA(), not LA(), to get original case
  1208. # CharScanner.LA() would toLower it.
  1209. c = self.inputState.input.LA(1)
  1210. self.append(c)
  1211. if c and c in "\t":
  1212. self.tab()
  1213. else:
  1214. self.inputState.column += 1
  1215. self.inputState.input.consume()
  1216. ## Consume chars until one matches the given char
  1217. def consumeUntil_char(self,c):
  1218. while self.LA(1) != EOF_CHAR and self.LA(1) != c:
  1219. self.consume()
  1220. ## Consume chars until one matches the given set
  1221. def consumeUntil_bitset(self,bitset):
  1222. while self.LA(1) != EOF_CHAR and not self.set.member(self.LA(1)):
  1223. self.consume()
  1224. ### If symbol seen is EOF then generate and set token, otherwise
  1225. ### throw exception.
  1226. def default(self,la1):
  1227. if not la1 :
  1228. self.uponEOF()
  1229. self._returnToken = self.makeToken(EOF_TYPE)
  1230. else:
  1231. self.raise_NoViableAlt(la1)
  1232. def filterdefault(self,la1,*args):
  1233. if not la1:
  1234. self.uponEOF()
  1235. self._returnToken = self.makeToken(EOF_TYPE)
  1236. return
  1237. if not args:
  1238. self.consume()
  1239. raise TryAgain()
  1240. else:
  1241. ### apply filter object
  1242. self.commit();
  1243. try:
  1244. func=args[0]
  1245. func(*args[1:])
  1246. except RecognitionException as e:
  1247. ## catastrophic failure
  1248. self.reportError(e);
  1249. self.consume();
  1250. raise TryAgain()
  1251. def raise_NoViableAlt(self,la1=None):
  1252. if not la1: la1 = self.LA(1)
  1253. fname = self.getFilename()
  1254. line = self.getLine()
  1255. col = self.getColumn()
  1256. raise NoViableAltForCharException(la1,fname,line,col)
  1257. def set_return_token(self,_create,_token,_ttype,_offset):
  1258. if _create and not _token and (not _ttype == SKIP):
  1259. string = self.text.getString(_offset)
  1260. _token = self.makeToken(_ttype)
  1261. _token.setText(string)
  1262. self._returnToken = _token
  1263. return _token
  1264. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1265. ### CharScannerIterator ###
  1266. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1267. class CharScannerIterator:
  1268. def __init__(self,inst):
  1269. if isinstance(inst,CharScanner):
  1270. self.inst = inst
  1271. return
  1272. raise TypeError("CharScannerIterator requires CharScanner object")
  1273. def next(self):
  1274. assert self.inst
  1275. item = self.inst.nextToken()
  1276. if not item or item.isEOF():
  1277. raise StopIteration()
  1278. return item
  1279. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1280. ### BitSet ###
  1281. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1282. ### I'm assuming here that a long is 64bits. It appears however, that
  1283. ### a long is of any size. That means we can use a single long as the
  1284. ### bitset (!), ie. Python would do almost all the work (TBD).
  1285. class BitSet(object):
  1286. BITS = 64
  1287. NIBBLE = 4
  1288. LOG_BITS = 6
  1289. MOD_MASK = BITS -1
  1290. def __init__(self,data=None):
  1291. if not data:
  1292. BitSet.__init__(self,[long(0)])
  1293. return
  1294. if isinstance(data,int):
  1295. BitSet.__init__(self,[long(data)])
  1296. return
  1297. if isinstance(data,long):
  1298. BitSet.__init__(self,[data])
  1299. return
  1300. if not isinstance(data,list):
  1301. raise TypeError("BitSet requires integer, long, or " +
  1302. "list argument")
  1303. for x in data:
  1304. if not isinstance(x, int_types):
  1305. raise TypeError(self,"List argument item is " +
  1306. "not a long: %s" % (x))
  1307. self.data = data
  1308. def __str__(self):
  1309. bits = len(self.data) * BitSet.BITS
  1310. s = ""
  1311. for i in xrange(0,bits):
  1312. if self.at(i):
  1313. s += "1"
  1314. else:
  1315. s += "o"
  1316. if not ((i+1) % 10):
  1317. s += '|%s|' % (i+1)
  1318. return s
  1319. def __repr__(self):
  1320. return str(self)
  1321. def member(self,item):
  1322. if not item:
  1323. return False
  1324. if isinstance(item,int):
  1325. return self.at(item)
  1326. if not is_string_type(item):
  1327. raise TypeError(self,"char or unichar expected: %s" % (item))
  1328. ## char is a (unicode) string with at most lenght 1, ie.
  1329. ## a char.
  1330. if len(item) != 1:
  1331. raise TypeError(self,"char expected: %s" % (item))
  1332. ### handle ASCII/UNICODE char
  1333. num = ord(item)
  1334. ### check whether position num is in bitset
  1335. return self.at(num)
  1336. def wordNumber(self,bit):
  1337. return bit >> BitSet.LOG_BITS
  1338. def bitMask(self,bit):
  1339. pos = bit & BitSet.MOD_MASK ## bit mod BITS
  1340. return (1 << pos)
  1341. def set(self,bit,on=True):
  1342. # grow bitset as required (use with care!)
  1343. i = self.wordNumber(bit)
  1344. mask = self.bitMask(bit)
  1345. if i>=len(self.data):
  1346. d = i - len(self.data) + 1
  1347. for x in xrange(0,d):
  1348. self.data.append(0)
  1349. assert len(self.data) == i+1
  1350. if on:
  1351. self.data[i] |= mask
  1352. else:
  1353. self.data[i] &= (~mask)
  1354. ### make add an alias for set
  1355. add = set
  1356. def off(self,bit,off=True):
  1357. self.set(bit,not off)
  1358. def at(self,bit):
  1359. i = self.wordNumber(bit)
  1360. v = self.data[i]
  1361. m = self.bitMask(bit)
  1362. return v & m
  1363. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1364. ### some further funcs ###
  1365. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1366. def illegalarg_ex(func):
  1367. raise ValueError(
  1368. "%s is only valid if parser is built for debugging" %
  1369. (func.func_name))
  1370. def runtime_ex(func):
  1371. raise RuntimeError(
  1372. "%s is only valid if parser is built for debugging" %
  1373. (func.func_name))
  1374. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1375. ### TokenBuffer ###
  1376. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1377. class TokenBuffer(object):
  1378. def __init__(self,stream):
  1379. self.input = stream
  1380. self.nMarkers = 0
  1381. self.markerOffset = 0
  1382. self.numToConsume = 0
  1383. self.queue = Queue()
  1384. def reset(self) :
  1385. self.nMarkers = 0
  1386. self.markerOffset = 0
  1387. self.numToConsume = 0
  1388. self.queue.reset()
  1389. def consume(self) :
  1390. self.numToConsume += 1
  1391. def fill(self, amount):
  1392. self.syncConsume()
  1393. while self.queue.length() < (amount + self.markerOffset):
  1394. self.queue.append(self.input.nextToken())
  1395. def getInput(self):
  1396. return self.input
  1397. def LA(self,k) :
  1398. self.fill(k)
  1399. return self.queue.elementAt(self.markerOffset + k - 1).type
  1400. def LT(self,k) :
  1401. self.fill(k)
  1402. return self.queue.elementAt(self.markerOffset + k - 1)
  1403. def mark(self) :
  1404. self.syncConsume()
  1405. self.nMarkers += 1
  1406. return self.markerOffset
  1407. def rewind(self,mark) :
  1408. self.syncConsume()
  1409. self.markerOffset = mark
  1410. self.nMarkers -= 1
  1411. def syncConsume(self) :
  1412. while self.numToConsume > 0:
  1413. if self.nMarkers > 0:
  1414. # guess mode -- leave leading characters and bump offset.
  1415. self.markerOffset += 1
  1416. else:
  1417. # normal mode -- remove first character
  1418. self.queue.removeFirst()
  1419. self.numToConsume -= 1
  1420. def __str__(self):
  1421. return "(%s,%s,%s,%s,%s)" % (
  1422. self.input,
  1423. self.nMarkers,
  1424. self.markerOffset,
  1425. self.numToConsume,
  1426. self.queue)
  1427. def __repr__(self):
  1428. return str(self)
  1429. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1430. ### ParserSharedInputState ###
  1431. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1432. class ParserSharedInputState(object):
  1433. def __init__(self):
  1434. self.input = None
  1435. self.reset()
  1436. def reset(self):
  1437. self.guessing = 0
  1438. self.filename = None
  1439. if self.input:
  1440. self.input.reset()
  1441. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1442. ### Parser ###
  1443. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1444. class Parser(object):
  1445. def __init__(self, *args, **kwargs):
  1446. self.tokenNames = None
  1447. self.returnAST = None
  1448. self.astFactory = None
  1449. self.tokenTypeToASTClassMap = {}
  1450. self.ignoreInvalidDebugCalls = False
  1451. self.traceDepth = 0
  1452. if not args:
  1453. self.inputState = ParserSharedInputState()
  1454. return
  1455. arg0 = args[0]
  1456. assert isinstance(arg0,ParserSharedInputState)
  1457. self.inputState = arg0
  1458. return
  1459. def getTokenTypeToASTClassMap(self):
  1460. return self.tokenTypeToASTClassMap
  1461. def addMessageListener(self, l):
  1462. if not self.ignoreInvalidDebugCalls:
  1463. illegalarg_ex(self.addMessageListener)
  1464. def addParserListener(self,l) :
  1465. if (not self.ignoreInvalidDebugCalls) :
  1466. illegalarg_ex(self.addParserListener)
  1467. def addParserMatchListener(self, l) :
  1468. if (not self.ignoreInvalidDebugCalls) :
  1469. illegalarg_ex(self.addParserMatchListener)
  1470. def addParserTokenListener(self, l) :
  1471. if (not self.ignoreInvalidDebugCalls):
  1472. illegalarg_ex(self.addParserTokenListener)
  1473. def addSemanticPredicateListener(self, l) :
  1474. if (not self.ignoreInvalidDebugCalls):
  1475. illegalarg_ex(self.addSemanticPredicateListener)
  1476. def addSyntacticPredicateListener(self, l) :
  1477. if (not self.ignoreInvalidDebugCalls):
  1478. illegalarg_ex(self.addSyntacticPredicateListener)
  1479. def addTraceListener(self, l) :
  1480. if (not self.ignoreInvalidDebugCalls):
  1481. illegalarg_ex(self.addTraceListener)
  1482. def consume(self):
  1483. raise NotImplementedError()
  1484. def _consumeUntil_type(self,tokenType):
  1485. while self.LA(1) != EOF_TYPE and self.LA(1) != tokenType:
  1486. self.consume()
  1487. def _consumeUntil_bitset(self, set):
  1488. while self.LA(1) != EOF_TYPE and not set.member(self.LA(1)):
  1489. self.consume()
  1490. def consumeUntil(self,arg):
  1491. if isinstance(arg,int):
  1492. self._consumeUntil_type(arg)
  1493. else:
  1494. self._consumeUntil_bitset(arg)
  1495. def defaultDebuggingSetup(self):
  1496. pass
  1497. def getAST(self) :
  1498. return self.returnAST
  1499. def getASTFactory(self) :
  1500. return self.astFactory
  1501. def getFilename(self) :
  1502. return self.inputState.filename
  1503. def getInputState(self) :
  1504. return self.inputState
  1505. def setInputState(self, state) :
  1506. self.inputState = state
  1507. def getTokenName(self,num) :
  1508. return self.tokenNames[num]
  1509. def getTokenNames(self) :
  1510. return self.tokenNames
  1511. def isDebugMode(self) :
  1512. return self.false
  1513. def LA(self, i):
  1514. raise NotImplementedError()
  1515. def LT(self, i):
  1516. raise NotImplementedError()
  1517. def mark(self):
  1518. return self.inputState.input.mark()
  1519. def _match_int(self,t):
  1520. if (self.LA(1) != t):
  1521. raise MismatchedTokenException(
  1522. self.tokenNames, self.LT(1), t, False, self.getFilename())
  1523. else:
  1524. self.consume()
  1525. def _match_set(self, b):
  1526. if (not b.member(self.LA(1))):
  1527. raise MismatchedTokenException(
  1528. self.tokenNames,self.LT(1), b, False, self.getFilename())
  1529. else:
  1530. self.consume()
  1531. def match(self,set) :
  1532. if isinstance(set,int):
  1533. self._match_int(set)
  1534. return
  1535. if isinstance(set,BitSet):
  1536. self._match_set(set)
  1537. return
  1538. raise TypeError("Parser.match requires integer ot BitSet argument")
  1539. def matchNot(self,t):
  1540. if self.LA(1) == t:
  1541. raise MismatchedTokenException(
  1542. self.tokenNames, self.LT(1), t, True, self.getFilename())
  1543. else:
  1544. self.consume()
  1545. def removeMessageListener(self, l) :
  1546. if (not self.ignoreInvalidDebugCalls):
  1547. runtime_ex(self.removeMessageListener)
  1548. def removeParserListener(self, l) :
  1549. if (not self.ignoreInvalidDebugCalls):
  1550. runtime_ex(self.removeParserListener)
  1551. def removeParserMatchListener(self, l) :
  1552. if (not self.ignoreInvalidDebugCalls):
  1553. runtime_ex(self.removeParserMatchListener)
  1554. def removeParserTokenListener(self, l) :
  1555. if (not self.ignoreInvalidDebugCalls):
  1556. runtime_ex(self.removeParserTokenListener)
  1557. def removeSemanticPredicateListener(self, l) :
  1558. if (not self.ignoreInvalidDebugCalls):
  1559. runtime_ex(self.removeSemanticPredicateListener)
  1560. def removeSyntacticPredicateListener(self, l) :
  1561. if (not self.ignoreInvalidDebugCalls):
  1562. runtime_ex(self.removeSyntacticPredicateListener)
  1563. def removeTraceListener(self, l) :
  1564. if (not self.ignoreInvalidDebugCalls):
  1565. runtime_ex(self.removeTraceListener)
  1566. def reportError(self,x) :
  1567. fmt = "syntax error:"
  1568. f = self.getFilename()
  1569. if f:
  1570. fmt = ("%s:" % f) + fmt
  1571. if isinstance(x,Token):
  1572. line = x.getColumn()
  1573. col = x.getLine()
  1574. text = x.getText()
  1575. fmt = fmt + 'unexpected symbol at line %s (column %s) : "%s"'
  1576. print(fmt % (line,col,text), file=sys.stderr)
  1577. else:
  1578. print(fmt,str(x), file=sys.stderr)
  1579. def reportWarning(self,s):
  1580. f = self.getFilename()
  1581. if f:
  1582. print("%s:warning: %s" % (f,str(s)))
  1583. else:
  1584. print("warning: %s" % (str(s)))
  1585. def rewind(self, pos) :
  1586. self.inputState.input.rewind(pos)
  1587. def setASTFactory(self, f) :
  1588. self.astFactory = f
  1589. def setASTNodeClass(self, cl) :
  1590. self.astFactory.setASTNodeType(cl)
  1591. def setASTNodeType(self, nodeType) :
  1592. self.setASTNodeClass(nodeType)
  1593. def setDebugMode(self, debugMode) :
  1594. if (not self.ignoreInvalidDebugCalls):
  1595. runtime_ex(self.setDebugMode)
  1596. def setFilename(self, f) :
  1597. self.inputState.filename = f
  1598. def setIgnoreInvalidDebugCalls(self, value) :
  1599. self.ignoreInvalidDebugCalls = value
  1600. def setTokenBuffer(self, t) :
  1601. self.inputState.input = t
  1602. def traceIndent(self):
  1603. print(" " * self.traceDepth)
  1604. def traceIn(self,rname):
  1605. self.traceDepth += 1
  1606. self.trace("> ", rname)
  1607. def traceOut(self,rname):
  1608. self.trace("< ", rname)
  1609. self.traceDepth -= 1
  1610. ### wh: moved from ASTFactory to Parser
  1611. def addASTChild(self,currentAST, child):
  1612. if not child:
  1613. return
  1614. if not currentAST.root:
  1615. currentAST.root = child
  1616. elif not currentAST.child:
  1617. currentAST.root.setFirstChild(child)
  1618. else:
  1619. currentAST.child.setNextSibling(child)
  1620. currentAST.child = child
  1621. currentAST.advanceChildToEnd()
  1622. ### wh: moved from ASTFactory to Parser
  1623. def makeASTRoot(self,currentAST,root) :
  1624. if root:
  1625. ### Add the current root as a child of new root
  1626. root.addChild(currentAST.root)
  1627. ### The new current child is the last sibling of the old root
  1628. currentAST.child = currentAST.root
  1629. currentAST.advanceChildToEnd()
  1630. ### Set the new root
  1631. currentAST.root = root
  1632. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1633. ### LLkParser ###
  1634. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1635. class LLkParser(Parser):
  1636. def __init__(self, *args, **kwargs):
  1637. try:
  1638. arg1 = args[0]
  1639. except:
  1640. arg1 = 1
  1641. if isinstance(arg1,int):
  1642. super(LLkParser,self).__init__()
  1643. self.k = arg1
  1644. return
  1645. if isinstance(arg1,ParserSharedInputState):
  1646. super(LLkParser,self).__init__(arg1)
  1647. self.set_k(1,*args)
  1648. return
  1649. if isinstance(arg1,TokenBuffer):
  1650. super(LLkParser,self).__init__()
  1651. self.setTokenBuffer(arg1)
  1652. self.set_k(1,*args)
  1653. return
  1654. if isinstance(arg1,TokenStream):
  1655. super(LLkParser,self).__init__()
  1656. tokenBuf = TokenBuffer(arg1)
  1657. self.setTokenBuffer(tokenBuf)
  1658. self.set_k(1,*args)
  1659. return
  1660. ### unknown argument
  1661. raise TypeError("LLkParser requires integer, " +
  1662. "ParserSharedInputStream or TokenStream argument")
  1663. def consume(self):
  1664. self.inputState.input.consume()
  1665. def LA(self,i):
  1666. return self.inputState.input.LA(i)
  1667. def LT(self,i):
  1668. return self.inputState.input.LT(i)
  1669. def set_k(self,index,*args):
  1670. try:
  1671. self.k = args[index]
  1672. except:
  1673. self.k = 1
  1674. def trace(self,ee,rname):
  1675. print(type(self))
  1676. self.traceIndent()
  1677. guess = ""
  1678. if self.inputState.guessing > 0:
  1679. guess = " [guessing]"
  1680. print((ee + rname + guess))
  1681. for i in xrange(1,self.k+1):
  1682. if i != 1:
  1683. print(", ")
  1684. if self.LT(i) :
  1685. v = self.LT(i).getText()
  1686. else:
  1687. v = "null"
  1688. print("LA(%s) == %s" % (i,v))
  1689. print("\n")
  1690. def traceIn(self,rname):
  1691. self.traceDepth += 1;
  1692. self.trace("> ", rname);
  1693. def traceOut(self,rname):
  1694. self.trace("< ", rname);
  1695. self.traceDepth -= 1;
  1696. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1697. ### TreeParserSharedInputState ###
  1698. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1699. class TreeParserSharedInputState(object):
  1700. def __init__(self):
  1701. self.guessing = 0
  1702. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1703. ### TreeParser ###
  1704. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1705. class TreeParser(object):
  1706. def __init__(self, *args, **kwargs):
  1707. self.inputState = TreeParserSharedInputState()
  1708. self._retTree = None
  1709. self.tokenNames = []
  1710. self.returnAST = None
  1711. self.astFactory = ASTFactory()
  1712. self.traceDepth = 0
  1713. def getAST(self):
  1714. return self.returnAST
  1715. def getASTFactory(self):
  1716. return self.astFactory
  1717. def getTokenName(self,num) :
  1718. return self.tokenNames[num]
  1719. def getTokenNames(self):
  1720. return self.tokenNames
  1721. def match(self,t,set) :
  1722. assert isinstance(set,int) or isinstance(set,BitSet)
  1723. if not t or t == ASTNULL:
  1724. raise MismatchedTokenException(self.getTokenNames(), t,set, False)
  1725. if isinstance(set,int) and t.getType() != set:
  1726. raise MismatchedTokenException(self.getTokenNames(), t,set, False)
  1727. if isinstance(set,BitSet) and not set.member(t.getType):
  1728. raise MismatchedTokenException(self.getTokenNames(), t,set, False)
  1729. def matchNot(self,t, ttype) :
  1730. if not t or (t == ASTNULL) or (t.getType() == ttype):
  1731. raise MismatchedTokenException(self.getTokenNames(), t, ttype, True)
  1732. def reportError(self,ex):
  1733. print("error:",ex, file=sys.stderr)
  1734. def reportWarning(self, s):
  1735. print("warning:",s)
  1736. def setASTFactory(self,f):
  1737. self.astFactory = f
  1738. def setASTNodeType(self,nodeType):
  1739. self.setASTNodeClass(nodeType)
  1740. def setASTNodeClass(self,nodeType):
  1741. self.astFactory.setASTNodeType(nodeType)
  1742. def traceIndent(self):
  1743. print(" " * self.traceDepth)
  1744. def traceIn(self,rname,t):
  1745. self.traceDepth += 1
  1746. self.traceIndent()
  1747. print(("> " + rname + "(" +
  1748. ifelse(t,str(t),"null") + ")" +
  1749. ifelse(self.inputState.guessing>0,"[guessing]","")))
  1750. def traceOut(self,rname,t):
  1751. self.traceIndent()
  1752. print(("< " + rname + "(" +
  1753. ifelse(t,str(t),"null") + ")" +
  1754. ifelse(self.inputState.guessing>0,"[guessing]","")))
  1755. self.traceDepth -= 1
  1756. ### wh: moved from ASTFactory to TreeParser
  1757. def addASTChild(self,currentAST, child):
  1758. if not child:
  1759. return
  1760. if not currentAST.root:
  1761. currentAST.root = child
  1762. elif not currentAST.child:
  1763. currentAST.root.setFirstChild(child)
  1764. else:
  1765. currentAST.child.setNextSibling(child)
  1766. currentAST.child = child
  1767. currentAST.advanceChildToEnd()
  1768. ### wh: moved from ASTFactory to TreeParser
  1769. def makeASTRoot(self,currentAST,root):
  1770. if root:
  1771. ### Add the current root as a child of new root
  1772. root.addChild(currentAST.root)
  1773. ### The new current child is the last sibling of the old root
  1774. currentAST.child = currentAST.root
  1775. currentAST.advanceChildToEnd()
  1776. ### Set the new root
  1777. currentAST.root = root
  1778. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1779. ### funcs to work on trees ###
  1780. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1781. def rightmost(ast):
  1782. if ast:
  1783. while(ast.right):
  1784. ast = ast.right
  1785. return ast
  1786. def cmptree(s,t,partial):
  1787. while(s and t):
  1788. ### as a quick optimization, check roots first.
  1789. if not s.equals(t):
  1790. return False
  1791. ### if roots match, do full list match test on children.
  1792. if not cmptree(s.getFirstChild(),t.getFirstChild(),partial):
  1793. return False
  1794. s = s.getNextSibling()
  1795. t = t.getNextSibling()
  1796. r = ifelse(partial,not t,not s and not t)
  1797. return r
  1798. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1799. ### AST ###
  1800. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1801. class AST(object):
  1802. def __init__(self):
  1803. pass
  1804. def addChild(self, c):
  1805. pass
  1806. def equals(self, t):
  1807. return False
  1808. def equalsList(self, t):
  1809. return False
  1810. def equalsListPartial(self, t):
  1811. return False
  1812. def equalsTree(self, t):
  1813. return False
  1814. def equalsTreePartial(self, t):
  1815. return False
  1816. def findAll(self, tree):
  1817. return None
  1818. def findAllPartial(self, subtree):
  1819. return None
  1820. def getFirstChild(self):
  1821. return self
  1822. def getNextSibling(self):
  1823. return self
  1824. def getText(self):
  1825. return ""
  1826. def getType(self):
  1827. return INVALID_TYPE
  1828. def getLine(self):
  1829. return 0
  1830. def getColumn(self):
  1831. return 0
  1832. def getNumberOfChildren(self):
  1833. return 0
  1834. def initialize(self, t):
  1835. pass
  1836. def setFirstChild(self, c):
  1837. pass
  1838. def setNextSibling(self, n):
  1839. pass
  1840. def setText(self, text):
  1841. pass
  1842. def setType(self, ttype):
  1843. pass
  1844. def toString(self):
  1845. self.getText()
  1846. __str__ = toString
  1847. def toStringList(self):
  1848. return self.getText()
  1849. def toStringTree(self):
  1850. return self.getText()
  1851. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1852. ### ASTNULLType ###
  1853. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1854. ### There is only one instance of this class **/
  1855. class ASTNULLType(AST):
  1856. def __init__(self):
  1857. AST.__init__(self)
  1858. pass
  1859. def getText(self):
  1860. return "<ASTNULL>"
  1861. def getType(self):
  1862. return NULL_TREE_LOOKAHEAD
  1863. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1864. ### BaseAST ###
  1865. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  1866. class BaseAST(AST):
  1867. verboseStringConversion = False
  1868. tokenNames = None
  1869. def __init__(self):
  1870. self.down = None ## kid
  1871. self.right = None ## sibling
  1872. def addChild(self,node):
  1873. if node:
  1874. t = rightmost(self.down)
  1875. if t:
  1876. t.right = node
  1877. else:
  1878. assert not self.down
  1879. self.down = node
  1880. def getNumberOfChildren(self):
  1881. t = self.down
  1882. n = 0
  1883. while t:
  1884. n += 1
  1885. t = t.right
  1886. return n
  1887. def doWorkForFindAll(self,v,target,partialMatch):
  1888. sibling = self
  1889. while sibling:
  1890. c1 = partialMatch and sibling.equalsTreePartial(target)
  1891. if c1:
  1892. v.append(sibling)
  1893. else:
  1894. c2 = not partialMatch and sibling.equalsTree(target)
  1895. if c2:
  1896. v.append(sibling)
  1897. ### regardless of match or not, check any children for matches
  1898. if sibling.getFirstChild():
  1899. sibling.getFirstChild().doWorkForFindAll(v,target,partialMatch)
  1900. sibling = sibling.getNextSibling()
  1901. ### Is node t equal to 'self' in terms of token type and text?
  1902. def equals(self,t):
  1903. if not t:
  1904. return False
  1905. return self.getText() == t.getText() and self.getType() == t.getType()
  1906. ### Is t an exact structural and equals() match of this tree. The
  1907. ### 'self' reference is considered the start of a sibling list.
  1908. ###
  1909. def equalsList(self, t):
  1910. return cmptree(self, t, partial=False)
  1911. ### Is 't' a subtree of this list?
  1912. ### The siblings of the root are NOT ignored.
  1913. ###
  1914. def equalsListPartial(self,t):
  1915. return cmptree(self,t,partial=True)
  1916. ### Is tree rooted at 'self' equal to 't'? The siblings
  1917. ### of 'self' are ignored.
  1918. ###
  1919. def equalsTree(self, t):
  1920. return self.equals(t) and \
  1921. cmptree(self.getFirstChild(), t.getFirstChild(), partial=False)
  1922. ### Is 't' a subtree of the tree rooted at 'self'? The siblings
  1923. ### of 'self' are ignored.
  1924. ###
  1925. def equalsTreePartial(self, t):
  1926. if not t:
  1927. return True
  1928. return self.equals(t) and cmptree(
  1929. self.getFirstChild(), t.getFirstChild(), partial=True)
  1930. ### Walk the tree looking for all exact subtree matches. Return
  1931. ### an ASTEnumerator that lets the caller walk the list
  1932. ### of subtree roots found herein.
  1933. def findAll(self,target):
  1934. roots = []
  1935. ### the empty tree cannot result in an enumeration
  1936. if not target:
  1937. return None
  1938. # find all matches recursively
  1939. self.doWorkForFindAll(roots, target, False)
  1940. return roots
  1941. ### Walk the tree looking for all subtrees. Return
  1942. ### an ASTEnumerator that lets the caller walk the list
  1943. ### of subtree roots found herein.
  1944. def findAllPartial(self,sub):
  1945. roots = []
  1946. ### the empty tree cannot result in an enumeration
  1947. if not sub:
  1948. return None
  1949. self.doWorkForFindAll(roots, sub, True) ### find all matches recursively
  1950. return roots
  1951. ### Get the first child of this node None if not children
  1952. def getFirstChild(self):
  1953. return self.down
  1954. ### Get the next sibling in line after this one
  1955. def getNextSibling(self):
  1956. return self.right
  1957. ### Get the token text for this node
  1958. def getText(self):
  1959. return ""
  1960. ### Get the token type for this node
  1961. def getType(self):
  1962. return 0
  1963. def getLine(self):
  1964. return 0
  1965. def getColumn(self):
  1966. return 0
  1967. ### Remove all children */
  1968. def removeChildren(self):
  1969. self.down = None
  1970. def setFirstChild(self,c):
  1971. self.down = c
  1972. def setNextSibling(self, n):
  1973. self.right = n
  1974. ### Set the token text for this node
  1975. def setText(self, text):
  1976. pass
  1977. ### Set the token type for this node
  1978. def setType(self, ttype):
  1979. pass
  1980. ### static
  1981. def setVerboseStringConversion(verbose,names):
  1982. verboseStringConversion = verbose
  1983. tokenNames = names
  1984. setVerboseStringConversion = staticmethod(setVerboseStringConversion)
  1985. ### Return an array of strings that maps token ID to it's text.
  1986. ## @since 2.7.3
  1987. def getTokenNames():
  1988. return tokenNames
  1989. def toString(self):
  1990. return self.getText()
  1991. ### return tree as lisp string - sibling included
  1992. def toStringList(self):
  1993. ts = self.toStringTree()
  1994. sib = self.getNextSibling()
  1995. if sib:
  1996. ts += sib.toStringList()
  1997. return ts
  1998. __str__ = toStringList
  1999. ### return tree as string - siblings ignored
  2000. def toStringTree(self):
  2001. ts = ""
  2002. kid = self.getFirstChild()
  2003. if kid:
  2004. ts += " ("
  2005. ts += " " + self.toString()
  2006. if kid:
  2007. ts += kid.toStringList()
  2008. ts += " )"
  2009. return ts
  2010. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2011. ### CommonAST ###
  2012. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2013. ### Common AST node implementation
  2014. class CommonAST(BaseAST):
  2015. def __init__(self,token=None):
  2016. super(CommonAST,self).__init__()
  2017. self.ttype = INVALID_TYPE
  2018. self.text = "<no text>"
  2019. self.line = 0
  2020. self.column= 0
  2021. self.initialize(token)
  2022. #assert self.text
  2023. ### Get the token text for this node
  2024. def getText(self):
  2025. return self.text
  2026. ### Get the token type for this node
  2027. def getType(self):
  2028. return self.ttype
  2029. ### Get the line for this node
  2030. def getLine(self):
  2031. return self.line
  2032. ### Get the column for this node
  2033. def getColumn(self):
  2034. return self.column
  2035. def initialize(self,*args):
  2036. if not args:
  2037. return
  2038. arg0 = args[0]
  2039. if isinstance(arg0,int):
  2040. arg1 = args[1]
  2041. self.setType(arg0)
  2042. self.setText(arg1)
  2043. return
  2044. if isinstance(arg0,AST) or isinstance(arg0,Token):
  2045. self.setText(arg0.getText())
  2046. self.setType(arg0.getType())
  2047. self.line = arg0.getLine()
  2048. self.column = arg0.getColumn()
  2049. return
  2050. ### Set the token text for this node
  2051. def setText(self,text_):
  2052. assert is_string_type(text_)
  2053. self.text = text_
  2054. ### Set the token type for this node
  2055. def setType(self,ttype_):
  2056. assert isinstance(ttype_,int)
  2057. self.ttype = ttype_
  2058. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2059. ### CommonASTWithHiddenTokens ###
  2060. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2061. class CommonASTWithHiddenTokens(CommonAST):
  2062. def __init__(self,*args):
  2063. CommonAST.__init__(self,*args)
  2064. self.hiddenBefore = None
  2065. self.hiddenAfter = None
  2066. def getHiddenAfter(self):
  2067. return self.hiddenAfter
  2068. def getHiddenBefore(self):
  2069. return self.hiddenBefore
  2070. def initialize(self,*args):
  2071. CommonAST.initialize(self,*args)
  2072. if args and isinstance(args[0],Token):
  2073. assert isinstance(args[0],CommonHiddenStreamToken)
  2074. self.hiddenBefore = args[0].getHiddenBefore()
  2075. self.hiddenAfter = args[0].getHiddenAfter()
  2076. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2077. ### ASTPair ###
  2078. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2079. class ASTPair(object):
  2080. def __init__(self):
  2081. self.root = None ### current root of tree
  2082. self.child = None ### current child to which siblings are added
  2083. ### Make sure that child is the last sibling */
  2084. def advanceChildToEnd(self):
  2085. if self.child:
  2086. while self.child.getNextSibling():
  2087. self.child = self.child.getNextSibling()
  2088. ### Copy an ASTPair. Don't call it clone() because we want type-safety */
  2089. def copy(self):
  2090. tmp = ASTPair()
  2091. tmp.root = self.root
  2092. tmp.child = self.child
  2093. return tmp
  2094. def toString(self):
  2095. r = ifelse(not root,"null",self.root.getText())
  2096. c = ifelse(not child,"null",self.child.getText())
  2097. return "[%s,%s]" % (r,c)
  2098. __str__ = toString
  2099. __repr__ = toString
  2100. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2101. ### ASTFactory ###
  2102. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2103. class ASTFactory(object):
  2104. def __init__(self,table=None):
  2105. self._class = None
  2106. self._classmap = ifelse(table,table,None)
  2107. def create(self,*args):
  2108. if not args:
  2109. return self.create(INVALID_TYPE)
  2110. arg0 = args[0]
  2111. arg1 = None
  2112. arg2 = None
  2113. try:
  2114. arg1 = args[1]
  2115. arg2 = args[2]
  2116. except:
  2117. pass
  2118. # ctor(int)
  2119. if isinstance(arg0,int) and not arg2:
  2120. ### get class for 'self' type
  2121. c = self.getASTNodeType(arg0)
  2122. t = self.create(c)
  2123. if t:
  2124. t.initialize(arg0, ifelse(arg1,arg1,""))
  2125. return t
  2126. # ctor(int,something)
  2127. if isinstance(arg0,int) and arg2:
  2128. t = self.create(arg2)
  2129. if t:
  2130. t.initialize(arg0,arg1)
  2131. return t
  2132. # ctor(AST)
  2133. if isinstance(arg0,AST):
  2134. t = self.create(arg0.getType())
  2135. if t:
  2136. t.initialize(arg0)
  2137. return t
  2138. # ctor(token)
  2139. if isinstance(arg0,Token) and not arg1:
  2140. ttype = arg0.getType()
  2141. assert isinstance(ttype,int)
  2142. t = self.create(ttype)
  2143. if t:
  2144. t.initialize(arg0)
  2145. return t
  2146. # ctor(token,class)
  2147. if isinstance(arg0,Token) and arg1:
  2148. assert isinstance(arg1,type)
  2149. assert issubclass(arg1,AST)
  2150. # this creates instance of 'arg1' using 'arg0' as
  2151. # argument. Wow, that's magic!
  2152. t = arg1(arg0)
  2153. assert t and isinstance(t,AST)
  2154. return t
  2155. # ctor(class)
  2156. if isinstance(arg0,type):
  2157. ### next statement creates instance of type (!)
  2158. t = arg0()
  2159. assert isinstance(t,AST)
  2160. return t
  2161. def setASTNodeClass(self,className=None):
  2162. if not className:
  2163. return
  2164. assert isinstance(className,type)
  2165. assert issubclass(className,AST)
  2166. self._class = className
  2167. ### kind of misnomer - use setASTNodeClass instead.
  2168. setASTNodeType = setASTNodeClass
  2169. def getASTNodeClass(self):
  2170. return self._class
  2171. def getTokenTypeToASTClassMap(self):
  2172. return self._classmap
  2173. def setTokenTypeToASTClassMap(self,amap):
  2174. self._classmap = amap
  2175. def error(self, e):
  2176. import sys
  2177. print(e, file=sys.stderr)
  2178. def setTokenTypeASTNodeType(self, tokenType, className):
  2179. """
  2180. Specify a mapping between a token type and a (AST) class.
  2181. """
  2182. if not self._classmap:
  2183. self._classmap = {}
  2184. if not className:
  2185. try:
  2186. del self._classmap[tokenType]
  2187. except:
  2188. pass
  2189. else:
  2190. ### here we should also perform actions to ensure that
  2191. ### a. class can be loaded
  2192. ### b. class is a subclass of AST
  2193. ###
  2194. assert isinstance(className,type)
  2195. assert issubclass(className,AST) ## a & b
  2196. ### enter the class
  2197. self._classmap[tokenType] = className
  2198. def getASTNodeType(self,tokenType):
  2199. """
  2200. For a given token type return the AST node type. First we
  2201. lookup a mapping table, second we try _class
  2202. and finally we resolve to "antlr.CommonAST".
  2203. """
  2204. # first
  2205. if self._classmap:
  2206. try:
  2207. c = self._classmap[tokenType]
  2208. if c:
  2209. return c
  2210. except:
  2211. pass
  2212. # second
  2213. if self._class:
  2214. return self._class
  2215. # default
  2216. return CommonAST
  2217. ### methods that have been moved to file scope - just listed
  2218. ### here to be somewhat consistent with original API
  2219. def dup(self,t):
  2220. return dup(t,self)
  2221. def dupList(self,t):
  2222. return dupList(t,self)
  2223. def dupTree(self,t):
  2224. return dupTree(t,self)
  2225. ### methods moved to other classes
  2226. ### 1. makeASTRoot -> Parser
  2227. ### 2. addASTChild -> Parser
  2228. ### non-standard: create alias for longish method name
  2229. maptype = setTokenTypeASTNodeType
  2230. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2231. ### ASTVisitor ###
  2232. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2233. class ASTVisitor(object):
  2234. def __init__(self,*args):
  2235. pass
  2236. def visit(self,ast):
  2237. pass
  2238. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2239. ### static methods and variables ###
  2240. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx###
  2241. ASTNULL = ASTNULLType()
  2242. ### wh: moved from ASTFactory as there's nothing ASTFactory-specific
  2243. ### in this method.
  2244. def make(*nodes):
  2245. if not nodes:
  2246. return None
  2247. for i in xrange(0,len(nodes)):
  2248. node = nodes[i]
  2249. if node:
  2250. assert isinstance(node,AST)
  2251. root = nodes[0]
  2252. tail = None
  2253. if root:
  2254. root.setFirstChild(None)
  2255. for i in xrange(1,len(nodes)):
  2256. if not nodes[i]:
  2257. continue
  2258. if not root:
  2259. root = tail = nodes[i]
  2260. elif not tail:
  2261. root.setFirstChild(nodes[i])
  2262. tail = root.getFirstChild()
  2263. else:
  2264. tail.setNextSibling(nodes[i])
  2265. tail = tail.getNextSibling()
  2266. ### Chase tail to last sibling
  2267. while tail.getNextSibling():
  2268. tail = tail.getNextSibling()
  2269. return root
  2270. def dup(t,factory):
  2271. if not t:
  2272. return None
  2273. if factory:
  2274. dup_t = factory.create(t.__class__)
  2275. else:
  2276. raise TypeError("dup function requires ASTFactory argument")
  2277. dup_t.initialize(t)
  2278. return dup_t
  2279. def dupList(t,factory):
  2280. result = dupTree(t,factory)
  2281. nt = result
  2282. while t:
  2283. ## for each sibling of the root
  2284. t = t.getNextSibling()
  2285. nt.setNextSibling(dupTree(t,factory))
  2286. nt = nt.getNextSibling()
  2287. return result
  2288. def dupTree(t,factory):
  2289. result = dup(t,factory)
  2290. if t:
  2291. result.setFirstChild(dupList(t.getFirstChild(),factory))
  2292. return result
  2293. ###xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  2294. ### $Id$
  2295. # Local Variables: ***
  2296. # mode: python ***
  2297. # py-indent-offset: 4 ***
  2298. # End: ***