PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/w4x/boolangstudio
C# | 507 lines | 369 code | 58 blank | 80 comment | 32 complexity | ae0f6dd90fff1503f89ce3aad6a5b61b MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using EventHandlerList = System.ComponentModel.EventHandlerList;
  3. using BitSet = antlr.collections.impl.BitSet;
  4. using AST = antlr.collections.AST;
  5. using ASTArray = antlr.collections.impl.ASTArray;
  6. using antlr.debug;
  7. using MessageListener = antlr.debug.MessageListener;
  8. using ParserListener = antlr.debug.ParserListener;
  9. using ParserMatchListener = antlr.debug.ParserMatchListener;
  10. using ParserTokenListener = antlr.debug.ParserTokenListener;
  11. using SemanticPredicateListener = antlr.debug.SemanticPredicateListener;
  12. using SyntacticPredicateListener = antlr.debug.SyntacticPredicateListener;
  13. using TraceListener = antlr.debug.TraceListener;
  14. /*
  15. private Vector messageListeners;
  16. private Vector newLineListeners;
  17. private Vector matchListeners;
  18. private Vector tokenListeners;
  19. private Vector semPredListeners;
  20. private Vector synPredListeners;
  21. private Vector traceListeners;
  22. */
  23. namespace antlr
  24. {
  25. /*ANTLR Translator Generator
  26. * Project led by Terence Parr at http://www.jGuru.com
  27. * Software rights: http://www.antlr.org/license.html
  28. *
  29. * $Id:$
  30. */
  31. //
  32. // ANTLR C# Code Generator by Micheal Jordan
  33. // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
  34. // Anthony Oguntimehin
  35. //
  36. // With many thanks to Eric V. Smith from the ANTLR list.
  37. //
  38. public abstract class Parser : IParserDebugSubject
  39. {
  40. // Used to store event delegates
  41. private EventHandlerList events_ = new EventHandlerList();
  42. protected internal EventHandlerList Events
  43. {
  44. get { return events_; }
  45. }
  46. // The unique keys for each event that Parser [objects] can generate
  47. internal static readonly object EnterRuleEventKey = new object();
  48. internal static readonly object ExitRuleEventKey = new object();
  49. internal static readonly object DoneEventKey = new object();
  50. internal static readonly object ReportErrorEventKey = new object();
  51. internal static readonly object ReportWarningEventKey = new object();
  52. internal static readonly object NewLineEventKey = new object();
  53. internal static readonly object MatchEventKey = new object();
  54. internal static readonly object MatchNotEventKey = new object();
  55. internal static readonly object MisMatchEventKey = new object();
  56. internal static readonly object MisMatchNotEventKey = new object();
  57. internal static readonly object ConsumeEventKey = new object();
  58. internal static readonly object LAEventKey = new object();
  59. internal static readonly object SemPredEvaluatedEventKey = new object();
  60. internal static readonly object SynPredStartedEventKey = new object();
  61. internal static readonly object SynPredFailedEventKey = new object();
  62. internal static readonly object SynPredSucceededEventKey = new object();
  63. protected internal ParserSharedInputState inputState;
  64. /*Nesting level of registered handlers */
  65. // protected int exceptionLevel = 0;
  66. /*Table of token type to token names */
  67. protected internal string[] tokenNames;
  68. /*AST return value for a rule is squirreled away here */
  69. protected internal AST returnAST;
  70. /*AST support code; parser and treeparser delegate to this object */
  71. protected internal ASTFactory astFactory = new ASTFactory();
  72. private bool ignoreInvalidDebugCalls = false;
  73. /*Used to keep track of indentdepth for traceIn/Out */
  74. protected internal int traceDepth = 0;
  75. public Parser()
  76. {
  77. inputState = new ParserSharedInputState();
  78. }
  79. public Parser(ParserSharedInputState state)
  80. {
  81. inputState = state;
  82. }
  83. /// <summary>
  84. ///
  85. /// </summary>
  86. public event TraceEventHandler EnterRule
  87. {
  88. add { Events.AddHandler(EnterRuleEventKey, value); }
  89. remove { Events.RemoveHandler(EnterRuleEventKey, value); }
  90. }
  91. public event TraceEventHandler ExitRule
  92. {
  93. add { Events.AddHandler(ExitRuleEventKey, value); }
  94. remove { Events.RemoveHandler(ExitRuleEventKey, value); }
  95. }
  96. public event TraceEventHandler Done
  97. {
  98. add { Events.AddHandler(DoneEventKey, value); }
  99. remove { Events.RemoveHandler(DoneEventKey, value); }
  100. }
  101. public event MessageEventHandler ErrorReported
  102. {
  103. add { Events.AddHandler(ReportErrorEventKey, value); }
  104. remove { Events.RemoveHandler(ReportErrorEventKey, value); }
  105. }
  106. public event MessageEventHandler WarningReported
  107. {
  108. add { Events.AddHandler(ReportWarningEventKey, value); }
  109. remove { Events.RemoveHandler(ReportWarningEventKey, value); }
  110. }
  111. public event MatchEventHandler MatchedToken
  112. {
  113. add { Events.AddHandler(MatchEventKey, value); }
  114. remove { Events.RemoveHandler(MatchEventKey, value); }
  115. }
  116. public event MatchEventHandler MatchedNotToken
  117. {
  118. add { Events.AddHandler(MatchNotEventKey, value); }
  119. remove { Events.RemoveHandler(MatchNotEventKey, value); }
  120. }
  121. public event MatchEventHandler MisMatchedToken
  122. {
  123. add { Events.AddHandler(MisMatchEventKey, value); }
  124. remove { Events.RemoveHandler(MisMatchEventKey, value); }
  125. }
  126. public event MatchEventHandler MisMatchedNotToken
  127. {
  128. add { Events.AddHandler(MisMatchNotEventKey, value); }
  129. remove { Events.RemoveHandler(MisMatchNotEventKey, value); }
  130. }
  131. public event TokenEventHandler ConsumedToken
  132. {
  133. add { Events.AddHandler(ConsumeEventKey, value); }
  134. remove { Events.RemoveHandler(ConsumeEventKey, value); }
  135. }
  136. public event TokenEventHandler TokenLA
  137. {
  138. add { Events.AddHandler(LAEventKey, value); }
  139. remove { Events.RemoveHandler(LAEventKey, value); }
  140. }
  141. public event SemanticPredicateEventHandler SemPredEvaluated
  142. {
  143. add { Events.AddHandler(SemPredEvaluatedEventKey, value); }
  144. remove { Events.RemoveHandler(SemPredEvaluatedEventKey, value); }
  145. }
  146. public event SyntacticPredicateEventHandler SynPredStarted
  147. {
  148. add { Events.AddHandler(SynPredStartedEventKey, value); }
  149. remove { Events.RemoveHandler(SynPredStartedEventKey, value); }
  150. }
  151. public event SyntacticPredicateEventHandler SynPredFailed
  152. {
  153. add { Events.AddHandler(SynPredFailedEventKey, value); }
  154. remove { Events.RemoveHandler(SynPredFailedEventKey, value); }
  155. }
  156. public event SyntacticPredicateEventHandler SynPredSucceeded
  157. {
  158. add { Events.AddHandler(SynPredSucceededEventKey, value); }
  159. remove { Events.RemoveHandler(SynPredSucceededEventKey, value); }
  160. }
  161. public virtual void addMessageListener(MessageListener l)
  162. {
  163. if (!ignoreInvalidDebugCalls)
  164. throw new System.ArgumentException("addMessageListener() is only valid if parser built for debugging");
  165. }
  166. public virtual void addParserListener(ParserListener l)
  167. {
  168. if (!ignoreInvalidDebugCalls)
  169. throw new System.ArgumentException("addParserListener() is only valid if parser built for debugging");
  170. }
  171. public virtual void addParserMatchListener(ParserMatchListener l)
  172. {
  173. if (!ignoreInvalidDebugCalls)
  174. throw new System.ArgumentException("addParserMatchListener() is only valid if parser built for debugging");
  175. }
  176. public virtual void addParserTokenListener(ParserTokenListener l)
  177. {
  178. if (!ignoreInvalidDebugCalls)
  179. throw new System.ArgumentException("addParserTokenListener() is only valid if parser built for debugging");
  180. }
  181. public virtual void addSemanticPredicateListener(SemanticPredicateListener l)
  182. {
  183. if (!ignoreInvalidDebugCalls)
  184. throw new System.ArgumentException("addSemanticPredicateListener() is only valid if parser built for debugging");
  185. }
  186. public virtual void addSyntacticPredicateListener(SyntacticPredicateListener l)
  187. {
  188. if (!ignoreInvalidDebugCalls)
  189. throw new System.ArgumentException("addSyntacticPredicateListener() is only valid if parser built for debugging");
  190. }
  191. public virtual void addTraceListener(TraceListener l)
  192. {
  193. if (!ignoreInvalidDebugCalls)
  194. throw new System.ArgumentException("addTraceListener() is only valid if parser built for debugging");
  195. }
  196. /*Get another token object from the token stream */
  197. public abstract void consume();
  198. /*Consume tokens until one matches the given token */
  199. public virtual void consumeUntil(int tokenType)
  200. {
  201. while (LA(1) != Token.EOF_TYPE && LA(1) != tokenType)
  202. {
  203. consume();
  204. }
  205. }
  206. /*Consume tokens until one matches the given token set */
  207. public virtual void consumeUntil(BitSet bset)
  208. {
  209. while (LA(1) != Token.EOF_TYPE && !bset.member(LA(1)))
  210. {
  211. consume();
  212. }
  213. }
  214. protected internal virtual void defaultDebuggingSetup(TokenStream lexer, TokenBuffer tokBuf)
  215. {
  216. // by default, do nothing -- we're not debugging
  217. }
  218. /*Get the AST return value squirreled away in the parser */
  219. public virtual AST getAST()
  220. {
  221. return returnAST;
  222. }
  223. public virtual ASTFactory getASTFactory()
  224. {
  225. return astFactory;
  226. }
  227. public virtual string getFilename()
  228. {
  229. return inputState.filename;
  230. }
  231. public virtual ParserSharedInputState getInputState()
  232. {
  233. return inputState;
  234. }
  235. public virtual void setInputState(ParserSharedInputState state)
  236. {
  237. inputState = state;
  238. }
  239. public virtual void resetState()
  240. {
  241. traceDepth = 0;
  242. inputState.reset();
  243. }
  244. public virtual string getTokenName(int num)
  245. {
  246. return tokenNames[num];
  247. }
  248. public virtual string[] getTokenNames()
  249. {
  250. return tokenNames;
  251. }
  252. public virtual bool isDebugMode()
  253. {
  254. return false;
  255. }
  256. /*Return the token type of the ith token of lookahead where i=1
  257. * is the current token being examined by the parser (i.e., it
  258. * has not been matched yet).
  259. */
  260. public abstract int LA(int i);
  261. /*Return the ith token of lookahead */
  262. public abstract IToken LT(int i);
  263. // Forwarded to TokenBuffer
  264. public virtual int mark()
  265. {
  266. return inputState.input.mark();
  267. }
  268. /*Make sure current lookahead symbol matches token type <tt>t</tt>.
  269. * Throw an exception upon mismatch, which is catch by either the
  270. * error handler or by the syntactic predicate.
  271. */
  272. public virtual void match(int t)
  273. {
  274. if (LA(1) != t)
  275. throw new MismatchedTokenException(tokenNames, LT(1), t, false, getFilename());
  276. else
  277. consume();
  278. }
  279. /*Make sure current lookahead symbol matches the given set
  280. * Throw an exception upon mismatch, which is catch by either the
  281. * error handler or by the syntactic predicate.
  282. */
  283. public virtual void match(BitSet b)
  284. {
  285. if (!b.member(LA(1)))
  286. throw new MismatchedTokenException(tokenNames, LT(1), b, false, getFilename());
  287. else
  288. consume();
  289. }
  290. public virtual void matchNot(int t)
  291. {
  292. if (LA(1) == t)
  293. throw new MismatchedTokenException(tokenNames, LT(1), t, true, getFilename());
  294. else
  295. consume();
  296. }
  297. /// <summary>
  298. /// @deprecated as of 2.7.2. This method calls System.exit() and writes
  299. /// directly to stderr, which is usually not appropriate when
  300. /// a parser is embedded into a larger application. Since the method is
  301. /// <code>static</code>, it cannot be overridden to avoid these problems.
  302. /// ANTLR no longer uses this method internally or in generated code.
  303. /// </summary>
  304. ///
  305. [Obsolete("De-activated since version 2.7.2.6 as it cannot be overidden.", true)]
  306. public static void panic()
  307. {
  308. System.Console.Error.WriteLine("Parser: panic");
  309. System.Environment.Exit(1);
  310. }
  311. public virtual void removeMessageListener(MessageListener l)
  312. {
  313. if (!ignoreInvalidDebugCalls)
  314. throw new System.SystemException("removeMessageListener() is only valid if parser built for debugging");
  315. }
  316. public virtual void removeParserListener(ParserListener l)
  317. {
  318. if (!ignoreInvalidDebugCalls)
  319. throw new System.SystemException("removeParserListener() is only valid if parser built for debugging");
  320. }
  321. public virtual void removeParserMatchListener(ParserMatchListener l)
  322. {
  323. if (!ignoreInvalidDebugCalls)
  324. throw new System.SystemException("removeParserMatchListener() is only valid if parser built for debugging");
  325. }
  326. public virtual void removeParserTokenListener(ParserTokenListener l)
  327. {
  328. if (!ignoreInvalidDebugCalls)
  329. throw new System.SystemException("removeParserTokenListener() is only valid if parser built for debugging");
  330. }
  331. public virtual void removeSemanticPredicateListener(SemanticPredicateListener l)
  332. {
  333. if (!ignoreInvalidDebugCalls)
  334. throw new System.ArgumentException("removeSemanticPredicateListener() is only valid if parser built for debugging");
  335. }
  336. public virtual void removeSyntacticPredicateListener(SyntacticPredicateListener l)
  337. {
  338. if (!ignoreInvalidDebugCalls)
  339. throw new System.ArgumentException("removeSyntacticPredicateListener() is only valid if parser built for debugging");
  340. }
  341. public virtual void removeTraceListener(TraceListener l)
  342. {
  343. if (!ignoreInvalidDebugCalls)
  344. throw new System.SystemException("removeTraceListener() is only valid if parser built for debugging");
  345. }
  346. /*Parser error-reporting function can be overridden in subclass */
  347. public virtual void reportError(RecognitionException ex)
  348. {
  349. Console.Error.WriteLine(ex);
  350. }
  351. /*Parser error-reporting function can be overridden in subclass */
  352. public virtual void reportError(string s)
  353. {
  354. if (getFilename() == null)
  355. {
  356. Console.Error.WriteLine("error: " + s);
  357. }
  358. else
  359. {
  360. Console.Error.WriteLine(getFilename() + ": error: " + s);
  361. }
  362. }
  363. /*Parser warning-reporting function can be overridden in subclass */
  364. public virtual void reportWarning(string s)
  365. {
  366. if (getFilename() == null)
  367. {
  368. Console.Error.WriteLine("warning: " + s);
  369. }
  370. else
  371. {
  372. Console.Error.WriteLine(getFilename() + ": warning: " + s);
  373. }
  374. }
  375. public virtual void recover(RecognitionException ex, BitSet tokenSet)
  376. {
  377. consume();
  378. consumeUntil(tokenSet);
  379. }
  380. public virtual void rewind(int pos)
  381. {
  382. inputState.input.rewind(pos);
  383. }
  384. /// <summary>
  385. /// Specify an object with support code (shared by Parser and TreeParser.
  386. /// Normally, the programmer does not play with this, using
  387. /// <see cref="setASTNodeClass"/> instead.
  388. /// </summary>
  389. /// <param name="f"></param>
  390. public virtual void setASTFactory(ASTFactory f)
  391. {
  392. astFactory = f;
  393. }
  394. /// <summary>
  395. /// Specify the type of node to create during tree building.
  396. /// </summary>
  397. /// <param name="cl">Fully qualified AST Node type name.</param>
  398. public virtual void setASTNodeClass(string cl)
  399. {
  400. astFactory.setASTNodeType(cl);
  401. }
  402. /// <summary>
  403. /// Specify the type of node to create during tree building.
  404. /// use <see cref="setASTNodeClass"/> now to be consistent with
  405. /// Token Object Type accessor.
  406. /// </summary>
  407. /// <param name="nodeType">Fully qualified AST Node type name.</param>
  408. [Obsolete("Replaced by setASTNodeClass(string) since version 2.7.1", true)]
  409. public virtual void setASTNodeType(string nodeType)
  410. {
  411. setASTNodeClass(nodeType);
  412. }
  413. public virtual void setDebugMode(bool debugMode)
  414. {
  415. if (!ignoreInvalidDebugCalls)
  416. throw new System.SystemException("setDebugMode() only valid if parser built for debugging");
  417. }
  418. public virtual void setFilename(string f)
  419. {
  420. inputState.filename = f;
  421. }
  422. public virtual void setIgnoreInvalidDebugCalls(bool Value)
  423. {
  424. ignoreInvalidDebugCalls = Value;
  425. }
  426. /*Set or change the input token buffer */
  427. public virtual void setTokenBuffer(TokenBuffer t)
  428. {
  429. inputState.input = t;
  430. }
  431. public virtual void traceIndent()
  432. {
  433. for (int i = 0; i < traceDepth; i++)
  434. Console.Out.Write(" ");
  435. }
  436. public virtual void traceIn(string rname)
  437. {
  438. traceDepth += 1;
  439. traceIndent();
  440. Console.Out.WriteLine("> " + rname + "; LA(1)==" + LT(1).getText() + ((inputState.guessing > 0)?" [guessing]":""));
  441. }
  442. public virtual void traceOut(string rname)
  443. {
  444. traceIndent();
  445. Console.Out.WriteLine("< " + rname + "; LA(1)==" + LT(1).getText() + ((inputState.guessing > 0)?" [guessing]":""));
  446. traceDepth -= 1;
  447. }
  448. }
  449. }