Skip to content

Commit

Permalink
Update JSON schema based on llama.cpp example (#37)
Browse files Browse the repository at this point in the history
Addresses #31 

Also drive-by: add support for negative `number`s
  • Loading branch information
a10y authored Apr 10, 2024
1 parent aa9c033 commit 0ef1dd7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
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

0 comments on commit 0ef1dd7

Please sign in to comment.