Skip to content

Commit

Permalink
implement comment syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
PocketMiner82 committed Mar 18, 2024
1 parent 5ba5504 commit 0c72e49
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pseudocodeIde/PseudocodeIDEForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ private void PseudocodeIDEForm_Load(object sender, EventArgs e)
codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_ESCAPE].ForeColor = Color.Orange;
codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_ESCAPE].Bold = true;

codeTextBox.Styles[SyntaxHighlightingLexer.STYLE_COMMENT].ForeColor = Color.Green;

codeTextBox.StyleNeeded += codeTextBox_StyleNeeded;
codeTextBox.Lexer = Lexer.Container;
}
Expand Down
36 changes: 34 additions & 2 deletions pseudocodeIde/interpreter/scanner/SyntaxHighlightingLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public static class SyntaxHighlightingLexer
public const int STYLE_NUMBER = 3;
public const int STYLE_STRING = 4;
public const int STYLE_ESCAPE = 5;
public const int STYLE_COMMENT = 6;

private const int STATE_UNKNOWN = 0;
private const int STATE_IDENTIFIER = 1;
private const int STATE_NUMBER = 2;
private const int STATE_STRING = 3;
private const int STATE_CHAR = 4;
private const int STATE_COMMENT = 5;

private static int startPos;
private static int endPos;
Expand All @@ -41,7 +43,7 @@ public static void style(Scintilla scintilla, int _startPos, int _endPos)
scintilla.StartStyling(startPos);
while (startPos < endPos)
{
char prevC = startPos == 0 ? (char)scintilla.GetCharAt(startPos) : (char)scintilla.GetCharAt(startPos - 1);
char prevC = startPos == 0 ? (char)0x00 : (char)scintilla.GetCharAt(startPos - 1);
char c = (char)scintilla.GetCharAt(startPos);

REPROCESS:
Expand All @@ -64,6 +66,13 @@ public static void style(Scintilla scintilla, int _startPos, int _endPos)
state = STATE_IDENTIFIER;
goto REPROCESS;
}
else if (prevC == '/' && c == '/')
{
startPos = Math.Max(startPos - 1, 0);
scintilla.StartStyling(startPos);
state = STATE_COMMENT;
goto REPROCESS;
}
else
{
// everything else
Expand Down Expand Up @@ -105,7 +114,7 @@ public static void style(Scintilla scintilla, int _startPos, int _endPos)
{
length++;
}

scintilla.SetStyling(length, STYLE_NUMBER);
length = 0;
state = STATE_UNKNOWN;
Expand Down Expand Up @@ -151,6 +160,29 @@ public static void style(Scintilla scintilla, int _startPos, int _endPos)
}
}
break;

case STATE_COMMENT:
if (c != '\n' && !isAtEnd())
{
length++;
}
else
{
if (c != '\n' && isAtEnd())
{
length++;
}

scintilla.SetStyling(length, STYLE_COMMENT);
length = 0;
state = STATE_UNKNOWN;

if (!isAtEnd())
{
goto REPROCESS;
}
}
break;
}

startPos++;
Expand Down

0 comments on commit 0c72e49

Please sign in to comment.