Skip to content

Commit

Permalink
feat(WIP): 2.0 Failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Olian04 committed Jun 18, 2024
1 parent 23b0c82 commit 13ac494
Show file tree
Hide file tree
Showing 18 changed files with 716 additions and 759 deletions.
37 changes: 15 additions & 22 deletions src/handles.gleam
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import gleam/dynamic
import gleam/result
import handles/engine
import handles/lexer
import handles/parser

pub type HandlesError {
LexError(error: lexer.LexError)
ParseError(error: List(parser.ParseError))
RuntimeError(error: engine.RuntimeError)
}
import gleam/string_builder
import handles/ctx
import handles/error
import handles/internal/engine
import handles/internal/parser
import handles/internal/tokenizer

pub type Template {
Template(ast: List(parser.AST))
}

pub fn prepare(template: String) -> Result(Template, HandlesError) {
use tokens <- result.try(result.map_error(lexer.run(template), LexError))
use ast <- result.try(
result.map_error(parser.run(tokens, ["if", "unless", "each"]), fn(err) {
ParseError(err)
}),
)
Ok(Template(ast))
pub fn prepare(template: String) -> Result(Template, error.TokenizerError) {
tokenizer.run(template, 0, [])
|> result.map(fn(tokens) {
let ast = parser.run(tokens, [])
Template(ast)
})
}

pub fn run(
template: Template,
ctx: dynamic.Dynamic,
) -> Result(String, HandlesError) {
root_ctx: List(ctx.Prop),
) -> Result(String, error.RuntimeError) {
let Template(ast) = template
engine.run(ast, ctx)
|> result.map_error(RuntimeError)
engine.run(ast, ctx.Dict(root_ctx), string_builder.new())
}
12 changes: 12 additions & 0 deletions src/handles/ctx.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub type Prop {
Prop(key: String, value: Value)
}

pub type Value {
Str(value: String)
Int(value: Int)
Float(value: Float)
Bool(value: Bool)
Dict(value: List(Prop))
List(value: List(Value))
}
166 changes: 0 additions & 166 deletions src/handles/engine.gleam

This file was deleted.

93 changes: 31 additions & 62 deletions src/handles/format.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import gleam/int
import gleam/list
import gleam/string
import handles/lexer
import handles/error

type Position {
Position(index: Int, row: Int, col: Int)
Expand Down Expand Up @@ -39,69 +38,39 @@ fn resolve_position(
}
}

pub fn format_lex_error(error: lexer.LexError, template: String) -> String {
case error {
lexer.UnbalancedTag(index, _) ->
case resolve_position(template, index, Position(0, 0, 0)) {
Position(_, row, col) ->
string.concat([
"Tag is missing closing braces }} ",
"(row=",
int.to_string(row),
", col=",
int.to_string(col),
")",
])
OutOfBounds -> panic as "Unable to resolve error position in template"
}
lexer.SyntaxError(errors) ->
errors
|> list.fold("", fn(acc, err) {
string.concat([acc, "\n", format_syntax_error(err, template)])
})
pub fn transform_error(template: String, offset: Int, message: String) {
case resolve_position(template, offset, Position(0, 0, 0)) {
Position(_, row, col) ->
Ok(
message
<> " (row="
<> int.to_string(row)
<> ", col="
<> int.to_string(col)
<> ")",
)
OutOfBounds -> Error(Nil)
}
}

pub fn format_syntax_error(error: lexer.SyntaxError, template: String) -> String {
pub fn format_tokenizer_error(
error: error.TokenizerError,
template: String,
) -> Result(String, Nil) {
case error {
lexer.MissingBody(start, _) ->
case resolve_position(template, start, Position(0, 0, 0)) {
Position(_, row, col) ->
string.concat([
"Tag is missing a body ",
"(row=",
int.to_string(row),
", col=",
int.to_string(col),
")",
])
OutOfBounds -> panic as "Unable to resolve error position in template"
}
lexer.MissingBlockKind(start, _) ->
case resolve_position(template, start, Position(0, 0, 0)) {
Position(_, row, col) ->
string.concat([
"Tag is of unknown block kind ",
"(row=",
int.to_string(row),
", col=",
int.to_string(col),
")",
])
OutOfBounds -> panic as "Unable to resolve error position in template"
}
lexer.UnexpectedBlockArgument(start, _) ->
case resolve_position(template, start, Position(0, 0, 0)) {
Position(_, row, col) ->
string.concat([
"Tag is a closing block, which does not take any arguments ",
"(row=",
int.to_string(row),
", col=",
int.to_string(col),
")",
])
OutOfBounds -> panic as "Unable to resolve error position in template"
}
error.UnbalancedTag(index) ->
transform_error(template, index, "Tag is missing closing braces }}")
error.UnexpectedBlockArgument(index) ->
transform_error(
template,
index,
"Tag is a closing block, which does not take any arguments",
)
error.MissingBlockArgument(index) ->
transform_error(template, index, "Tag is missing block argument")
error.MissingPropertyPath(index) ->
transform_error(template, index, "Tag is missing property path")
error.UnexpectedBlockKind(index) ->
transform_error(template, index, "Tag is of unknown block kind")
}
}
Loading

0 comments on commit 13ac494

Please sign in to comment.