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

/Dependencies/boo/src/Boo.Lang.Parser/antlr/antlr/Parser.cs

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