Exemple #1
0
		public ITerm Implode(ITerm parseTree)
		{
			parseTree.IsCons("parsetree", 2);
			ITerm result = ImplodeAppl(parseTree[0].ToCons("appl", 2));
			return result;
		}
		/// <summary>
		/// Reads the production type from a term.
		/// </summary>
		/// <param name="term">The term.</param>
		/// <returns>The production type.</returns>
		private ProductionType ReadProductionType(ITerm term)
		{
			#region Contract
			Contract.Requires<ArgumentNullException>(term != null);
			Contract.Ensures(Enum.IsDefined(typeof(ProductionType), Contract.Result<ProductionType>()));
			#endregion

			if (term.IsCons("no-attrs", 0))
				return ProductionType.None;

			ProductionType type = ProductionType.None;
			IListTerm list = (IListTerm)term.ToCons("attrs", 1)[0];
			foreach (var t in list.SubTerms.OfType<IConsTerm>())
			{
				if (t.SubTerms.Count != 0)
					continue;

				// TODO: Error when the type is set more than once.

				switch (t.Name)
				{
					case "reject": type = ProductionType.Reject; break;
					case "prefer": type = ProductionType.Prefer; break;
					case "avoid": type = ProductionType.Avoid; break;
					case "bracket": type = ProductionType.Bracket; break;
					case "assoc":
						{
							var a = t[0];
							if (a.IsCons("left", 0) || a.IsCons("assoc", 0))
								type = ProductionType.LeftAssociative;
							else if (a.IsCons("right", 0))
								type = ProductionType.RightAssociative;
							else if (a.IsCons("non-assoc", 0))
								type = ProductionType.None;
							else
								throw new InvalidOperationException("Unknown associativity: " + a);
						}
						break;
				}
			}

			return type;
		}
		/// <summary>
		/// Reads the production flags from a term.
		/// </summary>
		/// <param name="term">The term.</param>
		/// <returns>The production flags.</returns>
		private ProductionFlags ReadProductionFlags(ITerm term)
		{
			#region Contract
			Contract.Requires<ArgumentNullException>(term != null);
			#endregion

			if (term.IsCons("no-attrs", 0))
				return ProductionFlags.None;

			ProductionFlags flags = ProductionFlags.None;
			IListTerm list = (IListTerm)term.ToCons("attrs", 1)[0];
			foreach (var t in list.SubTerms.OfType<IConsTerm>())
			{
				if (t.SubTerms.Count != 0)
					continue;

				switch (t.Name)
				{
					case "recover": flags |= ProductionFlags.Recover; break;
					case "completion": flags |= ProductionFlags.Completion; break;
					case "ignore-indent":
					case "ignore-layout": flags |= ProductionFlags.IgnoreLayout; break;
					case "enforce-newline": flags |= ProductionFlags.NewlineEnforced; break;
					case "longest-match": flags |= ProductionFlags.LongestMatch; break;
				}
			}

			return flags;
		}
		/// <summary>
		/// Reads the constructor name from a term.
		/// </summary>
		/// <param name="term">The term.</param>
		/// <returns>The constructor name.</returns>
		private string ReadConstructor(ITerm term)
		{
			#region Contract
			Contract.Requires<ArgumentNullException>(term != null);
			#endregion

			if (term.IsCons("no-attrs", 0))
				return null;

			IListTerm list = (IListTerm)term.ToCons("attrs", 1)[0];
			var consTerms = list.SubTerms.Select(t => t.AsCons("cons", 1)).Where(c => c != null);
			IConsTerm consTerm = consTerms.SingleOrDefault();
			return consTerm?[0]?.ToString();
		}