fix(cli): add reserved type declarations and schema
- Use `globalThis` for `reserved` function export
- Add `reserved` field and function to DSL declarations
- Add `reserved` rule to grammar schema
(cherry picked from commit 07b4c8d05d)
This commit is contained in:
parent
e7f4dfcd4a
commit
05bfeb5b69
3 changed files with 38 additions and 1 deletions
|
|
@ -529,7 +529,7 @@ globalThis.optional = optional;
|
|||
globalThis.prec = prec;
|
||||
globalThis.repeat = repeat;
|
||||
globalThis.repeat1 = repeat1;
|
||||
global.reserved = reserved;
|
||||
globalThis.reserved = reserved;
|
||||
globalThis.seq = seq;
|
||||
globalThis.sym = sym;
|
||||
globalThis.token = token;
|
||||
|
|
|
|||
21
cli/npm/dsl.d.ts
vendored
21
cli/npm/dsl.d.ts
vendored
|
|
@ -10,6 +10,7 @@ type PrecRightRule = { type: 'PREC_RIGHT'; content: Rule; value: number };
|
|||
type PrecRule = { type: 'PREC'; content: Rule; value: number };
|
||||
type Repeat1Rule = { type: 'REPEAT1'; content: Rule };
|
||||
type RepeatRule = { type: 'REPEAT'; content: Rule };
|
||||
type ReservedRule = { type: 'RESERVED'; content: Rule; context_name: string };
|
||||
type SeqRule = { type: 'SEQ'; members: Rule[] };
|
||||
type StringRule = { type: 'STRING'; value: string };
|
||||
type SymbolRule<Name extends string> = { type: 'SYMBOL'; name: Name };
|
||||
|
|
@ -167,6 +168,17 @@ interface Grammar<
|
|||
* @see https://tree-sitter.github.io/tree-sitter/creating-parsers/3-writing-the-grammar#keyword-extraction
|
||||
*/
|
||||
word?: ($: GrammarSymbols<RuleName | BaseGrammarRuleName>) => RuleOrLiteral;
|
||||
|
||||
|
||||
/**
|
||||
* Mapping of names to reserved word sets. The first reserved word set is the
|
||||
* global word set, meaning it applies to every rule in every parse state.
|
||||
* The other word sets can be used with the `reserved` function.
|
||||
*/
|
||||
reserved?: Record<
|
||||
string,
|
||||
($: GrammarSymbols<RuleName | BaseGrammarRuleName>) => RuleOrLiteral[]
|
||||
>;
|
||||
}
|
||||
|
||||
type GrammarSchema<RuleName extends string> = {
|
||||
|
|
@ -320,6 +332,15 @@ declare function repeat(rule: RuleOrLiteral): RepeatRule;
|
|||
*/
|
||||
declare function repeat1(rule: RuleOrLiteral): Repeat1Rule;
|
||||
|
||||
/**
|
||||
* Overrides the global reserved word set for a given rule. The word set name
|
||||
* should be defined in the `reserved` field in the grammar.
|
||||
*
|
||||
* @param wordset name of the reserved word set
|
||||
* @param rule rule that will use the reserved word set
|
||||
*/
|
||||
declare function reserved(wordset: string, rule: RuleOrLiteral): ReservedRule;
|
||||
|
||||
/**
|
||||
* Creates a rule that matches any number of other rules, one after another.
|
||||
* It is analogous to simply writing multiple symbols next to each other
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue