DatemathEx - Parser for datemath syntax

I fully understand why you use nimble_parsec, but if you are already working with timex wouldn’t that be better to use combine instead? That would reduce dependencies as combine would be used anyway.

Not sure if remember nimble_parsec well enough, i.e. not sure if we can use label/2 in the middle of pipeline, but wouldn’t that be a more clear to read parser’s rule?

 defparsec :parse_input,
  choice([
    string("now")
    |> map({:now, []})
    |> post_traverse({:overwrite_now, []})
    |> label("UTC now anchor"),
    choice([
      datetime(),
      date()
    ])
    |> label("ISO datetime anchor")
    |> ignore_whitespace
    |> ignore(string("||"))
  ])
  |> ignore_whitespace
  |> parsec(:math_expressions)
  |> eos()
  |> reduce({:reduce_expressions, []})

With that we can:

  1. reduce code (by 4 doubled LOC)
  2. make rule more clear by moving common combinators to the end of pipe and outside of the choice combinator
  3. make rule more clear by moving ignore_whitespace and ignore(string/1) combinators after label which says that those are not part of a “ISO datetime anchor”
1 Like