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

Update JSON schema based on llama.cpp example #37

Merged
merged 3 commits into from
Apr 10, 2024
Merged
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
28 changes: 14 additions & 14 deletions src/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ test("union types", () => {
root ::= Person
Person ::= "{" ws "\"age\":" ws ( stringlist | numberlist ) "}"
Personlist ::= "[]" | "[" ws Person ("," ws Person)* "]"
string ::= "\"" ([^"]*) "\""
string ::= "\"" (( [^"\x7F\x00-\x1F] | "\\" ( ["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] ) ))* "\""
boolean ::= "true" | "false"
ws ::= [ \t\n]*
number ::= [0-9]+ "."? [0-9]*
number ::= ("-")? [0-9]+ "."? [0-9]*
stringlist ::= "[" ws "]" | "[" ws string ("," ws string)* ws "]"
numberlist ::= "[" ws "]" | "[" ws string ("," ws number)* ws "]"`.trim())
});
Expand All @@ -40,10 +40,10 @@ test("Single interface generation", () => {
root ::= PostalAddress
PostalAddress ::= "{" ws "\"streetNumber\":" ws number "," ws "\"street\":" ws string "," ws "\"city\":" ws string "," ws "\"state\":" ws string "," ws "\"postalCode\":" ws number "}"
PostalAddresslist ::= "[]" | "[" ws PostalAddress ("," ws PostalAddress)* "]"
string ::= "\"" ([^"]*) "\""
string ::= "\"" (( [^"\x7F\x00-\x1F] | "\\" ( ["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] ) ))* "\""
boolean ::= "true" | "false"
ws ::= [ \t\n]*
number ::= [0-9]+ "."? [0-9]*
number ::= ("-")? [0-9]+ "."? [0-9]*
stringlist ::= "[" ws "]" | "[" ws string ("," ws string)* ws "]"
numberlist ::= "[" ws "]" | "[" ws string ("," ws number)* ws "]"`.trim()
);
Expand All @@ -69,10 +69,10 @@ root ::= PostalAddress
PostalAddress ::= "{" ws "\"streetNumber\":" ws number "," ws "\"type\":" ws AddressType "," ws "\"street\":" ws string "," ws "\"city\":" ws string "," ws "\"state\":" ws string "," ws "\"postalCode\":" ws number "}"
PostalAddresslist ::= "[]" | "[" ws PostalAddress ("," ws PostalAddress)* "]"
AddressType ::= "\"business\"" | "\"home\""
string ::= "\"" ([^"]*) "\""
string ::= "\"" (( [^"\x7F\x00-\x1F] | "\\" ( ["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] ) ))* "\""
boolean ::= "true" | "false"
ws ::= [ \t\n]*
number ::= [0-9]+ "."? [0-9]*
number ::= ("-")? [0-9]+ "."? [0-9]*
stringlist ::= "[" ws "]" | "[" ws string ("," ws string)* ws "]"
numberlist ::= "[" ws "]" | "[" ws string ("," ws number)* ws "]"
`.trim()
Expand Down Expand Up @@ -104,10 +104,10 @@ WorkExperience ::= "{" ws "\"company\":" ws string "," ws "\"jobTi
WorkExperiencelist ::= "[]" | "[" ws WorkExperience ("," ws WorkExperience)* "]"
JobCandidate ::= "{" ws "\"name\":" ws string "," ws "\"jobs\":" ws WorkExperiencelist "}"
JobCandidatelist ::= "[]" | "[" ws JobCandidate ("," ws JobCandidate)* "]"
string ::= "\"" ([^"]*) "\""
string ::= "\"" (( [^"\x7F\x00-\x1F] | "\\" ( ["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] ) ))* "\""
boolean ::= "true" | "false"
ws ::= [ \t\n]*
number ::= [0-9]+ "."? [0-9]*
number ::= ("-")? [0-9]+ "."? [0-9]*
stringlist ::= "[" ws "]" | "[" ws string ("," ws string)* ws "]"
numberlist ::= "[" ws "]" | "[" ws string ("," ws number)* ws "]"`);
});
Expand Down Expand Up @@ -159,10 +159,10 @@ OrderStatus ::= "\"Pending\"" | "\"Shipped\"" | "\"Delivered\"" | "\"Canceled\""
Product ::= "{" ws "\"id\":" ws number "," ws "\"name\":" ws string "," ws "\"description\":" ws string "," ws "\"price\":" ws number "," ws "\"category\":" ws ProductCategory "}"
Productlist ::= "[]" | "[" ws Product ("," ws Product)* "]"
ProductCategory ::= "\"Electronics\"" | "\"Clothing\"" | "\"Food\""
string ::= "\"" ([^"]*) "\""
string ::= "\"" (( [^"\x7F\x00-\x1F] | "\\" ( ["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] ) ))* "\""
boolean ::= "true" | "false"
ws ::= [ \t\n]*
number ::= [0-9]+ "."? [0-9]*
number ::= ("-")? [0-9]+ "."? [0-9]*
stringlist ::= "[" ws "]" | "[" ws string ("," ws string)* ws "]"
numberlist ::= "[" ws "]" | "[" ws string ("," ws number)* ws "]"
`.trim()
Expand Down Expand Up @@ -240,10 +240,10 @@ Car ::= "{" ws "\"make\":" ws string "," ws "\"model\":" ws st
Carlist ::= "[]" | "[" ws Car ("," ws Car)* "]"
CarAndOwner ::= "{" ws "\"car\":" ws Car "," ws "\"owner\":" ws Owner "}"
CarAndOwnerlist ::= "[]" | "[" ws CarAndOwner ("," ws CarAndOwner)* "]"
string ::= "\"" ([^"]*) "\""
string ::= "\"" (( [^"\x7F\x00-\x1F] | "\\" ( ["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] ) ))* "\""
boolean ::= "true" | "false"
ws ::= [ \t\n]*
number ::= [0-9]+ "."? [0-9]*
number ::= ("-")? [0-9]+ "."? [0-9]*
stringlist ::= "[" ws "]" | "[" ws string ("," ws string)* ws "]"
numberlist ::= "[" ws "]" | "[" ws string ("," ws number)* ws "]"`.trim()
);
Expand Down Expand Up @@ -386,10 +386,10 @@ Car ::= "{" ws "\"make\":" ws string "," ws "\"model\":" ws st
Carlist ::= "[]" | "[" ws Car ("," ws Car)* "]"
CarAndOwner ::= "{" ws "\"car\":" ws Car "," ws "\"owner\":" ws Owner "}"
CarAndOwnerlist ::= "[]" | "[" ws CarAndOwner ("," ws CarAndOwner)* "]"
string ::= "\"" ([^"]*) "\""
string ::= "\"" (( [^"\x7F\x00-\x1F] | "\\" ( ["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] ) ))* "\""
boolean ::= "true" | "false"
ws ::= [ \t\n]*
number ::= [0-9]+ "."? [0-9]*
number ::= ("-")? [0-9]+ "."? [0-9]*
stringlist ::= "[" ws "]" | "[" ws string ("," ws string)* ws "]"
numberlist ::= "[" ws "]" | "[" ws string ("," ws number)* ws "]"`.trim()
);
Expand Down
23 changes: 22 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
GrammarElement,
GrammarRule,
RuleReference,
alternatives,
charPattern,
group,
literal,
Expand All @@ -18,7 +19,26 @@ const WS_ELEM: GrammarElement = {

const STRING_ELEM: GrammarElement = {
identifier: "string",
alternatives: [sequence(literal(`"`), charPattern(/([^"]*)/g), literal(`"`))],
alternatives: [
sequence(
literal(`"`),
group(
sequence(alternatives(
charPattern(/[^"\x7F\x00-\x1F]/g), // Anything that's not a control sequence
sequence(
literal(`\\`), // Escape sequences
alternatives(
charPattern(/["\\/bfnrt]/g), // WS escape sequences
sequence( // Unicode escape sequences
literal(`u`),
sequence(
charPattern(/[0-9a-fA-F]/g),
charPattern(/[0-9a-fA-F]/g),
charPattern(/[0-9a-fA-F]/g),
charPattern(/[0-9a-fA-F]/g))))))),
"star"),
literal(`"`))
],
};
const BOOLEAN_ELEM: GrammarElement = {
identifier: "boolean",
Expand All @@ -31,6 +51,7 @@ const NUMBER_ELEM: GrammarElement = {
identifier: "number",
alternatives: [
sequence(
group(sequence(literal(`-`)), "optional"),
charPattern(/[0-9]+/g),
charPattern(/"."?/g),
charPattern(/[0-9]*/g)
Expand Down
Loading