Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephTLyons committed Nov 20, 2024
1 parent 924cba7 commit 0243d7a
Showing 1 changed file with 41 additions and 48 deletions.
89 changes: 41 additions & 48 deletions src/lenient_parse/internal/parser.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -246,60 +246,36 @@ fn parse_base_prefix(
let lookahead = rest |> list.first

case lookahead {
Ok(Digit(#(_, end_index), specifier, _))
Ok(Digit(_, specifier, _))
if { base == base_0 || base == base_2 }
&& { specifier == "b" || specifier == "B" }
-> {
let new_rest = case rest {
[] -> []
[_, ..rest] -> rest
}
Ok(ParseData(
data: Some(#(
#(index_range.0, index_range.1 + 1),
"0" <> specifier,
base_2,
)),
next_index: end_index + 2,
tokens: new_rest,
))
}
Ok(Digit(#(_, end_index), specifier, _))
->
form_base_prefix(
tokens: rest,
index_range: index_range,
specifier: specifier,
base: base_2,
)
Ok(Digit(_, specifier, _))
if { base == base_0 || base == base_8 }
&& { specifier == "o" || specifier == "O" }
-> {
let new_rest = case rest {
[] -> []
[_, ..rest] -> rest
}
Ok(ParseData(
data: Some(#(
#(index_range.0, index_range.1 + 1),
"0" <> specifier,
base_8,
)),
next_index: end_index + 2,
tokens: new_rest,
))
}
Ok(Digit(#(_, end_index), specifier, _))
->
form_base_prefix(
tokens: rest,
index_range: index_range,
specifier: specifier,
base: base_8,
)
Ok(Digit(_, specifier, _))
if { base == base_0 || base == base_16 }
&& { specifier == "x" || specifier == "X" }
-> {
let new_rest = case rest {
[] -> []
[_, ..rest] -> rest
}
Ok(ParseData(
data: Some(#(
#(index_range.0, index_range.1 + 1),
"0" <> specifier,
base_16,
)),
next_index: end_index + 2,
tokens: new_rest,
))
}
->
form_base_prefix(
tokens: rest,
index_range: index_range,
specifier: specifier,
base: base_16,
)
Ok(Digit(#(start_index, _), character, _)) if base == base_0 -> {
Error(UnknownCharacter(start_index, character))
}
Expand All @@ -310,6 +286,23 @@ fn parse_base_prefix(
}
}

fn form_base_prefix(
tokens tokens: List(Token),
index_range index_range: #(Int, Int),
specifier specifier: String,
base base: Int,
) -> Result(ParseData(Option(#(#(Int, Int), String, Int))), ParseError) {
let new_rest = case tokens {
[] -> []
[_, ..tokens] -> tokens
}
Ok(ParseData(
data: Some(#(#(index_range.0, index_range.1 + 1), "0" <> specifier, base)),
next_index: index_range.1 + 2,
tokens: new_rest,
))
}

fn parse_decimal_point(
tokens tokens: List(Token),
index index: Int,
Expand Down

0 comments on commit 0243d7a

Please sign in to comment.