Parsing a complex key = value structure with optional trailing comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Test.Parser do | |
import NimbleParsec | |
# iex(9)> Test.Parser.parse """ | |
# ...(9)> FOO = 1 (some foo) | |
# ...(9)> BAR = 2 (some | |
# ...(9)> bar) | |
# ...(9)> FAZ = 3 (some faz) | |
# ...(9)> """ | |
# {:ok, ["FOO", 1, "somefoo", "BAR", 2, "somebar", "FAZ", 3, "somefaz"], "\n", | |
# %{}, {4, 55}, 75} | |
@space 0x0020 | |
@tab 0x009 | |
comment = | |
lookahead_not(string(")")) | |
|> optional(ignore(string(" "))) | |
|> choice([ | |
ignore(string("(")), | |
ignore(string("\n")), | |
ignore(utf8_string([@space, @tab], min: 2)), | |
utf8_string([], 1) | |
]) | |
|> repeat() | |
|> ignore(string(")")) | |
|> wrap() | |
|> reduce({Enum, :join, [""]}) | |
start_comment = | |
lookahead_not(string("(")) | |
|> concat(comment) | |
no_comment = | |
lookahead(string("\n")) | |
|> string("") | |
defparsec( | |
:parse, | |
eventually(utf8_string([?A..?Z, ?_], min: 1)) | |
|> eventually(integer(min: 1)) | |
|> choice([no_comment, start_comment]) | |
|> repeat() | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment