aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/lex.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lisp/lex.rs')
-rw-r--r--src/lisp/lex.rs22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/lisp/lex.rs b/src/lisp/lex.rs
index e514d7f..5d3030b 100644
--- a/src/lisp/lex.rs
+++ b/src/lisp/lex.rs
@@ -1,6 +1,6 @@
1use std::{fmt, str::CharIndices}; 1use std::{fmt, str::CharIndices};
2 2
3use crate::lisp::error::{LispError, ParseError, ParseErrorKind}; 3use crate::lisp::error::{ParseError, ParseErrorKind};
4 4
5#[derive(Copy, Clone, Debug, Eq, PartialEq)] 5#[derive(Copy, Clone, Debug, Eq, PartialEq)]
6pub enum Token<'a> { 6pub enum Token<'a> {
@@ -75,21 +75,27 @@ impl Span {
75} 75}
76 76
77#[derive(Debug, Clone)] 77#[derive(Debug, Clone)]
78pub struct SpanDisplay<'src> { 78pub struct SpanDisplay<'src, 'file> {
79 pub file_name: &'file str,
79 pub source: &'src str, 80 pub source: &'src str,
80 pub line: usize, 81 pub line: usize,
81 pub col: usize, 82 pub col: usize,
82} 83}
83 84
84impl<'src> SpanDisplay<'src> { 85impl<'src, 'file> SpanDisplay<'src, 'file> {
85 pub fn highlight_span(span: Span, source: &'src str) -> Self { 86 pub fn highlight_span(span: Span, source: &'src str, file_name: &'file str) -> Self {
86 let line_start = match source[..span.low as usize].rfind('\n') { 87 let line_start = match source[..span.low as usize].rfind('\n') {
87 Some(pos) => pos + 1, 88 Some(pos) => pos + 1,
88 None => 0, 89 None => 0,
89 }; 90 };
90 let line = source[..line_start].chars().filter(|&c| c == '\n').count() + 1; 91 let line = source[..line_start].chars().filter(|&c| c == '\n').count() + 1;
91 let col = source[line_start..span.low as usize].chars().count(); 92 let col = source[line_start..span.low as usize].chars().count();
92 Self { source, line, col } 93 Self {
94 file_name,
95 source,
96 line,
97 col,
98 }
93 } 99 }
94} 100}
95 101
@@ -108,7 +114,7 @@ impl<'a> Lexer<'a> {
108 } 114 }
109 } 115 }
110 116
111 pub fn next_token(&mut self) -> Result<(Span, Token<'a>), LispError> { 117 pub fn next_token(&mut self) -> Result<(Span, Token<'a>), ParseError> {
112 let mut chars = self.input.char_indices(); 118 let mut chars = self.input.char_indices();
113 119
114 while let Some((ind, chr)) = chars.next() { 120 while let Some((ind, chr)) = chars.next() {
@@ -142,13 +148,13 @@ impl<'a> Lexer<'a> {
142 let (size, token) = match res { 148 let (size, token) = match res {
143 Ok(v) => v, 149 Ok(v) => v,
144 Err(kind) => { 150 Err(kind) => {
145 return Err(LispError::Parse(ParseError { 151 return Err(ParseError {
146 span: Span { 152 span: Span {
147 low, 153 low,
148 high: low + chr.len_utf8() as u32, 154 high: low + chr.len_utf8() as u32,
149 }, 155 },
150 kind, 156 kind,
151 })) 157 })
152 } 158 }
153 }; 159 };
154 self.cur_pos += size as u32; 160 self.cur_pos += size as u32;