-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ pub mod literal; | |
pub mod scope; | ||
pub mod interface; | ||
pub mod object; | ||
pub mod path; | ||
mod parser; | ||
pub use parser::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use rustpython_parser::ast::{ExprAttribute, Expr, Identifier, ExprName}; | ||
|
||
use crate::{prelude::*, identifier::IdentifierParser}; | ||
|
||
use ligen::{ir::Path, parser::ParserConfig}; | ||
|
||
#[derive(Default)] | ||
pub struct PathParser { | ||
identifier_parser: IdentifierParser, | ||
} | ||
|
||
impl Parser<&ExprAttribute> for PathParser { | ||
type Output = Path; | ||
fn parse(&self, input: &ExprAttribute, config: &ParserConfig) -> Result<Self::Output> { | ||
Ok(self.parse(&input.attr, config)?.join(self.parse(&*input.value, config)?)) | ||
} | ||
} | ||
|
||
impl Parser<&ExprName> for PathParser { | ||
type Output = Path; | ||
fn parse(&self, input: &ExprName, config: &ParserConfig) -> Result<Self::Output> { | ||
self.parse(&input.id, config) | ||
} | ||
|
||
} | ||
|
||
impl Parser<&Identifier> for PathParser { | ||
type Output = Path; | ||
fn parse(&self, input: &Identifier, config: &ParserConfig) -> Result<Self::Output> { | ||
let identifier = self.identifier_parser.parse(input.as_str(), config)?; | ||
Ok(Path::from(identifier)) | ||
} | ||
} | ||
|
||
impl Parser<&Expr> for PathParser { | ||
type Output = Path; | ||
fn parse(&self, input: &Expr, config: &ParserConfig) -> Result<Self::Output> { | ||
match input { | ||
Expr::Attribute(attribute) => self.parse(attribute, config), | ||
Expr::Name(name) => self.parse(name, config), | ||
_ => Err(Error::Message(format!("Failed to parse path from {:?}", input))), | ||
} | ||
} | ||
} |