authorgravatar for me@paperdave.netdave caruso <me@paperdave.net> 2024-02-02 22:30:49-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-02-02 22:30:49-08:00
logf7fcc6aa17b5528e2cecce40d36fd37840f80dd1
tree53bb8692a45fd848c0677a61378cf91d9e177757
parentb417db0d1e1f2ce026092e1b310e2c98e7de265f
signature Signed by PGP key B5690EEEBB952194

chore: rename tokenizer::Loc -> log::Range (#15)


34 files changed, 362 insertions(+), 255 deletions(-)

Cargo.lock+80-2
...@@ -69,6 +69,7 @@ dependencies = [...@@ -69,6 +69,7 @@ dependencies = [
69name = "ast"69name = "ast"
70version = "0.1.0"70version = "0.1.0"
71dependencies = [71dependencies = [
72 "log",
72 "miette",73 "miette",
73 "num",74 "num",
74 "thiserror",75 "thiserror",
...@@ -174,6 +175,7 @@ dependencies = [...@@ -174,6 +175,7 @@ dependencies = [
174 "clap",175 "clap",
175 "js_ast",176 "js_ast",
176 "js_printer",177 "js_printer",
178 "log",
177 "miette",179 "miette",
178 "parser",180 "parser",
179 "sema",181 "sema",
...@@ -197,6 +199,15 @@ dependencies = [...@@ -197,6 +199,15 @@ dependencies = [
197 "windows-sys",199 "windows-sys",
198]200]
199201
202[[package]]
203name = "form_urlencoded"
204version = "1.2.1"
205source = "registry+https://github.com/rust-lang/crates.io-index"
206checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456"
207dependencies = [
208 "percent-encoding",
209]
210
200[[package]]211[[package]]
201name = "gimli"212name = "gimli"
202version = "0.28.1"213version = "0.28.1"
...@@ -215,6 +226,16 @@ version = "0.3.3"...@@ -215,6 +226,16 @@ version = "0.3.3"
215source = "registry+https://github.com/rust-lang/crates.io-index"226source = "registry+https://github.com/rust-lang/crates.io-index"
216checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"227checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
217228
229[[package]]
230name = "idna"
231version = "0.5.0"
232source = "registry+https://github.com/rust-lang/crates.io-index"
233checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6"
234dependencies = [
235 "unicode-bidi",
236 "unicode-normalization",
237]
238
218[[package]]239[[package]]
219name = "is-terminal"240name = "is-terminal"
220version = "0.4.10"241version = "0.4.10"
...@@ -237,11 +258,10 @@ name = "js_ast"...@@ -237,11 +258,10 @@ name = "js_ast"
237version = "0.1.0"258version = "0.1.0"
238dependencies = [259dependencies = [
239 "ast",260 "ast",
261 "log",
240 "miette",262 "miette",
241 "num",263 "num",
242 "parser",
243 "thiserror",264 "thiserror",
244 "tokenizer",
245]265]
246266
247[[package]]267[[package]]
...@@ -266,6 +286,15 @@ version = "0.4.12"...@@ -266,6 +286,15 @@ version = "0.4.12"
266source = "registry+https://github.com/rust-lang/crates.io-index"286source = "registry+https://github.com/rust-lang/crates.io-index"
267checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"287checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
268288
289[[package]]
290name = "log"
291version = "0.0.0"
292dependencies = [
293 "miette",
294 "thiserror",
295 "url",
296]
297
269[[package]]298[[package]]
270name = "memchr"299name = "memchr"
271version = "2.7.1"300version = "2.7.1"
...@@ -415,12 +444,19 @@ name = "parser"...@@ -415,12 +444,19 @@ name = "parser"
415version = "0.1.0"444version = "0.1.0"
416dependencies = [445dependencies = [
417 "ast",446 "ast",
447 "log",
418 "miette",448 "miette",
419 "num",449 "num",
420 "thiserror",450 "thiserror",
421 "tokenizer",451 "tokenizer",
422]452]
423453
454[[package]]
455name = "percent-encoding"
456version = "2.3.1"
457source = "registry+https://github.com/rust-lang/crates.io-index"
458checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
459
424[[package]]460[[package]]
425name = "proc-macro2"461name = "proc-macro2"
426version = "1.0.76"462version = "1.0.76"
...@@ -560,14 +596,36 @@ dependencies = [...@@ -560,14 +596,36 @@ dependencies = [
560 "syn",596 "syn",
561]597]
562598
599[[package]]
600name = "tinyvec"
601version = "1.6.0"
602source = "registry+https://github.com/rust-lang/crates.io-index"
603checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
604dependencies = [
605 "tinyvec_macros",
606]
607
608[[package]]
609name = "tinyvec_macros"
610version = "0.1.1"
611source = "registry+https://github.com/rust-lang/crates.io-index"
612checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
613
563[[package]]614[[package]]
564name = "tokenizer"615name = "tokenizer"
565version = "0.1.0"616version = "0.1.0"
566dependencies = [617dependencies = [
618 "log",
567 "miette",619 "miette",
568 "thiserror",620 "thiserror",
569]621]
570622
623[[package]]
624name = "unicode-bidi"
625version = "0.3.15"
626source = "registry+https://github.com/rust-lang/crates.io-index"
627checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75"
628
571[[package]]629[[package]]
572name = "unicode-ident"630name = "unicode-ident"
573version = "1.0.12"631version = "1.0.12"
...@@ -580,12 +638,32 @@ version = "0.1.5"...@@ -580,12 +638,32 @@ version = "0.1.5"
580source = "registry+https://github.com/rust-lang/crates.io-index"638source = "registry+https://github.com/rust-lang/crates.io-index"
581checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f"639checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f"
582640
641[[package]]
642name = "unicode-normalization"
643version = "0.1.22"
644source = "registry+https://github.com/rust-lang/crates.io-index"
645checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
646dependencies = [
647 "tinyvec",
648]
649
583[[package]]650[[package]]
584name = "unicode-width"651name = "unicode-width"
585version = "0.1.11"652version = "0.1.11"
586source = "registry+https://github.com/rust-lang/crates.io-index"653source = "registry+https://github.com/rust-lang/crates.io-index"
587checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"654checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
588655
656[[package]]
657name = "url"
658version = "2.5.0"
659source = "registry+https://github.com/rust-lang/crates.io-index"
660checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633"
661dependencies = [
662 "form_urlencoded",
663 "idna",
664 "percent-encoding",
665]
666
589[[package]]667[[package]]
590name = "utf8parse"668name = "utf8parse"
591version = "0.2.1"669version = "0.2.1"
Cargo.toml+3-1
...@@ -9,11 +9,12 @@ repository = "https://github.com/lino-levan/blight"...@@ -9,11 +9,12 @@ repository = "https://github.com/lino-levan/blight"
9resolver = "2"9resolver = "2"
10members = [10members = [
11 "crates/ast",11 "crates/ast",
12 "crates/sema",
13 "crates/cli",12 "crates/cli",
14 "crates/js_ast",13 "crates/js_ast",
15 "crates/js_printer",14 "crates/js_printer",
15 "crates/log",
16 "crates/parser",16 "crates/parser",
17 "crates/sema",
17 "crates/tokenizer",18 "crates/tokenizer",
18]19]
1920
...@@ -22,3 +23,4 @@ clap = { version = "4.4.12", features = ["derive"] }...@@ -22,3 +23,4 @@ clap = { version = "4.4.12", features = ["derive"] }
22miette = { version = "5.10.0", features = ["fancy"] }23miette = { version = "5.10.0", features = ["fancy"] }
23thiserror = "1.0.53"24thiserror = "1.0.53"
24num = "0.4.1"25num = "0.4.1"
26url = "2.2.2"
crates/ast/Cargo.toml+2-1
...@@ -11,4 +11,5 @@ miette.workspace = true...@@ -11,4 +11,5 @@ miette.workspace = true
11thiserror.workspace = true11thiserror.workspace = true
12num.workspace = true12num.workspace = true
1313
14tokenizer = { path = "../tokenizer" }14tokenizer = { path = "../tokenizer" } # Do not depend on the tokenizer
15log = { path = "../log" }
crates/ast/ast.rs+19-17
...@@ -1,9 +1,7 @@...@@ -1,9 +1,7 @@
1use tokenizer::Loc;1use log::Range;
22
3use crate::{3use crate::types::{Literal, Type};
4 types::{Literal, Type},4use crate::{BinaryOperator, UnaryOperator};
5 BinaryOperator, UnaryOperator,
6};
75
8#[derive(Debug, Clone)]6#[derive(Debug, Clone)]
9pub enum BuiltinId {7pub enum BuiltinId {
...@@ -36,7 +34,7 @@ pub struct VarDecl {...@@ -36,7 +34,7 @@ pub struct VarDecl {
36 pub init: Expr,34 pub init: Expr,
37 pub is_constant: bool,35 pub is_constant: bool,
38 pub is_pub: bool,36 pub is_pub: bool,
39 pub loc: Loc,37 pub range: Range,
40 pub comment: Option<DocComment>,38 pub comment: Option<DocComment>,
41}39}
4240
...@@ -47,27 +45,31 @@ pub struct FnDecl {...@@ -47,27 +45,31 @@ pub struct FnDecl {
47 pub params: Vec<Param>,45 pub params: Vec<Param>,
48 pub body: Expr,46 pub body: Expr,
49 pub is_pub: bool,47 pub is_pub: bool,
50 pub loc: Loc,48 pub range: Range,
51 pub comment: Option<DocComment>,49 pub comment: Option<DocComment>,
52}50}
5351
54#[derive(Debug, Clone)]52#[derive(Debug, Clone)]
55pub struct Expr {53pub struct Expr {
56 pub data: ExprData,54 pub data: ExprData,
57 pub loc: Loc,55 pub range: Range,
58 pub comment: Option<Comment>,56 pub comment: Option<Comment>,
59}57}
6058
61impl Expr {59impl Expr {
62 pub fn new(data: ExprData, loc: Loc, comment: Option<Comment>) -> Self {60 pub fn new(data: ExprData, range: Range, comment: Option<Comment>) -> Self {
63 Self { data, loc, comment }61 Self {
62 data,
63 range,
64 comment,
65 }
64 }66 }
65}67}
6668
67#[derive(Debug, Clone)]69#[derive(Debug, Clone)]
68pub struct DocComment {70pub struct DocComment {
69 pub data: String,71 pub data: String,
70 pub loc: Loc,72 pub range: Range,
71}73}
7274
73#[derive(Debug, PartialEq, Clone)]75#[derive(Debug, PartialEq, Clone)]
...@@ -86,10 +88,10 @@ impl Comment {...@@ -86,10 +88,10 @@ impl Comment {
86 result88 result
87 }89 }
8890
89 pub fn to_doc_comment(self, loc: Loc) -> DocComment {91 pub fn to_doc_comment(self, range: Range) -> DocComment {
90 DocComment {92 DocComment {
91 data: self.data,93 data: self.data,
92 loc,94 range,
93 }95 }
94 }96 }
95}97}
...@@ -113,21 +115,21 @@ impl DocComment {...@@ -113,21 +115,21 @@ impl DocComment {
113}115}
114116
115#[derive(Debug, Clone)]117#[derive(Debug, Clone)]
116pub struct BlockWithLoc {118pub struct BlockWithRange {
117 pub block: Block,119 pub block: Block,
118 pub loc: Loc,120 pub range: Range,
119}121}
120122
121#[derive(Debug, Clone)]123#[derive(Debug, Clone)]
122pub struct If {124pub struct If {
123 pub condition: Box<Expr>,125 pub condition: Box<Expr>,
124 pub yes: BlockWithLoc,126 pub yes: BlockWithRange,
125 pub no: Option<BlockOrIf>,127 pub no: Option<BlockOrIf>,
126}128}
127129
128#[derive(Debug, Clone)]130#[derive(Debug, Clone)]
129pub enum BlockOrIf {131pub enum BlockOrIf {
130 Block(BlockWithLoc),132 Block(BlockWithRange),
131 If(Box<If>),133 If(Box<If>),
132}134}
133135
crates/ast/types.rs-74
...@@ -120,80 +120,6 @@ pub fn merge_types(a: Type, b: Type) -> Result<Type> {...@@ -120,80 +120,6 @@ pub fn merge_types(a: Type, b: Type) -> Result<Type> {
120 Err(TypeError::new(format!("TODO: Implement type merging for {} and {}", a, b)).into())120 Err(TypeError::new(format!("TODO: Implement type merging for {} and {}", a, b)).into())
121}121}
122122
123// pub fn coerce_type(target: Type, source: Type) -> Result<Type> {
124// match source {
125// Type::Any => return Ok(target),
126// _ => {}
127// }
128// match target {
129// Type::Infer => Ok(source),
130// Type::Any => Ok(Type::Any),
131
132// Type::Literal(_) => unreachable!(),
133
134// Type::Int => match source {
135// Type::Int => Ok(Type::Int),
136// Type::Float => Ok(Type::Int),
137// Type::Literal(value) => {
138// if value.fract() != 0.0 {
139// Err(
140// TypeError::new(format!("Numeric Literal '{}' is not an integer", value))
141// .into(),
142// )
143// } else if value > i32::MAX.into() || value < i32::MIN.into() {
144// Err(TypeError::new(format!(
145// "Numeric Literal '{}' is out of range for an integer ({}..{})",
146// value,
147// i32::MIN,
148// i32::MAX
149// ))
150// .into())
151// } else {
152// Ok(Type::Int)
153// }
154// }
155// t => Err(TypeError::new(format!("Cannot coerce {} to int", t)).into()),
156// },
157
158// Type::BigInt => match source {
159// Type::Int => Ok(Type::Int),
160// Type::Float => Ok(Type::Int),
161// Type::NumberLiteral(value) => {
162// if value.fract() != 0.0 {
163// Err(
164// TypeError::new(format!("Numeric Literal '{}' is not an integer", value))
165// .into(),
166// )
167// } else {
168// Ok(Type::BigInt)
169// }
170// }
171// t => Err(TypeError::new(format!("Cannot coerce {} to int", t)).into()),
172// },
173
174// Type::Float => match source {
175// Type::Int => Ok(Type::Float),
176// Type::Float => Ok(Type::Float),
177// Type::NumberLiteral(value) => {
178// if value.fract() == 0.0 && value <= f64::MAX.into() && value >= f64::MIN.into() {
179// Ok(Type::Float)
180// } else {
181// Err(TypeError::new(format!("Numeric Literal")).into())
182// }
183// }
184// _ => Err(TypeError::new(format!("Cannot coerce int to float")).into()),
185// },
186
187// other => {
188// if other == source {
189// Ok(other)
190// } else {
191// Err(TypeError::new(format!("Cannot coerce {} to {}", source, other)).into())
192// }
193// }
194// }
195// }
196
197#[derive(Error, Diagnostic, Debug)]123#[derive(Error, Diagnostic, Debug)]
198#[error("TypeError")]124#[error("TypeError")]
199pub struct TypeError {125pub struct TypeError {
crates/cli/Cargo.toml+1
...@@ -18,3 +18,4 @@ parser = { path = "../parser" }...@@ -18,3 +18,4 @@ parser = { path = "../parser" }
18js_ast = { path = "../js_ast" }18js_ast = { path = "../js_ast" }
19js_printer = { path = "../js_printer" }19js_printer = { path = "../js_printer" }
20sema = { path = "../sema" }20sema = { path = "../sema" }
21log = { path = "../log" }
crates/js_ast/Cargo.toml+1-2
...@@ -12,5 +12,4 @@ thiserror.workspace = true...@@ -12,5 +12,4 @@ thiserror.workspace = true
12num.workspace = true12num.workspace = true
1313
14ast = { path = "../ast" }14ast = { path = "../ast" }
15tokenizer = { path = "../tokenizer" } # TODO: move Loc out of the tokenizer15log = { path = "../log" }
16parser = { path = "../parser" } # TODO: move Loc out of the tokenizer
crates/js_ast/convert/block_convert_context.rs+8-8
...@@ -1,8 +1,8 @@...@@ -1,8 +1,8 @@
1use crate::js_ast::*;1use crate::js_ast::*;
2use ast::*;2use ast::*;
3use log::Range;
3use std::collections::{HashMap, HashSet};4use std::collections::{HashMap, HashSet};
4use std::vec;5use std::vec;
5use tokenizer::Loc;
66
7use super::tree_shake_statement_list;7use super::tree_shake_statement_list;
88
...@@ -198,33 +198,33 @@ impl BlockConvertContext {...@@ -198,33 +198,33 @@ impl BlockConvertContext {
198 }198 }
199}199}
200200
201pub fn new_js_assignment_statement(expr: JsExpr, target: String, loc: Loc) -> JsStatement {201pub fn new_js_assignment_statement(expr: JsExpr, target: String, range: Range) -> JsStatement {
202 JsStatement::new(202 JsStatement::new(
203 JsStatementData::Expression(new_js_assignment_expr(expr, target, loc.clone())),203 JsStatementData::Expression(new_js_assignment_expr(expr, target, range.clone())),
204 loc,204 range,
205 None,205 None,
206 )206 )
207}207}
208208
209pub fn new_js_assignment_expr(expr: JsExpr, target: String, loc: Loc) -> JsExpr {209pub fn new_js_assignment_expr(expr: JsExpr, target: String, range: Range) -> JsExpr {
210 JsExpr::new(210 JsExpr::new(
211 JsExprData::Binary {211 JsExprData::Binary {
212 op: BinaryOperator::Assign,212 op: BinaryOperator::Assign,
213 left: Box::new(JsExpr::new(213 left: Box::new(JsExpr::new(
214 JsExprData::Identifier(target),214 JsExprData::Identifier(target),
215 loc.clone(),215 range.clone(),
216 None,216 None,
217 )),217 )),
218 right: Box::new(expr),218 right: Box::new(expr),
219 },219 },
220 loc,220 range,
221 None,221 None,
222 )222 )
223}223}
224224
225impl Into<JsBlock> for BlockConvertContext {225impl Into<JsBlock> for BlockConvertContext {
226 fn into(self) -> JsBlock {226 fn into(self) -> JsBlock {
227 JsBlock::new(self.into(), Loc::EMPTY)227 JsBlock::new(self.into(), Range::EMPTY)
228 }228 }
229}229}
230230
crates/js_ast/convert/convert_valid_block.rs+6-7
...@@ -3,18 +3,18 @@ use super::BlockConvertContext;...@@ -3,18 +3,18 @@ use super::BlockConvertContext;
3use super::IsValidJSExpression;3use super::IsValidJSExpression;
4use crate::js_ast::*;4use crate::js_ast::*;
5use ast::*;5use ast::*;
6use tokenizer::Loc;6use log::Range;
77
8pub fn convert_valid_block(ctx: &mut BlockConvertContext, block: Block, loc: Loc) -> JsExpr {8pub fn convert_valid_block(ctx: &mut BlockConvertContext, block: Block, range: Range) -> JsExpr {
9 debug_assert!(9 debug_assert!(
10 block.is_valid_js_expression(),10 block.is_valid_js_expression(),
11 "must call convert_valid_block with valid js expression"11 "must call convert_valid_block with valid js expression"
12 );12 );
1313
14 let len = block.statements.len();14 let len = block.statements.len();
15 let loc = loc;15 let range = range;
16 let block_expr = match len {16 let block_expr = match len {
17 0 => JsExpr::new(JsExprData::Null, loc.clone(), None),17 0 => JsExpr::new(JsExprData::Null, range.clone(), None),
18 // a block expression with a single expression is just that expression18 // a block expression with a single expression is just that expression
19 1 => convert_valid_statement_to_expr(ctx, block.statements.into_iter().next().unwrap()),19 1 => convert_valid_statement_to_expr(ctx, block.statements.into_iter().next().unwrap()),
20 // Since blocks are not valid expressions in JS, we create a20 // Since blocks are not valid expressions in JS, we create a
...@@ -24,14 +24,13 @@ pub fn convert_valid_block(ctx: &mut BlockConvertContext, block: Block, loc: Loc...@@ -24,14 +24,13 @@ pub fn convert_valid_block(ctx: &mut BlockConvertContext, block: Block, loc: Loc
24 for statement in block.statements.into_iter() {24 for statement in block.statements.into_iter() {
25 items.push(convert_valid_statement_to_expr(ctx, statement));25 items.push(convert_valid_statement_to_expr(ctx, statement));
26 }26 }
27 JsExpr::new(JsExprData::Comma { items }, loc.clone(), None)27 JsExpr::new(JsExprData::Comma { items }, range.clone(), None)
28 }28 }
29 _ => todo!(),
30 };29 };
3130
32 // a block without implicit return is to be type void, so we wrap in a void expression31 // a block without implicit return is to be type void, so we wrap in a void expression
33 if !block.has_implicit_return && len > 0 {32 if !block.has_implicit_return && len > 0 {
34 JsExpr::new(JsExprData::Void(Box::new(block_expr)), loc, None)33 JsExpr::new(JsExprData::Void(Box::new(block_expr)), range, None)
35 } else {34 } else {
36 block_expr35 block_expr
37 }36 }
crates/js_ast/convert/convert_valid_expr.rs+14-14
...@@ -16,28 +16,28 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {...@@ -16,28 +16,28 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {
16 "must call convert_valid_expr with valid js expression (use hoist instead)"16 "must call convert_valid_expr with valid js expression (use hoist instead)"
17 );17 );
1818
19 let loc = expr.loc;19 let range = expr.range;
20 let comment = expr.comment;20 let comment = expr.comment;
2121
22 match expr.data {22 match expr.data {
23 ExprData::Identifier(str) => JsExpr::new(23 ExprData::Identifier(str) => JsExpr::new(
24 JsExprData::Identifier(ctx.remap_identifier(&str)),24 JsExprData::Identifier(ctx.remap_identifier(&str)),
25 loc,25 range,
26 comment,26 comment,
27 ),27 ),
2828
29 ExprData::Literal(Literal::Bool(value)) => {29 ExprData::Literal(Literal::Bool(value)) => {
30 JsExpr::new(JsExprData::Bool(value), loc, comment)30 JsExpr::new(JsExprData::Bool(value), range, comment)
31 }31 }
32 ExprData::Literal(Literal::Number(value)) => {32 ExprData::Literal(Literal::Number(value)) => {
33 JsExpr::new(JsExprData::Number(value), loc, comment)33 JsExpr::new(JsExprData::Number(value), range, comment)
34 }34 }
35 ExprData::Literal(Literal::String(value)) => {35 ExprData::Literal(Literal::String(value)) => {
36 JsExpr::new(JsExprData::String(value), loc, comment)36 JsExpr::new(JsExprData::String(value), range, comment)
37 }37 }
38 ExprData::Literal(Literal::Void) => JsExpr::new(JsExprData::Null, loc, comment),38 ExprData::Literal(Literal::Void) => JsExpr::new(JsExprData::Null, range, comment),
3939
40 ExprData::Block(block) => convert_valid_block(ctx, block, loc),40 ExprData::Block(block) => convert_valid_block(ctx, block, range),
4141
42 ExprData::Binary { op, left, right } => {42 ExprData::Binary { op, left, right } => {
43 let left = convert_valid_expr(ctx, *left);43 let left = convert_valid_expr(ctx, *left);
...@@ -48,7 +48,7 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {...@@ -48,7 +48,7 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {
48 left: Box::new(left),48 left: Box::new(left),
49 right: Box::new(right),49 right: Box::new(right),
50 },50 },
51 loc,51 range,
52 comment,52 comment,
53 )53 )
54 }54 }
...@@ -59,12 +59,12 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {...@@ -59,12 +59,12 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {
59 op,59 op,
60 value: Box::new(value),60 value: Box::new(value),
61 },61 },
62 loc,62 range,
63 comment,63 comment,
64 )64 )
65 }65 }
66 ExprData::Fn { body, params, .. } => {66 ExprData::Fn { body, params, .. } => {
67 process_function_to_arrow(ctx, *body, params, loc, comment)67 process_function_to_arrow(ctx, *body, params, range, comment)
68 }68 }
69 ExprData::Builtin { id, arguments } => match id {69 ExprData::Builtin { id, arguments } => match id {
70 BuiltinId::Raw => {70 BuiltinId::Raw => {
...@@ -74,10 +74,10 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {...@@ -74,10 +74,10 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {
74 ExprData::Literal(Literal::String(s)) => s,74 ExprData::Literal(Literal::String(s)) => s,
75 _ => panic!("raw expects a string"),75 _ => panic!("raw expects a string"),
76 };76 };
77 JsExpr::new(JsExprData::Raw(str), loc, comment)77 JsExpr::new(JsExprData::Raw(str), range, comment)
78 }78 }
79 },79 },
80 ExprData::If(expr) => convert_valid_if(ctx, expr, loc, comment),80 ExprData::If(expr) => convert_valid_if(ctx, expr, range, comment),
8181
82 ExprData::Call { base, arguments } => {82 ExprData::Call { base, arguments } => {
83 let base = convert_valid_expr(ctx, *base);83 let base = convert_valid_expr(ctx, *base);
...@@ -90,7 +90,7 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {...@@ -90,7 +90,7 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {
90 base: Box::new(base),90 base: Box::new(base),
91 arguments,91 arguments,
92 },92 },
93 loc,93 range,
94 comment,94 comment,
95 )95 )
96 }96 }
...@@ -106,7 +106,7 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {...@@ -106,7 +106,7 @@ pub fn convert_valid_expr(ctx: &mut BlockConvertContext, expr: Expr) -> JsExpr {
106 index: Box::new(convert_valid_expr(ctx, *field)),106 index: Box::new(convert_valid_expr(ctx, *field)),
107 },107 },
108 },108 },
109 loc,109 range,
110 comment,110 comment,
111 ),111 ),
112112
crates/js_ast/convert/convert_valid_if.rs+6-6
...@@ -4,13 +4,13 @@ use super::BlockConvertContext;...@@ -4,13 +4,13 @@ use super::BlockConvertContext;
4use crate::convert::IsValidJSExpression;4use crate::convert::IsValidJSExpression;
5use crate::js_ast::*;5use crate::js_ast::*;
6use ast::*;6use ast::*;
7use tokenizer::Loc;7use log::Range;
88
9/// Convert a valid blight if statement to a JS expression9/// Convert a valid blight if statement to a JS expression
10pub fn convert_valid_if(10pub fn convert_valid_if(
11 ctx: &mut BlockConvertContext,11 ctx: &mut BlockConvertContext,
12 expr: If,12 expr: If,
13 loc: Loc,13 range: Range,
14 comment: Option<Comment>,14 comment: Option<Comment>,
15) -> JsExpr {15) -> JsExpr {
16 debug_assert!(16 debug_assert!(
...@@ -19,10 +19,10 @@ pub fn convert_valid_if(...@@ -19,10 +19,10 @@ pub fn convert_valid_if(
19 );19 );
20 let condition = convert_valid_expr(ctx, *expr.condition);20 let condition = convert_valid_expr(ctx, *expr.condition);
2121
22 let yes = convert_valid_block(ctx, expr.yes.block, expr.yes.loc);22 let yes = convert_valid_block(ctx, expr.yes.block, expr.yes.range);
23 let no = match expr.no {23 let no = match expr.no {
24 Some(BlockOrIf::Block(b)) => Some(convert_valid_block(ctx, b.block, b.loc)),24 Some(BlockOrIf::Block(b)) => Some(convert_valid_block(ctx, b.block, b.range)),
25 Some(BlockOrIf::If(else_if)) => Some(convert_valid_if(ctx, *else_if, Loc::EMPTY, None)),25 Some(BlockOrIf::If(else_if)) => Some(convert_valid_if(ctx, *else_if, Range::EMPTY, None)),
26 None => None,26 None => None,
27 };27 };
2828
...@@ -32,7 +32,7 @@ pub fn convert_valid_if(...@@ -32,7 +32,7 @@ pub fn convert_valid_if(
32 yes: Box::new(yes),32 yes: Box::new(yes),
33 no: no.map(Box::new),33 no: no.map(Box::new),
34 },34 },
35 loc,35 range,
36 comment,36 comment,
37 )37 )
38}38}
crates/js_ast/convert/convert_valid_statement.rs+2-2
...@@ -27,6 +27,6 @@ pub fn convert_valid_statement_to_expr(...@@ -27,6 +27,6 @@ pub fn convert_valid_statement_to_expr(
27/// TODO(@paperdave): Replace this with Into<JsStatement> for JsExpr27/// TODO(@paperdave): Replace this with Into<JsStatement> for JsExpr
28pub fn convert_valid_statement(ctx: &mut BlockConvertContext, statement: Statement) -> JsStatement {28pub fn convert_valid_statement(ctx: &mut BlockConvertContext, statement: Statement) -> JsStatement {
29 let expr = convert_valid_statement_to_expr(ctx, statement);29 let expr = convert_valid_statement_to_expr(ctx, statement);
30 let loc = expr.loc.clone();30 let range = expr.range.clone();
31 JsStatement::new(JsStatementData::Expression(expr), loc, None)31 JsStatement::new(JsStatementData::Expression(expr), range, None)
32}32}
crates/js_ast/convert/hoist_block_to_expr.rs+5-5
...@@ -3,7 +3,7 @@ use super::process_statement;...@@ -3,7 +3,7 @@ use super::process_statement;
3use super::BlockConvertContext;3use super::BlockConvertContext;
4use crate::js_ast::*;4use crate::js_ast::*;
5use ast::*;5use ast::*;
6use tokenizer::Loc;6use log::Range;
77
8/// This block hoist is used when we have a block in an expression context.8/// This block hoist is used when we have a block in an expression context.
9///9///
...@@ -30,7 +30,7 @@ use tokenizer::Loc;...@@ -30,7 +30,7 @@ use tokenizer::Loc;
30pub fn hoist_block_to_expr(30pub fn hoist_block_to_expr(
31 ctx: &mut BlockConvertContext,31 ctx: &mut BlockConvertContext,
32 block: Block,32 block: Block,
33 loc: Loc,33 range: Range,
34 comment: Option<Comment>,34 comment: Option<Comment>,
35) -> Option<JsExpr> {35) -> Option<JsExpr> {
36 debug_assert!(36 debug_assert!(
...@@ -57,7 +57,7 @@ pub fn hoist_block_to_expr(...@@ -57,7 +57,7 @@ pub fn hoist_block_to_expr(
57 _ => unreachable!(),57 _ => unreachable!(),
58 }58 }
59 }59 }
60 _ => JsExpr::new(JsExprData::Null, loc.clone(), None),60 _ => JsExpr::new(JsExprData::Null, range.clone(), None),
61 };61 };
6262
63 ctx.add_statement(JsStatement::new(63 ctx.add_statement(JsStatement::new(
...@@ -66,13 +66,13 @@ pub fn hoist_block_to_expr(...@@ -66,13 +66,13 @@ pub fn hoist_block_to_expr(
66 init: Some(result),66 init: Some(result),
67 is_constant: false,67 is_constant: false,
68 }),68 }),
69 loc.clone(),69 range.clone(),
70 comment,70 comment,
71 ));71 ));
7272
73 Some(JsExpr::new(73 Some(JsExpr::new(
74 JsExprData::Identifier(temporary.get(&ctx)),74 JsExprData::Identifier(temporary.get(&ctx)),
75 loc,75 range,
76 None,76 None,
77 ))77 ))
78}78}
crates/js_ast/convert/hoist_block_to_js_block.rs+6-6
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1use crate::js_ast::*;1use crate::js_ast::*;
2use ast::*;2use ast::*;
3use tokenizer::Loc;3use log::Range;
44
5use super::*;5use super::*;
66
...@@ -22,7 +22,7 @@ use super::*;...@@ -22,7 +22,7 @@ use super::*;
22pub fn hoist_block_to_js_block(22pub fn hoist_block_to_js_block(
23 ctx: &mut BlockConvertContext,23 ctx: &mut BlockConvertContext,
24 block: Block,24 block: Block,
25 loc: Loc,25 range: Range,
26 target: &TempId,26 target: &TempId,
27 did_assign: &mut bool,27 did_assign: &mut bool,
28) -> JsBlock {28) -> JsBlock {
...@@ -34,11 +34,11 @@ pub fn hoist_block_to_js_block(...@@ -34,11 +34,11 @@ pub fn hoist_block_to_js_block(
34 if block.is_valid_js_expression() {34 if block.is_valid_js_expression() {
35 return JsBlock::new(35 return JsBlock::new(
36 vec![new_js_assignment_statement(36 vec![new_js_assignment_statement(
37 convert_valid_block(ctx, block, loc.clone()),37 convert_valid_block(ctx, block, range.clone()),
38 target.get(ctx),38 target.get(ctx),
39 loc.clone(),39 range.clone(),
40 )],40 )],
41 loc,41 range,
42 );42 );
43 }43 }
4444
...@@ -59,7 +59,7 @@ pub fn hoist_block_to_js_block(...@@ -59,7 +59,7 @@ pub fn hoist_block_to_js_block(
59 child.add_statement(new_js_assignment_statement(59 child.add_statement(new_js_assignment_statement(
60 expr,60 expr,
61 target.get(ctx),61 target.get(ctx),
62 loc.clone(),62 range.clone(),
63 ));63 ));
64 }64 }
65 _ => {}65 _ => {}
crates/js_ast/convert/hoist_expr.rs+14-10
...@@ -18,7 +18,7 @@ pub fn hoist_expr_if_needed(ctx: &mut BlockConvertContext, expr: Expr) -> Option...@@ -18,7 +18,7 @@ pub fn hoist_expr_if_needed(ctx: &mut BlockConvertContext, expr: Expr) -> Option
18 let init = hoist_expr_contents(ctx, expr)?;18 let init = hoist_expr_contents(ctx, expr)?;
1919
20 let id = ctx.generate_temp_id("_");20 let id = ctx.generate_temp_id("_");
21 let loc = init.loc.clone();21 let range = init.range.clone();
2222
23 ctx.add_statement(JsStatement::new(23 ctx.add_statement(JsStatement::new(
24 JsStatementData::Declaration(JSDeclaration {24 JsStatementData::Declaration(JSDeclaration {
...@@ -26,11 +26,15 @@ pub fn hoist_expr_if_needed(ctx: &mut BlockConvertContext, expr: Expr) -> Option...@@ -26,11 +26,15 @@ pub fn hoist_expr_if_needed(ctx: &mut BlockConvertContext, expr: Expr) -> Option
26 init: Some(init),26 init: Some(init),
27 is_constant: true,27 is_constant: true,
28 }),28 }),
29 loc.clone(),29 range.clone(),
30 None,30 None,
31 ));31 ));
3232
33 Some(JsExpr::new(JsExprData::Identifier(id.get(&ctx)), loc, None))33 Some(JsExpr::new(
34 JsExprData::Identifier(id.get(&ctx)),
35 range,
36 None,
37 ))
34}38}
3539
36/// Returning `None` from this means that the expression is noreturn, and that40/// Returning `None` from this means that the expression is noreturn, and that
...@@ -51,7 +55,7 @@ pub fn hoist_expr_contents(ctx: &mut BlockConvertContext, expr: Expr) -> Option<...@@ -51,7 +55,7 @@ pub fn hoist_expr_contents(ctx: &mut BlockConvertContext, expr: Expr) -> Option<
51 left: Box::new(left),55 left: Box::new(left),
52 right: Box::new(right),56 right: Box::new(right),
53 },57 },
54 expr.loc,58 expr.range,
55 expr.comment,59 expr.comment,
56 ))60 ))
57 }61 }
...@@ -63,7 +67,7 @@ pub fn hoist_expr_contents(ctx: &mut BlockConvertContext, expr: Expr) -> Option<...@@ -63,7 +67,7 @@ pub fn hoist_expr_contents(ctx: &mut BlockConvertContext, expr: Expr) -> Option<
63 op,67 op,
64 value: Box::new(value),68 value: Box::new(value),
65 },69 },
66 expr.loc,70 expr.range,
67 expr.comment,71 expr.comment,
68 ))72 ))
69 }73 }
...@@ -85,24 +89,24 @@ pub fn hoist_expr_contents(ctx: &mut BlockConvertContext, expr: Expr) -> Option<...@@ -85,24 +89,24 @@ pub fn hoist_expr_contents(ctx: &mut BlockConvertContext, expr: Expr) -> Option<
85 index: Box::new(field),89 index: Box::new(field),
86 },90 },
87 },91 },
88 expr.loc,92 expr.range,
89 expr.comment,93 expr.comment,
90 ))94 ))
91 }95 }
9296
93 ExprData::Block(block) => hoist_block_to_expr(ctx, block, expr.loc, expr.comment),97 ExprData::Block(block) => hoist_block_to_expr(ctx, block, expr.range, expr.comment),
9498
95 ExprData::If(_) => hoist_if_expr(ctx, expr),99 ExprData::If(_) => hoist_if_expr(ctx, expr),
96100
97 ExprData::Return(maybe_expr) => {101 ExprData::Return(maybe_expr) => {
98 let loc = expr.loc.clone();102 let range = expr.range.clone();
99 let return_value = match maybe_expr {103 let return_value = match maybe_expr {
100 Some(expr) => Some(hoist_expr_if_needed(ctx, *expr)?),104 Some(expr) => Some(hoist_expr_if_needed(ctx, *expr)?),
101 None => None,105 None => None,
102 };106 };
103 ctx.add_statement(JsStatement::new(107 ctx.add_statement(JsStatement::new(
104 JsStatementData::Return(return_value),108 JsStatementData::Return(return_value),
105 loc,109 range,
106 None,110 None,
107 ));111 ));
108 None112 None
...@@ -120,7 +124,7 @@ pub fn hoist_expr_contents(ctx: &mut BlockConvertContext, expr: Expr) -> Option<...@@ -120,7 +124,7 @@ pub fn hoist_expr_contents(ctx: &mut BlockConvertContext, expr: Expr) -> Option<
120 base: Box::new(base),124 base: Box::new(base),
121 arguments,125 arguments,
122 },126 },
123 expr.loc,127 expr.range,
124 expr.comment,128 expr.comment,
125 ))129 ))
126 }130 }
crates/js_ast/convert/hoist_if.rs+9-9
...@@ -6,7 +6,7 @@ use super::IsValidJSExpression;...@@ -6,7 +6,7 @@ use super::IsValidJSExpression;
6use super::TempId;6use super::TempId;
7use crate::js_ast::*;7use crate::js_ast::*;
8use ast::*;8use ast::*;
9use tokenizer::Loc;9use log::Range;
1010
11pub fn hoist_if_expr(ctx: &mut BlockConvertContext, expr: Expr) -> Option<JsExpr> {11pub fn hoist_if_expr(ctx: &mut BlockConvertContext, expr: Expr) -> Option<JsExpr> {
12 let data = match expr.data {12 let data = match expr.data {
...@@ -22,7 +22,7 @@ pub fn hoist_if_expr(ctx: &mut BlockConvertContext, expr: Expr) -> Option<JsExpr...@@ -22,7 +22,7 @@ pub fn hoist_if_expr(ctx: &mut BlockConvertContext, expr: Expr) -> Option<JsExpr
22 init: None,22 init: None,
23 is_constant: false,23 is_constant: false,
24 }),24 }),
25 expr.loc.clone(),25 expr.range.clone(),
26 None,26 None,
27 ));27 ));
2828
...@@ -40,35 +40,35 @@ pub fn hoist_if_expr(ctx: &mut BlockConvertContext, expr: Expr) -> Option<JsExpr...@@ -40,35 +40,35 @@ pub fn hoist_if_expr(ctx: &mut BlockConvertContext, expr: Expr) -> Option<JsExpr
4040
41 ctx.add_statement(JsStatement::new(41 ctx.add_statement(JsStatement::new(
42 JsStatementData::If(js_if),42 JsStatementData::If(js_if),
43 expr.loc.clone(),43 expr.range.clone(),
44 None,44 None,
45 ));45 ));
4646
47 if did_assign {47 if did_assign {
48 Some(JsExpr::new(48 Some(JsExpr::new(
49 JsExprData::Identifier(temporary.get(&ctx)),49 JsExprData::Identifier(temporary.get(&ctx)),
50 expr.loc,50 expr.range,
51 expr.comment,51 expr.comment,
52 ))52 ))
53 } else {53 } else {
54 decl.delete(ctx);54 decl.delete(ctx);
55 Some(JsExpr::new(JsExprData::Null, expr.loc, expr.comment))55 Some(JsExpr::new(JsExprData::Null, expr.range, expr.comment))
56 }56 }
57}57}
5858
59pub fn hoist_if_branch(59pub fn hoist_if_branch(
60 ctx: &mut BlockConvertContext,60 ctx: &mut BlockConvertContext,
61 yes: BlockWithLoc,61 yes: BlockWithRange,
62 no: Option<BlockOrIf>,62 no: Option<BlockOrIf>,
63 condition: JsExpr,63 condition: JsExpr,
64 did_assign: &mut bool,64 did_assign: &mut bool,
65 temporary: &TempId,65 temporary: &TempId,
66) -> JsIf {66) -> JsIf {
67 let yes = hoist_block_to_js_block(ctx, yes.block, yes.loc, &temporary, did_assign);67 let yes = hoist_block_to_js_block(ctx, yes.block, yes.range, &temporary, did_assign);
6868
69 let no = match no {69 let no = match no {
70 Some(BlockOrIf::Block(no)) => Some(JsElse::Block(hoist_block_to_js_block(70 Some(BlockOrIf::Block(no)) => Some(JsElse::Block(hoist_block_to_js_block(
71 ctx, no.block, no.loc, &temporary, did_assign,71 ctx, no.block, no.range, &temporary, did_assign,
72 ))),72 ))),
73 Some(BlockOrIf::If(else_if)) => {73 Some(BlockOrIf::If(else_if)) => {
74 // If the condition of this block has a side effect, we need to74 // If the condition of this block has a side effect, we need to
...@@ -99,7 +99,7 @@ pub fn hoist_if_branch(...@@ -99,7 +99,7 @@ pub fn hoist_if_branch(
99 did_assign,99 did_assign,
100 temporary,100 temporary,
101 )),101 )),
102 Loc::EMPTY,102 Range::EMPTY,
103 None,103 None,
104 ));104 ));
105105
crates/js_ast/convert/is_valid_js_expression.rs+1-1
...@@ -75,7 +75,7 @@ impl IsValidJSExpression for BlockOrIf {...@@ -75,7 +75,7 @@ impl IsValidJSExpression for BlockOrIf {
75 }75 }
76}76}
7777
78impl IsValidJSExpression for BlockWithLoc {78impl IsValidJSExpression for BlockWithRange {
79 fn is_valid_js_expression(&self) -> bool {79 fn is_valid_js_expression(&self) -> bool {
80 self.block.is_valid_js_expression()80 self.block.is_valid_js_expression()
81 }81 }
crates/js_ast/convert/process_function.rs+5-5
...@@ -5,13 +5,13 @@ use super::BlockConvertContext;...@@ -5,13 +5,13 @@ use super::BlockConvertContext;
5use super::IsValidJSExpression;5use super::IsValidJSExpression;
6use crate::js_ast::*;6use crate::js_ast::*;
7use ast::*;7use ast::*;
8use tokenizer::Loc;8use log::Range;
99
10pub fn process_function_to_arrow(10pub fn process_function_to_arrow(
11 ctx: &mut BlockConvertContext,11 ctx: &mut BlockConvertContext,
12 body: Expr,12 body: Expr,
13 params: Vec<Param>,13 params: Vec<Param>,
14 loc: Loc,14 range: Range,
15 comment: Option<Comment>,15 comment: Option<Comment>,
16) -> JsExpr {16) -> JsExpr {
17 // A block that is a valid expression will just become17 // A block that is a valid expression will just become
...@@ -34,7 +34,7 @@ pub fn process_function_to_arrow(...@@ -34,7 +34,7 @@ pub fn process_function_to_arrow(
34 if let Some(expr) = child.take_last_statement_if_expr() {34 if let Some(expr) = child.take_last_statement_if_expr() {
35 child.add_statement(JsStatement::new(35 child.add_statement(JsStatement::new(
36 JsStatementData::Return(Some(expr)),36 JsStatementData::Return(Some(expr)),
37 loc.clone(),37 range.clone(),
38 None,38 None,
39 ));39 ));
40 }40 }
...@@ -44,7 +44,7 @@ pub fn process_function_to_arrow(...@@ -44,7 +44,7 @@ pub fn process_function_to_arrow(
44 if let Some(expr) = hoist_expr_if_needed(&mut child, body) {44 if let Some(expr) = hoist_expr_if_needed(&mut child, body) {
45 child.add_statement(JsStatement::new(45 child.add_statement(JsStatement::new(
46 JsStatementData::Return(Some(expr)),46 JsStatementData::Return(Some(expr)),
47 loc.clone(),47 range.clone(),
48 None,48 None,
49 ));49 ));
50 }50 }
...@@ -59,7 +59,7 @@ pub fn process_function_to_arrow(...@@ -59,7 +59,7 @@ pub fn process_function_to_arrow(
59 params: params.into_iter().map(|x| x.name).collect(),59 params: params.into_iter().map(|x| x.name).collect(),
60 body: Box::new(body),60 body: Box::new(body),
61 },61 },
62 loc.clone(),62 range.clone(),
63 comment.map(|x| x.into()),63 comment.map(|x| x.into()),
64 )64 )
65}65}
crates/js_ast/convert/process_statement.rs+6-6
...@@ -35,7 +35,7 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->...@@ -35,7 +35,7 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->
35 } else {35 } else {
36 JsStatementData::Declaration(decl_data)36 JsStatementData::Declaration(decl_data)
37 },37 },
38 decl.loc,38 decl.range,
39 decl.comment.map(|x| x.into()),39 decl.comment.map(|x| x.into()),
40 );40 );
4141
...@@ -45,10 +45,10 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->...@@ -45,10 +45,10 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->
45 }45 }
46 Statement::Expr(expr) => {46 Statement::Expr(expr) => {
47 if let Some(expr) = hoist_expr_contents(ctx, expr) {47 if let Some(expr) = hoist_expr_contents(ctx, expr) {
48 let loc = expr.loc.clone();48 let range = expr.range.clone();
49 ctx.add_statement(JsStatement::new(49 ctx.add_statement(JsStatement::new(
50 JsStatementData::Expression(expr),50 JsStatementData::Expression(expr),
51 loc,51 range,
52 None,52 None,
53 ));53 ));
54 Some(())54 Some(())
...@@ -60,7 +60,7 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->...@@ -60,7 +60,7 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->
60 name,60 name,
61 params,61 params,
62 body,62 body,
63 loc,63 range,
64 comment,64 comment,
65 is_pub,65 is_pub,
66 ..66 ..
...@@ -71,7 +71,7 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->...@@ -71,7 +71,7 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->
71 ctx,71 ctx,
72 body,72 body,
73 params,73 params,
74 loc.clone(),74 range.clone(),
75 None,75 None,
76 ));76 ));
7777
...@@ -87,7 +87,7 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->...@@ -87,7 +87,7 @@ pub fn process_statement(ctx: &mut BlockConvertContext, statement: Statement) ->
87 } else {87 } else {
88 JsStatementData::Declaration(decl_data)88 JsStatementData::Declaration(decl_data)
89 },89 },
90 loc,90 range,
91 comment.map(|x| x.into()),91 comment.map(|x| x.into()),
92 ));92 ));
9393
crates/js_ast/convert/side_effects.rs+1-1
...@@ -84,7 +84,7 @@ impl HasSideEffects for BlockOrIf {...@@ -84,7 +84,7 @@ impl HasSideEffects for BlockOrIf {
84 }84 }
85}85}
8686
87impl HasSideEffects for BlockWithLoc {87impl HasSideEffects for BlockWithRange {
88 fn has_side_effects(&self) -> bool {88 fn has_side_effects(&self) -> bool {
89 self.block.has_side_effects()89 self.block.has_side_effects()
90 }90 }
crates/js_ast/js_ast.rs+18-10
...@@ -1,11 +1,11 @@...@@ -1,11 +1,11 @@
1use ast::{BinaryOperator, Comment, UnaryOperator};1use ast::{BinaryOperator, Comment, UnaryOperator};
2use log::Range;
2use num::bigint::BigInt;3use num::bigint::BigInt;
3use tokenizer::Loc;
44
5#[derive(Debug, PartialEq, Clone)]5#[derive(Debug, PartialEq, Clone)]
6pub struct JsExpr {6pub struct JsExpr {
7 pub data: JsExprData,7 pub data: JsExprData,
8 pub loc: Loc,8 pub range: Range,
9 pub comment: Option<Comment>,9 pub comment: Option<Comment>,
10}10}
1111
...@@ -74,7 +74,7 @@ pub enum JsExprData {...@@ -74,7 +74,7 @@ pub enum JsExprData {
74#[derive(Debug, PartialEq, Clone)]74#[derive(Debug, PartialEq, Clone)]
75pub struct JsStatement {75pub struct JsStatement {
76 pub data: JsStatementData,76 pub data: JsStatementData,
77 pub loc: Loc,77 pub range: Range,
78 pub comment: Option<Comment>,78 pub comment: Option<Comment>,
79}79}
8080
...@@ -133,12 +133,16 @@ pub struct JSNamedExport {...@@ -133,12 +133,16 @@ pub struct JSNamedExport {
133#[derive(Debug, PartialEq, Clone)]133#[derive(Debug, PartialEq, Clone)]
134pub struct JsBlock {134pub struct JsBlock {
135 pub statements: Vec<JsStatement>,135 pub statements: Vec<JsStatement>,
136 pub loc: Loc,136 pub range: Range,
137}137}
138138
139impl JsExpr {139impl JsExpr {
140 pub fn new(data: JsExprData, loc: Loc, comment: Option<Comment>) -> Self {140 pub fn new(data: JsExprData, range: Range, comment: Option<Comment>) -> Self {
141 Self { data, loc, comment }141 Self {
142 data,
143 range,
144 comment,
145 }
142 }146 }
143147
144 pub fn is_literal(&self) -> bool {148 pub fn is_literal(&self) -> bool {
...@@ -167,14 +171,18 @@ impl JsExprData {...@@ -167,14 +171,18 @@ impl JsExprData {
167}171}
168172
169impl JsBlock {173impl JsBlock {
170 pub fn new(statements: Vec<JsStatement>, loc: Loc) -> Self {174 pub fn new(statements: Vec<JsStatement>, range: Range) -> Self {
171 Self { statements, loc }175 Self { statements, range }
172 }176 }
173}177}
174178
175impl JsStatement {179impl JsStatement {
176 pub fn new(data: JsStatementData, loc: Loc, comment: Option<Comment>) -> Self {180 pub fn new(data: JsStatementData, range: Range, comment: Option<Comment>) -> Self {
177 Self { data, loc, comment }181 Self {
182 data,
183 range,
184 comment,
185 }
178 }186 }
179}187}
180188
crates/log/Cargo.toml created+11
...@@ -0,0 +1,11 @@
1[package]
2name = "log"
3edition = "2021"
4
5[lib]
6path="./log.rs"
7
8[dependencies]
9miette.workspace = true
10thiserror.workspace = true
11url.workspace = true
crates/log/log.rs created+88
...@@ -0,0 +1,88 @@
1use std::{fs, path::Path, rc::Rc};
2
3use miette::{IntoDiagnostic, Result};
4use url::Url;
5
6#[derive(Debug, PartialEq, Clone, Copy)]
7pub struct Range {
8 pub start: usize,
9 pub length: usize,
10}
11
12impl Range {
13 /// TODO(@paperdave): Remove this. every usage of this is incorrect.
14 pub const EMPTY: Range = Range {
15 start: 0,
16 length: 0,
17 };
18
19 pub fn new(start: usize, length: usize) -> Self {
20 Self { start, length }
21 }
22
23 pub fn len(&self) -> usize {
24 self.length
25 }
26}
27
28// TODO(@lino-levan): make this loggable nicely
29#[derive(Debug, Clone)]
30pub struct Message<'a> {
31 pub source: &'a Source,
32 pub range: Range,
33 pub message: String,
34 pub notes: Vec<Note<'a>>,
35}
36
37#[derive(Debug, Clone)]
38pub struct Note<'a> {
39 pub location: Option<RangeAndSource<'a>>,
40 pub message: String,
41}
42
43#[derive(Debug, Clone)]
44pub struct RangeAndSource<'a> {
45 pub range: Range,
46 pub source: &'a Source,
47}
48
49#[derive(Debug)]
50pub struct Source {
51 pub url: Url,
52 pub code: String,
53}
54
55impl Source {
56 pub fn new(url: Url, source_code: String) -> Rc<Self> {
57 Rc::new(Source {
58 url,
59 code: source_code,
60 })
61 }
62
63 pub fn from_file(path: &Path) -> Result<Rc<Self>> {
64 let source_code = fs::read_to_string(path).into_diagnostic()?;
65 let url = Url::from_file_path(path).unwrap();
66 Ok(Rc::new(Source {
67 code: source_code,
68 url,
69 }))
70 }
71
72 pub fn from_url(url: Url) -> Result<Rc<Self>> {
73 match url.scheme() {
74 "file" => {
75 // TODO: how to return this error, into_diagnostic() doesn't exist
76 let file_path = url.to_file_path().unwrap();
77 let source_code = fs::read_to_string(file_path).into_diagnostic()?;
78 Ok(Rc::new(Source {
79 code: source_code,
80 url,
81 }))
82 }
83 _ => {
84 todo!("Fetch from URL: {}", url)
85 }
86 }
87 }
88}
crates/parser/Cargo.toml+1
...@@ -13,3 +13,4 @@ num.workspace = true...@@ -13,3 +13,4 @@ num.workspace = true
1313
14ast = { path = "../ast" }14ast = { path = "../ast" }
15tokenizer = { path = "../tokenizer" }15tokenizer = { path = "../tokenizer" }
16log = { path = "../log" }
crates/parser/parse_declaration.rs+3-2
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1use ast::{DocComment, Type, VarDecl};1use ast::{DocComment, Type, VarDecl};
2use log::Range;
2use miette::Result;3use miette::Result;
3use tokenizer::{Loc, TokenType};4use tokenizer::TokenType;
45
5use crate::parse_expr::parse_expr;6use crate::parse_expr::parse_expr;
6use crate::parse_type::parse_type;7use crate::parse_type::parse_type;
...@@ -45,6 +46,6 @@ pub fn parse_declaration(...@@ -45,6 +46,6 @@ pub fn parse_declaration(
45 is_constant,46 is_constant,
46 is_pub,47 is_pub,
47 comment,48 comment,
48 loc: Loc::new(start, p.index - start),49 range: Range::new(start, p.index - start),
49 })50 })
50}51}
crates/parser/parse_doc_comment.rs+3-2
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1use ast::DocComment;1use ast::DocComment;
2use tokenizer::{Loc, TokenType};2use log::Range;
3use tokenizer::TokenType;
34
4use crate::Parser;5use crate::Parser;
56
...@@ -10,7 +11,7 @@ pub fn maybe_read_doc_comment(p: &mut Parser) -> Option<DocComment> {...@@ -10,7 +11,7 @@ pub fn maybe_read_doc_comment(p: &mut Parser) -> Option<DocComment> {
10 p.next();11 p.next();
11 Some(DocComment {12 Some(DocComment {
12 data,13 data,
13 loc: Loc::new(start, p.index - start),14 range: Range::new(start, p.index - start),
14 })15 })
15 }16 }
16 _ => None,17 _ => None,
crates/parser/parse_expr.rs+2-2
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1use ast::Associativity::*;1use ast::Associativity::*;
2use ast::{BinaryOperator, Expr, ExprData, OperatorLevel};2use ast::{BinaryOperator, Expr, ExprData, OperatorLevel};
3use log::Range;
3use miette::Result;4use miette::Result;
4use tokenizer::Loc;
55
6use crate::parse_expr_basic::parse_expr_basic;6use crate::parse_expr_basic::parse_expr_basic;
7use crate::Parser;7use crate::Parser;
...@@ -94,7 +94,7 @@ fn parse_expr_binary(...@@ -94,7 +94,7 @@ fn parse_expr_binary(
94 left: Box::new(left),94 left: Box::new(left),
95 right: Box::new(right),95 right: Box::new(right),
96 },96 },
97 Loc::new(start, p.index - start),97 Range::new(start, p.index - start),
98 None,98 None,
99 ))99 ))
100}100}
crates/parser/parse_expr_basic.rs+19-18
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1use ast::{BuiltinId, Expr, ExprData, Literal, UnaryOperator};1use ast::{BuiltinId, Expr, ExprData, Literal, UnaryOperator};
2use log::Range;
2use miette::Result;3use miette::Result;
3use tokenizer::{Loc, TokenType};4use tokenizer::TokenType;
45
5use crate::{6use crate::{
6 parse_block::parse_block, parse_call_arguments::parse_call_arguments, parse_expr::parse_expr,7 parse_block::parse_block, parse_call_arguments::parse_call_arguments, parse_expr::parse_expr,
...@@ -15,7 +16,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -15,7 +16,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
15 _ = p.next();16 _ = p.next();
16 Expr::new(17 Expr::new(
17 ExprData::Literal(Literal::Number(number.clone())),18 ExprData::Literal(Literal::Number(number.clone())),
18 Loc::new(start, p.index - start),19 Range::new(start, p.index - start),
19 comments,20 comments,
20 )21 )
21 }22 }
...@@ -23,7 +24,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -23,7 +24,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
23 _ = p.next();24 _ = p.next();
24 Expr::new(25 Expr::new(
25 ExprData::Literal(Literal::String(string.clone())),26 ExprData::Literal(Literal::String(string.clone())),
26 Loc::new(start, p.index - start),27 Range::new(start, p.index - start),
27 comments,28 comments,
28 )29 )
29 }30 }
...@@ -31,7 +32,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -31,7 +32,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
31 _ = p.next();32 _ = p.next();
32 Expr::new(33 Expr::new(
33 ExprData::Literal(Literal::Bool(true)),34 ExprData::Literal(Literal::Bool(true)),
34 Loc::new(start, p.index - start),35 Range::new(start, p.index - start),
35 comments,36 comments,
36 )37 )
37 }38 }
...@@ -39,7 +40,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -39,7 +40,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
39 _ = p.next();40 _ = p.next();
40 Expr::new(41 Expr::new(
41 ExprData::Literal(Literal::Bool(false)),42 ExprData::Literal(Literal::Bool(false)),
42 Loc::new(start, p.index - start),43 Range::new(start, p.index - start),
43 comments,44 comments,
44 )45 )
45 }46 }
...@@ -47,7 +48,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -47,7 +48,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
47 _ = p.next();48 _ = p.next();
48 Expr::new(49 Expr::new(
49 ExprData::Literal(Literal::Void),50 ExprData::Literal(Literal::Void),
50 Loc::new(start, p.index - start),51 Range::new(start, p.index - start),
51 comments,52 comments,
52 )53 )
53 }54 }
...@@ -60,14 +61,14 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -60,14 +61,14 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
60 let arguments = parse_call_arguments(p)?;61 let arguments = parse_call_arguments(p)?;
61 Expr::new(62 Expr::new(
62 ExprData::Builtin { id, arguments },63 ExprData::Builtin { id, arguments },
63 Loc::new(identifier_start, p.index - identifier_start),64 Range::new(identifier_start, p.index - identifier_start),
64 comments,65 comments,
65 )66 )
66 } else {67 } else {
67 return Err(p68 return Err(p
68 .error_at(69 .error_at(
69 format!("Built-in function '{}' must be called", name),70 format!("Built-in function '{}' must be called", name),
70 Loc::new(identifier_start, p.index - identifier_start),71 Range::new(identifier_start, p.index - identifier_start),
71 )72 )
72 .into());73 .into());
73 }74 }
...@@ -75,14 +76,14 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -75,14 +76,14 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
75 return Err(p76 return Err(p
76 .error_at(77 .error_at(
77 format!("Non-existant built-in: '{}'", name),78 format!("Non-existant built-in: '{}'", name),
78 Loc::new(identifier_start, p.index - identifier_start),79 Range::new(identifier_start, p.index - identifier_start),
79 )80 )
80 .into());81 .into());
81 }82 }
82 } else {83 } else {
83 Expr::new(84 Expr::new(
84 ExprData::Identifier(name),85 ExprData::Identifier(name),
85 Loc::new(identifier_start, p.index - identifier_start),86 Range::new(identifier_start, p.index - identifier_start),
86 comments,87 comments,
87 )88 )
88 }89 }
...@@ -99,7 +100,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -99,7 +100,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
99100
100 return Ok(Some(Expr::new(101 return Ok(Some(Expr::new(
101 ExprData::Return(inner),102 ExprData::Return(inner),
102 Loc::new(start, p.index - start),103 Range::new(start, p.index - start),
103 comments,104 comments,
104 )));105 )));
105 }106 }
...@@ -107,13 +108,13 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -107,13 +108,13 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
107108
108 Some(TokenType::If) => Expr::new(109 Some(TokenType::If) => Expr::new(
109 ExprData::If(parse_expr_if(p)?),110 ExprData::If(parse_expr_if(p)?),
110 Loc::new(start, p.index - start),111 Range::new(start, p.index - start),
111 comments,112 comments,
112 ),113 ),
113114
114 Some(TokenType::OpenBrace) => Expr::new(115 Some(TokenType::OpenBrace) => Expr::new(
115 ExprData::Block(parse_block(p)?),116 ExprData::Block(parse_block(p)?),
116 Loc::new(start, p.index - start),117 Range::new(start, p.index - start),
117 comments,118 comments,
118 ),119 ),
119120
...@@ -142,7 +143,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -142,7 +143,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
142 },143 },
143 value: Box::new(parse_expr_basic(p)?),144 value: Box::new(parse_expr_basic(p)?),
144 },145 },
145 Loc::new(start, p.index - start),146 Range::new(start, p.index - start),
146 comments,147 comments,
147 )148 )
148 }149 }
...@@ -162,7 +163,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -162,7 +163,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
162 p.next();163 p.next();
163 Expr::new(164 Expr::new(
164 ExprData::Literal(Literal::String(name)),165 ExprData::Literal(Literal::String(name)),
165 Loc::new(field_start, p.index - field_start),166 Range::new(field_start, p.index - field_start),
166 None,167 None,
167 )168 )
168 }169 }
...@@ -175,7 +176,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -175,7 +176,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
175 base: Box::new(expr),176 base: Box::new(expr),
176 field: Box::new(field),177 field: Box::new(field),
177 },178 },
178 Loc::new(field_start, p.index - field_start),179 Range::new(field_start, p.index - field_start),
179 None,180 None,
180 );181 );
181 }182 }
...@@ -188,7 +189,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -188,7 +189,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
188 base: Box::new(expr),189 base: Box::new(expr),
189 arguments,190 arguments,
190 },191 },
191 Loc::new(expr_start, p.index - expr_start),192 Range::new(expr_start, p.index - expr_start),
192 None,193 None,
193 );194 );
194 }195 }
...@@ -202,7 +203,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {...@@ -202,7 +203,7 @@ pub fn maybe_parse_expr_basic(p: &mut Parser) -> Result<Option<Expr>> {
202 base: Box::new(expr),203 base: Box::new(expr),
203 field: Box::new(index),204 field: Box::new(index),
204 },205 },
205 Loc::new(expr_start, p.index - expr_start),206 Range::new(expr_start, p.index - expr_start),
206 None,207 None,
207 );208 );
208 } else {209 } else {
crates/parser/parse_expr_if.rs+8-7
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1use ast::{BlockOrIf, BlockWithLoc, If};1use ast::{BlockOrIf, BlockWithRange, If};
2use log::Range;
2use miette::Result;3use miette::Result;
3use tokenizer::{Loc, TokenType};4use tokenizer::TokenType;
45
5use crate::parse_block::parse_block;6use crate::parse_block::parse_block;
6use crate::parse_expr::parse_expr;7use crate::parse_expr::parse_expr;
...@@ -30,9 +31,9 @@ pub fn parse_expr_if(p: &mut Parser) -> Result<If> {...@@ -30,9 +31,9 @@ pub fn parse_expr_if(p: &mut Parser) -> Result<If> {
30 let yes = match p.peek() {31 let yes = match p.peek() {
31 Some(TokenType::OpenBrace) => {32 Some(TokenType::OpenBrace) => {
32 let block_start = p.index;33 let block_start = p.index;
33 BlockWithLoc {34 BlockWithRange {
34 block: parse_block(p)?,35 block: parse_block(p)?,
35 loc: Loc::new(block_start, p.index - block_start),36 range: Range::new(block_start, p.index - block_start),
36 }37 }
37 }38 }
38 _ => {39 _ => {
...@@ -49,14 +50,14 @@ pub fn parse_expr_if(p: &mut Parser) -> Result<If> {...@@ -49,14 +50,14 @@ pub fn parse_expr_if(p: &mut Parser) -> Result<If> {
49 Some(TokenType::OpenBrace) => {50 Some(TokenType::OpenBrace) => {
50 let block_start = p.index;51 let block_start = p.index;
51 // let comments = p.take_comments();52 // let comments = p.take_comments();
52 Some(BlockOrIf::Block(BlockWithLoc {53 Some(BlockOrIf::Block(BlockWithRange {
53 block: parse_block(p)?,54 block: parse_block(p)?,
54 loc: Loc::new(block_start, p.index - block_start),55 range: Range::new(block_start, p.index - block_start),
55 }))56 }))
56 }57 }
57 Some(TokenType::If) => {58 Some(TokenType::If) => {
58 // TODO(@paperdave): Save location for the if59 // TODO(@paperdave): Save location for the if
59 // let loc = p.loc();60 // let range = p.range();
60 // let comments = p.take_comments();61 // let comments = p.take_comments();
61 Some(BlockOrIf::If(Box::new(parse_expr_if(p)?)))62 Some(BlockOrIf::If(Box::new(parse_expr_if(p)?)))
62 }63 }
crates/parser/parse_fn_declaration.rs+4-3
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1use ast::{DocComment, Expr, ExprData, FnDecl, Type};1use ast::{DocComment, Expr, ExprData, FnDecl, Type};
2use log::Range;
2use miette::Result;3use miette::Result;
3use tokenizer::{Loc, TokenType};4use tokenizer::TokenType;
45
5use crate::parse_block::parse_block;6use crate::parse_block::parse_block;
6use crate::parse_expr::parse_expr;7use crate::parse_expr::parse_expr;
...@@ -93,7 +94,7 @@ pub fn parse_fn_declaration(...@@ -93,7 +94,7 @@ pub fn parse_fn_declaration(
93 let block = parse_block(p)?;94 let block = parse_block(p)?;
94 Expr::new(95 Expr::new(
95 ExprData::Block(block),96 ExprData::Block(block),
96 Loc::new(block_start, p.index - block_start),97 Range::new(block_start, p.index - block_start),
97 None,98 None,
98 )99 )
99 }100 }
...@@ -121,6 +122,6 @@ pub fn parse_fn_declaration(...@@ -121,6 +122,6 @@ pub fn parse_fn_declaration(
121 body,122 body,
122 is_pub,123 is_pub,
123 comment,124 comment,
124 loc: Loc::new(start, p.index - start),125 range: Range::new(start, p.index - start),
125 })126 })
126}127}
crates/parser/parse_statement.rs+2-2
...@@ -18,7 +18,7 @@ pub fn parse_statement(p: &mut Parser) -> Result<Statement> {...@@ -18,7 +18,7 @@ pub fn parse_statement(p: &mut Parser) -> Result<Statement> {
18 if p.has_comments() {18 if p.has_comments() {
19 // this implies that comments are immediately after the doc19 // this implies that comments are immediately after the doc
20 return Err(p20 return Err(p
21 .error_at(format!("Unattached doc comment"), doc_comment.loc)21 .error_at(format!("Unattached doc comment"), doc_comment.range)
22 .into());22 .into());
23 }23 }
24 }24 }
...@@ -58,7 +58,7 @@ pub fn parse_statement(p: &mut Parser) -> Result<Statement> {...@@ -58,7 +58,7 @@ pub fn parse_statement(p: &mut Parser) -> Result<Statement> {
58 Some(_) => {58 Some(_) => {
59 if let Some(doc_comment) = m_doc_comment {59 if let Some(doc_comment) = m_doc_comment {
60 Err(p60 Err(p
61 .error_at(format!("Unattached doc comment"), doc_comment.loc)61 .error_at(format!("Unattached doc comment"), doc_comment.range)
62 .into())62 .into())
63 } else {63 } else {
64 Ok(Statement::Expr(parse_expr(p)?))64 Ok(Statement::Expr(parse_expr(p)?))
crates/parser/parser.rs+8-7
...@@ -14,13 +14,14 @@ mod parse_type;...@@ -14,13 +14,14 @@ mod parse_type;
1414
15use ast::Comment;15use ast::Comment;
16use ast::Module;16use ast::Module;
17use log::Range;
17use miette::Diagnostic;18use miette::Diagnostic;
18use miette::Result;19use miette::Result;
19use parse_module::parse_module;20use parse_module::parse_module;
20use thiserror::Error;21use thiserror::Error;
21use tokenizer::tokenize;22use tokenizer::tokenize;
23use tokenizer::Token;
22use tokenizer::TokenType;24use tokenizer::TokenType;
23use tokenizer::{Loc, Token};
2425
25pub fn parse(tokens: Vec<Token>) -> Result<Module> {26pub fn parse(tokens: Vec<Token>) -> Result<Module> {
26 let mut p = Parser::new(tokens);27 let mut p = Parser::new(tokens);
...@@ -37,12 +38,12 @@ pub fn parse_from_string(string: String) -> Result<Module> {...@@ -37,12 +38,12 @@ pub fn parse_from_string(string: String) -> Result<Module> {
37pub struct ParseError {38pub struct ParseError {
38 #[help]39 #[help]
39 error: String,40 error: String,
40 loc: Option<Loc>,41 range: Option<Range>,
41}42}
4243
43impl ParseError {44impl ParseError {
44 pub fn new(error: String, loc: Option<Loc>) -> ParseError {45 pub fn new(error: String, range: Option<Range>) -> ParseError {
45 ParseError { error, loc }46 ParseError { error, range }
46 }47 }
47}48}
4849
...@@ -89,7 +90,7 @@ impl Parser {...@@ -89,7 +90,7 @@ impl Parser {
8990
90 loop {91 loop {
91 let token = self.tokens[self.cursor].clone();92 let token = self.tokens[self.cursor].clone();
92 self.index = token.loc.start;93 self.index = token.range.start;
93 match token.variant {94 match token.variant {
94 TokenType::Comment(comment) => {95 TokenType::Comment(comment) => {
95 self.comments.push(comment);96 self.comments.push(comment);
...@@ -144,7 +145,7 @@ impl Parser {...@@ -144,7 +145,7 @@ impl Parser {
144 ParseError::new(error, None)145 ParseError::new(error, None)
145 }146 }
146147
147 fn error_at(&self, error: String, loc: Loc) -> ParseError {148 fn error_at(&self, error: String, range: Range) -> ParseError {
148 ParseError::new(error, Some(loc))149 ParseError::new(error, Some(range))
149 }150 }
150}151}
crates/tokenizer/Cargo.toml+2
...@@ -9,3 +9,5 @@ path="./tokenizer.rs"...@@ -9,3 +9,5 @@ path="./tokenizer.rs"
9[dependencies]9[dependencies]
10miette.workspace = true10miette.workspace = true
11thiserror.workspace = true11thiserror.workspace = true
12
13log = { path = "../log" }
crates/tokenizer/tokenizer.rs+4-25
...@@ -1,28 +1,7 @@...@@ -1,28 +1,7 @@
1use log::Range;
1use miette::{Diagnostic, Result};2use miette::{Diagnostic, Result};
2use thiserror::Error;3use thiserror::Error;
34
4#[derive(Debug, PartialEq, Clone)]
5pub struct Loc {
6 pub start: usize,
7 pub length: usize,
8}
9
10impl Loc {
11 /// TODO(@paperdave): Remove this. every usage of this is incorrect.
12 pub const EMPTY: Loc = Loc {
13 start: 0,
14 length: 0,
15 };
16
17 pub fn new(start: usize, length: usize) -> Self {
18 Self { start, length }
19 }
20
21 pub fn len(&self) -> usize {
22 self.length
23 }
24}
25
26#[derive(Error, Diagnostic, Debug)]5#[derive(Error, Diagnostic, Debug)]
27#[error("UnknownCharacter")]6#[error("UnknownCharacter")]
28struct UnknownCharacter {7struct UnknownCharacter {
...@@ -162,19 +141,19 @@ impl std::fmt::Display for TokenType {...@@ -162,19 +141,19 @@ impl std::fmt::Display for TokenType {
162#[derive(Debug, Clone)]141#[derive(Debug, Clone)]
163pub struct Token {142pub struct Token {
164 pub variant: TokenType,143 pub variant: TokenType,
165 pub loc: Loc,144 pub range: Range,
166}145}
167146
168impl Token {147impl Token {
169 pub fn new(variant: TokenType, start: usize, length: usize) -> Self {148 pub fn new(variant: TokenType, start: usize, length: usize) -> Self {
170 Self {149 Self {
171 variant,150 variant,
172 loc: Loc::new(start, length),151 range: Range::new(start, length),
173 }152 }
174 }153 }
175}154}
176155
177// TODO(@paperdave): rewrite this to not use a Vec<Token>. we probably dont need it. also store Loc156// TODO(@paperdave): rewrite this to not use a Vec<Token>. we probably dont need it. also store Range
178pub fn tokenize(content: String) -> Result<Vec<Token>> {157pub fn tokenize(content: String) -> Result<Vec<Token>> {
179 let mut tokens: Vec<Token> = Vec::new();158 let mut tokens: Vec<Token> = Vec::new();
180 let mut chars = content.chars().enumerate().peekable();159 let mut chars = content.chars().enumerate().peekable();