Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 4c4ae27

Browse files
MouradifMaddiaa0
andauthored
feat: show cursor on parser error (#313)
* feat: show cursor on parser error fix #312 * fix: merge * chore: fmt --------- Co-authored-by: Maddiaa0 <[email protected]>
1 parent c944050 commit 4c4ae27

File tree

4 files changed

+89
-26
lines changed

4 files changed

+89
-26
lines changed

Diff for: huff_core/tests/parser_errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fn test_invalid_macro_statement() {
4242
kind: ParserErrorKind::InvalidTokenInMacroBody(TokenKind::FreeStoragePointer),
4343
hint: None,
4444
spans: AstSpan(vec![Span { start: const_start, end: const_end, file: None }]),
45+
cursor: 36,
4546
}
4647
)
4748
}
@@ -72,6 +73,7 @@ fn test_unexpected_type() {
7273
end: source.find("internal").unwrap_or(0) + "internal".len() - 1,
7374
file: None
7475
}]),
76+
cursor: 5,
7577
}
7678
)
7779
}
@@ -103,6 +105,7 @@ fn test_invalid_definition() {
103105
end: source.find("invalid").unwrap_or(0) + "invalid".len() - 1,
104106
file: None
105107
}]),
108+
cursor: 1,
106109
}
107110
)
108111
}
@@ -147,6 +150,7 @@ fn test_invalid_constant_value() {
147150
end: source.find(value).unwrap_or(0) + value.len() - 1,
148151
file: None
149152
}]),
153+
cursor: 4
150154
}
151155
)
152156
}
@@ -191,6 +195,7 @@ fn test_invalid_token_in_macro_body() {
191195
end: source.rfind(value).unwrap_or(0) + value.len() - 1,
192196
file: None
193197
}]),
198+
cursor: 15,
194199
}
195200
)
196201
}
@@ -236,6 +241,7 @@ fn test_invalid_token_in_label_definition() {
236241
end: source.rfind(value).unwrap_or(0) + value.len() - 1,
237242
file: None
238243
}]),
244+
cursor: 17,
239245
}
240246
)
241247
}
@@ -279,6 +285,7 @@ fn test_invalid_single_arg() {
279285
))),
280286
hint: Some("Expected number representing stack item count.".to_string()),
281287
spans: AstSpan(vec![Span { start: 34, end: 34, file: None }]),
288+
cursor: 8,
282289
}
283290
)
284291
}

Diff for: huff_parser/src/lib.rs

+37-5
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ impl Parser {
120120
kind: ParserErrorKind::InvalidDefinition(self.current_token.kind.clone()),
121121
hint: Some("Definition must be one of: `function`, `event`, `constant`, `error`, `macro`, `fn`, or `test`.".to_string()),
122122
spans: AstSpan(vec![self.current_token.span.clone()]),
123+
cursor: self.cursor,
123124
});
124125
}
125126
};
@@ -133,6 +134,7 @@ impl Parser {
133134
TokenKind::Include
134135
)),
135136
spans: AstSpan(self.spans.clone()),
137+
cursor: self.cursor,
136138
});
137139
}
138140
}
@@ -158,6 +160,7 @@ impl Parser {
158160
kind: ParserErrorKind::InvalidName(tok.clone()),
159161
hint: Some(format!("Expected import string. Got: \"{tok}\"")),
160162
spans: AstSpan(new_spans),
163+
cursor: self.cursor,
161164
});
162165
}
163166
};
@@ -177,6 +180,7 @@ impl Parser {
177180
kind: ParserErrorKind::UnexpectedType(self.current_token.kind.clone()),
178181
hint: Some(format!("Expected: \"{kind}\"")),
179182
spans: AstSpan(self.spans.clone()),
183+
cursor: self.cursor,
180184
})
181185
}
182186
}
@@ -198,6 +202,7 @@ impl Parser {
198202
kind: ParserErrorKind::DuplicateMacro(m.name.to_owned()),
199203
hint: Some("MACRO names should be unique".to_string()),
200204
spans: AstSpan(vec![m.span[2].clone()]),
205+
cursor: self.cursor,
201206
})
202207
} else {
203208
Ok(())
@@ -257,6 +262,7 @@ impl Parser {
257262
kind: ParserErrorKind::InvalidName(tok.clone()),
258263
hint: Some(format!("Expected function name, found: \"{tok}\"")),
259264
spans: AstSpan(self.spans.clone()),
265+
cursor: self.cursor,
260266
});
261267
}
262268
};
@@ -276,6 +282,7 @@ impl Parser {
276282
"Expected one of: `view`, `pure`, `payable`, `nonpayable`.".to_string(),
277283
),
278284
spans: AstSpan(vec![self.current_token.span.clone()]),
285+
cursor: self.cursor,
279286
})
280287
}
281288
};
@@ -319,6 +326,7 @@ impl Parser {
319326
kind: ParserErrorKind::InvalidName(tok.clone()),
320327
hint: Some(format!("Expected event name, found: \"{tok}\"")),
321328
spans: AstSpan(self.spans.clone()),
329+
cursor: self.cursor,
322330
});
323331
}
324332
};
@@ -350,6 +358,7 @@ impl Parser {
350358
kind: ParserErrorKind::UnexpectedType(tok),
351359
hint: Some("Expected constant name.".to_string()),
352360
spans: AstSpan(self.spans.clone()),
361+
cursor: self.cursor,
353362
});
354363
}
355364
};
@@ -375,6 +384,7 @@ impl Parser {
375384
.to_string(),
376385
),
377386
spans: AstSpan(vec![self.current_token.span.clone()]),
387+
cursor: self.cursor,
378388
});
379389
}
380390
};
@@ -403,6 +413,7 @@ impl Parser {
403413
kind: ParserErrorKind::UnexpectedType(tok),
404414
hint: Some("Expected error name.".to_string()),
405415
spans: AstSpan(self.spans.clone()),
416+
cursor: self.cursor,
406417
});
407418
}
408419
};
@@ -450,6 +461,7 @@ impl Parser {
450461
),
451462
hint: Some(format!("Expected string for decorator flag: {s}")),
452463
spans: AstSpan(vec![self.current_token.span.clone()]),
464+
cursor: self.cursor,
453465
});
454466
}
455467
}
@@ -466,6 +478,7 @@ impl Parser {
466478
),
467479
hint: Some(format!("Expected literal for decorator flag: {s}")),
468480
spans: AstSpan(vec![self.current_token.span.clone()]),
481+
cursor: self.cursor,
469482
});
470483
}
471484
}
@@ -475,6 +488,7 @@ impl Parser {
475488
kind: ParserErrorKind::InvalidDecoratorFlag(s.clone()),
476489
hint: Some(format!("Unknown decorator flag: {s}")),
477490
spans: AstSpan(self.spans.clone()),
491+
cursor: self.cursor,
478492
});
479493
}
480494
}
@@ -493,6 +507,7 @@ impl Parser {
493507
)),
494508
hint: Some(String::from("Unknown decorator flag")),
495509
spans: AstSpan(self.spans.clone()),
510+
cursor: self.cursor,
496511
});
497512
}
498513
}
@@ -597,6 +612,7 @@ impl Parser {
597612
"Literal {hex_literal:?} contains too many bytes for opcode \"{o:?}\""
598613
)),
599614
spans: AstSpan(curr_spans),
615+
cursor: self.cursor,
600616
});
601617
}
602618

@@ -614,6 +630,7 @@ impl Parser {
614630
o, self.current_token.kind
615631
)),
616632
spans: AstSpan(vec![self.current_token.span.clone()]),
633+
cursor: self.cursor,
617634
})
618635
}
619636
}
@@ -704,6 +721,7 @@ impl Parser {
704721
kind: ParserErrorKind::InvalidTokenInMacroBody(kind),
705722
hint: None,
706723
spans: AstSpan(vec![self.current_token.span.clone()]),
724+
cursor: self.cursor,
707725
});
708726
}
709727
};
@@ -817,6 +835,7 @@ impl Parser {
817835
kind: ParserErrorKind::InvalidTokenInLabelDefinition(kind),
818836
hint: None,
819837
spans: AstSpan(vec![self.current_token.span.clone()]),
838+
cursor: self.cursor,
820839
});
821840
}
822841
};
@@ -948,6 +967,7 @@ impl Parser {
948967
"Argument names cannot be EVM types: {arg_str}"
949968
)),
950969
spans: AstSpan(vec![self.current_token.span.clone()]),
970+
cursor: self.cursor,
951971
});
952972
}
953973
}
@@ -958,6 +978,7 @@ impl Parser {
958978
),
959979
hint: Some(format!("Argument names cannot be EVM types: {ty}")),
960980
spans: AstSpan(vec![self.current_token.span.clone()]),
981+
cursor: self.cursor,
961982
})
962983
}
963984
_ => { /* continue, valid string */ }
@@ -981,6 +1002,7 @@ impl Parser {
9811002
kind: ParserErrorKind::InvalidArgs(self.current_token.kind.clone()),
9821003
hint: None,
9831004
spans: AstSpan(vec![self.current_token.span.clone()]),
1005+
cursor: self.cursor,
9841006
});
9851007
}
9861008

@@ -1004,6 +1026,7 @@ impl Parser {
10041026
kind: ParserErrorKind::InvalidSingleArg(self.current_token.kind.clone()),
10051027
hint: Some("Expected number representing stack item count.".to_string()),
10061028
spans: AstSpan(single_arg_span),
1029+
cursor: self.cursor,
10071030
})
10081031
}
10091032
};
@@ -1059,6 +1082,7 @@ impl Parser {
10591082
.to_string(),
10601083
),
10611084
spans: AstSpan(new_spans),
1085+
cursor: self.cursor,
10621086
});
10631087
}
10641088
}
@@ -1147,7 +1171,8 @@ impl Parser {
11471171
),
11481172
hint: Some("Expected valid hex bytecode.".to_string()),
11491173
spans: AstSpan(new_spans),
1150-
});
1174+
cursor: self.cursor,
1175+
})
11511176
}
11521177
} else {
11531178
StatementType::LabelCall(ident_str.to_string())
@@ -1162,7 +1187,8 @@ impl Parser {
11621187
kind: ParserErrorKind::InvalidTableBodyToken(kind.clone()),
11631188
hint: Some("Expected an identifier string.".to_string()),
11641189
spans: AstSpan(new_spans),
1165-
});
1190+
cursor: self.cursor,
1191+
})
11661192
}
11671193
};
11681194
}
@@ -1189,6 +1215,7 @@ impl Parser {
11891215
kind: ParserErrorKind::InvalidConstant(kind),
11901216
hint: None,
11911217
spans: AstSpan(new_spans),
1218+
cursor: self.cursor,
11921219
})
11931220
}
11941221
}
@@ -1221,6 +1248,7 @@ impl Parser {
12211248
kind: ParserErrorKind::InvalidArgCallIdent(kind),
12221249
hint: None,
12231250
spans: AstSpan(new_spans),
1251+
cursor: self.cursor,
12241252
})
12251253
}
12261254
}
@@ -1250,6 +1278,7 @@ impl Parser {
12501278
kind: ParserErrorKind::InvalidArgs(kind),
12511279
hint: None,
12521280
spans: AstSpan(vec![self.current_token.span.clone()]),
1281+
cursor: self.cursor,
12531282
}),
12541283
}
12551284
}
@@ -1267,7 +1296,8 @@ impl Parser {
12671296
kind: ParserErrorKind::InvalidUint256(size),
12681297
hint: None,
12691298
spans: AstSpan(vec![self.current_token.span.clone()]),
1270-
});
1299+
cursor: self.cursor,
1300+
})
12711301
}
12721302
Ok(self.match_kind(self.current_token.kind.clone())?)
12731303
}
@@ -1277,7 +1307,8 @@ impl Parser {
12771307
kind: ParserErrorKind::InvalidBytes(size),
12781308
hint: None,
12791309
spans: AstSpan(vec![self.current_token.span.clone()]),
1280-
});
1310+
cursor: self.cursor,
1311+
})
12811312
}
12821313
Ok(self.match_kind(self.current_token.kind.clone())?)
12831314
}
@@ -1291,7 +1322,8 @@ impl Parser {
12911322
kind: ParserErrorKind::InvalidInt(size),
12921323
hint: None,
12931324
spans: AstSpan(vec![self.current_token.span.clone()]),
1294-
});
1325+
cursor: self.cursor,
1326+
})
12951327
}
12961328
let curr_token_kind = self.current_token.kind.clone();
12971329
self.consume();

Diff for: huff_parser/tests/macro.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,7 @@ fn test_duplicate_macro_error() {
12911291
end: occurrences[1].1,
12921292
file: None
12931293
}]),
1294+
cursor: 58,
12941295
}
12951296
)
12961297
}

0 commit comments

Comments
 (0)