Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of LocationSpan::todo #395

Merged
merged 8 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 63 additions & 20 deletions lib/shiika_ast/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,82 @@ impl Location {

/// Range in a source file (end-exclusive)
#[derive(Debug, PartialEq, Clone)]
pub struct LocationSpan {
pub filepath: Rc<PathBuf>,
pub begin: Location,
pub end: Location,
pub enum LocationSpan {
Empty,
Just {
filepath: Rc<PathBuf>,
begin: Location,
end: Location,
},
}

impl LocationSpan {
pub fn new(filepath: &Rc<PathBuf>, begin: Location, end: Location) -> LocationSpan {
LocationSpan {
if begin.pos > end.pos {
println!(
"[BUG] invalid LocationSpan pos (begin: {}, end: {})",
begin.pos, end.pos
);
return LocationSpan::internal();
}
LocationSpan::Just {
filepath: filepath.clone(),
begin,
end,
}
}

pub fn begin_end(&self) -> (Location, Location) {
(self.begin.clone(), self.end.clone())
pub fn merge(begin: &LocationSpan, end: &LocationSpan) -> LocationSpan {
match (begin, end) {
(LocationSpan::Empty, LocationSpan::Empty) => LocationSpan::Empty,
(LocationSpan::Just { .. }, LocationSpan::Empty) => begin.clone(),
(LocationSpan::Empty, LocationSpan::Just { .. }) => end.clone(),
(
LocationSpan::Just {
filepath, begin, ..
},
LocationSpan::Just {
filepath: filepath2,
end,
..
},
) if filepath == filepath2 => Self::new(&filepath, begin.clone(), end.clone()),
_ => {
println!(
"[BUG] invalid LocationSpan (begin: {:?}, end: {:?})",
begin, end
);
LocationSpan::Empty
}
}
}

pub fn get_begin(&self) -> Location {
match self {
LocationSpan::Just { begin, .. } => begin.clone(),
_ => panic!("get_end called on Empty"),
}
}

pub fn get_end(&self) -> Location {
match self {
LocationSpan::Just { end, .. } => end.clone(),
_ => panic!("get_end called on Empty"),
}
}

// fn begin_end(&self) -> (Location, Location) {
// (self.begin.clone(), self.end.clone())
// }

/// Denotes that this ast or hir does not correspond to any source text.
pub fn internal() -> LocationSpan {
LocationSpan::Empty
}

// Used as placeholder.
// TODO: remove this
pub fn todo() -> LocationSpan {
LocationSpan {
filepath: Rc::new(PathBuf::new()),
begin: Location {
line: 0,
col: 0,
pos: 0,
},
end: Location {
line: 0,
col: 0,
pos: 0,
},
}
LocationSpan::Empty
}
}
60 changes: 42 additions & 18 deletions lib/shiika_parser/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ impl AstBuilder {
}

pub fn logical_and(&self, left: AstExpression, right: AstExpression) -> AstExpression {
self.primary_expression(
left.locs.begin.clone(),
right.locs.end.clone(),
self.primary_expression_(
&left.locs.clone(),
&right.locs.clone(),
AstExpressionBody::LogicalAnd {
left: Box::new(left),
right: Box::new(right),
Expand All @@ -61,9 +61,9 @@ impl AstBuilder {
}

pub fn logical_or(&self, left: AstExpression, right: AstExpression) -> AstExpression {
self.primary_expression(
left.locs.begin.clone(),
right.locs.end.clone(),
self.primary_expression_(
&left.locs.clone(),
&right.locs.clone(),
AstExpressionBody::LogicalOr {
left: Box::new(left),
right: Box::new(right),
Expand Down Expand Up @@ -328,6 +328,19 @@ impl AstBuilder {
}
}

fn primary_expression_(
&self,
begin: &LocationSpan,
end: &LocationSpan,
body: AstExpressionBody,
) -> AstExpression {
AstExpression {
primary: true,
body,
locs: LocationSpan::merge(begin, end),
}
}

fn non_primary_expression(
&self,
begin: Location,
Expand All @@ -341,18 +354,29 @@ impl AstBuilder {
}
}

fn non_primary_expression_(
&self,
begin: &LocationSpan,
end: &LocationSpan,
body: AstExpressionBody,
) -> AstExpression {
AstExpression {
primary: false,
body,
locs: LocationSpan::merge(begin, end),
}
}

/// Create an expression of the form `left <op> right`
pub fn bin_op_expr(
&self,
left: AstExpression,
op: &str,
right: AstExpression,
) -> AstExpression {
let begin = left.locs.begin.clone();
let end = right.locs.end.clone();
self.non_primary_expression(
begin,
end,
self.non_primary_expression_(
&left.locs.clone(),
&right.locs.clone(),
AstExpressionBody::MethodCall(AstMethodCall {
receiver_expr: Some(Box::new(left)),
method_name: method_firstname(op),
Expand All @@ -366,8 +390,8 @@ impl AstBuilder {

/// Create an expression of the form `lhs = rhs`
pub fn assignment(&self, lhs: AstExpression, rhs: AstExpression) -> AstExpression {
let begin = lhs.locs.begin.clone();
let end = rhs.locs.end.clone();
let begin = &lhs.locs.clone();
let end = &rhs.locs.clone();
let body = match lhs.body {
AstExpressionBody::BareName(s) => AstExpressionBody::LVarAssign {
name: s,
Expand Down Expand Up @@ -396,7 +420,7 @@ impl AstBuilder {
}
_ => panic!("[BUG] unexpectd lhs: {:?}", lhs.body),
};
self.non_primary_expression(begin, end, body)
self.non_primary_expression_(begin, end, body)
}

/// Extend `foo.bar` to `foo.bar args`, or
Expand All @@ -408,8 +432,8 @@ impl AstBuilder {
args: Vec<AstExpression>,
has_block: bool,
) -> AstExpression {
let begin = expr.locs.begin.clone();
let end = args.last().unwrap().locs.end.clone();
let begin = &expr.locs;
let end = &args.last().unwrap().locs.clone();
match expr.body {
AstExpressionBody::MethodCall(x) => {
if !x.arg_exprs.is_empty() {
Expand All @@ -418,7 +442,7 @@ impl AstBuilder {
x.arg_exprs
);
}
self.non_primary_expression(
self.non_primary_expression_(
begin,
end,
AstExpressionBody::MethodCall(AstMethodCall {
Expand All @@ -428,7 +452,7 @@ impl AstBuilder {
}),
)
}
AstExpressionBody::BareName(s) => self.non_primary_expression(
AstExpressionBody::BareName(s) => self.non_primary_expression_(
begin,
end,
AstExpressionBody::MethodCall(AstMethodCall {
Expand Down
13 changes: 8 additions & 5 deletions lib/shiika_parser/src/definition_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,16 @@ impl<'a> Parser<'a> {
.iter()
.filter(|param| param.is_iparam)
.map(|param| {
let span = LocationSpan::todo();
let loc = Location {
pos: 0,
col: 0,
line: 0,
};
self.ast.ivar_assign(
param.name.clone(),
self.ast
.bare_name(&param.name, span.begin.clone(), span.end.clone()),
span.begin,
span.end,
self.ast.bare_name(&param.name, loc.clone(), loc.clone()),
loc.clone(),
loc.clone(),
)
})
.collect()
Expand Down
4 changes: 2 additions & 2 deletions lib/shiika_parser/src/expression_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ impl<'a> Parser<'a> {
self.parse_exprs(vec![Token::KwEnd, Token::KwElse, Token::KwElsif])?;
self.skip_wsn()?;
let cont = self._parse_if_expr(cond_expr2, then_exprs2, begin.clone())?;
let end = cont.locs.end.clone();
let end = cont.locs.get_end();
Ok(self
.ast
.if_expr(cond_expr, then_exprs, Some(vec![cont]), begin, end))
Expand Down Expand Up @@ -749,6 +749,7 @@ impl<'a> Parser<'a> {
fn parse_method_chain(&mut self, expr: AstExpression) -> Result<AstExpression, Error> {
self.lv += 1;
self.debug_log("parse_method_chain");
let begin = self.lexer.location();
// .
self.set_lexer_state(LexerState::MethodName);
assert!(self.consume(Token::Dot)?);
Expand Down Expand Up @@ -786,7 +787,6 @@ impl<'a> Parser<'a> {
};

self.lv -= 1;
let begin = expr.locs.begin.clone();
let end = self.lexer.location();
Ok(self.ast.method_call(
true,
Expand Down
Loading