top 8 comments

sorted by: hot top controversial new old
[–] 5 points 5 months ago (4 children)

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()) {
  • source
  • hideshow 4 child comments
  • [+] [S] 3 points 5 months ago (2 children)
  • [–] 3 points 5 months ago (5 children)
    private char Advance() {
       	return source[current++];
       }
    

    This is wrong. At least as I would expect advance to behave. While you do advance, you are returning the old value, not the new one. ++current would return the new value.

    Also, it would help if you said what the wrong behaviour was.

  • source
  • hideshow 5 child comments
  • [+] [S] 1 point 5 months ago (4 children)
  • [–] 2 points 5 months ago (1 child)
    while (Peek() != '*' && PeekAhead() != '/' && !IsAtEnd()) {
    

    Probably this guy.

    If you encounter an asterisk or a slash, then it stops. You want to stop when you encounter an asterisk and a slash, so:

    while (!(Peek() == '*' && PeekAhead() == '/') && !IsAtEnd()) {
    

    I also find your detection of when to remove the comment a bit scary, but that might be because you haven't shown the Match method.

  • source
  • parent
  • hideshow 1 child comment
  • load more comments (1 reply)
  • [–] 1 point 5 months ago (1 child)

    It's not strictly speaking a bug, but the behavior of Advance is a bit confusing. Based on how you use it in the code snippets, I would suggest changing it to

    private void Advance() {
        current++;
    }
    

    So that it only does what it says, and then changing the place where you read the next char to

    char c = Peek();
    Advance();
    

    Alternatively, you could rename it to something like ReadChar, which more closely matches its current behavior, since a read is expected to return the data (character) at the current position, and then advance the position

  • source
  • parent
  • hideshow 1 child comment
  • load more comments (1 reply)