From 36f0c84ef77ac36ff964536a880ffba597dc7e8b Mon Sep 17 00:00:00 2001 From: Oliver Linnarsson Date: Tue, 23 Jul 2024 17:26:13 +0200 Subject: [PATCH] chore: Formatting + DRY --- src/handles/internal/ctx_utils.gleam | 50 ++++++--- src/handles/internal/engine.gleam | 79 ++++++-------- src/handles/internal/parser.gleam | 43 +++++--- src/handles/internal/tokenizer.gleam | 149 ++++++++++++--------------- 4 files changed, 158 insertions(+), 163 deletions(-) diff --git a/src/handles/internal/ctx_utils.gleam b/src/handles/internal/ctx_utils.gleam index 80002a3..a9bda86 100644 --- a/src/handles/internal/ctx_utils.gleam +++ b/src/handles/internal/ctx_utils.gleam @@ -51,9 +51,15 @@ pub fn get_property( get(path, root_ctx, index) |> result.try(fn(it) { case it { - ctx.Str(value) -> value |> Ok - ctx.Int(value) -> value |> int.to_string |> Ok - ctx.Float(value) -> value |> float.to_string |> Ok + ctx.Str(value) -> Ok(value) + ctx.Int(value) -> + value + |> int.to_string + |> Ok + ctx.Float(value) -> + value + |> float.to_string + |> Ok ctx.List(_) -> error.UnexpectedType(index, path, "List", ["Str", "Int", "Float"]) |> Error @@ -75,15 +81,22 @@ pub fn get_list( get(path, root_ctx, index) |> result.try(fn(it) { case it { - ctx.List(value) -> value |> Ok - ctx.Str(_) -> error.UnexpectedType(index, path, "Str", ["List"]) |> Error - ctx.Int(_) -> error.UnexpectedType(index, path, "Int", ["List"]) |> Error + ctx.List(value) -> Ok(value) + ctx.Str(_) -> + error.UnexpectedType(index, path, "Str", ["List"]) + |> Error + ctx.Int(_) -> + error.UnexpectedType(index, path, "Int", ["List"]) + |> Error ctx.Bool(_) -> - error.UnexpectedType(index, path, "Bool", ["List"]) |> Error + error.UnexpectedType(index, path, "Bool", ["List"]) + |> Error ctx.Float(_) -> - error.UnexpectedType(index, path, "Float", ["List"]) |> Error + error.UnexpectedType(index, path, "Float", ["List"]) + |> Error ctx.Dict(_) -> - error.UnexpectedType(index, path, "Dict", ["List"]) |> Error + error.UnexpectedType(index, path, "Dict", ["List"]) + |> Error } }) } @@ -96,15 +109,22 @@ pub fn get_bool( get(path, root_ctx, index) |> result.try(fn(it) { case it { - ctx.Bool(value) -> value |> Ok + ctx.Bool(value) -> Ok(value) ctx.List(_) -> - error.UnexpectedType(index, path, "List", ["Bool"]) |> Error - ctx.Str(_) -> error.UnexpectedType(index, path, "Str", ["Bool"]) |> Error - ctx.Int(_) -> error.UnexpectedType(index, path, "Int", ["Bool"]) |> Error + error.UnexpectedType(index, path, "List", ["Bool"]) + |> Error + ctx.Str(_) -> + error.UnexpectedType(index, path, "Str", ["Bool"]) + |> Error + ctx.Int(_) -> + error.UnexpectedType(index, path, "Int", ["Bool"]) + |> Error ctx.Float(_) -> - error.UnexpectedType(index, path, "Float", ["Bool"]) |> Error + error.UnexpectedType(index, path, "Float", ["Bool"]) + |> Error ctx.Dict(_) -> - error.UnexpectedType(index, path, "Dict", ["Bool"]) |> Error + error.UnexpectedType(index, path, "Dict", ["Bool"]) + |> Error } }) } diff --git a/src/handles/internal/engine.gleam b/src/handles/internal/engine.gleam index ab9e82a..ef6abd9 100644 --- a/src/handles/internal/engine.gleam +++ b/src/handles/internal/engine.gleam @@ -24,42 +24,32 @@ fn eval( eval(rest_action, new_ctx, partials, builder) [RunAst([]), ..rest_action] -> eval(rest_action, ctx, partials, builder) [RunAst([parser.Constant(_, value), ..rest_ast]), ..rest_action] -> - eval( - [RunAst(rest_ast), ..rest_action], - ctx, - partials, - string_builder.append(builder, value), - ) + [RunAst(rest_ast), ..rest_action] + |> eval(ctx, partials, string_builder.append(builder, value)) [RunAst([parser.Property(index, path), ..rest_ast]), ..rest_action] -> case ctx_utils.get_property(path, ctx, index) { Error(err) -> Error(err) Ok(value) -> - eval( - [RunAst(rest_ast), ..rest_action], - ctx, - partials, - string_builder.append(builder, value), - ) + [RunAst(rest_ast), ..rest_action] + |> eval(ctx, partials, string_builder.append(builder, value)) } [RunAst([parser.Partial(index, id, path), ..rest_ast]), ..rest_action] -> case dict.get(partials, id) { - Error(_) -> Error(error.UnknownPartial(index, id)) + Error(_) -> + error.UnknownPartial(index, id) + |> Error Ok(partial_ast) -> case ctx_utils.get(path, ctx, index) { Error(err) -> Error(err) Ok(inner_ctx) -> - eval( - [ - SetCtx(inner_ctx), - RunAst(partial_ast), - SetCtx(ctx), - RunAst(rest_ast), - ..rest_action - ], - ctx, - partials, - builder, - ) + [ + SetCtx(inner_ctx), + RunAst(partial_ast), + SetCtx(ctx), + RunAst(rest_ast), + ..rest_action + ] + |> eval(ctx, partials, builder) } } [ @@ -72,14 +62,11 @@ fn eval( case ctx_utils.get_bool(path, ctx, start_index) { Error(err) -> Error(err) Ok(False) -> - eval([RunAst(rest_ast), ..rest_action], ctx, partials, builder) + [RunAst(rest_ast), ..rest_action] + |> eval(ctx, partials, builder) Ok(True) -> - eval( - [RunAst(children), RunAst(rest_ast), ..rest_action], - ctx, - partials, - builder, - ) + [RunAst(children), RunAst(rest_ast), ..rest_action] + |> eval(ctx, partials, builder) } [ RunAst([ @@ -91,14 +78,11 @@ fn eval( case ctx_utils.get_bool(path, ctx, start_index) { Error(err) -> Error(err) Ok(True) -> - eval([RunAst(rest_ast), ..rest_action], ctx, partials, builder) + [RunAst(rest_ast), ..rest_action] + |> eval(ctx, partials, builder) Ok(False) -> - eval( - [RunAst(children), RunAst(rest_ast), ..rest_action], - ctx, - partials, - builder, - ) + [RunAst(children), RunAst(rest_ast), ..rest_action] + |> eval(ctx, partials, builder) } [ RunAst([ @@ -110,18 +94,13 @@ fn eval( case ctx_utils.get_list(path, ctx, start_index) { Error(err) -> Error(err) Ok([]) -> - eval([RunAst(rest_ast), ..rest_action], ctx, partials, builder) + [RunAst(rest_ast), ..rest_action] + |> eval(ctx, partials, builder) Ok(ctxs) -> - eval( - ctxs - |> list.flat_map(fn(new_ctx) { - [SetCtx(new_ctx), RunAst(children), SetCtx(ctx)] - }) - |> list.append([RunAst(rest_ast), ..rest_action]), - ctx, - partials, - builder, - ) + ctxs + |> list.flat_map(fn(new_ctx) { [SetCtx(new_ctx), RunAst(children)] }) + |> list.append([SetCtx(ctx), RunAst(rest_ast), ..rest_action]) + |> eval(ctx, partials, builder) } } } diff --git a/src/handles/internal/parser.gleam b/src/handles/internal/parser.gleam index 5fa7b35..542e845 100644 --- a/src/handles/internal/parser.gleam +++ b/src/handles/internal/parser.gleam @@ -19,12 +19,17 @@ pub type AST { type ParseResult { EOF(List(AST)) - BlockEnd(index: Int, block.Kind, List(AST), List(tokenizer.Token)) + BlockEnd( + index: Int, + kind: block.Kind, + children: List(AST), + rest: List(tokenizer.Token), + ) } fn parse( - tokens: List(tokenizer.Token), ast: List(AST), + tokens: List(tokenizer.Token), ) -> Result(ParseResult, error.TokenizerError) { case tokens { [] -> Ok(EOF(list.reverse(ast))) @@ -32,24 +37,31 @@ fn parse( Ok(BlockEnd(index, kind, list.reverse(ast), tail)) [tokenizer.Constant(index, value), ..tail] -> - parse(tail, [Constant(index, value), ..ast]) + [Constant(index, value), ..ast] + |> parse(tail) [tokenizer.Property(index, path), ..tail] -> - parse(tail, [Property(index, path), ..ast]) + [Property(index, path), ..ast] + |> parse(tail) [tokenizer.Partial(index, id, value), ..tail] -> - parse(tail, [Partial(index, id, value), ..ast]) + [Partial(index, id, value), ..ast] + |> parse(tail) [tokenizer.BlockStart(start_index, start_kind, path), ..tail] -> - case parse(tail, []) { + case parse([], tail) { Error(err) -> Error(err) - Ok(EOF(_)) -> Error(error.UnbalancedBlock(start_index)) + Ok(EOF(_)) -> + start_index + |> error.UnbalancedBlock + |> Error Ok(BlockEnd(end_index, end_kind, children, rest)) if end_kind == start_kind -> - parse(rest, [ - Block(start_index, end_index, start_kind, path, children), - ..ast - ]) - Ok(BlockEnd(index, _, _, _)) -> Error(error.UnexpectedBlockEnd(index)) + [Block(start_index, end_index, start_kind, path, children), ..ast] + |> parse(rest) + Ok(BlockEnd(index, _, _, _)) -> + index + |> error.UnexpectedBlockEnd + |> Error } } } @@ -57,11 +69,14 @@ fn parse( pub fn run( tokens: List(tokenizer.Token), ) -> Result(List(AST), error.TokenizerError) { - parse(tokens, []) + parse([], tokens) |> result.try(fn(it) { case it { EOF(ast) -> Ok(ast) - BlockEnd(index, _, _, _) -> Error(error.UnexpectedBlockEnd(index)) + BlockEnd(index, _, _, _) -> + index + |> error.UnexpectedBlockEnd + |> Error } }) } diff --git a/src/handles/internal/tokenizer.gleam b/src/handles/internal/tokenizer.gleam index 3538365..5db2684 100644 --- a/src/handles/internal/tokenizer.gleam +++ b/src/handles/internal/tokenizer.gleam @@ -14,7 +14,7 @@ pub type Token { } type Action { - AddToken(String, Int, Token) + AddToken(token: Token, new_index: Int, rest_of_input: String) Stop(error.TokenizerError) Done } @@ -22,7 +22,7 @@ type Action { /// {{ const length_of_open_tag_syntax = 2 -/// {{#}} or {{#/}} or {{>}} +/// {{#}} or {{/}} or {{>}} const length_of_block_syntax = 5 /// {{}} @@ -54,10 +54,30 @@ fn capture_tag_body( input |> string.split_once("}}") |> result.map_error(fn(_) { - error.UnbalancedTag(index + length_of_open_tag_syntax) + index + length_of_open_tag_syntax + |> error.UnbalancedTag }) } +fn stop(index: Int, to_error: fn(Int) -> error.TokenizerError) -> Action { + index + length_of_open_tag_syntax + |> to_error + |> Stop +} + +fn add_block_sized_token( + token: Token, + index: Int, + consumed: String, + rest: String, +) { + AddToken( + token, + index + length_of_block_syntax + string.length(consumed), + rest, + ) +} + fn tokenize(input: String, index: Int) -> Action { case input { "" -> Done @@ -66,20 +86,12 @@ fn tokenize(input: String, index: Int) -> Action { Error(err) -> Stop(err) Ok(#(body, rest)) -> case split_body(body) { - [] -> - Stop(error.MissingPartialId(index + length_of_open_tag_syntax)) - [_] -> - Stop(error.MissingArgument(index + length_of_open_tag_syntax)) + [] -> stop(index, error.MissingPartialId) + [_] -> stop(index, error.MissingArgument) [id, arg] -> - AddToken( - rest, - index + length_of_block_syntax + string.length(body), - Partial(index + length_of_open_tag_syntax, id, split_arg(arg)), - ) - _ -> - Stop(error.UnexpectedMultipleArguments( - index + length_of_open_tag_syntax, - )) + Partial(index + length_of_open_tag_syntax, id, split_arg(arg)) + |> add_block_sized_token(index, body, rest) + _ -> stop(index, error.UnexpectedMultipleArguments) } } @@ -88,46 +100,31 @@ fn tokenize(input: String, index: Int) -> Action { Error(err) -> Stop(err) Ok(#(body, rest)) -> case split_body(body) { - [] -> - Stop(error.MissingBlockKind(index + length_of_open_tag_syntax)) - [_] -> - Stop(error.MissingArgument(index + length_of_open_tag_syntax)) + [] -> stop(index, error.MissingBlockKind) + [_] -> stop(index, error.MissingArgument) ["if", arg] -> - AddToken( - rest, - index + length_of_block_syntax + string.length(body), - BlockStart( - index + length_of_open_tag_syntax, - block.If, - split_arg(arg), - ), + BlockStart( + index + length_of_open_tag_syntax, + block.If, + split_arg(arg), ) + |> add_block_sized_token(index, body, rest) ["unless", arg] -> - AddToken( - rest, - index + length_of_block_syntax + string.length(body), - BlockStart( - index + length_of_open_tag_syntax, - block.Unless, - split_arg(arg), - ), + BlockStart( + index + length_of_open_tag_syntax, + block.Unless, + split_arg(arg), ) + |> add_block_sized_token(index, body, rest) ["each", arg] -> - AddToken( - rest, - index + length_of_block_syntax + string.length(body), - BlockStart( - index + length_of_open_tag_syntax, - block.Each, - split_arg(arg), - ), - ) - [_, _] -> - Stop(error.UnexpectedBlockKind(index + length_of_open_tag_syntax)) - _ -> - Stop(error.UnexpectedMultipleArguments( + BlockStart( index + length_of_open_tag_syntax, - )) + block.Each, + split_arg(arg), + ) + |> add_block_sized_token(index, body, rest) + [_, _] -> stop(index, error.UnexpectedBlockKind) + _ -> stop(index, error.UnexpectedMultipleArguments) } } @@ -136,32 +133,19 @@ fn tokenize(input: String, index: Int) -> Action { Error(err) -> Stop(err) Ok(#(body, rest)) -> case split_body(body) { - [] -> - Stop(error.MissingBlockKind(index + length_of_open_tag_syntax)) - [_, _] -> - Stop(error.UnexpectedArgument(index + length_of_open_tag_syntax)) + [] -> stop(index, error.MissingBlockKind) + [_, _] -> stop(index, error.UnexpectedArgument) ["if"] -> - AddToken( - rest, - index + length_of_block_syntax + string.length(body), - BlockEnd(index + length_of_open_tag_syntax, block.If), - ) + BlockEnd(index + length_of_open_tag_syntax, block.If) + |> add_block_sized_token(index, body, rest) ["unless"] -> - AddToken( - rest, - index + length_of_block_syntax + string.length(body), - BlockEnd(index + length_of_open_tag_syntax, block.Unless), - ) + BlockEnd(index + length_of_open_tag_syntax, block.Unless) + |> add_block_sized_token(index, body, rest) ["each"] -> - AddToken( - rest, - index + length_of_block_syntax + string.length(body), - BlockEnd(index + length_of_open_tag_syntax, block.Each), - ) - [_] -> - Stop(error.UnexpectedBlockKind(index + length_of_open_tag_syntax)) - _ -> - Stop(error.UnexpectedArgument(index + length_of_open_tag_syntax)) + BlockEnd(index + length_of_open_tag_syntax, block.Each) + |> add_block_sized_token(index, body, rest) + [_] -> stop(index, error.UnexpectedBlockKind) + _ -> stop(index, error.UnexpectedArgument) } } @@ -170,17 +154,14 @@ fn tokenize(input: String, index: Int) -> Action { Error(err) -> Stop(err) Ok(#(body, rest)) -> case split_body(body) { - [] -> Stop(error.MissingArgument(index + length_of_open_tag_syntax)) + [] -> stop(index, error.MissingArgument) [arg] -> AddToken( - rest, - index + length_of_property_syntax + string.length(body), Property(index + length_of_open_tag_syntax, split_arg(arg)), + index + length_of_property_syntax + string.length(body), + rest, ) - _ -> - Stop(error.UnexpectedMultipleArguments( - index + length_of_open_tag_syntax, - )) + _ -> stop(index, error.UnexpectedMultipleArguments) } } _ -> @@ -190,13 +171,13 @@ fn tokenize(input: String, index: Int) -> Action { |> result.map(pair.map_second(_, fn(it) { "{{" <> it })) { Ok(#(str, rest)) -> - AddToken(rest, index + string.length(str), Constant(index, str)) - _ -> AddToken("", index + string.length(input), Constant(index, input)) + AddToken(Constant(index, str), index + string.length(str), rest) + _ -> AddToken(Constant(index, input), index + string.length(input), "") } } } -pub fn do_run( +fn do_run( input: String, index: Int, tokens: List(Token), @@ -204,7 +185,7 @@ pub fn do_run( case tokenize(input, index) { Done -> Ok(list.reverse(tokens)) Stop(err) -> Error(err) - AddToken(rest, index, token) -> do_run(rest, index, [token, ..tokens]) + AddToken(token, index, rest) -> do_run(rest, index, [token, ..tokens]) } }