Skip to main content

Module parse

Module parse 

Source
Expand description

TextMate / LSP snippet body parser. Produces a SnippetBody from a string; consumers (renderer + active-snippet state machine) walk the resulting tokens.

Grammar (matching VS Code’s parser; see LSP 3.17 §Snippet Syntax):

body          ::= ( token )*
token         ::= literal | dollar
literal       ::= ( escaped | safe_char )+
escaped       ::= '\\' ( '$' | '\\' | '}' )
safe_char     ::= any char except '$' | '}' | escape opener

dollar        ::= '$' ( int | name | '{' block '}' )
block         ::= int                            -- '${1}'
                | int ':' body                   -- '${1:foo}'
                | int '|' choices '|'            -- '${1|a,b,c|}'
                | int '/' pat '/' repl '/' flags -- '${1/.../.../i}'
                | name                           -- '${VAR}'
                | name ':' body                  -- '${VAR:fallback}'
                | name '/' pat '/' repl '/' flags
choices       ::= choice ( ',' choice )*
choice        ::= ( '\\,' | not(',' | '|') )*
pat / repl    ::= ( '\\/' | not('/') )*
flags         ::= [a-zA-Z]*
int           ::= [0-9]+
name          ::= [a-zA-Z_][a-zA-Z0-9_]*

On a malformed dollar block (e.g. unclosed ${), the parser falls back to literal output for that span – VS Code’s “be lenient” behaviour. This means a snippet body that looks-like-but-isn’t a placeholder still inserts as plain text rather than failing the whole expansion. Returns a ParseError only for genuinely catastrophic input (currently never – v1 always falls through to literal).

Enums§

ParseError
Snippet parse error. Empty in v1 – the parser is total and falls back to literal output on malformed input. Reserved for future strict-mode parsing if a use case appears.

Functions§

parse
Parse a TextMate / LSP snippet body string.