private string GetParameterizedSortName(IConsTerm term) { string name = ((IConsTerm)term[0]).Name; IListTerm args = (IListTerm)term[1]; return String.Format("{0}_{1}", name, String.Join("", args.SubTerms.Select(t => ((IConsTerm)t).Name))); }
/// <inheritdoc /> protected override void WriteConsTerm(IConsTerm term, TextWriter writer) { // CONTRACT: Inherited from TermTextWriter // TODO: Escape constructor name. writer.Write(term.Name); writer.Write('('); this.WriteTermsList(term.SubTerms, ",", writer); writer.Write(')'); this.WriteAnnotations(term.Annotations, writer); }
public string TryGetSort(IConsTerm term) { if (term.IsCons("cf", 1)) return TryGetSort((IConsTerm)term[0]); if (term.IsCons("lex", 1)) return TryGetSort((IConsTerm)term[0]); if (term.IsCons("sort", 1)) return term[0].ToString(); if (term.IsCons("parameterized-sort", 2)) return GetParameterizedSortName(term); if (term.IsCons("char-class", 1)) return null; if (term.IsCons("alt", 2)) return GetAltSortName(term); return null; }
/// <inheritdoc /> protected override void WriteConsTerm(IConsTerm term, TextWriter writer) { #region Contract if (term == null) { throw new ArgumentNullException(nameof(term)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } #endregion // TODO: Escape constructor name. writer.Write(term.Name); writer.Write('('); this.WriteTermsList(term.SubTerms, ",", writer); writer.Write(')'); this.WriteAnnotations(term.Annotations, writer); }
/// <summary> /// Writes a application term to the specified writer. /// </summary> /// <param name="term">The term to write.</param> /// <param name="writer">The text writer to write to.</param> protected abstract void WriteConsTerm(IConsTerm term, TextWriter writer);
private string GetAltSortName(IConsTerm term) { string left = GetSort((IConsTerm)term[0]); string right = GetSort((IConsTerm)term[1]); return String.Format("{0}_{1}0", left, right); }
/// <summary> /// Gets the RTG sort name of a production, or the RTG element sort name for a list. /// </summary> /// <param name="term"></param> /// <returns></returns> public string GetSort(IConsTerm term) { string sort = TryGetSort(term); return sort ?? (term.SubTerms.Count > 0 ? GetSort((IConsTerm)term[0]) : null); }
/// <summary> /// Implode a `appl(_, _)`. /// </summary> /// <param name="term"></param> /// <returns></returns> private ITerm ImplodeAppl(IConsTerm term) { throw new NotImplementedException(); }
protected override void WriteConsTerm(IConsTerm term, TextWriter writer) { Contract.Requires(term != null); Contract.Requires(writer != null); }
/// <summary> /// Parses a reduce action item. /// </summary> /// <param name="term">The term.</param> /// <param name="labels">The labels in the parse table.</param> /// <returns>The parsed action item.</returns> /// <example> /// The term might look like this (without lookahead) /// <code> /// reduce(0,280,0) /// </code> /// or this (with lookahead) /// <code> /// reduce(0,280,0,[follow-restriction([char-class([42,47])])]) /// </code> /// </example> private ActionItem ParseReduce(IConsTerm term, IReadOnlyList<Label> labels) { #region Contract Contract.Requires<ArgumentNullException>(term != null); Contract.Requires<ArgumentException>(term.IsCons("reduce", 3) || term.IsCons("reduce", 4)); Contract.Requires<ArgumentNullException>(labels != null); Contract.Ensures(Contract.Result<ActionItem>() != null); #endregion int productionArity = term[0].ToInt32(); var label = new LabelRef(term[1].ToInt32() - SpoofaxParseTableFormat.LabelBase); ProductionType status = (ProductionType)term[2].ToInt32(); bool isRecover = labels[label.Index].Production.IsRecover; bool isCompletion = labels[label.Index].Production.IsCompletion; var followRestriction = term.SubTerms.Count == 4 ? ParseLookaheadCharRanges((IListTerm)term[3]) : ArrayExt.Empty<IReadOnlySet<CodePoint>>(); // Redundant information. Let's check it while we're at it. var production = labels[label.Index].Production; Contract.Assert(production.Arity == productionArity); Contract.Assert(production.IsCompletion == isCompletion); Contract.Assert(production.IsRecover == isRecover); Contract.Assert(production.Type == status); return new ReduceActionItem(label, followRestriction); }
/// <summary> /// Parses a shift action item. /// </summary> /// <param name="term">The term.</param> /// <returns>The parsed action item.</returns> /// <example> /// The term might look like this: /// <code> /// shift(22) /// </code> /// </example> private ActionItem ParseShift(IConsTerm term) { #region Contract Contract.Requires<ArgumentNullException>(term != null); Contract.Requires<ArgumentException>(term.IsCons("shift", 1)); Contract.Ensures(Contract.Result<ActionItem>() != null); #endregion var nextState = new StateRef(term[0].ToInt32()); return new ShiftActionItem(nextState); }
/// <summary> /// Parses an accept action item. /// </summary> /// <param name="term">The term.</param> /// <returns>The parsed action item.</returns> /// <example> /// The term might look like this: /// <code> /// accept() /// </code> /// </example> private ActionItem ParseAccept(IConsTerm term) { #region Contract Contract.Requires<ArgumentNullException>(term != null); Contract.Requires<ArgumentException>(term.IsCons("accept", 0)); Contract.Ensures(Contract.Result<ActionItem>() != null); #endregion return new AcceptActionItem(); }
public void VisitCons(IConsTerm term) { Contract.Requires<ArgumentNullException>(term != null); }