1.3.6 Expressions

A minilang primary expression is either a literal, identifier or a parenthesized expression.

primary‑expression literal | identifier | ( expression )

I have added a syntax tree node class for unary nodes and for parenthesized expressions.

// ast.ixx:

export module minilang.ast;

// ...

class UnaryNode : public Node
{
public:
    UnaryNode(NodeKind kind_, Node* child_);
    Node* Child() const { return child.get(); }
private:
    std::unique_ptr<Node> child;
};

class ParenthesizedExpressionNode : public UnaryNode
{
public:
    ParenthesizedExpressionNode(Node* child_);
    // ...
};

When a rule used by a parser is defined in another parser, a using statement is required to import the rule. A module import is also needed for the used parser.

// expression_parser.parser:

export module minilang.parser.expression;

[interface]import minilang.ast;
[implementation]import minilang.lexer;
[implementation]import minilang.token;
[implementation]import minilang.parser.literal;
[implementation]import minilang.parser.identifier;

parser ExpressionParser
{
    lexer minilang::lexer::MinilangLexer<char32_t>;

    using LiteralParser.Literal;
    using IdentifierParser.Identifier;

    Expression : minilang::ast::Node*
        ::= empty{ return nullptr; }
        ;

    PrimaryExpression : minilang::ast::Node*
        ::= Literal:literal{ return literal; }
        |   Identifier:identifier{ return identifier; }
        |   LPAREN Expression:expression RPAREN{ return new minilang::ast::ParenthesizedExpressionNode(expression); }
        ;
    ...
}