module.exports = grammar({
    name: "compare_output",

    rules: {
        source_file: ($) => repeat($._item),

        _item: ($) =>
            choice($.table_row, $.header_row, $.separator_row, $._whitespace),

        // Header row with column names
        header_row: ($) => seq("|", repeat1($.header_cell), "|"),

        // Data row with values
        table_row: ($) => seq("|", repeat1($.data_cell), "|"),

        // Separator row (optional, sometimes used)
        separator_row: ($) => seq("|", repeat1(seq(/[-=]+/, "|"))),

        // Header cell (column name)
        header_cell: ($) => seq(/\s*/, $.column_name, /\s*/, "|"),

        // Data cell (value)
        data_cell: ($) =>
            seq(
                /\s*/,
                optional(
                    choice(
                        $.binary_value,
                        $.decimal_value,
                        $.register_reference,
                    ),
                ),
                /\s*/,
                "|",
            ),

        // Column names in headers
        column_name: ($) => choice($.register_name, $.pin_name, $.generic_name),

        // Register references like RAM[0], RAM[256]
        register_name: ($) => /RAM\[\d+\]/,

        // Pin names like 'a', 'b', 'out', 'in'
        pin_name: ($) => /[a-zA-Z][a-zA-Z0-9_]*/,

        // Generic column names
        generic_name: ($) => /[a-zA-Z][a-zA-Z0-9_\[\]]*/,

        // Values in data cells
        binary_value: ($) => /[01]+/,
        decimal_value: ($) => /\d+/,
        register_reference: ($) => /[A-Z][a-zA-Z0-9_\[\]]*/,

        // Whitespace
        _whitespace: ($) => /\s+/,
    },

    extras: ($) => [/[ \t]/],
});
