fix: changed span to range because generated spans make no sense in the concrete tree

This commit is contained in:
Felipe g 2022-10-09 19:35:50 -03:00
parent dbf740ff5b
commit 921e70d668
15 changed files with 215 additions and 216 deletions

View File

@ -8,3 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
kind-span = { path = "../kind-span" } kind-span = { path = "../kind-span" }
kind-tree = { path = "../kind-tree" } kind-tree = { path = "../kind-tree" }
kind-report = { path = "../kind-report" }

View File

@ -1,4 +1,5 @@
use kind_span::Span; use kind_report::data::DiagnosticFrame;
use kind_span::Range;
use crate::lexer::tokens::Token; use crate::lexer::tokens::Token;
@ -12,10 +13,16 @@ pub enum EncodeSequence {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum SyntaxError { pub enum SyntaxError {
UnfinishedString(Span), UnfinishedString(Range),
UnfinishedComment(Span), UnfinishedComment(Range),
InvalidEscapeSequence(EncodeSequence, Span), InvalidEscapeSequence(EncodeSequence, Range),
InvalidNumberRepresentation(EncodeSequence, Span), InvalidNumberRepresentation(EncodeSequence, Range),
UnexpectedChar(char, Span), UnexpectedChar(char, Range),
UnexpectedToken(Token, Span, Vec<Token>), UnexpectedToken(Token, Range, Vec<Token>),
}
impl Into<DiagnosticFrame> for SyntaxError {
fn into(self) -> DiagnosticFrame {
todo!()
}
} }

View File

@ -70,43 +70,43 @@ impl<'a> Parser<'a> {
} }
pub fn parse_substitution(&mut self) -> Result<Box<Expr>, SyntaxError> { pub fn parse_substitution(&mut self) -> Result<Box<Expr>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // '##' self.bump(); // '##'
let name = self.parse_id()?; let name = self.parse_id()?;
self.eat_variant(Token::Slash)?; self.eat_variant(Token::Slash)?;
let redx = self.parse_num_lit()?; let redx = self.parse_num_lit()?;
let expr = self.parse_expr(false)?; let expr = self.parse_expr(false)?;
let span = start.mix(expr.span); let range = start.mix(expr.range);
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::Subst(Substitution { name, redx, indx: 0, expr }), data: ExprKind::Subst(Substitution { name, redx, indx: 0, expr }),
span, range,
})) }))
} }
pub fn parse_id(&mut self) -> Result<Ident, SyntaxError> { pub fn parse_id(&mut self) -> Result<Ident, SyntaxError> {
let span = self.span(); let range = self.range();
let id = eat_single!(self, Token::Id(x) => x.clone())?; let id = eat_single!(self, Token::Id(x) => x.clone())?;
let ident = Ident::new(Symbol(id), self.ctx, span); let ident = Ident::new(Symbol(id), self.ctx, range);
Ok(ident) Ok(ident)
} }
fn parse_lambda(&mut self) -> Result<Box<Expr>, SyntaxError> { fn parse_lambda(&mut self) -> Result<Box<Expr>, SyntaxError> {
let name_span = self.span(); let name_span = self.range();
let ident = self.parse_id()?; let ident = self.parse_id()?;
self.bump(); // '=>' self.bump(); // '=>'
let expr = self.parse_expr(false)?; let expr = self.parse_expr(false)?;
let end_range = expr.span; let end_range = expr.range;
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::Lambda(ident, None, expr), data: ExprKind::Lambda(ident, None, expr),
span: name_span.mix(end_range), range: name_span.mix(end_range),
})) }))
} }
fn parse_pi_or_lambda(&mut self) -> Result<Box<Expr>, SyntaxError> { fn parse_pi_or_lambda(&mut self) -> Result<Box<Expr>, SyntaxError> {
let span = self.span(); let range = self.range();
self.bump(); // '(' self.bump(); // '('
let ident = self.parse_id()?; let ident = self.parse_id()?;
self.bump(); // ':' self.bump(); // ':'
@ -117,21 +117,21 @@ impl<'a> Parser<'a> {
if self.eat_keyword(Token::FatArrow) { if self.eat_keyword(Token::FatArrow) {
let body = self.parse_expr(false)?; let body = self.parse_expr(false)?;
Ok(Box::new(Expr { Ok(Box::new(Expr {
span: span.mix(body.span), range: range.mix(body.range),
data: ExprKind::Lambda(ident, Some(typ), body), data: ExprKind::Lambda(ident, Some(typ), body),
})) }))
} else { } else {
self.eat_keyword(Token::RightArrow); self.eat_keyword(Token::RightArrow);
let body = self.parse_expr(false)?; let body = self.parse_expr(false)?;
Ok(Box::new(Expr { Ok(Box::new(Expr {
span: span.mix(body.span), range: range.mix(body.range),
data: ExprKind::All(Some(ident), typ, body), data: ExprKind::All(Some(ident), typ, body),
})) }))
} }
} }
fn parse_sigma_type(&mut self) -> Result<Box<Expr>, SyntaxError> { fn parse_sigma_type(&mut self) -> Result<Box<Expr>, SyntaxError> {
let span = self.span(); let range = self.range();
self.bump(); // '[' self.bump(); // '['
let ident = self.parse_id()?; let ident = self.parse_id()?;
self.bump(); // ':' self.bump(); // ':'
@ -143,7 +143,7 @@ impl<'a> Parser<'a> {
let body = self.parse_expr(false)?; let body = self.parse_expr(false)?;
Ok(Box::new(Expr { Ok(Box::new(Expr {
span: span.mix(end), range: range.mix(end),
data: ExprKind::Sigma(Some(ident), typ, body), data: ExprKind::Sigma(Some(ident), typ, body),
})) }))
} }
@ -151,25 +151,25 @@ impl<'a> Parser<'a> {
fn parse_var(&mut self) -> Result<Box<Expr>, SyntaxError> { fn parse_var(&mut self) -> Result<Box<Expr>, SyntaxError> {
let id = self.parse_id()?; let id = self.parse_id()?;
Ok(Box::new(Expr { Ok(Box::new(Expr {
span: id.span, range: id.range,
data: ExprKind::Var(id), data: ExprKind::Var(id),
})) }))
} }
fn parse_num(&mut self, num: u64) -> Result<Box<Expr>, SyntaxError> { fn parse_num(&mut self, num: u64) -> Result<Box<Expr>, SyntaxError> {
let span = self.span(); let range = self.range();
self.bump(); self.bump();
Ok(Box::new(Expr { Ok(Box::new(Expr {
span, range,
data: ExprKind::Lit(Literal::Number(num)), data: ExprKind::Lit(Literal::Number(num)),
})) }))
} }
fn parse_char(&mut self, chr: char) -> Result<Box<Expr>, SyntaxError> { fn parse_char(&mut self, chr: char) -> Result<Box<Expr>, SyntaxError> {
let span = self.span(); let range = self.range();
self.bump(); self.bump();
Ok(Box::new(Expr { Ok(Box::new(Expr {
span, range,
data: ExprKind::Lit(Literal::Char(chr)), data: ExprKind::Lit(Literal::Char(chr)),
})) }))
} }
@ -179,26 +179,26 @@ impl<'a> Parser<'a> {
} }
fn parse_binary_op(&mut self) -> Result<Box<Expr>, SyntaxError> { fn parse_binary_op(&mut self) -> Result<Box<Expr>, SyntaxError> {
let span = self.span(); let range = self.range();
self.bump(); // '(' self.bump(); // '('
let op = self.eat_operator()?; let op = self.eat_operator()?;
let fst = self.parse_atom()?; let fst = self.parse_atom()?;
let snd = self.parse_atom()?; let snd = self.parse_atom()?;
let end = self.eat_variant(Token::RPar)?.1; let end = self.eat_variant(Token::RPar)?.1;
Ok(Box::new(Expr { Ok(Box::new(Expr {
span: span.mix(end), range: range.mix(end),
data: ExprKind::Binary(op, fst, snd), data: ExprKind::Binary(op, fst, snd),
})) }))
} }
fn parse_array(&mut self) -> Result<Box<Expr>, SyntaxError> { fn parse_array(&mut self) -> Result<Box<Expr>, SyntaxError> {
let span = self.span(); let range = self.range();
self.bump(); // '[' self.bump(); // '['
let mut vec = Vec::new(); let mut vec = Vec::new();
if self.check_actual(Token::RBracket) { if self.check_actual(Token::RBracket) {
let span = self.advance().1.mix(span); let range = self.advance().1.mix(range);
return Ok(Box::new(Expr { span, data: ExprKind::List(vec) })); return Ok(Box::new(Expr { range, data: ExprKind::List(vec) }));
} }
vec.push(*self.parse_expr(false)?); vec.push(*self.parse_expr(false)?);
@ -225,52 +225,52 @@ impl<'a> Parser<'a> {
} }
} }
let span = self.eat_variant(Token::RBracket)?.1.mix(span); let range = self.eat_variant(Token::RBracket)?.1.mix(range);
Ok(Box::new(Expr { span, data: ExprKind::List(vec) })) Ok(Box::new(Expr { range, data: ExprKind::List(vec) }))
} }
fn parse_paren(&mut self) -> Result<Box<Expr>, SyntaxError> { fn parse_paren(&mut self) -> Result<Box<Expr>, SyntaxError> {
if self.is_operator() { if self.is_operator() {
self.parse_binary_op() self.parse_binary_op()
} else { } else {
let span = self.span(); let range = self.range();
self.bump(); // '(' self.bump(); // '('
let mut expr = self.parse_expr(true)?; let mut expr = self.parse_expr(true)?;
if self.get().same_variant(Token::ColonColon) { if self.get().same_variant(Token::ColonColon) {
self.bump(); // '::' self.bump(); // '::'
let typ = self.parse_expr(false)?; let typ = self.parse_expr(false)?;
let span = span.mix(self.eat_variant(Token::RPar)?.1); let range = range.mix(self.eat_variant(Token::RPar)?.1);
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::Ann(expr, typ), data: ExprKind::Ann(expr, typ),
span, range,
})) }))
} else { } else {
let end = self.eat_variant(Token::RPar)?.1; let end = self.eat_variant(Token::RPar)?.1;
expr.span = span.mix(end); expr.range = range.mix(end);
Ok(expr) Ok(expr)
} }
} }
} }
pub fn parse_help(&mut self, str: String) -> Result<Box<Expr>, SyntaxError> { pub fn parse_help(&mut self, str: String) -> Result<Box<Expr>, SyntaxError> {
let span = self.span(); let range = self.range();
self.bump(); self.bump();
Ok(Box::new(Expr { Ok(Box::new(Expr {
span, range,
data: ExprKind::Help(Ident { data: ExprKind::Help(Ident {
data: Symbol(str), data: Symbol(str),
ctx: self.ctx, ctx: self.ctx,
span, range,
}), }),
})) }))
} }
pub fn parse_str(&mut self, str: String) -> Result<Box<Expr>, SyntaxError> { pub fn parse_str(&mut self, str: String) -> Result<Box<Expr>, SyntaxError> {
let span = self.span(); let range = self.range();
self.bump(); self.bump();
Ok(Box::new(Expr { Ok(Box::new(Expr {
span, range,
data: ExprKind::Lit(Literal::String(str)), data: ExprKind::Lit(Literal::String(str)),
})) }))
} }
@ -291,14 +291,14 @@ impl<'a> Parser<'a> {
fn parse_call(&mut self, multiline: bool) -> Result<Box<Expr>, SyntaxError> { fn parse_call(&mut self, multiline: bool) -> Result<Box<Expr>, SyntaxError> {
let head = self.parse_atom()?; let head = self.parse_atom()?;
let start = head.span; let start = head.range;
let mut spine = Vec::new(); let mut spine = Vec::new();
let mut end = head.span; let mut end = head.range;
while (!self.is_linebreak() || multiline) && !self.get().same_variant(Token::Eof) { while (!self.is_linebreak() || multiline) && !self.get().same_variant(Token::Eof) {
let res = self.try_single(&|parser| parser.parse_atom())?; let res = self.try_single(&|parser| parser.parse_atom())?;
match res { match res {
Some(atom) => { Some(atom) => {
end = atom.span; end = atom.range;
spine.push(atom) spine.push(atom)
} }
None => break, None => break,
@ -309,7 +309,7 @@ impl<'a> Parser<'a> {
} else { } else {
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::App(head, spine), data: ExprKind::App(head, spine),
span: start.mix(end), range: start.mix(end),
})) }))
} }
} }
@ -318,16 +318,16 @@ impl<'a> Parser<'a> {
let mut head = self.parse_call(multiline)?; let mut head = self.parse_call(multiline)?;
while self.eat_keyword(Token::RightArrow) { while self.eat_keyword(Token::RightArrow) {
let next = self.parse_expr(false)?; let next = self.parse_expr(false)?;
let span = head.span.mix(next.span); let range = head.range.mix(next.range);
head = Box::new(Expr { head = Box::new(Expr {
data: ExprKind::All(None, head, next), data: ExprKind::All(None, head, next),
span, range,
}); });
} }
if self.eat_keyword(Token::ColonColon) { if self.eat_keyword(Token::ColonColon) {
let expr = self.parse_expr(false)?; let expr = self.parse_expr(false)?;
Ok(Box::new(Expr { Ok(Box::new(Expr {
span: head.span.mix(expr.span), range: head.range.mix(expr.range),
data: ExprKind::Ann(head, expr), data: ExprKind::Ann(head, expr),
})) }))
} else { } else {
@ -336,7 +336,7 @@ impl<'a> Parser<'a> {
} }
pub fn parse_ask(&mut self) -> Result<Box<Sttm>, SyntaxError> { pub fn parse_ask(&mut self) -> Result<Box<Sttm>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // 'ask' self.bump(); // 'ask'
// Parses the name for Ask that is optional // Parses the name for Ask that is optional
let name = if self.peek(1).same_variant(Token::Eq) { let name = if self.peek(1).same_variant(Token::Eq) {
@ -350,41 +350,41 @@ impl<'a> Parser<'a> {
let expr = self.parse_expr(false)?; let expr = self.parse_expr(false)?;
self.eat_keyword(Token::Semi); self.eat_keyword(Token::Semi);
let next = self.parse_sttm()?; let next = self.parse_sttm()?;
let end = expr.span; let end = expr.range;
Ok(Box::new(Sttm { Ok(Box::new(Sttm {
data: SttmKind::Ask(name, expr, next), data: SttmKind::Ask(name, expr, next),
span: start.mix(end), range: start.mix(end),
})) }))
} }
pub fn parse_monadic_let(&mut self) -> Result<Box<Sttm>, SyntaxError> { pub fn parse_monadic_let(&mut self) -> Result<Box<Sttm>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // 'let' self.bump(); // 'let'
let name = self.parse_id()?; let name = self.parse_id()?;
self.eat_variant(Token::Eq)?; self.eat_variant(Token::Eq)?;
let expr = self.parse_expr(false)?; let expr = self.parse_expr(false)?;
self.eat_keyword(Token::Semi); self.eat_keyword(Token::Semi);
let next = self.parse_sttm()?; let next = self.parse_sttm()?;
let end = expr.span; let end = expr.range;
Ok(Box::new(Sttm { Ok(Box::new(Sttm {
data: SttmKind::Let(name, expr, next), data: SttmKind::Let(name, expr, next),
span: start.mix(end), range: start.mix(end),
})) }))
} }
pub fn parse_return(&mut self) -> Result<Box<Sttm>, SyntaxError> { pub fn parse_return(&mut self) -> Result<Box<Sttm>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // 'return' self.bump(); // 'return'
let expr = self.parse_expr(false)?; let expr = self.parse_expr(false)?;
let end = expr.span; let end = expr.range;
Ok(Box::new(Sttm { Ok(Box::new(Sttm {
data: SttmKind::Return(expr), data: SttmKind::Return(expr),
span: start.mix(end), range: start.mix(end),
})) }))
} }
pub fn parse_sttm(&mut self) -> Result<Box<Sttm>, SyntaxError> { pub fn parse_sttm(&mut self) -> Result<Box<Sttm>, SyntaxError> {
let start = self.span(); let start = self.range();
if self.check_actual(Token::Ask) { if self.check_actual(Token::Ask) {
self.parse_ask() self.parse_ask()
} else if self.check_actual(Token::Return) { } else if self.check_actual(Token::Return) {
@ -394,24 +394,24 @@ impl<'a> Parser<'a> {
} else { } else {
let expr = self.parse_expr(false)?; let expr = self.parse_expr(false)?;
if self.get().same_variant(Token::RBrace) { if self.get().same_variant(Token::RBrace) {
let end = expr.span; let end = expr.range;
Ok(Box::new(Sttm { Ok(Box::new(Sttm {
data: SttmKind::Return(expr), data: SttmKind::Return(expr),
span: start.mix(end), range: start.mix(end),
})) }))
} else { } else {
let next = self.parse_sttm()?; let next = self.parse_sttm()?;
let end = next.span; let end = next.range;
Ok(Box::new(Sttm { Ok(Box::new(Sttm {
data: SttmKind::Expr(expr, next), data: SttmKind::Expr(expr, next),
span: start.mix(end), range: start.mix(end),
})) }))
} }
} }
} }
pub fn parse_do(&mut self) -> Result<Box<Expr>, SyntaxError> { pub fn parse_do(&mut self) -> Result<Box<Expr>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // 'do' self.bump(); // 'do'
let typ = self.parse_id()?; let typ = self.parse_id()?;
self.eat_variant(Token::LBrace)?; self.eat_variant(Token::LBrace)?;
@ -419,12 +419,12 @@ impl<'a> Parser<'a> {
let end = self.eat_variant(Token::RBrace)?.1; let end = self.eat_variant(Token::RBrace)?.1;
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::Do(typ, sttm), data: ExprKind::Do(typ, sttm),
span: start.mix(end), range: start.mix(end),
})) }))
} }
pub fn parse_match(&mut self) -> Result<Box<Expr>, SyntaxError> { pub fn parse_match(&mut self) -> Result<Box<Expr>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // 'match' self.bump(); // 'match'
let tipo = self.parse_id()?; let tipo = self.parse_id()?;
let name = self.parse_id()?; let name = self.parse_id()?;
@ -445,7 +445,7 @@ impl<'a> Parser<'a> {
let motive = if self.eat_keyword(Token::Colon) { let motive = if self.eat_keyword(Token::Colon) {
let expr = self.parse_expr(false)?; let expr = self.parse_expr(false)?;
end = expr.span; end = expr.range;
Some(self.parse_expr(false)?) Some(self.parse_expr(false)?)
} else { } else {
None None
@ -455,12 +455,12 @@ impl<'a> Parser<'a> {
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::Match(match_), data: ExprKind::Match(match_),
span: start.mix(end), range: start.mix(end),
})) }))
} }
pub fn parse_open(&mut self) -> Result<Box<Expr>, SyntaxError> { pub fn parse_open(&mut self) -> Result<Box<Expr>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // 'open' self.bump(); // 'open'
let tipo = self.parse_id()?; let tipo = self.parse_id()?;
let name = self.parse_id()?; let name = self.parse_id()?;
@ -468,45 +468,45 @@ impl<'a> Parser<'a> {
let expr = if self.eat_keyword(Token::Eq) { Some(self.parse_expr(false)?) } else { None }; let expr = if self.eat_keyword(Token::Eq) { Some(self.parse_expr(false)?) } else { None };
let body = self.parse_expr(false)?; let body = self.parse_expr(false)?;
let end = body.span; let end = body.range;
let open = Box::new(Open { tipo, name, expr, body }); let open = Box::new(Open { tipo, name, expr, body });
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::Open(open), data: ExprKind::Open(open),
span: start.mix(end), range: start.mix(end),
})) }))
} }
pub fn parse_let(&mut self) -> Result<Box<Expr>, SyntaxError> { pub fn parse_let(&mut self) -> Result<Box<Expr>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // 'let' self.bump(); // 'let'
let name = self.parse_id()?; let name = self.parse_id()?;
self.eat_variant(Token::Eq)?; self.eat_variant(Token::Eq)?;
let expr = self.parse_expr(false)?; let expr = self.parse_expr(false)?;
self.eat_keyword(Token::Semi); self.eat_keyword(Token::Semi);
let next = self.parse_expr(false)?; let next = self.parse_expr(false)?;
let end = next.span; let end = next.range;
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::Let(name, expr, next), data: ExprKind::Let(name, expr, next),
span: start.mix(end), range: start.mix(end),
})) }))
} }
fn parse_sigma_pair(&mut self) -> Result<Box<Expr>, SyntaxError> { fn parse_sigma_pair(&mut self) -> Result<Box<Expr>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // '$' self.bump(); // '$'
let fst = self.parse_atom()?; let fst = self.parse_atom()?;
let snd = self.parse_atom()?; let snd = self.parse_atom()?;
let end = snd.span; let end = snd.range;
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::Pair(fst, snd), data: ExprKind::Pair(fst, snd),
span: start.mix(end), range: start.mix(end),
})) }))
} }
fn parse_if(&mut self) -> Result<Box<Expr>, SyntaxError> { fn parse_if(&mut self) -> Result<Box<Expr>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // 'if' self.bump(); // 'if'
let cond = self.parse_expr(false)?; let cond = self.parse_expr(false)?;
self.eat_variant(Token::LBrace)?; self.eat_variant(Token::LBrace)?;
@ -516,10 +516,10 @@ impl<'a> Parser<'a> {
self.eat_variant(Token::LBrace)?; self.eat_variant(Token::LBrace)?;
let els_ = self.parse_expr(false)?; let els_ = self.parse_expr(false)?;
let end = self.eat_variant(Token::RBrace)?.1; let end = self.eat_variant(Token::RBrace)?.1;
let span = start.mix(end); let range = start.mix(end);
Ok(Box::new(Expr { Ok(Box::new(Expr {
data: ExprKind::If(cond, if_, els_), data: ExprKind::If(cond, if_, els_),
span, range,
})) }))
} }

View File

@ -1,4 +1,4 @@
use kind_span::Span; use kind_span::Range;
use crate::errors::SyntaxError; use crate::errors::SyntaxError;
use crate::lexer::tokens::Token; use crate::lexer::tokens::Token;
@ -6,7 +6,7 @@ use crate::Lexer;
impl<'a> Lexer<'a> { impl<'a> Lexer<'a> {
/// Single line comments /// Single line comments
pub fn lex_comment(&mut self, start: usize) -> (Token, Span) { pub fn lex_comment(&mut self, start: usize) -> (Token, Range) {
self.next_char(); self.next_char();
let mut is_doc = false; let mut is_doc = false;
if let Some('/') = self.peekable.peek() { if let Some('/') = self.peekable.peek() {
@ -14,12 +14,12 @@ impl<'a> Lexer<'a> {
is_doc = true; is_doc = true;
} }
let cmt = self.accumulate_while(&|x| x != '\n'); let cmt = self.accumulate_while(&|x| x != '\n');
(Token::Comment(is_doc, cmt.to_string()), self.mk_span(start)) (Token::Comment(is_doc, cmt.to_string()), self.mk_range(start))
} }
/// Parses multi line comments with nested comments /// Parses multi line comments with nested comments
/// really useful /// really useful
pub fn lex_multiline_comment(&mut self, start: usize) -> (Token, Span) { pub fn lex_multiline_comment(&mut self, start: usize) -> (Token, Range) {
let mut size = 0; let mut size = 0;
self.next_char(); self.next_char();
@ -54,11 +54,11 @@ impl<'a> Lexer<'a> {
} }
self.pos += size; self.pos += size;
if self.comment_depth != 0 { if self.comment_depth != 0 {
(Token::Error(Box::new(SyntaxError::UnfinishedComment(self.mk_span(start)))), self.mk_span(start)) (Token::Error(Box::new(SyntaxError::UnfinishedComment(self.mk_range(start)))), self.mk_range(start))
} else { } else {
let str = &self.input[..size - 2]; let str = &self.input[..size - 2];
self.input = &self.input[size..]; self.input = &self.input[size..];
(Token::Comment(false, str.to_string()), self.mk_span(start)) (Token::Comment(false, str.to_string()), self.mk_range(start))
} }
} }
} }

View File

@ -1,4 +1,4 @@
use kind_span::Span; use kind_span::Range;
use crate::errors::{EncodeSequence, SyntaxError}; use crate::errors::{EncodeSequence, SyntaxError};
use crate::lexer::tokens::Token; use crate::lexer::tokens::Token;
@ -13,13 +13,13 @@ impl<'a> Lexer<'a> {
if let Some(chr) = to_chr.and_then(char::from_u32) { if let Some(chr) = to_chr.and_then(char::from_u32) {
return Ok(chr); return Ok(chr);
} }
Err(SyntaxError::InvalidEscapeSequence(err, self.mk_span(start))) Err(SyntaxError::InvalidEscapeSequence(err, self.mk_range(start)))
} }
/// Turns a escaped char into a normal char. /// Turns a escaped char into a normal char.
fn lex_escaped_char(&mut self, start: usize) -> Result<char, SyntaxError> { fn lex_escaped_char(&mut self, start: usize) -> Result<char, SyntaxError> {
match self.peekable.peek() { match self.peekable.peek() {
None => Err(SyntaxError::UnfinishedString(self.mk_span(start))), None => Err(SyntaxError::UnfinishedString(self.mk_range(start))),
Some(&x) => { Some(&x) => {
self.next_char(); self.next_char();
match x { match x {
@ -39,31 +39,31 @@ impl<'a> Lexer<'a> {
} }
/// Lex a base-10 digit. /// Lex a base-10 digit.
fn lex_digit(&mut self, start: usize) -> (Token, Span) { fn lex_digit(&mut self, start: usize) -> (Token, Range) {
let num = self.accumulate_while(&|x| x.is_ascii_digit()); let num = self.accumulate_while(&|x| x.is_ascii_digit());
(Token::Num(num.parse::<u64>().unwrap()), self.mk_span(start)) (Token::Num(num.parse::<u64>().unwrap()), self.mk_range(start))
} }
/// Lexes a number of base @base@ removing the first /// Lexes a number of base @base@ removing the first
/// character that indicates the encoding /// character that indicates the encoding
fn lex_base(&mut self, start: usize, base: u32, err: EncodeSequence) -> (Token, Span) { fn lex_base(&mut self, start: usize, base: u32, err: EncodeSequence) -> (Token, Range) {
self.next_char(); self.next_char();
let num = self.accumulate_while(&|x| x.is_digit(base)); let num = self.accumulate_while(&|x| x.is_digit(base));
if let Ok(res) = u64::from_str_radix(num, base) { if let Ok(res) = u64::from_str_radix(num, base) {
(Token::Num(res), self.mk_span(start)) (Token::Num(res), self.mk_range(start))
} else { } else {
( (
Token::Error(Box::new(SyntaxError::InvalidNumberRepresentation(err, self.mk_span(start)))), Token::Error(Box::new(SyntaxError::InvalidNumberRepresentation(err, self.mk_range(start)))),
self.mk_span(start), self.mk_range(start),
) )
} }
} }
/// Lex numbers with decimal, hexadecimal, binary or octal. /// Lex numbers with decimal, hexadecimal, binary or octal.
pub fn lex_number(&mut self) -> (Token, Span) { pub fn lex_number(&mut self) -> (Token, Range) {
let start = self.span(); let start = self.span();
match self.peekable.peek() { match self.peekable.peek() {
None => (Token::Eof, self.mk_span(start)), None => (Token::Eof, self.mk_range(start)),
Some('0') => { Some('0') => {
self.next_char(); self.next_char();
match self.peekable.peek() { match self.peekable.peek() {
@ -71,12 +71,12 @@ impl<'a> Lexer<'a> {
Some('o') => self.lex_base(start, 8, EncodeSequence::Octal), Some('o') => self.lex_base(start, 8, EncodeSequence::Octal),
Some('b') => self.lex_base(start, 2, EncodeSequence::Binary), Some('b') => self.lex_base(start, 2, EncodeSequence::Binary),
Some('0'..='9') => self.lex_digit(start), Some('0'..='9') => self.lex_digit(start),
Some(_) => (Token::Num(0), self.mk_span(start)), Some(_) => (Token::Num(0), self.mk_range(start)),
None => (Token::Num(0), self.mk_span(start)), None => (Token::Num(0), self.mk_range(start)),
} }
} }
Some('0'..='9') => self.lex_digit(start), Some('0'..='9') => self.lex_digit(start),
Some(_) => (Token::Num(0), self.mk_span(start)), Some(_) => (Token::Num(0), self.mk_range(start)),
} }
} }
@ -85,13 +85,13 @@ impl<'a> Lexer<'a> {
/// and if the esaped char is not well-formed then it will /// and if the esaped char is not well-formed then it will
/// acummulate the error until the end of the string. /// acummulate the error until the end of the string.
/// TODO: Accumulate multiple encoding errors? /// TODO: Accumulate multiple encoding errors?
pub fn lex_string(&mut self) -> (Token, Span) { pub fn lex_string(&mut self) -> (Token, Range) {
let start = self.span(); let start = self.span();
self.next_char(); self.next_char();
let mut string = String::new(); let mut string = String::new();
let mut error: Option<(Token, Span)> = None; let mut error: Option<(Token, Range)> = None;
while let Some(&x) = self.peekable.peek() { while let Some(&x) = self.peekable.peek() {
let chr_start = self.span(); let chr_start = self.span();
@ -103,7 +103,7 @@ impl<'a> Lexer<'a> {
Ok(x) => string.push(x), Ok(x) => string.push(x),
Err(t) => { Err(t) => {
self.accumulate_while(&|x| x != '"'); self.accumulate_while(&|x| x != '"');
error = Some((Token::Error(Box::new(t)), self.mk_span(start))); error = Some((Token::Error(Box::new(t)), self.mk_range(start)));
} }
} }
} }
@ -114,8 +114,8 @@ impl<'a> Lexer<'a> {
match (self.next_char(), error) { match (self.next_char(), error) {
(_, Some(err)) => err, (_, Some(err)) => err,
(Some('"'), _) => (Token::Str(string), self.mk_span(start)), (Some('"'), _) => (Token::Str(string), self.mk_range(start)),
_ => (Token::Error(Box::new(SyntaxError::UnfinishedString(self.mk_span(start)))), self.mk_span(start)), _ => (Token::Error(Box::new(SyntaxError::UnfinishedString(self.mk_range(start)))), self.mk_range(start)),
} }
} }
} }

View File

@ -1,6 +1,6 @@
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use kind_span::Span; use kind_span::Range;
use crate::errors::SyntaxError; use crate::errors::SyntaxError;
@ -24,9 +24,9 @@ fn is_valid_id_start(chr: char) -> bool {
} }
impl<'a> Lexer<'a> { impl<'a> Lexer<'a> {
pub fn single_token(&mut self, token: Token, start: usize) -> (Token, Span) { pub fn single_token(&mut self, token: Token, start: usize) -> (Token, Range) {
self.next_char(); self.next_char();
(token, self.mk_span(start)) (token, self.mk_range(start))
} }
pub fn is_linebreak(&mut self) -> bool { pub fn is_linebreak(&mut self) -> bool {
@ -50,7 +50,7 @@ impl<'a> Lexer<'a> {
} }
} }
pub fn get_next_no_error(&mut self, vec: &Sender<Box<SyntaxError>>) -> (Token, Span) { pub fn get_next_no_error(&mut self, vec: &Sender<Box<SyntaxError>>) -> (Token, Range) {
loop { loop {
let (token, span) = self.lex_token(); let (token, span) = self.lex_token();
match token { match token {
@ -66,10 +66,10 @@ impl<'a> Lexer<'a> {
} }
} }
pub fn lex_token(&mut self) -> (Token, Span) { pub fn lex_token(&mut self) -> (Token, Range) {
let start = self.span(); let start = self.span();
match self.peekable.peek() { match self.peekable.peek() {
None => (Token::Eof, self.mk_span(start)), None => (Token::Eof, self.mk_range(start)),
Some(chr) => match chr { Some(chr) => match chr {
c if is_whitespace(*c) => { c if is_whitespace(*c) => {
self.accumulate_while(&is_whitespace); self.accumulate_while(&is_whitespace);
@ -82,7 +82,7 @@ impl<'a> Lexer<'a> {
c if c.is_ascii_digit() => self.lex_number(), c if c.is_ascii_digit() => self.lex_number(),
c if is_valid_id_start(*c) => { c if is_valid_id_start(*c) => {
let str = self.accumulate_while(&is_valid_id); let str = self.accumulate_while(&is_valid_id);
(Lexer::to_keyword(str), self.mk_span(start)) (Lexer::to_keyword(str), self.mk_range(start))
} }
'(' => self.single_token(Token::LPar, start), '(' => self.single_token(Token::LPar, start),
')' => self.single_token(Token::RPar, start), ')' => self.single_token(Token::RPar, start),
@ -94,7 +94,7 @@ impl<'a> Lexer<'a> {
self.next_char(); self.next_char();
match self.peekable.peek() { match self.peekable.peek() {
Some('#') => self.single_token(Token::HashHash, start), Some('#') => self.single_token(Token::HashHash, start),
_ => (Token::Hash, self.mk_span(start)), _ => (Token::Hash, self.mk_range(start)),
} }
} }
'=' => { '=' => {
@ -102,7 +102,7 @@ impl<'a> Lexer<'a> {
match self.peekable.peek() { match self.peekable.peek() {
Some('>') => self.single_token(Token::FatArrow, start), Some('>') => self.single_token(Token::FatArrow, start),
Some('=') => self.single_token(Token::EqEq, start), Some('=') => self.single_token(Token::EqEq, start),
_ => (Token::Eq, self.mk_span(start)), _ => (Token::Eq, self.mk_range(start)),
} }
} }
'>' => { '>' => {
@ -110,7 +110,7 @@ impl<'a> Lexer<'a> {
match self.peekable.peek() { match self.peekable.peek() {
Some('>') => self.single_token(Token::GreaterGreater, start), Some('>') => self.single_token(Token::GreaterGreater, start),
Some('=') => self.single_token(Token::GreaterEq, start), Some('=') => self.single_token(Token::GreaterEq, start),
_ => (Token::Greater, self.mk_span(start)), _ => (Token::Greater, self.mk_range(start)),
} }
} }
'<' => { '<' => {
@ -118,7 +118,7 @@ impl<'a> Lexer<'a> {
match self.peekable.peek() { match self.peekable.peek() {
Some('<') => self.single_token(Token::LessLess, start), Some('<') => self.single_token(Token::LessLess, start),
Some('=') => self.single_token(Token::LessEq, start), Some('=') => self.single_token(Token::LessEq, start),
_ => (Token::Less, self.mk_span(start)), _ => (Token::Less, self.mk_range(start)),
} }
} }
'/' => { '/' => {
@ -126,14 +126,14 @@ impl<'a> Lexer<'a> {
match self.peekable.peek() { match self.peekable.peek() {
Some('/') => self.lex_comment(start), Some('/') => self.lex_comment(start),
Some('*') => self.lex_multiline_comment(start), Some('*') => self.lex_multiline_comment(start),
_ => (Token::Slash, self.mk_span(start)), _ => (Token::Slash, self.mk_range(start)),
} }
} }
':' => { ':' => {
self.next_char(); self.next_char();
match self.peekable.peek() { match self.peekable.peek() {
Some(':') => self.single_token(Token::ColonColon, start), Some(':') => self.single_token(Token::ColonColon, start),
_ => (Token::Colon, self.mk_span(start)), _ => (Token::Colon, self.mk_range(start)),
} }
} }
';' => self.single_token(Token::Semi, start), ';' => self.single_token(Token::Semi, start),
@ -144,7 +144,7 @@ impl<'a> Lexer<'a> {
self.next_char(); self.next_char();
match self.peekable.peek() { match self.peekable.peek() {
Some('>') => self.single_token(Token::RightArrow, start), Some('>') => self.single_token(Token::RightArrow, start),
_ => (Token::Minus, self.mk_span(start)), _ => (Token::Minus, self.mk_range(start)),
} }
} }
'*' => self.single_token(Token::Star, start), '*' => self.single_token(Token::Star, start),
@ -156,18 +156,18 @@ impl<'a> Lexer<'a> {
'?' => { '?' => {
self.next_char(); self.next_char();
let str = self.accumulate_while(&is_valid_id); let str = self.accumulate_while(&is_valid_id);
(Token::Help(str.to_string()), self.mk_span(start)) (Token::Help(str.to_string()), self.mk_range(start))
} }
'!' => { '!' => {
self.next_char(); self.next_char();
match self.peekable.peek() { match self.peekable.peek() {
Some('=') => self.single_token(Token::BangEq, start), Some('=') => self.single_token(Token::BangEq, start),
_ => (Token::Bang, self.mk_span(start)), _ => (Token::Bang, self.mk_range(start)),
} }
} }
&c => { &c => {
self.next_char(); self.next_char();
(Token::Error(Box::new(SyntaxError::UnexpectedChar(c, self.mk_span(start)))), self.mk_span(start)) (Token::Error(Box::new(SyntaxError::UnexpectedChar(c, self.mk_range(start)))), self.mk_range(start))
} }
}, },
} }

View File

@ -1,6 +1,6 @@
use std::{iter::Peekable, str::Chars}; use std::{iter::Peekable, str::Chars};
use kind_span::{Pos, Range, Span, SyntaxCtxIndex}; use kind_span::{Pos, Range, SyntaxCtxIndex};
use crate::{errors::SyntaxError, lexer::tokens::Token}; use crate::{errors::SyntaxError, lexer::tokens::Token};
@ -34,8 +34,8 @@ impl<'a> Lexer<'a> {
self.pos self.pos
} }
pub fn mk_span(&self, start: usize) -> Span { pub fn mk_range(&self, start: usize) -> Range {
Span::new(Range::new(Pos { index: start as u32 }, Pos { index: self.pos as u32 }, self.ctx)) Range::new(Pos { index: start as u32 }, Pos { index: self.pos as u32 }, self.ctx)
} }
pub fn next_char(&mut self) -> Option<char> { pub fn next_char(&mut self) -> Option<char> {
@ -82,7 +82,7 @@ impl<'a> Lexer<'a> {
#[inline] #[inline]
/// Useful as entrypoint /// Useful as entrypoint
pub fn lex_next(&mut self) -> (Token, Span) { pub fn lex_next(&mut self) -> (Token, Range) {
self.lex_token() self.lex_token()
} }
} }

View File

@ -11,7 +11,7 @@ impl<'a> Parser<'a> {
} }
pub fn parse_pat_constructor(&mut self) -> Result<Box<Pat>, SyntaxError> { pub fn parse_pat_constructor(&mut self) -> Result<Box<Pat>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // '(' self.bump(); // '('
let name = self.parse_id()?; let name = self.parse_id()?;
let mut pats = Vec::new(); let mut pats = Vec::new();
@ -20,54 +20,54 @@ impl<'a> Parser<'a> {
} }
let end = self.eat_variant(Token::RPar)?.1; let end = self.eat_variant(Token::RPar)?.1;
Ok(Box::new(Pat { Ok(Box::new(Pat {
span: start.mix(end), range: start.mix(end),
data: PatKind::App(name, pats), data: PatKind::App(name, pats),
})) }))
} }
pub fn parse_pat_num(&mut self) -> Result<Box<Pat>, SyntaxError> { pub fn parse_pat_num(&mut self) -> Result<Box<Pat>, SyntaxError> {
let start = self.span(); let start = self.range();
let num = eat_single!(self, Token::Num(n) => *n)?; let num = eat_single!(self, Token::Num(n) => *n)?;
Ok(Box::new(Pat { Ok(Box::new(Pat {
span: start, range: start,
data: PatKind::Num(num), data: PatKind::Num(num),
})) }))
} }
pub fn parse_pat_str(&mut self) -> Result<Box<Pat>, SyntaxError> { pub fn parse_pat_str(&mut self) -> Result<Box<Pat>, SyntaxError> {
let start = self.span(); let start = self.range();
let string = eat_single!(self, Token::Str(str) => str.clone())?; let string = eat_single!(self, Token::Str(str) => str.clone())?;
Ok(Box::new(Pat { Ok(Box::new(Pat {
span: start, range: start,
data: PatKind::Str(string), data: PatKind::Str(string),
})) }))
} }
pub fn parse_pat_group(&mut self) -> Result<Box<Pat>, SyntaxError> { pub fn parse_pat_group(&mut self) -> Result<Box<Pat>, SyntaxError> {
let start = self.span(); let start = self.range();
self.bump(); // '(' self.bump(); // '('
let mut pat = self.parse_pat()?; let mut pat = self.parse_pat()?;
let end = self.eat_variant(Token::RPar)?.1; let end = self.eat_variant(Token::RPar)?.1;
pat.span = start.mix(end); pat.range = start.mix(end);
Ok(pat) Ok(pat)
} }
pub fn parse_pat_var(&mut self) -> Result<Box<Pat>, SyntaxError> { pub fn parse_pat_var(&mut self) -> Result<Box<Pat>, SyntaxError> {
let id = self.parse_id()?; let id = self.parse_id()?;
Ok(Box::new(Pat { Ok(Box::new(Pat {
span: id.span, range: id.range,
data: PatKind::Var(id), data: PatKind::Var(id),
})) }))
} }
fn parse_pat_list(&mut self) -> Result<Box<Pat>, SyntaxError> { fn parse_pat_list(&mut self) -> Result<Box<Pat>, SyntaxError> {
let span = self.span(); let range = self.range();
self.bump(); // '[' self.bump(); // '['
let mut vec = Vec::new(); let mut vec = Vec::new();
if self.check_actual(Token::RBracket) { if self.check_actual(Token::RBracket) {
let span = self.advance().1.mix(span); let range = self.advance().1.mix(range);
return Ok(Box::new(Pat { span, data: PatKind::List(vec) })); return Ok(Box::new(Pat { range, data: PatKind::List(vec) }));
} }
vec.push(*self.parse_pat()?); vec.push(*self.parse_pat()?);
@ -89,9 +89,9 @@ impl<'a> Parser<'a> {
} }
} }
let span = self.eat_variant(Token::RBracket)?.1.mix(span); let range = self.eat_variant(Token::RBracket)?.1.mix(range);
Ok(Box::new(Pat { span, data: PatKind::List(vec) })) Ok(Box::new(Pat { range, data: PatKind::List(vec) }))
} }
pub fn parse_pat(&mut self) -> Result<Box<Pat>, SyntaxError> { pub fn parse_pat(&mut self) -> Result<Box<Pat>, SyntaxError> {

View File

@ -1,7 +1,7 @@
use std::collections::VecDeque; use std::collections::VecDeque;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use kind_span::{Span, SyntaxCtxIndex}; use kind_span::{Range, SyntaxCtxIndex};
use crate::{errors::SyntaxError, lexer::tokens::Token, Lexer}; use crate::{errors::SyntaxError, lexer::tokens::Token, Lexer};
@ -15,7 +15,7 @@ pub struct Parser<'a> {
// We have to shift these things one position // We have to shift these things one position
// to the left so idk what i should use it here // to the left so idk what i should use it here
// probably the movement will not affect it so much. // probably the movement will not affect it so much.
pub queue: VecDeque<(Token, Span)>, pub queue: VecDeque<(Token, Range)>,
pub breaks: VecDeque<bool>, pub breaks: VecDeque<bool>,
pub errs: &'a Sender<Box<SyntaxError>>, pub errs: &'a Sender<Box<SyntaxError>>,
pub eaten: u32, pub eaten: u32,
@ -40,7 +40,7 @@ impl<'a> Parser<'a> {
} }
} }
pub fn advance(&mut self) -> (Token, Span) { pub fn advance(&mut self) -> (Token, Range) {
let cur = self.queue.pop_front().unwrap(); let cur = self.queue.pop_front().unwrap();
self.breaks.pop_front(); self.breaks.pop_front();
self.breaks.push_back(self.lexer.is_linebreak()); self.breaks.push_back(self.lexer.is_linebreak());
@ -64,7 +64,7 @@ impl<'a> Parser<'a> {
} }
#[inline] #[inline]
pub fn span(&self) -> Span { pub fn range(&self) -> Range {
self.queue[0].1 self.queue[0].1
} }
@ -75,10 +75,10 @@ impl<'a> Parser<'a> {
#[inline] #[inline]
pub fn fail<T>(&mut self, expect: Vec<Token>) -> Result<T, SyntaxError> { pub fn fail<T>(&mut self, expect: Vec<Token>) -> Result<T, SyntaxError> {
Err(SyntaxError::UnexpectedToken(self.get().clone(), self.span(), expect)) Err(SyntaxError::UnexpectedToken(self.get().clone(), self.range(), expect))
} }
pub fn eat_variant(&mut self, expect: Token) -> Result<(Token, Span), SyntaxError> { pub fn eat_variant(&mut self, expect: Token) -> Result<(Token, Range), SyntaxError> {
if self.get().same_variant(expect.clone()) { if self.get().same_variant(expect.clone()) {
Ok(self.advance()) Ok(self.advance())
} else { } else {

View File

@ -22,7 +22,7 @@ impl<'a> Parser<'a> {
} }
pub fn parse_argument(&mut self) -> Result<Box<Argument>, SyntaxError> { pub fn parse_argument(&mut self) -> Result<Box<Argument>, SyntaxError> {
let start = self.span(); let start = self.range();
let erased = self.eat_keyword(Token::Minus); let erased = self.eat_keyword(Token::Minus);
let keep = self.eat_keyword(Token::Plus); let keep = self.eat_keyword(Token::Plus);
@ -40,12 +40,18 @@ impl<'a> Parser<'a> {
let erased = if hidden { !keep } else { erased }; let erased = if hidden { !keep } else { erased };
let res = self.eat_variant(complement.unwrap())?.1; let res = self.eat_variant(complement.unwrap())?.1;
let span = res.mix(start); let range = res.mix(start);
Ok(Box::new(Argument { hidden, erased, name, tipo, span })) Ok(Box::new(Argument {
hidden,
erased,
name,
tipo,
range,
}))
} }
pub fn parse_rule(&mut self, name: String) -> Result<Box<Rule>, SyntaxError> { pub fn parse_rule(&mut self, name: String) -> Result<Box<Rule>, SyntaxError> {
let start = self.span(); let start = self.range();
let ident; let ident;
if let Token::Id(name_id) = self.get() { if let Token::Id(name_id) = self.get() {
if *name_id == name { if *name_id == name {
@ -62,17 +68,17 @@ impl<'a> Parser<'a> {
} }
self.eat_variant(Token::Eq)?; self.eat_variant(Token::Eq)?;
let body = self.parse_expr(false)?; let body = self.parse_expr(false)?;
let end = start.mix(body.span); let end = start.mix(body.range);
Ok(Box::new(Rule { Ok(Box::new(Rule {
name: ident, name: ident,
pats, pats,
body, body,
span: end, range: end,
})) }))
} }
pub fn parse_entry(&mut self) -> Result<Box<Entry>, SyntaxError> { pub fn parse_entry(&mut self) -> Result<Box<Entry>, SyntaxError> {
let start = self.span(); let start = self.range();
let ident = self.parse_id()?; let ident = self.parse_id()?;
let docs = None; let docs = None;
let mut args = Vec::new(); let mut args = Vec::new();
@ -92,7 +98,7 @@ impl<'a> Parser<'a> {
None => break, None => break,
} }
} }
let end = rules.last().as_ref().map(|x| x.span).unwrap_or(tipo.span); let end = rules.last().as_ref().map(|x| x.range).unwrap_or(tipo.range);
Ok(Box::new(Entry { Ok(Box::new(Entry {
name: ident, name: ident,
docs, docs,
@ -100,7 +106,7 @@ impl<'a> Parser<'a> {
tipo, tipo,
rules, rules,
attrs: Vec::new(), attrs: Vec::new(),
span: start.mix(end), range: start.mix(end),
})) }))
} }

View File

@ -2,7 +2,7 @@
/// without parenthesis. It helps when it comes to /// without parenthesis. It helps when it comes to
/// a static analysis of the tree with the syntax sugars /// a static analysis of the tree with the syntax sugars
/// and it makes it easier to split phases. /// and it makes it easier to split phases.
use kind_span::Span; use kind_span::Range;
use std::fmt::{Display, Error, Formatter}; use std::fmt::{Display, Error, Formatter};
use crate::symbol::Ident; use crate::symbol::Ident;
@ -89,7 +89,7 @@ pub enum SttmKind {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Sttm { pub struct Sttm {
pub data: SttmKind, pub data: SttmKind,
pub span: Span, pub range: Range,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -137,7 +137,7 @@ pub enum ExprKind {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Expr { pub struct Expr {
pub data: ExprKind, pub data: ExprKind,
pub span: Span, pub range: Range,
} }
impl Display for Operator { impl Display for Operator {
@ -166,13 +166,6 @@ impl Display for Operator {
} }
impl Expr { impl Expr {
pub fn new_var(name: Ident) -> Expr {
Expr {
span: Span::Generated,
data: ExprKind::Var(name),
}
}
pub fn traverse_pi_types(&self) -> String { pub fn traverse_pi_types(&self) -> String {
match &self.data { match &self.data {
ExprKind::All(binder, typ, body) => match binder { ExprKind::All(binder, typ, body) => match binder {

View File

@ -3,7 +3,7 @@ use std::fmt::{Display, Error, Formatter};
use crate::symbol::Ident; use crate::symbol::Ident;
use expr::Expr; use expr::Expr;
use kind_span::Span; use kind_span::Range;
use self::pat::Pat; use self::pat::Pat;
@ -16,7 +16,7 @@ pub mod visitor;
pub enum AttributeStyle { pub enum AttributeStyle {
Ident(Ident), Ident(Ident),
String(String), String(String),
Number(Span, u64), Number(Range, u64),
} }
/// A attribute is a kind of declaration /// A attribute is a kind of declaration
@ -27,7 +27,7 @@ pub enum AttributeStyle {
pub struct Attribute { pub struct Attribute {
pub name: Ident, pub name: Ident,
pub value: Option<AttributeStyle>, pub value: Option<AttributeStyle>,
pub span: Span, pub range: Range,
} }
/// An argument is a 'binding' of a name to a type /// An argument is a 'binding' of a name to a type
@ -42,7 +42,7 @@ pub struct Argument {
pub erased: bool, pub erased: bool,
pub name: Ident, pub name: Ident,
pub tipo: Option<Box<Expr>>, pub tipo: Option<Box<Expr>>,
pub span: Span, pub range: Range,
} }
/// A rule is a equation that in the left-hand-side /// A rule is a equation that in the left-hand-side
@ -53,7 +53,7 @@ pub struct Rule {
pub name: Ident, pub name: Ident,
pub pats: Vec<Box<Pat>>, pub pats: Vec<Box<Pat>>,
pub body: Box<Expr>, pub body: Box<Expr>,
pub span: Span, pub range: Range,
} }
/// An entry describes a function that is typed /// An entry describes a function that is typed
@ -68,7 +68,7 @@ pub struct Entry {
pub tipo: Box<Expr>, pub tipo: Box<Expr>,
pub rules: Vec<Box<Rule>>, pub rules: Vec<Box<Rule>>,
pub attrs: Vec<Attribute>, pub attrs: Vec<Attribute>,
pub span: Span, pub range: Range,
} }
// A book is a collection of entries. // A book is a collection of entries.

View File

@ -1,6 +1,6 @@
use std::fmt::{Display, Error, Formatter}; use std::fmt::{Display, Error, Formatter};
use kind_span::Span; use kind_span::Range;
use crate::symbol::Ident; use crate::symbol::Ident;
@ -24,7 +24,7 @@ pub enum PatKind {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Pat { pub struct Pat {
pub data: PatKind, pub data: PatKind,
pub span: Span, pub range: Range,
} }
impl Display for Pat { impl Display for Pat {

View File

@ -1,4 +1,4 @@
use kind_span::{Span, SyntaxCtxIndex}; use kind_span::{Range, Span, SyntaxCtxIndex};
use crate::concrete::expr::*; use crate::concrete::expr::*;
use crate::symbol::*; use crate::symbol::*;
@ -19,8 +19,8 @@ use super::{
/// change these default implementations. /// change these default implementations.
pub trait Visitor: Sized { pub trait Visitor: Sized {
fn visit_span(&mut self, x: &mut Span) { fn visit_range(&mut self, x: &mut Range) {
walk_span(self, x); walk_range(self, x);
} }
fn visit_syntax_ctx(&mut self, synt: &mut SyntaxCtxIndex) { fn visit_syntax_ctx(&mut self, synt: &mut SyntaxCtxIndex) {
@ -84,20 +84,20 @@ pub trait Visitor: Sized {
} }
} }
fn walk_span<T: Visitor>(_: &mut T, _: &mut Span) {} pub fn walk_range<T: Visitor>(_: &mut T, _: &mut Range) {}
fn walk_syntax_ctx<T: Visitor>(_: &mut T, _: &mut SyntaxCtxIndex) {} pub fn walk_syntax_ctx<T: Visitor>(_: &mut T, _: &mut SyntaxCtxIndex) {}
fn walk_operator<T: Visitor>(_: &mut T, _: &mut expr::Operator) {} pub fn walk_operator<T: Visitor>(_: &mut T, _: &mut expr::Operator) {}
fn walk_literal<T: Visitor>(_: &mut T, _: &mut Literal) {} pub fn walk_literal<T: Visitor>(_: &mut T, _: &mut Literal) {}
fn walk_ident<T: Visitor>(ctx: &mut T, ident: &mut Ident) { pub fn walk_ident<T: Visitor>(ctx: &mut T, ident: &mut Ident) {
ctx.visit_span(&mut ident.span); ctx.visit_range(&mut ident.range);
ctx.visit_syntax_ctx(&mut ident.ctx); ctx.visit_syntax_ctx(&mut ident.ctx);
} }
fn walk_open<T: Visitor>(ctx: &mut T, open: &mut Open) { pub fn walk_open<T: Visitor>(ctx: &mut T, open: &mut Open) {
ctx.visit_expr(&mut open.body); ctx.visit_expr(&mut open.body);
match &mut open.expr { match &mut open.expr {
Some(expr) => ctx.visit_expr(expr), Some(expr) => ctx.visit_expr(expr),
@ -107,7 +107,7 @@ fn walk_open<T: Visitor>(ctx: &mut T, open: &mut Open) {
ctx.visit_ident(&mut open.name); ctx.visit_ident(&mut open.name);
} }
fn walk_match<T: Visitor>(ctx: &mut T, matcher: &mut Match) { pub fn walk_match<T: Visitor>(ctx: &mut T, matcher: &mut Match) {
match &mut matcher.expr { match &mut matcher.expr {
Some(expr) => ctx.visit_expr(expr), Some(expr) => ctx.visit_expr(expr),
None => (), None => (),
@ -124,16 +124,16 @@ fn walk_match<T: Visitor>(ctx: &mut T, matcher: &mut Match) {
} }
} }
fn walk_argument<T: Visitor>(ctx: &mut T, argument: &mut Argument) { pub fn walk_argument<T: Visitor>(ctx: &mut T, argument: &mut Argument) {
ctx.visit_ident(&mut argument.name); ctx.visit_ident(&mut argument.name);
match &mut argument.tipo { match &mut argument.tipo {
Some(tipo) => ctx.visit_expr(tipo), Some(tipo) => ctx.visit_expr(tipo),
None => (), None => (),
} }
ctx.visit_span(&mut argument.span); ctx.visit_range(&mut argument.range);
} }
fn walk_entry<T: Visitor>(ctx: &mut T, entry: &mut Entry) { pub fn walk_entry<T: Visitor>(ctx: &mut T, entry: &mut Entry) {
ctx.visit_ident(&mut entry.name); ctx.visit_ident(&mut entry.name);
for arg in &mut entry.args { for arg in &mut entry.args {
ctx.visit_argument(arg) ctx.visit_argument(arg)
@ -145,17 +145,17 @@ fn walk_entry<T: Visitor>(ctx: &mut T, entry: &mut Entry) {
for attr in &mut entry.attrs { for attr in &mut entry.attrs {
ctx.visit_attr(attr); ctx.visit_attr(attr);
} }
ctx.visit_span(&mut entry.span); ctx.visit_range(&mut entry.range);
} }
fn walk_attr<T: Visitor>(ctx: &mut T, attr: &mut Attribute) { pub fn walk_attr<T: Visitor>(ctx: &mut T, attr: &mut Attribute) {
ctx.visit_ident(&mut attr.name); ctx.visit_ident(&mut attr.name);
ctx.visit_span(&mut attr.span); ctx.visit_range(&mut attr.range);
// TODO: Visit inner side of the attribute // TODO: Visit inner side of the attribute
} }
fn walk_pat<T: Visitor>(ctx: &mut T, pat: &mut Pat) { pub fn walk_pat<T: Visitor>(ctx: &mut T, pat: &mut Pat) {
ctx.visit_span(&mut pat.span); ctx.visit_range(&mut pat.range);
match &mut pat.data { match &mut pat.data {
PatKind::Var(ident) => ctx.visit_ident(ident), PatKind::Var(ident) => ctx.visit_ident(ident),
PatKind::Str(_) => (), PatKind::Str(_) => (),
@ -179,28 +179,28 @@ fn walk_pat<T: Visitor>(ctx: &mut T, pat: &mut Pat) {
} }
} }
fn walk_rule<T: Visitor>(ctx: &mut T, rule: &mut Rule) { pub fn walk_rule<T: Visitor>(ctx: &mut T, rule: &mut Rule) {
ctx.visit_ident(&mut rule.name); ctx.visit_ident(&mut rule.name);
for pat in &mut rule.pats { for pat in &mut rule.pats {
ctx.visit_pat(pat); ctx.visit_pat(pat);
} }
ctx.visit_expr(&mut rule.body); ctx.visit_expr(&mut rule.body);
ctx.visit_span(&mut rule.span); ctx.visit_range(&mut rule.range);
} }
fn walk_book<T: Visitor>(ctx: &mut T, book: &mut Book) { pub fn walk_book<T: Visitor>(ctx: &mut T, book: &mut Book) {
for entr in book.entrs.values_mut() { for entr in book.entrs.values_mut() {
ctx.visit_entry(entr); ctx.visit_entry(entr);
} }
} }
fn walk_substitution<T: Visitor>(ctx: &mut T, subst: &mut Substitution) { pub fn walk_substitution<T: Visitor>(ctx: &mut T, subst: &mut Substitution) {
ctx.visit_expr(&mut subst.expr); ctx.visit_expr(&mut subst.expr);
ctx.visit_ident(&mut subst.name); ctx.visit_ident(&mut subst.name);
} }
fn walk_sttm<T: Visitor>(ctx: &mut T, sttm: &mut Sttm) { pub fn walk_sttm<T: Visitor>(ctx: &mut T, sttm: &mut Sttm) {
ctx.visit_span(&mut sttm.span); ctx.visit_range(&mut sttm.range);
match &mut sttm.data { match &mut sttm.data {
SttmKind::Ask(Some(ident), val, next) => { SttmKind::Ask(Some(ident), val, next) => {
ctx.visit_ident(ident); ctx.visit_ident(ident);
@ -235,8 +235,8 @@ fn walk_sttm<T: Visitor>(ctx: &mut T, sttm: &mut Sttm) {
} }
} }
fn walk_expr<T: Visitor>(ctx: &mut T, expr: &mut Expr) { pub fn walk_expr<T: Visitor>(ctx: &mut T, expr: &mut Expr) {
ctx.visit_span(&mut expr.span); ctx.visit_range(&mut expr.range);
match &mut expr.data { match &mut expr.data {
ExprKind::Var(ident) => ctx.visit_ident(ident), ExprKind::Var(ident) => ctx.visit_ident(ident),
ExprKind::All(None, typ, body) => { ExprKind::All(None, typ, body) => {

View File

@ -1,6 +1,6 @@
use std::fmt::Display; use std::fmt::Display;
use kind_span::{Span, SyntaxCtxIndex}; use kind_span::{Range, Span, SyntaxCtxIndex};
// Stores the name of a variable or constructor // Stores the name of a variable or constructor
#[derive(Clone, PartialEq, Eq, Hash, Debug)] #[derive(Clone, PartialEq, Eq, Hash, Debug)]
@ -11,30 +11,22 @@ pub struct Symbol(pub String);
pub struct Ident { pub struct Ident {
pub data: Symbol, pub data: Symbol,
pub ctx: SyntaxCtxIndex, pub ctx: SyntaxCtxIndex,
pub span: Span, pub range: Range,
} }
impl Ident { impl Ident {
pub fn new(data: Symbol, ctx: SyntaxCtxIndex, span: Span) -> Ident { pub fn new(data: Symbol, ctx: SyntaxCtxIndex, range: Range) -> Ident {
Ident { data, ctx, span } Ident { data, ctx, range }
} }
pub fn new_path(data: &str, id: &str) -> Ident { /// Changes the syntax context of the range and of the ident
Ident {
data: Symbol(format!("{}.{}", data, id)),
ctx: SyntaxCtxIndex(0),
span: Span::Generated,
}
}
/// Changes the syntax context of the span and of the ident
pub fn set_ctx(&self, ctx: SyntaxCtxIndex) -> Ident { pub fn set_ctx(&self, ctx: SyntaxCtxIndex) -> Ident {
let mut span = self.span; let mut range = self.range;
span.set_ctx(ctx); range.set_ctx(ctx);
Ident { Ident {
data: self.data.clone(), data: self.data.clone(),
ctx, ctx,
span, range,
} }
} }
} }