mirror of
https://github.com/fadden/6502bench.git
synced 2025-07-24 07:24:06 +00:00
Fancy comments, part 2
Started implementing the fancy formatter. It currently doesn't do anything fancy, just word-wrapping. Moved the static DebugShowRuler field into Formatter, so that the cached data is discarded when the setting changes. Also, updated SourceNotes with instructions for publishing a release.
This commit is contained in:
@@ -19,6 +19,17 @@ using System.Diagnostics;
|
||||
using System.Windows.Media;
|
||||
using System.Text;
|
||||
|
||||
// Spaces and hyphens are different. For example, if width is 10,
|
||||
// "long words<space>more words" becomes:
|
||||
// 0123456789
|
||||
// long words
|
||||
// more words
|
||||
// However, "long words-more words" becomes:
|
||||
// long
|
||||
// words-more
|
||||
// words
|
||||
// because the hyphen is retained but the space is discarded.
|
||||
|
||||
namespace SourceGen {
|
||||
/// <summary>
|
||||
/// <para>Representation of a multi-line comment, which is a string plus some format options.
|
||||
@@ -30,11 +41,6 @@ namespace SourceGen {
|
||||
/// calls.</para>
|
||||
/// </summary>
|
||||
public class MultiLineComment {
|
||||
/// <summary>
|
||||
/// If set, sticks a MaxWidth "ruler" at the top, and makes spaces visible.
|
||||
/// </summary>
|
||||
public static bool DebugShowRuler { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Unformatted text.
|
||||
/// </summary>
|
||||
@@ -68,6 +74,10 @@ namespace SourceGen {
|
||||
|
||||
private const int DEFAULT_WIDTH = 80;
|
||||
private const int MIN_WIDTH = 8;
|
||||
private const int MAX_WIDTH = 128;
|
||||
private const string SPACES = // MAX_WIDTH spaces
|
||||
" " +
|
||||
" ";
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -166,44 +176,48 @@ namespace SourceGen {
|
||||
const char spcRep = '\u2219'; // BULLET OPERATOR
|
||||
string workString = string.IsNullOrEmpty(textPrefix) ? Text : textPrefix + Text;
|
||||
List<string> lines = new List<string>();
|
||||
bool debugMode = formatter.DebugLongComments;
|
||||
|
||||
if (MaxWidth > MAX_WIDTH) {
|
||||
lines.Add("!Bad MaxWidth!");
|
||||
return lines;
|
||||
}
|
||||
|
||||
string linePrefix;
|
||||
if (!string.IsNullOrEmpty(textPrefix)) {
|
||||
// This is a Note, no comment delimiter needed.
|
||||
linePrefix = string.Empty;
|
||||
} else if (BoxMode) {
|
||||
if (formatter.FullLineCommentDelimiterBase.Length == 1 &&
|
||||
formatter.FullLineCommentDelimiterBase[0] == BASIC_BOX_CHAR) {
|
||||
// Box char is same as comment delimiter, don't double-up.
|
||||
linePrefix = string.Empty;
|
||||
} else {
|
||||
// Prefix with comment delimiter, but don't include optional space.
|
||||
linePrefix = formatter.FullLineCommentDelimiterBase;
|
||||
}
|
||||
} else {
|
||||
// No box, prefix every line with comment delimiter and optional space.
|
||||
linePrefix = formatter.FullLineCommentDelimiterPlus;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder(MaxWidth);
|
||||
if (DebugShowRuler) {
|
||||
if (debugMode) {
|
||||
for (int i = 0; i < MaxWidth; i++) {
|
||||
sb.Append((i % 10).ToString());
|
||||
}
|
||||
lines.Add(sb.ToString());
|
||||
sb.Clear();
|
||||
}
|
||||
string boxLine, spaces;
|
||||
string boxLine;
|
||||
if (BoxMode) {
|
||||
for (int i = 0; i < MaxWidth - linePrefix.Length; i++) {
|
||||
sb.Append(BASIC_BOX_CHAR);
|
||||
}
|
||||
boxLine = sb.ToString();
|
||||
sb.Clear();
|
||||
for (int i = 0; i < MaxWidth; i++) {
|
||||
sb.Append(' ');
|
||||
}
|
||||
spaces = sb.ToString();
|
||||
sb.Clear();
|
||||
|
||||
} else {
|
||||
boxLine = spaces = null;
|
||||
boxLine = null;
|
||||
}
|
||||
|
||||
if (BoxMode && workString.Length > 0) {
|
||||
@@ -213,24 +227,14 @@ namespace SourceGen {
|
||||
int lineWidth = BoxMode ?
|
||||
MaxWidth - linePrefix.Length - 4 :
|
||||
MaxWidth - linePrefix.Length;
|
||||
Debug.Assert(lineWidth > 0);
|
||||
int startIndex = 0;
|
||||
int breakIndex = -1;
|
||||
for (int i = 0; i < workString.Length; i++) {
|
||||
// Spaces and hyphens are different. For example, if width is 10,
|
||||
// "long words<space>more words" becomes:
|
||||
// 0123456789
|
||||
// long words
|
||||
// more words
|
||||
// However, "long words-more words" becomes:
|
||||
// long
|
||||
// words-more
|
||||
// words
|
||||
// because the hyphen is retained but the space is discarded.
|
||||
|
||||
if (workString[i] == '\r' || workString[i] == '\n') {
|
||||
// explicit line break, emit line
|
||||
string str = workString.Substring(startIndex, i - startIndex);
|
||||
if (DebugShowRuler) { str = str.Replace(' ', spcRep); }
|
||||
if (debugMode) { str = str.Replace(' ', spcRep); }
|
||||
if (BoxMode) {
|
||||
if (str == "" + BASIC_BOX_CHAR) {
|
||||
// asterisk on a line by itself means "output row of asterisks"
|
||||
@@ -238,16 +242,15 @@ namespace SourceGen {
|
||||
} else {
|
||||
int padLen = lineWidth - str.Length;
|
||||
str = linePrefix + BASIC_BOX_CHAR + " " + str +
|
||||
spaces.Substring(0, padLen + 1) + BASIC_BOX_CHAR;
|
||||
SPACES.Substring(0, padLen + 1) + BASIC_BOX_CHAR;
|
||||
}
|
||||
} else {
|
||||
str = linePrefix + str;
|
||||
}
|
||||
lines.Add(str);
|
||||
// Eat the LF in CRLF. We don't actually work right with just LF,
|
||||
// because this will consume LFLF, but it's okay to insist that the
|
||||
// string use CRLF for line breaks.
|
||||
if (i < workString.Length - 1 && workString[i + 1] == '\n') {
|
||||
// Eat the LF in CRLF.
|
||||
if (workString[i] == '\r' && i < workString.Length - 1 &&
|
||||
workString[i + 1] == '\n') {
|
||||
i++;
|
||||
}
|
||||
startIndex = i + 1;
|
||||
@@ -258,11 +261,11 @@ namespace SourceGen {
|
||||
}
|
||||
|
||||
if (i - startIndex >= lineWidth) {
|
||||
// this character was one too many, break line one back
|
||||
// this character was one too many, break line at last break point
|
||||
if (breakIndex <= 0) {
|
||||
// no break found, just chop it
|
||||
string str = workString.Substring(startIndex, i - startIndex);
|
||||
if (DebugShowRuler) { str = str.Replace(' ', spcRep); }
|
||||
if (debugMode) { str = str.Replace(' ', spcRep); }
|
||||
if (BoxMode) {
|
||||
str = linePrefix + BASIC_BOX_CHAR + " " + str + " " + BASIC_BOX_CHAR;
|
||||
} else {
|
||||
@@ -279,11 +282,11 @@ namespace SourceGen {
|
||||
}
|
||||
string str = workString.Substring(startIndex,
|
||||
breakIndex + adj - startIndex);
|
||||
if (DebugShowRuler) { str = str.Replace(' ', spcRep); }
|
||||
if (debugMode) { str = str.Replace(' ', spcRep); }
|
||||
if (BoxMode) {
|
||||
int padLen = lineWidth - str.Length;
|
||||
str = linePrefix + BASIC_BOX_CHAR + " " + str +
|
||||
spaces.Substring(0, padLen + 1) + BASIC_BOX_CHAR;
|
||||
SPACES.Substring(0, padLen + 1) + BASIC_BOX_CHAR;
|
||||
} else {
|
||||
str = linePrefix + str;
|
||||
}
|
||||
@@ -309,11 +312,11 @@ namespace SourceGen {
|
||||
if (startIndex < workString.Length) {
|
||||
// Output remainder.
|
||||
string str = workString.Substring(startIndex, workString.Length - startIndex);
|
||||
if (DebugShowRuler) { str = str.Replace(' ', spcRep); }
|
||||
if (debugMode) { str = str.Replace(' ', spcRep); }
|
||||
if (BoxMode) {
|
||||
int padLen = lineWidth - str.Length;
|
||||
str = linePrefix + BASIC_BOX_CHAR + " " + str +
|
||||
spaces.Substring(0, padLen + 1) + BASIC_BOX_CHAR;
|
||||
SPACES.Substring(0, padLen + 1) + BASIC_BOX_CHAR;
|
||||
} else {
|
||||
str = linePrefix + str;
|
||||
}
|
||||
@@ -327,25 +330,283 @@ namespace SourceGen {
|
||||
return lines;
|
||||
}
|
||||
|
||||
#region Fancy
|
||||
|
||||
private enum Tag {
|
||||
Unknown = 0, Width, HorizRule, Break,
|
||||
BoxStart, BoxEnd, UrlStart, UrlEnd, SymStart, SymEnd
|
||||
}
|
||||
|
||||
private class DataSource {
|
||||
private string mString;
|
||||
private int mPosn;
|
||||
public bool mInBox, mInUrl, mInSym;
|
||||
|
||||
public char this[int i] {
|
||||
get { return mString[i]; }
|
||||
}
|
||||
public int Posn { get { return mPosn; } set { mPosn = value; } }
|
||||
public int Length => mString.Length;
|
||||
public char CurChar => mString[mPosn]; // mostly for debugger
|
||||
|
||||
public DataSource(string str, int posn, DataSource outer) {
|
||||
mString = str;
|
||||
mPosn = posn;
|
||||
|
||||
if (outer != null) {
|
||||
// Inherit the values from the "outer" source.
|
||||
mInBox = outer.mInBox;
|
||||
mInUrl = outer.mInUrl;
|
||||
mInSym = outer.mInSym;
|
||||
}
|
||||
}
|
||||
}
|
||||
private Stack<DataSource> mSourceStack = new Stack<DataSource>();
|
||||
private StringBuilder mLineBuilder = new StringBuilder(MAX_WIDTH);
|
||||
|
||||
private const char DEFAULT_CHAR = '\0';
|
||||
|
||||
private int mLineWidth;
|
||||
private char mBoxCharOrDef, mHorizRuleCharOrDef;
|
||||
private char mBoxCharActual;
|
||||
private bool mEscapeNext, mEatNextIfNewline;
|
||||
private string mLinePrefix, mBoxPrefix;
|
||||
private bool mDebugMode;
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the width of the usable text area, given the current attributes.
|
||||
/// </summary>
|
||||
private int CalcTextWidth(DataSource source) {
|
||||
if (source.mInBox) {
|
||||
if (mBoxCharOrDef == DEFAULT_CHAR) {
|
||||
// Leave space for left/right box edges.
|
||||
return mLineWidth - mBoxPrefix.Length - 4;
|
||||
} else {
|
||||
// Also leave space for a leading comment delimiter, even if the chosen
|
||||
// box char happens to match the current delimiter. It might not match when
|
||||
// it's rendered for asm gen, and we don't want the output to change.
|
||||
return mLineWidth - mBoxPrefix.Length - 5;
|
||||
}
|
||||
} else {
|
||||
return mLineWidth - mLinePrefix.Length;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates one or more lines of formatted text, using the fancy formatter.
|
||||
/// </summary>
|
||||
/// <param name="formatter">Formatter, with comment delimiters.</param>
|
||||
/// <returns>List of formatted strings.</returns>
|
||||
private List<string> FormatFancyText(Asm65.Formatter formatter) {
|
||||
Debug.Assert(SPACES.Length == MAX_WIDTH);
|
||||
|
||||
mLineWidth = DEFAULT_WIDTH;
|
||||
mBoxCharOrDef = mHorizRuleCharOrDef = DEFAULT_CHAR;
|
||||
mEscapeNext = mEatNextIfNewline = false;
|
||||
mDebugMode = formatter.DebugLongComments;
|
||||
mSourceStack.Clear();
|
||||
|
||||
mLinePrefix = formatter.FullLineCommentDelimiterPlus; // does not change
|
||||
mBoxPrefix = formatter.FullLineCommentDelimiterBase; // changes if box char set
|
||||
mBoxCharActual = mBoxPrefix[0];
|
||||
|
||||
DataSource source = new DataSource(Text, 0, null);
|
||||
int textWidth = CalcTextWidth(source);
|
||||
|
||||
char[] outBuf = new char[MAX_WIDTH];
|
||||
int outIndex = 0;
|
||||
int outBreakIndex = -1;
|
||||
|
||||
List<string> lines = new List<string>();
|
||||
string mod = Text.Replace("\r\n", "CRLF");
|
||||
for (int i = 0; i < mod.Length; i += 10) {
|
||||
lines.Add(formatter.FullLineCommentDelimiterPlus +
|
||||
mod.Substring(i, Math.Min(10, mod.Length - i)));
|
||||
if (mDebugMode) {
|
||||
for (int i = 0; i < mLineWidth; i++) {
|
||||
outBuf[i] = (char)('0' + i % 10);
|
||||
}
|
||||
lines.Add(new string(outBuf, 0, mLineWidth));
|
||||
}
|
||||
|
||||
// Walk through the input source.
|
||||
while (true) {
|
||||
if (source.Posn == source.Length) {
|
||||
if (mSourceStack.Count != 0) {
|
||||
source = mSourceStack.Pop();
|
||||
textWidth = CalcTextWidth(source);
|
||||
continue; // resume earlier string
|
||||
}
|
||||
break; // all done
|
||||
}
|
||||
|
||||
if (source.CurChar == '\r' || source.CurChar == '\n') {
|
||||
// Explicit line break. If it's a CRLF, eat both.
|
||||
if (source.CurChar == '\r' && source.Posn + 1 < source.Length &&
|
||||
source[source.Posn + 1] == '\n') {
|
||||
source.Posn++;
|
||||
}
|
||||
|
||||
mEscapeNext = false; // can't escape newlines
|
||||
|
||||
if (mEatNextIfNewline) {
|
||||
mEatNextIfNewline = false;
|
||||
} else {
|
||||
// Output what we have.
|
||||
OutputLine(outBuf, outIndex, source.mInBox, lines);
|
||||
outIndex = 0;
|
||||
outBreakIndex = -1;
|
||||
}
|
||||
source.Posn++;
|
||||
continue;
|
||||
}
|
||||
mEatNextIfNewline = false;
|
||||
|
||||
char thisCh = source.CurChar;
|
||||
if (thisCh == '\\') {
|
||||
if (!mEscapeNext) {
|
||||
mEscapeNext = true;
|
||||
source.Posn++; // eat the backslash
|
||||
continue; // restart loop; backslash might have been last char
|
||||
}
|
||||
} else if (thisCh == '[' && !mEscapeNext) {
|
||||
// Start of format tag?
|
||||
if (TryParseTag(source, out int skipLen, out DataSource subSource)) {
|
||||
source.Posn += skipLen;
|
||||
if (subSource != null) {
|
||||
mSourceStack.Push(source);
|
||||
source = subSource;
|
||||
textWidth = CalcTextWidth(source);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
} else if (thisCh == ' ') {
|
||||
// Remember position of space for line break. If there are multiple
|
||||
// consecutive spaces, remember the position of the first one.
|
||||
if (outBreakIndex < 0 || outBuf[outBreakIndex] != ' ' ||
|
||||
(outIndex > 0 && outBuf[outIndex - 1] != ' ')) {
|
||||
outBreakIndex = outIndex;
|
||||
}
|
||||
}
|
||||
|
||||
// We need to add a character to the buffer. Will this put us over the limit?
|
||||
if (outIndex == textWidth) {
|
||||
int outputCount;
|
||||
if (outBreakIndex <= 0) {
|
||||
// No break found, or break char was at start of line. Just chop what
|
||||
// we have.
|
||||
outputCount = outIndex;
|
||||
} else {
|
||||
// Break was a hyphen or space.
|
||||
outputCount = outBreakIndex;
|
||||
|
||||
}
|
||||
int adj = 0;
|
||||
if (outBuf[outputCount] == '-') {
|
||||
// Break was a hyphen, include it.
|
||||
adj = 1;
|
||||
}
|
||||
|
||||
// Output everything up to the break point, but not the break char itself
|
||||
// unless it's a hyphen.
|
||||
OutputLine(outBuf, outputCount + adj, source.mInBox, lines);
|
||||
|
||||
// Consume any trailing spaces (which are about to become leading spaces).
|
||||
while (outputCount < outIndex && outBuf[outputCount] == ' ') {
|
||||
outputCount++;
|
||||
}
|
||||
// Copy any remaining chars to start of buffer.
|
||||
outputCount += adj;
|
||||
if (outputCount < outIndex) {
|
||||
for (int i = 0; i < outIndex - outputCount; i++) {
|
||||
outBuf[i] = outBuf[outputCount + i];
|
||||
}
|
||||
}
|
||||
outIndex -= outputCount;
|
||||
outBreakIndex = -1;
|
||||
|
||||
// If we're at the start of a line, eat all leading spaces. (This is what
|
||||
// the WPF TextEdit dialog does when word-wrapping.)
|
||||
if (outIndex == 0) {
|
||||
while (source.Posn < source.Length && source.CurChar == ' ') {
|
||||
source.Posn++;
|
||||
}
|
||||
if (source.Posn == source.Length) {
|
||||
// Whoops, ran out of input.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (source.CurChar == '-') {
|
||||
// Can break on hyphen if it fits in line.
|
||||
outBreakIndex = outIndex;
|
||||
}
|
||||
|
||||
outBuf[outIndex++] = source[source.Posn++];
|
||||
}
|
||||
|
||||
// If we didn't end with a CRLF, output the last bits.
|
||||
if (outIndex > 0) {
|
||||
OutputLine(outBuf, outIndex, source.mInBox, lines);
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return "MLC box=" + BoxMode + " width=" + MaxWidth + " text='" + Text + "'";
|
||||
/// <summary>
|
||||
/// Attempts to parse a tag at the current source position.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>This attempts to parse the full tag, including the closing tag if such is
|
||||
/// appropriate.</para>
|
||||
/// </remarks>
|
||||
/// <param name="source">Input data source.</param>
|
||||
/// <param name="skipLen">Number of characters to advance in data source.</param>
|
||||
/// <param name="subSource">Result: data source with tag contents. May be null.</param>
|
||||
/// <returns>True if the tag was successfully parsed.</returns>
|
||||
private bool TryParseTag(DataSource source, out int skipLen, out DataSource subSource) {
|
||||
skipLen = 0;
|
||||
subSource = null;
|
||||
// TODO
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the contents of the output buffer to the line list, prefixing it with comment
|
||||
/// delimiters and/or wrapping it in a box.
|
||||
/// </summary>
|
||||
/// <param name="outBuf">Output buffer.</param>
|
||||
/// <param name="length">Length of data in output buffer.</param>
|
||||
/// <param name="inBox">True if we're inside a box.</param>
|
||||
/// <param name="lines">Line list to add the line to.</param>
|
||||
private void OutputLine(char[] outBuf, int length, bool inBox, List<string> lines) {
|
||||
Debug.Assert(length >= 0);
|
||||
mLineBuilder.Clear();
|
||||
if (inBox) {
|
||||
mLineBuilder.Append(mBoxPrefix);
|
||||
mLineBuilder.Append(outBuf, 0, length);
|
||||
int trailingCount = mLineWidth - mBoxPrefix.Length - length - 1;
|
||||
if (trailingCount > 0) {
|
||||
mLineBuilder.Append(SPACES, 0, trailingCount);
|
||||
}
|
||||
mLineBuilder.Append(mBoxCharActual);
|
||||
} else {
|
||||
mLineBuilder.Append(mLinePrefix);
|
||||
mLineBuilder.Append(outBuf, 0, length);
|
||||
}
|
||||
string str = mLineBuilder.ToString();
|
||||
if (mDebugMode) {
|
||||
str = str.Replace(' ', '\u2219'); // replace spaces with BULLET OPERATOR
|
||||
}
|
||||
lines.Add(str);
|
||||
}
|
||||
|
||||
#endregion Fancy
|
||||
|
||||
public override string ToString() {
|
||||
if (IsFancy) {
|
||||
return "MLC fancy text='" + Text + "'";
|
||||
} else {
|
||||
return "MLC box=" + BoxMode + " width=" + MaxWidth + " text='" + Text + "'";
|
||||
}
|
||||
}
|
||||
|
||||
public static bool operator ==(MultiLineComment a, MultiLineComment b) {
|
||||
if (ReferenceEquals(a, b)) {
|
||||
|
Reference in New Issue
Block a user