pax_global_header00006660000000000000000000000064146153363130014517gustar00rootroot0000000000000052 comment=b3775dc544f97b66cee02dc324c0cd6f5a532dea javascript-1.4.16/000077500000000000000000000000001461533631300137565ustar00rootroot00000000000000javascript-1.4.16/.gitignore000066400000000000000000000000531461533631300157440ustar00rootroot00000000000000/node_modules/ /src/parser.* .tern-* /dist javascript-1.4.16/.npmignore000066400000000000000000000000161461533631300157520ustar00rootroot00000000000000/node_modules javascript-1.4.16/CHANGELOG.md000066400000000000000000000164251461533631300155770ustar00rootroot00000000000000## 1.4.16 (2024-05-04) ### Bug fixes Don't consume `?.` tokens when followed by a digit. Support type arguments on non-call expressions. ## 1.4.15 (2024-04-23) ### Bug fixes Add support for `new.target` syntax. ## 1.4.14 (2024-03-30) ### Bug fixes Recognize the `d` and `v` RegExp flags. Support destructured parameters in function types Allow destructured parameters in function signature types. ## 1.4.13 (2024-01-16) ### Bug fixes Fix inverted relative precedence of `&`, `|`, and `^` bitwise operators. Add an explicit dependency on @lezer/common ^1.2.0. ## 1.4.12 (2024-01-04) ### Bug fixes Mark strings, comments, and interpolations as isolating for bidirectional text. ## 1.4.11 (2023-12-18) ### Bug fixes In TSX mode, parse `` or `` as type parameters, not JSX tags. ## 1.4.10 (2023-12-06) ### Bug fixes Support `|` and `&` as prefixes in TypeScript types. ## 1.4.9 (2023-10-30) ### Bug fixes Allow `default` to be used in `import`/`export` binding sets. ## 1.4.8 (2023-10-05) ### Bug fixes Properly highlight `using` as a keyword. ## 1.4.7 (2023-08-23) ### Bug fixes Properly parse hashbang comments. ## 1.4.6 (2023-08-22) ### Bug fixes Make sure that, in TypeScript, type argument lists are prefered over comparison operators when both produce valid parses. ## 1.4.5 (2023-07-25) ### Bug fixes Allow the index in a TypeScript indexed type to be any kind of type. ## 1.4.4 (2023-07-03) ### Bug fixes Add support for `using` syntax. Make the package work with new TS resolution styles. ## 1.4.3 (2023-04-24) ### Bug fixes Properly parse `this: Type` within parameter lists for TypeScript. ## 1.4.2 (2023-03-29) ### Bug fixes Properly parse `declare` in front of class properties and methods in TypeScript. ## 1.4.1 (2023-01-09) ### Bug fixes Fix a bug where something like `yield [1]` (or `await`) was parsed as a member expression. Add support for `yield*` syntax. Escapes in strings are now parsed as their own tokens (and styled with the `escape` tag). ## 1.4.0 (2022-12-19) ### New features The new `"SingleClassItem"` top-level rule can be used to parse only a class item (method, property, or static block). ## 1.3.2 (2022-12-14) ### Bug fixes Typescript allows `override` on all class elements, not just methods. Allow expressions in class extends clauses in TypeScript. ## 1.3.1 (2022-11-29) ### Bug fixes Actually emit a tree node for the `@` characters in decorators. ## 1.3.0 (2022-11-28) ### New features Add support for decorator syntax. ## 1.2.0 (2022-11-24) ### New features The grammar now supports `top: "SingleExpression"` to parse an expression rather than a script. ## 1.1.1 (2022-11-19) ### Bug fixes Fix parsing of computed properties in class declarations. ## 1.1.0 (2022-11-17) ### Bug fixes Fix parsing of 'null' as type in TypeScript. Allow computed properties in object destructuring patterns. Add TypeScript 4.9's `satisfies` operator. Support `accessor` syntax on class properties. ### New features Add support for optional call syntax. Distinguish lower-case JSX element names syntactically, give them a `standard(tagName)` highlight tag. ## 1.0.2 (2022-07-21) ### Bug fixes Properly assign a highlighting tag to the `super` keyword. ## 1.0.1 (2022-06-27) ### Bug fixes Fix parsing of TypeScript conditional types. Support type parameters in TypeScript function type syntax. ## 1.0.0 (2022-06-06) ### New features First stable version. ## 0.16.0 (2022-04-20) ### Breaking changes Move to 0.16 serialized parser format. ### Bug fixes Allow commas as separators in TypeScript object type syntax. ### New features Add `CatchClause` and `FinallyClause` nodes wrapping parts of `TryStatement`. The parser now includes syntax highlighting information in its node types. ## 0.15.3 (2022-01-26) ### Bug fixes Support missing values in array pattern syntax. Support quoted module export names. ### New features Template string interpolations now get their own nodes in the syntax tree. ## 0.15.2 (2021-12-08) ### Bug fixes Fix a typo in the `TaggedTemplateExpression` node name. Support n suffixes after non-decimal integers Add support for non-decimal bignum literals (). Add support for static class initialization blocks. ## 0.15.1 (2021-11-12) ### Bug fixes Add support for TypeScript `import {type X} from y` syntax. Indexed TypeScript types can now take type parameters. Add support for private field syntax. Rename PropertyNameDefinition node to PropertyDefinition for consistency with other names. ### New features Recognize TypeScript 4.3's `override` keyword. ## 0.15.0 (2021-08-11) ### Breaking changes The module's name changed from `lezer-javascript` to `@lezer/javascript`. Upgrade to the 0.15.0 lezer interfaces. ## 0.13.4 (2021-04-30) ### Bug fixes Fixes a bug where arrow functions with expression bodies would include commas after the expression. ## 0.13.3 (2021-02-15) ### Bug fixes Wrap escaped JSX attribute values in a `JSXEscape` node. ## 0.13.2 (2021-01-18) ### Bug fixes Fix parsing of async function expressions. ## 0.13.1 (2020-12-04) ### Bug fixes Fix versions of lezer packages depended on. ## 0.13.0 (2020-12-04) ## 0.12.0 (2020-10-23) ### Breaking changes Adjust to changed serialized parser format. ## 0.11.1 (2020-09-26) ### Bug fixes Fix lezer depencency versions ## 0.11.0 (2020-09-26) ### Breaking changes Follow change in serialized parser format. ## 0.10.1 (2020-09-02) ### Bug fixes Fix associativity of `else` and ternary operators. Work around accidental ambiguity of TypeScript method and constructor signatures. Properly parse `??=` as an update operator. ## 0.10.0 (2020-08-07) ### Breaking changes Upgrade to 0.10 parser serialization ### New features The gammar now supports TypeScript (use the `"ts"` dialect). The grammar can now parse JSX syntax (use the `"jsx"` dialect). ## 0.9.1 (2020-06-29) ### Bug fixes Fix accidental use of non-ES5 library methods. ## 0.9.0 (2020-06-08) ### Breaking changes Upgrade to 0.9 parser serialization ## 0.8.4 (2020-05-30) ### Bug fixes Fix the package.json `main` field pointing at the wrong file, breaking the library in node versions older than 13. ## 0.8.3 (2020-04-09) ### Bug fixes Regenerate parser with a fix in lezer-generator so that the top node prop is properly assigned. ## 0.8.2 (2020-04-01) ### Bug fixes Make the package load as an ES module on node ## 0.8.1 (2020-02-28) ### New features Provide an ES module file. ## 0.8.0 (2020-02-03) ### Bug fixes Add support for the spread ... operator in array literals. ### New features Follow 0.8.0 release of the library. Add support for nullish coalescing and optional chaining. ## 0.7.0 (2020-01-20) ### Breaking changes Use the lezer 0.7.0 parser format. ## 0.5.2 (2020-01-15) ### Bug fixes Regenerate with lezer-generator 0.5.2 to avoid cyclic forced reductions. ## 0.5.1 (2019-10-22) ### Bug fixes Fix top prop missing from build output. ## 0.5.0 (2019-10-22) ### Breaking changes Move from `lang` to `top` prop on document node. ## 0.4.0 (2019-09-10) ### Breaking changes Adjust to 0.4.0 parse table format. ## 0.3.0 (2019-08-22) ### New features Go back to node names, add props, follow changes in grammar syntax. ## 0.2.0 (2019-08-02) ### New features Use tags rather than names. ## 0.1.0 (2019-07-09) ### New Features First documented release. javascript-1.4.16/LICENSE000066400000000000000000000021311461533631300147600ustar00rootroot00000000000000MIT License Copyright (C) 2018 by Marijn Haverbeke and others Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. javascript-1.4.16/README.md000066400000000000000000000007071461533631300152410ustar00rootroot00000000000000# @lezer/javascript This is a JavaScript grammar for the [lezer](https://lezer.codemirror.net/) parser system. It parses ES2020, and supports a `"ts"` [dialect](https://lezer.codemirror.net/docs/guide/#dialects) to parse TypeScript, and a `"jsx"` dialect to parse JSX. The `top` option can be set to `"SingleExpression"` or `"SingleClassItem"` to parse an expression or class item instead of a full program. The code is licensed under an MIT license. javascript-1.4.16/dist/000077500000000000000000000000001461533631300147215ustar00rootroot00000000000000javascript-1.4.16/dist/index.d.cts000066400000000000000000000001021461533631300167560ustar00rootroot00000000000000import {LRParser} from "@lezer/lr" export const parser: LRParser javascript-1.4.16/dist/index.d.ts000066400000000000000000000001021461533631300166130ustar00rootroot00000000000000import {LRParser} from "@lezer/lr" export const parser: LRParser javascript-1.4.16/package.json000066400000000000000000000017721461533631300162530ustar00rootroot00000000000000{ "name": "@lezer/javascript", "version": "1.4.16", "description": "lezer-based JavaScript grammar", "main": "dist/index.cjs", "type": "module", "exports": { "import": "./dist/index.js", "require": "./dist/index.cjs" }, "module": "dist/index.js", "types": "dist/index.d.ts", "author": "Marijn Haverbeke ", "license": "MIT", "devDependencies": { "@lezer/generator": "^1.0.0", "mocha": "^10.2.0", "rollup": "^2.52.2", "@rollup/plugin-node-resolve": "^9.0.0" }, "dependencies": { "@lezer/lr": "^1.3.0", "@lezer/common": "^1.2.0", "@lezer/highlight": "^1.1.3" }, "repository": { "type": "git", "url": "https://github.com/lezer-parser/javascript.git" }, "scripts": { "build": "lezer-generator src/javascript.grammar -o src/parser && rollup -c", "build-debug": "lezer-generator src/javascript.grammar --names -o src/parser && rollup -c", "prepare": "npm run build", "test": "mocha test/test-*.js" } } javascript-1.4.16/rollup.config.js000066400000000000000000000004621461533631300170770ustar00rootroot00000000000000import {nodeResolve} from "@rollup/plugin-node-resolve" export default { input: "./src/parser.js", output: [{ format: "cjs", file: "./dist/index.cjs" }, { format: "es", file: "./dist/index.js" }], external(id) { return !/^[\.\/]/.test(id) }, plugins: [ nodeResolve() ] } javascript-1.4.16/src/000077500000000000000000000000001461533631300145455ustar00rootroot00000000000000javascript-1.4.16/src/highlight.js000066400000000000000000000045411461533631300170560ustar00rootroot00000000000000import {styleTags, tags as t} from "@lezer/highlight" export const jsHighlight = styleTags({ "get set async static": t.modifier, "for while do if else switch try catch finally return throw break continue default case": t.controlKeyword, "in of await yield void typeof delete instanceof": t.operatorKeyword, "let var const using function class extends": t.definitionKeyword, "import export from": t.moduleKeyword, "with debugger as new": t.keyword, TemplateString: t.special(t.string), super: t.atom, BooleanLiteral: t.bool, this: t.self, null: t.null, Star: t.modifier, VariableName: t.variableName, "CallExpression/VariableName TaggedTemplateExpression/VariableName": t.function(t.variableName), VariableDefinition: t.definition(t.variableName), Label: t.labelName, PropertyName: t.propertyName, PrivatePropertyName: t.special(t.propertyName), "CallExpression/MemberExpression/PropertyName": t.function(t.propertyName), "FunctionDeclaration/VariableDefinition": t.function(t.definition(t.variableName)), "ClassDeclaration/VariableDefinition": t.definition(t.className), PropertyDefinition: t.definition(t.propertyName), PrivatePropertyDefinition: t.definition(t.special(t.propertyName)), UpdateOp: t.updateOperator, "LineComment Hashbang": t.lineComment, BlockComment: t.blockComment, Number: t.number, String: t.string, Escape: t.escape, ArithOp: t.arithmeticOperator, LogicOp: t.logicOperator, BitOp: t.bitwiseOperator, CompareOp: t.compareOperator, RegExp: t.regexp, Equals: t.definitionOperator, Arrow: t.function(t.punctuation), ": Spread": t.punctuation, "( )": t.paren, "[ ]": t.squareBracket, "{ }": t.brace, "InterpolationStart InterpolationEnd": t.special(t.brace), ".": t.derefOperator, ", ;": t.separator, "@": t.meta, TypeName: t.typeName, TypeDefinition: t.definition(t.typeName), "type enum interface implements namespace module declare": t.definitionKeyword, "abstract global Privacy readonly override": t.modifier, "is keyof unique infer": t.operatorKeyword, JSXAttributeValue: t.attributeValue, JSXText: t.content, "JSXStartTag JSXStartCloseTag JSXSelfCloseEndTag JSXEndTag": t.angleBracket, "JSXIdentifier JSXNameSpacedName": t.tagName, "JSXAttribute/JSXIdentifier JSXAttribute/JSXNameSpacedName": t.attributeName, "JSXBuiltin/JSXIdentifier": t.standard(t.tagName) }) javascript-1.4.16/src/javascript.grammar000066400000000000000000000454021461533631300202700ustar00rootroot00000000000000@dialects { jsx, ts } @precedence { typeargs, typeMember, typePrefix, intersectionPrefixed @left, intersection @left, unionPrefixed @left, union @left, typeExtends @right, else @right, member, readonly, newArgs, call, instantiate, taggedTemplate, prefix, postfix, typeof, exp @left, times @left, plus @left, shift @left, loop, rel @left, satisfies, equal @left, bitAnd @left, bitXor @left, bitOr @left, and @left, or @left, ternary @right, assign @right, comma @left, statement @cut, predicate } @top Script { Hashbang? statement+ } @top SingleExpression { expression } @top SingleClassItem { classItem } statement[@isGroup=Statement] { ExportDeclaration | ImportDeclaration | ForStatement { kw<"for"> ckw<"await">? (ForSpec | ForInSpec | ForOfSpec) statement } | WhileStatement { kw<"while"> ParenthesizedExpression statement } | WithStatement { kw<"with"> ParenthesizedExpression statement } | DoStatement { kw<"do"> statement kw<"while"> ParenthesizedExpression semi } | IfStatement { kw<"if"> ParenthesizedExpression statement (!else kw<"else"> statement)? } | SwitchStatement { kw<"switch"> ParenthesizedExpression SwitchBody { "{" switchItem* "}" } } | TryStatement { kw<"try"> Block CatchClause { kw<"catch"> ("(" pattern ")")? Block }? FinallyClause { kw<"finally"> Block }? } | ReturnStatement { kw<"return"> (noSemi expression)? semi } | ThrowStatement { kw<"throw"> expression semi } | BreakStatement { kw<"break"> (noSemi Label)? semi } | ContinueStatement { kw<"continue"> (noSemi Label)? semi } | DebuggerStatement { kw<"debugger"> semi } | Block | LabeledStatement { Label ":" statement } | declaration | ExpressionStatement { expression semi } | ";" } ExportDeclaration { kw<"export"> Star (ckw<"as"> (VariableName | String))? ckw<"from"> String semi | kw<"export"> kw<"default"> (FunctionDeclaration | ClassDeclaration | expression semi) | kw<"export"> tskw<"type">? declaration | kw<"export"> tskw<"type">? ExportGroup (ckw<"from"> String)? semi | kw<"export"> "=" expression semi } ExportGroup { "{" commaSep<(VariableName | String | kw<"default">) (ckw<"as"> (VariableName { word } | String))?> "}" } ImportDeclaration { kw<"import"> tskw<"type">? (Star ckw<"as"> VariableDefinition | commaSep) ckw<"from"> String semi | kw<"import"> String semi } ImportGroup { "{" commaSep? (VariableDefinition | (VariableName | String | kw<"default">) ckw<"as"> VariableDefinition)> "}" } ForSpec { "(" (VariableDeclaration | expression ";" | ";") expression? ";" expression? ")" } forXSpec { "(" (variableDeclarationKeyword pattern | VariableName | MemberExpression | ArrayPattern | ObjectPattern) !loop op expression ")" } ForInSpec { forXSpec> } ForOfSpec { forXSpec> } declaration { FunctionDeclaration | ClassDeclaration | VariableDeclaration | TypeAliasDeclaration | InterfaceDeclaration | EnumDeclaration | NamespaceDeclaration | AmbientDeclaration } FunctionDeclaration { async? !statement kw<"function"> Star? VariableDefinition? functionSignature (Block | semi) } ClassDeclaration { !statement Decorator* tskw<"abstract">? kw<"class"> VariableDefinition TypeParamList? (kw<"extends"> ((VariableName | MemberExpression) !typeargs TypeArgList | expression))? (tskw<"implements"> commaSep1)? ClassBody } classItem { MethodDeclaration | PropertyDeclaration | StaticBlock | ";" } ClassBody { "{" classItem* "}" } privacy { @extend[@name=Privacy,@dialect=ts] } privacyArg { @extend[@name=Privacy,@dialect=ts] } propModifier { Decorator | tsPkwMod<"declare"> | privacy | pkwMod<"static"> | tsPkwMod<"abstract"> | tsPkwMod<"override"> } classPropName { propName | PrivatePropertyDefinition } MethodDeclaration[group=ClassItem] { propModifier* pkwMod<"async">? (pkwMod<"get"> | pkwMod<"set"> | Star)? classPropName functionSignature (Block | semi) } StaticBlock[group=ClassItem] { pkwMod<"static"> Block } PropertyDeclaration[group=ClassItem] { propModifier* (tsPkwMod<"readonly"> | pkwMod<"accessor">)? classPropName (Optional | LogicOp<"!">)? TypeAnnotation? ("=" expressionNoComma)? semi } variableDeclarationKeyword { kw<"let"> | kw<"var"> | kw<"const"> | ckw<"await">? ckw<"using"> } VariableDeclaration { variableDeclarationKeyword commaSep1 semi } TypeAliasDeclaration { tskw<"type"> TypeDefinition TypeParamList? "=" type semi } InterfaceDeclaration { tskw<"interface"> TypeDefinition TypeParamList? (kw<"extends"> type)? ObjectType } EnumDeclaration { kw<"const">? tskw<"enum"> TypeDefinition EnumBody { "{" commaSep | tskw<"module">) VariableDefinition ("." PropertyDefinition)* Block } AmbientDeclaration { tskw<"declare"> ( VariableDeclaration | TypeAliasDeclaration | EnumDeclaration | InterfaceDeclaration | NamespaceDeclaration | GlobalDeclaration { tskw<"global"> Block } | ClassDeclaration { tskw<"abstract">? kw<"class"> VariableDefinition TypeParamList? (kw<"extends"> expression)? (tskw<"implements"> commaSep1)? ClassBody { "{" ( MethodDeclaration | PropertyDeclaration | IndexSignature semi )* "}" } } | AmbientFunctionDeclaration { async? kw<"function"> Star? VariableDefinition? TypeParamList? ParamList (TypeAnnotation | TypePredicate) semi } ) } decoratorExpression { VariableName | MemberExpression { decoratorExpression !member ("." | questionDot) (PropertyName | PrivatePropertyName) } | CallExpression { decoratorExpression !call TypeArgList? questionDot? ArgList } | ParenthesizedExpression } Decorator { "@" decoratorExpression } pattern { VariableDefinition | ArrayPattern | ObjectPattern } ArrayPattern { "[" commaSep<("..."? patternAssign)?> ~destructure "]" } ObjectPattern { "{" commaSep ~destructure "}" } patternAssign { pattern ("=" expressionNoComma)? } TypeAnnotation { ":" type } TypePredicate { ":" (VariableName | kw<"this">) !predicate tskw<"is"> type } patternAssignTyped { pattern Optional? TypeAnnotation? ("=" expressionNoComma)? } ParamList { "(" commaSep<"..." patternAssignTyped | Decorator* privacyArg? tskw<"readonly">? patternAssignTyped | kw<"this"> TypeAnnotation> ")" } Block { !statement "{" statement* "}" } switchItem { CaseLabel { kw<"case"> expression ":" } | DefaultLabel { kw<"default"> ":" } | statement } expression[@isGroup=Expression] { expressionNoComma | SequenceExpression } SequenceExpression { expressionNoComma !comma ("," expressionNoComma)+ } expressionNoComma { Number | String | TemplateString | VariableName | boolean | kw<"this"> | kw<"null"> | kw<"super"> | RegExp | ArrayExpression | ObjectExpression { "{" commaSep ~destructure "}" } | NewTarget { kw<"new"> "." PropertyName } | NewExpression { kw<"new"> expressionNoComma (!newArgs ArgList)? } | UnaryExpression | YieldExpression | AwaitExpression | ParenthesizedExpression | ClassExpression | FunctionExpression | ArrowFunction | MemberExpression | BinaryExpression | ConditionalExpression { expressionNoComma !ternary questionOp expressionNoComma LogicOp<":"> expressionNoComma } | AssignmentExpression | PostfixExpression { expressionNoComma !postfix (incdec | LogicOp<"!">) } | CallExpression { expressionNoComma !call questionDot? ArgList } | InstantiationExpression { (VariableName | MemberExpression) !instantiate TypeArgList } | TaggedTemplateExpression { expressionNoComma !taggedTemplate TemplateString } | DynamicImport { kw<"import"> "(" expressionNoComma ")" } | ImportMeta { kw<"import"> "." PropertyName } | JSXElement | PrefixCast { tsAngleOpen type ~tsAngle ">" expressionNoComma } | ArrowFunction[@dynamicPrecedence=1] { TypeParamList { tsAngleOpen commaSep ">" } ParamList TypeAnnotation? "=>" (Block | expressionNoComma) } } ParenthesizedExpression { "(" expression ")" } ArrayExpression { "[" commaSep1<"..."? expressionNoComma | ""> ~destructure "]" } propName { PropertyDefinition | "[" expression "]" | Number | String } Property { pkwMod<"async">? (pkwMod<"get"> | pkwMod<"set"> | Star)? propName functionSignature Block | propName ~destructure (":" expressionNoComma)? | "..." expressionNoComma } PatternProperty { "..." patternAssign | ((PropertyName | Number | String) ~destructure (":" pattern)? | ("[" expression "]" ~destructure ":" pattern)) ("=" expressionNoComma)? } ClassExpression { kw<"class"> VariableDefinition? (kw<"extends"> expression)? ClassBody } functionSignature { TypeParamList? ParamList (TypeAnnotation | TypePredicate)? } FunctionExpression { async? kw<"function"> Star? VariableDefinition? functionSignature Block } YieldExpression[@dynamicPrecedence=1] { !prefix ckw<"yield"> Star? expressionNoComma } AwaitExpression[@dynamicPrecedence=1] { !prefix ckw<"await"> expressionNoComma } UnaryExpression { !prefix (kw<"void"> | kw<"typeof"> | kw<"delete"> | LogicOp<"!"> | BitOp<"~"> | incdec | incdecPrefix | ArithOp<"+" | "-">) expressionNoComma } BinaryExpression { expressionNoComma !exp ArithOp<"**"> expressionNoComma | expressionNoComma !times (divide | ArithOp<"%"> | ArithOp<"*">) expressionNoComma | expressionNoComma !plus ArithOp<"+" | "-"> expressionNoComma | expressionNoComma !shift BitOp<">>" ">"? | "<<"> expressionNoComma | expressionNoComma !rel (LessThan | CompareOp<"<=" | ">" "="?> | kw<"instanceof">) expressionNoComma | expressionNoComma !satisfies tskw<"satisfies"> type | (expressionNoComma | PrivatePropertyName) !rel ~tsIn kw<"in"> expressionNoComma | expressionNoComma !rel ckw<"as"> (kw<"const"> | type) | expressionNoComma !equal CompareOp<"==" "="? | "!=" "="?> expressionNoComma | expressionNoComma !bitOr BitOp { "|" } expressionNoComma | expressionNoComma !bitXor BitOp<"^"> expressionNoComma | expressionNoComma !bitAnd BitOp { "&" } expressionNoComma | expressionNoComma !and LogicOp<"&&"> expressionNoComma | expressionNoComma !or LogicOp<"||" | "??"> expressionNoComma } AssignmentExpression { (VariableName | MemberExpression) !assign UpdateOp<($[+\-/%^] | "*" "*"? | "|" "|"? | "&" "&"? | "<<" | ">>" ">"? | "??") "="> expressionNoComma | (VariableName | MemberExpression | ArrayPattern | ObjectPattern) !assign "=" expressionNoComma } MemberExpression { expressionNoComma !member (("." | questionDot) (PropertyName | PrivatePropertyName) | questionDot? "[" expression "]") } ArgList { "(" commaSep<"..."? expressionNoComma> ")" } ArrowFunction { async? (ParamList { VariableDefinition } | ParamList TypeAnnotation?) "=>" (Block | expressionNoComma) } TypeArgList[@dynamicPrecedence=1] { @extend[@dialect=ts,@name="<"] commaSep ">" } TypeParamList { "<" commaSep ">" } typeParam { TypeDefinition ~tsAngle (kw<"extends"> type)? ("=" type)? } typeofExpression { MemberExpression { typeofExpression !member (("." | questionDot) PropertyName | "[" expression "]") } | InstantiationExpression { typeofExpression !instantiate TypeArgList } | VariableName } type[@isGroup=Type] { ThisType { kw<"this"> } | LiteralType { ArithOp<"+" | "-">? Number | boolean | String } | TemplateType | NullType { kw<"null"> } | VoidType { kw<"void"> } | TypeofType { kw<"typeof"> typeofExpression } | KeyofType { !typePrefix tskw<"keyof"> type } | UniqueType { !typePrefix tskw<"unique"> type } | ImportType { kw<"import"> "(" String ")" } | InferredType { tskw<"infer"> TypeName } | ParenthesizedType { "(" type ")" } | FunctionSignature { TypeParamList? ParamTypeList "=>" type } | NewSignature { kw<"new"> ParamTypeList "=>" type } | IndexedType | TupleType { "[" commaSep<(Label ":")? type | "..." type> ~destructure "]" } | ArrayType { type "[" "]" | type "[" "]" } | ReadonlyType { tskw<"readonly"> !readonly type } | ObjectType | UnionType { type (!union unionOp type)+ | unionOp type (!unionPrefixed unionOp type)* } | IntersectionType { type (!intersection intersectionOp type)+ | intersectionOp type (!intersectionPrefixed intersectionOp type)* } | ConditionalType { type !typeExtends kw<"extends"> type questionOp ~arrow type LogicOp<":"> type } | ParameterizedType { (TypeName | IndexedType) !typeargs TypeArgList } | TypeName } IndexedType { type !typeMember ("." TypeName | "[" type "]")+ } ObjectType { "{" ( (MethodType | PropertyType | IndexSignature | CallSignature { ParamTypeList (TypeAnnotation | TypePredicate) } | NewSignature[@dynamicPrecedence=1] { @extend[@name=new] ParamTypeList TypeAnnotation }) ("," | semi) )* ~destructure "}" } IndexSignature { tsPkwMod<"readonly">? "[" PropertyDefinition { identifier } (TypeAnnotation | ~tsIn kw<"in"> type) "]" TypeAnnotation } MethodType { pkwMod<"async">? (pkwMod<"get"> | pkwMod<"set"> | Star)? PropertyDefinition functionSignature } PropertyType { (ArithOp<"+" | "-">? tsPkwMod<"readonly">)? PropertyDefinition (ArithOp<"+" | "-">? Optional)? TypeAnnotation } ParamTypeList[@name=ParamList] { "(" commaSep<"..."? pattern ~arrow Optional? ~arrow TypeAnnotation?> ")" } @skip {} { TemplateString[isolate] { templateStart (templateEscape | templateContent | templateExpr)* templateEnd } TemplateType[isolate] { templateStart (templateContent | templateType)* templateEnd } String[isolate] { '"' (stringContentDouble | Escape)* ('"' | "\n") | "'" (stringContentSingle | Escape)* ("'" | "\n") } BlockComment[isolate] { "/*" (blockCommentContent | blockCommentNewline)* blockCommentEnd } } templateExpr[@name=Interpolation,isolate] { InterpolationStart expression InterpolationEnd } templateType[@name=Interpolation,isolate] { InterpolationStart type InterpolationEnd } @skip {} { JSXElement { JSXSelfClosingTag | (JSXOpenTag | JSXFragmentTag) (JSXText | JSXElement | JSXEscape)* JSXCloseTag } } JSXSelfClosingTag { JSXStartTag jsxElementName jsxAttribute* JSXSelfCloseEndTag } JSXOpenTag { JSXStartTag jsxElementName jsxAttribute* JSXEndTag } JSXFragmentTag { JSXStartTag JSXEndTag } JSXCloseTag { JSXStartCloseTag jsxElementName? JSXEndTag } jsxElementName { JSXIdentifier | JSXBuiltin { JSXLowerIdentifier } | JSXNamespacedName | JSXMemberExpression } JSXMemberExpression { (JSXMemberExpression | JSXIdentifier | JSXLowerIdentifier) "." (JSXIdentifier | JSXLowerIdentifier) } JSXNamespacedName { (JSXIdentifier | JSXNamespacedName | JSXLowerIdentifier) ":" (JSXIdentifier | JSXLowerIdentifier) } jsxAttribute { JSXSpreadAttribute { "{" "..." expression "}" } | JSXAttribute { (JSXIdentifier | JSXNamespacedName | JSXLowerIdentifier) ("=" jsxAttributeValue)? } } jsxAttributeValue { JSXAttributeValue | JSXEscape { "{" expression "}" } | JSXElement } JSXEscape { "{" "..."? expression "}" } commaSep { "" | content ("," content?)* } commaSep1 { content ("," content)* } // Keywords kw { @specialize[@name={term}] } // Contextual keywords ckw { @extend[@name={term}] } tskw { @extend[@name={term},@dialect=ts] } async { @extend[@name=async] } // Contextual keyword in property context pkwMod { @extend[@name={term}] } tsPkwMod { @extend[@name={term},@dialect=ts] } semi { ";" | insertSemi } boolean { @specialize[@name=BooleanLiteral] } Star { "*" } VariableName { identifier ~arrow } VariableDefinition { identifier ~arrow } TypeDefinition { identifier } TypeName { identifier ~arrow } Label { identifier } PropertyName { word ~propName } PropertyDefinition { word ~propName } PrivatePropertyName { privateIdentifier } PrivatePropertyDefinition { privateIdentifier } Optional { "?" } questionOp[@name=LogicOp] { "?" } unionOp[@name=LogicOp] { "|" } intersectionOp[@name=LogicOp] { "&" } @skip { spaces | newline | LineComment | BlockComment } @context trackNewline from "./tokens.js" @external tokens noSemicolon from "./tokens" { noSemi } @external tokens operatorToken from "./tokens" { incdec[@name=ArithOp], incdecPrefix[@name=ArithOp] questionDot[@name="?."] } @external tokens jsx from "./tokens" { JSXStartTag } @local tokens { InterpolationStart[closedBy=InterpolationEnd] { "${" } templateEnd { "`" } templateEscape[@name=Escape] { Escape } @else templateContent } @local tokens { blockCommentEnd { "*/" } blockCommentNewline { "\n" } @else blockCommentContent } @tokens { spaces[@export] { $[\u0009 \u000b\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff]+ } newline[@export] { $[\r\n\u2028\u2029] } LineComment[isolate] { "//" ![\n]* } Hashbang { "#!" ![\n]* } divide[@name=ArithOp] { "/" } @precedence { "/*", LineComment, divide } @precedence { "/*", LineComment, RegExp } identifierChar { @asciiLetter | $[_$\u{a1}-\u{10ffff}] } word { identifierChar (identifierChar | @digit)* } identifier { word } privateIdentifier { "#" word } @precedence { spaces, newline, identifier } @precedence { spaces, newline, JSXIdentifier, JSXLowerIdentifier } @precedence { spaces, newline, word } hex { @digit | $[a-fA-F] } Number { (@digit ("_" | @digit)* ("." ("_" | @digit)*)? | "." @digit ("_" | @digit)*) (("e" | "E") ("+" | "-")? ("_" | @digit)+)? | @digit ("_" | @digit)* "n" | "0x" (hex | "_")+ "n"? | "0b" $[01_]+ "n"? | "0o" $[0-7_]+ "n"? } @precedence { Number "." } Escape { "\\" ("x" hex hex | "u" ("{" hex+ "}" | hex hex hex hex) | ![xu]) } stringContentSingle { ![\\\n']+ } stringContentDouble { ![\\\n"]+ } templateStart { "`" } InterpolationEnd[openedBy=InterpolationStart] { "}" } ArithOp { expr } LogicOp { expr } BitOp { expr } CompareOp { expr } UpdateOp { expr } @precedence { "*", ArithOp } RegExp[isolate] { "/" (![/\\\n[] | "\\" ![\n] | "[" (![\n\\\]] | "\\" ![\n])* "]")+ ("/" $[dgimsuvy]*)? } LessThan[@name=CompareOp] { "<" } "="[@name=Equals] "..."[@name=Spread] "=>"[@name=Arrow] "(" ")" "[" "]" "{" "}" "<" ">" "." "," ";" ":" "@" JSXIdentifier { $[A-Z_$\u{a1}-\u{10ffff}] (identifierChar | @digit | "-")* } JSXLowerIdentifier[@name=JSXIdentifier] { $[a-z] (identifierChar | @digit | "-")* } JSXAttributeValue { '"' !["]* '"' | "'" ![']* "'" } JSXStartCloseTag { "" } JSXSelfCloseEndTag { "/>" } JSXText { ![<{]+ } tsAngleOpen[@dialect=ts] { "<" } } @external tokens insertSemicolon from "./tokens" { insertSemi } @external propSource jsHighlight from "./highlight" @detectDelim javascript-1.4.16/src/tokens.js000066400000000000000000000057421461533631300164160ustar00rootroot00000000000000/* Hand-written tokenizers for JavaScript tokens that can't be expressed by lezer's built-in tokenizer. */ import {ExternalTokenizer, ContextTracker} from "@lezer/lr" import {insertSemi, noSemi, incdec, incdecPrefix, questionDot, spaces, newline, BlockComment, LineComment, JSXStartTag, Dialect_jsx} from "./parser.terms.js" const space = [9, 10, 11, 12, 13, 32, 133, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288] const braceR = 125, semicolon = 59, slash = 47, star = 42, plus = 43, minus = 45, lt = 60, comma = 44, question = 63, dot = 46 export const trackNewline = new ContextTracker({ start: false, shift(context, term) { return term == LineComment || term == BlockComment || term == spaces ? context : term == newline }, strict: false }) export const insertSemicolon = new ExternalTokenizer((input, stack) => { let {next} = input if (next == braceR || next == -1 || stack.context) input.acceptToken(insertSemi) }, {contextual: true, fallback: true}) export const noSemicolon = new ExternalTokenizer((input, stack) => { let {next} = input, after if (space.indexOf(next) > -1) return if (next == slash && ((after = input.peek(1)) == slash || after == star)) return if (next != braceR && next != semicolon && next != -1 && !stack.context) input.acceptToken(noSemi) }, {contextual: true}) export const operatorToken = new ExternalTokenizer((input, stack) => { let {next} = input if (next == plus || next == minus) { input.advance() if (next == input.next) { input.advance() let mayPostfix = !stack.context && stack.canShift(incdec) input.acceptToken(mayPostfix ? incdec : incdecPrefix) } } else if (next == question && input.peek(1) == dot) { input.advance(); input.advance() if (input.next < 48 || input.next > 57) // No digit after input.acceptToken(questionDot) } }, {contextual: true}) function identifierChar(ch, start) { return ch >= 65 && ch <= 90 || ch >= 97 && ch <= 122 || ch == 95 || ch >= 192 || !start && ch >= 48 && ch <= 57 } export const jsx = new ExternalTokenizer((input, stack) => { if (input.next != lt || !stack.dialectEnabled(Dialect_jsx)) return input.advance() if (input.next == slash) return // Scan for an identifier followed by a comma or 'extends', don't // treat this as a start tag if present. let back = 0 while (space.indexOf(input.next) > -1) { input.advance(); back++ } if (identifierChar(input.next, true)) { input.advance() back++ while (identifierChar(input.next, false)) { input.advance(); back++ } while (space.indexOf(input.next) > -1) { input.advance(); back++ } if (input.next == comma) return for (let i = 0;; i++) { if (i == 7) { if (!identifierChar(input.next, true)) return break } if (input.next != "extends".charCodeAt(i)) break input.advance() back++ } } input.acceptToken(JSXStartTag, -back) }) javascript-1.4.16/test/000077500000000000000000000000001461533631300147355ustar00rootroot00000000000000javascript-1.4.16/test/decorator.txt000066400000000000000000000031021461533631300174540ustar00rootroot00000000000000# Decorators on classes and class fields @d1 class Foo { @d2 bar() {} @d3 get baz() { return 1 } @d4 quux = 1 } ==> Script(ClassDeclaration( Decorator(VariableName), class,VariableDefinition,ClassBody( MethodDeclaration(Decorator(VariableName),PropertyDefinition,ParamList,Block), MethodDeclaration(Decorator(VariableName),get,PropertyDefinition,ParamList,Block( ReturnStatement(return,Number))), PropertyDeclaration(Decorator(VariableName),PropertyDefinition,Equals,Number)))) # Multiple decorators @d1 @d2 class Y {} ==> Script(ClassDeclaration(Decorator(VariableName),Decorator(VariableName),class,VariableDefinition,ClassBody)) # Member decorators @one.two class X {} ==> Script(ClassDeclaration(Decorator(MemberExpression(VariableName,PropertyName)),class,VariableDefinition,ClassBody)) # Call decorators @d(2) @a.b() class Z {} ==> Script(ClassDeclaration( Decorator(CallExpression(VariableName,ArgList(Number))), Decorator(CallExpression(MemberExpression(VariableName,PropertyName),ArgList)), class,VariableDefinition,ClassBody)) # Parenthesized decorators @(a instanceof Array ? x : y)(2) class P {} ==> Script(ClassDeclaration( Decorator(CallExpression(ParenthesizedExpression( ConditionalExpression(BinaryExpression(VariableName,instanceof,VariableName),LogicOp,VariableName,LogicOp,VariableName)), ArgList(Number))), class,VariableDefinition,ClassBody)) # Parameter decorators function foo(@d bar) {} ==> Script(FunctionDeclaration(function,VariableDefinition,ParamList(Decorator(VariableName),VariableDefinition),Block)) javascript-1.4.16/test/expression.txt000066400000000000000000000446641461533631300177130ustar00rootroot00000000000000# Minimal 0 ==> Script(ExpressionStatement(Number)) # Strings "A string with \"double\" and 'single' quotes"; 'A string with "double" and \'single\' quotes'; '\\'; "\\"; 'A string with new \ line'; ==> Script(ExpressionStatement(String(Escape,Escape)), ExpressionStatement(String(Escape,Escape)), ExpressionStatement(String(Escape)), ExpressionStatement(String(Escape)), ExpressionStatement(String(Escape))) # Numbers 101; 3.14; 3.14e+1; 0x1ABCDEFabcdef; 0o7632157312; 0b1010101001; 1e+3; ==> Script( ExpressionStatement(Number), ExpressionStatement(Number), ExpressionStatement(Number), ExpressionStatement(Number), ExpressionStatement(Number), ExpressionStatement(Number), ExpressionStatement(Number)) # Identifiers theVar; theVar2; $_; é象𫝄; últimaVez; 県; ==> Script( ExpressionStatement(VariableName), ExpressionStatement(VariableName), ExpressionStatement(VariableName), ExpressionStatement(VariableName), ExpressionStatement(VariableName), ExpressionStatement(VariableName)) # RegExps /one\\/; /one/g; /one/i; /one/gim; /on\/e/gim; /on[^/]afe/gim; /[\]/]/; ==> Script( ExpressionStatement(RegExp), ExpressionStatement(RegExp), ExpressionStatement(RegExp), ExpressionStatement(RegExp), ExpressionStatement(RegExp), ExpressionStatement(RegExp), ExpressionStatement(RegExp)) # Arrays []; [ "item1" ]; [ "item1", ]; [ "item1", item2 ]; [ , item2 ]; [ item2 = 5 ]; [ a, ...b, c ]; ==> Script( ExpressionStatement(ArrayExpression), ExpressionStatement(ArrayExpression(String)), ExpressionStatement(ArrayExpression(String)), ExpressionStatement(ArrayExpression(String,VariableName)), ExpressionStatement(ArrayExpression(VariableName)), ExpressionStatement(ArrayExpression(AssignmentExpression(VariableName,Equals,Number))), ExpressionStatement(ArrayExpression(VariableName, Spread, VariableName, VariableName))) # Functions [ function() {}, function(arg1, ...arg2) { arg2; }, function stuff() {}, function trailing(a,) {}, function trailing(a,b,) {} ] ==> Script(ExpressionStatement(ArrayExpression( FunctionExpression(function,ParamList,Block), FunctionExpression(function,ParamList(VariableDefinition,Spread,VariableDefinition), Block(ExpressionStatement(VariableName))), FunctionExpression(function,VariableDefinition,ParamList,Block), FunctionExpression(function,VariableDefinition,ParamList(VariableDefinition), Block), FunctionExpression(function,VariableDefinition,ParamList(VariableDefinition,VariableDefinition),Block)))) # Arrow functions a => 1; () => 2; (d, e) => 3; (f, g,) => { return h; }; async () => 4; ==> Script( ExpressionStatement(ArrowFunction(ParamList(VariableDefinition),Arrow,Number)), ExpressionStatement(ArrowFunction(ParamList,Arrow,Number)), ExpressionStatement(ArrowFunction(ParamList(VariableDefinition,VariableDefinition),Arrow,Number)), ExpressionStatement(ArrowFunction(ParamList(VariableDefinition,VariableDefinition),Arrow,Block(ReturnStatement(return,VariableName)))), ExpressionStatement(ArrowFunction(async,ParamList,Arrow,Number))) # Arrow function followed by comma ({ a: () => 1, b: "x" }) ==> Script(ExpressionStatement(ParenthesizedExpression(ObjectExpression( Property(PropertyDefinition,ArrowFunction(ParamList,Arrow,Number)), Property(PropertyDefinition,String))))) # Long potential arrow function (assign = [to, from], from = assign[0], to = assign[1]); ==> Script(ExpressionStatement(ParenthesizedExpression(SequenceExpression( AssignmentExpression(VariableName,Equals,ArrayExpression(VariableName,VariableName)), AssignmentExpression(VariableName,Equals,MemberExpression(VariableName,Number)), AssignmentExpression(VariableName,Equals,MemberExpression(VariableName,Number)))))) # Ternary operator condition ? case1 : case2; x.y = some.condition ? 2**x : 1 - 2; ==> Script( ExpressionStatement(ConditionalExpression(VariableName,LogicOp,VariableName,LogicOp,VariableName)), ExpressionStatement(AssignmentExpression( MemberExpression(VariableName,PropertyName),Equals, ConditionalExpression( MemberExpression(VariableName,PropertyName),LogicOp, BinaryExpression(Number,ArithOp,VariableName),LogicOp, BinaryExpression(Number,ArithOp,Number))))) # Type operators typeof x; x instanceof String; ==> Script(ExpressionStatement(UnaryExpression(typeof,VariableName)), ExpressionStatement(BinaryExpression(VariableName,instanceof,VariableName))) # Delete delete thing['prop']; true ? delete thing.prop : null; ==> Script( ExpressionStatement(UnaryExpression(delete,MemberExpression(VariableName,String))), ExpressionStatement(ConditionalExpression(BooleanLiteral,LogicOp, UnaryExpression(delete,MemberExpression(VariableName,PropertyName)),LogicOp,null))) # Void a = void b(); ==> Script(ExpressionStatement(AssignmentExpression(VariableName,Equals,UnaryExpression(void,CallExpression(VariableName,ArgList))))) # Augmented assignment s |= 1; t %= 2; w ^= 3; x += 4; y.z *= 5; z += 1; a >>= 1; b >>>= 1; c <<= 1; ==> Script( ExpressionStatement(AssignmentExpression(VariableName,UpdateOp,Number)), ExpressionStatement(AssignmentExpression(VariableName,UpdateOp,Number)), ExpressionStatement(AssignmentExpression(VariableName,UpdateOp,Number)), ExpressionStatement(AssignmentExpression(VariableName,UpdateOp,Number)), ExpressionStatement(AssignmentExpression(MemberExpression(VariableName,PropertyName),UpdateOp,Number)), ExpressionStatement(AssignmentExpression(VariableName,UpdateOp,Number)), ExpressionStatement(AssignmentExpression(VariableName,UpdateOp,Number)), ExpressionStatement(AssignmentExpression(VariableName,UpdateOp,Number)), ExpressionStatement(AssignmentExpression(VariableName,UpdateOp,Number))) # Operator precedence a <= b && c >= d; a.b = c ? d : e; a && b(c) && d; a && new b(c) && d; typeof a == b && c instanceof d; ==> Script( ExpressionStatement(BinaryExpression(BinaryExpression(VariableName,CompareOp,VariableName),LogicOp, BinaryExpression(VariableName,CompareOp,VariableName))), ExpressionStatement(AssignmentExpression(MemberExpression(VariableName,PropertyName),Equals, ConditionalExpression(VariableName,LogicOp,VariableName,LogicOp,VariableName))), ExpressionStatement(BinaryExpression(BinaryExpression(VariableName,LogicOp,CallExpression(VariableName,ArgList(VariableName))),LogicOp, VariableName)), ExpressionStatement(BinaryExpression(BinaryExpression(VariableName,LogicOp,NewExpression(new,VariableName,ArgList(VariableName))),LogicOp, VariableName)), ExpressionStatement(BinaryExpression(BinaryExpression(UnaryExpression(typeof,VariableName),CompareOp,VariableName),LogicOp, BinaryExpression(VariableName,instanceof,VariableName)))) # Rest args foo(...rest); ==> Script(ExpressionStatement(CallExpression(VariableName,ArgList(Spread,VariableName)))) # Forward slashes after parenthesized expressions (foo - bar) / baz; if (foo - bar) /baz/; (this.a() / this.b() - 1) / 2; ==> Script( ExpressionStatement(BinaryExpression(ParenthesizedExpression(BinaryExpression(VariableName,ArithOp,VariableName)),ArithOp,VariableName)), IfStatement(if,ParenthesizedExpression(BinaryExpression(VariableName,ArithOp,VariableName)), ExpressionStatement(RegExp)), ExpressionStatement(BinaryExpression(ParenthesizedExpression( BinaryExpression( BinaryExpression( CallExpression(MemberExpression(this,PropertyName),ArgList),ArithOp, CallExpression(MemberExpression(this,PropertyName),ArgList)),ArithOp,Number)),ArithOp,Number))) # Yield expressions yield db.users.where('[endpoint+email]'); yield* a; yield [22]; ==> Script( ExpressionStatement(YieldExpression(yield, CallExpression(MemberExpression(MemberExpression(VariableName,PropertyName),PropertyName),ArgList(String)))), ExpressionStatement(YieldExpression(yield,Star,VariableName)), ExpressionStatement(YieldExpression(yield,ArrayExpression(Number)))) # Template strings `one line`; `multi line`; `multi ${2 + 2} hello ${1, 2} line`; `$$$$`; `$`; `$$$$${ async }`; `\\\``; `one${`two${`three`}`}`; f`hi${there}`; ==> Script( ExpressionStatement(TemplateString), ExpressionStatement(TemplateString), ExpressionStatement(TemplateString( Interpolation(InterpolationStart,BinaryExpression(Number,ArithOp,Number),InterpolationEnd), Interpolation(InterpolationStart,SequenceExpression(Number,Number),InterpolationEnd))), ExpressionStatement(TemplateString), ExpressionStatement(TemplateString), ExpressionStatement(TemplateString(Interpolation(InterpolationStart,VariableName,InterpolationEnd))), ExpressionStatement(TemplateString(Escape,Escape)), ExpressionStatement(TemplateString(Interpolation(InterpolationStart,TemplateString( Interpolation(InterpolationStart,TemplateString,InterpolationEnd)),InterpolationEnd))), ExpressionStatement(TaggedTemplateExpression(VariableName,TemplateString( Interpolation(InterpolationStart,VariableName,InterpolationEnd))))) # Atoms this; null; undefined; true; false; ==> Script( ExpressionStatement(this), ExpressionStatement(null), ExpressionStatement(VariableName), ExpressionStatement(BooleanLiteral), ExpressionStatement(BooleanLiteral)) # Objects foo({}, { a: "b" }, { c: "d", "e": f, 1: 2 }, { g, [methodName]() {} }, {b, get}, {a,}); ==> Script(ExpressionStatement(CallExpression(VariableName,ArgList( ObjectExpression, ObjectExpression(Property(PropertyDefinition,String)), ObjectExpression(Property(PropertyDefinition,String),Property(String,VariableName),Property(Number,Number)), ObjectExpression(Property(PropertyDefinition),Property(VariableName,ParamList,Block)), ObjectExpression(Property(PropertyDefinition),Property(PropertyDefinition)), ObjectExpression(Property(PropertyDefinition)))))) # Method definitions ({ foo: true, add(a, b) { return a + b; }, get bar() { return c; }, set bar(a) { c = a; }, *barGenerator() { yield c; }, get() { return 1; } }); ==> Script(ExpressionStatement(ParenthesizedExpression(ObjectExpression( Property(PropertyDefinition,BooleanLiteral), Property(PropertyDefinition,ParamList(VariableDefinition,VariableDefinition), Block(ReturnStatement(return,BinaryExpression(VariableName,ArithOp,VariableName)))), Property(get,PropertyDefinition,ParamList,Block(ReturnStatement(return,VariableName))), Property(set,PropertyDefinition,ParamList(VariableDefinition), Block(ExpressionStatement(AssignmentExpression(VariableName,Equals,VariableName)))), Property(Star,PropertyDefinition,ParamList,Block(ExpressionStatement(YieldExpression(yield,VariableName)))), Property(PropertyDefinition,ParamList,Block(ReturnStatement(return,Number))))))) # Keyword property names ({ finally() {}, catch() {}, get: function () {}, set() {}, static: true, async: true, }); ==> Script(ExpressionStatement(ParenthesizedExpression(ObjectExpression( Property(PropertyDefinition,ParamList,Block), Property(PropertyDefinition,ParamList,Block), Property(PropertyDefinition,FunctionExpression(function,ParamList,Block)), Property(PropertyDefinition,ParamList,Block), Property(PropertyDefinition,BooleanLiteral), Property(PropertyDefinition,BooleanLiteral))))) # Generator functions [ function *() {}, function *generateStuff(arg1, arg2) { yield; yield arg2; } ]; ==> Script(ExpressionStatement(ArrayExpression( FunctionExpression(function,Star,ParamList,Block), FunctionExpression(function,Star,VariableDefinition,ParamList(VariableDefinition,VariableDefinition),Block( ExpressionStatement(VariableName), ExpressionStatement(YieldExpression(yield,VariableName))))))) # Member expressions x.someProperty; x?.other; x[someVariable]; f()["some-string"]; return returned.promise().done(a).fail(b); ==> Script( ExpressionStatement(MemberExpression(VariableName,PropertyName)), ExpressionStatement(MemberExpression(VariableName,PropertyName)), ExpressionStatement(MemberExpression(VariableName,VariableName)), ExpressionStatement(MemberExpression(CallExpression(VariableName,ArgList),String)), ReturnStatement(return,CallExpression(MemberExpression(CallExpression(MemberExpression(CallExpression( MemberExpression(VariableName,PropertyName),ArgList),PropertyName),ArgList(VariableName)),PropertyName),ArgList(VariableName)))) # Callback chain return this.map(function (a) { return a.b; }) // a comment .filter(function (c) { return 2; }); ==> Script(ReturnStatement(return,CallExpression(MemberExpression(CallExpression(MemberExpression(this,PropertyName), ArgList(FunctionExpression(function,ParamList(VariableDefinition),Block(ReturnStatement(return,MemberExpression(VariableName,PropertyName)))))), LineComment,PropertyName),ArgList(FunctionExpression(function,ParamList(VariableDefinition),Block(ReturnStatement(return,Number))))))) # Function calls x.someMethod(arg1, "arg2"); (function(x, y) { }(a, b)); f(new foo.bar(1), 2); ==> Script( ExpressionStatement(CallExpression(MemberExpression(VariableName,PropertyName),ArgList(VariableName,String))), ExpressionStatement(ParenthesizedExpression(CallExpression(FunctionExpression(function,ParamList(VariableDefinition,VariableDefinition),Block), ArgList(VariableName,VariableName)))), ExpressionStatement(CallExpression(VariableName,ArgList(NewExpression(new,MemberExpression(VariableName,PropertyName),ArgList(Number)),Number)))) # Constructor calls new foo(1); new module.Klass(1, "two"); new Thing; ==> Script( ExpressionStatement(NewExpression(new,VariableName,ArgList(Number))), ExpressionStatement(NewExpression(new,MemberExpression(VariableName,PropertyName),ArgList(Number,String))), ExpressionStatement(NewExpression(new,VariableName))) # Await Expressions await asyncFunction(); await asyncPromise; ==> Script( ExpressionStatement(AwaitExpression(await,CallExpression(VariableName,ArgList))), ExpressionStatement(AwaitExpression(await,VariableName))) # Numeric operators i++; i--; i + j * 3 - j % 5; 2 ** i * 3; 2 * i ** 3; +x; -x; ==> Script( ExpressionStatement(PostfixExpression(VariableName,ArithOp)), ExpressionStatement(PostfixExpression(VariableName,ArithOp)), ExpressionStatement(BinaryExpression(BinaryExpression(VariableName,ArithOp,BinaryExpression(VariableName,ArithOp,Number)),ArithOp,BinaryExpression(VariableName,ArithOp,Number))), ExpressionStatement(BinaryExpression(BinaryExpression(Number,ArithOp,VariableName),ArithOp,Number)), ExpressionStatement(BinaryExpression(Number,ArithOp,BinaryExpression(VariableName,ArithOp,Number))), ExpressionStatement(UnaryExpression(ArithOp,VariableName)), ExpressionStatement(UnaryExpression(ArithOp,VariableName))) # Boolean operators i || j; i && j; i ?? j; !a && !b || !c && !d; ==> Script( ExpressionStatement(BinaryExpression(VariableName,LogicOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,LogicOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,LogicOp,VariableName)), ExpressionStatement(BinaryExpression(BinaryExpression(UnaryExpression(LogicOp,VariableName),LogicOp, UnaryExpression(LogicOp,VariableName)),LogicOp,BinaryExpression(UnaryExpression(LogicOp,VariableName),LogicOp, UnaryExpression(LogicOp,VariableName))))) # Bitwise operators i >> j; i >>> j; i << j; i & j; i | j; ~i ^ ~j; ==> Script( ExpressionStatement(BinaryExpression(VariableName,BitOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,BitOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,BitOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,BitOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,BitOp,VariableName)), ExpressionStatement(BinaryExpression(UnaryExpression(BitOp,VariableName),BitOp,UnaryExpression(BitOp,VariableName)))) # Relational operators x < y; x <= y; x == y; x === y; x != y; x !== y; x > y; x >= y; ==> Script( ExpressionStatement(BinaryExpression(VariableName,CompareOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,CompareOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,CompareOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,CompareOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,CompareOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,CompareOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,CompareOp,VariableName)), ExpressionStatement(BinaryExpression(VariableName,CompareOp,VariableName))) # Word operators x in y; x instanceof y; !x instanceof y; ==> Script( ExpressionStatement(BinaryExpression(VariableName,in,VariableName)), ExpressionStatement(BinaryExpression(VariableName,instanceof,VariableName)), ExpressionStatement(BinaryExpression(UnaryExpression(LogicOp,VariableName),instanceof,VariableName))) # Assignments x = 0; x.y = 0; x["y"] = 0; async = 0; [a, b = 2] = foo; ({a, b, ...d} = c); ==> Script( ExpressionStatement(AssignmentExpression(VariableName,Equals,Number)), ExpressionStatement(AssignmentExpression(MemberExpression(VariableName,PropertyName),Equals,Number)), ExpressionStatement(AssignmentExpression(MemberExpression(VariableName,String),Equals,Number)), ExpressionStatement(AssignmentExpression(VariableName,Equals,Number)), ExpressionStatement(AssignmentExpression(ArrayPattern(VariableDefinition,VariableDefinition,Equals,Number),Equals,VariableName)), ExpressionStatement(ParenthesizedExpression(AssignmentExpression(ObjectPattern( PatternProperty(PropertyName),PatternProperty(PropertyName),PatternProperty(Spread,VariableDefinition)),Equals,VariableName)))) # Comma operator a = 1, b = 2; c = {d: (3, 4 + 5)}; ==> Script( ExpressionStatement(SequenceExpression(AssignmentExpression(VariableName,Equals,Number),AssignmentExpression(VariableName,Equals,Number))), ExpressionStatement(AssignmentExpression(VariableName,Equals,ObjectExpression( Property(PropertyDefinition,ParenthesizedExpression(SequenceExpression(Number,BinaryExpression(Number,ArithOp,Number)))))))) # Punctuation (foo(1, 2), bar); ==> Script(ExpressionStatement(ParenthesizedExpression( "(",SequenceExpression(CallExpression(VariableName,ArgList("(",Number,Number,")")),",",VariableName),")"))) # Doesn't choke on unfinished ternary operator 1?1 ==> Script(ExpressionStatement(ConditionalExpression(Number,LogicOp,Number,⚠))) # Can handle unterminated template literals `f ==> Script(ExpressionStatement(TemplateString(⚠))) # Ternary with leading-dot number a?.2:.3 ==> Script(ExpressionStatement(ConditionalExpression(VariableName,LogicOp,Number,LogicOp,Number))) javascript-1.4.16/test/jsx.txt000066400000000000000000000036621461533631300163110ustar00rootroot00000000000000# Self-closing element {"dialect": "jsx"} ==> Script(ExpressionStatement(JSXElement(JSXSelfClosingTag(JSXStartTag,JSXBuiltin(JSXIdentifier),JSXSelfCloseEndTag)))) # Regular element {"dialect": "jsx"} bar ==> Script(ExpressionStatement(JSXElement( JSXOpenTag(JSXStartTag, JSXIdentifier, JSXEndTag), JSXText, JSXCloseTag(JSXStartCloseTag, JSXIdentifier, JSXEndTag)))) # Fragment {"dialect": "jsx"} <>bar ==> Script(ExpressionStatement(JSXElement( JSXFragmentTag(JSXStartTag, JSXEndTag), JSXText, JSXCloseTag(JSXStartCloseTag, JSXEndTag)))) # Namespaced name {"dialect": "jsx"} ==> Script(ExpressionStatement(JSXElement( JSXSelfClosingTag(JSXStartTag,JSXNamespacedName(JSXIdentifier, JSXIdentifier),JSXSelfCloseEndTag)))) # Member name {"dialect": "jsx"} ==> Script(ExpressionStatement(JSXElement( JSXSelfClosingTag(JSXStartTag,JSXMemberExpression(JSXIdentifier, JSXIdentifier),JSXSelfCloseEndTag)))) # Nested tags {"dialect": "jsx"} text{x} {...y} ==> Script(ExpressionStatement(JSXElement( JSXOpenTag(JSXStartTag, JSXBuiltin(JSXIdentifier), JSXEndTag), JSXElement( JSXOpenTag(JSXStartTag, JSXMemberExpression(JSXIdentifier, JSXIdentifier), JSXEndTag), JSXText, JSXCloseTag(JSXStartCloseTag, JSXMemberExpression(JSXIdentifier, JSXIdentifier), JSXEndTag)), JSXEscape(VariableName), JSXText, JSXEscape(Spread, VariableName), JSXCloseTag(JSXStartCloseTag, JSXBuiltin(JSXIdentifier), JSXEndTag)))) # Attributes {"dialect": "jsx"} ==> Script(ExpressionStatement(JSXElement( JSXOpenTag(JSXStartTag, JSXIdentifier, JSXAttribute(JSXIdentifier, Equals, JSXAttributeValue), JSXAttribute(JSXIdentifier), JSXSpreadAttribute(Spread, VariableName), JSXAttribute(JSXIdentifier, Equals, JSXEscape(VariableName)), JSXEndTag), JSXCloseTag(JSXStartCloseTag, JSXIdentifier, JSXEndTag)))) javascript-1.4.16/test/semicolon.txt000066400000000000000000000023461461533631300174730ustar00rootroot00000000000000# No semicolons x if (a) { var b = c d } else e ==> Script( ExpressionStatement(VariableName), IfStatement(if,ParenthesizedExpression(VariableName),Block( VariableDeclaration(var,VariableDefinition,Equals,VariableName), ExpressionStatement(VariableName)), else,ExpressionStatement(VariableName))) # Continued expressions on new line x + 2 foo (bar) ==> Script( ExpressionStatement(BinaryExpression(VariableName,ArithOp,Number)), ExpressionStatement(CallExpression(VariableName,ArgList(VariableName)))) # Doesn't parse postfix ops on a new line x ++y ==> Script( ExpressionStatement(VariableName), ExpressionStatement(UnaryExpression(ArithOp,VariableName))) # Eagerly cut return/break/continue return 2 return 2 continue foo continue foo break bar break bar ==> Script( ReturnStatement(return,Number), ReturnStatement(return), ExpressionStatement(Number), ContinueStatement(continue,Label), ContinueStatement(continue), ExpressionStatement(VariableName), BreakStatement(break,Label), BreakStatement(break), ExpressionStatement(VariableName)) # Cut return regardless of whitespace { return } return // foo ; ==> Script(Block(ReturnStatement(return)),ReturnStatement(return,LineComment)) javascript-1.4.16/test/statement.txt000066400000000000000000000261601461533631300175070ustar00rootroot00000000000000# Variable declaration var a = b , c = d; const [x] = y = 3; ==> Script( VariableDeclaration(var,VariableDefinition,Equals,VariableName,VariableDefinition,Equals,VariableName), VariableDeclaration(const,ArrayPattern(VariableDefinition),Equals,AssignmentExpression(VariableName,Equals,Number))) # Function declaration function a(a, b) { return 3; } function b({b}, c = d, e = f) {} ==> Script( FunctionDeclaration(function,VariableDefinition,ParamList(VariableDefinition,VariableDefinition),Block(ReturnStatement(return,Number))), FunctionDeclaration(function,VariableDefinition,ParamList( ObjectPattern(PatternProperty(PropertyName)),VariableDefinition,Equals,VariableName,VariableDefinition,Equals,VariableName),Block)) # Async functions async function foo() {} class Foo { async bar() {} } async (a) => { return foo; }; ==> Script( FunctionDeclaration(async,function,VariableDefinition,ParamList,Block), ClassDeclaration(class,VariableDefinition,ClassBody(MethodDeclaration(async,PropertyDefinition,ParamList,Block))), ExpressionStatement(ArrowFunction(async,ParamList(VariableDefinition),Arrow,Block(ReturnStatement(return,VariableName))))) # If statements if (x) log(y); if (a.b) { d; } if (a) { c; d; } else { e; } if (1) if (2) b; else c; ==> Script( IfStatement(if,ParenthesizedExpression(VariableName),ExpressionStatement(CallExpression(VariableName,ArgList(VariableName)))), IfStatement(if,ParenthesizedExpression(MemberExpression(VariableName,PropertyName)),Block(ExpressionStatement(VariableName))), IfStatement(if,ParenthesizedExpression(VariableName),Block(ExpressionStatement(VariableName),ExpressionStatement(VariableName)), else,Block(ExpressionStatement(VariableName))), IfStatement(if,ParenthesizedExpression(Number),IfStatement(if,ParenthesizedExpression(Number),ExpressionStatement(VariableName), else,ExpressionStatement(VariableName)))) # While loop while (1) debugger; while (2) { a; b; } ==> Script( WhileStatement(while,ParenthesizedExpression(Number),DebuggerStatement(debugger)), WhileStatement(while,ParenthesizedExpression(Number),Block(ExpressionStatement(VariableName),ExpressionStatement(VariableName)))) # Labels foo: 1; foo: while(2) break foo; ==> Script( LabeledStatement(Label,ExpressionStatement(Number)), LabeledStatement(Label,WhileStatement(while,ParenthesizedExpression(Number),BreakStatement(break,Label)))) # Try try { throw new Error; } catch {} try { 1; } catch (x) { 2; } finally { 3; } ==> Script( TryStatement(try,Block(ThrowStatement(throw,NewExpression(new,VariableName))),CatchClause(catch,Block)), TryStatement(try,Block(ExpressionStatement(Number)), CatchClause(catch,VariableDefinition,Block(ExpressionStatement(Number))), FinallyClause(finally,Block(ExpressionStatement(Number))))) # Switch switch (x) { case 1: return true; case 2: case 50 * 3: console.log("ok"); default: return false; } ==> Script(SwitchStatement(switch,ParenthesizedExpression(VariableName),SwitchBody( CaseLabel(case,Number), ReturnStatement(return,BooleanLiteral), CaseLabel(case,Number), CaseLabel(case,BinaryExpression(Number,ArithOp,Number)), ExpressionStatement(CallExpression(MemberExpression(VariableName,PropertyName),ArgList(String))), DefaultLabel(default), ReturnStatement(return,BooleanLiteral)))) # For for (let x = 1; x < 10; x++) {} for (const y of z) {} for (var m in n) {} for (q in r) {} for (var a, b; c; d) continue; for (i = 0, init(); i < 10; i++) {} for (;;) {} for (const {thing} in things) thing; for await (let x of stream) {} ==> Script( ForStatement(for,ForSpec(VariableDeclaration(let,VariableDefinition,Equals,Number), BinaryExpression(VariableName,CompareOp,Number),PostfixExpression(VariableName,ArithOp)),Block), ForStatement(for,ForOfSpec(const,VariableDefinition,of,VariableName),Block), ForStatement(for,ForInSpec(var,VariableDefinition,in,VariableName),Block), ForStatement(for,ForInSpec(VariableName,in,VariableName),Block), ForStatement(for,ForSpec(VariableDeclaration(var,VariableDefinition,VariableDefinition),VariableName,VariableName),ContinueStatement(continue)), ForStatement(for,ForSpec(SequenceExpression(AssignmentExpression(VariableName,Equals,Number), CallExpression(VariableName,ArgList)),BinaryExpression(VariableName,CompareOp,Number),PostfixExpression(VariableName,ArithOp)),Block), ForStatement(for,ForSpec,Block), ForStatement(for,ForInSpec(const,ObjectPattern(PatternProperty(PropertyName)),in,VariableName),ExpressionStatement(VariableName)), ForStatement(for,await,ForOfSpec(let,VariableDefinition,of,VariableName),Block)) # Labeled statements theLoop: for (;;) { if (a) { break theLoop; } } ==> Script(LabeledStatement(Label,ForStatement(for,ForSpec,Block( IfStatement(if,ParenthesizedExpression(VariableName),Block(BreakStatement(break,Label))))))) # Classes class Foo { static one(a) { return a; }; two(b) { return b; } finally() {} } class Foo extends require('another-class') { constructor() { super(); } bar() { super.a(); } prop; etc = 20; static { f() } } ==> Script( ClassDeclaration(class,VariableDefinition,ClassBody( MethodDeclaration(static,PropertyDefinition,ParamList(VariableDefinition),Block(ReturnStatement(return,VariableName))), MethodDeclaration(PropertyDefinition,ParamList(VariableDefinition),Block(ReturnStatement(return,VariableName))), MethodDeclaration(PropertyDefinition,ParamList,Block))), ClassDeclaration(class,VariableDefinition,extends,CallExpression(VariableName,ArgList(String)),ClassBody( MethodDeclaration(PropertyDefinition,ParamList,Block(ExpressionStatement(CallExpression(super,ArgList)))), MethodDeclaration(PropertyDefinition,ParamList,Block(ExpressionStatement(CallExpression(MemberExpression(super,PropertyName),ArgList)))), PropertyDeclaration(PropertyDefinition), PropertyDeclaration(PropertyDefinition,Equals,Number), StaticBlock(static, Block(ExpressionStatement(CallExpression(VariableName,ArgList))))))) # Private properties class Foo { #bar() { this.#a() + this?.#prop == #prop in this; } #prop; #etc = 20; } ==> Script(ClassDeclaration(class,VariableDefinition,ClassBody( MethodDeclaration(PrivatePropertyDefinition,ParamList,Block( ExpressionStatement(BinaryExpression( BinaryExpression( CallExpression(MemberExpression(this,PrivatePropertyName),ArgList), ArithOp, MemberExpression(this,PrivatePropertyName)), CompareOp, BinaryExpression(PrivatePropertyName, in, this))))), PropertyDeclaration(PrivatePropertyDefinition), PropertyDeclaration(PrivatePropertyDefinition,Equals,Number)))) # Computed properties class Foo { [x] = 44; [Symbol.iterator]() {} } ==> Script(ClassDeclaration(class,VariableDefinition,ClassBody( PropertyDeclaration(VariableName,Equals,Number), MethodDeclaration(MemberExpression(VariableName,PropertyName),ParamList,Block)))) # Imports import defaultMember from "module-name"; import * as name from "module-name"; import { member } from "module-name"; import { member1, member2 as alias2 } from "module-name"; import defaultMember, { member1, member2 as alias2, } from "module-name"; import "module-name"; ==> Script( ImportDeclaration(import,VariableDefinition,from,String), ImportDeclaration(import,Star,as,VariableDefinition,from,String), ImportDeclaration(import,ImportGroup(VariableDefinition),from,String), ImportDeclaration(import,ImportGroup(VariableDefinition,VariableName,as,VariableDefinition),from,String), ImportDeclaration(import,VariableDefinition,ImportGroup(VariableDefinition,VariableName,as,VariableDefinition),from,String), ImportDeclaration(import,String)) # Exports export { name1, name2, name3 as x, nameN }; export let a, b = 2; export default 2 + 2; export default function() { } export default async function name1() { } export { name1 as default, } from "foo"; export * from 'foo'; ==> Script( ExportDeclaration(export,ExportGroup(VariableName,VariableName,VariableName,as,VariableName,VariableName)), ExportDeclaration(export,VariableDeclaration(let,VariableDefinition,VariableDefinition,Equals,Number)), ExportDeclaration(export,default,BinaryExpression(Number,ArithOp,Number)), ExportDeclaration(export,default,FunctionDeclaration(function,ParamList,Block)), ExportDeclaration(export,default,FunctionDeclaration(async,function,VariableDefinition,ParamList,Block)), ExportDeclaration(export,ExportGroup(VariableName,as,VariableName),from,String), ExportDeclaration(export,Star,from,String)) # Empty statements if (true) { ; };;; ==> Script(IfStatement(if,ParenthesizedExpression(BooleanLiteral),Block)) # Comments /* a */ one; /* b **/ two; /* c ***/ three; /* d ***/ four; y // comment * z; ==> Script( BlockComment, ExpressionStatement(VariableName), BlockComment, ExpressionStatement(VariableName), BlockComment, ExpressionStatement(VariableName), BlockComment, ExpressionStatement(VariableName), ExpressionStatement(BinaryExpression(VariableName,LineComment,ArithOp,VariableName))) # Recover from invalid char const {foobar} = {}; ==> Script(VariableDeclaration( const, ObjectPattern("{",PatternProperty(PropertyName,⚠),"}"), Equals, ObjectExpression)) # Sync back to statement function f() { log(a b --c) } function g() {} ==> Script( FunctionDeclaration(function,VariableDefinition,ParamList,Block(ExpressionStatement(CallExpression(VariableName,ArgList(...))))), FunctionDeclaration(function,VariableDefinition,ParamList,Block)) # Destructuring ({x} = y); [u, v] = w; let [a,, b = 0] = c; let {x, y: z = 1} = d; let {[f]: m} = e; ==> Script( ExpressionStatement(ParenthesizedExpression(AssignmentExpression( ObjectPattern(PatternProperty(PropertyName)),Equals,VariableName))), ExpressionStatement(AssignmentExpression(ArrayPattern(VariableDefinition,VariableDefinition),Equals,VariableName)), VariableDeclaration(let,ArrayPattern(VariableDefinition,VariableDefinition,Equals,Number),Equals,VariableName), VariableDeclaration(let,ObjectPattern( PatternProperty(PropertyName), PatternProperty(PropertyName,VariableDefinition,Equals,Number) ),Equals,VariableName), VariableDeclaration(let,ObjectPattern(PatternProperty(VariableName,VariableDefinition)),Equals,VariableName)) # Generators function* foo() { yield 1 } class B { *method() {} } ({*x() {}}) ==> Script( FunctionDeclaration(function,Star,VariableDefinition,ParamList,Block( ExpressionStatement(YieldExpression(yield,Number)))), ClassDeclaration(class,VariableDefinition,ClassBody( MethodDeclaration(Star,PropertyDefinition,ParamList,Block))), ExpressionStatement(ParenthesizedExpression(ObjectExpression(Property(Star,PropertyDefinition,ParamList,Block))))) # Hashbang #!/bin/env node foo() ==> Script(Hashbang,ExpressionStatement(CallExpression(VariableName,ArgList))) # new.target function MyObj() { if (!new.target) { throw new Error('Must construct MyObj with new'); } } ==> Script( FunctionDeclaration(function,VariableDefinition,ParamList,Block( IfStatement(if,ParenthesizedExpression(UnaryExpression(LogicOp,NewTarget(new,PropertyName))), Block( ThrowStatement(throw,NewExpression(new,VariableName,ArgList(String)))))))) javascript-1.4.16/test/test-javascript.js000066400000000000000000000010131461533631300204110ustar00rootroot00000000000000import {parser} from "../dist/index.js" import {fileTests} from "@lezer/generator/dist/test" import * as fs from "fs" import * as path from "path" import {fileURLToPath} from "url" let caseDir = path.dirname(fileURLToPath(import.meta.url)) for (let file of fs.readdirSync(caseDir)) { if (!/\.txt$/.test(file)) continue let name = /^[^\.]*/.exec(file)[0] describe(name, () => { for (let {name, run} of fileTests(fs.readFileSync(path.join(caseDir, file), "utf8"), file)) it(name, () => run(parser)) }) } javascript-1.4.16/test/typescript.txt000066400000000000000000000253621461533631300177140ustar00rootroot00000000000000# Undefined and Null Type {"dialect": "ts"} let x: undefined let y: null ==> Script( VariableDeclaration(let,VariableDefinition,TypeAnnotation( TypeName)), VariableDeclaration(let,VariableDefinition,TypeAnnotation( NullType(null)))) # Type declaration {"dialect": "ts"} function foo(a: number, b: "literal" | Map): RegExp[] {} ==> Script(FunctionDeclaration(function, VariableDefinition, ParamList( VariableDefinition, TypeAnnotation(TypeName), VariableDefinition, TypeAnnotation(UnionType(LiteralType(String), LogicOp, ParameterizedType(TypeName, TypeArgList(TypeName, TypeName)))) ), TypeAnnotation(ArrayType(TypeName)), Block)) # Type predicate {"dialect": "ts"} function isFoo(foo: any): foo is Foo { return true } ==> Script(FunctionDeclaration(function, VariableDefinition, ParamList( VariableDefinition, TypeAnnotation(TypeName) ), TypePredicate(VariableName, is, TypeName), Block(ReturnStatement(return, BooleanLiteral)))) # Type alias {"dialect": "ts"} type Foo = T[] ==> Script(TypeAliasDeclaration(type, TypeDefinition, TypeParamList(TypeDefinition, extends, TypeName), Equals, ArrayType(TypeName))) # Enum declaration {"dialect": "ts"} const enum Type { Red = 1, Blue, Green } ==> Script(EnumDeclaration(const, enum, TypeDefinition, EnumBody(PropertyName, Equals, Number, PropertyName, PropertyName))) # Interface declaration {"dialect": "ts"} interface Foo { readonly a: number b(arg: string): void (call: number): boolean new (): Foo readonly [x: string]: number } ==> Script(InterfaceDeclaration(interface, TypeDefinition, ObjectType( PropertyType(readonly, PropertyDefinition, TypeAnnotation(TypeName)), MethodType(PropertyDefinition, ParamList(VariableDefinition, TypeAnnotation(TypeName)), TypeAnnotation(VoidType(void))), CallSignature(ParamList(VariableDefinition, TypeAnnotation(TypeName)), TypeAnnotation(TypeName)), NewSignature(new,ParamList, TypeAnnotation(TypeName)), IndexSignature(readonly, PropertyDefinition, TypeAnnotation(TypeName), TypeAnnotation(TypeName))))) # Call type args {"dialect": "ts"} foo() + new Bar<11>() x < 10 > 5 ==> Script( ExpressionStatement(BinaryExpression( CallExpression(InstantiationExpression(VariableName, TypeArgList(TypeName, TypeName)), ArgList), ArithOp, NewExpression(new, InstantiationExpression(VariableName, TypeArgList(LiteralType(Number))), ArgList))), ExpressionStatement(BinaryExpression(BinaryExpression(VariableName, CompareOp, Number), CompareOp, Number))) # Advanced types {"dialect": "ts"} let x: typeof X.x | keyof Y & Z["Foo"] | A let tuple: [a, b] let f: (x: number) => boolean ==> Script( VariableDeclaration(let, VariableDefinition, TypeAnnotation( UnionType(TypeofType(typeof, MemberExpression(VariableName, PropertyName)), LogicOp, IntersectionType(KeyofType(keyof, TypeName), LogicOp, IndexedType(TypeName, LiteralType(String))), LogicOp, ParameterizedType(TypeName, TypeArgList(TypeName))))), VariableDeclaration(let, VariableDefinition, TypeAnnotation(TupleType(TypeName, TypeName))), VariableDeclaration(let, VariableDefinition, TypeAnnotation(FunctionSignature( ParamList(VariableDefinition, TypeAnnotation(TypeName)), Arrow, TypeName)))) # Prefix union/intersection let x: | A | B | C let y: & RegExp & (& Date) ==> Script( VariableDeclaration(let,VariableDefinition,TypeAnnotation( UnionType(LogicOp,TypeName,LogicOp,TypeName,LogicOp,TypeName))), VariableDeclaration(let,VariableDefinition,TypeAnnotation( IntersectionType(LogicOp,TypeName,LogicOp,ParenthesizedType(IntersectionType(LogicOp,TypeName)))))) # Prefix cast {"dialect": "ts"} foo ==> Script(ExpressionStatement(PrefixCast(TypeName, VariableName))) # No prefix cast in JSX {"dialect": "ts jsx"} foo ==> Script(ExpressionStatement(JSXElement( JSXOpenTag(JSXStartTag, JSXBuiltin(JSXIdentifier), JSXEndTag), JSXText, JSXCloseTag(JSXStartCloseTag, JSXBuiltin(JSXIdentifier), JSXEndTag)))) # Class definition {"dialect": "ts"} class Foo extends Bar implements Stuff { a: number public readonly b: string = "two" constructor(readonly x: boolean, public y: number, z: string) {} private static blah(): void {} } ==> Script(ClassDeclaration( class, VariableDefinition, TypeParamList(TypeDefinition), extends, VariableName, TypeArgList(TypeName), implements TypeName, ClassBody( PropertyDeclaration(PropertyDefinition, TypeAnnotation(TypeName)), PropertyDeclaration(Privacy, readonly, PropertyDefinition, TypeAnnotation(TypeName), Equals, String), MethodDeclaration(PropertyDefinition, ParamList( readonly, VariableDefinition, TypeAnnotation(TypeName), Privacy, VariableDefinition, TypeAnnotation(TypeName), VariableDefinition, TypeAnnotation(TypeName)), Block), MethodDeclaration(Privacy, static, PropertyDefinition, ParamList, TypeAnnotation(VoidType(void)), Block)))) # Arrow with type params {"dialect": "ts"} let x = (arg: T): T => arg ==> Script(VariableDeclaration(let, VariableDefinition, Equals, ArrowFunction( TypeParamList(TypeDefinition), ParamList(VariableDefinition, TypeAnnotation(TypeName)), TypeAnnotation(TypeName), Arrow, VariableName))) # Template types {"dialect": "ts"} type Tmpl = `${string} ${5}` | `one ${Two}` ==> Script(TypeAliasDeclaration(type, TypeDefinition, TypeParamList(TypeDefinition), Equals, UnionType(TemplateType(Interpolation(InterpolationStart,TypeName,InterpolationEnd), Interpolation(InterpolationStart,LiteralType(Number),InterpolationEnd)), LogicOp, TemplateType(Interpolation(InterpolationStart,TypeName,InterpolationEnd))))) # Extending complex types {"dialect": "ts"} class Foo extends A.B {} ==> Script(ClassDeclaration(class, VariableDefinition, extends, MemberExpression(VariableName, PropertyName), TypeArgList(TypeName), ClassBody)) # Object type {"dialect": "ts"} type A = {a: number, b: number} type B = {a: number; b: number;} ==> Script( TypeAliasDeclaration(type,TypeDefinition,Equals,ObjectType( PropertyType(PropertyDefinition,TypeAnnotation(TypeName)), PropertyType(PropertyDefinition,TypeAnnotation(TypeName)))), TypeAliasDeclaration(type,TypeDefinition,Equals,ObjectType( PropertyType(PropertyDefinition,TypeAnnotation(TypeName)), PropertyType(PropertyDefinition,TypeAnnotation(TypeName))))) # Conditional Type {"dialect": "ts"} type X = T extends E ? number : A ==> Script( TypeAliasDeclaration(type,TypeDefinition,TypeParamList(TypeDefinition),Equals, ConditionalType(TypeName,extends,TypeName,LogicOp,TypeName,LogicOp,TypeName))) # Generic Function Type {"dialect": "ts"} let f: () => T ==> Script( VariableDeclaration(let,VariableDefinition,TypeAnnotation( FunctionSignature(TypeParamList(TypeDefinition),ParamList,Arrow,TypeName)))) # Satisfies operator {"dialect": "ts"} let x = 1 satisfies number ==> Script(VariableDeclaration(let,VariableDefinition,Equals,BinaryExpression(Number,satisfies,TypeName))) # Override modifier on properties {"dialect": "ts"} class A { override accessor a; static override b = 1; override c = 2; } ==> Script(ClassDeclaration(class,VariableDefinition,ClassBody( PropertyDeclaration(override,accessor,PropertyDefinition), PropertyDeclaration(static,override,PropertyDefinition,Equals,Number), PropertyDeclaration(override,PropertyDefinition,Equals,Number)))) # Class extending expression {"dialect": "ts"} class X extends class {} {} ==> Script(ClassDeclaration(class,VariableDefinition,extends,ClassExpression(class,ClassBody),ClassBody)) # Declare syntax {"dialect": "ts"} declare namespace myLib { function makeGreeting(s: string): string; let numberOfGreetings: number; } declare function greet(setting: GreetingSettings): void; declare class Greeter { constructor(greeting: string); greeting: string; showGreeting(): void; } class X { declare foo(); declare bar: number; } ==> Script( AmbientDeclaration(declare,NamespaceDeclaration(namespace,VariableDefinition,Block( FunctionDeclaration(function,VariableDefinition,ParamList(VariableDefinition,TypeAnnotation(TypeName)), TypeAnnotation(TypeName)), VariableDeclaration(let,VariableDefinition,TypeAnnotation(TypeName))))), AmbientDeclaration(declare,AmbientFunctionDeclaration(function,VariableDefinition, ParamList(VariableDefinition,TypeAnnotation(TypeName)),TypeAnnotation(VoidType(void)))), AmbientDeclaration(declare,ClassDeclaration(class,VariableDefinition,ClassBody( MethodDeclaration(PropertyDefinition,ParamList(VariableDefinition,TypeAnnotation(TypeName))), PropertyDeclaration(PropertyDefinition,TypeAnnotation(TypeName)), MethodDeclaration(PropertyDefinition,ParamList,TypeAnnotation(VoidType(void)))))), ClassDeclaration(class,VariableDefinition,ClassBody( MethodDeclaration(declare,PropertyDefinition,ParamList), PropertyDeclaration(declare,PropertyDefinition,TypeAnnotation(TypeName))))) # Declare this in a Function {"dialect": "ts"} function foo(this: User) {} ==> Script(FunctionDeclaration(function,VariableDefinition,ParamList(this,TypeAnnotation(TypeName)),Block)) # Prefers type parameters to comparison operators {"dialect": "ts jsx"} let a = useState(1) return 2 ==> Script( VariableDeclaration(let,VariableDefinition,Equals, CallExpression(InstantiationExpression(VariableName,TypeArgList(TypeName)),ArgList(Number))), ReturnStatement(return,Number)) # Type parameters vs JSX {"dialect": "jsx ts"} let a = (f) => null let b = () => 1 ==> Script( VariableDeclaration(let,VariableDefinition,Equals,ArrowFunction( TypeParamList(TypeDefinition,extends,TypeName),ParamList(VariableDefinition),Arrow,null)), VariableDeclaration(let,VariableDefinition,Equals,ArrowFunction( TypeParamList(TypeDefinition),ParamList,Arrow,Number))) # Destructured parameters in function signature {"dialect": "ts"} type F = ([a, b]: [number, number]) => void ==> Script(TypeAliasDeclaration(type,TypeDefinition,Equals,FunctionSignature( ParamList(ArrayPattern(VariableDefinition,VariableDefinition),TypeAnnotation(TupleType(TypeName,TypeName))), Arrow, VoidType(void)))) # Instantiated expression {"dialect": "ts"} let x = a; type Foo = Bar>>; ==> Script( VariableDeclaration(let,VariableDefinition,Equals,InstantiationExpression(VariableName,TypeArgList(TypeName))), TypeAliasDeclaration(type,TypeDefinition,Equals,ParameterizedType(TypeName,TypeArgList( TypeofType(typeof,InstantiationExpression(VariableName,TypeArgList( ParameterizedType(TypeName,TypeArgList(TypeName))))))))) # Not instantiated {"dialect": "ts"} let x = ac ==> Script(VariableDeclaration(let,VariableDefinition,Equals,BinaryExpression( BinaryExpression(VariableName,CompareOp,VariableName),CompareOp,VariableName)))