Refactored cargo structure
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "mist-parser"
|
||||
version = "0.1.5"
|
||||
edition = "2024"
|
||||
description = "The Mist programming language parser"
|
||||
license = "Apache-2.0"
|
||||
repository = "https://github.com/mist-go/mist"
|
||||
keywords = ["compiler", "language", "mist"]
|
||||
categories = ["compilers", "parser-implementations", "parsing"]
|
||||
|
||||
[dependencies]
|
||||
pest = "2.8.6"
|
||||
pest_derive = "2.8.6"
|
||||
serde = { version = "1.0.228", features = ["serde_derive"] }
|
||||
serde_json = "1.0.149"
|
||||
@@ -0,0 +1,93 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Expression {
|
||||
Literal(Literal),
|
||||
Path(ExprPath),
|
||||
Statement(Box<Statement>),
|
||||
Fix {
|
||||
initial: Box<Expression>,
|
||||
prefixes: Vec<Prefix>,
|
||||
postfixes: Vec<Postfix>,
|
||||
},
|
||||
Binary {
|
||||
lhs: Box<Expression>,
|
||||
op: String,
|
||||
rhs: Box<Expression>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Literal {
|
||||
String(String),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
Tuple(Vec<Expression>),
|
||||
Array(Vec<Expression>),
|
||||
ArrayRepeat(Box<Expression>, Box<Expression>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Pattern {
|
||||
NamedTuple(Path, Vec<Identifier>),
|
||||
Struct(Path, Vec<Identifier>),
|
||||
Tuple(Vec<Identifier>),
|
||||
Literal(Literal),
|
||||
Path(Path),
|
||||
Id(Identifier),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Postfix {
|
||||
FieldAccess(Identifier, Option<Generics>),
|
||||
Call(Vec<Expression>),
|
||||
MacroCall(String),
|
||||
StructCall(Vec<(Identifier, Expression)>),
|
||||
Assign(String, Box<Expression>),
|
||||
Index(Expression),
|
||||
As(TypeExpr),
|
||||
Increment,
|
||||
Decrement,
|
||||
Try,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Prefix {
|
||||
Ref,
|
||||
RefMut,
|
||||
Deref,
|
||||
New(Option<Generics>),
|
||||
Not,
|
||||
Neg,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ExprPathSegment {
|
||||
pub ident: Identifier,
|
||||
pub generics: Option<Generics>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ExprPath(pub Vec<ExprPathSegment>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Generics(pub Vec<Generic>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Generic {
|
||||
Lifetime(Identifier),
|
||||
Type(TypeExpr),
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
pub fn is_block(&self) -> bool {
|
||||
if let Expression::Statement(stmt) = self {
|
||||
stmt.is_block()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
use serde::Serialize;
|
||||
|
||||
pub mod expr;
|
||||
pub mod statement;
|
||||
pub mod top_level;
|
||||
|
||||
pub use expr::*;
|
||||
pub use statement::*;
|
||||
pub use top_level::*;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Path(pub Vec<Identifier>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Identifier(pub String);
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Default)]
|
||||
pub struct ParamList(pub Vec<VarDecl>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct TypeExpr(pub TypeExprKind, pub Vec<TypePostfix>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum TypePostfix {
|
||||
Ref,
|
||||
RefMut,
|
||||
RefLifetime(Identifier),
|
||||
RefMutLifetime(Identifier),
|
||||
Dyn,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum TypeExprKind {
|
||||
Path(Path),
|
||||
PathParams(Path, Vec<TypeExpr>),
|
||||
Tuple(Vec<TypeExpr>),
|
||||
Lifetime(Identifier),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Spanned<T> {
|
||||
pub line: usize,
|
||||
pub column: usize,
|
||||
pub item: T,
|
||||
}
|
||||
|
||||
impl TypeExpr {
|
||||
pub fn no_px(kind: TypeExprKind) -> Self {
|
||||
Self(kind, Vec::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GenericDecl> for Generic {
|
||||
fn from(value: GenericDecl) -> Self {
|
||||
match value {
|
||||
GenericDecl::Lifetime(life) => Generic::Lifetime(life),
|
||||
GenericDecl::Type(ty, _) => {
|
||||
Generic::Type(TypeExpr(TypeExprKind::Path(Path(vec![ty])), Vec::new()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Spanned<T> {
|
||||
pub fn get_comment(&self) -> String {
|
||||
format!("/* {}:{} */", self.line, self.column)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Default)]
|
||||
pub struct Block(
|
||||
pub Vec<Spanned<Expression>>,
|
||||
pub Option<Spanned<Expression>>,
|
||||
);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Statement {
|
||||
Block(Block),
|
||||
If {
|
||||
initial: StatementBranch,
|
||||
else_if: Vec<StatementBranch>,
|
||||
else_branch: Option<Box<Expression>>,
|
||||
},
|
||||
Loop(Expression),
|
||||
While(StatementBranch),
|
||||
CStyleFor {
|
||||
init: Expression,
|
||||
condition: Expression,
|
||||
update: Expression,
|
||||
body: Expression,
|
||||
},
|
||||
For {
|
||||
mutable: bool,
|
||||
pattern: Pattern,
|
||||
iterator: Expression,
|
||||
body: Box<Expression>,
|
||||
},
|
||||
Match(Expression, Vec<(Vec<Pattern>, Expression)>),
|
||||
|
||||
VarDecl(VarDeclStmt),
|
||||
Return(Option<Expression>),
|
||||
Break,
|
||||
Continue,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct VarDecl {
|
||||
pub mutable: bool,
|
||||
pub name: Pattern,
|
||||
pub type_: Option<TypeExpr>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct VarDeclStmt {
|
||||
pub decl: VarDecl,
|
||||
pub init: Option<Expression>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct StatementBranch {
|
||||
pub condition: Expression,
|
||||
pub body: Box<Expression>,
|
||||
}
|
||||
|
||||
impl Statement {
|
||||
pub fn is_block(&self) -> bool {
|
||||
match self {
|
||||
Self::Block(_) => true,
|
||||
Self::If {
|
||||
initial,
|
||||
else_if,
|
||||
else_branch,
|
||||
} => {
|
||||
else_branch
|
||||
.as_ref()
|
||||
.map(|v| v.is_block())
|
||||
.unwrap_or_default()
|
||||
|| else_if
|
||||
.last()
|
||||
.map(|b| b.body.is_block())
|
||||
.unwrap_or_default()
|
||||
|| initial.body.is_block()
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Visibility {
|
||||
Public,
|
||||
PublicTarget(Path),
|
||||
Private,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Attribute {
|
||||
/// #[test]
|
||||
Path(Path),
|
||||
|
||||
/// #[name = "value"]
|
||||
NameValue { path: Path, value: Literal },
|
||||
|
||||
/// #[derive(Clone, Copy)]
|
||||
List { path: Path, items: Vec<Attribute> },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct TopLevel(pub Spanned<TopLevelKind>, pub Vec<Attribute>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum TopLevelKind {
|
||||
ModAttribute,
|
||||
Import(Visibility, Path),
|
||||
Mod(Visibility, Identifier),
|
||||
ImplDecl(ImplDecl),
|
||||
TraitDecl {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
generics: GenericsDecl,
|
||||
requirements: Vec<TypeExpr>,
|
||||
items: Vec<Spanned<FunctionDecl>>,
|
||||
},
|
||||
EnumDecl {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
generics: GenericsDecl,
|
||||
fields: Vec<Spanned<EnumItem>>,
|
||||
},
|
||||
StructDecl {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
generics: GenericsDecl,
|
||||
fields: Vec<Spanned<FieldDecl>>,
|
||||
},
|
||||
FunctionDecl(FunctionDecl),
|
||||
ClassDecl {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
generics: GenericsDecl,
|
||||
fields: Vec<Spanned<FieldDeclStmt>>,
|
||||
constructor: Spanned<ClassConstructor>,
|
||||
items: Vec<ClassItem>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum ClassItem {
|
||||
Method(Spanned<FunctionDecl>),
|
||||
ImplDecl(Spanned<ImplDecl>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Default)]
|
||||
pub struct GenericsDecl(pub Vec<GenericDecl>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum GenericDecl {
|
||||
Lifetime(Identifier),
|
||||
Type(Identifier, Vec<TypeExpr>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum EnumItem {
|
||||
Named(Identifier),
|
||||
Tuple(Identifier, Vec<TypeExpr>),
|
||||
Struct(Identifier, Vec<FieldDecl>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ClassConstructor {
|
||||
pub visibility: Visibility,
|
||||
pub generics: GenericsDecl,
|
||||
pub params: ParamList,
|
||||
pub body: Block,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct FunctionDecl {
|
||||
pub visibility: Visibility,
|
||||
pub name: Identifier,
|
||||
pub generics: GenericsDecl,
|
||||
pub params: ParamList,
|
||||
pub return_type: TypeExpr,
|
||||
pub body: Option<Block>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ImplDecl {
|
||||
pub generics: GenericsDecl,
|
||||
pub target: TypeExpr,
|
||||
pub trait_: Option<TypeExpr>,
|
||||
pub methods: Vec<Spanned<FunctionDecl>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct FieldDecl {
|
||||
pub visibility: Visibility,
|
||||
pub type_: TypeExpr,
|
||||
pub name: Identifier,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct FieldDeclStmt {
|
||||
pub decl: FieldDecl,
|
||||
pub init: Option<Expression>,
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use pest::iterators::Pair;
|
||||
|
||||
use crate::Rule;
|
||||
|
||||
pub type AstResult<'a, T, ET = T> = Result<T, AstError<'a, ET>>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ParseError<'a, T> {
|
||||
PreAst(pest::error::Error<Rule>),
|
||||
Ast(AstError<'a, T>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AstError<'a, T> {
|
||||
pub span: pest::Span<'a>,
|
||||
pub error_code: ErrorCode,
|
||||
pub error_message: String,
|
||||
pub recovered: Option<T>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ErrorCode {
|
||||
InvalidStatement,
|
||||
AstGenBug,
|
||||
}
|
||||
|
||||
impl<T> From<pest::error::Error<Rule>> for ParseError<'_, T> {
|
||||
fn from(value: pest::error::Error<Rule>) -> Self {
|
||||
Self::PreAst(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> From<AstError<'a, T>> for ParseError<'a, T> {
|
||||
fn from(value: AstError<'a, T>) -> Self {
|
||||
Self::Ast(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, F> AstError<'a, F> {
|
||||
pub fn get<T>(self) -> AstError<'a, T> {
|
||||
AstError {
|
||||
span: self.span,
|
||||
error_code: self.error_code,
|
||||
error_message: self.error_message,
|
||||
recovered: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub fn bug_unimplemented<T>(pair: Pair<'a, Rule>) -> AstResult<'a, T, F> {
|
||||
let loc = std::panic::Location::caller();
|
||||
|
||||
Err(Self {
|
||||
span: pair.as_span(),
|
||||
error_code: ErrorCode::AstGenBug,
|
||||
error_message: format!(
|
||||
"Possible bug, unimplemented: {:#?}, at {}:{}",
|
||||
pair.as_rule(),
|
||||
loc.file(),
|
||||
loc.line(),
|
||||
),
|
||||
recovered: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoErr<T, FA, FR> {
|
||||
fn get(self) -> T;
|
||||
fn get_map(self, m: impl Fn(FA) -> FR) -> T;
|
||||
}
|
||||
|
||||
impl<'a, T, TE, TE2> IntoErr<AstResult<'a, T, TE2>, TE, TE2> for AstResult<'a, T, TE> {
|
||||
fn get(self) -> AstResult<'a, T, TE2> {
|
||||
self.map_err(AstError::get)
|
||||
}
|
||||
|
||||
fn get_map(self, m: impl Fn(TE) -> TE2) -> AstResult<'a, T, TE2> {
|
||||
self.map_err(|e| AstError {
|
||||
span: e.span,
|
||||
error_code: e.error_code,
|
||||
error_message: e.error_message,
|
||||
recovered: e.recovered.map(m),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GetLength {
|
||||
fn len(&self) -> usize;
|
||||
}
|
||||
|
||||
impl<T, E> GetLength for Result<Vec<T>, E> {
|
||||
fn len(&self) -> usize {
|
||||
if let Ok(v) = self { v.len() } else { 0 }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn collect_recovered<'a, T: Debug, ET>(
|
||||
pairs: impl Iterator<Item = pest::iterators::Pair<'a, Rule>>,
|
||||
) -> AstResult<'a, Vec<T>, Vec<T>>
|
||||
where
|
||||
T: TryFrom<pest::iterators::Pair<'a, Rule>, Error = AstError<'a, ET>>,
|
||||
{
|
||||
collect_recovered_map(pairs, T::try_from)
|
||||
}
|
||||
|
||||
pub fn collect_recovered_map<'a, T: Debug, F, ET>(
|
||||
pairs: impl Iterator<Item = pest::iterators::Pair<'a, Rule>>,
|
||||
f: F,
|
||||
) -> AstResult<'a, Vec<T>, Vec<T>>
|
||||
where
|
||||
F: Fn(pest::iterators::Pair<'a, Rule>) -> AstResult<'a, T, ET>,
|
||||
{
|
||||
let mut items = Vec::new();
|
||||
let mut last_error: Option<AstError<'a, ET>> = None;
|
||||
|
||||
for pair in pairs {
|
||||
match f(pair) {
|
||||
Ok(item) => items.push(item),
|
||||
Err(e) => {
|
||||
last_error = Some(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match last_error {
|
||||
Some(ast_err) => Err(AstError {
|
||||
span: ast_err.span,
|
||||
error_code: ast_err.error_code,
|
||||
error_message: ast_err.error_message,
|
||||
recovered: Some(items),
|
||||
}),
|
||||
None => Ok(items),
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AstErrorAnalyzer<'a, T>(pub Option<AstError<'a, T>>);
|
||||
|
||||
impl<'a, T> AstErrorAnalyzer<'a, T> {
|
||||
pub fn get<V: Clone, V2: Clone + Into<V>>(
|
||||
&mut self,
|
||||
r: AstResult<'a, V, V2>,
|
||||
) -> AstResult<'a, V, V2> {
|
||||
if let Err(e) = r {
|
||||
self.0 = Some(e.clone().get());
|
||||
|
||||
if let Some(recovered) = e.recovered {
|
||||
Ok(recovered.into())
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
} else {
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(self, v: T) -> AstResult<'a, T> {
|
||||
if let Some(mut e) = self.0 {
|
||||
e.recovered = Some(v);
|
||||
|
||||
Err(e)
|
||||
} else {
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,603 @@
|
||||
// ======================================================
|
||||
// LEXICAL
|
||||
// ======================================================
|
||||
|
||||
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
||||
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
|
||||
|
||||
// ------------------------------------------------------
|
||||
// PRIMITIVES
|
||||
// ------------------------------------------------------
|
||||
|
||||
keyword = {
|
||||
"if"
|
||||
| "else"
|
||||
| "for"
|
||||
| "while"
|
||||
| "match"
|
||||
| "return"
|
||||
| "break"
|
||||
| "continue"
|
||||
| "struct"
|
||||
| "enum"
|
||||
| "class"
|
||||
| "trait"
|
||||
| "impl"
|
||||
| "pub"
|
||||
| "mut"
|
||||
| "var"
|
||||
| "true"
|
||||
| "false"
|
||||
| "dyn"
|
||||
| "loop"
|
||||
}
|
||||
|
||||
identifier = @{
|
||||
!(keyword ~ !(ASCII_ALPHANUMERIC | "_")) ~ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")*
|
||||
}
|
||||
|
||||
integer = @{ ASCII_DIGIT+ }
|
||||
|
||||
float = @{
|
||||
ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+
|
||||
}
|
||||
|
||||
boolean = {
|
||||
"true"
|
||||
| "false"
|
||||
}
|
||||
|
||||
string_lit = {
|
||||
"\"" ~ inner_str ~ "\""
|
||||
}
|
||||
|
||||
inner_str = @{
|
||||
(!"\"" ~ ("\\\"" | ANY))*
|
||||
}
|
||||
|
||||
semicolon = _{ ";" }
|
||||
|
||||
static_path = {
|
||||
identifier ~ ("::" ~ identifier)*
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// PROGRAM
|
||||
// ======================================================
|
||||
|
||||
program = {
|
||||
SOI ~ top_level* ~ EOI
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// MODIFIERS
|
||||
// ======================================================
|
||||
|
||||
visibility = { "pub" ~ ("(" ~ static_path ~ ")")? }
|
||||
mutable = { "mut" }
|
||||
var = { "var" }
|
||||
|
||||
// ======================================================
|
||||
// GENERICS
|
||||
// ======================================================
|
||||
generic_decl = { lifetime | (identifier ~ (":" ~ type_expr_param ~ ("+" ~ type_expr_param)*)?) }
|
||||
generics_decl = { "<" ~ (generic_decl ~ ("," ~ generic_decl)*)? ~ ">" }
|
||||
|
||||
generic = { lifetime | type_expr_param }
|
||||
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
|
||||
|
||||
// ======================================================
|
||||
// TYPES
|
||||
// ======================================================
|
||||
|
||||
tuple_type = {
|
||||
"(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")"
|
||||
}
|
||||
|
||||
path_type = {
|
||||
static_path ~ ("<" ~ (type_expr_param ~ ("," ~ type_expr_param)*)? ~ ">")?
|
||||
}
|
||||
|
||||
ref_type = {
|
||||
mutable? ~ lifetime? ~ "*"
|
||||
}
|
||||
|
||||
dyn_type = {
|
||||
"dyn"
|
||||
}
|
||||
|
||||
type_expr = {
|
||||
(path_type | tuple_type) ~ (ref_type | dyn_type)*
|
||||
}
|
||||
|
||||
lifetime = {
|
||||
"'" ~ identifier
|
||||
}
|
||||
|
||||
type_expr_param = {
|
||||
type_expr
|
||||
| lifetime
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// LITERALS
|
||||
// ======================================================
|
||||
|
||||
literal = {
|
||||
float
|
||||
| integer
|
||||
| boolean
|
||||
| string_lit
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// PATTERNS
|
||||
// ======================================================
|
||||
|
||||
tuple_pattern = {
|
||||
"[" ~ (identifier ~ ("," ~ identifier)*) ~ "]"
|
||||
}
|
||||
|
||||
named_tuple_pattern = {
|
||||
static_path ~ "[" ~ (identifier ~ ("," ~ identifier)*)? ~ "]"
|
||||
}
|
||||
|
||||
struct_pattern = {
|
||||
static_path ~ "{" ~ (identifier ~ ("," ~ identifier)*)? ~ "}"
|
||||
}
|
||||
|
||||
pattern = _{
|
||||
named_tuple_pattern
|
||||
| tuple_pattern
|
||||
| struct_pattern
|
||||
| literal
|
||||
| static_path
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// ATTRIBUTES / META
|
||||
// ======================================================
|
||||
|
||||
attribute = {
|
||||
"#" ~ "[" ~ meta ~ "]"
|
||||
}
|
||||
|
||||
mod_attribute = {
|
||||
"#" ~ "!" ~ "[" ~ meta ~ "]"
|
||||
}
|
||||
|
||||
meta = {
|
||||
static_path ~ ("=" ~ literal | "(" ~ meta_list? ~ ")")?
|
||||
}
|
||||
|
||||
meta_list = {
|
||||
meta ~ ("," ~ meta)* ~ ","?
|
||||
}
|
||||
|
||||
attributes = {
|
||||
attribute*
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// DECLARATION HELPERS
|
||||
// ======================================================
|
||||
var_decl = {
|
||||
(var | type_expr) ~ mutable? ~ pattern
|
||||
}
|
||||
|
||||
field = {
|
||||
visibility? ~ type_expr ~ identifier
|
||||
}
|
||||
|
||||
field_list = {
|
||||
field ~ ("," ~ field)* ~ ","?
|
||||
}
|
||||
|
||||
param_list = {
|
||||
var_decl ~ ("," ~ var_decl)* ~ ","?
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// IMPORTS / MODULES
|
||||
// ======================================================
|
||||
|
||||
import = {
|
||||
visibility? ~ "use" ~ "<" ~ static_path ~ ">" ~ semicolon
|
||||
}
|
||||
|
||||
mod_package = {
|
||||
visibility? ~ "mod" ~ identifier ~ semicolon
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// STRUCTS
|
||||
// ======================================================
|
||||
|
||||
struct_decl = {
|
||||
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list? ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// ENUMS
|
||||
// ======================================================
|
||||
|
||||
enum_named = {
|
||||
identifier
|
||||
}
|
||||
|
||||
enum_tuple = {
|
||||
identifier ~ tuple_type
|
||||
}
|
||||
|
||||
enum_struct = {
|
||||
identifier ~ "{" ~ field_list ~ "}"
|
||||
}
|
||||
|
||||
enum_field = _{
|
||||
enum_tuple
|
||||
| enum_struct
|
||||
| enum_named
|
||||
}
|
||||
|
||||
enum_fields = _{
|
||||
enum_field ~ ("," ~ enum_field)* ~ ","?
|
||||
}
|
||||
|
||||
enum_decl = {
|
||||
visibility? ~ "enum" ~ identifier ~ generics_decl? ~ "{" ~ enum_fields ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// CLASSES
|
||||
// ======================================================
|
||||
|
||||
class_constructor = {
|
||||
visibility? ~ "constructor" ~ generics_decl? ~ "(" ~ param_list? ~ ")" ~ block
|
||||
}
|
||||
|
||||
class_fields = {
|
||||
class_field*
|
||||
}
|
||||
|
||||
class_field = {
|
||||
field ~ ("=" ~ expr)? ~ semicolon
|
||||
}
|
||||
|
||||
self_param = {
|
||||
"self" ~ mutable? ~ deref_px?
|
||||
}
|
||||
|
||||
method = {
|
||||
visibility? ~ type_expr ~ identifier ~ generics_decl? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ block
|
||||
}
|
||||
|
||||
class_item = _{
|
||||
impl_decl
|
||||
| method
|
||||
}
|
||||
|
||||
class_decl = {
|
||||
visibility? ~ "class" ~ identifier ~ generics_decl? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// IMPL
|
||||
// ======================================================
|
||||
impl_for_decl = {
|
||||
"impl" ~ generics_decl? ~ type_expr ~ "for" ~ type_expr ~ "{" ~ method* ~ "}"
|
||||
}
|
||||
|
||||
impl_decl = {
|
||||
"impl" ~ generics_decl? ~ type_expr ~ "{" ~ method* ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// Traits
|
||||
// ======================================================
|
||||
trait_requirements = { ":" ~ type_expr_param ~ ("+" ~ type_expr_param)* }
|
||||
|
||||
method_no_body = {
|
||||
visibility? ~ type_expr ~ identifier ~ generics_decl? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ semicolon
|
||||
}
|
||||
|
||||
trait_item = _{
|
||||
method
|
||||
| method_no_body
|
||||
}
|
||||
|
||||
trait_decl = {
|
||||
visibility? ~ "trait" ~ identifier ~ generics_decl? ~ trait_requirements? ~ "{" ~ trait_item* ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// FUNCTIONS
|
||||
// ======================================================
|
||||
|
||||
function_decl = {
|
||||
visibility? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// TOP LEVEL
|
||||
// ======================================================
|
||||
|
||||
top_level = {
|
||||
(attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | mod_package | function_decl))
|
||||
| mod_attribute
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// STATEMENTS
|
||||
// ======================================================
|
||||
|
||||
block = {
|
||||
"{" ~ statement_list ~ expr? ~ "}"
|
||||
}
|
||||
|
||||
statement_list = {
|
||||
statement*
|
||||
}
|
||||
|
||||
statement = _{
|
||||
control_flow
|
||||
| (expr ~ semicolon)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// BASIC STATEMENTS
|
||||
// ------------------------------------------------------
|
||||
|
||||
basic_stmt = {
|
||||
return_stmt
|
||||
| break_stmt
|
||||
| continue_stmt
|
||||
| var_decl_statement
|
||||
}
|
||||
|
||||
return_stmt = {
|
||||
"return" ~ expr?
|
||||
}
|
||||
|
||||
break_stmt = {
|
||||
"break"
|
||||
}
|
||||
|
||||
continue_stmt = {
|
||||
"continue"
|
||||
}
|
||||
|
||||
var_decl_statement = {
|
||||
var_decl ~ ("=" ~ expr)?
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// CONDITIONALS
|
||||
// ------------------------------------------------------
|
||||
|
||||
control_flow = {
|
||||
if_stmt
|
||||
| while_stmt
|
||||
| c_for_stmt
|
||||
| for_stmt
|
||||
| match_stmt
|
||||
| loop_stmt
|
||||
| block
|
||||
}
|
||||
|
||||
statement_branch = { "(" ~ expr ~ ")" ~ statement }
|
||||
|
||||
else_if = {
|
||||
"else" ~ "if" ~ statement_branch
|
||||
}
|
||||
|
||||
else_if_list = {
|
||||
else_if*
|
||||
}
|
||||
|
||||
if_stmt = {
|
||||
"if" ~ statement_branch ~ else_if_list ~ ("else" ~ statement)?
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// LOOPS
|
||||
// ------------------------------------------------------
|
||||
|
||||
while_stmt = {
|
||||
"while" ~ statement_branch
|
||||
}
|
||||
|
||||
c_for_stmt = {
|
||||
"for" ~ "(" ~ statement ~ statement ~ expr ~ ")" ~ statement
|
||||
}
|
||||
|
||||
for_stmt = {
|
||||
"for" ~ "(" ~ mutable? ~ pattern ~ ":" ~ expr ~ ")" ~ statement
|
||||
}
|
||||
|
||||
loop_stmt = {
|
||||
"loop" ~ statement
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// MATCH
|
||||
// ------------------------------------------------------
|
||||
|
||||
pattern_list = { pattern ~ ("|" ~ pattern)* }
|
||||
|
||||
match_item = {
|
||||
pattern_list ~ "=>" ~ statement
|
||||
}
|
||||
|
||||
match_stmt = {
|
||||
"match" ~ "(" ~ expr ~ ")" ~ "{" ~ match_item* ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// EXPRESSIONS
|
||||
// ======================================================
|
||||
|
||||
// ------------------------------------------------------
|
||||
// TUPLES & ARRAYS
|
||||
// ------------------------------------------------------
|
||||
|
||||
tuple = {
|
||||
"(" ~ (expr ~ ("," ~ expr)*)? ~ ")"
|
||||
}
|
||||
|
||||
array = {
|
||||
"[" ~ expr ~ ";" ~ expr ~ "]" // Repeat notation: [0; 10]
|
||||
|
||||
| "[" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ "]" // Standard elements initialization
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// MACROS
|
||||
// ------------------------------------------------------
|
||||
|
||||
macro_inner_v = @{
|
||||
string_lit
|
||||
| ("(" ~ macro_inner_v* ~ ")")
|
||||
| (!("(" | ")") ~ ANY)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// PRIMARY EXPRESSIONS
|
||||
// ------------------------------------------------------
|
||||
|
||||
expr_path_segment = {
|
||||
identifier ~ ("::" ~ generics)?
|
||||
}
|
||||
|
||||
expr_path = {
|
||||
expr_path_segment ~ ("::" ~ expr_path_segment)*
|
||||
}
|
||||
|
||||
primary = {
|
||||
control_flow
|
||||
| basic_stmt
|
||||
| literal
|
||||
| expr_path
|
||||
| tuple
|
||||
| array
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// POSTFIX EXPRESSIONS
|
||||
// ------------------------------------------------------
|
||||
|
||||
field_px = {
|
||||
"." ~ identifier ~ ("::" ~ generics)?
|
||||
}
|
||||
|
||||
call_px = {
|
||||
"(" ~ (expr ~ ("," ~ expr)*)? ~ ")"
|
||||
}
|
||||
|
||||
macro_call_px = {
|
||||
"!" ~ "(" ~ macro_inner_v* ~ ")"
|
||||
}
|
||||
|
||||
struct_field_decl = {
|
||||
identifier ~ ":" ~ expr
|
||||
}
|
||||
|
||||
struct_px = {
|
||||
"{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ ","? ~ "}"
|
||||
}
|
||||
|
||||
index_px = {
|
||||
"[" ~ expr ~ "]"
|
||||
}
|
||||
|
||||
as_px = {
|
||||
"as" ~ type_expr
|
||||
}
|
||||
|
||||
try_px = {
|
||||
"?"
|
||||
}
|
||||
|
||||
increment = {
|
||||
"++"
|
||||
}
|
||||
|
||||
decrement = {
|
||||
"--"
|
||||
}
|
||||
|
||||
postfix = {
|
||||
field_px
|
||||
| call_px
|
||||
| macro_call_px
|
||||
| struct_px
|
||||
| index_px
|
||||
| as_px
|
||||
| try_px
|
||||
| increment
|
||||
| decrement
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// PREFIX EXPRESSIONS
|
||||
// ------------------------------------------------------
|
||||
|
||||
deref_px = { "*" }
|
||||
mut_ref_px = { "&" ~ mutable }
|
||||
ref_px = { "&" }
|
||||
new_px = { "new" ~ generics? }
|
||||
not_px = { "!" }
|
||||
neg_px = { "-" }
|
||||
|
||||
prefix = {
|
||||
deref_px
|
||||
| mut_ref_px
|
||||
| ref_px
|
||||
| new_px
|
||||
| not_px
|
||||
| neg_px
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// OPERATORS
|
||||
// ------------------------------------------------------
|
||||
|
||||
bin_op = {
|
||||
"+="
|
||||
| "-="
|
||||
| "/="
|
||||
| "%="
|
||||
| "&="
|
||||
| "|="
|
||||
| "^="
|
||||
| "<<="
|
||||
| ">>="
|
||||
| "<<"
|
||||
| ">>"
|
||||
| "..="
|
||||
| ".."
|
||||
| "<="
|
||||
| ">="
|
||||
| "=="
|
||||
| "!="
|
||||
| "&&"
|
||||
| "||"
|
||||
| "+"
|
||||
| "-"
|
||||
| "*"
|
||||
| "/"
|
||||
| "%"
|
||||
| "<"
|
||||
| ">"
|
||||
| "&"
|
||||
| "|"
|
||||
| "^"
|
||||
| "="
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// EXPRESSIONS
|
||||
// ------------------------------------------------------
|
||||
|
||||
expr = { term ~ (bin_op ~ term)* }
|
||||
|
||||
term = { prefix* ~ primary ~ postfix* }
|
||||
@@ -0,0 +1,83 @@
|
||||
use pest::Parser;
|
||||
use pest_derive::Parser;
|
||||
|
||||
pub mod ast;
|
||||
pub mod error;
|
||||
pub mod parser;
|
||||
pub mod rev_mapper;
|
||||
|
||||
use ast::*;
|
||||
|
||||
use crate::error::{IntoErr, ParseError};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "./src/grammar.pest"]
|
||||
pub struct MistParser;
|
||||
|
||||
pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<TopLevel>>> {
|
||||
let mut pairs = MistParser::parse(Rule::program, source)?;
|
||||
|
||||
let mut statements = vec![];
|
||||
|
||||
let mut analyzer = error::AstErrorAnalyzer(None);
|
||||
|
||||
for pair in pairs.next().unwrap().into_inner() {
|
||||
if pair.as_rule() != Rule::EOI {
|
||||
statements.push(analyzer.get(TopLevel::try_from(pair)).get()?);
|
||||
}
|
||||
}
|
||||
|
||||
match analyzer.build(statements) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => Err(ParseError::Ast(e)),
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! ast_ensure {
|
||||
($pair:expr, $rule:expr $(, $rules:expr)* => $body:block) => {
|
||||
if $pair.as_rule() == $rule $(|| $pair.as_rule() == $rules)*
|
||||
$body
|
||||
else {
|
||||
Err(AstError {
|
||||
span: $pair.as_span(),
|
||||
error_code: crate::error::ErrorCode::AstGenBug,
|
||||
error_message: format!("Possible bug: expected {:?}, got {:?}", $rule, $pair.as_rule()),
|
||||
recovered: None,
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! ast_expr {
|
||||
(use $r:expr, $($v:expr),* $(,)?) => {{
|
||||
let mut analyzer = $crate::error::AstErrorAnalyzer(None);
|
||||
|
||||
$(
|
||||
analyzer.get($v).get()?;
|
||||
)*
|
||||
|
||||
analyzer.build($r)
|
||||
}};
|
||||
|
||||
($($item:ident)::+ { $($k:ident: $v:expr),* $(,)? }) => {{
|
||||
let mut analyzer = $crate::error::AstErrorAnalyzer(None);
|
||||
|
||||
let v = $($item)::+ { $($k: analyzer.get($v).get()?),* };
|
||||
|
||||
analyzer.build(v)
|
||||
}};
|
||||
|
||||
($($item:ident)::+ ( $($v:expr),* $(,)? )) => {{
|
||||
let mut analyzer = $crate::error::AstErrorAnalyzer(None);
|
||||
|
||||
let v = $($item)::+ ( $(analyzer.get($v).get()?),* );
|
||||
|
||||
analyzer.build(v)
|
||||
}};
|
||||
|
||||
($($item:ident)::+) => {
|
||||
$($item)::+
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_expr,
|
||||
error::{AstError, AstResult, IntoErr},
|
||||
parser::listen_rule,
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
match pair.as_rule() {
|
||||
Rule::var_decl_statement => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
ast_expr!(VarDeclStmt {
|
||||
decl: inner.next().unwrap().try_into(),
|
||||
init: inner.next().map(Expression::try_from).transpose()
|
||||
})
|
||||
}
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDeclStmt {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
match pair.as_rule() {
|
||||
Rule::class_field => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
ast_expr!(FieldDeclStmt {
|
||||
decl: inner.next().unwrap().try_into(),
|
||||
init: inner.next().map(Expression::try_from).transpose(),
|
||||
})
|
||||
}
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
match pair.as_rule() {
|
||||
Rule::var_decl => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let type_ = inner
|
||||
.next()
|
||||
.and_then(|pair| {
|
||||
if pair.as_str().trim() == "var" {
|
||||
None
|
||||
} else {
|
||||
Some(TypeExpr::try_from(pair))
|
||||
}
|
||||
})
|
||||
.transpose();
|
||||
|
||||
let mutable: AstResult<'_, bool> = Ok(listen_rule(&mut inner, Rule::mutable));
|
||||
|
||||
let name = Pattern::try_from(inner.next().unwrap());
|
||||
|
||||
ast_expr!(VarDecl {
|
||||
type_: type_,
|
||||
mutable: mutable,
|
||||
name: name,
|
||||
})
|
||||
}
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDecl {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
match pair.as_rule() {
|
||||
Rule::field => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
ast_expr!(FieldDecl {
|
||||
visibility: Visibility::try_from(&mut inner),
|
||||
type_: TypeExpr::try_from(inner.next().unwrap()),
|
||||
name: Identifier::try_from(inner.next().unwrap()),
|
||||
})
|
||||
}
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_ensure, ast_expr,
|
||||
error::{AstError, AstResult, GetLength, IntoErr, collect_recovered, collect_recovered_map},
|
||||
};
|
||||
use pest::pratt_parser::PrattParser;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::expr => {
|
||||
static PRATT_PARSER: OnceLock<PrattParser<Rule>> = OnceLock::new();
|
||||
let pratt = PRATT_PARSER.get_or_init(|| {
|
||||
use pest::pratt_parser::{Assoc::*, Op};
|
||||
|
||||
PrattParser::new().op(Op::infix(Rule::bin_op, Left))
|
||||
});
|
||||
|
||||
pratt
|
||||
.map_primary(|primary_pair| Expression::try_from(primary_pair))
|
||||
.map_infix(|expr, op, rhs| {
|
||||
ast_expr!(Expression::Binary {
|
||||
lhs: expr.map(Box::new).get_map(Box::new),
|
||||
op: Ok(op.as_str().to_string()) as AstResult<'_, String>,
|
||||
rhs: rhs.map(Box::new).get_map(Box::new),
|
||||
})
|
||||
})
|
||||
.parse(inner)
|
||||
}
|
||||
|
||||
Rule::term => {
|
||||
let mut prefix_pairs = Vec::new();
|
||||
let mut primary_pair = None;
|
||||
let mut postfix_pairs = Vec::new();
|
||||
|
||||
for p in inner {
|
||||
match p.as_rule() {
|
||||
Rule::prefix => prefix_pairs.push(p),
|
||||
Rule::primary => primary_pair = Some(p),
|
||||
Rule::postfix => postfix_pairs.push(p),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let prefixes = collect_recovered::<Prefix, Prefix>(prefix_pairs.into_iter());
|
||||
let exp = Expression::try_from(
|
||||
primary_pair.expect("Term must contain a primary expression"),
|
||||
);
|
||||
let postfixes = collect_recovered::<Postfix, Postfix>(postfix_pairs.into_iter());
|
||||
|
||||
if postfixes.len() > 0 || prefixes.len() > 0 {
|
||||
ast_expr!(Expression::Fix {
|
||||
initial: exp.map(Box::new),
|
||||
prefixes: prefixes,
|
||||
postfixes: postfixes,
|
||||
})
|
||||
} else {
|
||||
ast_expr!(use exp?, prefixes, postfixes)
|
||||
}
|
||||
}
|
||||
|
||||
Rule::tuple => {
|
||||
ast_expr!(Expression::Literal(
|
||||
collect_recovered(pair.into_inner())
|
||||
.map(Literal::Tuple)
|
||||
.get_map(Literal::Tuple)
|
||||
))
|
||||
}
|
||||
|
||||
Rule::primary => pair.into_inner().next().unwrap().try_into(),
|
||||
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
|
||||
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
|
||||
Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())),
|
||||
Rule::statement | Rule::basic_stmt | Rule::control_flow => ast_expr!(
|
||||
Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new))
|
||||
),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Prefix {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
Ok(match pair.as_rule() {
|
||||
Rule::prefix => Self::try_from(pair.into_inner().next().unwrap())?,
|
||||
Rule::deref_px => Self::Deref,
|
||||
Rule::mut_ref_px => Self::RefMut,
|
||||
Rule::ref_px => Self::Ref,
|
||||
Rule::new_px => Self::New(
|
||||
pair.into_inner()
|
||||
.next()
|
||||
.map(|v| v.try_into().get())
|
||||
.transpose()?,
|
||||
),
|
||||
Rule::not_px => Self::Not,
|
||||
Rule::neg_px => Self::Neg,
|
||||
|
||||
_ => return AstError::bug_unimplemented(pair),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::postfix => Postfix::try_from(inner.next().unwrap()),
|
||||
|
||||
Rule::field_px => {
|
||||
ast_expr!(Postfix::FieldAccess(
|
||||
inner.next().unwrap().try_into(),
|
||||
inner.next().map(Generics::try_from).transpose()
|
||||
))
|
||||
}
|
||||
|
||||
Rule::call_px => ast_expr!(Postfix::Call(collect_recovered(inner))),
|
||||
|
||||
Rule::struct_px => ast_expr!(Postfix::StructCall(collect_recovered_map(inner, |p| {
|
||||
let mut pi = p.into_inner();
|
||||
Ok((
|
||||
Identifier::try_from(pi.next().unwrap())?,
|
||||
Expression::try_from(pi.next().unwrap()).get()?,
|
||||
))
|
||||
}))),
|
||||
|
||||
Rule::index_px => {
|
||||
ast_expr!(Postfix::Index(Expression::try_from(inner.next().unwrap())))
|
||||
}
|
||||
|
||||
Rule::macro_call_px => Ok(Postfix::MacroCall(inner.as_str().to_string())),
|
||||
|
||||
Rule::as_px => {
|
||||
ast_expr!(Postfix::As(inner.next().unwrap().try_into()))
|
||||
}
|
||||
|
||||
Rule::try_px => Ok(Postfix::Try),
|
||||
|
||||
Rule::increment => Ok(Postfix::Increment),
|
||||
Rule::decrement => Ok(Postfix::Decrement),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ExprPath {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
ast_ensure!(pair, Rule::expr_path => {
|
||||
ast_expr!(ExprPath(collect_recovered(pair.into_inner())))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ExprPathSegment {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
ast_ensure!(pair, Rule::expr_path_segment => {
|
||||
ast_expr!(ExprPathSegment {
|
||||
ident: Identifier::try_from(inner.next().unwrap()),
|
||||
generics: inner.next().map(Generics::try_from).transpose(),
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
pub mod decl;
|
||||
pub mod expr;
|
||||
pub mod statement;
|
||||
pub mod types;
|
||||
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_ensure, ast_expr,
|
||||
error::{AstError, IntoErr, collect_recovered},
|
||||
parser::consume_rule,
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Identifier {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
ast_ensure!(pair, Rule::identifier => {
|
||||
Ok(Identifier(pair.as_str().to_string()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Path {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
match pair.as_rule() {
|
||||
Rule::static_path => Ok(Path(collect_recovered(pair.into_inner()).get()?)),
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Literal {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
Ok(match rule {
|
||||
Rule::primary => Self::try_from(inner.next().unwrap())?,
|
||||
Rule::literal => Self::try_from(inner.next().unwrap())?,
|
||||
Rule::integer => Literal::Int(pair.as_str().parse::<i64>().unwrap()),
|
||||
Rule::float => Literal::Float(pair.as_str().parse::<f64>().unwrap()),
|
||||
Rule::boolean => Literal::Bool(pair.as_str().parse::<bool>().unwrap()),
|
||||
Rule::string_lit => Literal::String(inner.as_str().to_string()),
|
||||
Rule::tuple => Literal::Tuple(collect_recovered(inner).get()?),
|
||||
_ => return AstError::bug_unimplemented(pair),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::tuple_pattern => ast_expr!(Pattern::Tuple(collect_recovered(pair.into_inner()))),
|
||||
|
||||
Rule::named_tuple_pattern => ast_expr!(Pattern::NamedTuple(
|
||||
Path::try_from(inner.next().unwrap()),
|
||||
collect_recovered(inner),
|
||||
)),
|
||||
|
||||
Rule::struct_pattern => ast_expr!(Pattern::Struct(
|
||||
Path::try_from(inner.next().unwrap()),
|
||||
collect_recovered(inner),
|
||||
)),
|
||||
|
||||
Rule::literal => ast_expr!(Pattern::Literal(pair.try_into())),
|
||||
|
||||
Rule::identifier => ast_expr!(Pattern::Id(pair.try_into())),
|
||||
|
||||
Rule::static_path => ast_expr!(Pattern::Path(pair.try_into())),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&mut pest::iterators::Pairs<'a, Rule>> for Visibility {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pairs: &mut pest::iterators::Pairs<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
Ok(consume_rule(pairs, Rule::visibility)
|
||||
.map(|pair| -> Result<Visibility, AstError<'a, Self>> {
|
||||
if let Some(path) = pair.into_inner().next() {
|
||||
ast_expr!(Visibility::PublicTarget(Path::try_from(path)))
|
||||
} else {
|
||||
Ok(Visibility::Public)
|
||||
}
|
||||
})
|
||||
.transpose()?
|
||||
.unwrap_or_else(|| Visibility::Private))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_ensure, ast_expr,
|
||||
error::{AstError, AstResult, IntoErr, collect_recovered},
|
||||
parser::listen_rule,
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
ast_ensure!(pair, Rule::block => {
|
||||
ast_expr!(Block(collect_recovered(inner.next().unwrap().into_inner()), inner.next().map(Spanned::try_from).transpose()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for StatementBranch {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
ast_ensure!(pair, Rule::statement_branch => {
|
||||
ast_expr!(StatementBranch {
|
||||
condition: inner.next().unwrap().try_into(),
|
||||
body: inner.next().unwrap().try_into().map(Box::new),
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::statement | Rule::basic_stmt | Rule::control_flow => {
|
||||
Statement::try_from(inner.next().unwrap())
|
||||
}
|
||||
|
||||
Rule::block => ast_expr!(Statement::Block(pair.try_into())),
|
||||
|
||||
Rule::var_decl_statement => ast_expr!(Statement::VarDecl(pair.try_into())),
|
||||
|
||||
Rule::return_stmt => {
|
||||
ast_expr!(Statement::Return(
|
||||
inner.next().map(Expression::try_from).transpose()
|
||||
))
|
||||
}
|
||||
|
||||
Rule::break_stmt => Ok(Statement::Break),
|
||||
|
||||
Rule::continue_stmt => Ok(Statement::Continue),
|
||||
|
||||
Rule::if_stmt => {
|
||||
ast_expr!(Statement::If {
|
||||
initial: inner.next().unwrap().try_into(),
|
||||
else_if: collect_recovered(inner.next().unwrap().into_inner()),
|
||||
else_branch: inner
|
||||
.next()
|
||||
.map(Expression::try_from)
|
||||
.transpose()
|
||||
.map(|v| v.map(Box::new))
|
||||
.get_map(|v| { Some(Box::new(v)) }),
|
||||
})
|
||||
}
|
||||
|
||||
Rule::while_stmt => ast_expr!(Statement::While(inner.next().unwrap().try_into())),
|
||||
|
||||
Rule::loop_stmt => ast_expr!(Statement::Loop(inner.next().unwrap().try_into())),
|
||||
|
||||
Rule::c_for_stmt => ast_expr!(Statement::CStyleFor {
|
||||
init: inner.next().unwrap().try_into(),
|
||||
condition: inner.next().unwrap().try_into(),
|
||||
update: inner.next().unwrap().try_into(),
|
||||
body: inner.next().unwrap().try_into(),
|
||||
}),
|
||||
|
||||
Rule::for_stmt => ast_expr!(Statement::For {
|
||||
mutable: Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult<'_, bool>,
|
||||
pattern: inner.next().unwrap().try_into(),
|
||||
iterator: inner.next().unwrap().try_into(),
|
||||
body: inner.next().unwrap().try_into().map(Box::new),
|
||||
}),
|
||||
|
||||
Rule::match_stmt => ast_expr!(Statement::Match(
|
||||
inner.next().unwrap().try_into(),
|
||||
inner
|
||||
.map(|match_itms| {
|
||||
let mut match_inner = match_itms.into_inner();
|
||||
Ok((
|
||||
collect_recovered(match_inner.next().unwrap().into_inner()).get()?,
|
||||
Expression::try_from(match_inner.next().unwrap()).get()?,
|
||||
))
|
||||
})
|
||||
.collect::<AstResult<'a, Vec<_>>>(),
|
||||
)),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_ensure, ast_expr,
|
||||
error::{AstError, GetLength, IntoErr, collect_recovered},
|
||||
parser::{consume_rule, listen_rule},
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypePostfix {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::ref_type => {
|
||||
let mutable = listen_rule(&mut inner, Rule::mutable);
|
||||
let lifetime = consume_rule(&mut inner, Rule::lifetime)
|
||||
.map(|pair| Identifier::try_from(pair.into_inner().next().unwrap()))
|
||||
.transpose()
|
||||
.get()?;
|
||||
|
||||
Ok(if mutable {
|
||||
if let Some(lifetime) = lifetime {
|
||||
TypePostfix::RefMutLifetime(lifetime)
|
||||
} else {
|
||||
TypePostfix::RefMut
|
||||
}
|
||||
} else {
|
||||
if let Some(lifetime) = lifetime {
|
||||
TypePostfix::RefLifetime(lifetime)
|
||||
} else {
|
||||
TypePostfix::Ref
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Rule::dyn_type => Ok(TypePostfix::Dyn),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::tuple_type => ast_expr!(TypeExprKind::Tuple(collect_recovered(inner))),
|
||||
Rule::path_type => {
|
||||
let path = Path::try_from(inner.next().unwrap());
|
||||
let params = collect_recovered(inner);
|
||||
|
||||
if params.len() == 0 {
|
||||
ast_expr!(TypeExprKind::Path(path))
|
||||
} else {
|
||||
ast_expr!(TypeExprKind::PathParams(path, params))
|
||||
}
|
||||
}
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::type_expr => ast_expr!(TypeExpr(
|
||||
inner.next().unwrap().try_into(),
|
||||
collect_recovered(inner),
|
||||
)),
|
||||
Rule::type_expr_param => Self::try_from(inner.next().unwrap()),
|
||||
Rule::lifetime => ast_expr!(TypeExprKind::Lifetime(inner.next().unwrap().try_into()))
|
||||
.get_map(TypeExpr::no_px)
|
||||
.map(TypeExpr::no_px),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for GenericsDecl {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let inner = pair.clone().into_inner();
|
||||
|
||||
ast_ensure!(pair, Rule::generics_decl => {
|
||||
ast_expr!(GenericsDecl(collect_recovered(inner)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for GenericDecl {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
ast_ensure!(pair, Rule::generic_decl => {
|
||||
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
|
||||
ast_expr!(GenericDecl::Lifetime(
|
||||
pair.into_inner().next().unwrap().try_into(),
|
||||
))
|
||||
} else {
|
||||
ast_expr!(GenericDecl::Type(
|
||||
inner.next().unwrap().try_into(),
|
||||
collect_recovered(inner),
|
||||
))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let inner = pair.clone().into_inner();
|
||||
|
||||
ast_ensure!(pair, Rule::generics => {
|
||||
ast_expr!(Generics(collect_recovered(inner)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generic {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
ast_ensure!(pair, Rule::generic => {
|
||||
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
|
||||
ast_expr!(Generic::Lifetime(
|
||||
pair.into_inner().next().unwrap().try_into(),
|
||||
))
|
||||
} else {
|
||||
ast_expr!(Generic::Type(
|
||||
inner.next().unwrap().try_into()
|
||||
))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
error::{AstError, IntoErr, collect_recovered},
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
match pair.as_rule() {
|
||||
Rule::attribute => {
|
||||
// unwrap #[ ... ]
|
||||
Attribute::try_from(pair.into_inner().next().unwrap())
|
||||
}
|
||||
|
||||
Rule::meta => {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
// first item is always the path
|
||||
let path = inner.next().unwrap().try_into().get()?;
|
||||
|
||||
// check what comes next
|
||||
match inner.next() {
|
||||
None => {
|
||||
// #[path]
|
||||
Ok(Attribute::Path(path))
|
||||
}
|
||||
|
||||
Some(next) => match next.as_rule() {
|
||||
Rule::primary => {
|
||||
// #[path = literal]
|
||||
Ok(Attribute::NameValue {
|
||||
path,
|
||||
value: inner.next().unwrap().try_into().get()?,
|
||||
})
|
||||
}
|
||||
|
||||
Rule::meta_list => {
|
||||
// #[path(...)]
|
||||
Ok(Attribute::List {
|
||||
path,
|
||||
items: collect_recovered(next.into_inner()).get()?,
|
||||
})
|
||||
}
|
||||
|
||||
_ => unreachable!("unexpected rule in meta: {:?}", next.as_rule()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Rule::meta_list => {
|
||||
// This case usually won't be hit directly,
|
||||
// but it's nice to keep it safe if reused
|
||||
let items = pair
|
||||
.into_inner()
|
||||
.map(Attribute::try_from)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// NOTE: this shouldn't normally construct an Attribute alone
|
||||
// but you can panic or wrap depending on your design
|
||||
panic!("meta_list should be handled inside meta: {:?}", items);
|
||||
}
|
||||
|
||||
_ => unreachable!("unexpected rule: {:?}", pair.as_rule()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
error::{AstError, IntoErr},
|
||||
parser::consume_rule,
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassConstructor {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
Ok(Self {
|
||||
visibility: Visibility::try_from(&mut inner).get()?,
|
||||
|
||||
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.get()?
|
||||
.unwrap_or_default(),
|
||||
|
||||
params: consume_rule(&mut inner, Rule::param_list)
|
||||
.map(ParamList::try_from)
|
||||
.transpose()
|
||||
.get()?
|
||||
.unwrap_or_default(),
|
||||
|
||||
body: inner.next().unwrap().try_into().get()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassItem {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
|
||||
match rule {
|
||||
Rule::impl_decl | Rule::impl_for_decl => {
|
||||
Ok(ClassItem::ImplDecl(pair.try_into().get()?))
|
||||
}
|
||||
|
||||
Rule::method => Ok(ClassItem::Method(pair.try_into().get()?)),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
error::{AstError, IntoErr, collect_recovered},
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::enum_named => Ok(EnumItem::Named(inner.next().unwrap().try_into().get()?)),
|
||||
|
||||
Rule::enum_tuple => Ok(EnumItem::Tuple(
|
||||
inner.next().unwrap().try_into().get()?,
|
||||
collect_recovered(inner.next().unwrap().into_inner()).get()?,
|
||||
)),
|
||||
|
||||
Rule::enum_struct => Ok(EnumItem::Struct(
|
||||
inner.next().unwrap().try_into().get()?,
|
||||
inner
|
||||
.next()
|
||||
.map(|pair| collect_recovered::<FieldDecl, FieldDecl>(pair.into_inner()))
|
||||
.transpose()
|
||||
.get()?
|
||||
.unwrap_or_default(),
|
||||
)),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_ensure, ast_expr,
|
||||
error::{AstError, AstResult, IntoErr, collect_recovered},
|
||||
parser::{consume_rule, listen_rule},
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
ast_ensure!(pair, Rule::function_decl, Rule::method, Rule::method_no_body => {
|
||||
let mut inner = pair.into_inner();
|
||||
let visibility = Visibility::try_from(&mut inner);
|
||||
let return_type = TypeExpr::try_from(inner.next().unwrap());
|
||||
let name = Identifier::try_from(inner.next().unwrap());
|
||||
|
||||
let generics = consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.map(|v| v.unwrap_or_default());
|
||||
|
||||
let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| {
|
||||
let mut param_inner = param.into_inner();
|
||||
let name = Pattern::Id(Identifier(String::from("self")));
|
||||
|
||||
let mutable = listen_rule(&mut param_inner, Rule::mutable);
|
||||
|
||||
let is_ref = listen_rule(&mut param_inner, Rule::deref_px);
|
||||
|
||||
VarDecl {
|
||||
mutable: mutable && !is_ref,
|
||||
name: name.clone(),
|
||||
type_: Some(TypeExpr(
|
||||
TypeExprKind::Path(Path(vec![Identifier("Self".to_string())])),
|
||||
if is_ref {
|
||||
vec![if mutable {
|
||||
TypePostfix::RefMut
|
||||
} else {
|
||||
TypePostfix::Ref
|
||||
}]
|
||||
} else {
|
||||
Vec::new()
|
||||
},
|
||||
)),
|
||||
}
|
||||
});
|
||||
|
||||
let params = consume_rule(&mut inner, Rule::param_list)
|
||||
.map({
|
||||
let self_param = self_param.clone();
|
||||
|params_pair| -> AstResult<'a, ParamList> {
|
||||
let mut params = ParamList::try_from(params_pair)?;
|
||||
if let Some(x) = self_param {
|
||||
params.0.insert(0, x);
|
||||
}
|
||||
Ok(params)
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| Ok(ParamList(self_param.into_iter().collect())));
|
||||
|
||||
let body = inner.next().map(Block::try_from).transpose();
|
||||
|
||||
ast_expr!(Self {
|
||||
visibility: visibility,
|
||||
return_type: return_type,
|
||||
name: name,
|
||||
generics: generics,
|
||||
params: params,
|
||||
body: body,
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ParamList {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
Ok(ParamList(collect_recovered(pair.into_inner()).get()?))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
error::{AstError, IntoErr, collect_recovered},
|
||||
parser::consume_rule,
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::impl_for_decl => Ok(ImplDecl {
|
||||
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.get()?
|
||||
.unwrap_or_default(),
|
||||
trait_: Some(inner.next().unwrap().try_into().get()?),
|
||||
target: inner.next().unwrap().try_into().get()?,
|
||||
methods: collect_recovered(inner).get()?,
|
||||
}),
|
||||
|
||||
Rule::impl_decl => Ok(ImplDecl {
|
||||
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.get()?
|
||||
.unwrap_or_default(),
|
||||
trait_: None,
|
||||
target: inner.next().unwrap().try_into().get()?,
|
||||
methods: collect_recovered(inner).get()?,
|
||||
}),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
pub mod attribute;
|
||||
pub mod class;
|
||||
pub mod enums;
|
||||
pub mod function;
|
||||
pub mod impl_decl;
|
||||
|
||||
use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_expr,
|
||||
error::{AstError, IntoErr, collect_recovered},
|
||||
parser::consume_rule,
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let attributes = collect_recovered(inner.next().unwrap().into_inner());
|
||||
|
||||
ast_expr!(TopLevel(
|
||||
inner.next().map(Spanned::try_from).unwrap(),
|
||||
attributes,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::import => ast_expr!(TopLevelKind::Import(
|
||||
Visibility::try_from(&mut inner),
|
||||
Path::try_from(inner.next().unwrap()),
|
||||
)),
|
||||
|
||||
Rule::function_decl => ast_expr!(TopLevelKind::FunctionDecl(pair.try_into())),
|
||||
|
||||
Rule::struct_decl => ast_expr!(TopLevelKind::StructDecl {
|
||||
visibility: Visibility::try_from(&mut inner),
|
||||
|
||||
name: inner.next().unwrap().try_into(),
|
||||
|
||||
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.map(|v| v.unwrap_or_default()),
|
||||
|
||||
fields: inner
|
||||
.next()
|
||||
.map(|pair| collect_recovered(pair.into_inner()))
|
||||
.transpose()
|
||||
.map(|v| v.unwrap_or_default()),
|
||||
}),
|
||||
|
||||
Rule::class_decl => ast_expr!(TopLevelKind::ClassDecl {
|
||||
visibility: Visibility::try_from(&mut inner),
|
||||
|
||||
name: inner.next().unwrap().try_into(),
|
||||
|
||||
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.map(|v| v.unwrap_or_default()),
|
||||
|
||||
fields: collect_recovered(inner.next().unwrap().into_inner()),
|
||||
|
||||
constructor: inner.next().unwrap().try_into(),
|
||||
|
||||
items: collect_recovered(inner),
|
||||
}),
|
||||
|
||||
Rule::enum_decl => ast_expr!(TopLevelKind::EnumDecl {
|
||||
visibility: Visibility::try_from(&mut inner),
|
||||
|
||||
name: inner.next().unwrap().try_into(),
|
||||
|
||||
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.map(|v| v.unwrap_or_default()),
|
||||
|
||||
fields: collect_recovered(inner),
|
||||
}),
|
||||
|
||||
Rule::mod_package => ast_expr!(TopLevelKind::Mod(
|
||||
Visibility::try_from(&mut inner),
|
||||
inner.next().unwrap().try_into(),
|
||||
)),
|
||||
|
||||
Rule::impl_for_decl | Rule::impl_decl => {
|
||||
ast_expr!(TopLevelKind::ImplDecl(pair.try_into()))
|
||||
}
|
||||
|
||||
Rule::trait_decl => ast_expr!(TopLevelKind::TraitDecl {
|
||||
visibility: Visibility::try_from(&mut inner),
|
||||
|
||||
name: inner.next().unwrap().try_into(),
|
||||
|
||||
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.map(|v| v.unwrap_or_default()),
|
||||
|
||||
requirements: consume_rule(&mut inner, Rule::trait_requirements)
|
||||
.map(|pair| collect_recovered(pair.into_inner()))
|
||||
.transpose()
|
||||
.map(|v| v.unwrap_or_default()),
|
||||
|
||||
items: collect_recovered(&mut inner),
|
||||
}),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
pub mod common;
|
||||
pub mod items;
|
||||
|
||||
use crate::{Rule, ast::Spanned, error::AstError};
|
||||
|
||||
pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool {
|
||||
let consumed = pairs
|
||||
.peek()
|
||||
.map(|p| p.as_rule() == rule)
|
||||
.unwrap_or_default();
|
||||
|
||||
if consumed {
|
||||
pairs.next();
|
||||
}
|
||||
|
||||
consumed
|
||||
}
|
||||
|
||||
pub fn consume_rule<'a>(
|
||||
pairs: &mut pest::iterators::Pairs<'a, Rule>,
|
||||
rule: Rule,
|
||||
) -> Option<pest::iterators::Pair<'a, Rule>> {
|
||||
let consumed = pairs
|
||||
.peek()
|
||||
.map(|p| p.as_rule() == rule)
|
||||
.unwrap_or_default();
|
||||
|
||||
if consumed { pairs.next() } else { None }
|
||||
}
|
||||
|
||||
impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>, Error = AstError<'a, T>>>
|
||||
TryFrom<pest::iterators::Pair<'a, Rule>> for Spanned<T>
|
||||
{
|
||||
type Error = AstError<'a, Self>;
|
||||
|
||||
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
|
||||
let span = pair.as_span().start_pos().line_col();
|
||||
|
||||
Ok(Self {
|
||||
line: span.0,
|
||||
column: span.1,
|
||||
item: pair.try_into().map_err(|err: AstError<'_, T>| AstError {
|
||||
span: err.span,
|
||||
error_code: err.error_code,
|
||||
error_message: err.error_message,
|
||||
recovered: err.recovered.map(|v| Spanned {
|
||||
line: span.0,
|
||||
column: span.1,
|
||||
item: v,
|
||||
}),
|
||||
})?,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
||||
|
||||
integer = @{ ASCII_DIGIT+ }
|
||||
|
||||
map = { "/*" ~ integer ~ ":" ~ integer ~ "*/" }
|
||||
|
||||
non_map = { (!map ~ ANY)+ }
|
||||
|
||||
mapping = { SOI ~ (map | non_map)* ~ EOI }
|
||||
@@ -0,0 +1,51 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use pest::Parser;
|
||||
use pest_derive::Parser;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "./src/rev_mapper/grammar.pest"]
|
||||
pub struct MistMapperParser;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct MistMap(pub usize, pub usize);
|
||||
|
||||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct RustMap(pub usize, pub usize);
|
||||
|
||||
pub fn get_mapping(input: &str) -> HashSet<(RustMap, MistMap)> {
|
||||
let mut mapping = HashSet::new();
|
||||
|
||||
let pairs = MistMapperParser::parse(Rule::mapping, input)
|
||||
.unwrap()
|
||||
.next()
|
||||
.unwrap()
|
||||
.into_inner();
|
||||
|
||||
for pair in pairs {
|
||||
if pair.as_rule() == Rule::map {
|
||||
let rs = pair.as_span().start_pos().line_col();
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
let (line, col) = (
|
||||
inner.next().unwrap().as_str().parse().unwrap(),
|
||||
inner.next().unwrap().as_str().parse().unwrap(),
|
||||
);
|
||||
|
||||
mapping.insert((RustMap(rs.0, rs.1), MistMap(line, col)));
|
||||
}
|
||||
}
|
||||
|
||||
mapping
|
||||
}
|
||||
|
||||
pub fn find_mapping(
|
||||
mapping: &HashSet<(RustMap, MistMap)>,
|
||||
target: &RustMap,
|
||||
) -> Option<(RustMap, MistMap)> {
|
||||
mapping
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|(rust, _)| rust <= target)
|
||||
.max_by_key(|(rust, _)| *rust)
|
||||
}
|
||||
Reference in New Issue
Block a user