PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Ref/Antlr/Runtime/antlr-dotnet-tool-3.3.1.7705_src/antlrcs/main/Antlr3.StringTemplate/StringTemplateGroupInterface.cs

http://webgrease.codeplex.com
C# | 317 lines | 224 code | 26 blank | 67 comment | 30 complexity | 545e652fc88b5926aa55ad89f77203c0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * [The "BSD licence"]
  3. * Copyright (c) 2003-2008 Terence Parr
  4. * All rights reserved.
  5. *
  6. * Conversion to C#:
  7. * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. namespace Antlr3.ST
  33. {
  34. using System;
  35. using System.Collections.Generic;
  36. using System.Linq;
  37. using Antlr.Runtime.JavaExtensions;
  38. using Antlr3.ST.Language;
  39. using DebuggerDisplay = System.Diagnostics.DebuggerDisplayAttribute;
  40. using IList = System.Collections.IList;
  41. using StringBuilder = System.Text.StringBuilder;
  42. using TextReader = System.IO.TextReader;
  43. /** <summary>A group interface is like a group without the template implementations.</summary>
  44. *
  45. * <remarks>
  46. * There are just template names/argument-lists like this:
  47. *
  48. * interface foo;
  49. * class(name,fields);
  50. * method(name,args,body);
  51. * </remarks>
  52. */
  53. [DebuggerDisplay( "TODO: StringTemplateGroupInterface debugger display" )]
  54. public class StringTemplateGroupInterface
  55. {
  56. /** <summary>What is the group name</summary> */
  57. string _name;
  58. /** <summary>Maps template name to TemplateDefinition object</summary> */
  59. Dictionary<string, TemplateDefinition> _templates = new Dictionary<string, TemplateDefinition>();
  60. /** <summary>
  61. * Are we derived from another group? Templates not found in this group
  62. * will be searched for in the superGroup recursively.
  63. * </summary>
  64. */
  65. StringTemplateGroupInterface _superInterface = null;
  66. /** <summary>
  67. * Where to report errors. All string templates in this group
  68. * use this error handler by default.
  69. * </summary>
  70. */
  71. IStringTemplateErrorListener _listener = DEFAULT_ERROR_LISTENER;
  72. class DefaultErrorListener : IStringTemplateErrorListener
  73. {
  74. #region StringTemplateErrorListener Members
  75. public void Error( string s, Exception e )
  76. {
  77. Console.Error.WriteLine( s );
  78. if ( e != null )
  79. {
  80. e.PrintStackTrace( Console.Error );
  81. }
  82. }
  83. public void Warning( string s )
  84. {
  85. Console.Out.WriteLine( s );
  86. }
  87. #endregion
  88. }
  89. public static IStringTemplateErrorListener DEFAULT_ERROR_LISTENER = new DefaultErrorListener();
  90. /** <summary>All the info we need to track for a template defined in an interface</summary> */
  91. protected class TemplateDefinition
  92. {
  93. public string name;
  94. public IDictionary<string, FormalArgument> formalArgs; // LinkedHashMap<FormalArgument>
  95. public bool optional = false;
  96. public TemplateDefinition( string name, IDictionary<string, FormalArgument> formalArgs, bool optional )
  97. {
  98. this.name = name;
  99. this.formalArgs = formalArgs;
  100. this.optional = optional;
  101. }
  102. }
  103. public StringTemplateGroupInterface( TextReader r )
  104. : this( r, DEFAULT_ERROR_LISTENER, (StringTemplateGroupInterface)null )
  105. {
  106. }
  107. public StringTemplateGroupInterface( TextReader r, IStringTemplateErrorListener errors )
  108. : this( r, errors, (StringTemplateGroupInterface)null )
  109. {
  110. }
  111. /** <summary>Create an interface from the input stream</summary> */
  112. public StringTemplateGroupInterface( TextReader r,
  113. IStringTemplateErrorListener errors,
  114. StringTemplateGroupInterface superInterface )
  115. {
  116. this._listener = errors;
  117. SuperInterface = superInterface;
  118. ParseInterface( r );
  119. }
  120. public string Name
  121. {
  122. get
  123. {
  124. return _name;
  125. }
  126. set
  127. {
  128. _name = value;
  129. }
  130. }
  131. public StringTemplateGroupInterface SuperInterface
  132. {
  133. get
  134. {
  135. return _superInterface;
  136. }
  137. set
  138. {
  139. _superInterface = value;
  140. }
  141. }
  142. protected virtual void ParseInterface( TextReader r )
  143. {
  144. try
  145. {
  146. InterfaceLexer lexer = new InterfaceLexer( new Antlr.Runtime.ANTLRReaderStream( r ) );
  147. InterfaceParser parser = new InterfaceParser( new Antlr.Runtime.CommonTokenStream( lexer ) );
  148. parser.groupInterface( this );
  149. //System.out.println("read interface\n"+this.toString());
  150. }
  151. catch ( Exception e )
  152. {
  153. string name = "<unknown>";
  154. if ( Name != null )
  155. {
  156. name = Name;
  157. }
  158. Error( "problem parsing group " + name + ": " + e, e );
  159. }
  160. }
  161. public virtual void DefineTemplate( string name, IDictionary<string, FormalArgument> formalArgs, bool optional )
  162. {
  163. TemplateDefinition d = new TemplateDefinition( name, formalArgs, optional );
  164. _templates[d.name] = d;
  165. }
  166. /** <summary>
  167. * Return a list of all template names missing from group that are defined
  168. * in this interface. Return null if all is well.
  169. * </summary>
  170. */
  171. public virtual IList<string> GetMissingTemplates( StringTemplateGroup group )
  172. {
  173. string[] missing =
  174. _templates.Values
  175. .Where( template => !template.optional && !group.IsDefined( template.name ) )
  176. .Select( template => template.name )
  177. .ToArray();
  178. return ( missing.Length == 0 ) ? null : missing;
  179. }
  180. /** <summary>
  181. * Return a list of all template sigs that are present in the group, but
  182. * that have wrong formal argument lists. Return null if all is well.
  183. * </summary>
  184. */
  185. public virtual IList<string> GetMismatchedTemplates( StringTemplateGroup group )
  186. {
  187. List<string> mismatched = new List<string>();
  188. foreach ( TemplateDefinition d in _templates.Values )
  189. {
  190. if ( group.IsDefined( d.name ) )
  191. {
  192. StringTemplate defST = group.GetTemplateDefinition( d.name );
  193. var formalArgs = defST.FormalArguments;
  194. bool ack = false;
  195. if ( ( d.formalArgs != null && formalArgs == null ) ||
  196. ( d.formalArgs == null && formalArgs != null ) ||
  197. d.formalArgs.Count != formalArgs.Count )
  198. {
  199. ack = true;
  200. }
  201. if ( !ack )
  202. {
  203. foreach ( var arg in formalArgs )
  204. {
  205. FormalArgument arg2;
  206. if ( !d.formalArgs.TryGetValue( arg.name, out arg2 ) || arg2 == null )
  207. {
  208. ack = true;
  209. break;
  210. }
  211. }
  212. }
  213. if ( ack )
  214. {
  215. //System.out.println(d.formalArgs+"!="+formalArgs);
  216. mismatched.Add( GetTemplateSignature( d ) );
  217. }
  218. }
  219. }
  220. if ( mismatched.Count == 0 )
  221. {
  222. mismatched = null;
  223. }
  224. return mismatched;
  225. }
  226. public virtual void Error( string msg )
  227. {
  228. Error( msg, null );
  229. }
  230. public virtual void Error( string msg, Exception e )
  231. {
  232. if ( _listener != null )
  233. {
  234. _listener.Error( msg, e );
  235. }
  236. else
  237. {
  238. Console.Error.WriteLine( "StringTemplate: " + msg );
  239. if ( e != null )
  240. {
  241. e.PrintStackTrace();
  242. }
  243. }
  244. }
  245. string _newline = Environment.NewLine;
  246. public override string ToString()
  247. {
  248. StringBuilder buf = new StringBuilder();
  249. buf.Append( "interface " );
  250. buf.Append( Name );
  251. buf.Append( ";" + _newline );
  252. foreach ( TemplateDefinition d in _templates.Values )
  253. {
  254. buf.Append( GetTemplateSignature( d ) );
  255. buf.Append( ";" + _newline );
  256. }
  257. return buf.ToString();
  258. }
  259. protected virtual string GetTemplateSignature( TemplateDefinition d )
  260. {
  261. StringBuilder buf = new StringBuilder();
  262. if ( d.optional )
  263. {
  264. buf.Append( "optional " );
  265. }
  266. buf.Append( d.name );
  267. if ( d.formalArgs != null )
  268. {
  269. StringBuilder args = new StringBuilder();
  270. args.Append( '(' );
  271. int i = 1;
  272. foreach ( string name in d.formalArgs.Keys )
  273. {
  274. if ( i > 1 )
  275. {
  276. args.Append( ", " );
  277. }
  278. args.Append( name );
  279. i++;
  280. }
  281. args.Append( ')' );
  282. buf.Append( args );
  283. }
  284. else
  285. {
  286. buf.Append( "()" );
  287. }
  288. return buf.ToString();
  289. }
  290. }
  291. }