// Copyright (c) 2021 Saadi Save
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

address = { num+ }
label = @{ ( !":" ~ unicode )+ ~ ":" }
op = { alpha+ }
data = { num+ }
// idata = { hex+ | num+ }
immediate = _{ "#" ~ ( num+ | ("b" | "B") ~ bin+ | ("x" | "X") ~ hex+ ) }
operand = @{ immediate | address | !"//" ~ !"#" ~ unicode+ }

instruction = ${ ( address | label )? ~ space+? ~ op ~ space+? ~ operand? ~ space+? ~ comment? ~ NEWLINE }
prog = ${ whitespace* ~ instruction+ ~ whitespace* }

memoryentry = ${ ( address | label ) ~ space+? ~ data? ~ space+? ~ comment? ~ NEWLINE }
memory = ${ whitespace* ~ memoryentry+ ~ whitespace* }

comment = _{ "//" ~ space* ~ ( unicode+ ~ space* )* ~ space* }

space = _{ " " | "\t" }
whitespace = _{ space | NEWLINE }
num = _{ ASCII_DIGIT }
alpha = _{ ASCII_ALPHA }
// alphanum = _{ ASCII_ALPHANUMERIC }
hex = _{ ASCII_HEX_DIGIT }
bin = _{ ASCII_BIN_DIGIT }
unicode = _{ !space ~ !NEWLINE ~ ANY }
