
I can't figure out what is going on in my program, I'm having a hard time wrapping my mind around what steps its taking to get to the wrong result.
I have this function which should make my parser ignore C-style comments, ie /* */. When I type /* * */ or any number of * in the comment my program is detecting the * characters inside the comment itself, and when it detects a * it also detects the last /:
// Handle C-style comments
private void HandleComment() {
// Consume until closing characters, multi-line comments are supported
while (Peek() != '*' && PeekAhead() != '/' && !IsAtEnd()) {
if (Peek() == '\n') line++;
Advance();
}
Advance();
Advance();
}
I figured I would need to Advance() twice in order to consume both the characters that denote the end of the comment.
Peek(), PeekAhead(), Advance() and IsAtEnd() are functions from the book, Crafting Interpreters, here they are:
// Peek at the next char
private char Peek() {
if (IsAtEnd()) return '\0';
return source[current];
}
// Peek after next char
private char PeekAhead() {
if (current + 1 >= source.Length) return '\0';
return source[current+1];
}
private char Advance() {
return source[current++];
}
private bool IsAtEnd() {
return current >= source.Length;
}
The source variable is just a string that contains the text contents of a file or the input from Console.ReadLine().
I based the comment logic on the string logic here:
// Handle string lexemes
private void HandleString() {
// Consume until closing quote, multi-line strings are supported
while (Peek() != '"' && !IsAtEnd()) {
if (Peek() == '\n') line++;
Advance();
}
// Handle unterminated strings
if (IsAtEnd()) {
DotLox.Error(line, "Unterminated string.");
return;
}
// Consume
Advance();
// Tokenize string and store value without quotes
string value = source[(start+1)..(current-1)];
AddToken(TokenType.STRING, value);
}
I almost forgot to share this crucial bit of logic here:
private void ScanToken() {
char c = Advance();
// Match characters to tokens
switch(c) {
// Division and comments
case '/':
if (Match('/')) {
// Consume comment but don't turn it into a token
while (Peek() != '\n' && !IsAtEnd()) Advance();
} else if (Match('*')) {
// Handle C-style comments
HandleComment();
} else {
// Turn lone slash into a token
AddToken(TokenType.SLASH);
}
break;
}
}
The problem is fairly simple:
while (Peek() != '*' && PeekAhead() != '/' && !IsAtEnd()) {
You are breaking the loop if either the current character is a * or if the next character is a /. In other words, you don't require the full */ token.
Instead, you'll want something like
while ((Peek() != '*' || PeekAhead() != '/') && !IsAtEnd()) {
Improving C# Memory Safety
27d 5h ago by mander.xyz/u/nemeski in csharp@programming.dev from devblogs.microsoft.comA Cross-Platform C# UI Framework via Qt’s Bridging Technology
28d 11h ago by mander.xyz/u/nemeski in csharp@programming.dev from www.qt.ioNavigating and Learning Data Access in FSharp
1mon 12d ago by piefed.keyboardvagabond.com/u/ragingHungryPanda in csharp@programming.dev from blog.keyboardvagabond.comAI integrations: rely or verify? Checking Semantic Kernel
1mon 25d ago by programming.dev/u/CodiUnicorn in csharp@programming.dev from pvs-studio.comExplore union types in C# 15
2mon 16d ago by lemmy.world/u/LPThinker in csharp@programming.dev from devblogs.microsoft.comBuilder vs Fluent Interface Pattern in C#: Key Differences Explained
3mon 3d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.devleader.caBuilder Pattern Best Practices in C#: Code Organization and Maintainability
3mon 4d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.devleader.caC# Extension Members | The .NET Tools Blog
3mon 10d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from blog.jetbrains.comWhen to Use Builder Pattern in C#: Decision Guide with Examples
3mon 10d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.devleader.caHow to Implement Builder Pattern in C#: Step-by-Step Guide
3mon 10d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.devleader.caDeep C# - Multicast Delegates and Events
3mon 16d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.i-programmer.infoBrave new C#
3mon 17d ago by programming.dev/u/CodiUnicorn in csharp@programming.dev from pvs-studio.comAutomatic Dependency Injection in C#: The Complete Guide to Needlr
4mon 8d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.devleader.caHow to Add Stamps to PDFs Using C# for Faster Reviews
4mon 8d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.syncfusion.comPrimary Constructors: Love them or Hate them?
4mon 16d ago by lemmy.world/u/monica_b1998 in csharp@programming.dev from slicker.menewtype - distinct type aliases for C#
4mon 17d ago by programming.dev/u/jupiter in csharp@programming.dev from programming.devUsing Voice Live API for speech-to-speech with .NET and C#
4mon 21d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.youtube.comC++ has scope_exit for running code at scope exit. C# says "We have scope_exit at home." - The Old New Thing
4mon 21d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from devblogs.microsoft.comLearn C# for free on Dometrain
4mon 25d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from mailchi.mpAvoiding common pitfalls with async/await - Stephen Cleary - NDC Copenhagen 2025
4mon 25d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.youtube.comMaking foreach on an IEnumerable allocation-free using reflection and dynamic methods
4mon 26d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from andrewlock.netConvert HTML to PDF in .NET (C#): Real-World Scenarios, PDF Standards, and Best Practices
4mon 26d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.textcontrol.comChanging Immutable Collections
4mon 26d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from codeblog.jonskeet.ukSimple OCR and NER Feature Extraction in C# with ONNX (English)
4mon 27d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.mostlylucid.netHow to Add Headers, Footers, and Page Numbers to PDFs in C#
4mon 28d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.syncfusion.comDeep C# - The Console
4mon 28d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.i-programmer.infoCollection Expression Arguments in C# 15+
4mon 28d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from steven-giesel.comGenerate an Open Graph Profile Image with C#
4mon 28d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.josephguadagno.netA Complete Guide to Converting Markdown to PDF in .NET C#
4mon 1d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.textcontrol.comC# Is TIOBE Language of 2025
4mon 1d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.i-programmer.infoC# – F# Interop (2026 edition)
4mon 1d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.planetgeek.chC# wins Tiobe Programming Language of the Year honors for 2025
5mon 10d ago by literature.cafe/u/cm0002 in csharp@programming.dev from www.infoworld.comAsync/Await Beyond the Basics in C#: Practical Concurrency Patterns for Scalable APIs
5mon 12d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from developersvoice.comTop 10 errors found in C# projects in 2025
5mon 20d ago by programming.dev/u/CodiUnicorn in csharp@programming.dev from pvs-studio.comAnnouncing iceoryx2 v0.8: Fast and Robust Inter-Process Communication (IPC) Middleware in Rust with C, C++, Python and with this release, C# bindings
5mon 26d ago by programming.dev/u/elBoberido in csharp@programming.dev from ekxide.ioAman Ghodawala's Website - Async/Await for dummies in c#
5mon 27d ago by programming.dev/u/ghodawalaaman in csharp@programming.dev from sites.google.comc# code of recursive descent to parse palindrome string
5mon 16h ago by programming.dev/u/ghodawalaaman in csharp@programming.devAman Ghodawala's Website
5mon 20h ago by programming.dev/u/ghodawalaaman in csharp@programming.dev from sites.google.comHow to call c function from c#
6mon 2d ago by programming.dev/u/ghodawalaaman in csharp@programming.devNeed help creating a forth interpreter in c#
6mon 2d ago by programming.dev/u/ghodawalaaman in csharp@programming.devwhy the code is not working?
6mon 3d ago by programming.dev/u/ghodawalaaman in csharp@programming.devDifference between Method Overriding and Method Hiding in C#
6mon 7d ago by lemmy.ca/u/kionite231 in csharp@programming.dev from ghodawalaaman.blogspot.comThread safety guide
6mon 8d ago by lemmy.world/u/monica_b1998 in csharp@programming.dev from slicker.meA simple question of C#
6mon 8d ago by lemmy.ca/u/kionite231 in csharp@programming.dev from ghodawalaaman.blogspot.comCollections in c#
6mon 8d ago by lemmy.ca/u/kionite231 in csharp@programming.dev from ghodawalaaman.blogspot.comChecking osu! and exploring features of static analyzers
6mon 15d ago by programming.dev/u/CodiUnicorn in csharp@programming.dev from pvs-studio.comProperty-Based Testing in C#: FsCheck + xUnit for Robust, Law-Driven Test Suites
6mon 18d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from developersvoice.comEnterprise Patterns, Real Code: Implementing Fowler’s Ideas in C# - Chris Woody Woodruff | Fractional Architect
6mon 18d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.woodruff.devThe Notable Difference in Dictionary Initialisation in C# - Improve & Repeat
6mon 23d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from improveandrepeat.com




























