Where does the name come from?

Aug 6, 2009 at 1:12 AM

I just used Irony for the 1st time yesterday! :-)

(instead of just reading about it)
And now I wonder, where does the name come from?

For the record I was trying to write an assembly qualified type name parser.
It turns out I won't be really using Irony for that (hence reducing my dependencies in my low level library). Nonetheless, even in my hurry, newbie and lack of sleep state I quickly manage to produce the simple grammar below. Neat! Irony is so easy, that's cool! :-)

(Note: I used the Irony revision 31155 from SVN, the latest one yesterday) 

assembly qualifed type name grammar:

 

	[Language("Type FullName", "1.0", "Generic type name")]
	class TypeNameGrammar : Grammar
	{
		public TypeNameGrammar()
			: base(true)
		{
			var typeName = new NonTerminal("TypeName");
			this.Root = typeName;

			var ident = new FreeTextToChars("identifier", ",[]` ");
			var aname = new FreeTextToChars("AssemblyName", "]");
			var number = new NumberLiteral("number", NumberFlags.IntOnly);

			var optionalGenericType = new NonTerminal("GenericPart");
			var optionalAssembly = new NonTerminal("Assembly");
			var typeParameters = new NonTerminal("TypeParameters");

			typeName.Rule = ident + optionalGenericType + optionalAssembly;
			optionalGenericType.Rule =
				Empty
				| Symbol("`") + number + "[" + typeParameters + "]";
			optionalAssembly.Rule =
				Empty
				| Symbol(",") + aname;
			typeParameters.Rule =
				typeParameters + "," + "[" + typeName + "]"
				| Symbol("[") + typeName + "]";

			this.RegisterPunctuation("[", "]", "`", ",");
		}

		class FreeTextToChars : Terminal
		{
			private char[] stopChars;

			public FreeTextToChars(string name, string chars)
				: base(name, TokenCategory.Literal)
			{
				stopChars = chars.ToCharArray();
			}

			public override Token TryMatch(CompilerContext context, ISourceStream source)
			{
				var next = source.Text.IndexOfAny(stopChars, source.PreviewPosition);
				if (next == -1)
				{
					string token = source.Text.Substring(source.PreviewPosition);
					source.PreviewPosition = source.Text.Length;
					return source.CreateToken(this, token);
				}
				else
				{
					string token = source.Text.Substring(source.PreviewPosition, next - source.PreviewPosition);
					source.PreviewPosition = next;
					return source.CreateToken(this, token);
				}
			}
		}
	}
 

 

Coordinator
Aug 6, 2009 at 4:23 AM

"Irony" - partly mockery, partly real irony, partly aknowledgement of inital inspiration that came from IronPython and other iron products.

Your code - congrats, glad you did it and happy with result!

Don't understand why you needed a custom terminal, why not IdentifierTerminal? as an excercise? way to go!

Roman

 

Aug 6, 2009 at 8:33 AM

I plan to parse things such as:
--
System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
-- 

And assemblyName should match:
--
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
-- 

Identifier won't do, and recombining the assembly name from smaller token is a bit difficult for the beginer that I am (and tired as I was in the morning)

I tried to use the FreeTextLiteral but for some reason it didn't work out. Making my own terminal seemed like the best idea at at the time (2:00 AM) ;-)