UNPKG

clarity-pattern-parser

Version:

Parsing Library for Typescript and Javascript.

33 lines (32 loc) 25 kB
/** * TypeScript Grammar for Clarity Pattern Parser * * A comprehensive parser for the TypeScript language, handling: * - Expressions with full operator precedence (Pratt parsing) * - Type expressions (unions, intersections, conditionals, mapped, etc.) * - All statement types (if, for, while, switch, try, etc.) * - Declarations (function, class, interface, type alias, enum, namespace) * - Module system (import/export) * - Generics, decorators, destructuring, template literals * * Known limitations: * - JSX/TSX is not supported * - Automatic Semicolon Insertion (ASI) is approximated (semicolons are optional) * - Some edge cases with generic type arguments vs comparison operators * - `declare global {}` augmentation not fully supported * - Overloaded function signatures not fully supported */ import { Pattern } from "../../patterns/Pattern"; export declare const typescriptGrammar = "\n\n# ===================================================\n# TYPESCRIPT GRAMMAR\n# ===================================================\n\n# ---------------------------------------------------\n# SECTION 1: Whitespace & Comments\n# ---------------------------------------------------\n_ = /(?:[ \\t\\n\\r\\f\\v]+|\\/\\/[^\\n]*(?:\\n|$)|\\/\\*[\\s\\S]*?\\*\\/)+/\nws = _?\nsemi = ws + \";\"\n\n# ---------------------------------------------------\n# SECTION 2: Identifiers & Keywords\n# ---------------------------------------------------\nreserved = /(?:break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|false|finally|for|function|if|import|in|instanceof|let|new|null|return|super|switch|this|throw|true|try|typeof|var|void|while|with|yield|async|await|undefined)(?![a-zA-Z0-9_$])/\nidentifier-name = /[a-zA-Z_$][a-zA-Z0-9_$]*/\nidentifier = !reserved + identifier-name\n\n# ---------------------------------------------------\n# SECTION 3: Literals\n# ---------------------------------------------------\n\n# Numeric literals: decimal, hex, octal, binary, bigint, separators\nnumber-literal = /(?:0[xX][\\da-fA-F][\\da-fA-F_]*|0[oO][0-7][0-7_]*|0[bB][01][01_]*|(?:(?:0|[1-9][\\d_]*)(?:\\.[\\d_]*)?|\\.[\\d][\\d_]*)(?:[eE][+-]?[\\d][\\d_]*)?)n?/\n\n# String literals\nsingle-string = /'(?:[^'\\\\]|\\\\.)*'/\ndouble-string = /\"(?:[^\"\\\\]|\\\\.)*\"/\nstring-literal = single-string | double-string\n\n# Template literals\ntemplate-chars = /(?:[^`$\\\\]|\\\\.|\\$(?!\\{))+/\ntemplate-span = \"${\" + ws + expr + ws + \"}\"\ntemplate-literal = \"`\" + template-chars? + (template-span + template-chars?)* + \"`\"\n\n# Regular expression literals\nregex-literal = /\\/(?![*\\/])(?:[^\\/\\\\\\[\\n]|\\\\.|\\[(?:[^\\]\\\\]|\\\\.)*\\])+\\/[dgimsuy]*/\n\n# Keyword literals\ntrue-literal = /true(?![a-zA-Z0-9_$])/\nfalse-literal = /false(?![a-zA-Z0-9_$])/\nnull-literal = /null(?![a-zA-Z0-9_$])/\nundefined-literal = /undefined(?![a-zA-Z0-9_$])/\nthis-literal = /this(?![a-zA-Z0-9_$])/\nsuper-literal = /super(?![a-zA-Z0-9_$])/\n\n# ---------------------------------------------------\n# SECTION 4: Property Names\n# ---------------------------------------------------\ncomputed-property-name = \"[\" + ws + expr + ws + \"]\"\nproperty-name = identifier-name | string-literal | number-literal | computed-property-name\n\n# ---------------------------------------------------\n# SECTION 5: Type Parameters & Annotations\n# ---------------------------------------------------\ntype-variance = (\"in\" | \"out\") + _\ntype-constraint = \"extends\" + _ + type-expr\ntype-default = \"=\" + ws + type-expr\ntype-param = (\"const\" + _)? + type-variance? + identifier + (ws + type-constraint)? + (ws + type-default)?\ntype-params = \"<\" + ws + (type-param, ws + \",\" + ws)+ + ws + \">\"\ntype-args = (type-expr, ws + \",\" + ws)+\ntype-annotation = \":\" + ws + type-expr\n\n# ---------------------------------------------------\n# SECTION 6: Parameters & Destructuring\n# ---------------------------------------------------\n\n# Binding patterns (destructuring)\narray-binding-element = (\"...\" + ws)? + (identifier | binding-pattern) + (ws + type-annotation)? + (ws + \"=\" + ws + expr)?\ncomma-sep = ws + \",\" + ws\ntrailing-comma = ws + \",\"\narray-pattern = \"[\" + ws + (array-binding-element, comma-sep)* + trailing-comma? + ws + \"]\"\n\nobject-binding-prop = (\"...\" + ws + identifier) | (property-name + ws + \":\" + ws + (identifier | binding-pattern) + (ws + type-annotation)? + (ws + \"=\" + ws + expr)?) | (identifier + (ws + type-annotation)? + (ws + \"=\" + ws + expr)?)\nobject-pattern = \"{\" + ws + (object-binding-prop, comma-sep)* + trailing-comma? + ws + \"}\"\n\nbinding-pattern = array-pattern | object-pattern\n\n# Function parameters\nrest-param = \"...\" + ws + (identifier | binding-pattern) + (ws + type-annotation)?\nparam = (identifier | binding-pattern) + ws + \"?\"? + (ws + type-annotation)? + (ws + \"=\" + ws + expr)?\nparam-list = (param | rest-param, comma-sep)+ + trailing-comma?\n\n# Constructor parameters (with access modifiers)\naccess-modifier = /(?:public|private|protected)(?![a-zA-Z0-9_$])/\nconstructor-param = (access-modifier + _)? + (\"readonly\" + _)? + (identifier | binding-pattern) + ws + \"?\"? + (ws + type-annotation)? + (ws + \"=\" + ws + expr)?\nconstructor-param-list = (constructor-param | rest-param, comma-sep)+ + trailing-comma?\n\n# ---------------------------------------------------\n# SECTION 7: Type Expressions\n# ---------------------------------------------------\n\n# Primitive types\nany-type = /any(?![a-zA-Z0-9_$])/\nunknown-type = /unknown(?![a-zA-Z0-9_$])/\nnever-type = /never(?![a-zA-Z0-9_$])/\nvoid-type = /void(?![a-zA-Z0-9_$])/\nstring-type = /string(?![a-zA-Z0-9_$])/\nnumber-type = /number(?![a-zA-Z0-9_$])/\nboolean-type = /boolean(?![a-zA-Z0-9_$])/\nsymbol-type = /symbol(?![a-zA-Z0-9_$])/\nbigint-type = /bigint(?![a-zA-Z0-9_$])/\nobject-type-keyword = /object(?![a-zA-Z0-9_$])/\nprimitive-type = any-type | unknown-type | never-type | void-type | string-type | number-type | boolean-type | symbol-type | bigint-type | object-type-keyword | undefined-literal | null-literal\n\n# Type reference: SomeType or Qualified.Name with optional generics\nqualified-type-name = (identifier-name, \".\")+\ntype-reference = qualified-type-name + (ws + \"<\" + ws + type-args + ws + \">\")?\n\n# Literal types\nliteral-type = string-literal | number-literal | true-literal | false-literal\n\n# Typeof type (uses value-level qualified name)\nqualified-name = (identifier-name, \".\")+\ntypeof-type = \"typeof\" + _ + qualified-name\n\n# Infer type\ninfer-type = \"infer\" + _ + identifier + (_ + \"extends\" + _ + type-expr)?\n\n# This type\nthis-type = /this(?![a-zA-Z0-9_$])/\n\n# Parenthesized type\nparen-type = \"(\" + ws + type-expr + ws + \")\"\n\n# Tuple type\ntuple-label = identifier + ws + \"?\"? + ws + \":\" + ws\ntuple-element = (\"...\" + ws)? + tuple-label? + type-expr + (ws + \"?\")?\ntuple-type = \"[\" + ws + (tuple-element, comma-sep)* + trailing-comma? + ws + \"]\"\n\n# Object type literal\nreadonly-mod = \"readonly\" + _\ntype-property-sig = readonly-mod? + property-name + ws + \"?\"? + ws + \":\" + ws + type-expr\ntype-method-sig = property-name + ws + \"?\"? + ws + type-params? + ws + \"(\" + ws + param-list? + ws + \")\" + ws + \":\" + ws + type-expr\ntype-index-sig = readonly-mod? + \"[\" + ws + identifier + ws + \":\" + ws + type-expr + ws + \"]\" + ws + \":\" + ws + type-expr\ntype-call-sig = type-params? + ws + \"(\" + ws + param-list? + ws + \")\" + ws + \":\" + ws + type-expr\ntype-construct-sig = \"new\" + ws + type-params? + ws + \"(\" + ws + param-list? + ws + \")\" + ws + \":\" + ws + type-expr\ntype-member = type-construct-sig | type-call-sig | type-index-sig | type-method-sig | type-property-sig\ntype-member-sep = ws + (\";\" | \",\") + ws\ntype-member-list = (type-member, type-member-sep)* + (ws + (\";\" | \",\"))?\nobject-type-literal = \"{\" + ws + type-member-list? + ws + \"}\"\n\n# Mapped type: { [K in keyof T as NewK]+?: V }\nmapped-readonly-mod = (\"+\" | \"-\")? + \"readonly\" + _\nmapped-optional-mod = (\"+\" | \"-\")? + \"?\"\nmapped-type = \"{\" + ws + mapped-readonly-mod? + \"[\" + ws + identifier + _ + \"in\" + _ + type-expr + (_ + \"as\" + _ + type-expr)? + ws + \"]\" + ws + mapped-optional-mod? + ws + \":\" + ws + type-expr + ws + semi? + ws + \"}\"\n\n# Template literal type\ntemplate-type-chars = /(?:[^`$\\\\]|\\\\.|\\$(?!\\{))+/\ntemplate-type-span = \"${\" + ws + type-expr + ws + \"}\"\ntemplate-literal-type = \"`\" + template-type-chars? + (template-type-span + template-type-chars?)* + \"`\"\n\n# Function type: (params) => ReturnType\n# Uses function-return alias to avoid being classified as prefix\nfunction-return = type-expr\nfunction-type = type-params? + ws + \"(\" + ws + param-list? + ws + \")\" + ws + \"=>\" + ws + function-return\nconstructor-type = \"new\" + ws + type-params? + ws + \"(\" + ws + param-list? + ws + \")\" + ws + \"=>\" + ws + function-return\n\n# --- Type Expression (auto-converts to Expression pattern) ---\n# Precedence order: first listed = highest binding power\n\n# Postfix types\narray-type-postfix = type-expr + \"[\" + ws + \"]\"\ntype-expr-inner = type-expr\nindexed-access-postfix = type-expr + \"[\" + ws + type-expr-inner + ws + \"]\"\n\n# Prefix types\nkeyof-type-prefix = \"keyof\" + _ + type-expr\nreadonly-type-prefix = \"readonly\" + _ + type-expr\nunique-type-prefix = \"unique\" + _ + type-expr\n\n# Infix types\nintersection-type = type-expr + ws + \"&\" + ws + type-expr\nunion-type = type-expr + ws + \"|\" + ws + type-expr\n\n# Conditional type (right-associative, lowest precedence infix)\nconditional-type = type-expr + _ + \"extends\" + _ + type-expr + ws + \"?\" + ws + type-expr + ws + \":\" + ws + type-expr\n\n# The type expression pattern\ntype-expr = array-type-postfix | indexed-access-postfix | keyof-type-prefix | readonly-type-prefix | unique-type-prefix | intersection-type | union-type | conditional-type right | function-type | constructor-type | mapped-type | object-type-literal | tuple-type | template-literal-type | paren-type | literal-type | primitive-type | typeof-type | infer-type | this-type | type-reference\n\n# ---------------------------------------------------\n# SECTION 8: Value Expressions\n# ---------------------------------------------------\n\n# Argument list for function calls\narg = (\"...\" + ws)? + expr\narg-list = (arg, comma-sep)+ + trailing-comma?\n\n# Arrow function\narrow-params = identifier | \"(\" + ws + param-list? + ws + \")\"\narrow-body = block | expr\narrow-function = (\"async\" + _)? + type-params? + ws + arrow-params + (ws + type-annotation)? + ws + \"=>\" + ws + arrow-body\n\n# Function expression\nfunction-expr = (\"async\" + _)? + \"function\" + ws + (\"*\" + ws)? + identifier? + ws + type-params? + ws + \"(\" + ws + param-list? + ws + \")\" + (ws + type-annotation)? + ws + block\n\n# Class expression (anonymous allowed)\nclass-expr = (\"abstract\" + _)? + \"class\" + (_ + identifier)? + (ws + type-params)? + (ws + extends-clause)? + (ws + implements-clause)? + ws + class-body\n\n# New expression\nnew-expr = \"new\" + _ + expr + ws + \"(\" + ws + arg-list? + ws + \")\"\nnew-no-args = \"new\" + _ + expr\n\n# Dynamic import\ndynamic-import = \"import\" + ws + \"(\" + ws + expr + ws + \")\"\n\n# Parenthesized expression\nparen-expr = \"(\" + ws + expr + ws + \")\"\n\n# Array literal\narray-element = (\"...\" + ws)? + expr\narray-literal = \"[\" + ws + (array-element, comma-sep)* + trailing-comma? + ws + \"]\"\n\n# Object literal\nshorthand-property = identifier\nspread-property = \"...\" + ws + expr\nproperty-assignment = property-name + ws + \":\" + ws + expr\nmethod-definition = (\"async\" + _)? + (\"*\" + ws)? + property-name + ws + type-params? + ws + \"(\" + ws + param-list? + ws + \")\" + (ws + type-annotation)? + ws + block\ngetter-definition = \"get\" + _ + property-name + ws + \"(\" + ws + \")\" + (ws + type-annotation)? + ws + block\nsetter-definition = \"set\" + _ + property-name + ws + \"(\" + ws + param + ws + \")\" + ws + block\nobject-property = getter-definition | setter-definition | method-definition | spread-property | property-assignment | shorthand-property\nobject-literal = \"{\" + ws + (object-property, comma-sep)* + trailing-comma? + ws + \"}\"\n\n# --- Expression Pattern (auto-converts to Expression) ---\n# Precedence order: first listed = highest binding power\n\n# Postfix operators\nmember-access = expr + ws + \".\" + ws + identifier-name\noptional-chain = expr + \"?.\" + identifier-name\nexpr-inner = expr\ncomputed-access = expr + \"[\" + ws + expr-inner + ws + \"]\"\noptional-computed = expr + \"?.[\" + ws + expr-inner + ws + \"]\"\ngeneric-call = expr + ws + \"<\" + ws + type-args + ws + \">\" + ws + \"(\" + ws + arg-list? + ws + \")\"\ncall-expr = expr + ws + \"(\" + ws + arg-list? + ws + \")\"\noptional-call = expr + \"?.(\" + ws + arg-list? + ws + \")\"\ntagged-template = expr + ws + template-literal\nnon-null-assert = expr + /!(?!=)/\npostfix-increment = expr + \"++\"\npostfix-decrement = expr + \"--\"\nas-const-expr = expr + _ + \"as\" + _ + \"const\"\nas-expr = expr + _ + \"as\" + _ + type-expr\nsatisfies-expr = expr + _ + \"satisfies\" + _ + type-expr\n\n# Prefix operators\nprefix-increment = \"++\" + ws + expr\nprefix-decrement = \"--\" + ws + expr\nunary-plus = \"+\" + ws + expr\nunary-minus = \"-\" + ws + expr\nlogical-not = /!(?!=)/ + ws + expr\nbitwise-not = \"~\" + ws + expr\ntypeof-expr = \"typeof\" + _ + expr\nvoid-expr = \"void\" + _ + expr\ndelete-expr = \"delete\" + _ + expr\nawait-expr = \"await\" + _ + expr\n\n# Infix operators (highest to lowest precedence)\n# Regex negative lookahead prevents matching prefixes of compound operators\nexp-expr = expr + ws + /\\*\\*(?!=)/ + ws + expr\nmul-div-mod = expr + ws + /\\*(?![*=])|\\/(?!=)|%(?!=)/ + ws + expr\nadd-sub = expr + ws + /\\+(?![+=])|-(?![-=])/ + ws + expr\nshift-expr = expr + ws + />>>(?!=)|>>(?![>=])|<<(?!=)/ + ws + expr\nrelational-expr = expr + _ + (/instanceof(?![a-zA-Z0-9_$])/ | /in(?![a-zA-Z0-9_$])/ | \"<=\" | \">=\" | /<(?![<=])/ | />(?![>=])/) + _ + expr\nequality-expr = expr + ws + /===|!==|==(?!=)|!=(?!=)/ + ws + expr\nbitwise-and = expr + ws + /&(?![&=])/ + ws + expr\nbitwise-xor = expr + ws + /\\^(?!=)/ + ws + expr\nbitwise-or = expr + ws + /\\|(?![|=])/ + ws + expr\nlogical-and = expr + ws + /&&(?!=)/ + ws + expr\nlogical-or = expr + ws + /\\|\\|(?!=)/ + ws + expr\nnullish-coalesce = expr + ws + /\\?\\?(?!=)/ + ws + expr\nternary-expr = expr + ws + /\\?(?!\\?|\\.)/ + ws + expr + ws + \":\" + ws + expr\nassignment-expr = expr + ws + (\">>>=\" | \">>=\" | \"<<=\" | \"**=\" | \"&&=\" | \"||=\" | \"??=\" | \"+=\" | \"-=\" | \"*=\" | \"/=\" | \"%=\" | \"&=\" | \"|=\" | \"^=\" | \"=\") + ws + expr\n\n# The value expression pattern\nexpr = member-access | optional-chain | computed-access | optional-computed | generic-call | call-expr | optional-call | tagged-template | non-null-assert | postfix-increment | postfix-decrement | as-const-expr | as-expr | satisfies-expr | prefix-increment | prefix-decrement | unary-plus | unary-minus | logical-not | bitwise-not | typeof-expr | void-expr | delete-expr | await-expr | exp-expr right | mul-div-mod | add-sub | shift-expr | relational-expr | equality-expr | bitwise-and | bitwise-xor | bitwise-or | logical-and | logical-or | nullish-coalesce | ternary-expr right | assignment-expr right | arrow-function | function-expr | class-expr | new-expr | new-no-args | dynamic-import | paren-expr | array-literal | object-literal | template-literal | regex-literal | number-literal | string-literal | true-literal | false-literal | null-literal | undefined-literal | this-literal | super-literal | identifier\n\n# ---------------------------------------------------\n# SECTION 9: Statements\n# ---------------------------------------------------\n\n# Block\nblock = \"{\" + ws + statement-list? + ws + \"}\"\n\n# Variable declaration\nvar-keyword = /(?:let|const|var|using)(?![a-zA-Z0-9_$])/\ndefinite-assign = ws + \"!\"\nvariable-declarator = (identifier | binding-pattern) + definite-assign? + (ws + type-annotation)? + (ws + \"=\" + ws + expr)?\nvariable-declaration = var-keyword + _ + (variable-declarator, ws + \",\" + ws)+ + semi?\n\n# Expression statement\nexpression-statement = expr + semi?\n\n# If statement\nif-statement = \"if\" + ws + \"(\" + ws + expr + ws + \")\" + ws + statement + (ws + \"else\" + ws + statement)?\n\n# For statement\nfor-var-decl = var-keyword + _ + (variable-declarator, comma-sep)+\nfor-init = for-var-decl | expr\nfor-statement = \"for\" + ws + \"(\" + ws + for-init? + ws + \";\" + ws + expr? + ws + \";\" + ws + expr? + ws + \")\" + ws + statement\n\n# For-in statement\nfor-binding = (var-keyword + _)? + (identifier | binding-pattern)\nfor-in-statement = \"for\" + ws + \"(\" + ws + for-binding + _ + \"in\" + _ + expr + ws + \")\" + ws + statement\n\n# For-of statement\nfor-of-statement = \"for\" + ws + (\"await\" + _)? + \"(\" + ws + for-binding + _ + \"of\" + _ + expr + ws + \")\" + ws + statement\n\n# While statement\nwhile-statement = \"while\" + ws + \"(\" + ws + expr + ws + \")\" + ws + statement\n\n# Do-while statement\ndo-while-statement = \"do\" + ws + statement + ws + \"while\" + ws + \"(\" + ws + expr + ws + \")\" + semi?\n\n# Switch statement\ncase-clause = \"case\" + _ + expr + ws + \":\" + (ws + statement-list)?\ndefault-clause = \"default\" + ws + \":\" + (ws + statement-list)?\nswitch-clause = case-clause | default-clause\nswitch-statement = \"switch\" + ws + \"(\" + ws + expr + ws + \")\" + ws + \"{\" + ws + (switch-clause, ws)* + ws + \"}\"\n\n# Try-catch-finally\ncatch-binding = \"(\" + ws + (identifier | binding-pattern) + (ws + type-annotation)? + ws + \")\"\ncatch-clause = \"catch\" + ws + catch-binding? + ws + block\nfinally-clause = \"finally\" + ws + block\ntry-statement = \"try\" + ws + block + (ws + catch-clause)? + (ws + finally-clause)?\n\n# Return statement\nreturn-statement = \"return\" + (_ + expr)? + semi?\n\n# Throw statement\nthrow-statement = \"throw\" + _ + expr + semi?\n\n# Break and continue\nbreak-statement = \"break\" + (_ + identifier)? + semi?\ncontinue-statement = \"continue\" + (_ + identifier)? + semi?\n\n# Labeled statement\nlabeled-statement = identifier + ws + \":\" + ws + statement\n\n# Debugger statement\ndebugger-statement = \"debugger\" + semi?\n\n# Empty statement\nempty-statement = \";\"\n\n# ---------------------------------------------------\n# SECTION 10: Declarations\n# ---------------------------------------------------\n\n# Decorators\ndecorator = \"@\" + ws + expr\ndecorator-list = (decorator, ws)+\n\n# Function declaration\nfunction-declaration = (\"async\" + _)? + \"function\" + ws + (\"*\" + ws)? + identifier + ws + type-params? + ws + \"(\" + ws + param-list? + ws + \")\" + (ws + type-annotation)? + ws + block\n\n# Class declaration\nclass-member-modifier = access-modifier | /(?:static|abstract|override|readonly|accessor|declare)(?![a-zA-Z0-9_$])/\nclass-modifiers = (class-member-modifier, _)*\n\nclass-property = class-modifiers + ws + property-name + ws + \"?\"? + ws + \"!\"? + (ws + type-annotation)? + (ws + \"=\" + ws + expr)? + semi?\nclass-method = class-modifiers + ws + (\"async\" + _)? + (\"*\" + ws)? + property-name + ws + type-params? + ws + \"(\" + ws + param-list? + ws + \")\" + (ws + type-annotation)? + (ws + block | semi)?\nclass-constructor = class-modifiers + ws + \"constructor\" + ws + \"(\" + ws + constructor-param-list? + ws + \")\" + ws + block\nclass-getter = class-modifiers + ws + \"get\" + _ + property-name + ws + \"(\" + ws + \")\" + (ws + type-annotation)? + ws + block\nclass-setter = class-modifiers + ws + \"set\" + _ + property-name + ws + \"(\" + ws + param + ws + \")\" + ws + block\nclass-index-sig = class-modifiers + ws + \"[\" + ws + identifier + ws + \":\" + ws + type-expr + ws + \"]\" + ws + \":\" + ws + type-expr + semi?\nclass-static-block = \"static\" + ws + block\n\nclass-member = decorator-list? + ws + (class-static-block | class-constructor | class-getter | class-setter | class-method | class-index-sig | class-property)\nclass-body = \"{\" + ws + (class-member, ws)* + ws + \"}\"\n\nextends-clause = \"extends\" + _ + type-expr\nimplements-clause = \"implements\" + _ + (type-expr, ws + \",\" + ws)+\nclass-declaration = decorator-list? + ws + (\"abstract\" + _)? + \"class\" + _ + identifier + (ws + type-params)? + (ws + extends-clause)? + (ws + implements-clause)? + ws + class-body\n\n# Interface declaration\ninterface-extends = \"extends\" + _ + (type-expr, ws + \",\" + ws)+\ninterface-body = \"{\" + ws + type-member-list? + ws + \"}\"\ninterface-declaration = \"interface\" + _ + identifier + (ws + type-params)? + (ws + interface-extends)? + ws + interface-body\n\n# Type alias\ntype-alias = \"type\" + _ + identifier + (ws + type-params)? + ws + \"=\" + ws + type-expr + semi?\n\n# Enum declaration\nenum-member-init = \"=\" + ws + expr\nenum-member = property-name + (ws + enum-member-init)?\nenum-body = \"{\" + ws + (enum-member, comma-sep)* + trailing-comma? + ws + \"}\"\nenum-declaration = (\"const\" + _)? + \"enum\" + _ + identifier + ws + enum-body\n\n# Namespace / module declaration\nnamespace-body = \"{\" + ws + statement-list? + ws + \"}\"\nnamespace-declaration = (\"namespace\" | \"module\") + _ + (identifier | string-literal) + ws + namespace-body\n\n# Ambient declarations\ndeclare-declaration = \"declare\" + _ + (variable-declaration | function-declaration | class-declaration | interface-declaration | type-alias | enum-declaration | namespace-declaration)\n\n# ---------------------------------------------------\n# SECTION 11: Module System (Import / Export)\n# ---------------------------------------------------\n\n# Import\nimport-specifier = (identifier-name + _ + \"as\" + _)? + identifier\nnamed-imports = \"{\" + ws + (import-specifier, comma-sep)* + trailing-comma? + ws + \"}\"\nnamespace-import = \"*\" + _ + \"as\" + _ + identifier\ndefault-and-named = identifier + ws + \",\" + ws + (named-imports | namespace-import)\nimport-clause = default-and-named | namespace-import | named-imports | identifier\nfrom-clause = \"from\" + _ + string-literal\nimport-attr-entry = identifier-name + ws + \":\" + ws + string-literal\nimport-attributes = (\"with\" | \"assert\") + ws + \"{\" + ws + (import-attr-entry, comma-sep)+ + trailing-comma? + ws + \"}\"\nimport-declaration = \"import\" + _ + (\"type\" + _)? + import-clause + _ + from-clause + (ws + import-attributes)? + semi?\nimport-side-effect = \"import\" + _ + string-literal + semi?\n\n# Export\nexport-specifier = (identifier-name + _ + \"as\" + _)? + identifier-name\nnamed-exports = \"{\" + ws + (export-specifier, comma-sep)* + trailing-comma? + ws + \"}\"\nexport-named = \"export\" + _ + (\"type\" + _)? + named-exports + (_ + from-clause)? + semi?\nexport-default-expr = \"export\" + _ + \"default\" + _ + expr + semi?\nexport-default-decl = \"export\" + _ + \"default\" + _ + (function-declaration | class-declaration)\nexport-declaration = \"export\" + _ + (variable-declaration | function-declaration | class-declaration | interface-declaration | type-alias | enum-declaration | namespace-declaration)\nexport-all = \"export\" + _ + (\"type\" + _)? + \"*\" + (_ + \"as\" + _ + identifier-name)? + _ + from-clause + semi?\nexport-equals = \"export\" + ws + \"=\" + ws + expr + semi?\n\n# ---------------------------------------------------\n# SECTION 12: Statements & Program\n# ---------------------------------------------------\n\n# Statement: union of all statement types\n# Order matters: more specific patterns first, expression-statement last\nstatement = import-declaration | import-side-effect | export-all | export-named | export-default-decl | export-default-expr | export-declaration | export-equals | declare-declaration | decorator-list + ws + (class-declaration | function-declaration) | function-declaration | class-declaration | interface-declaration | type-alias | enum-declaration | namespace-declaration | variable-declaration | if-statement | for-of-statement | for-in-statement | for-statement | while-statement | do-while-statement | switch-statement | try-statement | return-statement | throw-statement | break-statement | continue-statement | labeled-statement | block | debugger-statement | empty-statement | expression-statement\n\n# Statement list\nstatement-list = (statement, ws)+\n\n# Program (top-level)\nprogram = ws + statement-list? + ws\n\n"; /** * Parse the TypeScript grammar and return all pattern definitions. */ export declare function createTypeScriptPatterns(): Record<string, Pattern>; /** * Get a specific pattern by name from the TypeScript grammar. */ export declare function getPattern(name: string): Pattern; /** * Parse TypeScript source code and return the AST. */ export declare function parseTypeScript(source: string): import("../..").ParseResult;