-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.rs
More file actions
54 lines (46 loc) · 1.38 KB
/
common.rs
File metadata and controls
54 lines (46 loc) · 1.38 KB
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
use chumsky::prelude::*;
use crate::error::parse_error::PError;
use crate::lexer::lr::TokenKind;
use crate::parser::pr::{Annotation, Expr, ExprKind, Stmt, StmtKind, Ty, TyKind};
use crate::span::Span;
pub fn ident_part() -> impl Parser<TokenKind, String, Error = PError> + Clone {
return select! {
TokenKind::Ident(ident) => ident,
TokenKind::Keyword(ident) if &ident == "module" => ident,
}
.map_err(|e: PError| {
PError::expected_input_found(
e.span(),
[Some(TokenKind::Ident("".to_string()))],
e.found().cloned(),
)
});
}
pub fn keyword(kw: &'static str) -> impl Parser<TokenKind, (), Error = PError> + Clone {
just(TokenKind::Keyword(kw.to_string())).ignored()
}
pub fn new_line() -> impl Parser<TokenKind, (), Error = PError> + Clone {
just(TokenKind::NewLine).ignored()
}
pub fn ctrl(char: char) -> impl Parser<TokenKind, (), Error = PError> + Clone {
just(TokenKind::Control(char)).ignored()
}
pub fn into_stmt((annotations, kind): (Vec<Annotation>, StmtKind), span: Span) -> Stmt {
Stmt {
kind,
span: Some(span),
annotations,
}
}
pub fn into_expr(kind: ExprKind, span: Span) -> Expr {
Expr {
span: Some(span),
..Expr::new(kind)
}
}
pub fn into_ty(kind: TyKind, span: Span) -> Ty {
Ty {
span: Some(span),
..Ty::new(kind)
}
}