* Add new variable shorthand operators and new variable macros
- Add `{{hasvar}}` and `{{deletevar}}` macros for local variables
- Add `{{hasglobalvar}}` and `{{deleteglobalvar}}` macros for global variables
- Implement new variable shorthand operators: `-=`, `||`, `??`, `||=`, `??=`, `==`
- Refactor variable expression evaluation to use direct variable API calls instead of routing through macro registry
- Add comprehensive operator support in lexer with proper token ordering for multi-character operators
* Implement lazy evaluation for variable shorthand value expressions
- Replace eager value evaluation with lazy evaluation pattern for variable operators
- Add `#createLazyValue()` method that caches value expression result on first call
- Change `#executeVariableOperation()` to accept `lazyValue` function instead of pre-evaluated value
- Only evaluate value expressions when actually needed (e.g., when variable is falsy for `||`, when variable doesn't exist for `??`)
- Add comprehensive e2e tests
* Update variable shorthand autocomplete with new operators and add edge case tests
- Add new operators to autocomplete definitions: `-=`, `||`, `??`, `||=`, `??=`, `==`
- Update example lists in `VariableShorthandAutoCompleteOption` and `VariableNameAutoCompleteOption`
- Add operator definitions with descriptions and `needsValue` flags to `VariableOperatorDefinitions`
- Update `parseMacroContext()` to recognize new operators with proper ordering (longer operators checked first)
* Add not equals (!=) operator to variable shorthand expressions
- Add `!=` operator to variable shorthand definitions for local and global variables
- Implement `notEquals` operation in `MacroCstWalker` that returns inverted equality comparison
- Add `NotEquals` token to lexer with pattern `/!=`
- Update parser to handle `!=` operator with value expression
- Add `!=` operator to autocomplete examples and operator definitions
- Update `parseMacroContext()` to recognize `!=` operator and...
* Tiny code review fixes
- Fix typo: "insie" → "inside" in variable shorthand operators comment
- Remove extra space in `{{getglobalvar}}` example usage
* Fix missing closing braces in hasglobalvar macro example usage
* Fix isFalsy to normalize value before checking boolean state in variable shorthand operators
- Apply `normalize()` to value before passing to `isFalseBoolean()` in `isFalsy` helper
- Ensures consistent falsy evaluation for variable shorthand operators like `||`, `??`, `||=`, `??=`
* Fix pipe character inside macro braces being treated as command separator
- Add `isInsideMacroBraces()` method to track unclosed `{{}}` macro braces
- Modify `testCommandEnd()` to only treat `|` as command end when not inside macro braces
- Scan text behind current position to calculate macro brace depth
- Skip second character when detecting `{{` or `}}` pairs
- Ensure `depth` never goes below 0 using `Math.max(0, depth - 1)`
* Add aliases for variable existence and deletion macros
- Add `varexists` alias for `hasvar` macro
- Add `flushvar` alias for `deletevar` macro
- Add `globalvarexists` alias for `hasglobalvar` macro
- Add `flushglobalvar` alias for `deleteglobalvar` macro
* Update variable shorthand operator descriptions to clarify return behavior
- Add "returns nothing" clarification to `=`, `+=`, `-=` operators in examples and definitions
- Add "returns the new value" clarification to `++`, `--` operators in examples and definitions
- Update `VariableOperatorDefinitions` descriptions to match example text
- Ensures users understand which operators return values vs perform silent mutations
262 lines
10 KiB
JavaScript
262 lines
10 KiB
JavaScript
import { chevrotain } from '../../../lib.js';
|
|
import { MacroLexer } from './MacroLexer.js';
|
|
|
|
const { CstParser } = chevrotain;
|
|
|
|
/** @typedef {import('chevrotain').TokenType} TokenType */
|
|
/** @typedef {import('chevrotain').CstNode} CstNode */
|
|
/** @typedef {import('chevrotain').ILexingError} ILexingError */
|
|
/** @typedef {import('chevrotain').IRecognitionException} IRecognitionException */
|
|
|
|
/**
|
|
* The singleton instance of the MacroParser.
|
|
*
|
|
* @type {MacroParser}
|
|
*/
|
|
let instance;
|
|
export { instance as MacroParser };
|
|
|
|
class MacroParser extends CstParser {
|
|
/** @type {MacroParser} */ static #instance;
|
|
/** @type {MacroParser} */ static get instance() { return MacroParser.#instance ?? (MacroParser.#instance = new MacroParser()); }
|
|
|
|
/** @private */
|
|
constructor() {
|
|
super(MacroLexer.def, {
|
|
traceInitPerf: false,
|
|
nodeLocationTracking: 'full',
|
|
recoveryEnabled: true,
|
|
});
|
|
const Tokens = MacroLexer.tokens;
|
|
|
|
const $ = this;
|
|
|
|
// Top-level document rule that can handle both plaintext and macros
|
|
$.document = $.RULE('document', () => {
|
|
$.MANY(() => {
|
|
$.OR([
|
|
{ ALT: () => $.CONSUME(Tokens.Plaintext, { LABEL: 'plaintext' }) },
|
|
{ ALT: () => $.CONSUME(Tokens.PlaintextOpenBrace, { LABEL: 'plaintext' }) },
|
|
{ ALT: () => $.SUBRULE($.macro) },
|
|
{ ALT: () => $.CONSUME(Tokens.Macro.Start, { LABEL: 'plaintext' }) },
|
|
]);
|
|
});
|
|
});
|
|
|
|
// Basic Macro Structure - can be either a regular macro or a variable expression
|
|
$.macro = $.RULE('macro', () => {
|
|
$.CONSUME(Tokens.Macro.Start);
|
|
|
|
// Optional flags before the identifier (e.g., {{!user}}, {{?~macro}}, {{>filtered}})
|
|
// Both regular flags and filter flag are captured under the 'flags' label
|
|
$.MANY(() => {
|
|
$.OR1([
|
|
{ ALT: () => $.CONSUME(Tokens.Macro.Flags, { LABEL: 'flags' }) },
|
|
{ ALT: () => $.CONSUME(Tokens.Macro.FilterFlag, { LABEL: 'flags' }) },
|
|
]);
|
|
});
|
|
|
|
// Branch: either a variable expression (starts with . or $) or a regular macro
|
|
$.OR([
|
|
// Variable expression branch
|
|
{ ALT: () => $.SUBRULE($.variableExpr) },
|
|
// Regular macro branch
|
|
{ ALT: () => $.SUBRULE($.macroBody) },
|
|
]);
|
|
|
|
$.CONSUME(Tokens.Macro.End);
|
|
});
|
|
|
|
// Regular macro body (flags + identifier + optional arguments)
|
|
$.macroBody = $.RULE('macroBody', () => {
|
|
// Macro identifier (name)
|
|
$.OR2([
|
|
{ ALT: () => $.CONSUME(Tokens.Macro.DoubleSlash, { LABEL: 'Macro.identifier' }) },
|
|
{ ALT: () => $.CONSUME(Tokens.Macro.Identifier, { LABEL: 'Macro.identifier' }) },
|
|
]);
|
|
$.OPTION(() => $.SUBRULE($.arguments));
|
|
});
|
|
|
|
// Variable expression: .varName or $varName with optional operator
|
|
$.variableExpr = $.RULE('variableExpr', () => {
|
|
// Variable scope prefix
|
|
$.OR3([
|
|
{ ALT: () => $.CONSUME(Tokens.Var.LocalPrefix, { LABEL: 'Var.scope' }) },
|
|
{ ALT: () => $.CONSUME(Tokens.Var.GlobalPrefix, { LABEL: 'Var.scope' }) },
|
|
]);
|
|
|
|
// Variable identifier (name)
|
|
$.CONSUME(Tokens.Var.Identifier, { LABEL: 'Var.identifier' });
|
|
|
|
// Optional operator (and expression, if operator requires one)
|
|
$.OPTION2(() => $.SUBRULE($.variableOperator));
|
|
});
|
|
|
|
// Variable operator: ++, --, = value, += value, -= value, ||, ??, ||=, ??=, ==, !=
|
|
$.variableOperator = $.RULE('variableOperator', () => {
|
|
$.OR4([
|
|
{ ALT: () => $.CONSUME(Tokens.Var.Operators.Increment, { LABEL: 'Var.operator' }) },
|
|
{ ALT: () => $.CONSUME(Tokens.Var.Operators.Decrement, { LABEL: 'Var.operator' }) },
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Var.Operators.NullishCoalescingEquals, { LABEL: 'Var.operator' });
|
|
$.SUBRULE($.variableValue, { LABEL: 'Var.value' });
|
|
},
|
|
},
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Var.Operators.NullishCoalescing, { LABEL: 'Var.operator' });
|
|
$.SUBRULE2($.variableValue, { LABEL: 'Var.value' });
|
|
},
|
|
},
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Var.Operators.LogicalOrEquals, { LABEL: 'Var.operator' });
|
|
$.SUBRULE3($.variableValue, { LABEL: 'Var.value' });
|
|
},
|
|
},
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Var.Operators.LogicalOr, { LABEL: 'Var.operator' });
|
|
$.SUBRULE4($.variableValue, { LABEL: 'Var.value' });
|
|
},
|
|
},
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Var.Operators.MinusEquals, { LABEL: 'Var.operator' });
|
|
$.SUBRULE5($.variableValue, { LABEL: 'Var.value' });
|
|
},
|
|
},
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Var.Operators.DoubleEquals, { LABEL: 'Var.operator' });
|
|
$.SUBRULE6($.variableValue, { LABEL: 'Var.value' });
|
|
},
|
|
},
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Var.Operators.NotEquals, { LABEL: 'Var.operator' });
|
|
$.SUBRULE7($.variableValue, { LABEL: 'Var.value' });
|
|
},
|
|
},
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Var.Operators.PlusEquals, { LABEL: 'Var.operator' });
|
|
$.SUBRULE8($.variableValue, { LABEL: 'Var.value' });
|
|
},
|
|
},
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Var.Operators.Equals, { LABEL: 'Var.operator' });
|
|
$.SUBRULE9($.variableValue, { LABEL: 'Var.value' });
|
|
},
|
|
},
|
|
]);
|
|
});
|
|
|
|
// Variable value: everything after = or += until the end
|
|
// Can contain nested macros and any other tokens
|
|
$.variableValue = $.RULE('variableValue', () => {
|
|
$.MANY2(() => {
|
|
$.OR5([
|
|
{ ALT: () => $.SUBRULE($.macro) }, // Nested macros
|
|
{ ALT: () => $.CONSUME(Tokens.Identifier) },
|
|
{ ALT: () => $.CONSUME(Tokens.Unknown) },
|
|
]);
|
|
});
|
|
});
|
|
|
|
// Arguments Parsing
|
|
$.arguments = $.RULE('arguments', () => {
|
|
$.OR([
|
|
{
|
|
ALT: () => {
|
|
$.CONSUME(Tokens.Args.DoubleColon, { LABEL: 'separator' });
|
|
$.AT_LEAST_ONE_SEP({
|
|
SEP: Tokens.Args.DoubleColon,
|
|
DEF: () => $.SUBRULE($.argument, { LABEL: 'argument' }),
|
|
});
|
|
},
|
|
},
|
|
{
|
|
ALT: () => {
|
|
$.OPTION(() => {
|
|
$.CONSUME(Tokens.Args.Colon, { LABEL: 'separator' });
|
|
});
|
|
$.SUBRULE($.argumentAllowingColons, { LABEL: 'argument' });
|
|
},
|
|
// So, this is a bit hacky. But implemented below, the argument capture does explicitly exclude double colons
|
|
// from being captured as the first token. The potential ambiguity chevrotain claims here is not possible.
|
|
// It says stuff like <Args.DoubleColon, Identifier/Macro/Unknown> is possible in both branches, but it is not.
|
|
IGNORE_AMBIGUITIES: true,
|
|
},
|
|
]);
|
|
});
|
|
|
|
// List the argument tokens here, as we need two rules, one to be able to parse with double colons and one without
|
|
const validArgumentTokens = [
|
|
{ ALT: () => $.SUBRULE($.macro) }, // Nested Macros
|
|
{ ALT: () => $.CONSUME(Tokens.Identifier) },
|
|
{ ALT: () => $.CONSUME(Tokens.Unknown) },
|
|
{ ALT: () => $.CONSUME(Tokens.Args.Colon) },
|
|
{ ALT: () => $.CONSUME(Tokens.Args.Equals) },
|
|
{ ALT: () => $.CONSUME(Tokens.Args.Quote) },
|
|
];
|
|
|
|
$.argument = $.RULE('argument', () => {
|
|
$.MANY(() => {
|
|
$.OR([...validArgumentTokens]);
|
|
});
|
|
});
|
|
$.argumentAllowingColons = $.RULE('argumentAllowingColons', () => {
|
|
$.AT_LEAST_ONE(() => {
|
|
$.OR([
|
|
...validArgumentTokens,
|
|
{ ALT: () => $.CONSUME(Tokens.Args.DoubleColon) },
|
|
]);
|
|
});
|
|
});
|
|
|
|
this.performSelfAnalysis();
|
|
}
|
|
|
|
/**
|
|
* Parses a document into a CST.
|
|
*
|
|
* @param {string} input
|
|
* @returns {{ cst: CstNode|null, errors: ({ message: string }|ILexingError|IRecognitionException)[] , lexingErrors: ILexingError[], parserErrors: IRecognitionException[] }}
|
|
*/
|
|
parseDocument(input) {
|
|
if (!input) {
|
|
return { cst: null, errors: [{ message: 'Input is empty' }], lexingErrors: [], parserErrors: [] };
|
|
}
|
|
|
|
const lexingResult = MacroLexer.tokenize(input);
|
|
|
|
this.input = lexingResult.tokens;
|
|
const cst = this.document();
|
|
|
|
const errors = [
|
|
...lexingResult.errors,
|
|
...this.errors,
|
|
];
|
|
|
|
return { cst, errors, lexingErrors: lexingResult.errors, parserErrors: this.errors };
|
|
}
|
|
|
|
test(input) {
|
|
const lexingResult = MacroLexer.tokenize(input);
|
|
// "input" is a setter which will reset the parser's state.
|
|
this.input = lexingResult.tokens;
|
|
const cst = this.macro();
|
|
|
|
// For testing purposes we need to actually persist the error messages in the object,
|
|
// otherwise the test cases cannot read those, as they don't have access to the exception object type.
|
|
const errors = this.errors.map(x => ({ message: x.message, ...x, stack: x.stack }));
|
|
|
|
return { cst, errors: errors };
|
|
}
|
|
}
|
|
|
|
instance = MacroParser.instance;
|