-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and add better support for error reporting
- Loading branch information
1 parent
8c48928
commit c024af4
Showing
18 changed files
with
208 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import gleam/iterator | ||
|
||
pub fn iterate(string: String) -> iterator.Iterator(Int) { | ||
iterator.unfold(<<string:utf8>>, fn(remaining) { | ||
case remaining { | ||
<<>> -> iterator.Done | ||
<<byte:8, rest:bytes>> -> iterator.Next(byte, rest) | ||
_ -> panic as "string should always return a byte-aligned bitarray" | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import glance | ||
import gleam/bit_array | ||
import gleam/bytes_builder.{type BytesBuilder} | ||
import gleam/int | ||
import gleam/iterator | ||
import gleam/list | ||
import gleam/result | ||
import gleam/string | ||
import glexer | ||
import glexer/token | ||
import internal/bytes | ||
import pprint | ||
|
||
pub fn format_glance_error( | ||
error: glance.Error, | ||
filename: String, | ||
contents: String, | ||
) -> String { | ||
pprint.debug(error) | ||
let error_message = case error { | ||
glance.UnexpectedEndOfInput -> "Unexpected EOF" | ||
glance.UnexpectedToken(token, position) -> | ||
format_unexpected_token(token, position, contents) | ||
} | ||
"Unable to compile " <> filename <> ":\n" <> error_message | ||
} | ||
|
||
type PositionState { | ||
PositionState( | ||
current_line_number: Int, | ||
current_line_bytes: BytesBuilder, | ||
current_line_first_byte_position: Int, | ||
current_position: Int, | ||
target_position: Int, | ||
) | ||
} | ||
|
||
pub fn format_unexpected_token( | ||
token: token.Token, | ||
position: glexer.Position, | ||
contents: String, | ||
) -> String { | ||
let initial = | ||
PositionState( | ||
current_line_number: 1, | ||
current_line_bytes: bytes_builder.new(), | ||
current_line_first_byte_position: 0, | ||
current_position: 0, | ||
// glexer positions start at byte 0, which is character 1 on a line based system | ||
target_position: position.byte_offset + 1, | ||
) | ||
|
||
let position_state = | ||
contents | ||
|> bytes.iterate | ||
|> iterator.fold_until(initial, fold_position_to_lines) | ||
|
||
case position_state.current_position { | ||
pos if pos < position_state.target_position -> | ||
"\nUnexpected EOF looking for " | ||
<> format_token(token) | ||
<> " at position " | ||
<> int.to_string(position_state.target_position) | ||
_ -> | ||
{ | ||
let column = | ||
position_state.target_position | ||
- position_state.current_line_first_byte_position | ||
"Unexpected Token " | ||
<> format_token(token) | ||
<> "\nAt line " | ||
<> int.to_string(position_state.current_line_number) | ||
<> " column " | ||
<> int.to_string(column) | ||
<> "\n\n" | ||
<> { | ||
position_state.current_line_bytes | ||
|> bytes_builder.to_bit_array | ||
|> bit_array.to_string | ||
|> result.unwrap("Unexpected unicode") | ||
} | ||
<> "\n" | ||
<> string.repeat(" ", column - 1) | ||
<> "^\n" | ||
} | ||
|> pprint.debug | ||
} | ||
} | ||
|
||
// Given a byte position, return information about the line that contains that | ||
// byte iterates over each bytes, counting lines. Once it finds the target, | ||
// continues iterating until the end of the line and returns that line. | ||
fn fold_position_to_lines( | ||
state: PositionState, | ||
byte: Int, | ||
) -> list.ContinueOrStop(PositionState) { | ||
pprint.debug(#( | ||
PositionState(..state, current_line_bytes: bytes_builder.new()), | ||
byte, | ||
)) | ||
case byte, state.current_position, state.target_position { | ||
10, curr, target if curr < target -> | ||
list.Continue( | ||
PositionState( | ||
..state, | ||
current_line_first_byte_position: state.current_position + 1, | ||
current_line_number: state.current_line_number + 1, | ||
current_line_bytes: bytes_builder.new(), | ||
current_position: state.current_position + 1, | ||
), | ||
) | ||
10, _, _ -> list.Stop(state) | ||
byte, _, _ -> { | ||
list.Continue( | ||
PositionState( | ||
..state, | ||
current_line_bytes: bytes_builder.append(state.current_line_bytes, << | ||
byte, | ||
>>), | ||
current_position: state.current_position + 1, | ||
), | ||
) | ||
} | ||
} | ||
} | ||
|
||
fn format_token(token: token.Token) -> String { | ||
case token { | ||
token.Int(num_str) -> num_str | ||
_ -> { | ||
pprint.debug(token) | ||
"<TODO Unknown Token>" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import gleam/iterator | ||
import gleeunit/should | ||
import internal/bytes | ||
|
||
pub fn iterate_ascii_bytes_test() { | ||
bytes.iterate("hello") | ||
|> iterator.to_list | ||
|> should.equal([104, 101, 108, 108, 111]) | ||
} | ||
|
||
pub fn iterate_utf8_bytes_test() { | ||
"🏳️🌈" | ||
|> bytes.iterate | ||
|> iterator.to_list | ||
|> should.equal([ | ||
240, 159, 143, 179, 239, 184, 143, 226, 128, 141, 240, 159, 140, 136, | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import gleeunit/should | ||
import glexer | ||
import glexer/token | ||
import internal/errors | ||
import pprint | ||
|
||
// Reminder: glexer.Position is 0-indexed, but output columns are 1-indexed | ||
pub fn position_at_first_byte_test() { | ||
errors.format_unexpected_token(token.Int("5"), glexer.Position(0), "5bcdefg") | ||
|> should.equal("Unexpected Token 5\nAt line 1 column 1\n\n5bcdefg\n^\n") | ||
} | ||
|
||
pub fn position_in_first_line_test() { | ||
errors.format_unexpected_token(token.Int("5"), glexer.Position(4), "abcd5fg") | ||
|> should.equal("Unexpected Token 5\nAt line 1 column 5\n\nabcd5fg\n ^\n") | ||
} | ||
|
||
pub fn position_in_second_line_test() { | ||
errors.format_unexpected_token( | ||
token.Int("5"), | ||
glexer.Position(5), | ||
"abc\nd5fg", | ||
) | ||
|> should.equal("Unexpected Token 5\nAt line 2 column 2\n\nd5fg\n ^\n") | ||
} | ||
|
||
pub fn position_after_newline_test() { | ||
pprint.debug("abc\n\nd5fg") | ||
errors.format_unexpected_token( | ||
token.Int("5"), | ||
glexer.Position(6), | ||
"abc\n\nd5fg", | ||
) | ||
|> should.equal("Unexpected Token 5\nAt line 3 column 2\n\nd5fg\n ^\n") | ||
} |