add file name to AST node location
This commit is contained in:
parent
1bd966965e
commit
88f0da7bdd
|
@ -97,10 +97,10 @@ impl Nac3 {
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let source = fs::read_to_string(source_file).map_err(|e| {
|
let source = fs::read_to_string(&source_file).map_err(|e| {
|
||||||
exceptions::PyIOError::new_err(format!("failed to read input file: {}", e))
|
exceptions::PyIOError::new_err(format!("failed to read input file: {}", e))
|
||||||
})?;
|
})?;
|
||||||
let parser_result = parser::parse_program(&source)
|
let parser_result = parser::parse_program(&source, source_file.into())
|
||||||
.map_err(|e| exceptions::PySyntaxError::new_err(format!("parse error: {}", e)))?;
|
.map_err(|e| exceptions::PySyntaxError::new_err(format!("parse error: {}", e)))?;
|
||||||
|
|
||||||
for mut stmt in parser_result.into_iter() {
|
for mut stmt in parser_result.into_iter() {
|
||||||
|
@ -476,7 +476,7 @@ impl Nac3 {
|
||||||
arg_names.join(", ")
|
arg_names.join(", ")
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
let mut synthesized = parse_program(&synthesized).unwrap();
|
let mut synthesized = parse_program(&synthesized, Default::default()).unwrap();
|
||||||
let resolver = Arc::new(Resolver(Arc::new(InnerResolver {
|
let resolver = Arc::new(Resolver(Arc::new(InnerResolver {
|
||||||
id_to_type: self.builtins_ty.clone().into(),
|
id_to_type: self.builtins_ty.clone().into(),
|
||||||
id_to_def: self.builtins_def.clone().into(),
|
id_to_def: self.builtins_def.clone().into(),
|
||||||
|
|
|
@ -9,6 +9,6 @@ mod impls;
|
||||||
mod location;
|
mod location;
|
||||||
|
|
||||||
pub use ast_gen::*;
|
pub use ast_gen::*;
|
||||||
pub use location::Location;
|
pub use location::{Location, FileName};
|
||||||
|
|
||||||
pub type Suite<U = ()> = Vec<Stmt<U>>;
|
pub type Suite<U = ()> = Vec<Stmt<U>>;
|
||||||
|
|
|
@ -1,17 +1,32 @@
|
||||||
//! Datatypes to support source location information.
|
//! Datatypes to support source location information.
|
||||||
|
use crate::ast_gen::StrRef;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
pub struct FileName(StrRef);
|
||||||
|
impl Default for FileName {
|
||||||
|
fn default() -> Self {
|
||||||
|
FileName("unknown".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for FileName {
|
||||||
|
fn from(s: String) -> Self {
|
||||||
|
FileName(s.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A location somewhere in the sourcecode.
|
/// A location somewhere in the sourcecode.
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||||
pub struct Location {
|
pub struct Location {
|
||||||
row: usize,
|
row: usize,
|
||||||
column: usize,
|
column: usize,
|
||||||
|
file: FileName
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Location {
|
impl fmt::Display for Location {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "line {} column {}", self.row, self.column)
|
write!(f, "{}: line {} column {}", self.file.0, self.row, self.column)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,8 +62,8 @@ impl Location {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Location {
|
impl Location {
|
||||||
pub fn new(row: usize, column: usize) -> Self {
|
pub fn new(row: usize, column: usize, file: FileName) -> Self {
|
||||||
Location { row, column }
|
Location { row, column, file }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn row(&self) -> usize {
|
pub fn row(&self) -> usize {
|
||||||
|
|
|
@ -163,9 +163,8 @@ impl TopLevelComposer {
|
||||||
ast::StmtKind::ClassDef { name: class_name, body, .. } => {
|
ast::StmtKind::ClassDef { name: class_name, body, .. } => {
|
||||||
if self.keyword_list.contains(class_name) {
|
if self.keyword_list.contains(class_name) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"cannot use keyword `{}` as a class name ({} at {})",
|
"cannot use keyword `{}` as a class name (at {})",
|
||||||
class_name,
|
class_name,
|
||||||
mod_path,
|
|
||||||
ast.location
|
ast.location
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -175,9 +174,8 @@ impl TopLevelComposer {
|
||||||
n
|
n
|
||||||
}) {
|
}) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"duplicate definition of class `{}` ({} at {})",
|
"duplicate definition of class `{}` (at {})",
|
||||||
class_name,
|
class_name,
|
||||||
mod_path,
|
|
||||||
ast.location
|
ast.location
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -223,9 +221,8 @@ impl TopLevelComposer {
|
||||||
}
|
}
|
||||||
if self.keyword_list.contains(method_name) {
|
if self.keyword_list.contains(method_name) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"cannot use keyword `{}` as a method name ({} at {})",
|
"cannot use keyword `{}` as a method name (at {})",
|
||||||
method_name,
|
method_name,
|
||||||
mod_path,
|
|
||||||
b.location
|
b.location
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -242,9 +239,8 @@ impl TopLevelComposer {
|
||||||
};
|
};
|
||||||
if !defined_names.insert(global_class_method_name.clone()) {
|
if !defined_names.insert(global_class_method_name.clone()) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"class method `{}` defined twice ({} at {})",
|
"class method `{}` defined twice (at {})",
|
||||||
&global_class_method_name[mod_path.len()..],
|
&global_class_method_name[mod_path.len()..],
|
||||||
mod_path,
|
|
||||||
b.location
|
b.location
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -309,9 +305,8 @@ impl TopLevelComposer {
|
||||||
};
|
};
|
||||||
if !defined_names.insert(global_fun_name.clone()) {
|
if !defined_names.insert(global_fun_name.clone()) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"top level function `{}` defined twice ({} at {})",
|
"top level function `{}` defined twice (at {})",
|
||||||
&global_fun_name[mod_path.len()..],
|
&global_fun_name[mod_path.len()..],
|
||||||
mod_path,
|
|
||||||
ast.location
|
ast.location
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -340,8 +335,7 @@ impl TopLevelComposer {
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => Err(format!(
|
_ => Err(format!(
|
||||||
"registrations of constructs other than top level classes/functions are not supported ({} at {})",
|
"registrations of constructs other than top level classes/functions are not supported (at {})",
|
||||||
mod_path,
|
|
||||||
ast.location
|
ast.location
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
|
@ -794,7 +788,7 @@ impl TopLevelComposer {
|
||||||
&type_annotation,
|
&type_annotation,
|
||||||
primitives_store,
|
primitives_store,
|
||||||
unifier
|
unifier
|
||||||
).map_err(|err| format!("{} at {}", err, x.location))?;
|
).map_err(|err| format!("{} (at {})", err, x.location))?;
|
||||||
v
|
v
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -865,7 +859,7 @@ impl TopLevelComposer {
|
||||||
));
|
));
|
||||||
unifier
|
unifier
|
||||||
.unify(*dummy_ty, function_ty)
|
.unify(*dummy_ty, function_ty)
|
||||||
.map_err(|old| format!("{} at {}", old, function_ast.location))?;
|
.map_err(|old| format!("{} (at {})", old, function_ast.location))?;
|
||||||
} else {
|
} else {
|
||||||
unreachable!("must be both function");
|
unreachable!("must be both function");
|
||||||
}
|
}
|
||||||
|
@ -1028,7 +1022,7 @@ impl TopLevelComposer {
|
||||||
Some({
|
Some({
|
||||||
let v = Self::parse_parameter_default_value(default, class_resolver)?;
|
let v = Self::parse_parameter_default_value(default, class_resolver)?;
|
||||||
Self::check_default_param_type(&v, &type_ann, primitives, unifier)
|
Self::check_default_param_type(&v, &type_ann, primitives, unifier)
|
||||||
.map_err(|err| format!("{} at {}", err, x.location))?;
|
.map_err(|err| format!("{} (at {})", err, x.location))?;
|
||||||
v
|
v
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1351,7 +1345,7 @@ impl TopLevelComposer {
|
||||||
));
|
));
|
||||||
self.unifier
|
self.unifier
|
||||||
.unify(constructor.unwrap(), contor_type)
|
.unify(constructor.unwrap(), contor_type)
|
||||||
.map_err(|old| format!("{} at {}", old, ast.as_ref().unwrap().location))?;
|
.map_err(|old| format!("{} (at {})", old, ast.as_ref().unwrap().location))?;
|
||||||
|
|
||||||
// class field instantiation check
|
// class field instantiation check
|
||||||
if let (Some(init_id), false) = (init_id, fields.is_empty()) {
|
if let (Some(init_id), false) = (init_id, fields.is_empty()) {
|
||||||
|
@ -1568,7 +1562,7 @@ impl TopLevelComposer {
|
||||||
&mut |id| format!("tvar{}", id),
|
&mut |id| format!("tvar{}", id),
|
||||||
);
|
);
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"expected return type of `{}` in function `{}` at {}",
|
"expected return type of `{}` in function `{}` (at {})",
|
||||||
ret_str,
|
ret_str,
|
||||||
name,
|
name,
|
||||||
ast.as_ref().unwrap().location
|
ast.as_ref().unwrap().location
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
//! This means source code is translated into separate tokens.
|
//! This means source code is translated into separate tokens.
|
||||||
|
|
||||||
pub use super::token::Tok;
|
pub use super::token::Tok;
|
||||||
use crate::ast::Location;
|
use crate::ast::{Location, FileName};
|
||||||
use crate::error::{LexicalError, LexicalErrorType};
|
use crate::error::{LexicalError, LexicalErrorType};
|
||||||
use std::char;
|
use std::char;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
@ -111,8 +111,8 @@ pub type Spanned = (Location, Tok, Location);
|
||||||
pub type LexResult = Result<Spanned, LexicalError>;
|
pub type LexResult = Result<Spanned, LexicalError>;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn make_tokenizer(source: &str) -> impl Iterator<Item = LexResult> + '_ {
|
pub fn make_tokenizer(source: &str, file: FileName) -> impl Iterator<Item = LexResult> + '_ {
|
||||||
make_tokenizer_located(source, Location::new(0, 0))
|
make_tokenizer_located(source, Location::new(0, 0, file))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_tokenizer_located(
|
pub fn make_tokenizer_located(
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
use crate::ast;
|
use crate::ast::{self, FileName};
|
||||||
use crate::error::ParseError;
|
use crate::error::ParseError;
|
||||||
use crate::lexer;
|
use crate::lexer;
|
||||||
pub use crate::mode::Mode;
|
pub use crate::mode::Mode;
|
||||||
|
@ -20,8 +20,8 @@ use crate::python;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/// Parse a full python program, containing usually multiple lines.
|
/// Parse a full python program, containing usually multiple lines.
|
||||||
pub fn parse_program(source: &str) -> Result<ast::Suite, ParseError> {
|
pub fn parse_program(source: &str, file: FileName) -> Result<ast::Suite, ParseError> {
|
||||||
parse(source, Mode::Module).map(|top| match top {
|
parse(source, Mode::Module, file).map(|top| match top {
|
||||||
ast::Mod::Module { body, .. } => body,
|
ast::Mod::Module { body, .. } => body,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
|
@ -63,15 +63,15 @@ pub fn parse_program(source: &str) -> Result<ast::Suite, ParseError> {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse_expression(source: &str) -> Result<ast::Expr, ParseError> {
|
pub fn parse_expression(source: &str) -> Result<ast::Expr, ParseError> {
|
||||||
parse(source, Mode::Expression).map(|top| match top {
|
parse(source, Mode::Expression, Default::default()).map(|top| match top {
|
||||||
ast::Mod::Expression { body } => *body,
|
ast::Mod::Expression { body } => *body,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse a given source code
|
// Parse a given source code
|
||||||
pub fn parse(source: &str, mode: Mode) -> Result<ast::Mod, ParseError> {
|
pub fn parse(source: &str, mode: Mode, file: FileName) -> Result<ast::Mod, ParseError> {
|
||||||
let lxr = lexer::make_tokenizer(source);
|
let lxr = lexer::make_tokenizer(source, file);
|
||||||
let marker_token = (Default::default(), mode.to_marker(), Default::default());
|
let marker_token = (Default::default(), mode.to_marker(), Default::default());
|
||||||
let tokenizer = iter::once(Ok(marker_token)).chain(lxr);
|
let tokenizer = iter::once(Ok(marker_token)).chain(lxr);
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ fn main() {
|
||||||
|
|
||||||
Target::initialize_all(&InitializationConfig::default());
|
Target::initialize_all(&InitializationConfig::default());
|
||||||
|
|
||||||
let program = match fs::read_to_string(demo_name + ".py") {
|
let program = match fs::read_to_string(demo_name.clone() + ".py") {
|
||||||
Ok(program) => program,
|
Ok(program) => program,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!("Cannot open input file: {}", err);
|
println!("Cannot open input file: {}", err);
|
||||||
|
@ -64,7 +64,7 @@ fn main() {
|
||||||
setup_time.duration_since(start).unwrap().as_millis()
|
setup_time.duration_since(start).unwrap().as_millis()
|
||||||
);
|
);
|
||||||
|
|
||||||
let parser_result = parser::parse_program(&program).unwrap();
|
let parser_result = parser::parse_program(&program, format!("{}.py", demo_name).into()).unwrap();
|
||||||
let parse_time = SystemTime::now();
|
let parse_time = SystemTime::now();
|
||||||
println!(
|
println!(
|
||||||
"parse time: {}ms",
|
"parse time: {}ms",
|
||||||
|
|
Loading…
Reference in New Issue