package dyvilx.tools.compiler.ast.header;

import dyvil.lang.Formattable;
import dyvil.source.position.SourcePosition;
import dyvilx.tools.compiler.ast.structure.Package;
import dyvilx.tools.compiler.util.Markers;
import dyvilx.tools.parsing.ASTNode;
import dyvilx.tools.parsing.marker.MarkerList;

public class PackageDeclaration implements ASTNode
{
	// =============== Fields ===============

	protected SourcePosition position;
	protected String         fullPackageName;

	// =============== Constructors ===============

	public PackageDeclaration(SourcePosition position)
	{
		this.position = position;
	}

	public PackageDeclaration(SourcePosition position, String fullPackageName)
	{
		this.position = position;
		this.fullPackageName = fullPackageName;
	}

	// =============== Properties ===============

	public String getPackage()
	{
		return this.fullPackageName;
	}

	public void setPackage(String thePackage)
	{
		this.fullPackageName = thePackage;
	}

	@Override
	public SourcePosition getPosition()
	{
		return this.position;
	}

	@Override
	public void setPosition(SourcePosition position)
	{
		this.position = position;
	}

	// =============== Methods ===============

	// --------------- Phases ---------------

	public static void check(PackageDeclaration packageDeclaration, MarkerList markers, Package enclosingPackage)
	{
		if (packageDeclaration != null)
		{
			packageDeclaration.check(markers, enclosingPackage);
			return;
		}

		if (enclosingPackage != Package.rootPackage)
		{
			markers.add(Markers.semanticError(SourcePosition.ORIGIN, "package_declaration.missing",
			                                  enclosingPackage.getFullName()));
		}
	}

	public void check(MarkerList markers, Package enclosingPackage)
	{
		if (enclosingPackage == Package.rootPackage)
		{
			markers.add(Markers.semanticError(this.position, "package_declaration.default_package"));
			return;
		}

		final String fullName = enclosingPackage.getFullName();
		if (!this.fullPackageName.equals(fullName))
		{
			markers.add(
				Markers.semanticError(this.position, "package_declaration.mismatch", this.fullPackageName, fullName));
		}
	}

	// --------------- Formatting ---------------

	@Override
	public String toString()
	{
		return Formattable.toString(this);
	}

	@Override
	public void toString(String prefix, StringBuilder buffer)
	{
		buffer.append("package ").append(this.fullPackageName);
	}
}

@@ -1733,24 +1733,82 @@
 Package)%0A%09%7B%0A
+%09%09final String fullName = enclosingPackage.getFullName();%0A
 %09%09if (enclos
@@ -1935,16 +1935,26 @@
 package%22
+, fullName
 ));%0A%09%09%09r
@@ -1969,66 +1969,8 @@
 %09%7D%0A%0A
-%09%09final String fullName = enclosingPackage.getFullName();%0A
 %09%09if

Fix missing format string in package_declaration.default_package message