-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: conditional rules in the language grammars
- Loading branch information
Showing
22 changed files
with
656 additions
and
320 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
#!/usr/bin/env -S node --import @keybr/tsl | ||
|
||
import { LCG } from "@keybr/rand"; | ||
import { flagSet } from "./flags.ts"; | ||
import { Syntax } from "./syntax.ts"; | ||
|
||
for (const syntax of Syntax.ALL) { | ||
console.log(`=== ${syntax.name} ===`); | ||
console.log(`=== ${syntax.name} (${[...syntax.flags].join(",")}) ===`); | ||
const rng = LCG(1); | ||
for (let i = 0; i < 5; i++) { | ||
console.log(syntax.generate(rng)); | ||
console.log(syntax.generate(flagSet(["*"]), rng)); | ||
} | ||
console.log(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import test from "ava"; | ||
import { findFlags } from "./find-flags.ts"; | ||
import { parse } from "./parse.ts"; | ||
|
||
test("find flags", (t) => { | ||
const rules = parse( | ||
`a -> { :if(f1) "a" { :if(f2) "b" | { :if(f3) "c" } } };\n` + | ||
`b -> "..." { :class(c1) { :if(xyz) "1" | "2" | "3" } } "...";\n`, | ||
); | ||
const flags = findFlags(rules); | ||
t.deepEqual(flags, new Set(["f1", "f2", "f3", "xyz"])); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
isAlt, | ||
isCond, | ||
isOpt, | ||
isSeq, | ||
isSpan, | ||
type Prod, | ||
type Rules, | ||
} from "./ast.ts"; | ||
|
||
export function findFlags(rules: Rules): Set<string> { | ||
const flags = new Set<string>(); | ||
for (const rule of Object.values(rules)) { | ||
visit(rule); | ||
} | ||
return flags; | ||
|
||
function visit(p: Prod): void { | ||
if (isCond(p)) { | ||
flags.add(p.flag); | ||
visit(p.cond); | ||
return; | ||
} | ||
|
||
if (isSpan(p)) { | ||
visit(p.span); | ||
return; | ||
} | ||
|
||
if (isOpt(p)) { | ||
visit(p.opt); | ||
return; | ||
} | ||
|
||
if (isSeq(p)) { | ||
for (const child of p.seq) { | ||
visit(child); | ||
} | ||
return; | ||
} | ||
|
||
if (isAlt(p)) { | ||
for (const child of p.alt) { | ||
visit(child); | ||
} | ||
return; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type Flags = { | ||
has(flag: string): boolean; | ||
}; | ||
|
||
export function flagSet(flags: readonly string[] | ReadonlySet<string>): Flags { | ||
const set = new Set(flags); | ||
return { | ||
has(flag) { | ||
return set.has(flag) || set.has("*"); | ||
}, | ||
}; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./flags.ts"; | ||
export * from "./generate.ts"; | ||
export * from "./syntax.ts"; |
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
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
Oops, something went wrong.