Skip to content

Commit

Permalink
✨ start and end for parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelvalley committed Jul 10, 2024
1 parent e72ad93 commit d88322d
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/parsing.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub type Parsed {
}

pub type ParserState {
ParserState(target: String, index: Int, result: Parsed)
ParserState(target: String, start: Int, end: Int, result: Parsed)
}

/// A Parser is defined as a function that takes in a ParserState and returns a
Expand All @@ -24,17 +24,18 @@ type Parser =

fn str(start: String) -> Parser {
fn(state: ParserState) {
let from_str = string.drop_left(state.target, state.index)
let from_str = string.drop_left(state.target, state.end)
let starts_with = string.starts_with(from_str, start)

case starts_with {
True ->
Ok(ParserState(
state.target,
state.index + string.length(start),
state.end,
state.end + string.length(start),
Str(start),
))
False -> Error(ExpectedStr(state.index, start, from_str))
False -> Error(ExpectedStr(state.end, start, from_str))
}
}
}
Expand Down Expand Up @@ -70,19 +71,20 @@ fn sequence_of(parsers: List(Parser)) -> Parser {
case result {
Error(err) -> Error(err)
Ok(ok) -> {
let empty_error = Error(EmptySequence(state.index))
let empty_error = Error(EmptySequence(state.end))

case list.last(ok) {
Error(_) -> empty_error
Ok(last) -> Ok(ParserState(last.target, last.index, Sequence(ok)))
Ok(last) ->
Ok(ParserState(last.target, state.end, last.end, Sequence(ok)))
}
}
}
}
}

fn run(parser, target) {
let initial = ParserState(target, 0, StartOfFile)
let initial = ParserState(target, 0, 0, StartOfFile)
parser(initial)
}

Expand Down

0 comments on commit d88322d

Please sign in to comment.