ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.81k stars 2.54k forks source link

Proposal: change the align(Expr:IntegerLiteral:IntegerLiteral) syntax #2973

Closed andersfr closed 4 years ago

andersfr commented 5 years ago

Resubmission of #2954 as proposal.

The syntax requires lookahead > 1 in the parser to identify and resolve correctly. The problem arises because Expr is parsed first and it expects a labelled block when encountering the colon token when Expr=Identifier.

I propose to change the colon after Expr in the align(...) into a different symbol without conflicting parses. My suggestion is to create a '::' token to preserve the look of the syntax, i.e. align(Expr::IntegerLiteral:IntegerLiteral).

Zig compiler fails to handle the syntax:

pub fn main() void {
    const identifier: usize = 8;
    const test1 = *align(label: { break :label 8; }:1:32) u8;
    const test2 = *align(identifier:1:32) u8;
}

issue.zig:4:37: error: invalid token: '1' const test2 = *align(identifier:1:32) u8;

andersfr commented 5 years ago

I fixed it in my own context-free LALR parser implementation. Perhaps it is better to just fix the Zig compiler after all (the PEG reference in zig-spec can parse it correctly). I already know how to make the patch :)

Vexu commented 4 years ago

This parser correctly now.

pub fn main() void {
    const identifier: usize = 8;
    const test1 = *align(label: { break :label 8; }:1:32) u8;
    const test2 = *align(identifier:1:32) u8;
    @compileLog(test2); // | *align(8:1:32) u8
}