Skip to content

Commit 5a928a3

Browse files
authored
Improve parser (#1237)
* chore: rename test to tests * docs: add docs and comments to doccomment * reduce allocations in doccomment * add doccomment tests * docs: add comments and docs to diagnostics * reduce allocations in parse * use ThisError for LexicalError, change result type * docs: document everything * feat: add helpers mod, impl CodeLocation for PT * fix: don't use ref * feat: implement Display for all PT structs * feat: implement Display for all PT enums * feat: add Ord implementations * feat: implement CodeLocation for smart pointers * feat: add more helpers * feat: finish pt::Expression impl * fix: make helpers module public * fix: use enum discriminant for sorting Signed-off-by: DaniPopes <[email protected]>
1 parent 242c187 commit 5a928a3

24 files changed

+4999
-934
lines changed

solang-parser/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ lalrpop-util = "0.19"
1919
phf = { version = "0.11", features = ["macros"] }
2020
unicode-xid = "0.2"
2121
itertools = "0.10"
22+
thiserror = "1.0"
23+
2224
serde = { version = "1.0", features = ["derive"], optional = true }
2325

2426
[dev-dependencies]

solang-parser/src/diagnostics.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3+
//! Solidity parser diagnostics.
4+
35
use crate::pt;
46
use crate::pt::Loc;
7+
use std::fmt;
58

9+
/// The level of a diagnostic.
610
#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
711
pub enum Level {
12+
/// Debug diagnostic level.
813
Debug,
14+
/// Info diagnostic level.
915
Info,
16+
/// Warning diagnostic level.
1017
Warning,
18+
/// Error diagnostic level.
1119
Error,
1220
}
1321

22+
impl fmt::Display for Level {
23+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24+
f.write_str(self.as_str())
25+
}
26+
}
27+
1428
impl Level {
15-
pub fn to_string(&self) -> &'static str {
29+
/// Returns this type as a static string slice.
30+
pub fn as_str(&self) -> &'static str {
1631
match self {
1732
Level::Debug => "debug",
1833
Level::Info => "info",
@@ -22,33 +37,51 @@ impl Level {
2237
}
2338
}
2439

25-
#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
40+
/// The type of a diagnostic.
41+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
2642
pub enum ErrorType {
43+
/// No specific error type.
2744
None,
45+
/// Parser error.
2846
ParserError,
47+
/// Syntax error.
2948
SyntaxError,
49+
/// Declaration error.
3050
DeclarationError,
51+
/// Cast error.
3152
CastError,
53+
/// Type error.
3254
TypeError,
55+
/// Warning.
3356
Warning,
3457
}
3558

36-
#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
59+
/// A diagnostic note.
60+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
3761
pub struct Note {
62+
/// The code location of the note.
3863
pub loc: pt::Loc,
64+
/// The message of the note.
3965
pub message: String,
4066
}
4167

42-
#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
68+
/// A Solidity diagnostic.
69+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
4370
pub struct Diagnostic {
71+
/// The code location of the diagnostic.
4472
pub loc: pt::Loc,
73+
/// The level of the diagnostic.
4574
pub level: Level,
75+
/// The type of diagnostic.
4676
pub ty: ErrorType,
77+
/// The message of the diagnostic.
4778
pub message: String,
79+
/// Extra notes about the diagnostic.
4880
pub notes: Vec<Note>,
4981
}
5082

5183
impl Diagnostic {
84+
/// Instantiate a new Diagnostic with the given location and message at the debug level.
5285
pub fn debug(loc: Loc, message: String) -> Self {
5386
Diagnostic {
5487
level: Level::Debug,
@@ -59,6 +92,7 @@ impl Diagnostic {
5992
}
6093
}
6194

95+
/// Instantiate a new Diagnostic with the given location and message at the info level.
6296
pub fn info(loc: Loc, message: String) -> Self {
6397
Diagnostic {
6498
level: Level::Info,
@@ -69,6 +103,7 @@ impl Diagnostic {
69103
}
70104
}
71105

106+
/// Instantiate a new parser error Diagnostic.
72107
pub fn parser_error(loc: Loc, message: String) -> Self {
73108
Diagnostic {
74109
level: Level::Error,
@@ -79,6 +114,7 @@ impl Diagnostic {
79114
}
80115
}
81116

117+
/// Instantiate a new syntax error Diagnostic.
82118
pub fn error(loc: Loc, message: String) -> Self {
83119
Diagnostic {
84120
level: Level::Error,
@@ -89,6 +125,7 @@ impl Diagnostic {
89125
}
90126
}
91127

128+
/// Instantiate a new declaration error Diagnostic.
92129
pub fn decl_error(loc: Loc, message: String) -> Self {
93130
Diagnostic {
94131
level: Level::Error,
@@ -99,6 +136,7 @@ impl Diagnostic {
99136
}
100137
}
101138

139+
/// Instantiate a new cast error error Diagnostic.
102140
pub fn cast_error(loc: Loc, message: String) -> Self {
103141
Diagnostic {
104142
level: Level::Error,
@@ -109,6 +147,7 @@ impl Diagnostic {
109147
}
110148
}
111149

150+
/// Instantiate a new cast error error Diagnostic, with a note.
112151
pub fn cast_error_with_note(loc: Loc, message: String, note_loc: Loc, note: String) -> Self {
113152
Diagnostic {
114153
level: Level::Error,
@@ -122,6 +161,7 @@ impl Diagnostic {
122161
}
123162
}
124163

164+
/// Instantiate a new type error error Diagnostic.
125165
pub fn type_error(loc: Loc, message: String) -> Self {
126166
Diagnostic {
127167
level: Level::Error,
@@ -132,6 +172,7 @@ impl Diagnostic {
132172
}
133173
}
134174

175+
/// Instantiate a new cast error Diagnostic at the warning level.
135176
pub fn cast_warning(loc: Loc, message: String) -> Self {
136177
Diagnostic {
137178
level: Level::Warning,
@@ -142,6 +183,7 @@ impl Diagnostic {
142183
}
143184
}
144185

186+
/// Instantiate a new warning Diagnostic.
145187
pub fn warning(loc: Loc, message: String) -> Self {
146188
Diagnostic {
147189
level: Level::Warning,
@@ -152,6 +194,7 @@ impl Diagnostic {
152194
}
153195
}
154196

197+
/// Instantiate a new warning Diagnostic, with a note.
155198
pub fn warning_with_note(loc: Loc, message: String, note_loc: Loc, note: String) -> Self {
156199
Diagnostic {
157200
level: Level::Warning,
@@ -165,6 +208,7 @@ impl Diagnostic {
165208
}
166209
}
167210

211+
/// Instantiate a new warning Diagnostic, with multiple notes.
168212
pub fn warning_with_notes(loc: Loc, message: String, notes: Vec<Note>) -> Self {
169213
Diagnostic {
170214
level: Level::Warning,
@@ -175,6 +219,7 @@ impl Diagnostic {
175219
}
176220
}
177221

222+
/// Instantiate a new error Diagnostic, with a note.
178223
pub fn error_with_note(loc: Loc, message: String, note_loc: Loc, note: String) -> Self {
179224
Diagnostic {
180225
level: Level::Error,
@@ -188,6 +233,7 @@ impl Diagnostic {
188233
}
189234
}
190235

236+
/// Instantiate a new error Diagnostic, with multiple notes.
191237
pub fn error_with_notes(loc: Loc, message: String, notes: Vec<Note>) -> Self {
192238
Diagnostic {
193239
level: Level::Error,

0 commit comments

Comments
 (0)