Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP add json-schema grammar test from llama.cpp #1

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/ebnf.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,45 @@ defmodule EBNF do
group_alternates(rest, alternates, [factor | current_group])
end

defp times(factor, times) do
1..times//1
|> Enum.map(fn _i -> factor end)
end

defp expand_repetition(factor, {:times, [t]}, ids, name) do

rule = if t > 0 do
times(factor, t)
else
{:empty, []}
end


next_symbol_id = map_size(ids)
next_rule_name = "#{name}_#{next_symbol_id}"
ids = Map.put(ids, next_rule_name, next_symbol_id)

{next_rule_name, traverse_rule({:rule, [{:identifier, [next_rule_name]} | rule ]}, ids, name)}
end

defp expand_repetition(factor, {:from_to, [from, to]}, ids, name) when from <= to do

from_rules = if from > 0 do
times(factor, from)
else
[{:empty, []}]
end
to_rules = from+1..to//1
|> Enum.reduce([], fn e,acc -> times(factor,e) ++ ["|" | acc] end)
next_symbol_id = map_size(ids)
next_rule_name = "#{name}_#{next_symbol_id}"
ids = Map.put(ids, next_rule_name, next_symbol_id)

rule = [alternate: to_rules ++ from_rules]

{next_rule_name, traverse_rule({:rule, [{:identifier, [next_rule_name]} | rule ]}, ids, name)}
end

defp expand_repetition(factor, rep, ids, name) do
next_symbol_id = map_size(ids)
next_rule_name = "#{name}_#{next_symbol_id}"
Expand Down
43 changes: 38 additions & 5 deletions lib/ebnf/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ defmodule EBNF.Parser do
@moduledoc false

import NimbleParsec
import EBNF.Parser.Helpers

from_to =
ignore(string("{"))
|> integer(min: 1)
|> ignore(string(","))
|> integer(min: 1)
|> ignore(string("}"))
|> tag(:from_to)

times =
ignore(string("{"))
|> integer(min: 1)
|> ignore(string("}"))
|> tag(:times)

whitespace = repeat(choice([string(" "), string("\n"), string("\t")]))

Expand All @@ -25,8 +38,16 @@ defmodule EBNF.Parser do
choice([
string(~S( )) |> replace(?\s),
string(~S(\t)) |> replace(?\t),
string("\t") |> replace(?\t),
string(~S(\n)) |> replace(?\n),
string("\n") |> replace(?\n),
string("\r") |> replace(?\r),
string(~S(\\)) |> replace(?\\),
string("\\") |> replace(?\\),
string("\"") |> replace(?\"),
string("\d") |> replace(?\d),
string("\0") |> replace(?\0),
string("\x1F") |> replace(~s(\x1F)),
ascii_char([?a..?z, ?A..?Z, ?0..?9, ?_, ?-, ?+, ?*, ?/])
])
|> tag(:char)
Expand All @@ -37,11 +58,20 @@ defmodule EBNF.Parser do
|> concat(char)
|> tag(:range)



range_or_char = choice([range, char])

negation =
ignore(string("^"))
|> concat(range_or_char)
|> tag(:negation)

negation_range_or_char = choice([negation, range, char])

character_set =
ignore(string("["))
|> times(range_or_char, min: 1)
|> times(negation_range_or_char, min: 1)
|> ignore(string("]"))
|> tag(:character_set)

Expand All @@ -50,7 +80,7 @@ defmodule EBNF.Parser do
|> tag(:terminal)

identifier =
ascii_string([?a..?z, ?A..?Z, ?0..?9, ?_], min: 1)
ascii_string([?a..?z, ?A..?Z, ?0..?9, ?_, ?-], min: 1)
|> tag(:identifier)

grouping =
Expand All @@ -63,10 +93,13 @@ defmodule EBNF.Parser do

factor =
choice([identifier, terminal, grouping])
|> repeatable()
|> then(&choice([
tag(concat(&1,choice([string("*"), string("+"), string("?"), from_to, times])),:repetition),
&1
]))
|> ignore(repeat(choice([string(" "), string("\t")])))

term = times(factor, min: 1)
term = times(factor, min: 0)

choice_separator =
ignore(repeat(string(" ")))
Expand Down
10 changes: 0 additions & 10 deletions lib/ebnf/parser/helpers.ex

This file was deleted.

Loading