1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
defmodule MakeupTest.Lexer.Fixtures.TokenLexer do
@moduledoc false
import NimbleParsec
import Makeup.Lexer.Combinators
@behaviour Makeup.Lexer
# use a literal binary as an argument
chris = token("chris", :phoenix_creator)
# use the string combinator
grych = token(string("grych"), :drab_creator)
# set attrbutes
jose = token("jose", :elixir_creator, %{country: "Brazil"})
# unicode fun
josé = token(string("josé"), :elixir_creator, %{country: "Poland"})
# parses a pair of characters and turns them into a binary using `lexeme/1`
characters_lexeme =
utf8_char([])
|> utf8_char([])
|> lexeme()
|> token(:character_lexeme)
whitespace = ascii_string([?\s], min: 1)
root_element_combinator =
choice([
whitespace,
chris,
grych,
jose,
josé,
characters_lexeme
])
root_combinator = repeat(root_element_combinator)
@impl Makeup.Lexer
defparsec(
:root,
root_combinator
)
@impl Makeup.Lexer
defparsec(
:root_element,
root_element_combinator
)
@impl Makeup.Lexer
def postprocess(tokens, _options \\ []), do: tokens
@impl Makeup.Lexer
def match_groups(tokens, _options \\ []), do: tokens
@impl Makeup.Lexer
def lex(text, _options \\ []) do
{:ok, tokens, "", _, _, _} = root(text)
tokens
end
end
|