Skip to content

Commit 1c07cdf

Browse files
Fix pretty print empty spans.
1 parent e9b76ed commit 1c07cdf

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

Nitra.Visualizer/HtmlPrettyPrintWriter.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@ protected override void Garbage(IPrettyPrintSource source, NSpan skip)
3535
WriteSpan(_garbageClass, text);
3636
}
3737

38-
protected override void FormatToken(IPrettyPrintSource source, NSpan token)
38+
protected override void FormatToken(IPrettyPrintSource source, NSpan token, bool canBeEmpty, string ruleName)
3939
{
4040
TryPrintGarbage(source, token);
41-
var text = source.Text.Substring(token.StartPos, token.Length);
42-
WebUtility.HtmlEncode(text, _writer);
41+
if (token.IsEmpty)
42+
{
43+
if (!canBeEmpty && (Options & PrettyPrintOptions.MissingNodes) == PrettyPrintOptions.MissingNodes)
44+
WriteSpan(_missingNodeClass, ruleName);
45+
}
46+
else
47+
{
48+
var text = source.Text.Substring(token.StartPos, token.Length);
49+
WebUtility.HtmlEncode(text, _writer);
50+
}
4351
}
4452

4553
protected override void FormatString(IPrettyPrintSource source, NSpan token, string text)

Nitra/Nitra.Compiler/Generation/Ast/DefineMembers-PrettyPrint.n

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ namespace Nitra.Compiler
6565
}
6666
<[ { ..$statements } ]>
6767

68+
| Repeat as loop when loop.IsSkipAnyPattern => <[ writer.Token(source, $expr, true, "SkipAnyPattern"); ]>
69+
| Call(SymbolRef.Some(RegularRuleSymbol as ruleSymbol), _) => <[ writer.Token(source, $expr, $(ruleSymbol.CanParseEmptyString : bool), $(ruleSymbol.Name : string)); ]>
6870
| Call(SymbolRef.Some(MarkerSymbol as markerSymbol), _bp) =>
6971
match (markerSymbol.Node.FullNameParts)
7072
{
@@ -83,8 +85,6 @@ namespace Nitra.Compiler
8385
| _ => <[ writer.Whitespace(); ]>
8486
}
8587

86-
| Repeat as loop when loop.IsSkipAnyPattern
87-
| Call when rule.Type is RuleType.Chars => <[ writer.Token(source, $expr); ]>
8888
| Call(ruleRef, bp) when isRecursiveCallFromPostfixRule(ruleRef) => <[ $expr.PrettyPrint(writer, $bp); ]>
8989
| Call => <[ $expr.PrettyPrint(writer, 0); ]>
9090
| Repeat(_, _, rule) when isVoid(rule) => <[ writer.Whitespace(); ]>

Nitra/Nitra.Runtime/PrettyPrint/PrettyPrintWriter.n

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Nitra
2424
[RecordIgnore]
2525
private mutable _previousTokenPos : int;
2626

27-
protected abstract FormatToken(source : IPrettyPrintSource, token : NSpan) : void;
27+
protected abstract FormatToken(source : IPrettyPrintSource, token : NSpan, canBeEmpty : bool, ruleName : string) : void;
2828
protected abstract FormatString(source : IPrettyPrintSource, token : NSpan, text : string) : void;// TODO: сделать protected
2929
protected abstract Garbage(source : IPrettyPrintSource, token : NSpan) : void;
3030
public abstract MissingNode(ruleDescriptor : RuleDescriptor) : void;
@@ -35,10 +35,10 @@ namespace Nitra
3535
public abstract Indent() : void;
3636
public abstract Unindent() : void;
3737

38-
public Token(source : IPrettyPrintSource, token : NSpan) : void
38+
public Token(source : IPrettyPrintSource, token : NSpan, canBeEmpty : bool, ruleName : string) : void
3939
{
4040
TryPrintGarbage(source, token);
41-
FormatToken(source, token);
41+
FormatToken(source, token, canBeEmpty, ruleName);
4242
}
4343

4444
public String(source : IPrettyPrintSource, token : NSpan, text : string) : void

Nitra/Nitra.Runtime/PrettyPrint/StringPrettyPrintWriter.n

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ namespace Nitra
3434
_ = _buffer.Append(">>");
3535
}
3636

37-
protected override FormatToken(source : IPrettyPrintSource, token : NSpan) : void
37+
protected override FormatToken(source : IPrettyPrintSource, token : NSpan, canBeEmpty : bool, ruleName : string) : void
3838
{
3939
TryPrintGarbage(source, token);
4040

41-
_ = _buffer.Append(source.Text, token.StartPos, token.Length);
41+
if (token.IsEmpty)
42+
when (!canBeEmpty && Options %&& PrettyPrintOptions.MissingNodes)
43+
_ = _buffer.Append('<').Append(ruleName).Append('>');
44+
else
45+
_ = _buffer.Append(source.Text, token.StartPos, token.Length);
4246
}
4347

4448
protected override FormatString(source : IPrettyPrintSource, token : NSpan, text : string) : void

0 commit comments

Comments
 (0)