Skip to content

Commit

Permalink
lexems_to_meta, O(n * m) -> O(n)
Browse files Browse the repository at this point in the history
  • Loading branch information
darky committed Jun 21, 2024
1 parent e9ba639 commit e80436f
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/glerd.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fswalk.{Entry, Stat}
import glance.{
CustomType, Definition, Field, Module, NamedType, TupleType, Variant,
}
import gleam/dict
import gleam/iterator
import gleam/list
import gleam/option.{Some}
Expand Down Expand Up @@ -96,11 +97,12 @@ fn type_args(types) {

fn ast_to_code(root, path, content) {
let lexems = content |> glexer.new |> glexer.lex
let meta = lexems |> lexems_to_meta
let m = module_name_from_path(root, path)
let variants = ast_from_content(content)
use Variant(r, fields) <- list.map(variants)
let f = normalize_fields(fields)
let mt = lexems_to_meta(r, lexems)
let assert Ok(mt) = dict.get(meta, r) |> result.or(Ok(""))
"#(\"" <> r <> "\",\"" <> m <> "\"," <> "[" <> f <> "],\"" <> mt <> "\")"
}

Expand All @@ -119,21 +121,16 @@ fn ast_from_content(content) {
variants
}

fn lexems_to_meta(record_name, lexems) {
let assert Ok(meta) =
lexems
|> list.window_by_2
|> list.find_map(fn(pair) {
case pair {
#(#(CommentDoc(meta), Position(_)), #(UpperName(rec_name), Position(_)))
if rec_name == record_name
-> Ok(meta)
_ -> Error(Nil)
}
})
|> result.or(Ok(""))
|> result.map(fn(s) { s |> string.trim_left })
meta
fn lexems_to_meta(lexems) {
lexems
|> list.window_by_2
|> list.fold(dict.new(), fn(meta_dict, pair) {
case pair {
#(#(CommentDoc(meta), Position(_)), #(UpperName(rec_name), Position(_))) ->
dict.insert(meta_dict, rec_name, meta |> string.trim_left)
_ -> meta_dict
}
})
}

fn normalize_fields(fields) {
Expand Down

0 comments on commit e80436f

Please sign in to comment.