Refactored parser
This commit is contained in:
+57
-73
@@ -28,42 +28,45 @@ pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
|
|||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
||||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::type_expr => {
|
let mut inner = pair.into_inner();
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
TypeExpr(
|
match rule {
|
||||||
TypeExprKind::from(inner.next().unwrap()),
|
Rule::type_expr => TypeExpr(
|
||||||
inner.map(TypePostfix::from).collect(),
|
TypeExprKind::from(inner.next().unwrap()),
|
||||||
)
|
inner.map(TypePostfix::from).collect(),
|
||||||
}
|
),
|
||||||
_ => unimplemented!("{pair:#?}"),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for TypePostfix {
|
impl From<pest::iterators::Pair<'_, Rule>> for TypePostfix {
|
||||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
|
let inner = pair.into_inner();
|
||||||
|
|
||||||
|
match rule {
|
||||||
Rule::ref_type => {
|
Rule::ref_type => {
|
||||||
if pair.into_inner().peek().is_some() {
|
if inner.peek().is_some() {
|
||||||
TypePostfix::RefMut
|
TypePostfix::RefMut
|
||||||
} else {
|
} else {
|
||||||
TypePostfix::Ref
|
TypePostfix::Ref
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unimplemented!("{pair:#?}"),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for TypeExprKind {
|
impl From<pest::iterators::Pair<'_, Rule>> for TypeExprKind {
|
||||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::tuple_type => {
|
let mut inner = pair.into_inner();
|
||||||
TypeExprKind::Tuple(pair.into_inner().map(TypeExpr::from).collect())
|
|
||||||
}
|
match rule {
|
||||||
|
Rule::tuple_type => TypeExprKind::Tuple(inner.map(TypeExpr::from).collect()),
|
||||||
Rule::path_type => {
|
Rule::path_type => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
let path = StaticPath::from(inner.next().unwrap());
|
let path = StaticPath::from(inner.next().unwrap());
|
||||||
let params = inner.map(TypeExpr::from).collect::<Vec<_>>();
|
let params = inner.map(TypeExpr::from).collect::<Vec<_>>();
|
||||||
|
|
||||||
@@ -73,7 +76,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TypeExprKind {
|
|||||||
TypeExprKind::PathParams(path, params)
|
TypeExprKind::PathParams(path, params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unimplemented!("{pair:#?}"),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,23 +116,20 @@ impl From<pest::iterators::Pair<'_, Rule>> for FieldList {
|
|||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for ParamList {
|
impl From<pest::iterators::Pair<'_, Rule>> for ParamList {
|
||||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
let params = pair.into_inner().map(VarDecl::from).collect();
|
ParamList(pair.into_inner().map(VarDecl::from).collect())
|
||||||
|
|
||||||
ParamList(params)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
fn try_from(pair: pest::iterators::Pair<Rule>) -> Result<Self, ()> {
|
fn try_from(pair: pest::iterators::Pair<Rule>) -> Result<Self, ()> {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::import => Ok(TopLevel::Include(StaticPath::from(
|
let mut inner = pair.into_inner();
|
||||||
pair.into_inner().next().unwrap(),
|
|
||||||
))),
|
match rule {
|
||||||
|
Rule::import => Ok(TopLevel::Include(StaticPath::from(inner.next().unwrap()))),
|
||||||
|
|
||||||
Rule::function_decl => {
|
Rule::function_decl => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
|
|
||||||
let export = if let Some(first) = inner.peek() {
|
let export = if let Some(first) = inner.peek() {
|
||||||
if first.as_rule() == Rule::export {
|
if first.as_rule() == Rule::export {
|
||||||
inner.next();
|
inner.next();
|
||||||
@@ -162,7 +162,6 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Rule::struct_decl => {
|
Rule::struct_decl => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
let export = if let Some(first) = inner.peek() {
|
let export = if let Some(first) = inner.peek() {
|
||||||
if first.as_rule() == Rule::export {
|
if first.as_rule() == Rule::export {
|
||||||
inner.next();
|
inner.next();
|
||||||
@@ -185,7 +184,7 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Rule::EOI => Err(()),
|
Rule::EOI => Err(()),
|
||||||
_ => unimplemented!("TopLevel parsing not implemented yet {:?}", pair.as_rule()),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -208,24 +207,19 @@ impl From<pest::iterators::Pair<'_, Rule>> for Block {
|
|||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
||||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::statement => {
|
let mut inner = pair.clone().into_inner();
|
||||||
let inner = pair.into_inner().next().unwrap();
|
|
||||||
Statement::from(inner)
|
|
||||||
}
|
|
||||||
|
|
||||||
Rule::expr_stmt => {
|
match rule {
|
||||||
let expr_pair = pair.into_inner().next().unwrap();
|
Rule::statement => Statement::from(inner.next().unwrap()),
|
||||||
Statement::Expression(Expression::from(expr_pair))
|
|
||||||
}
|
|
||||||
|
|
||||||
Rule::block => Statement::Block(Block::from(pair.into_inner().next().unwrap())),
|
Rule::expr_stmt => Statement::Expression(Expression::from(inner.next().unwrap())),
|
||||||
|
|
||||||
|
Rule::block => Statement::Block(Block::from(inner.next().unwrap())),
|
||||||
|
|
||||||
Rule::var_decl_statement => Statement::VarDecl(VarDeclStmt::from(pair)),
|
Rule::var_decl_statement => Statement::VarDecl(VarDeclStmt::from(pair)),
|
||||||
|
|
||||||
Rule::return_stmt => {
|
Rule::return_stmt => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
|
|
||||||
let expr = inner.next().map(Expression::from);
|
let expr = inner.next().map(Expression::from);
|
||||||
|
|
||||||
Statement::Return(expr)
|
Statement::Return(expr)
|
||||||
@@ -236,8 +230,6 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
|||||||
Rule::continue_stmt => Statement::Continue,
|
Rule::continue_stmt => Statement::Continue,
|
||||||
|
|
||||||
Rule::if_stmt => {
|
Rule::if_stmt => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
|
|
||||||
let condition = Expression::from(inner.next().unwrap());
|
let condition = Expression::from(inner.next().unwrap());
|
||||||
let then_branch = Statement::from(inner.next().unwrap());
|
let then_branch = Statement::from(inner.next().unwrap());
|
||||||
|
|
||||||
@@ -251,8 +243,6 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Rule::while_stmt => {
|
Rule::while_stmt => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
|
|
||||||
let condition = Expression::from(inner.next().unwrap());
|
let condition = Expression::from(inner.next().unwrap());
|
||||||
let body = Statement::from(inner.next().unwrap());
|
let body = Statement::from(inner.next().unwrap());
|
||||||
|
|
||||||
@@ -262,28 +252,23 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule::assign_statement => {
|
Rule::assign_statement => Statement::VarAssign(VarAssignStmt {
|
||||||
let mut inner = pair.into_inner();
|
target: Expression::from(inner.next().unwrap()),
|
||||||
|
value: Expression::from(inner.next().unwrap()),
|
||||||
|
}),
|
||||||
|
|
||||||
Statement::VarAssign(VarAssignStmt {
|
_ => unimplemented!("{rule:#?}"),
|
||||||
target: Expression::from(inner.next().unwrap()),
|
|
||||||
value: Expression::from(inner.next().unwrap()),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => unimplemented!(
|
|
||||||
"Statement parsing not implemented yet: {:?}",
|
|
||||||
pair.as_rule()
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for Expression {
|
impl From<pest::iterators::Pair<'_, Rule>> for Expression {
|
||||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
|
let mut inner = pair.clone().into_inner();
|
||||||
|
|
||||||
|
match rule {
|
||||||
Rule::expr => {
|
Rule::expr => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
let mut prefixes = Vec::new();
|
let mut prefixes = Vec::new();
|
||||||
|
|
||||||
while inner
|
while inner
|
||||||
@@ -306,7 +291,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Expression {
|
|||||||
exp
|
exp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Rule::primary => Expression::from(pair.into_inner().next().unwrap()),
|
Rule::primary => Expression::from(inner.next().unwrap()),
|
||||||
Rule::static_path => Expression::Path(StaticPath::from(pair)),
|
Rule::static_path => Expression::Path(StaticPath::from(pair)),
|
||||||
Rule::integer => {
|
Rule::integer => {
|
||||||
let value = pair.as_str().parse::<i64>().unwrap();
|
let value = pair.as_str().parse::<i64>().unwrap();
|
||||||
@@ -325,10 +310,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Expression {
|
|||||||
Expression::StringLiteral(inner_str.to_string())
|
Expression::StringLiteral(inner_str.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => unimplemented!(
|
_ => unimplemented!("{rule:#?}"),
|
||||||
"Expression parsing not implemented yet {:?}",
|
|
||||||
pair.as_rule()
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -347,18 +329,21 @@ impl From<pest::iterators::Pair<'_, Rule>> for Prefix {
|
|||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
||||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::postfix => Postfix::from(pair.into_inner().next().unwrap()),
|
let mut inner = pair.into_inner();
|
||||||
|
|
||||||
|
match rule {
|
||||||
|
Rule::postfix => Postfix::from(inner.next().unwrap()),
|
||||||
|
|
||||||
Rule::field_px => {
|
Rule::field_px => {
|
||||||
let field_name = pair.into_inner().next().unwrap().as_str().to_string();
|
let field_name = inner.next().unwrap().as_str().to_string();
|
||||||
Postfix::FieldAccess(field_name)
|
Postfix::FieldAccess(field_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule::call_px => Postfix::Call(pair.into_inner().map(Expression::from).collect()),
|
Rule::call_px => Postfix::Call(inner.map(Expression::from).collect()),
|
||||||
|
|
||||||
Rule::struct_px => Postfix::StructCall(
|
Rule::struct_px => Postfix::StructCall(
|
||||||
pair.into_inner()
|
inner
|
||||||
.map(|p| {
|
.map(|p| {
|
||||||
let mut pi = p.into_inner();
|
let mut pi = p.into_inner();
|
||||||
(
|
(
|
||||||
@@ -369,10 +354,9 @@ impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
|||||||
.collect(),
|
.collect(),
|
||||||
),
|
),
|
||||||
|
|
||||||
Rule::index_px => Postfix::Index(Expression::from(pair.into_inner().next().unwrap())),
|
Rule::index_px => Postfix::Index(Expression::from(inner.next().unwrap())),
|
||||||
|
|
||||||
Rule::binary_px => {
|
Rule::binary_px => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
let op_pair = inner.next().unwrap();
|
let op_pair = inner.next().unwrap();
|
||||||
let op = match op_pair.as_str() {
|
let op = match op_pair.as_str() {
|
||||||
"+" => BinaryOp::Plus,
|
"+" => BinaryOp::Plus,
|
||||||
@@ -394,9 +378,9 @@ impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
|||||||
Postfix::Binary(op, Expression::from(inner.next().unwrap()))
|
Postfix::Binary(op, Expression::from(inner.next().unwrap()))
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule::macro_call_px => Postfix::MacroCall(pair.into_inner().as_str().to_string()),
|
Rule::macro_call_px => Postfix::MacroCall(inner.as_str().to_string()),
|
||||||
|
|
||||||
_ => unimplemented!("Postfix parsing not implemented yet {:?}", pair.as_rule()),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user