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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use wast::parser::{Cursor, Parse, Parser, Peek, Result};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Integer<'a> {
sign: Option<Sign>,
src: &'a str,
val: &'a str,
hex: bool,
}
impl<'a> Integer<'a> {
pub fn new(
sign: Option<Sign>,
src: &'a str,
val: &'a str,
hex: bool,
) -> Self {
Self {
sign,
src,
val,
hex,
}
}
pub fn sign(&self) -> Option<Sign> {
self.sign
}
pub fn src(&self) -> &'a str {
self.src
}
pub fn val(&self) -> (&str, u32) {
(&self.val, if self.hex { 16 } else { 10 })
}
}
impl<'a> Parse<'a> for Integer<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|cursor| match cursor.integer() {
Some((s, cur)) => {
let src = s.src();
let mut sign = None;
let (val, base) = s.val();
let hex = if base == 16 { true } else { false };
if let Some(si) = s.sign() {
match si {
wast::lexer::SignToken::Plus => sign = Some(Sign::Pos),
wast::lexer::SignToken::Minus => sign = Some(Sign::Neg),
}
}
Ok((
Self {
sign,
src,
val,
hex,
},
cur,
))
}
None => Err(parser.error("could not parse integer")),
})
}
}
impl Peek for Integer<'_> {
fn peek(cursor: Cursor<'_>) -> bool {
cursor.integer().is_some()
}
fn display() -> &'static str {
"integer"
}
}
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
pub enum Sign {
Pos,
Neg,
}