Skip to content

Commit

Permalink
Simplify delimited parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail committed Feb 27, 2024
1 parent cce4c66 commit fb76a34
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 85 deletions.
18 changes: 4 additions & 14 deletions internal/bloblang/parser/combinators.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ func Array() Func {
DiscardedWhitespaceNewlineComments,
),
Sequence(DiscardedWhitespaceNewlineComments, charSquareClose),
true,
)

return func(r []rune) Result {
Expand All @@ -306,7 +305,6 @@ func Object() Func {
DiscardedWhitespaceNewlineComments,
),
Sequence(DiscardedWhitespaceNewlineComments, charSquigClose),
true,
)

return func(input []rune) Result {
Expand Down Expand Up @@ -487,15 +485,9 @@ func UntilFail(parser Func) Func {
// start and stop parser, where after the first parse a delimiter is expected.
// Parsing is stopped only once an explicit stop parser is successful.
//
// If allowTrailing is set to false and a delimiter is parsed but a subsequent
// primary parse fails then an error is returned.
//
// Only the results of the primary parser are returned, the results of the
// start, delimiter and stop parsers are discarded.
func DelimitedPattern(
start, primary, delimiter, stop Func,
allowTrailing bool,
) Func {
func DelimitedPattern(start, primary, delimiter, stop Func) Func {
return func(input []rune) Result {
res := start(input)
if res.Err != nil {
Expand Down Expand Up @@ -524,11 +516,9 @@ func DelimitedPattern(
return Fail(res.Err, input)
}
if res = primary(res.Remaining); res.Err != nil {
if allowTrailing {
if resStop := stop(res.Remaining); resStop.Err == nil {
resStop.Payload = results
return resStop
}
if resStop := stop(res.Remaining); resStop.Err == nil {
resStop.Payload = results
return resStop
}
return Fail(res.Err, input)
}
Expand Down
66 changes: 1 addition & 65 deletions internal/bloblang/parser/combinators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,72 +778,8 @@ func TestDelimited(t *testing.T) {
}
}

func TestDelimitedPattern(t *testing.T) {
parser := DelimitedPattern(charHash, Term("abc"), charComma, Char('!'), false)

tests := map[string]struct {
input string
result any
remaining string
err *Error
}{
"empty input": {
err: NewError([]rune(""), "#"),
},
"no start": {
input: "ab",
remaining: "ab",
err: NewError([]rune("ab"), "#"),
},
"smaller than string": {
input: "#ab",
remaining: "#ab",
err: NewError([]rune("ab"), "abc"),
},
"matches first": {
input: "#abc!No",
remaining: "No",
result: []any{"abc"},
},
"matches some of second": {
input: "#abc,abNo",
remaining: "#abc,abNo",
err: NewError([]rune("abNo"), "abc"),
},
"matches not stopped": {
input: "#abc,abcNo",
remaining: "#abc,abcNo",
err: NewError([]rune("No"), ",", "!"),
},
"matches all": {
input: "#abc,abc!",
remaining: "",
result: []any{"abc", "abc"},
},
"matches some": {
input: "#abc,abc! and this",
remaining: " and this",
result: []any{"abc", "abc"},
},
"matches all of these": {
input: "#abc,abc,abc,abc!def and this",
remaining: "def and this",
result: []any{"abc", "abc", "abc", "abc"},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := parser([]rune(test.input))
require.Equal(t, test.err, res.Err, "Error")
assert.Equal(t, test.result, res.Payload, "Result")
assert.Equal(t, test.remaining, string(res.Remaining), "Remaining")
})
}
}

func TestDelimitedPatternAllowTrailing(t *testing.T) {
parser := DelimitedPattern(charHash, Term("abc"), charComma, Char('!'), true)
parser := DelimitedPattern(charHash, Term("abc"), charComma, Char('!'))

tests := map[string]struct {
input string
Expand Down
3 changes: 1 addition & 2 deletions internal/bloblang/parser/mapping_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func importParser(maps map[string]query.Function, pCtx Context) Func {
func mapParser(maps map[string]query.Function, pCtx Context) Func {
p := Sequence(
Term("map"),
DiscardedWhitespaceNewlineComments,
SpacesAndTabs,
// Prevents a missing path from being captured by the next parser
MustBe(
Expect(
Expand Down Expand Up @@ -261,7 +261,6 @@ func mapParser(maps map[string]query.Function, pCtx Context) Func {
DiscardedWhitespaceNewlineComments,
charSquigClose,
),
true,
),
)

Expand Down
1 change: 0 additions & 1 deletion internal/bloblang/parser/query_expression_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func matchExpressionParser(pCtx Context) Func {
DiscardedWhitespaceNewlineComments,
charSquigClose,
),
true,
),
),
)(input)
Expand Down
1 change: 0 additions & 1 deletion internal/bloblang/parser/query_function_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func functionArgsParser(pCtx Context) Func {
),
MustBe(Expect(Sequence(Discard(SpacesAndTabs), charComma, DiscardedWhitespaceNewlineComments), "comma")),
MustBe(Expect(Sequence(DiscardedWhitespaceNewlineComments, charBracketClose), "closing bracket")),
true,
)(input)
}
}
Expand Down
2 changes: 0 additions & 2 deletions internal/bloblang/parser/query_literal_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func dynamicArrayParser(pCtx Context) Func {
DiscardedWhitespaceNewlineComments,
charSquareClose,
),
true,
)(input)
if res.Err != nil {
return res
Expand Down Expand Up @@ -58,7 +57,6 @@ func dynamicObjectParser(pCtx Context) Func {
DiscardedWhitespaceNewlineComments,
charSquigClose,
),
true,
)(input)
if res.Err != nil {
return res
Expand Down

0 comments on commit fb76a34

Please sign in to comment.