Skip to content

Update grammar.y #43

Open
Open
@perillo

Description

Currently, grammar.y is out of sync with the actual grammar implemented by the zig compiler (in std/zig/tokenizer and std/zig/parser.zig).

These are the places where grammar.y and zig diverges, found with the improved parser from #42, using files in src and lib/std in the zig repository.

  • doc-comments not allowed in top-level comptime and test declaration.

    /// doc-comment for comptime.
    comptime {
        var a = 1;
        _ = a;
    }
    
    /// doc-comment for test.
    test {}
  • Saturating arithmetic is not supported by grammar.y.

    test {
        var a: isize = 10;
    
        a +|= 1;
        a -|= 1;
        a *|= 1;
        a <<|= 1;
    
        const b: isize = a +| 1;
        const c: isize = b -| 1;
        const d: isize = c *| 1;
        const e: isize = d <<| 1;
        _ = e;
    }
  • Mixed doc-comment and line-comment is not supported by grammar.y.

    // A doc-comment followed by a line-comment is not supported by grammar.y.
    const S = struct {
        //! doc
        /// doc
        // doc
        a: i32,
    };

    NOTE: I found mixing doc-comment and line-comment confusing, and autodoc doesn't not handle them correctly.

    Examples:

    • std/mem.zig:3760

      /// Force an evaluation of the expression; this tries to prevent
      /// the compiler from optimizing the computation away even if the
      /// result eventually gets discarded.
      // TODO: use @declareSideEffect() when it is available - https://github.com/ziglang/zig/issues/6168
      pub fn doNotOptimizeAway(val: anytype) void {

      See: https://ziglang.org/documentation/master/std/#root;mem.doNotOptimizeAway.

    • std/coff.zig:354

      /// This relocation is meaningful only when the machine type is ARM or Thumb.
      /// The base relocation applies the 32-bit address of a symbol across a consecutive MOVW/MOVT instruction pair.
      // ARM_MOV32 = 5,
      
      /// This relocation is only meaningful when the machine type is RISC-V.
      /// The base relocation applies to the high 20 bits of a 32-bit absolute address.
      // RISCV_HIGH20 = 5,
      
      /// Reserved, must be zero.
      RESERVED = 6,

      See https://ziglang.org/documentation/master/std/#root;coff.BaseRelocationType.

  • New addrspace keyword.

    Commit: ziglang/zig@ccc7f9987 (Address spaces: addrspace(A) parsing)
    Date: 2021-09-14

    test {
        const y: *allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
        _ = y;
    }
    
  • Inline switch prong not supported by grammar.y.

    Commit: ziglang/zig@b4d81857f (stage1+2: parse inline switch cases)
    Date: 2022-02-13

    const std = @import("std");
    const expect = std.testing.expect;
    
    const SliceTypeA = extern struct {
        len: usize,
        ptr: [*]u32,
    };
    const SliceTypeB = extern struct {
        ptr: [*]SliceTypeA,
        len: usize,
    };
    const AnySlice = union(enum) {
        a: SliceTypeA,
        b: SliceTypeB,
        c: []const u8,
        d: []AnySlice,
    };
    
    fn withSwitch(any: AnySlice) usize {
        return switch (any) {
            // With `inline else` the function is explicitly generated
            // as the desired switch and the compiler can check that
            // every possible case is handled.
            inline else => |slice| slice.len,
        };
    }
    
    test "inline else" {
        var any = AnySlice{ .c = "hello" };
        try expect(withSwitch(any) == 5);
    }
  • New packed struct syntax.

    Commit: ziglang/zig@6249a24e8 (stage2: integer-backed packed structs)
    Date: 2022-02-23

    pub const AbsolutePointerModeAttributes = packed struct(u32) {
        supports_alt_active: bool,
        supports_pressure_as_z: bool,
        _pad: u30 = 0,
    };

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions