From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.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 cG7OOSd0AGfxNQEAqHPOHw:P1 (envelope-from ) for ; Sat, 05 Oct 2024 01:03:04 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id cG7OOSd0AGfxNQEAqHPOHw (envelope-from ) for ; Sat, 05 Oct 2024 01:03:04 +0200 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b="G/NcZXwB"; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=O+qHexjJ; 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=1728082983; 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=5JdQaKvWfV4Do3H5XF+kJKk9Z4gmClnpJzSyBfGQ05I=; b=GpZqbFiIzrse0iKC325OsyaPoCPAS0UY1dkzL9pnaob78frQWV3B7L948V8aokWCSnlPaw SCW04ONeM9vtWNRwb9YPAAiTsFbpZjO5I11QP+DVnKV5SAIy0pJ8ESMGmbSYliwKe6r7hE J1h5zpV5AKOYufiLv9MFqJKwng1G9yXx/5aOltX8x1qsU5Tu6BNBPRioKYogelEg4GzYXv 04lG5UT5H4ZHTkAv48ImH7VvqOZeKNpjKU4uqaBKu8SghU3BgKivQ5mumtlU5vBn023Zyj 1TQzaW6usswYQPfHxjdSb171Moco2SdMZKoPPhQLjilc82VgmhzPV+RygQEaKw== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b="G/NcZXwB"; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=O+qHexjJ; 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=1728082983; a=rsa-sha256; cv=pass; b=inQYtIk/zlFipH+iQYiciFyMuH9B6weQuXDsltJbkmuVsTvNqc3OpAU0pLC6VDrfNv/2Hq Hslr0pNZX+2UbCpSsB6NF0xeNCczWg8IdT4cquoRNVajuyDB+EzM1MoFAIgVfHhS5u+pgc x5KeMY/18SDcJ+65ZAqbd/TBptgA4DcS8JEklrOQ97rnPYD+IcjoH+Khk8UXg2g2QZ1A2V HMjDBqa3blxIEvQzKXFJa3kzwT1+5trJYWVFmNp7unWzposWPLhskpRm0UQvB3UF9r6Vxa Htkj2uNckz4mKs9r3qsFb+t3cJYNR1NfUSaTU7NLw+E/yZSXnOA+bfEWlpqkNw== 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 BF98D8D156 for ; Sat, 05 Oct 2024 01:03:02 +0200 (CEST) DKIM-Signature: a=rsa-sha256; bh=TV1ArBw107knpLk3mI9YbtFZY2MhQo2/fTQhLk/5a04=; 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=1728082975; v=1; b=G/NcZXwBSLyPoOQLq3p6RlmDzmo0oexRe7oWP+GniPFh7cbnApDRAdD9ZC3u34SxdNJILTcC 2wwUf9tlkcr35vSu4YbmERGnVDuhQULCiYNRzzdODzvSGZ/1yNkgjrpYvu5+j1mppO69tdLBdE4 pdZBLsMVoOCaSWhjisCoWawBXH/AGuvijfG4J2U+XGQtZB2jT8Q+kxzuOgVNNok/RvOANc8K+wM uBCKfozBbEZJVZEr5/xAOmt/C/bQlHdmbbMH9b0DgxWPBxaC5rQujmZMt9Rn0/YIexzuV0vT9nr 8n+3vjyjXsWCsBAZVp1Hd2d6mYI7W1GzQQEayz7X8GSQw== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 86D4620246 for ; Fri, 04 Oct 2024 23:02:55 +0000 (UTC) Received: from buffalo.birch.relay.mailchannels.net (buffalo.birch.relay.mailchannels.net [23.83.209.24]) by mail-a.sr.ht (Postfix) with ESMTPS id 8C3932023E for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 04 Oct 2024 23:02:50 +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 57911A3120 for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 4 Oct 2024 23:02:44 +0000 (UTC) Received: from nl-srv-smtpout1.hostinger.io (trex-2.trex.outbound.svc.cluster.local [100.101.175.87]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 9F1BDA4CB1 for <~johnnyrichard/olang-devel@lists.sr.ht>; Fri, 4 Oct 2024 23:02:43 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1728082964; a=rsa-sha256; cv=none; b=wBmDrwD/L+qZHn6MjK2kntPfRVwaAmBNY8LVnwGSyHWu1iIZ/SQe98a+sScduPlnWuo4Ms aCNJzNJWDCU11YTpT2GzHZ4w6yVHJlrREs8bpEAw6U6AuHSlDxRSLDIifpWaMwE6/j9+8X JYN1N52hyVQAp58OZJ+sVYTyf89RZtWWgDo+smtmcbV1HJBlgV5Fz6rjawcdM/iSbx4nld O1v6mZSsocckw1kMGOm8tZk07dpWPRvUlotUhZCiCg2vRf95JTpU3fOiT64DfdOvzs5gy5 iVuDA7SSwwr4OSMmODc+20J0+YnrQQOpKjeyq5uLQxpTNushbefOgK281opIvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1728082964; 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=5JdQaKvWfV4Do3H5XF+kJKk9Z4gmClnpJzSyBfGQ05I=; b=KGuVPkGF72Kg7Xfg1acnBHBN40gjvgUSLi/+jhcBOpltfp2GTXqc4uXgwwoj44AX0bc6+J OFoG8UkxIvvnH/jK70EF4ficNxxUlP1WiKNo+8pQsoiWplj4JTiVMTqira/9DqZ+tct1mW 4f4p+qOK8RLXckwZS8j0/QAgLhqkTelrYxQt2fGLN9mLlXichOqRQPaj80LD/XZRFg+SZ3 bXgdv09YiLrdlGtV9dCeyzpAo8Snxk1l9Do0+Vh6bIRZqBnWuRCZ2iykQkZY8G/fnWdwKR 1RBCtDs9b18zvLOaOb0IP7ogDSWSEfpZ6B11A8+NASBPm+KnQ5c3xphE1C1Avg== ARC-Authentication-Results: i=1; rspamd-58b99b864b-4zmmd; 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-Cooing-Abaft: 7a3be0274d4ad977_1728082964248_286142442 X-MC-Loop-Signature: 1728082964248:1708369144 X-MC-Ingress-Time: 1728082964248 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.175.87 (trex/7.0.2); Fri, 04 Oct 2024 23:02:44 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1728082961; 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=5JdQaKvWfV4Do3H5XF+kJKk9Z4gmClnpJzSyBfGQ05I=; b=O+qHexjJhE7UKhl9getCR4a47tM8Im/d45iD7qPlYRHOvx/7N0iIMusu22h1S0R5ubLV1k gtsCEaqWWlLLFDJDK+xJuVKiidesx8ZmKVTlLtyANvalV+bHoQ8nxMZPoreVlE7uq41ObQ Wo+Gwd6LeSsydi7DAivd7W5uaJ4csNDfBCR12J2WKj3NrEv+a6RiHTv5yXmSvqOGdskJmk 5XJvSZ04GlClRsPlFKUUEGKfU/vESt1Yp/BR/qrfP1i7Hy3/f+Xcyxds/b/nNBU2LMUfur 4VAf+MTCy4whvBlzCKmhbZQ3tTqDrNwuW1+Pe11B4Y0F6o6HgnXtFh44rYvKxQ== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang] ast: add token location at the ast nodes Message-Id: <20241004230210.184300-1-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Fri, 04 Oct 2024 23:02:41 +0000 (UTC) X-CM-Envelope: MS4xfBdmCwv7Bzvy2XmoDW198psdkgq6+tQUA+It1JBYzxCWI23GhZsN/R6Hjr55MUYBk3EMXJ/m3dgjN9/02ajO/p46mpXlDHPR/4VeSQ/GGNHS0bD8wlb0 UHILdLQ7R4Y9z791exIHer2s38D/d2DN629p8YiPh8xF2xTQvY27OI1ssme8mR74Oa7anCVnbOODxrVXufHoVteoiZTqpK8yPEDja2uqZHFwN5ORmUxYStY4 X-CM-Analysis: v=2.4 cv=b6rg4cGx c=1 sm=1 tr=0 ts=67007411 a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=m4VVMLW0We-JNJnaQD0A: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.64 X-Spam-Score: -1.64 X-Migadu-Queue-Id: BF98D8D156 X-TUID: 8AzgOqHNVHar This is an important step for future semantics error reporting and binary debug information. Signed-off-by: Carlos Maniero --- src/ast.c | 29 +++++++++++++++++++-------- src/ast.h | 55 ++++++++++++++++++++++++++++++++++------------------ src/parser.c | 29 ++++++++++++++------------- 3 files changed, 73 insertions(+), 40 deletions(-) diff --git a/src/ast.c b/src/ast.c index db18426..1224305 100644 --- a/src/ast.c +++ b/src/ast.c @@ -40,7 +40,12 @@ ast_new_translation_unit(arena_t *arena) } ast_node_t * -ast_new_node_fn_def(arena_t *arena, string_view_t id, list_t *params, string_view_t return_type, ast_node_t *block) +ast_new_node_fn_def(arena_t *arena, + token_loc_t loc, + string_view_t id, + list_t *params, + string_view_t return_type, + ast_node_t *block) { assert(arena); assert(params); @@ -50,6 +55,7 @@ ast_new_node_fn_def(arena_t *arena, string_view_t id, list_t *params, string_vie assert(node_fn_def); node_fn_def->kind = AST_NODE_FN_DEF; + node_fn_def->loc = loc; ast_fn_definition_t *fn_def = &node_fn_def->as_fn_def; fn_def->id = id; @@ -61,7 +67,7 @@ ast_new_node_fn_def(arena_t *arena, string_view_t id, list_t *params, string_vie } ast_node_t * -ast_new_node_fn_call(arena_t *arena, string_view_t id, list_t *args) +ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t *args) { assert(arena); assert(args); @@ -70,6 +76,7 @@ ast_new_node_fn_call(arena_t *arena, string_view_t id, list_t *args) assert(node_fn_call); node_fn_call->kind = AST_NODE_FN_CALL; + node_fn_call->loc = loc; ast_fn_call_t *fn_call = &node_fn_call->as_fn_call; fn_call->id = id; @@ -79,12 +86,13 @@ ast_new_node_fn_call(arena_t *arena, string_view_t id, list_t *args) } ast_node_t * -ast_new_node_var_def(arena_t *arena, string_view_t id, string_view_t type, ast_node_t *value) +ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, string_view_t type, ast_node_t *value) { ast_node_t *node_var_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_var_def); node_var_def->kind = AST_NODE_VAR_DEF; + node_var_def->loc = loc; ast_var_definition_t *var_def = &node_var_def->as_var_def; var_def->id = id; @@ -95,12 +103,13 @@ ast_new_node_var_def(arena_t *arena, string_view_t id, string_view_t type, ast_n } ast_node_t * -ast_new_node_bin_op(arena_t *arena, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs) +ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs) { ast_node_t *node_bin_op = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_bin_op); node_bin_op->kind = AST_NODE_BINARY_OP; + node_bin_op->loc = loc; node_bin_op->as_bin_op.kind = kind; node_bin_op->as_bin_op.lhs = lhs; node_bin_op->as_bin_op.rhs = rhs; @@ -109,12 +118,13 @@ ast_new_node_bin_op(arena_t *arena, ast_binary_op_kind_t kind, ast_node_t *lhs, } ast_node_t * -ast_new_node_literal_u32(arena_t *arena, uint32_t value) +ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value) { ast_node_t *node_literal = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_literal); node_literal->kind = AST_NODE_LITERAL; + node_literal->loc = loc; node_literal->as_literal.kind = AST_LITERAL_U32; node_literal->as_literal.as_u32 = value; @@ -122,36 +132,39 @@ ast_new_node_literal_u32(arena_t *arena, uint32_t value) } ast_node_t * -ast_new_node_ref(arena_t *arena, string_view_t id) +ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id) { ast_node_t *node_ref = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_ref); node_ref->kind = AST_NODE_REF; + node_ref->loc = loc; node_ref->as_ref.id = id; return node_ref; } ast_node_t * -ast_new_node_return_stmt(arena_t *arena, ast_node_t *expr) +ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr) { ast_node_t *node_return_stmt = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t)); assert(node_return_stmt); node_return_stmt->kind = AST_NODE_RETURN_STMT; + node_return_stmt->loc = loc; node_return_stmt->as_return_stmt.expr = expr; return node_return_stmt; } ast_node_t * -ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then, ast_node_t *_else) +ast_new_node_if_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then, ast_node_t *_else) { ast_node_t *node_if_stmt = arena_alloc(arena, sizeof(ast_node_t)); assert(node_if_stmt); node_if_stmt->kind = AST_NODE_IF_STMT; + node_if_stmt->loc = loc; node_if_stmt->as_if_stmt.cond = cond; node_if_stmt->as_if_stmt.then = then; node_if_stmt->as_if_stmt._else = _else; diff --git a/src/ast.h b/src/ast.h index 4791d6b..f9a23b5 100644 --- a/src/ast.h +++ b/src/ast.h @@ -20,6 +20,7 @@ #include #include "arena.h" +#include "lexer.h" #include "list.h" #include "scope.h" #include "string_view.h" @@ -42,15 +43,21 @@ typedef enum AST_NODE_UNKNOWN } ast_node_kind_t; +typedef struct ast_node_meta +{ + ast_node_kind_t kind; + token_loc_t loc; +} ast_node_meta_t; + typedef struct ast_block { - ast_node_kind_t node_kind; + ast_node_meta_t meta; list_t *nodes; } ast_block_t; typedef struct ast_translation_unit { - ast_node_kind_t node_kind; + ast_node_meta_t meta; list_t *decls; } ast_translation_unit_t; @@ -62,7 +69,7 @@ typedef struct ast_fn_param typedef struct ast_fn_definition { - ast_node_kind_t node_kind; + ast_node_meta_t meta; string_view_t id; list_t *params; string_view_t return_type; @@ -72,7 +79,7 @@ typedef struct ast_fn_definition typedef struct ast_fn_call { - ast_node_kind_t node_kind; + ast_node_meta_t meta; string_view_t id; list_t *args; scope_t *scope; @@ -80,7 +87,7 @@ typedef struct ast_fn_call typedef struct ast_var_definition { - ast_node_kind_t node_kind; + ast_node_meta_t meta; string_view_t id; string_view_t type; ast_node_t *value; @@ -94,7 +101,7 @@ typedef enum typedef struct ast_literal { - ast_node_kind_t node_kind; + ast_node_meta_t meta; ast_literal_kind_t kind; union { @@ -104,7 +111,7 @@ typedef struct ast_literal typedef struct ast_ref { - ast_node_kind_t node_kind; + ast_node_meta_t meta; string_view_t id; scope_t *scope; } ast_ref_t; @@ -133,7 +140,7 @@ typedef enum ast_binary_op_kind typedef struct ast_binary_op { - ast_node_kind_t node_kind; + ast_node_meta_t meta; ast_binary_op_kind_t kind; ast_node_t *lhs; ast_node_t *rhs; @@ -141,13 +148,13 @@ typedef struct ast_binary_op typedef struct ast_return_stmt { - ast_node_kind_t node_kind; + ast_node_meta_t meta; ast_node_t *expr; } ast_return_stmt_t; typedef struct ast_if_stmt { - ast_node_kind_t node_kind; + ast_node_meta_t meta; ast_node_t *cond; ast_node_t *then; ast_node_t *_else; @@ -155,7 +162,12 @@ typedef struct ast_if_stmt typedef union ast_node { - ast_node_kind_t kind; + // inlined ast_node_meta_t struct. + struct + { + ast_node_kind_t kind; + token_loc_t loc; + }; ast_translation_unit_t as_translation_unit; ast_fn_definition_t as_fn_def; ast_fn_call_t as_fn_call; @@ -172,28 +184,33 @@ ast_node_t * ast_new_translation_unit(arena_t *arena); ast_node_t * -ast_new_node_fn_def(arena_t *arena, string_view_t id, list_t *params, string_view_t return_type, ast_node_t *block); +ast_new_node_fn_def(arena_t *arena, + token_loc_t loc, + string_view_t id, + list_t *params, + string_view_t return_type, + ast_node_t *block); ast_node_t * -ast_new_node_fn_call(arena_t *arena, string_view_t id, list_t *args); +ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t *args); ast_node_t * -ast_new_node_var_def(arena_t *arena, string_view_t id, string_view_t type, ast_node_t *value); +ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, string_view_t type, ast_node_t *value); ast_node_t * -ast_new_node_bin_op(arena_t *arena, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs); +ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs); ast_node_t * -ast_new_node_literal_u32(arena_t *arena, uint32_t value); +ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value); ast_node_t * -ast_new_node_ref(arena_t *arena, string_view_t id); +ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id); ast_node_t * -ast_new_node_return_stmt(arena_t *arena, ast_node_t *expr); +ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr); ast_node_t * -ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then, ast_node_t *_else); +ast_new_node_if_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then, ast_node_t *_else); ast_node_t * ast_new_node_block(arena_t *arena); diff --git a/src/parser.c b/src/parser.c index ecc10f0..35c8107 100644 --- a/src/parser.c +++ b/src/parser.c @@ -224,7 +224,7 @@ parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence) lexer_peek_next(parser->lexer, &lookahead_token); } - lhs = ast_new_node_bin_op(parser->arena, token_kind_to_binary_op_kind(token_op.kind), lhs, rhs); + lhs = ast_new_node_bin_op(parser->arena, token_op.loc, token_kind_to_binary_op_kind(token_op.kind), lhs, rhs); if (lhs == NULL) { return NULL; } @@ -252,19 +252,19 @@ parser_parse_factor(parser_t *parser) switch (token.kind) { case TOKEN_NUMBER: - return ast_new_node_literal_u32(parser->arena, string_view_to_u32(token.value)); + return ast_new_node_literal_u32(parser->arena, token.loc, string_view_to_u32(token.value)); case TOKEN_ID: { - string_view_t id = token.value; + token_t token_id = token; lexer_peek_next(parser->lexer, &token); if (token.kind == TOKEN_OPAREN) { list_t *args = parser_parse_fn_args(parser); - return ast_new_node_fn_call(parser->arena, id, args); + return ast_new_node_fn_call(parser->arena, token_id.loc, token_id.value, args); } - return ast_new_node_ref(parser->arena, id); + return ast_new_node_ref(parser->arena, token_id.loc, token_id.value); } case TOKEN_OPAREN: { @@ -411,7 +411,7 @@ parser_parse_fn_definition(parser_t *parser) return NULL; } - return ast_new_node_fn_def(parser->arena, fn_name_token.value, params, fn_return_type, block); + return ast_new_node_fn_def(parser->arena, fn_name_token.loc, fn_name_token.value, params, fn_return_type, block); } static bool @@ -498,7 +498,9 @@ EndLoop: static ast_node_t * parser_parse_return_stmt(parser_t *parser) { - if (!skip_expected_token(parser, TOKEN_RETURN)) { + token_t token_ret; + + if (!expected_next_token(parser, &token_ret, TOKEN_RETURN)) { return NULL; } @@ -507,7 +509,7 @@ parser_parse_return_stmt(parser_t *parser) return NULL; } - ast_node_t *node_return_stmt = ast_new_node_return_stmt(parser->arena, expr); + 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)) { @@ -521,7 +523,8 @@ parser_parse_return_stmt(parser_t *parser) static ast_node_t * parser_parse_if_stmt(parser_t *parser) { - if (!skip_expected_token(parser, TOKEN_IF)) { + token_t token_if; + if (!expected_next_token(parser, &token_if, TOKEN_IF)) { return NULL; } @@ -557,7 +560,7 @@ parser_parse_if_stmt(parser_t *parser) return NULL; } - ast_node_t *node_if_stmt = ast_new_node_if_stmt(parser->arena, cond, then, _else); + ast_node_t *node_if_stmt = ast_new_node_if_stmt(parser->arena, token_if.loc, cond, then, _else); assert(node_if_stmt); @@ -573,8 +576,8 @@ parser_parse_var_def(parser_t *parser) return NULL; } - token_t id_token; - if (!expected_next_token(parser, &id_token, TOKEN_ID)) { + token_t token_id; + if (!expected_next_token(parser, &token_id, TOKEN_ID)) { return NULL; } @@ -593,7 +596,7 @@ parser_parse_var_def(parser_t *parser) return NULL; } - ast_node_t *var_node = ast_new_node_var_def(parser->arena, id_token.value, var_type, expr); + 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); base-commit: 832f13d2ed2762bb9582eb1b633a30af608e028f -- 2.34.1