C Sharp

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()) {
4 replies

Improving C# Memory Safety

27d 5h ago by mander.xyz/u/nemeski in csharp@programming.dev from devblogs.microsoft.com
902

A 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.io
1113

Navigating and Learning Data Access in FSharp

1mon 12d ago by piefed.keyboardvagabond.com/u/ragingHungryPanda in csharp@programming.dev from blog.keyboardvagabond.com
104

AI integrations: rely or verify? Checking Semantic Kernel

1mon 25d ago by programming.dev/u/CodiUnicorn in csharp@programming.dev from pvs-studio.com
805

Explore union types in C# 15

2mon 16d ago by lemmy.world/u/LPThinker in csharp@programming.dev from devblogs.microsoft.com
806

Builder vs Fluent Interface Pattern in C#: Key Differences Explained

3mon 3d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.devleader.ca
1007

Builder Pattern Best Practices in C#: Code Organization and Maintainability

3mon 4d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.devleader.ca
1218

C# Extension Members | The .NET Tools Blog

3mon 10d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from blog.jetbrains.com
509

When 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.ca
4010

How 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.ca
5011

Deep C# - Multicast Delegates and Events

3mon 16d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.i-programmer.info
4012

Brave new C#

3mon 17d ago by programming.dev/u/CodiUnicorn in csharp@programming.dev from pvs-studio.com
2013

Automatic Dependency Injection in C#: The Complete Guide to Needlr

4mon 8d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.devleader.ca
7114

How 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.com
2015

Primary Constructors: Love them or Hate them?

4mon 16d ago by lemmy.world/u/monica_b1998 in csharp@programming.dev from slicker.me
14716

newtype - distinct type aliases for C#

4mon 17d ago by programming.dev/u/jupiter in csharp@programming.dev from programming.dev
7017

Using 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.com
3018

C++ 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.com
6719

Learn C# for free on Dometrain

4mon 25d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from mailchi.mp
-2320

Avoiding 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.com
3021

Making 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.net
5322

Convert 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.com
3123

Changing Immutable Collections

4mon 26d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from codeblog.jonskeet.uk
2024

Simple 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.net
4025

How 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.com
1026

Deep C# - The Console

4mon 28d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.i-programmer.info
3027

Collection Expression Arguments in C# 15+

4mon 28d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from steven-giesel.com
3128

Generate an Open Graph Profile Image with C#

4mon 28d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.josephguadagno.net
3029

A 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.com
2230

C# Is TIOBE Language of 2025

4mon 1d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.i-programmer.info
12131

C# – F# Interop (2026 edition)

4mon 1d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from www.planetgeek.ch
3032

C# 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.com
8033

Async/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.com
3134

Top 10 errors found in C# projects in 2025

5mon 20d ago by programming.dev/u/CodiUnicorn in csharp@programming.dev from pvs-studio.com
5035

Aman Ghodawala's Website - Async/Await for dummies in c#

5mon 27d ago by programming.dev/u/ghodawalaaman in csharp@programming.dev from sites.google.com
-4037

c# code of recursive descent to parse palindrome string

5mon 16h ago by programming.dev/u/ghodawalaaman in csharp@programming.dev
3038

Aman Ghodawala's Website

5mon 20h ago by programming.dev/u/ghodawalaaman in csharp@programming.dev from sites.google.com
3039

How to call c function from c#

6mon 2d ago by programming.dev/u/ghodawalaaman in csharp@programming.dev
4040

Need help creating a forth interpreter in c#

6mon 2d ago by programming.dev/u/ghodawalaaman in csharp@programming.dev
3041

why the code is not working?

6mon 3d ago by programming.dev/u/ghodawalaaman in csharp@programming.dev
101042

Difference between Method Overriding and Method Hiding in C#

6mon 7d ago by lemmy.ca/u/kionite231 in csharp@programming.dev from ghodawalaaman.blogspot.com
4043

Thread safety guide

6mon 8d ago by lemmy.world/u/monica_b1998 in csharp@programming.dev from slicker.me
3044

A simple question of C#

6mon 8d ago by lemmy.ca/u/kionite231 in csharp@programming.dev from ghodawalaaman.blogspot.com
0045

Collections in c#

6mon 8d ago by lemmy.ca/u/kionite231 in csharp@programming.dev from ghodawalaaman.blogspot.com
-1046

Checking osu! and exploring features of static analyzers

6mon 15d ago by programming.dev/u/CodiUnicorn in csharp@programming.dev from pvs-studio.com
3047

Property-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.com
4048

Enterprise 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.dev
1049

The Notable Difference in Dictionary Initialisation in C# - Improve & Repeat

6mon 23d ago by programming.dev/u/SmartmanApps in csharp@programming.dev from improveandrepeat.com
-1050