Skip to content

Add ES6-style shorthand property syntax for grammar object literals#1942

Merged
curtisman merged 1 commit intomicrosoft:mainfrom
curtisman:short
Feb 22, 2026
Merged

Add ES6-style shorthand property syntax for grammar object literals#1942
curtisman merged 1 commit intomicrosoft:mainfrom
curtisman:short

Conversation

@curtisman
Copy link
Copy Markdown
Member

Summary

This PR implements JavaScript-style shorthand property syntax for object literals in .agr grammar files. When a property name matches a variable name, you can now write { name } instead of the more verbose { name: name }.

Example transformation:

- play $(track:string) -> { actionName: "play", parameters: { track: track } }
+ play $(track:string) -> { actionName: "play", parameters: { track } }

Changes

  • Parser (grammarRuleParser.ts): Extended object property parsing to recognize shorthand syntax, storing it as null in the AST to distinguish from explicit values
  • Compiler (grammarCompiler.ts): Added validation for shorthand variable references to ensure referenced variables exist in scope
  • Matcher (grammarMatcher.ts): Implemented shorthand property resolution during pattern matching
  • Writer (grammarRuleWriter.ts): Updated serialization to output shorthand form when appropriate
  • Environment (environment.ts): Added parseObjectProperties helper to process shorthand properties in value expressions
  • Grammar files: Updated all .agr schema files across agents (browser, calendar, desktop, list, player) to use shorthand syntax
  • Tests: Updated all test files to use and verify shorthand syntax

Benefits

  • Improved readability: Reduces visual noise in grammar definitions
  • Less verbosity: Particularly beneficial for parameter blocks with multiple variables
  • Familiar syntax: Aligns with ES6 JavaScript conventions, making grammars more intuitive for developers
  • Backward compatible: Full form ({ name: name }) still works; this is purely additive

Test Plan

  • ✅ All existing tests pass with updated syntax
  • ✅ Parser correctly handles both shorthand and full forms
  • ✅ Compiler validates shorthand variable references
  • ✅ Matcher resolves shorthand properties correctly
  • ✅ Serialization round-trips preserve shorthand form

🤖 Generated with Claude Code

Implements JavaScript-like shorthand property syntax in .agr grammar files,
allowing `{ name }` instead of `{ name: name }` when property name matches
variable name.

Changes:
- Parser: Recognize shorthand syntax (stores as null in AST)
- Compiler: Validate shorthand variable references
- Matcher: Resolve shorthand properties during matching
- Writer: Output shorthand form in serialized grammars
- Environment: Process shorthand in value expressions
- Updated all .agr files and tests to use shorthand syntax

This improves grammar readability and reduces verbosity, especially in
parameter blocks with many variables.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@curtisman curtisman enabled auto-merge February 22, 2026 18:36
@curtisman curtisman added this pull request to the merge queue Feb 22, 2026
Merged via the queue into microsoft:main with commit 8f91f8b Feb 22, 2026
13 of 15 checks passed
steveluc added a commit that referenced this pull request Feb 22, 2026
- Revert "what's" (double-quoted in pattern expressions) back to
  unquoted what's — double quotes are literal token chars in expression
  parsing (not string delimiters), so "what's" created the wrong token.
  The tmLanguage (?<!\w)' lookbehind handles syntax highlighting correctly
  for unquoted apostrophes.
- Resolve conflict: remove calendarModule.agr (keep deletion over main's
  modification of the unreferenced example file)
- Resolve conflict: keep our removal of the/my $(wildcard) list patterns
  from listSchema.agr (spurious completion fix)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant