Skip to content

Commit

Permalink
Run dotnet-format
Browse files Browse the repository at this point in the history
  • Loading branch information
desplesda committed Nov 26, 2024
1 parent 62d8394 commit a7c7386
Show file tree
Hide file tree
Showing 103 changed files with 1,919 additions and 1,443 deletions.
14 changes: 8 additions & 6 deletions YarnSpinner.Compiler/Builders/EnumTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ namespace Yarn.Compiler
/// </summary>
public class EnumTypeBuilder
{
public static EnumType FromEnum<TEnum>(string? description = null) where TEnum : System.Enum {
public static EnumType FromEnum<TEnum>(string? description = null) where TEnum : System.Enum
{
var enumType = new EnumType(
typeof(TEnum).Name,
description ?? $"Imported from enum {typeof(TEnum).FullName}",
typeof(TEnum).Name,
description ?? $"Imported from enum {typeof(TEnum).FullName}",
(TypeBase)Types.Number
);

var nameAndValues = Enum.GetNames(typeof(TEnum)).Zip(
Enum.GetValues(typeof(TEnum)) as int[],
(name,value) => new KeyValuePair<string,int>(name,value)
Enum.GetValues(typeof(TEnum)) as int[],
(name, value) => new KeyValuePair<string, int>(name, value)
);

foreach (var pair in nameAndValues) {
foreach (var pair in nameAndValues)
{
var value = new ConstantTypeProperty(Types.Number, pair.Value, $"Imported from {typeof(TEnum).FullName}.{pair.Key}");
enumType.AddMember(pair.Key, value);
}
Expand Down
11 changes: 7 additions & 4 deletions YarnSpinner.Compiler/CompilationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace Yarn.Compiler
using System.Collections.Generic;
using System.Linq;

public class FileCompilationResult {
public class FileCompilationResult
{
public List<Node> Nodes { get; internal set; } = new List<Node>();

public List<NodeDebugInfo> DebugInfos { get; internal set; } = new List<NodeDebugInfo>();
Expand Down Expand Up @@ -167,15 +168,17 @@ public IReadOnlyDictionary<int, string> GetLabelsForNode(string node)

public string GetStringForKey(string key)
{
if (this.StringTable == null) {
if (this.StringTable == null)
{
throw new InvalidOperationException("No string table available");
}
return this.StringTable[key].text;
}

internal string DumpProgram() {
internal string DumpProgram()
{
return this.Program?.DumpCode(null, this) ?? "<no program>";
}

}
}
101 changes: 63 additions & 38 deletions YarnSpinner.Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

namespace Yarn.Compiler
{
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using TypeChecker;

internal enum ContentIdentifierType {
internal enum ContentIdentifierType
{
/// <summary>
/// The content identifier is a <c>#line:</c> tag.
/// </summary>
Expand Down Expand Up @@ -110,18 +111,21 @@ public static CompilationResult Compile(CompilationJob compilationJob)

// Check to see if any lines that shadow another have a valid source
// line, the same text as their source line
foreach (var shadowLineContext in stringTableManager.LineContexts.Values.Where(v => v.ShadowLineID != null)) {
foreach (var shadowLineContext in stringTableManager.LineContexts.Values.Where(v => v.ShadowLineID != null))
{
var shadowLineID = shadowLineContext.ShadowLineID!;

if (shadowLineContext.LineID == null) {

if (shadowLineContext.LineID == null)
{
// All lines have a unique line ID, including shadow lines -
// it's an error if we have a shadow ID but no line ID
throw new InvalidOperationException($"Internal error: line with shadow id {shadowLineID} did not have a line ID of its own");
}

var sourceFile = stringTableManager.StringTable[shadowLineContext.LineID].fileName;

if (stringTableManager.LineContexts.TryGetValue(shadowLineID, out var sourceLineContext) == false) {
if (stringTableManager.LineContexts.TryGetValue(shadowLineID, out var sourceLineContext) == false)
{
// No source line found
diagnostics.Add(new Diagnostic(
sourceFile, shadowLineContext, $"\"{shadowLineID}\" is not a known line ID."
Expand All @@ -132,20 +136,23 @@ public static CompilationResult Compile(CompilationJob compilationJob)
var sourceText = stringTableManager.StringTable[shadowLineID].text;
var shadowText = stringTableManager.StringTable[shadowLineContext.LineID].text;

if (sourceText == null) {
if (sourceText == null)
{
throw new InvalidOperationException($"Internal error: line with shadow id {shadowLineID} was referencing line {shadowLineID}, but that line's text is null");
}

var sourceContext = stringTableManager.LineContexts[shadowLineID];

if (sourceContext.line_formatted_text().expression().Length > 0) {
if (sourceContext.line_formatted_text().expression().Length > 0)
{
// Lines must not have inline expressions
diagnostics.Add(new Diagnostic(
sourceFile, shadowLineContext, $"Shadow lines must not have expressions"
));
}

if (sourceText.Equals(shadowText, StringComparison.CurrentCulture) == false) {
if (sourceText.Equals(shadowText, StringComparison.CurrentCulture) == false)
{
// Lines must be identical
diagnostics.Add(new Diagnostic(
sourceFile, shadowLineContext, $"Shadow lines must have the same text as their source lines"
Expand All @@ -159,7 +166,7 @@ public static CompilationResult Compile(CompilationJob compilationJob)
shadowLineTableEntry.text = null;
stringTableManager.StringTable[shadowLineContext.LineID] = shadowLineTableEntry;


}

// Ensure that all nodes names in this compilation are unique. Node
Expand All @@ -170,7 +177,8 @@ public static CompilationResult Compile(CompilationJob compilationJob)
// For nodes that have a 'when' clause (that is, they're in a node
// group), make their node names unique and store which group
// they're in.
foreach (var file in parsedFiles) {
foreach (var file in parsedFiles)
{
var nodeGroupVisitor = new NodeGroupVisitor(file.Name);
nodeGroupVisitor.Visit(file.Tree);
}
Expand Down Expand Up @@ -218,7 +226,8 @@ public static CompilationResult Compile(CompilationJob compilationJob)
failingConstraints = new HashSet<TypeConstraint>(TypeCheckerListener.ApplySolution(typeSolution, failingConstraints));
}

if (failingConstraints.Count > 0) {
if (failingConstraints.Count > 0)
{
// We have a number of constraints that we were unable to
// resolve - either they failed to unify during solving, or they
// were left unresolved at the end of type-checking all files.
Expand All @@ -235,7 +244,8 @@ public static CompilationResult Compile(CompilationJob compilationJob)
var watchdog = System.Diagnostics.Stopwatch.StartNew();

bool anySucceeded;
do {
do
{
#if !DEBUG
if (watchdog.ElapsedMilliseconds > TypeSolverTimeLimit * 1000) {
// We've taken too long to solve. Create error
Expand All @@ -253,22 +263,24 @@ public static CompilationResult Compile(CompilationJob compilationJob)
// constraints to fix, or no constraints ended up resolving.
anySucceeded = false;

foreach (var constraint in failingConstraints) {
foreach (var constraint in failingConstraints)
{
anySucceeded |= Solver.TrySolve(new[] { constraint }, knownTypes, diagnostics, ref typeSolution);
}
failingConstraints = new HashSet<TypeConstraint>(TypeCheckerListener.ApplySolution(typeSolution, failingConstraints));
} while (anySucceeded);

// If we have any left, then we well and truly failed to resolve
// the constraint, and we should produce diagnostics for them.
foreach (var constraint in failingConstraints) {
foreach (var constraint in failingConstraints)
{
foreach (var failureMessage in constraint.GetFailureMessages(typeSolution))
{
diagnostics.Add(new Yarn.Compiler.Diagnostic(constraint.SourceFileName, constraint.SourceRange, failureMessage));
}
}
watchdog.Stop();
}
}

// determining the nodes we need to track visits on
// this needs to be done before we finish up with declarations
Expand All @@ -294,7 +306,7 @@ public static CompilationResult Compile(CompilationJob compilationJob)
// this way any future variable storage system will know about them
// if we didn't do this later stages wouldn't be able to interface with them
declarations.AddRange(trackingDeclarations);

// Apply the type solution to all declarations.
foreach (var decl in declarations)
{
Expand Down Expand Up @@ -327,8 +339,9 @@ public static CompilationResult Compile(CompilationJob compilationJob)
foreach (var parsedFile in parsedFiles)
{
Stack<IParseTree> stack = new Stack<IParseTree>();

if (!(parsedFile.Tree.Payload is IParseTree parseTree)) {

if (!(parsedFile.Tree.Payload is IParseTree parseTree))
{
throw new InvalidOperationException($"Internal error: expected {nameof(parsedFile.Tree.Payload)} to be {nameof(IParseTree)}");
}

Expand Down Expand Up @@ -389,7 +402,8 @@ public static CompilationResult Compile(CompilationJob compilationJob)

// Check to see if we're permitted to use preview features. If not,
// and preview features are used, then emit errors.
foreach (var file in parsedFiles) {
foreach (var file in parsedFiles)
{
var previewFeatureChecker = new PreviewFeatureVisitor(file, !compilationJob.AllowPreviewFeatures, diagnostics);
previewFeatureChecker.Visit(file.Tree);

Expand Down Expand Up @@ -617,7 +631,7 @@ private static void AddErrorsForSettingReadonlyVariables(List<FileParseResult> p
// to a smart variable. That's not allowed, because
// smart variables are read-only.
diagnostics.Add(new Diagnostic(
file.Name,
file.Name,
setStatement.variable(),
$"{variableName} cannot be modified (it's a smart variable and is always equal to " +
$"{smartVariables[variableName]?.InitialValueParserContext?.GetTextWithWhitespace() ?? "(unknown)"})"));
Expand Down Expand Up @@ -709,19 +723,23 @@ private static void AddErrorsForInvalidNodeNames(List<FileParseResult> parseResu
continue;
}

bool HasWhenHeader(YarnSpinnerParser.NodeContext nodeContext) {
bool HasWhenHeader(YarnSpinnerParser.NodeContext nodeContext)
{
return nodeContext.GetWhenHeaders().Any();
}

// If any of these nodes have 'when' clauses, then all nodes
// must have them for the group to be valid. In this situation,
// it's not an error for the nodes to share the same name,
// because after this check is done, they will be renamed.
if (group.All(n => HasWhenHeader(n.Node))) {
if (group.All(n => HasWhenHeader(n.Node)))
{
// No error - all nodes that have this name have at least
// one 'when' header
continue;
} else if (group.Any(n => HasWhenHeader(n.Node))) {
}
else if (group.Any(n => HasWhenHeader(n.Node)))
{
// Error - some nodes have a 'when' header, but others
// don't. Create errors for these others.
foreach (var entry in group.Where(n => n.Node.GetWhenHeaders().Any() == false))
Expand Down Expand Up @@ -786,7 +804,7 @@ private static FileCompilationResult GenerateCode(FileParseResult fileParseResul
.ToDictionary(d => d.Name, d => d),
});


return compiler.Compile();
}

Expand Down Expand Up @@ -994,7 +1012,8 @@ internal static List<string> GetTokensFromString(string text)
/// <param name="instruction">The instruction to add.</param>
internal static void Emit(Node node, NodeDebugInfo debugInfo, int sourceLine, int sourceCharacter, Instruction instruction)
{
debugInfo.LinePositions.Add(node.Instructions.Count, new Position {
debugInfo.LinePositions.Add(node.Instructions.Count, new Position
{
Line = sourceLine,
Character = sourceCharacter,
});
Expand Down Expand Up @@ -1212,15 +1231,17 @@ public partial class NodeContext
/// key. To fetch all headers with this key, use <see
/// cref="GetHeaders"/>.
/// </remarks>
public HeaderContext? GetHeader(string key) {
public HeaderContext? GetHeader(string key)
{
return this.header()?
.FirstOrDefault(h =>
h.header_key?.Text.Equals(key, StringComparison.InvariantCultureIgnoreCase) ?? false
.FirstOrDefault(h =>
h.header_key?.Text.Equals(key, StringComparison.InvariantCultureIgnoreCase) ?? false
&& h.header_value != null
);
}

internal IEnumerable<When_headerContext> GetWhenHeaders() {
internal IEnumerable<When_headerContext> GetWhenHeaders()
{
return this.when_header();
}

Expand All @@ -1230,18 +1251,21 @@ internal IEnumerable<When_headerContext> GetWhenHeaders() {
/// <param name="key">The key of the header to find.</param>
/// <returns>A collection of headers whose <see cref="HeaderContext.header_key"/>
/// is <paramref name="key"/>. </returns>
internal IEnumerable<HeaderContext> GetHeaders(string? key = null) {
if (this.header() == null) {
internal IEnumerable<HeaderContext> GetHeaders(string? key = null)
{
if (this.header() == null)
{
return Enumerable.Empty<HeaderContext>();
}

if (key == null) {
if (key == null)
{
return this.header();
}

return this.header()
.Where(h =>
h.header_key?.Text.Equals(key, StringComparison.InvariantCultureIgnoreCase) ?? false
.Where(h =>
h.header_key?.Text.Equals(key, StringComparison.InvariantCultureIgnoreCase) ?? false
&& h.header_value != null
);
}
Expand All @@ -1266,7 +1290,8 @@ private static int GetBooleanOperatorCountInExpression(ParserRuleContext context

foreach (var child in context.children)
{
if (child is ParserRuleContext childContext) {
if (child is ParserRuleContext childContext)
{
subtreeCount += GetBooleanOperatorCountInExpression(childContext);
}
}
Expand Down Expand Up @@ -1313,7 +1338,7 @@ public partial class When_headerContext
{
internal bool IsOnce => this.header_expression.once != null;
internal bool IsAlways => this.header_expression.always != null;

/// <summary>
/// Gets the complexity of this line's condition.
/// </summary>
Expand Down
Loading

0 comments on commit a7c7386

Please sign in to comment.