Skip to content

Commit 654a42a

Browse files
authored
remove line numbering because it is much better to use offsets with indexing (#66)
1 parent 58b4156 commit 654a42a

File tree

3 files changed

+13
-54
lines changed

3 files changed

+13
-54
lines changed

crates/cellang-cli/src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ fn main() -> Result<(), Error> {
212212
struct LexToken {
213213
kind: String,
214214
lexeme: String,
215-
line: usize,
216215
offset: usize,
217216
span_len: usize,
218217
}
@@ -222,7 +221,6 @@ impl<'src> From<Token<'src>> for LexToken {
222221
LexToken {
223222
kind: format!("{:?}", token.ty),
224223
lexeme: token.origin.to_string(),
225-
line: token.line,
226224
offset: token.offset,
227225
span_len: token.span_len,
228226
}

crates/cellang/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(unused_assignments)]
2+
13
use crate::value::ValueError;
24
use miette::{Diagnostic, SourceSpan};
35
use std::error::Error as StdError;
@@ -23,6 +25,7 @@ pub struct SyntaxError {
2325

2426
impl SyntaxError {
2527
/// Creates a new syntax error capturing the offending source span.
28+
#[allow(unused_assignments)]
2629
pub fn new(
2730
source_code: impl Into<String>,
2831
span: SourceSpan,

crates/cellang/src/lexer.rs

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub struct Token<'src> {
1111
pub origin: &'src str,
1212
pub offset: usize,
1313
pub span_len: usize,
14-
pub line: usize,
1514
pub ty: TokenType,
1615
}
1716

@@ -294,7 +293,6 @@ pub struct Lexer<'src> {
294293
whole: &'src str,
295294
rest: &'src str,
296295
byte: usize,
297-
line: usize,
298296
peeked: Option<Result<Token<'src>, SyntaxError>>,
299297
}
300298

@@ -305,7 +303,6 @@ impl<'src> Lexer<'src> {
305303
whole: input,
306304
rest: input,
307305
byte: 0,
308-
line: 1,
309306
peeked: None,
310307
}
311308
}
@@ -396,11 +393,9 @@ impl<'src> Iterator for Lexer<'src> {
396393
OrEqual(TokenType, TokenType),
397394
}
398395

399-
let line = self.line;
400396
let just = move |ty: TokenType| {
401397
Some(Ok(Token {
402398
ty,
403-
line,
404399
offset: c_at,
405400
span_len: c_str.len(),
406401
origin: c_str,
@@ -449,9 +444,7 @@ impl<'src> Iterator for Lexer<'src> {
449444
return Some(Err(SyntaxError::new(
450445
self.whole.to_string(),
451446
SourceSpan::new(c_at.into(), self.byte - c_at),
452-
format!(
453-
"[line {line}] Error: Unexpected character: {c}"
454-
),
447+
format!("Error: Unexpected character: {c}"),
455448
)
456449
.with_help("expected `&&`")));
457450
}
@@ -465,9 +458,7 @@ impl<'src> Iterator for Lexer<'src> {
465458
return Some(Err(SyntaxError::new(
466459
self.whole.to_string(),
467460
SourceSpan::new(c_at.into(), self.byte - c_at),
468-
format!(
469-
"[line {line}] Error: Unexpected character: {c}"
470-
),
461+
format!("Error: Unexpected character: {c}"),
471462
)
472463
.with_help("expected `||`")));
473464
}
@@ -511,19 +502,14 @@ impl<'src> Iterator for Lexer<'src> {
511502
_ => Started::Ident,
512503
},
513504
'\n' | '\r' => {
514-
self.line += 1;
515505
continue;
516506
}
517507
c if c.is_whitespace() => continue,
518508
c => {
519-
let line = self.line;
520-
521509
return Some(Err(SyntaxError::new(
522510
self.whole.to_string(),
523511
SourceSpan::new(c_at.into(), self.byte - c_at),
524-
format!(
525-
"[line {line}] Error: Unexpected character: {c}"
526-
),
512+
format!("Error: Unexpected character: {c}"),
527513
)));
528514
}
529515
};
@@ -541,7 +527,6 @@ impl<'src> Iterator for Lexer<'src> {
541527
} else {
542528
Some(Ok(Token {
543529
ty: TokenType::Slash,
544-
line: self.line,
545530
offset: c_at,
546531
span_len: self.byte - c_at,
547532
origin: c_str,
@@ -558,7 +543,6 @@ impl<'src> Iterator for Lexer<'src> {
558543
self.read_extra(extra_bytes);
559544
Some(Ok(Token {
560545
ty: TokenType::String,
561-
line: self.line,
562546
offset: c_at,
563547
span_len: self.byte - c_at,
564548
origin: literal,
@@ -575,7 +559,6 @@ impl<'src> Iterator for Lexer<'src> {
575559
self.read_extra(n);
576560
Some(Ok(Token {
577561
ty: TokenType::RawString,
578-
line: self.line,
579562
offset: c_at,
580563
span_len: self.byte - c_at,
581564
origin: literal,
@@ -591,7 +574,6 @@ impl<'src> Iterator for Lexer<'src> {
591574
self.read_extra(n);
592575
Some(Ok(Token {
593576
ty: TokenType::Bytes,
594-
line: self.line,
595577
offset: c_at,
596578
span_len: self.byte - c_at,
597579
origin: literal,
@@ -609,7 +591,6 @@ impl<'src> Iterator for Lexer<'src> {
609591
self.read_extra(extra_bytes);
610592
Some(Ok(Token {
611593
ty: TokenType::RawBytes,
612-
line: self.line,
613594
offset: c_at,
614595
span_len: self.byte - c_at,
615596
origin: literal,
@@ -634,15 +615,13 @@ impl<'src> Iterator for Lexer<'src> {
634615
if literal == "dyn" {
635616
Some(Ok(Token {
636617
ty: TokenType::Dyn,
637-
line: self.line,
638618
offset: c_at,
639619
span_len,
640620
origin: literal,
641621
}))
642622
} else {
643623
Some(Ok(Token {
644624
ty,
645-
line: self.line,
646625
offset: c_at,
647626
span_len,
648627
origin: literal,
@@ -701,21 +680,12 @@ impl<'src> Iterator for Lexer<'src> {
701680

702681
match state {
703682
State::Nil => {
704-
let line = self.line;
705-
706-
return Some(Err(
707-
SyntaxError::new(
708-
self.whole.to_string(),
709-
SourceSpan::new(
710-
c_at.into(),
711-
self.byte - c_at,
712-
),
713-
format!(
714-
"[line {line}] Error: Unexpected character: {c}"
715-
),
716-
)
717-
.with_help("Expected a number"),
718-
));
683+
return Some(Err(SyntaxError::new(
684+
self.whole.to_string(),
685+
SourceSpan::new(c_at.into(), self.byte - c_at),
686+
format!("Error: Unexpected character: {c}"),
687+
)
688+
.with_help("Expected a number")));
719689
}
720690
State::Dot | State::Exp => {
721691
// unread the dot or exp
@@ -726,7 +696,6 @@ impl<'src> Iterator for Lexer<'src> {
726696
ty: TokenType::Int(
727697
c_onwards[..read].parse().unwrap(),
728698
),
729-
line: self.line,
730699
offset: c_at,
731700
span_len: self.byte - c_at,
732701
origin: &c_onwards[..read],
@@ -741,7 +710,6 @@ impl<'src> Iterator for Lexer<'src> {
741710
ty: TokenType::Int(
742711
c_onwards[..read].parse().unwrap(),
743712
),
744-
line: self.line,
745713
offset: c_at,
746714
span_len: self.byte - c_at,
747715
origin: &c_onwards[..read],
@@ -754,7 +722,6 @@ impl<'src> Iterator for Lexer<'src> {
754722
ty: TokenType::Int(
755723
c_onwards[..index].parse().unwrap(),
756724
),
757-
line: self.line,
758725
offset: c_at,
759726
span_len: self.byte - c_at,
760727
origin: &c_onwards[..index],
@@ -767,7 +734,6 @@ impl<'src> Iterator for Lexer<'src> {
767734
ty: TokenType::Uint(
768735
c_onwards[..index - 1].parse().unwrap(),
769736
),
770-
line: self.line,
771737
offset: c_at,
772738
span_len: self.byte - c_at,
773739
origin: &c_onwards[..index],
@@ -780,7 +746,6 @@ impl<'src> Iterator for Lexer<'src> {
780746
ty: TokenType::Double(
781747
c_onwards[..index].parse().unwrap(),
782748
),
783-
line: self.line,
784749
offset: c_at,
785750
span_len: self.byte - c_at,
786751
origin: &c_onwards[..index],
@@ -791,13 +756,10 @@ impl<'src> Iterator for Lexer<'src> {
791756
Started::HexNumber => {
792757
let rest = &c_onwards[2..]; // ignore 0x
793758
if rest.is_empty() {
794-
let line = self.line;
795759
return Some(Err(SyntaxError::new(
796760
self.whole.to_string(),
797761
SourceSpan::new(c_at.into(), self.byte - c_at),
798-
format!(
799-
"[line {line}] Error: Unexpected character: {c}"
800-
),
762+
format!("Error: Unexpected character: {c}"),
801763
)
802764
.with_help("Expected a number")));
803765
}
@@ -823,7 +785,6 @@ impl<'src> Iterator for Lexer<'src> {
823785
ty: TokenType::Uint(
824786
u64::from_str_radix(&literal[2..], 16).unwrap(),
825787
),
826-
line: self.line,
827788
offset: c_at,
828789
span_len: self.byte - c_at,
829790
origin: literal,
@@ -834,7 +795,6 @@ impl<'src> Iterator for Lexer<'src> {
834795
ty: TokenType::Int(
835796
i64::from_str_radix(&literal[2..], 16).unwrap(),
836797
),
837-
line: self.line,
838798
offset: c_at,
839799
span_len: self.byte - c_at,
840800
origin: literal,
@@ -848,15 +808,13 @@ impl<'src> Iterator for Lexer<'src> {
848808
self.byte += 1;
849809
Some(Ok(Token {
850810
ty: or_else,
851-
line: self.line,
852811
offset: c_at,
853812
span_len: self.byte - c_at,
854813
origin: span,
855814
}))
856815
} else {
857816
Some(Ok(Token {
858817
ty: token,
859-
line: self.line,
860818
offset: c_at,
861819
span_len: self.byte - c_at,
862820
origin: c_str,

0 commit comments

Comments
 (0)