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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::parser::{Parse, Parser, Result};
use crate::token::{self, Span};
use crate::{annotation, kw};
#[derive(Debug)]
pub struct Custom<'a> {
pub span: Span,
pub name: &'a str,
pub place: CustomPlace,
pub data: Vec<&'a [u8]>,
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum CustomPlace {
BeforeFirst,
Before(CustomPlaceAnchor),
After(CustomPlaceAnchor),
AfterLast,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[allow(missing_docs)]
pub enum CustomPlaceAnchor {
Type,
Import,
Func,
Table,
Memory,
Global,
Export,
Start,
Elem,
Code,
Data,
Tag,
}
impl<'a> Parse<'a> for Custom<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let span = parser.parse::<annotation::custom>()?.0;
let name = parser.parse()?;
let place = if parser.peek::<token::LParen>() {
parser.parens(|p| p.parse())?
} else {
CustomPlace::AfterLast
};
let mut data = Vec::new();
while !parser.is_empty() {
data.push(parser.parse()?);
}
Ok(Custom {
span,
name,
place,
data,
})
}
}
impl<'a> Parse<'a> for CustomPlace {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut l = parser.lookahead1();
let ctor = if l.peek::<kw::before>() {
parser.parse::<kw::before>()?;
if l.peek::<kw::first>() {
parser.parse::<kw::first>()?;
return Ok(CustomPlace::BeforeFirst);
}
CustomPlace::Before as fn(CustomPlaceAnchor) -> _
} else if l.peek::<kw::after>() {
parser.parse::<kw::after>()?;
if l.peek::<kw::last>() {
parser.parse::<kw::last>()?;
return Ok(CustomPlace::AfterLast);
}
CustomPlace::After
} else {
return Err(l.error());
};
Ok(ctor(parser.parse()?))
}
}
impl<'a> Parse<'a> for CustomPlaceAnchor {
fn parse(parser: Parser<'a>) -> Result<Self> {
if parser.peek::<kw::r#type>() {
parser.parse::<kw::r#type>()?;
return Ok(CustomPlaceAnchor::Type);
}
if parser.peek::<kw::import>() {
parser.parse::<kw::import>()?;
return Ok(CustomPlaceAnchor::Import);
}
if parser.peek::<kw::func>() {
parser.parse::<kw::func>()?;
return Ok(CustomPlaceAnchor::Func);
}
if parser.peek::<kw::table>() {
parser.parse::<kw::table>()?;
return Ok(CustomPlaceAnchor::Table);
}
if parser.peek::<kw::memory>() {
parser.parse::<kw::memory>()?;
return Ok(CustomPlaceAnchor::Memory);
}
if parser.peek::<kw::global>() {
parser.parse::<kw::global>()?;
return Ok(CustomPlaceAnchor::Global);
}
if parser.peek::<kw::export>() {
parser.parse::<kw::export>()?;
return Ok(CustomPlaceAnchor::Export);
}
if parser.peek::<kw::start>() {
parser.parse::<kw::start>()?;
return Ok(CustomPlaceAnchor::Start);
}
if parser.peek::<kw::elem>() {
parser.parse::<kw::elem>()?;
return Ok(CustomPlaceAnchor::Elem);
}
if parser.peek::<kw::code>() {
parser.parse::<kw::code>()?;
return Ok(CustomPlaceAnchor::Code);
}
if parser.peek::<kw::data>() {
parser.parse::<kw::data>()?;
return Ok(CustomPlaceAnchor::Data);
}
if parser.peek::<kw::tag>() {
parser.parse::<kw::tag>()?;
return Ok(CustomPlaceAnchor::Tag);
}
Err(parser.error("expected a valid section name"))
}
}