Skip to content

Commit b20104e

Browse files
committed
Refactor expression and parser to use Location struct for line and column tracking
- Updated Expression and Statement structs to replace u32 line numbers with Location struct for better tracking of token positions. - Modified Parser implementation to handle Location instead of line numbers, ensuring accurate error reporting and debugging. - Adjusted tokenizer functions to return tokens with Location information, enhancing the ability to pinpoint errors in the source code. - Updated pest implementation to utilize Location for improved error handling and code clarity. Signed-off-by: zuisong <com.me@foxmail.com>
1 parent d5a55e4 commit b20104e

File tree

9 files changed

+414
-367
lines changed

9 files changed

+414
-367
lines changed

src/compiler.rs

Lines changed: 130 additions & 128 deletions
Large diffs are not rendered by default.

src/expression.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::clone::Clone;
22
use std::fmt::Debug;
33

4+
use crate::tokenizer::Location;
45
use crate::tokenizer::Operator;
56
use crate::value::Value;
67

@@ -13,57 +14,57 @@ pub enum Literal {
1314
pub struct FunctionCall {
1415
pub callee: Box<Expression>,
1516
pub arguments: Vec<Expression>,
16-
pub line: u32,
17+
pub loc: Location,
1718
}
1819

1920
#[derive(Debug, PartialEq, Clone)]
2021
pub struct MethodCall {
2122
pub object: Box<Expression>,
2223
pub method: String,
2324
pub arguments: Vec<Expression>,
24-
pub line: u32,
25+
pub loc: Location,
2526
}
2627

2728
#[derive(Debug, PartialEq, Clone)]
2829
pub struct BinaryOperation {
2930
pub operator: Operator,
3031
pub left: Box<Expression>,
3132
pub right: Box<Expression>,
32-
pub line: u32,
33+
pub loc: Location,
3334
}
3435

3536
#[derive(Debug, PartialEq, Clone)]
3637
pub enum Expression {
3738
FunctionCall(FunctionCall),
3839
MethodCall(MethodCall),
3940
BinaryOperation(BinaryOperation),
40-
Literal(Literal, u32),
41+
Literal(Literal, Location),
4142
Unary(Unary),
42-
Identifier(String, u32),
43-
Block(Vec<Statement>, u32),
43+
Identifier(String, Location),
44+
Block(Vec<Statement>, Location),
4445
If(If),
4546
/// 对象字面量: ${ k: v, ... }
46-
ObjectLiteral(Vec<(String, Expression)>, u32),
47+
ObjectLiteral(Vec<(String, Expression)>, Location),
4748
/// 数组字面量
48-
ArrayLiteral(Vec<Expression>, u32),
49+
ArrayLiteral(Vec<Expression>, Location),
4950
/// 属性访问: obj.field
5051
GetField {
5152
object: Box<Expression>,
5253
field: String,
53-
line: u32,
54+
loc: Location,
5455
},
5556
/// 索引访问: obj[expr]
5657
Index {
5758
object: Box<Expression>,
5859
index: Box<Expression>,
59-
line: u32,
60+
loc: Location,
6061
},
6162
/// 函数定义表达式 (匿名函数/Lambda)
6263
Function(FunctionDeclaration),
6364
/// Import 表达式: import "path"
6465
Import {
6566
path: String,
66-
line: u32,
67+
loc: Location,
6768
},
6869
}
6970

@@ -72,28 +73,28 @@ pub struct FunctionDeclaration {
7273
pub name: Option<String>,
7374
pub parameters: Vec<String>,
7475
pub body: Vec<Statement>,
75-
pub line: u32,
76+
pub loc: Location,
7677
}
7778

7879
#[derive(Debug, PartialEq, Clone)]
7980
pub struct If {
8081
pub test: Box<Expression>,
8182
pub body: Vec<Statement>,
8283
pub else_body: Vec<Statement>,
83-
pub line: u32,
84+
pub loc: Location,
8485
}
8586

8687
#[derive(Debug, PartialEq, Clone)]
8788
pub struct Local {
8889
pub name: String,
8990
pub expression: Expression,
90-
pub line: u32,
91+
pub loc: Location,
9192
}
9293

9394
#[derive(Debug, PartialEq, Clone)]
9495
pub struct Return {
9596
pub expression: Expression,
96-
pub line: u32,
97+
pub loc: Location,
9798
}
9899

99100
#[derive(Debug, PartialEq, Clone)]
@@ -109,23 +110,23 @@ pub enum Statement {
109110
object: Expression,
110111
field: String,
111112
value: Expression,
112-
line: u32,
113+
loc: Location,
113114
},
114115
/// 设置索引: obj[index] = value
115116
SetIndex {
116117
object: Expression,
117118
index: Expression,
118119
value: Expression,
119-
line: u32,
120+
loc: Location,
120121
},
121-
Break(u32),
122-
Continue(u32),
122+
Break(Location),
123+
Continue(Location),
123124
/// Try-Catch-Finally 异常处理
124125
TryCatch(TryCatch),
125126
/// Throw 抛出异常
126127
Throw {
127128
value: Expression,
128-
line: u32,
129+
loc: Location,
129130
},
130131
}
131132

@@ -136,7 +137,7 @@ pub type Ast = Vec<Statement>;
136137
pub struct Unary {
137138
pub operator: Operator,
138139
pub expr: Box<Expression>,
139-
pub line: u32,
140+
pub loc: Location,
140141
}
141142

142143
/// 赋值语句
@@ -146,7 +147,7 @@ pub struct Assign {
146147
pub name: String,
147148
/// 赋值语句右边的表达式
148149
pub expr: Box<Expression>,
149-
pub line: u32,
150+
pub loc: Location,
150151
}
151152

152153
/// 循环语句
@@ -156,7 +157,7 @@ pub struct Loop {
156157
pub test: Expression,
157158
/// 循环语句里面要执行的语句块
158159
pub body: Vec<Statement>,
159-
pub line: u32,
160+
pub loc: Location,
160161
}
161162

162163
/// Try-Catch-Finally 异常处理
@@ -170,5 +171,5 @@ pub struct TryCatch {
170171
pub catch_body: Vec<Statement>,
171172
/// finally 块中的语句(可选)
172173
pub finally_body: Option<Vec<Statement>>,
173-
pub line: u32,
174+
pub loc: Location,
174175
}

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,20 @@ where
115115
fn build_diagnostic(code: &str, file_id: usize, error: &ChenError) -> Diagnostic<usize> {
116116
match error {
117117
ChenError::Runtime(err) => {
118-
let range = get_line_range(code, err.line);
118+
let range = get_line_range(code, err.loc.line);
119119
Diagnostic::error().with_message(err.to_string()).with_labels(vec![
120120
Label::primary(file_id, range).with_message("Runtime error occurred here"),
121121
])
122122
}
123123
ChenError::Parser(parser::ParserError::Handwritten(err)) => match err {
124-
parser::handwritten::ParseError::Message { msg, line } => {
125-
let range = get_line_range(code, *line);
124+
parser::handwritten::ParseError::Message { msg, loc } => {
125+
let range = get_line_range(code, loc.line);
126126
Diagnostic::error()
127127
.with_message(msg)
128128
.with_labels(vec![Label::primary(file_id, range).with_message("Parse error here")])
129129
}
130-
parser::handwritten::ParseError::UnexpectedToken { token, line } => {
131-
let range = get_line_range(code, *line);
130+
parser::handwritten::ParseError::UnexpectedToken { token, loc } => {
131+
let range = get_line_range(code, loc.line);
132132
Diagnostic::error()
133133
.with_message(format!("Unexpected token: {:?}", token))
134134
.with_labels(vec![Label::primary(file_id, range).with_message("Unexpected token")])

0 commit comments

Comments
 (0)