Skip to content

Commit

Permalink
define type alias type Range = (Position, Position)
Browse files Browse the repository at this point in the history
  • Loading branch information
He1pa committed Aug 7, 2023
1 parent 7dbb090 commit cfb559e
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 81 deletions.
6 changes: 3 additions & 3 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use compiler_base_span::{Loc, Span};

use super::token;
use crate::{node_ref, pos::ContainsPos};
use kclvm_error::Position;
use kclvm_error::{diagnostic::Range, Position};

/// PosTuple denotes the tuple `(filename, line, column, end_line, end_column)`.
pub type PosTuple = (String, u64, u64, u64, u64);
Expand All @@ -62,8 +62,8 @@ impl Into<PosTuple> for Pos {
}
}

impl Into<(Position, Position)> for Pos {
fn into(self) -> (Position, Position) {
impl Into<Range> for Pos {
fn into(self) -> Range {
(
Position {
filename: self.0.clone(),
Expand Down
4 changes: 2 additions & 2 deletions kclvm/ast/src/pos.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use kclvm_error::Position;
use kclvm_error::{diagnostic::Range, Position};

use crate::ast;

Expand All @@ -9,7 +9,7 @@ pub trait ContainsPos {

pub trait GetPos {
/// Get start and end position from node.
fn get_span_pos(&self) -> (Position, Position) {
fn get_span_pos(&self) -> Range {
(self.get_pos(), self.get_end_pos())
}
/// Get start pos from node.
Expand Down
8 changes: 5 additions & 3 deletions kclvm/error/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl From<Loc> for Position {
}

impl Diagnostic {
pub fn new(level: Level, message: &str, range: (Position, Position)) -> Self {
pub fn new(level: Level, message: &str, range: Range) -> Self {
Diagnostic::new_with_code(level, message, None, range, None)
}

Expand All @@ -104,7 +104,7 @@ impl Diagnostic {
level: Level,
message: &str,
note: Option<&str>,
range: (Position, Position),
range: Range,
code: Option<DiagnosticId>,
) -> Self {
Diagnostic {
Expand All @@ -125,9 +125,11 @@ impl Diagnostic {
}
}

pub type Range = (Position, Position);

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Message {
pub range: (Position, Position),
pub range: Range,
pub style: Style,
pub message: String,
pub note: Option<String>,
Expand Down
4 changes: 2 additions & 2 deletions kclvm/error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub enum WarningKind {
/// let mut handler = Handler::default();
/// handler.add_warning(WarningKind::UnusedImportWarning, &[
/// Message {
/// pos: Position::dummy_pos(),
/// range: (Position::dummy_pos(), Position::dummy_pos()),
/// style: Style::LineAndColumn,
/// message: "Module 'a' imported but unused.".to_string(),
/// note: None,
Expand All @@ -168,7 +168,7 @@ impl std::fmt::Display for WarningKind {
/// let mut handler = Handler::default();
/// handler.add_warning(WarningKind::UnusedImportWarning, &[
/// Message {
/// pos: Position::dummy_pos(),
/// range: (Position::dummy_pos(), Position::dummy_pos()),
/// style: Style::LineAndColumn,
/// message: "Module 'a' imported but unused.".to_string(),
/// note: None,
Expand Down
13 changes: 7 additions & 6 deletions kclvm/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use compiler_base_error::{
};
use compiler_base_session::{Session, SessionDiagnostic};
use compiler_base_span::{span::new_byte_pos, Span};
use diagnostic::Range;
use indexmap::IndexSet;
use kclvm_runtime::PanicInfo;
use std::{any::Any, sync::Arc};
Expand Down Expand Up @@ -91,7 +92,7 @@ impl Handler {
}

/// Construct a parse error and put it into the handler diagnostic buffer
pub fn add_syntex_error(&mut self, msg: &str, range: (Position, Position)) -> &mut Self {
pub fn add_syntex_error(&mut self, msg: &str, range: Range) -> &mut Self {
let message = format!("Invalid syntax: {msg}");
let diag = Diagnostic::new_with_code(
Level::Error,
Expand All @@ -106,7 +107,7 @@ impl Handler {
}

/// Construct a type error and put it into the handler diagnostic buffer
pub fn add_type_error(&mut self, msg: &str, range: (Position, Position)) -> &mut Self {
pub fn add_type_error(&mut self, msg: &str, range: Range) -> &mut Self {
let diag = Diagnostic::new_with_code(
Level::Error,
msg,
Expand All @@ -120,7 +121,7 @@ impl Handler {
}

/// Construct a type error and put it into the handler diagnostic buffer
pub fn add_compile_error(&mut self, msg: &str, range: (Position, Position)) -> &mut Self {
pub fn add_compile_error(&mut self, msg: &str, range: Range) -> &mut Self {
let diag = Diagnostic::new_with_code(
Level::Error,
msg,
Expand All @@ -146,7 +147,7 @@ impl Handler {
/// let mut handler = Handler::default();
/// handler.add_error(ErrorKind::InvalidSyntax, &[
/// Message {
/// pos: Position::dummy_pos(),
/// range: (Position::dummy_pos(), Position::dummy_pos()),
/// style: Style::LineAndColumn,
/// message: "Invalid syntax: expected '+', got '-'".to_string(),
/// note: None,
Expand All @@ -170,7 +171,7 @@ impl Handler {
/// let mut handler = Handler::default();
/// handler.add_warning(WarningKind::UnusedImportWarning, &[
/// Message {
/// pos: Position::dummy_pos(),
/// range: (Position::dummy_pos(), Position::dummy_pos()),
/// style: Style::LineAndColumn,
/// message: "Module 'a' imported but unused.".to_string(),
/// note: None,
Expand Down Expand Up @@ -210,7 +211,7 @@ impl Handler {
/// ```
/// use kclvm_error::*;
/// let mut handler = Handler::default();
/// handler.add_diagnostic(Diagnostic::new_with_code(Level::Error, "error message", None, Position::dummy_pos(), Some(DiagnosticId::Error(E1001.kind))));
/// handler.add_diagnostic(Diagnostic::new_with_code(Level::Error, "error message", None, (Position::dummy_pos(), Position::dummy_pos()), Some(DiagnosticId::Error(E1001.kind))));
/// ```
#[inline]
pub fn add_diagnostic(&mut self, diagnostic: Diagnostic) -> &mut Self {
Expand Down
9 changes: 5 additions & 4 deletions kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use kclvm_config::modfile::{
KCL_MOD_FILE,
};
use kclvm_config::path::ModRelativePath;
use kclvm_error::{ErrorKind, Message, Position, Style};
use kclvm_error::diagnostic::Range;
use kclvm_error::{ErrorKind, Message, Style};
use kclvm_sema::plugin::PLUGIN_MODULE_PREFIX;
use kclvm_utils::path::PathPrefix;
use kclvm_utils::pkgpath::parse_external_pkg_name;
Expand Down Expand Up @@ -355,7 +356,7 @@ impl Loader {
self.sess.1.borrow_mut().add_error(
ErrorKind::CannotFindModule,
&[Message {
range: Into::<(Position, Position)>::into(pos),
range: Into::<Range>::into(pos),
style: Style::Line,
message: format!(
"the `{}` is found multiple times in the current package and vendor package",
Expand All @@ -374,7 +375,7 @@ impl Loader {
self.sess.1.borrow_mut().add_error(
ErrorKind::CannotFindModule,
&[Message {
range: Into::<(Position, Position)>::into(pos),
range: Into::<Range>::into(pos),
style: Style::Line,
message: format!("pkgpath {} not found in the program", pkg_path),
note: None,
Expand Down Expand Up @@ -544,7 +545,7 @@ impl Loader {
self.sess.1.borrow_mut().add_error(
ErrorKind::CannotFindModule,
&[Message {
range: Into::<(Position, Position)>::into(pos),
range: Into::<Range>::into(pos),
style: Style::Line,
message: format!("the plugin package `{}` is not found, please confirm if plugin mode is enabled", pkgpath),
note: None,
Expand Down
10 changes: 3 additions & 7 deletions kclvm/sema/src/resolver/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use crate::builtin::system_module::{get_system_module_members, UNITS, UNITS_NUMB
use crate::builtin::STRING_MEMBER_FUNCTIONS;
use crate::resolver::Resolver;
use crate::ty::{ModuleKind, Type, TypeKind};
use kclvm_error::diagnostic::Range;
use kclvm_error::*;

use super::node::ResolvedResult;

impl<'ctx> Resolver<'ctx> {
pub fn check_attr_ty(&mut self, attr_ty: &Type, range: (Position, Position)) {
pub fn check_attr_ty(&mut self, attr_ty: &Type, range: Range) {
if !attr_ty.is_any() && !attr_ty.is_key() {
self.handler.add_error(
ErrorKind::IllegalAttributeError,
Expand All @@ -26,12 +27,7 @@ impl<'ctx> Resolver<'ctx> {
}
}

pub fn load_attr(
&mut self,
obj: Rc<Type>,
attr: &str,
range: (Position, Position),
) -> ResolvedResult {
pub fn load_attr(&mut self, obj: Rc<Type>, attr: &str, range: Range) -> ResolvedResult {
let (result, return_ty) = match &obj.kind {
TypeKind::Any => (true, self.any_ty()),
TypeKind::None
Expand Down
12 changes: 4 additions & 8 deletions kclvm/sema/src/resolver/calculation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::rc::Rc;
use crate::resolver::Resolver;
use crate::ty::{has_any_type, is_upper_bound, sup, Type, TypeInferMethods, ZERO_LIT_TYPES};
use kclvm_ast::ast;
use kclvm_error::diagnostic::Range;
use kclvm_error::Position;

const DIV_OR_MOD_ZERO_MSG: &str = "integer division or modulo by zero";
Expand Down Expand Up @@ -56,7 +57,7 @@ impl<'ctx> Resolver<'ctx> {
left: Rc<Type>,
right: Rc<Type>,
op: &ast::BinOp,
range: (Position, Position),
range: Range,
) -> Rc<Type> {
let t1 = self
.ctx
Expand Down Expand Up @@ -213,12 +214,7 @@ impl<'ctx> Resolver<'ctx> {
/// - number unary negation (int, float)
/// ~ number unary bitwise inversion (int)
/// not x logical negation (any type)
pub fn unary(
&mut self,
ty: Rc<Type>,
op: &ast::UnaryOp,
range: (Position, Position),
) -> Rc<Type> {
pub fn unary(&mut self, ty: Rc<Type>, op: &ast::UnaryOp, range: Range) -> Rc<Type> {
if has_any_type(&[ty.clone()]) {
return self.any_ty();
}
Expand Down Expand Up @@ -259,7 +255,7 @@ impl<'ctx> Resolver<'ctx> {
left: Rc<Type>,
right: Rc<Type>,
op: &ast::CmpOp,
range: (Position, Position),
range: Range,
) -> Rc<Type> {
let t1 = self.ctx.ty_ctx.literal_union_type_to_variable_type(left);
let t2 = self.ctx.ty_ctx.literal_union_type_to_variable_type(right);
Expand Down
9 changes: 2 additions & 7 deletions kclvm/sema/src/resolver/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::ty::SchemaType;
use crate::ty::{Type, TypeKind};
use kclvm_ast::ast;
use kclvm_ast::pos::GetPos;
use kclvm_error::{ErrorKind, Message, Position, Style};
use kclvm_error::{diagnostic::Range, ErrorKind, Message, Position, Style};

/// Config Expr type check state.
///
Expand Down Expand Up @@ -314,12 +314,7 @@ impl<'ctx> Resolver<'ctx> {
}

/// Check config attr has been defined.
pub(crate) fn check_config_attr(
&mut self,
attr: &str,
range: &(Position, Position),
schema_ty: &SchemaType,
) {
pub(crate) fn check_config_attr(&mut self, attr: &str, range: &Range, schema_ty: &SchemaType) {
let runtime_type = kclvm_runtime::schema_runtime_type(&schema_ty.name, &schema_ty.pkgpath);
match self.ctx.schema_mapping.get(&runtime_type) {
Some(schema_mapping_ty) => {
Expand Down
4 changes: 2 additions & 2 deletions kclvm/sema/src/resolver/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::resolver::Resolver;
use crate::ty::{sup, Type, TypeKind};
use kclvm_ast::ast;
use kclvm_ast::pos::GetPos;
use kclvm_error::Position;
use kclvm_error::diagnostic::Range;

impl<'ctx> Resolver<'ctx> {
/// Do loop type check including quant and comp for expression.
Expand All @@ -14,7 +14,7 @@ impl<'ctx> Resolver<'ctx> {
first_var_name: Option<String>,
second_var_name: Option<String>,
iter_ty: Rc<Type>,
iter_range: (Position, Position),
iter_range: Range,
) {
let types = match &iter_ty.kind {
TypeKind::Union(types) => types.clone(),
Expand Down
3 changes: 2 additions & 1 deletion kclvm/sema/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod var;
mod tests;

use indexmap::IndexMap;
use kclvm_error::diagnostic::Range;
use std::{cell::RefCell, rc::Rc};

use crate::lint::{CombinedLintPass, Linter};
Expand Down Expand Up @@ -118,7 +119,7 @@ pub struct Context {
/// Import pkgpath and name
pub import_names: IndexMap<String, IndexMap<String, String>>,
/// Global names at top level of the program.
pub global_names: IndexMap<String, IndexMap<String, (Position, Position)>>,
pub global_names: IndexMap<String, IndexMap<String, Range>>,
/// Are we resolving the left value.
pub l_value: bool,
/// Are we resolving the statement start position.
Expand Down
7 changes: 4 additions & 3 deletions kclvm/sema/src/resolver/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::bail;
use compiler_base_session::Session;
use indexmap::{IndexMap, IndexSet};
use kclvm_ast::{ast, MAIN_PKG};
use kclvm_error::diagnostic::Range;
use kclvm_error::{Handler, Level};
use std::sync::Arc;
use std::{
Expand Down Expand Up @@ -50,7 +51,7 @@ impl ContainsPos for ScopeObject {
}

impl GetPos for ScopeObject {
fn get_span_pos(&self) -> (Position, Position) {
fn get_span_pos(&self) -> Range {
(self.start.clone(), self.end.clone())
}
fn get_pos(&self) -> Position {
Expand Down Expand Up @@ -399,7 +400,7 @@ impl<'ctx> Resolver<'ctx> {

/// Lookup type from the scope by name, if not found, emit a compile error and
/// return the any type.
pub fn lookup_type_from_scope(&mut self, name: &str, range: (Position, Position)) -> Rc<Type> {
pub fn lookup_type_from_scope(&mut self, name: &str, range: Range) -> Rc<Type> {
match self.find_type_in_scope(name) {
Some(ty) => ty,
None => {
Expand All @@ -413,7 +414,7 @@ impl<'ctx> Resolver<'ctx> {
}

/// Set type to the scope exited object, if not found, emit a compile error.
pub fn set_type_to_scope(&mut self, name: &str, ty: Rc<Type>, range: (Position, Position)) {
pub fn set_type_to_scope(&mut self, name: &str, ty: Rc<Type>, range: Range) {
let mut scope = self.scope.borrow_mut();
match scope.elems.get_mut(name) {
Some(obj) => {
Expand Down
Loading

0 comments on commit cfb559e

Please sign in to comment.