From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id KPCRMtNeBWcHXwAA62LTzQ:P1 (envelope-from ) for ; Tue, 08 Oct 2024 18:33:23 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id KPCRMtNeBWcHXwAA62LTzQ (envelope-from ) for ; Tue, 08 Oct 2024 18:33:23 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=X5F5WTYv; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=MjX2RW34; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; dmarc=pass (policy=none) header.from=maniero.me; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1728405203; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:list-id: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=lQ8OWG7hqi0maFXpEe1aQ7GSPu1NoHsy52qPb2TTNPQ=; b=GGmBml+86lc0SIoVZA6ekl3K35X8O2hHW0sPnszhtolM5UchRbAyPvE2CylgY/mRvhOqPW gz5zfL/cgF1IcAMo/oqxVAnblswPb59+W9FJg6FeiURXt0QEcPWfvoV5AyxyBcSYNA30md QKF+b4VBNIIQ71U3Dw4NOtwNk1xSHoRxNORy5zsGVhrSNHpAYsHQpruWqDVhT5uF/QQrEB 4GGTHPzA5iM4LPtCuIYBYzqfCqrR5b0wyPgj2/PXNLdwdKpIkFnSJ2SQZT8QMAtj5qjIqu 6Qfwm8WYn7vyIauHhuzjByrDeTr0RRvj7jdtXBBWnukqkiyVpH8SaJIkUu4l0A== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b=X5F5WTYv; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=MjX2RW34; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; dmarc=pass (policy=none) header.from=maniero.me; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1728405203; a=rsa-sha256; cv=pass; b=m/+GGyi4hq8mtVgNEvnGJ3S4t6r72nkE5LEIikRCrkrwWxQQ5/YXNKhukKaJs/ZQOkvkUs 2CmaRnFteiQWE8jQtLeJlRxSViHt80bGkzJ+hnn3V7lcJaqkoOPTsSmizbxY1BJ4Q9VDsT 5o5jBhdsqbQob76TEFbfvCBleMesM2XgiUCi+//3n1s1oUvACXDrpxaKpqWJMtSTSGCktx xcqt4IYHEudJehltUc43XCqlIGgy83+MXD1pmd1aAleKzgZLStKUafHgwJKrN3aoht3Wvq myCc+5FAsRIkRrDBR9XwzKHmrYhhQTUH6/a3xH21qL6NHuSlPmGHTCpc7JMfdw== Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id DED3181159 for ; Tue, 08 Oct 2024 18:33:22 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=OsMiUxvlgYiFf7mGYaOubvjI3rcPlqhkY3zXPASvEvU=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:Date:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1728405202; v=1; b=X5F5WTYveb9NnHKE/jDgLGgljq9G3N8Kn+7XFA/esoGEOcv1DzJsTbeWYVf0SqPIwXWUV7qy lQQDX/TcbUlDMPwQ9/ozS2CJPhhrp8HLZcpftFcCrBwljZuxmkgWiKFALUKDsF76pNrDMOlrnPA bWddRM8VRObBTxQE4pj6DL+BEKq8DZ/I4vvLP4dmgu+PszWe8FEtm4wtgAlh9O1W3asUrx3uwO+ fc1VPfYv3qDGUr6+w5m6iygmQuBhXcAQk2U808Vynu0qnR7lSioHfbV2aY6o//GDGzWUoJ04n8i GLAgNhoSoqCWsF5pwxuWHlqv67onualSdVAFrhcsWQkMA== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id A3F1620197 for ; Tue, 08 Oct 2024 16:33:22 +0000 (UTC) Received: from fennec.ash.relay.mailchannels.net (fennec.ash.relay.mailchannels.net [23.83.222.58]) by mail-a.sr.ht (Postfix) with ESMTPS id 7C9BD20156 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 08 Oct 2024 16:33:21 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id D2342844DDE for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 8 Oct 2024 16:33:19 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (trex-11.trex.outbound.svc.cluster.local [100.101.176.20]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id ED04B844EEA for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 8 Oct 2024 16:33:17 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1728405198; a=rsa-sha256; cv=none; b=9Dt2++seBZ3XSj5WSLvua1wt3QwTS5flVkWCqE61gcx3Sajh0ztrZ+aMjQvoomRLzg2ysD eZs13S85s0vVvtL9G9E8D+qyCBmo3QwCmkY8xpY1J0zqjHaASK01TUv92el44WjDPnJUWH LC+9nXqEaeL1bUnKsc711JXVG5J4xFDh87KywkS+MyMsFEVlIhP5UuVJttCZEhL1v2ZfUA XVJGjzjJVGQn30DuhQxfIMrRIYAJcKzn87LR4v+ElYItLdttq9SgybjgsB7yj7OQASo1yJ V7u0tFax6eh3LPcEtd+3VjubXXfgzoBnvWoavkufnD3DMkjDug3Rfvcj7uevFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1728405198; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:dkim-signature; bh=lQ8OWG7hqi0maFXpEe1aQ7GSPu1NoHsy52qPb2TTNPQ=; b=BPAC1sIbYaynLNC4eyBiuXdNUPXgRW4R3gOOkVyEmOcGAmgx2BLsKUFLH740TKssdSNyQN tgMsZSMfhjAYZV9taJZRA51MxNVVRb69qkAMdkGU3pCIQJ5n+PmpugumltWFEXGWjxNOJm U5e2PF0DcIjO31oiOteZCXP2tTuMe3nOPb5Hosdcvaicb7Fu/KZyX5lxs2SYJRIRZxG8lv Q12App1Me/x67tzsSiFhePWhntq3fdXQw/y95MGR4y1zrlq22HXxJ4M2IOEQGCiX9ZHC0a XX0un+eLN7c0FDuHkJAZEh51+3z6QhtI0tQbgB6P1DC5EVP8asJOv91hfTtJaQ== ARC-Authentication-Results: i=1; rspamd-54f99b5bc4-xhcqv; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Trade-Occur: 58382328504e4e45_1728405199580_1389362991 X-MC-Loop-Signature: 1728405199580:1273843246 X-MC-Ingress-Time: 1728405199580 Received: from nl-srv-smtpout1.hostinger.io (nl-srv-smtpout1.hostinger.io [145.14.150.87]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.101.176.20 (trex/7.0.2); Tue, 08 Oct 2024 16:33:19 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1728405196; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=lQ8OWG7hqi0maFXpEe1aQ7GSPu1NoHsy52qPb2TTNPQ=; b=MjX2RW34DnjCsMA/kDQy5mAdtDQklWN4f4PcaZNyJZ3owqeQD0XGQI8qlKtoqP7Blu78h2 c2vxP4NV8t/MUzCvtlQqZgTDmhtVIUYPnomL9iJdWbBmsBbC1jlf2t8VFnrwPgWoCTgaLP ch6Q4KVS/E4kaflqXz4mEUZClGi6G39CZt/VVlY4KrMeiQ4sZ8g8kY8jZgRQYFHsjUWK4/ 0yIUgAYIvafVd7tFGtKkp8NdrFb7zMndBQ/d0I3+m9Hssgfeo+kzsZ1j8WzzbtQud+wg60 LvzLUnCzrunpfnJpyx/QrQr2i/ge+FFJhSkFhsjg+DIrPjqvwt34DIeyRy2xtA== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang] parser: conform block line feeds with the spec Message-ID: <20241008163307.76735-1-carlos@maniero.me> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Tue, 08 Oct 2024 16:33:15 +0000 (UTC) X-CM-Envelope: MS4xfCt/GV0pEyFC3QEVzEBqVg6YruZiziTN7t6v++8SCa2Q497HwydkVrmKJOQUB0nng/D+fFtJBCFdSWDXr7UVQuZCABzaffTTwQc7FLuW9KwfdTO6vB+u NN4SvhMZmTw0rJAxukPkCMlZNawQDwWRK1vhIWuq8ZFTaKZMKi3m4kRe/+0R111pW5AqrM5VPfbTwq+gPTjULNRNRMVMl2Ejwgv2t9cbniAtYsUZszq+xVE/ X-CM-Analysis: v=2.4 cv=bZY5U/PB c=1 sm=1 tr=0 ts=67055ecc a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=mDV3o1hIAAAA:8 a=ZUa0bTZW6g0UX3PZ8QAA:9 a=BXDaF_L80NY05PYiAFlV:22 X-AuthUser: carlos@maniero.me X-Sourcehut-Patchset-Status: PROPOSED List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Country: NL X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx11.migadu.com X-Migadu-Spam-Score: -1.61 X-Spam-Score: -1.61 X-Migadu-Queue-Id: DED3181159 X-TUID: kARrEXvjtGTp The line feeds has been handled by the statement parser functions when the olang spec describe this grammar rule as a block responsibility. There was also a FIXME related to allowing line feeds in between the if/else statement and the block start that was also addressed. Signed-off-by: Carlos Maniero --- src/parser.c | 47 ++++++++++++++----------- tests/olc/0031_else_extra_line_feeds.ol | 30 ++++++++++++++++ 2 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 tests/olc/0031_else_extra_line_feeds.ol diff --git a/src/parser.c b/src/parser.c index d16b79d..2096129 100644 --- a/src/parser.c +++ b/src/parser.c @@ -70,6 +70,9 @@ parser_parse_factor(parser_t *parser); static void skip_line_feeds(lexer_t *lexer); +static void +peek_next_non_lf_token(lexer_t *lexer, token_t *token); + void parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena) { @@ -493,6 +496,12 @@ StartLoop: return NULL; } + if (!skip_expected_token(parser, TOKEN_LF)) { + return NULL; + } + + skip_line_feeds(parser->lexer); + list_append(node_block->as_block.nodes, node); goto StartLoop; @@ -522,11 +531,6 @@ parser_parse_return_stmt(parser_t *parser) ast_node_t *node_return_stmt = ast_new_node_return_stmt(parser->arena, token_ret.loc, expr); assert(node_return_stmt); - if (!skip_expected_token(parser, TOKEN_LF)) { - return NULL; - } - skip_line_feeds(parser->lexer); - return node_return_stmt; } @@ -544,6 +548,8 @@ parser_parse_if_stmt(parser_t *parser) return NULL; } + skip_line_feeds(parser->lexer); + ast_node_t *then = parser_parse_block(parser); if (then == NULL) { @@ -553,29 +559,25 @@ parser_parse_if_stmt(parser_t *parser) ast_node_t *_else = NULL; token_t next_token; - lexer_next_token(parser->lexer, &next_token); - - // FIXME: We are not allowing line feed right after if block statement when - // the else branch is present. We also noticed that if has the same - // problem which will be addressed later. + peek_next_non_lf_token(parser->lexer, &next_token); if (next_token.kind == TOKEN_ELSE) { + skip_line_feeds(parser->lexer); + lexer_next_token(parser->lexer, &next_token); + skip_line_feeds(parser->lexer); + _else = parser_parse_block(parser); if (_else == NULL) { return NULL; } - } else if (!expected_token(&next_token, TOKEN_LF)) { - return NULL; } ast_node_t *node_if_stmt = ast_new_node_if_stmt(parser->arena, token_if.loc, cond, then, _else); assert(node_if_stmt); - skip_line_feeds(parser->lexer); - return node_if_stmt; } @@ -608,8 +610,6 @@ parser_parse_var_def(parser_t *parser) ast_node_t *var_node = ast_new_node_var_def(parser->arena, token_id.loc, token_id.value, var_type, expr); - skip_line_feeds(parser->lexer); - return var_node; } @@ -631,10 +631,6 @@ parser_parse_var_assign_stmt(parser_t *parser) ast_node_t *ref = ast_new_node_ref(parser->arena, token_id.loc, token_id.value); ast_node_t *expr = parser_parse_expr(parser); - // FIXME: The expected line feed should be parsed from parent call - // according to the grammar rules - skip_line_feeds(parser->lexer); - return ast_new_node_var_assign_stmt(parser->arena, token_eq.loc, ref, expr); } @@ -683,3 +679,14 @@ skip_line_feeds(lexer_t *lexer) lexer_peek_next(lexer, &token); } } + +static void +peek_next_non_lf_token(lexer_t *lexer, token_t *token) +{ + lexer_cursor_t cur = lexer->cur; + + skip_line_feeds(lexer); + lexer_peek_next(lexer, token); + + lexer->cur = cur; +} diff --git a/tests/olc/0031_else_extra_line_feeds.ol b/tests/olc/0031_else_extra_line_feeds.ol new file mode 100644 index 0000000..bc44ccd --- /dev/null +++ b/tests/olc/0031_else_extra_line_feeds.ol @@ -0,0 +1,30 @@ +# Copyright (C) 2024 olang mantainers +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +fn main(): u32 +{ + if 0 != 0 + { + return 1 + } + else + { + return 0 + } +} + +# TEST test_compile(exit_code=0) + +# TEST test_run_binary(exit_code=0) base-commit: 3c8975ba27c87d084187eefe622cbd783e289c99 -- 2.46.0