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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
pub mod diagnostics;
mod doc;
pub mod lexer;
pub mod pt;
pub use diagnostics::Diagnostic;
#[allow(clippy::all)]
pub mod solidity {
include!(concat!(env!("OUT_DIR"), "/solidity.rs"));
}
use lalrpop_util::ParseError;
pub fn parse(src: &str, file_no: usize) -> Result<pt::SourceUnit, Vec<Diagnostic>> {
let lex = lexer::Lexer::new(src);
let s = solidity::SourceUnitParser::new().parse(src, file_no, lex);
if let Err(e) = s {
let errors = vec![match e {
ParseError::InvalidToken { location } => Diagnostic::parser_error(
pt::Loc(file_no, location, location),
"invalid token".to_string(),
),
ParseError::UnrecognizedToken {
token: (l, token, r),
expected,
} => Diagnostic::parser_error(
pt::Loc(file_no, l, r),
format!(
"unrecognised token `{}', expected {}",
token,
expected.join(", ")
),
),
ParseError::User { error } => {
Diagnostic::parser_error(error.loc(file_no), error.to_string())
}
ParseError::ExtraToken { token } => Diagnostic::parser_error(
pt::Loc(file_no, token.0, token.2),
format!("extra token `{}' encountered", token.0),
),
ParseError::UnrecognizedEOF { location, expected } => Diagnostic::parser_error(
pt::Loc(file_no, location, location),
format!("unexpected end of file, expecting {}", expected.join(", ")),
),
}];
Err(errors)
} else {
Ok(s.unwrap())
}
}
pub fn box_option<T>(o: Option<T>) -> Option<Box<T>> {
o.map(Box::new)
}
#[cfg(test)]
mod test {
use super::lexer;
use super::pt::*;
use super::solidity;
#[test]
fn parse_test() {
let src = "contract foo {
struct Jurisdiction {
bool exists;
uint keyIdx;
bytes2 country;
bytes32 region;
}
string __abba_$;
int64 $thing_102;
}";
let lex = lexer::Lexer::new(src);
let e = solidity::SourceUnitParser::new()
.parse(src, 0, lex)
.unwrap();
let a = SourceUnit(vec![SourceUnitPart::ContractDefinition(Box::new(
ContractDefinition {
doc: vec![],
loc: Loc(0, 0, 13),
ty: ContractTy::Contract(Loc(0, 0, 8)),
name: Identifier {
loc: Loc(0, 9, 12),
name: "foo".to_string(),
},
base: Vec::new(),
parts: vec![
ContractPart::StructDefinition(Box::new(StructDefinition {
doc: vec![],
name: Identifier {
loc: Loc(0, 42, 54),
name: "Jurisdiction".to_string(),
},
loc: Loc(0, 35, 232),
fields: vec![
VariableDeclaration {
loc: Loc(0, 81, 92),
ty: Expression::Type(Loc(0, 81, 85), Type::Bool),
storage: None,
name: Identifier {
loc: Loc(0, 86, 92),
name: "exists".to_string(),
},
},
VariableDeclaration {
loc: Loc(0, 118, 129),
ty: Expression::Type(Loc(0, 118, 122), Type::Uint(256)),
storage: None,
name: Identifier {
loc: Loc(0, 123, 129),
name: "keyIdx".to_string(),
},
},
VariableDeclaration {
loc: Loc(0, 155, 169),
ty: Expression::Type(Loc(0, 155, 161), Type::Bytes(2)),
storage: None,
name: Identifier {
loc: Loc(0, 162, 169),
name: "country".to_string(),
},
},
VariableDeclaration {
loc: Loc(0, 195, 209),
ty: Expression::Type(Loc(0, 195, 202), Type::Bytes(32)),
storage: None,
name: Identifier {
loc: Loc(0, 203, 209),
name: "region".to_string(),
},
},
],
})),
ContractPart::VariableDefinition(Box::new(VariableDefinition {
doc: vec![],
ty: Expression::Type(Loc(0, 253, 259), Type::String),
attrs: vec![],
name: Identifier {
loc: Loc(0, 260, 268),
name: "__abba_$".to_string(),
},
loc: Loc(0, 253, 268),
initializer: None,
})),
ContractPart::VariableDefinition(Box::new(VariableDefinition {
doc: vec![],
ty: Expression::Type(Loc(0, 290, 295), Type::Int(64)),
attrs: vec![],
name: Identifier {
loc: Loc(0, 296, 306),
name: "$thing_102".to_string(),
},
loc: Loc(0, 290, 306),
initializer: None,
})),
],
},
))]);
assert_eq!(e, a);
}
}