erick-alcachofa a3d5c0ac68
feat(parser): implement full statement parsing and control flow logic
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

Complete the transition from a declarations-only parser to a functional
imperative parser. This commit introduces the implementation for all
major statement types, loop constructs, and core control flow logic.

- **Match Case Update**: Updated `grammar.ebnf` to use pipe delimiters
  `|id|` for unwrapped variables in match cases, replacing the previous
  parenthetical syntax.
- **Labels**: Implemented loop labeling using the `ident := loop`
  syntax. Labels are validated to ensure they only prefix valid loop
  constructs.
- **Labels and Ranges**: Standardized the use of the `:=` operator for
  both loop labels (`label := loop`) and range-for declarations (`let i
  := range`).

- **Conditional Branches**:
    - Fully implemented `if` and `else` statements.
    - Added support for optional variable unwrapping (e.g., `if (expr)
      |val|`).
    - Supported `else if` chaining by recursively parsing if-statements
      within else-branches.
- **Loops**:
    - **C-Style For**: Implemented `for (init; cond; post)` with
      optional initializers and post-loop expressions.
    - **Range For**: Implemented `for (let i := range)` with mutability
      controls.
    - **While & Do-While**: Implemented standard condition-based loops.
    - **Infinite Loop**: Added the explicit `loop` keyword for infinite
      iteration.
    - **Loop Dispatch**: Added a lookahead mechanism in
      `parseForLoopStatement` to differentiate between C-style and
      Range-style loops based on token positioning.

- **Variables**: Implemented `let`/`def` parsing within local scopes,
  including type annotations and initializers.
- **Defer Logic**: Implemented `defer` and `errdefer` for scope-guarded
  execution.
- **Jumps**: Implemented `break`, `continue` (with optional label
  targets), and `return` (with optional expressions).
- **Match & Switch**: Fully implemented branch parsing, with possible
  default cases via the `_` (underscore) keyword.

- **Expression Integration**: Stubbed `parseExpression` in a new
  `Expressions.cpp` to serve as the integration point for value parsing.
- **OverloadSet**: Integrated `OverloadSet` utility in `Statements.cpp`
  to cleanly handle AST node variant visitation for label injection.
- **Error Handling**: Standardized error reporting across all new paths
  using `langException`, providing specific "expected" messages for
  delimiters and keywords.
2025-12-25 23:27:06 -06:00

179 lines
5.0 KiB
C++

//============================================================================//
// //
// artichoke programming language //
// //
// Copyright (C) 2025 Erick Saul Guzman Ramos, whoami.artichoke.dev //
// //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero 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 Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
// //
//============================================================================//
#pragma once
#include <artichoke/Parser/AST/AST.hpp>
#include <artichoke/Tokenizer/Tokenizer.hpp>
namespace arti::lang {
struct Parser {
Parser(std::string source) noexcept;
Parser(std::string unitName, std::string source) noexcept;
Parser(Parser &&) noexcept;
Parser &operator=(Parser &&) noexcept;
Parser(const Parser &) noexcept = delete;
Parser &operator=(const Parser &) noexcept = delete;
Expected<ast::AST> parse();
Expected<Token>
consume(TokenV type, std::string_view message);
Expected<bool>
matchAndConsume(TokenV type);
Expected<bool>
match(TokenV type, std::size_t offset = 0);
Expected<ast::Optional<ast::TopLevelDeclNode>>
parseTopLevelDeclaration();
Expected<ast::ImportDeclNode>
parseImportDeclaration();
Expected<ast::AliasDeclNode>
parseAliasDeclaration();
Expected<ast::ModuleDeclNode>
parseModuleDeclaration();
Expected<ast::StructDeclNode>
parseStructDeclaration();
Expected<ast::EnumDeclNode>
parseEnumDeclaration();
Expected<ast::Vector<ast::GenericParamNode>>
parseGenericParamsList();
Expected<ast::GenericParamNode>
parseGenericParam();
Expected<ast::Vector<ast::StructMemberNode>>
parseStructMembersList();
Expected<ast::StructMemberNode>
parseStructMember();
Expected<ast::Vector<ast::EnumMemberNode>>
parseEnumMembersList();
Expected<ast::EnumMemberNode>
parseEnumMember();
Expected<ast::FunctionDeclNode>
parseFunctionDeclaration();
Expected<ast::NamespacedIdentifierNode>
parseNamespacedIdentifier();
Expected<ast::TypeNode>
parseType();
Expected<ast::Vector<ast::TypeQualifier>>
parseTypeQualifiers();
Expected<ast::Vector<ast::TypeNode>>
parseGenericArgumentsList();
Expected<ast::Vector<ast::FunctionParamNode>>
parseFunctionParamsList();
Expected<ast::FunctionParamNode>
parseFunctionParam();
Expected<ast::FunctionParamNode>
parseFunctionParamThis();
Expected<ast::CodeBlockStmtNode>
parseCodeBlock();
Expected<ast::Optional<ast::StatementNode>>
parseStatement();
Expected<ast::VariableStmtNode>
parseVariableStatement();
Expected<ast::IfStmtNode>
parseIfStatement();
Expected<ast::ElseBranchNode>
parseElseStatement();
Expected<ast::DeferStmtNode>
parseDeferStatement();
Expected<ast::ErrDeferStmtNode>
parseErrDeferStatement();
Expected<ast::ReturnStmtNode>
parseReturnStatement();
Expected<ast::BreakStmtNode>
parseBreakStatement();
Expected<ast::ContinueStmtNode>
parseContinueStatement();
Expected<ast::MatchStmtNode>
parseMatchStatement();
Expected<ast::SwitchStmtNode>
parseSwitchStatement();
Expected<ast::StatementNode>
parseForLoopStatement();
Expected<ast::CForStmtNode>
parseCForStatement();
Expected<ast::RangeForStmtNode>
parseRangeForStatement();
Expected<ast::WhileStmtNode>
parseWhileStatement();
Expected<ast::DoWhileStmtNode>
parseDoWhileStatement();
Expected<ast::InfLoopStmtNode>
parseInfLoopStatement();
Expected<ast::ExpressionStmtNode>
parseExpressionStatement();
Expected<ast::ExpressionNode>
parseExpression();
private:
std::string unitName;
std::string sourceCode;
Tokenizer tokenizer;
};
}