Skip to content

Commit 25b5082

Browse files
committed
Hide sexpr annotations behind a flag
1 parent f9f3059 commit 25b5082

7 files changed

Lines changed: 101 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--format
2+
sexpr
3+
--term-annotations
4+
--
5+
data/perfect-prop-3a.sem

IntegrationTests/tests/test-sexpr-annotations.txt

Lines changed: 50 additions & 0 deletions
Large diffs are not rendered by default.

SemgusParser/HandlerFlags.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ private HandlerFlags(InvocationContext ctx, HandlerFlagsFactory fac)
4444
/// </summary>
4545
public bool LegacySymbols => _fac.LegacySymbols.GetFlag(_ctx);
4646

47+
/// <summary>
48+
/// Whether or not the S-expression format should include term annotations
49+
/// </summary>
50+
public bool TermAnnotations => _fac.TermAnnotations.GetFlag(_ctx);
51+
4752
/// <summary>
4853
/// Class for configuring and creating handler flags
4954
/// </summary>
@@ -59,6 +64,11 @@ public class HandlerFlagsFactory
5964
/// </summary>
6065
public readonly CommandFlag LegacySymbols = new("legacy-symbols", defaultValue: true, isHidden: true);
6166

67+
/// <summary>
68+
/// Whether or not to output term annotations for the S-expression output
69+
/// </summary>
70+
public readonly CommandFlag TermAnnotations = new("term-annotations", defaultValue: false, isHidden: true);
71+
6272
/// <summary>
6373
/// Creates a new HandlerFlagsFactory and configures it for the given command
6474
/// </summary>
@@ -67,6 +77,7 @@ public HandlerFlagsFactory(Command command)
6777
{
6878
command.AddFlag(FunctionEvents);
6979
command.AddFlag(LegacySymbols);
80+
command.AddFlag(TermAnnotations);
7081
}
7182

7283
/// <summary>

SemgusParser/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private static SemgusParser GetParser(string input, bool test, out string friend
228228
break;
229229

230230
case OutputFormat.Sexpr:
231-
handler = new SexprHandler(writer);
231+
handler = new SexprHandler(writer, hf);
232232
break;
233233

234234
default:

SemgusParser/Sexpr/SexprHandler.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,20 @@ internal class SexprHandler : ISemgusProblemHandler
1717
{
1818
private readonly ISexprWriter _sw;
1919

20-
public SexprHandler(TextWriter writer)
20+
/// <summary>
21+
/// Flags for the processing
22+
/// </summary>
23+
private readonly HandlerFlags _flags;
24+
25+
/// <summary>
26+
/// Creates a new S-expression handler for the given writer and options
27+
/// </summary>
28+
/// <param name="writer">Underlying s-expression writer</param>
29+
/// <param name="flags">Processing flags</param>
30+
public SexprHandler(TextWriter writer, HandlerFlags flags)
2131
{
2232
_sw = new SexprWriter(writer);
33+
_flags = flags;
2334
}
2435

2536
public void OnCheckSynth(SmtContext smtCtx, SemgusContext semgusCtx)
@@ -53,7 +64,7 @@ public void OnCheckSynth(SmtContext smtCtx, SemgusContext semgusCtx)
5364
_sw.WriteKeyword("symbols");
5465
_sw.Write(chc.Symbols);
5566
_sw.WriteKeyword("constraint");
56-
_sw.Write(chc.Constraint);
67+
_sw.Write(chc.Constraint, _flags.TermAnnotations);
5768
_sw.WriteKeyword("constructor");
5869
_sw.WriteConstructor(chc.Binder);
5970
});
@@ -69,7 +80,7 @@ public void OnCheckSynth(SmtContext smtCtx, SemgusContext semgusCtx)
6980
_sw.WriteList(() =>
7081
{
7182
_sw.WriteSymbol("constraint");
72-
_sw.Write(constraint);
83+
_sw.Write(constraint, _flags.TermAnnotations);
7384
});
7485
}
7586

@@ -145,7 +156,7 @@ public void OnFunctionDefinition(SmtContext ctx, SmtFunction function, SmtFuncti
145156
_sw.WriteKeyword("rank");
146157
_sw.Write(rank);
147158
_sw.WriteKeyword("definition");
148-
_sw.Write(lambda);
159+
_sw.Write(lambda, _flags.TermAnnotations);
149160
});
150161
}
151162

SemgusParser/Sexpr/SexprWriterExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ public static void WriteConstructor(this ISexprWriter writer, SmtMatchBinder bin
130130
/// </summary>
131131
/// <param name="writer">Writer to write to</param>
132132
/// <param name="term">Term to write</param>
133-
public static void Write(this ISexprWriter writer, SmtTerm term)
133+
public static void Write(this ISexprWriter writer, SmtTerm term, bool includeTermAnnotations)
134134
{
135135
writer.WriteList(() =>
136136
{
137137
writer.WriteSymbol("term");
138-
term.Accept(new SmtTermWriter(writer));
138+
term.Accept(new SmtTermWriter(writer, includeTermAnnotations));
139139
});
140140
}
141141

@@ -284,7 +284,7 @@ public static void Write(this ISexprWriter sw, SmtAttributeValue attrval)
284284
sw.WriteKeyword(attrval.KeywordValue!.Name);
285285
break;
286286
case SmtAttributeValue.AttributeType.Literal:
287-
sw.Write(attrval.LiteralValue!);
287+
sw.Write(attrval.LiteralValue!, includeTermAnnotations: false);
288288
break;
289289
case SmtAttributeValue.AttributeType.List:
290290
sw.WriteList(attrval.ListValue!, se => sw.Write(se));

SemgusParser/Sexpr/SmtTermWriter.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,25 @@ namespace Semgus.Parser.Sexpr
1111
{
1212
internal class SmtTermWriter : ISmtTermVisitor<ISexprWriter>
1313
{
14+
/// <summary>
15+
/// The writer to write with
16+
/// </summary>
1417
private readonly ISexprWriter _sw;
1518

16-
public SmtTermWriter(ISexprWriter sw)
19+
/// <summary>
20+
/// Whether or not to include term annotations
21+
/// </summary>
22+
private readonly bool _includeTermAnnotations;
23+
24+
/// <summary>
25+
/// Creates a new SmtTermWriter
26+
/// </summary>
27+
/// <param name="sw">The underlying S-expression writer</param>
28+
/// <param name="includeTermAnnotations">Whether or not annotations should be included on terms</param>
29+
public SmtTermWriter(ISexprWriter sw, bool includeTermAnnotations)
1730
{
1831
_sw = sw;
32+
_includeTermAnnotations = includeTermAnnotations;
1933
}
2034

2135
/// <summary>
@@ -25,7 +39,7 @@ public SmtTermWriter(ISexprWriter sw)
2539
/// <param name="inner">Action to call for writing the term</param>
2640
private void MaybeWriteAnnotations(SmtTerm term, Action inner)
2741
{
28-
if (term.Annotations != null && term.Annotations.Count > 0)
42+
if (_includeTermAnnotations && term.Annotations != null && term.Annotations.Count > 0)
2943
{
3044
_sw.WriteList(() =>
3145
{

0 commit comments

Comments
 (0)