public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: "Carlos Maniero" <carlos@maniero.me>
To: "Johnny Richard" <johnny@johnnyrichard.com>,
	<~johnnyrichard/olang-devel@lists.sr.ht>
Subject: Re: [PATCH olang v2 1/3] lexer: add tokenize support to binary op tokens
Date: Sun, 17 Mar 2024 21:30:34 -0300	[thread overview]
Message-ID: <CZWGA19PG4DZ.1KWSAW2TV1XKJ@maniero.me> (raw)
In-Reply-To: <20240317213638.131057-2-johnny@johnnyrichard.com>

> +            case '=': {
> +                size_t start_offset = lexer->offset;
> +
> +                if (lexer_peek_next_char(lexer) == '=') {
> +                    lexer_skip_char(lexer);
> +                    lexer_skip_char(lexer);
> +                    lexer_init_str_value_token(lexer, token, TOKEN_CMP_EQ, start_offset);
> +                    return;
> +                }
> +
> +                lexer_init_char_value_token(lexer, token, TOKEN_EQ);
> +                lexer_skip_char(lexer);
> +                return;
> +            }

We could apply the *maximal munch* [1] here avoiding looking to the
future by skipping the char right after founding it:

+            case '=': {
+                size_t start_offset = lexer->offset;
+                lexer_skip_char(lexer);
+
+                if (lexer_current_char(lexer) == '=') {
+                    lexer_skip_char(lexer);
+                    lexer_init_str_value_token(lexer, token, TOKEN_CMP_EQ, start_offset);
+                    return;
+                }
+
+                lexer_init_char_value_token(lexer, token, TOKEN_EQ);
+                lexer_skip_char(lexer);
+                return;
+            }

I know it is a silly change, but this help us to keep the parsing
process as deterministic as possible.

[1]: https://en.wikipedia.org/wiki/Maximal_munch#:~:text=In%20computer%20programming%20and%20computer,as%20possible%20should%20be%20consumed.

  reply	other threads:[~2024-03-18  0:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-17 21:29 [PATCH olang v2 0/3] frontend: add binary operation expr support Johnny Richard
2024-03-17 21:29 ` [PATCH olang v2 1/3] lexer: add tokenize support to binary op tokens Johnny Richard
2024-03-18  0:30   ` Carlos Maniero [this message]
2024-03-18  8:49     ` Johnny Richard
2024-03-17 21:29 ` [PATCH olang v2 2/3] ast: create binary operation ast node Johnny Richard
2024-03-17 21:29 ` [PATCH olang v2 3/3] parser: add all binary operation expressions Johnny Richard
2024-03-17 20:37   ` [olang/patches/.build.yml] build success builds.sr.ht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CZWGA19PG4DZ.1KWSAW2TV1XKJ@maniero.me \
    --to=carlos@maniero.me \
    --cc=johnny@johnnyrichard.com \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox