-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scope type parser piece of custom command grammar (#2295)
Initial work towards #492; will be used to parse scope types in #2131 Exposes a function `parseScopeType` that can parse strings like `funk`, `curly` etc into their corresponding scope type payloads Here's a railroad: https://deploy-preview-2295--cursorless.netlify.app/custom-command-railroad ## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
- Loading branch information
Showing
18 changed files
with
465 additions
and
174 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
51 changes: 51 additions & 0 deletions
51
packages/cursorless-engine/src/customCommandGrammar/generated/grammar.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Generated automatically by nearley, version 2.20.1 | ||
// http://github.com/Hardmath123/nearley | ||
// Bypasses TS6133. Allow declared but unused functions. | ||
// @ts-ignore | ||
function id(d: any[]): any { return d[0]; } | ||
declare var simpleScopeTypeType: any; | ||
declare var pairedDelimiter: any; | ||
|
||
import { capture } from "../../util/grammarHelpers"; | ||
import { lexer } from "../lexer"; | ||
|
||
interface NearleyToken { | ||
value: any; | ||
[key: string]: any; | ||
}; | ||
|
||
interface NearleyLexer { | ||
reset: (chunk: any, info: any) => void; | ||
next: () => NearleyToken | undefined; | ||
save: () => any; | ||
formatError: (token: any, message: string) => string; | ||
has: (tokenType: any) => boolean; | ||
}; | ||
|
||
interface NearleyRule { | ||
name: string; | ||
symbols: NearleySymbol[]; | ||
postprocess?: (d: any[], loc?: number, reject?: {}) => any; | ||
}; | ||
|
||
type NearleySymbol = string | { literal: any } | { test: (token: any) => boolean }; | ||
|
||
interface Grammar { | ||
Lexer: NearleyLexer | undefined; | ||
ParserRules: NearleyRule[]; | ||
ParserStart: string; | ||
}; | ||
|
||
const grammar: Grammar = { | ||
Lexer: lexer, | ||
ParserRules: [ | ||
{"name": "main", "symbols": ["scopeType"]}, | ||
{"name": "scopeType", "symbols": [(lexer.has("simpleScopeTypeType") ? {type: "simpleScopeTypeType"} : simpleScopeTypeType)], "postprocess": capture("type")}, | ||
{"name": "scopeType", "symbols": [(lexer.has("pairedDelimiter") ? {type: "pairedDelimiter"} : pairedDelimiter)], "postprocess": | ||
([delimiter]) => ({ type: "surroundingPair", delimiter }) | ||
} | ||
], | ||
ParserStart: "main", | ||
}; | ||
|
||
export default grammar; |
14 changes: 14 additions & 0 deletions
14
packages/cursorless-engine/src/customCommandGrammar/grammar.ne
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,14 @@ | ||
@preprocessor typescript | ||
@{% | ||
import { capture } from "../../util/grammarHelpers"; | ||
import { lexer } from "../lexer"; | ||
%} | ||
@lexer lexer | ||
|
||
main -> scopeType | ||
|
||
# --------------------------- Scope types --------------------------- | ||
scopeType -> %simpleScopeTypeType {% capture("type") %} | ||
scopeType -> %pairedDelimiter {% | ||
([delimiter]) => ({ type: "surroundingPair", delimiter }) | ||
%} |
39 changes: 39 additions & 0 deletions
39
packages/cursorless-engine/src/customCommandGrammar/grammarScopeType.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import assert from "assert"; | ||
import { ScopeType } from "@cursorless/common"; | ||
import { parseScopeType } from "./parseScopeType"; | ||
|
||
interface TestCase { | ||
input: string; | ||
expectedOutput: ScopeType; | ||
} | ||
|
||
const testCases: TestCase[] = [ | ||
{ | ||
input: "funk", | ||
expectedOutput: { | ||
type: "namedFunction", | ||
}, | ||
}, | ||
{ | ||
input: "curly", | ||
expectedOutput: { | ||
type: "surroundingPair", | ||
delimiter: "curlyBrackets", | ||
}, | ||
}, | ||
{ | ||
input: "string", | ||
expectedOutput: { | ||
type: "surroundingPair", | ||
delimiter: "string", | ||
}, | ||
}, | ||
]; | ||
|
||
suite("custom grammar: scope types", () => { | ||
testCases.forEach(({ input, expectedOutput }) => { | ||
test(input, () => { | ||
assert.deepStrictEqual(parseScopeType(input), expectedOutput); | ||
}); | ||
}); | ||
}); |
76 changes: 76 additions & 0 deletions
76
packages/cursorless-engine/src/customCommandGrammar/lexer.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as assert from "assert"; | ||
import { unitTestSetup } from "../test/unitTestSetup"; | ||
import { lexer } from "./lexer"; | ||
|
||
interface Token { | ||
type: string; | ||
value: string; | ||
} | ||
|
||
interface Fixture { | ||
input: string; | ||
expectedOutput: Token[]; | ||
} | ||
|
||
const fixtures: Fixture[] = [ | ||
{ | ||
input: "funk", | ||
expectedOutput: [ | ||
{ | ||
type: "simpleScopeTypeType", | ||
value: "namedFunction", | ||
}, | ||
], | ||
}, | ||
{ | ||
input: "curly", | ||
expectedOutput: [ | ||
{ | ||
type: "pairedDelimiter", | ||
value: "curlyBrackets", | ||
}, | ||
], | ||
}, | ||
{ | ||
input: "state name", | ||
expectedOutput: [ | ||
{ | ||
type: "simpleScopeTypeType", | ||
value: "statement", | ||
}, | ||
{ | ||
type: "ws", | ||
value: " ", | ||
}, | ||
{ | ||
type: "simpleScopeTypeType", | ||
value: "name", | ||
}, | ||
], | ||
}, | ||
{ | ||
input: "funk name", | ||
expectedOutput: [ | ||
{ | ||
type: "simpleScopeTypeType", | ||
value: "functionName", | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
suite("custom grammar lexer", () => { | ||
unitTestSetup(); | ||
|
||
fixtures.forEach(({ input, expectedOutput }) => { | ||
test(input, () => { | ||
assert.deepStrictEqual( | ||
Array.from(lexer.reset(input)).map(({ type, value }) => ({ | ||
type, | ||
value, | ||
})), | ||
expectedOutput, | ||
); | ||
}); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
packages/cursorless-engine/src/customCommandGrammar/lexer.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { simpleScopeTypeTypes, surroundingPairNames } from "@cursorless/common"; | ||
import moo from "moo"; | ||
import { defaultSpokenFormMap } from "../spokenForms/defaultSpokenFormMap"; | ||
|
||
interface Token { | ||
type: string; | ||
value: string; | ||
} | ||
|
||
const tokens: Record<string, Token> = {}; | ||
|
||
// FIXME: Remove the duplication below? | ||
|
||
for (const simpleScopeTypeType of simpleScopeTypeTypes) { | ||
const { spokenForms } = | ||
defaultSpokenFormMap.simpleScopeTypeType[simpleScopeTypeType]; | ||
for (const spokenForm of spokenForms) { | ||
tokens[spokenForm] = { | ||
type: "simpleScopeTypeType", | ||
value: simpleScopeTypeType, | ||
}; | ||
} | ||
} | ||
|
||
for (const pairedDelimiter of surroundingPairNames) { | ||
const { spokenForms } = defaultSpokenFormMap.pairedDelimiter[pairedDelimiter]; | ||
for (const spokenForm of spokenForms) { | ||
tokens[spokenForm] = { | ||
type: "pairedDelimiter", | ||
value: pairedDelimiter, | ||
}; | ||
} | ||
} | ||
|
||
export const lexer = moo.compile({ | ||
ws: /[ \t]+/, | ||
token: { | ||
match: Object.keys(tokens), | ||
type: (text) => tokens[text].type, | ||
value: (text) => tokens[text].value, | ||
}, | ||
}); | ||
|
||
(lexer as any).transform = (token: { value: string }) => token.value; |
29 changes: 29 additions & 0 deletions
29
packages/cursorless-engine/src/customCommandGrammar/parseScopeType.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Parser, Grammar } from "nearley"; | ||
import grammar from "./generated/grammar"; | ||
import { ScopeType } from "@cursorless/common"; | ||
|
||
function getScopeTypeParser(): Parser { | ||
return new Parser( | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
Grammar.fromCompiled({ ...grammar, ParserStart: "scopeType" }), | ||
); | ||
} | ||
|
||
/** | ||
* Given a textual representation of a scope type, parse it into a scope type. | ||
* | ||
* @param input A textual representation of a scope type | ||
* @returns A parsed scope type | ||
*/ | ||
export function parseScopeType(input: string): ScopeType { | ||
const parser = getScopeTypeParser(); | ||
parser.feed(input); | ||
|
||
if (parser.results.length !== 1) { | ||
throw new Error( | ||
`Expected exactly one result, got ${parser.results.length}`, | ||
); | ||
} | ||
|
||
return parser.results[0] as ScopeType; | ||
} |
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.