// 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 = @{ alpha+ ~ ":" }
op = { alpha+ }
data = { num+ }
idata = { hex+ | num+ }
immediate = _{ "#" ~ ( "b" | "B" | "x" | "X" )? ~ idata }
operand = { immediate | address | alpha+ }

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* ~ (alphanum+ ~ space*)* ~ space* }

space = _{ " " | "\t" }
newline = _{ "\r\n" | "\n" }
whitespace = _{ (space | newline) }
num = _{ ('0'..'9') }
alpha = _{ ('a'..'z' | 'A'..'Z' ) }
alphanum = _{ alpha | num }
hex = _{ ('0'..'9') | ('A'..'F') | ('a'..'f') }
