PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/boo/lib/antlr-2.7.5/lib/csharp/src/antlr/BaseAST.cs

https://github.com/w4x/boolangstudio
C# | 616 lines | 463 code | 54 blank | 99 comment | 82 complexity | 926f13ee6de0ec263aca8cb3a83a5475 MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using StringBuilder = System.Text.StringBuilder;
  3. using ISerializable = System.Runtime.Serialization.ISerializable;
  4. using TextWriter = System.IO.TextWriter;
  5. using ArrayList = System.Collections.ArrayList;
  6. using IEnumerator = System.Collections.IEnumerator;
  7. using AST = antlr.collections.AST;
  8. namespace antlr
  9. {
  10. /*ANTLR Translator Generator
  11. * Project led by Terence Parr at http://www.jGuru.com
  12. * Software rights: http://www.antlr.org/license.html
  13. *
  14. * $Id:$
  15. */
  16. //
  17. // ANTLR C# Code Generator by Micheal Jordan
  18. // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
  19. // Anthony Oguntimehin
  20. //
  21. // With many thanks to Eric V. Smith from the ANTLR list.
  22. //
  23. /*
  24. * A Child-Sibling Tree.
  25. *
  26. * A tree with PLUS at the root and with two children 3 and 4 is
  27. * structured as:
  28. *
  29. * PLUS
  30. * |
  31. * 3 -- 4
  32. *
  33. * and can be specified easily in LISP notation as
  34. *
  35. * (PLUS 3 4)
  36. *
  37. * where every '(' starts a new subtree.
  38. *
  39. * These trees are particular useful for translators because of
  40. * the flexibility of the children lists. They are also very easy
  41. * to walk automatically, whereas trees with specific children
  42. * reference fields can't easily be walked automatically.
  43. *
  44. * This class contains the basic support for an AST.
  45. * Most people will create ASTs that are subclasses of
  46. * BaseAST or of CommonAST.
  47. */
  48. [Serializable()]
  49. public abstract class BaseAST : AST
  50. {
  51. protected internal BaseAST down;
  52. protected internal BaseAST right;
  53. private static bool verboseStringConversion = false;
  54. private static string[] tokenNames = null;
  55. /*Add a node to the end of the child list for this node */
  56. public virtual void addChild(AST node)
  57. {
  58. if (node == null)
  59. return ;
  60. BaseAST t = this.down;
  61. if (t != null)
  62. {
  63. while (t.right != null)
  64. {
  65. t = t.right;
  66. }
  67. t.right = (BaseAST) node;
  68. }
  69. else
  70. {
  71. this.down = (BaseAST) node;
  72. }
  73. }
  74. private void doWorkForFindAll(ArrayList v, AST target, bool partialMatch)
  75. {
  76. AST sibling;
  77. // Start walking sibling lists, looking for matches.
  78. //siblingWalk:
  79. for (sibling = this; sibling != null; sibling = sibling.getNextSibling())
  80. {
  81. if ((partialMatch && sibling.EqualsTreePartial(target)) || (!partialMatch && sibling.EqualsTree(target)))
  82. {
  83. v.Add(sibling);
  84. }
  85. // regardless of match or not, check any children for matches
  86. if (sibling.getFirstChild() != null)
  87. {
  88. ((BaseAST) sibling.getFirstChild()).doWorkForFindAll(v, target, partialMatch);
  89. }
  90. }
  91. }
  92. public override bool Equals(object obj)
  93. {
  94. if (obj == null)
  95. return false;
  96. if (this.GetType() != obj.GetType())
  97. return false;
  98. return Equals((AST)obj);
  99. }
  100. /*Is node t equal to this in terms of token type and text? */
  101. public virtual bool Equals(AST t)
  102. {
  103. if (t == null)
  104. return false;
  105. return (Object.Equals(this.getText(), t.getText())) &&
  106. (this.Type == t.Type);
  107. }
  108. /*Is t an exact structural and equals() match of this tree. The
  109. * 'this' reference is considered the start of a sibling list.
  110. */
  111. public virtual bool EqualsList(AST t)
  112. {
  113. AST sibling;
  114. // the empty tree is not a match of any non-null tree.
  115. if (t == null)
  116. {
  117. return false;
  118. }
  119. // Otherwise, start walking sibling lists. First mismatch, return false.
  120. for (sibling = this; sibling != null && t != null; sibling = sibling.getNextSibling(), t = t.getNextSibling())
  121. {
  122. // as a quick optimization, check roots first.
  123. if (!sibling.Equals(t))
  124. {
  125. return false;
  126. }
  127. // if roots match, do full list match test on children.
  128. if (sibling.getFirstChild() != null)
  129. {
  130. if (!sibling.getFirstChild().EqualsList(t.getFirstChild()))
  131. {
  132. return false;
  133. }
  134. }
  135. else if (t.getFirstChild() != null)
  136. {
  137. return false;
  138. }
  139. }
  140. if (sibling == null && t == null)
  141. {
  142. return true;
  143. }
  144. // one sibling list has more than the other
  145. return false;
  146. }
  147. /*Is 'sub' a subtree of this list?
  148. * The siblings of the root are NOT ignored.
  149. */
  150. public virtual bool EqualsListPartial(AST sub)
  151. {
  152. AST sibling;
  153. // the empty tree is always a subset of any tree.
  154. if (sub == null)
  155. {
  156. return true;
  157. }
  158. // Otherwise, start walking sibling lists. First mismatch, return false.
  159. for (sibling = this; sibling != null && sub != null; sibling = sibling.getNextSibling(), sub = sub.getNextSibling())
  160. {
  161. // as a quick optimization, check roots first.
  162. if (!sibling.Equals(sub))
  163. return false;
  164. // if roots match, do partial list match test on children.
  165. if (sibling.getFirstChild() != null)
  166. {
  167. if (!sibling.getFirstChild().EqualsListPartial(sub.getFirstChild()))
  168. return false;
  169. }
  170. }
  171. if (sibling == null && sub != null)
  172. {
  173. // nothing left to match in this tree, but subtree has more
  174. return false;
  175. }
  176. // either both are null or sibling has more, but subtree doesn't
  177. return true;
  178. }
  179. /*Is tree rooted at 'this' equal to 't'? The siblings
  180. * of 'this' are ignored.
  181. */
  182. public virtual bool EqualsTree(AST t)
  183. {
  184. // check roots first.
  185. if (!this.Equals(t))
  186. return false;
  187. // if roots match, do full list match test on children.
  188. if (this.getFirstChild() != null)
  189. {
  190. if (!this.getFirstChild().EqualsList(t.getFirstChild()))
  191. return false;
  192. }
  193. else if (t.getFirstChild() != null)
  194. {
  195. return false;
  196. }
  197. return true;
  198. }
  199. /*Is 't' a subtree of the tree rooted at 'this'? The siblings
  200. * of 'this' are ignored.
  201. */
  202. public virtual bool EqualsTreePartial(AST sub)
  203. {
  204. // the empty tree is always a subset of any tree.
  205. if (sub == null)
  206. {
  207. return true;
  208. }
  209. // check roots first.
  210. if (!this.Equals(sub))
  211. return false;
  212. // if roots match, do full list partial match test on children.
  213. if (this.getFirstChild() != null)
  214. {
  215. if (!this.getFirstChild().EqualsListPartial(sub.getFirstChild()))
  216. return false;
  217. }
  218. return true;
  219. }
  220. /*Walk the tree looking for all exact subtree matches. Return
  221. * an IEnumerator that lets the caller walk the list
  222. * of subtree roots found herein.
  223. */
  224. public virtual IEnumerator findAll(AST target)
  225. {
  226. ArrayList roots = new ArrayList(10);
  227. //AST sibling;
  228. // the empty tree cannot result in an enumeration
  229. if (target == null)
  230. {
  231. return null;
  232. }
  233. doWorkForFindAll(roots, target, false); // find all matches recursively
  234. return roots.GetEnumerator();
  235. }
  236. /*Walk the tree looking for all subtrees. Return
  237. * an IEnumerator that lets the caller walk the list
  238. * of subtree roots found herein.
  239. */
  240. public virtual IEnumerator findAllPartial(AST sub)
  241. {
  242. ArrayList roots = new ArrayList(10);
  243. //AST sibling;
  244. // the empty tree cannot result in an enumeration
  245. if (sub == null)
  246. {
  247. return null;
  248. }
  249. doWorkForFindAll(roots, sub, true); // find all matches recursively
  250. return roots.GetEnumerator();
  251. }
  252. /*Get the first child of this node; null if not children */
  253. public virtual AST getFirstChild()
  254. {
  255. return down;
  256. }
  257. /*Get the next sibling in line after this one */
  258. public virtual AST getNextSibling()
  259. {
  260. return right;
  261. }
  262. /*Get the token text for this node */
  263. public virtual string getText()
  264. {
  265. return "";
  266. }
  267. /*Get the token type for this node */
  268. public virtual int Type
  269. {
  270. get { return 0; }
  271. set { ; }
  272. }
  273. /// <summary>
  274. /// Get number of children of this node; if leaf, returns 0
  275. /// </summary>
  276. /// <returns>Number of children</returns>
  277. public int getNumberOfChildren()
  278. {
  279. BaseAST t = this.down;
  280. int n = 0;
  281. if (t != null)
  282. {
  283. n = 1;
  284. while (t.right != null)
  285. {
  286. t = t.right;
  287. n++;
  288. }
  289. }
  290. return n;
  291. }
  292. public abstract void initialize(int t, string txt);
  293. public abstract void initialize(AST t);
  294. public abstract void initialize(IToken t);
  295. /*Remove all children */
  296. public virtual void removeChildren()
  297. {
  298. down = null;
  299. }
  300. public virtual void setFirstChild(AST c)
  301. {
  302. down = (BaseAST) c;
  303. }
  304. public virtual void setNextSibling(AST n)
  305. {
  306. right = (BaseAST) n;
  307. }
  308. /*Set the token text for this node */
  309. public virtual void setText(string text)
  310. {
  311. ;
  312. }
  313. /*Set the token type for this node */
  314. public virtual void setType(int ttype)
  315. {
  316. this.Type = ttype;
  317. }
  318. public static void setVerboseStringConversion(bool verbose, string[] names)
  319. {
  320. verboseStringConversion = verbose;
  321. tokenNames = names;
  322. }
  323. override public string ToString()
  324. {
  325. StringBuilder b = new StringBuilder();
  326. // if verbose and type name not same as text (keyword probably)
  327. if (verboseStringConversion &&
  328. (0 != String.Compare(getText(), (tokenNames[Type]), true)) &&
  329. (0 != String.Compare(getText(), StringUtils.stripFrontBack(tokenNames[Type], @"""", @""""), true)))
  330. {
  331. b.Append('[');
  332. b.Append(getText());
  333. b.Append(",<");
  334. b.Append(tokenNames[Type]);
  335. b.Append(">]");
  336. return b.ToString();
  337. }
  338. return getText();
  339. }
  340. /*Print out a child-sibling tree in LISP notation */
  341. public virtual string ToStringList()
  342. {
  343. AST t = this;
  344. string ts = "";
  345. if (t.getFirstChild() != null)
  346. ts += " (";
  347. ts += " " + this.ToString();
  348. if (t.getFirstChild() != null)
  349. {
  350. ts += ((BaseAST) t.getFirstChild()).ToStringList();
  351. }
  352. if (t.getFirstChild() != null)
  353. ts += " )";
  354. if (t.getNextSibling() != null)
  355. {
  356. ts += ((BaseAST) t.getNextSibling()).ToStringList();
  357. }
  358. return ts;
  359. }
  360. public virtual string ToStringTree()
  361. {
  362. AST t = this;
  363. string ts = "";
  364. if (t.getFirstChild() != null)
  365. {
  366. ts += " (";
  367. }
  368. ts += " " + this.ToString();
  369. if (t.getFirstChild() != null)
  370. {
  371. ts += ((BaseAST) t.getFirstChild()).ToStringList();
  372. }
  373. if (t.getFirstChild() != null)
  374. {
  375. ts += " )";
  376. }
  377. return ts;
  378. }
  379. public virtual string ToTree()
  380. {
  381. return ToTree(string.Empty);
  382. }
  383. public virtual string ToTree(string prefix)
  384. {
  385. StringBuilder sb = new StringBuilder(prefix);
  386. // Replace vertical bar if there is no next sibling.
  387. if ( (getNextSibling() == null) )
  388. sb.Append("+--");
  389. else
  390. sb.Append("|--");
  391. sb.Append( ToString() );
  392. sb.Append( Environment.NewLine );
  393. if ( getFirstChild() != null )
  394. {
  395. // Replace vertical bar if there is no next sibling.
  396. if ( getNextSibling() == null )
  397. sb.Append( ((BaseAST) getFirstChild()).ToTree(prefix + " ") );
  398. else
  399. sb.Append( ((BaseAST) getFirstChild()).ToTree(prefix + "| ") );
  400. }
  401. if ( getNextSibling() != null )
  402. sb.Append( ((BaseAST) getNextSibling()).ToTree(prefix) );
  403. return sb.ToString();
  404. }
  405. public static string decode(string text)
  406. {
  407. char c, c1, c2, c3, c4, c5;
  408. StringBuilder n = new StringBuilder();
  409. for (int i = 0; i < text.Length; i++)
  410. {
  411. c = text[i];
  412. if (c == '&')
  413. {
  414. c1 = text[i + 1];
  415. c2 = text[i + 2];
  416. c3 = text[i + 3];
  417. c4 = text[i + 4];
  418. c5 = text[i + 5];
  419. if (c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';')
  420. {
  421. n.Append("&");
  422. i += 5;
  423. }
  424. else if (c1 == 'l' && c2 == 't' && c3 == ';')
  425. {
  426. n.Append("<");
  427. i += 4;
  428. }
  429. else if (c1 == 'g' && c2 == 't' && c3 == ';')
  430. {
  431. n.Append(">");
  432. i += 4;
  433. }
  434. else if (c1 == 'q' && c2 == 'u' && c3 == 'o' && c4 == 't' && c5 == ';')
  435. {
  436. n.Append("\"");
  437. i += 6;
  438. }
  439. else if (c1 == 'a' && c2 == 'p' && c3 == 'o' && c4 == 's' && c5 == ';')
  440. {
  441. n.Append("'");
  442. i += 6;
  443. }
  444. else
  445. n.Append("&");
  446. }
  447. else
  448. n.Append(c);
  449. }
  450. return n.ToString();
  451. }
  452. public static string encode(string text)
  453. {
  454. char c;
  455. StringBuilder n = new StringBuilder();
  456. for (int i = 0; i < text.Length; i++)
  457. {
  458. c = text[i];
  459. switch (c)
  460. {
  461. case '&':
  462. {
  463. n.Append("&amp;");
  464. break;
  465. }
  466. case '<':
  467. {
  468. n.Append("&lt;");
  469. break;
  470. }
  471. case '>':
  472. {
  473. n.Append("&gt;");
  474. break;
  475. }
  476. case '"':
  477. {
  478. n.Append("&quot;");
  479. break;
  480. }
  481. case '\'':
  482. {
  483. n.Append("&apos;");
  484. break;
  485. }
  486. default:
  487. {
  488. n.Append(c);
  489. break;
  490. }
  491. }
  492. }
  493. return n.ToString();
  494. }
  495. public virtual void xmlSerializeNode(TextWriter outWriter)
  496. {
  497. StringBuilder buf = new StringBuilder(100);
  498. buf.Append("<");
  499. buf.Append(GetType().FullName + " ");
  500. buf.Append("text=\"" + encode(getText()) + "\" type=\"" + Type + "\"/>");
  501. outWriter.Write(buf.ToString());
  502. }
  503. public virtual void xmlSerializeRootOpen(TextWriter outWriter)
  504. {
  505. StringBuilder buf = new StringBuilder(100);
  506. buf.Append("<");
  507. buf.Append(GetType().FullName + " ");
  508. buf.Append("text=\"" + encode(getText()) + "\" type=\"" + Type + "\">\n");
  509. outWriter.Write(buf.ToString());
  510. }
  511. public virtual void xmlSerializeRootClose(TextWriter outWriter)
  512. {
  513. outWriter.Write("</" + GetType().FullName + ">\n");
  514. }
  515. public virtual void xmlSerialize(TextWriter outWriter)
  516. {
  517. // print out this node and all siblings
  518. for (AST node = this; node != null; node = node.getNextSibling())
  519. {
  520. if (node.getFirstChild() == null)
  521. {
  522. // print guts (class name, attributes)
  523. ((BaseAST) node).xmlSerializeNode(outWriter);
  524. }
  525. else
  526. {
  527. ((BaseAST) node).xmlSerializeRootOpen(outWriter);
  528. // print children
  529. ((BaseAST) node.getFirstChild()).xmlSerialize(outWriter);
  530. // print end tag
  531. ((BaseAST) node).xmlSerializeRootClose(outWriter);
  532. }
  533. }
  534. }
  535. #region Implementation of ICloneable
  536. [Obsolete("Deprecated since version 2.7.2. Use ASTFactory.dup() instead.", false)]
  537. public virtual object Clone()
  538. {
  539. return MemberwiseClone();
  540. }
  541. #endregion
  542. public override Int32 GetHashCode()
  543. {
  544. return base.GetHashCode();
  545. }
  546. }
  547. }