-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.rs
69 lines (59 loc) · 1.91 KB
/
data.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use nom::{
branch::alt,
combinator::map,
error::{ErrorKind, FromExternalError},
};
use nom_supreme::{error::ErrorTree, tag::complete::tag, ParserExt};
use serde::Serialize;
use super::{
instruction::RefToken,
shared::{lexeme, parse_number, parse_placeholder, parse_symbol_reference, AsmResult},
};
#[derive(Debug, Serialize)]
pub enum DataType {
Value(u32),
SymbolRef(RefToken),
PlaceHolder(String),
}
pub fn parse_data_type(i: &str) -> AsmResult<DataType> {
alt((
// TODO: Make sure that numbers are defined consistently in SIRC Asm
// category=Toolchain
// There should probably be a hash before the number here. (Also check .ORG)
map(parse_number, DataType::Value).context("number"),
map(parse_symbol_reference, |ref_token| {
DataType::SymbolRef(ref_token)
})
.context("symbol reference"),
))(i)
}
pub fn parse_data_(i: &str) -> AsmResult<(u8, DataType)> {
let (i, tag) = lexeme(alt((tag(".DB"), tag(".DW"), tag(".DQ"))))(i)?;
let (i, value) = parse_data_type(i)?;
let size = match tag {
".DB" => Ok(1),
".DW" => Ok(2),
".DQ" => Ok(4),
_ => {
let error_string = format!("Only DB (byte), DW (word) or DQ (quad) data directives are supported. Got [{tag}] ");
Err(nom::Err::Error(ErrorTree::from_external_error(
i,
ErrorKind::Fail,
error_string.as_str(),
)))
}
}?;
Ok((i, (size, value)))
}
pub fn parse_data(i: &str) -> AsmResult<(u8, DataType)> {
lexeme(parse_data_)(i)
}
pub fn parse_equ_(i: &str) -> AsmResult<(String, u32)> {
let (i, _) = lexeme(tag(".EQU"))(i)?;
let (i, placeholder_name) = parse_placeholder(i)?;
let (i, value) = parse_number(i)?;
Ok((i, (placeholder_name, value)))
}
pub fn parse_equ(i: &str) -> AsmResult<(String, u32)> {
lexeme(parse_equ_)(i)
}