| 1 |
// |reftest| skip |
| 2 |
|
| 3 |
loadRelativeToScript('Match.js'); |
| 4 |
|
| 5 |
var { Pattern, MatchError } = Match; |
| 6 |
|
| 7 |
function program(elts) { |
| 8 |
return Pattern({ type: "Program", body: elts }); |
| 9 |
} |
| 10 |
function exprStmt(expr) { |
| 11 |
return Pattern({ type: "ExpressionStatement", expression: expr }); |
| 12 |
} |
| 13 |
function throwStmt(expr) { |
| 14 |
return Pattern({ type: "ThrowStatement", argument: expr }); |
| 15 |
} |
| 16 |
function returnStmt(expr) { |
| 17 |
return Pattern({ type: "ReturnStatement", argument: expr }); |
| 18 |
} |
| 19 |
function yieldExpr(expr) { |
| 20 |
return Pattern({ type: "YieldExpression", argument: expr }); |
| 21 |
} |
| 22 |
function lit(val) { |
| 23 |
return Pattern({ type: "Literal", value: val }); |
| 24 |
} |
| 25 |
function comp(name) { |
| 26 |
return Pattern({ type: "ComputedName", name: name }); |
| 27 |
} |
| 28 |
function spread(val) { |
| 29 |
return Pattern({ type: "SpreadExpression", expression: val}); |
| 30 |
} |
| 31 |
function optExpr(val) { |
| 32 |
return Pattern({ type: "OptionalExpression", expression: val}); |
| 33 |
} |
| 34 |
function delOptExpr(val) { |
| 35 |
return Pattern({ type: "DeleteOptionalExpression", expression: val}); |
| 36 |
} |
| 37 |
var thisExpr = Pattern({ type: "ThisExpression" }); |
| 38 |
function funDecl(id, params, body, defaults=[], rest=null) { |
| 39 |
return Pattern({ type: "FunctionDeclaration", |
| 40 |
id: id, |
| 41 |
params: params, |
| 42 |
defaults: defaults, |
| 43 |
body: body, |
| 44 |
rest: rest, |
| 45 |
generator: false }); |
| 46 |
} |
| 47 |
function genFunDecl(style, id, params, body) { |
| 48 |
return Pattern({ type: "FunctionDeclaration", |
| 49 |
id: id, |
| 50 |
params: params, |
| 51 |
defaults: [], |
| 52 |
body: body, |
| 53 |
generator: true, |
| 54 |
style: style }); |
| 55 |
} |
| 56 |
function asyncFunDecl(id, params, body) { |
| 57 |
return Pattern({ type: "FunctionDeclaration", |
| 58 |
id: id, |
| 59 |
params: params, |
| 60 |
defaults: [], |
| 61 |
body: body, |
| 62 |
generator: false, |
| 63 |
async: true }); |
| 64 |
} |
| 65 |
function varDecl(decls) { |
| 66 |
return Pattern({ type: "VariableDeclaration", declarations: decls, kind: "var" }); |
| 67 |
} |
| 68 |
function letDecl(decls) { |
| 69 |
return Pattern({ type: "VariableDeclaration", declarations: decls, kind: "let" }); |
| 70 |
} |
| 71 |
function constDecl(decls) { |
| 72 |
return Pattern({ type: "VariableDeclaration", declarations: decls, kind: "const" }); |
| 73 |
} |
| 74 |
function ident(name) { |
| 75 |
return Pattern({ type: "Identifier", name: name }); |
| 76 |
} |
| 77 |
function dotExpr(obj, id) { |
| 78 |
return Pattern({ type: "MemberExpression", computed: false, object: obj, property: id }); |
| 79 |
} |
| 80 |
function memExpr(obj, id) { |
| 81 |
return Pattern({ type: "MemberExpression", computed: true, object: obj, property: id }); |
| 82 |
} |
| 83 |
function optDotExpr(obj, id) { |
| 84 |
return Pattern({ type: "OptionalMemberExpression", computed: false, object: obj, property: id }); |
| 85 |
} |
| 86 |
function optMemExpr(obj, id) { |
| 87 |
return Pattern({ type: "OptionalMemberExpression", computed: true, object: obj, property: id }); |
| 88 |
} |
| 89 |
function forStmt(init, test, update, body) { |
| 90 |
return Pattern({ type: "ForStatement", init: init, test: test, update: update, body: body }); |
| 91 |
} |
| 92 |
function forOfStmt(lhs, rhs, body) { |
| 93 |
return Pattern({ type: "ForOfStatement", left: lhs, right: rhs, body: body }); |
| 94 |
} |
| 95 |
function forInStmt(lhs, rhs, body) { |
| 96 |
return Pattern({ type: "ForInStatement", left: lhs, right: rhs, body: body }); |
| 97 |
} |
| 98 |
function breakStmt(lab) { |
| 99 |
return Pattern({ type: "BreakStatement", label: lab }); |
| 100 |
} |
| 101 |
function continueStmt(lab) { |
| 102 |
return Pattern({ type: "ContinueStatement", label: lab }); |
| 103 |
} |
| 104 |
function blockStmt(body) { |
| 105 |
return Pattern({ type: "BlockStatement", body: body }); |
| 106 |
} |
| 107 |
function literal(val) { |
| 108 |
return Pattern({ type: "Literal", value: val }); |
| 109 |
} |
| 110 |
var emptyStmt = Pattern({ type: "EmptyStatement" }); |
| 111 |
function ifStmt(test, cons, alt) { |
| 112 |
return Pattern({ type: "IfStatement", test: test, alternate: alt, consequent: cons }); |
| 113 |
} |
| 114 |
function labStmt(lab, stmt) { |
| 115 |
return Pattern({ type: "LabeledStatement", label: lab, body: stmt }); |
| 116 |
} |
| 117 |
function withStmt(obj, stmt) { |
| 118 |
return Pattern({ type: "WithStatement", object: obj, body: stmt }); |
| 119 |
} |
| 120 |
function whileStmt(test, stmt) { |
| 121 |
return Pattern({ type: "WhileStatement", test: test, body: stmt }); |
| 122 |
} |
| 123 |
function doStmt(stmt, test) { |
| 124 |
return Pattern({ type: "DoWhileStatement", test: test, body: stmt }); |
| 125 |
} |
| 126 |
function switchStmt(disc, cases) { |
| 127 |
return Pattern({ type: "SwitchStatement", discriminant: disc, cases: cases }); |
| 128 |
} |
| 129 |
function caseClause(test, stmts) { |
| 130 |
return Pattern({ type: "SwitchCase", test: test, consequent: stmts }); |
| 131 |
} |
| 132 |
function defaultClause(stmts) { |
| 133 |
return Pattern({ type: "SwitchCase", test: null, consequent: stmts }); |
| 134 |
} |
| 135 |
function catchClause(id, body) { |
| 136 |
return Pattern({ type: "CatchClause", param: id, body: body }); |
| 137 |
} |
| 138 |
function tryStmt(body, handler, fin) { |
| 139 |
return Pattern({ type: "TryStatement", block: body, handler: handler, finalizer: fin }); |
| 140 |
} |
| 141 |
|
| 142 |
function superProp(id) { |
| 143 |
return dotExpr(Pattern({ type: "Super" }), id); |
| 144 |
} |
| 145 |
function superElem(id) { |
| 146 |
return memExpr(Pattern({ type: "Super" }), id); |
| 147 |
} |
| 148 |
|
| 149 |
function classStmt(id, heritage, body) { |
| 150 |
return Pattern({ type: "ClassStatement", |
| 151 |
id: id, |
| 152 |
superClass: heritage, |
| 153 |
body: body}); |
| 154 |
} |
| 155 |
function classExpr(id, heritage, body) { |
| 156 |
return Pattern({ type: "ClassExpression", |
| 157 |
id: id, |
| 158 |
superClass: heritage, |
| 159 |
body: body}); |
| 160 |
} |
| 161 |
function classMethod(id, body, kind, static) { |
| 162 |
return Pattern({ type: "ClassMethod", |
| 163 |
name: id, |
| 164 |
body: body, |
| 165 |
kind: kind, |
| 166 |
static: static }); |
| 167 |
} |
| 168 |
function classField(id, init) { |
| 169 |
return Pattern({ type: "ClassField", |
| 170 |
name: id, |
| 171 |
init: init }); |
| 172 |
} |
| 173 |
function staticClassBlock(body) { |
| 174 |
return Pattern({ type: "StaticClassBlock", body: body }); |
| 175 |
} |
| 176 |
|
| 177 |
function funExpr(id, args, body, gen) { |
| 178 |
return Pattern({ type: "FunctionExpression", |
| 179 |
id: id, |
| 180 |
params: args, |
| 181 |
body: body, |
| 182 |
generator: false }); |
| 183 |
} |
| 184 |
function genFunExpr(style, id, args, body) { |
| 185 |
return Pattern({ type: "FunctionExpression", |
| 186 |
id: id, |
| 187 |
params: args, |
| 188 |
body: body, |
| 189 |
generator: true, |
| 190 |
style: style }); |
| 191 |
} |
| 192 |
function asyncFunExpr(id, args, body) { |
| 193 |
return Pattern({ type: "FunctionExpression", |
| 194 |
id: id, |
| 195 |
params: args, |
| 196 |
body: body, |
| 197 |
generator: false, |
| 198 |
async: true }); |
| 199 |
} |
| 200 |
function arrowExpr(args, body) { |
| 201 |
return Pattern({ type: "ArrowFunctionExpression", |
| 202 |
params: args, |
| 203 |
body: body }); |
| 204 |
} |
| 205 |
function asyncArrowExpr(isExpression, args, body) { |
| 206 |
return Pattern({ type: "ArrowFunctionExpression", |
| 207 |
params: args, |
| 208 |
body: body, |
| 209 |
generator: false, |
| 210 |
async: true, |
| 211 |
expression: isExpression }); |
| 212 |
} |
| 213 |
|
| 214 |
function metaProperty(meta, property) { |
| 215 |
return Pattern({ type: "MetaProperty", |
| 216 |
meta: meta, |
| 217 |
property: property }); |
| 218 |
} |
| 219 |
function newTarget() { |
| 220 |
return metaProperty(ident("new"), ident("target")); |
| 221 |
} |
| 222 |
|
| 223 |
function unExpr(op, arg) { |
| 224 |
return Pattern({ type: "UnaryExpression", operator: op, argument: arg }); |
| 225 |
} |
| 226 |
function binExpr(op, left, right) { |
| 227 |
return Pattern({ type: "BinaryExpression", operator: op, left: left, right: right }); |
| 228 |
} |
| 229 |
function aExpr(op, left, right) { |
| 230 |
return Pattern({ type: "AssignmentExpression", operator: op, left: left, right: right }); |
| 231 |
} |
| 232 |
function updExpr(op, arg, prefix) { |
| 233 |
return Pattern({ type: "UpdateExpression", operator: op, argument: arg, prefix: prefix }); |
| 234 |
} |
| 235 |
function logExpr(op, left, right) { |
| 236 |
return Pattern({ type: "LogicalExpression", operator: op, left: left, right: right }); |
| 237 |
} |
| 238 |
|
| 239 |
function condExpr(test, cons, alt) { |
| 240 |
return Pattern({ type: "ConditionalExpression", test: test, consequent: cons, alternate: alt }); |
| 241 |
} |
| 242 |
function seqExpr(exprs) { |
| 243 |
return Pattern({ type: "SequenceExpression", expressions: exprs }); |
| 244 |
} |
| 245 |
function newExpr(callee, args) { |
| 246 |
return Pattern({ type: "NewExpression", callee: callee, arguments: args }); |
| 247 |
} |
| 248 |
function callExpr(callee, args) { |
| 249 |
return Pattern({ type: "CallExpression", callee: callee, arguments: args }); |
| 250 |
} |
| 251 |
function optCallExpr(callee, args) { |
| 252 |
return Pattern({ type: "OptionalCallExpression", callee: callee, arguments: args }); |
| 253 |
} |
| 254 |
function superCallExpr(args) { |
| 255 |
return callExpr({ type: "Super" }, args); |
| 256 |
} |
| 257 |
function arrExpr(elts) { |
| 258 |
return Pattern({ type: "ArrayExpression", elements: elts }); |
| 259 |
} |
| 260 |
function objExpr(elts) { |
| 261 |
return Pattern({ type: "ObjectExpression", properties: elts }); |
| 262 |
} |
| 263 |
function computedName(elts) { |
| 264 |
return Pattern({ type: "ComputedName", name: elts }); |
| 265 |
} |
| 266 |
function templateLit(elts) { |
| 267 |
return Pattern({ type: "TemplateLiteral", elements: elts }); |
| 268 |
} |
| 269 |
function taggedTemplate(tagPart, templatePart) { |
| 270 |
return Pattern({ type: "TaggedTemplate", callee: tagPart, arguments : templatePart }); |
| 271 |
} |
| 272 |
function template(raw, cooked, ...args) { |
| 273 |
return Pattern([{ type: "CallSiteObject", raw: raw, cooked: cooked}, ...args]); |
| 274 |
} |
| 275 |
|
| 276 |
function arrPatt(elts) { |
| 277 |
return Pattern({ type: "ArrayPattern", elements: elts }); |
| 278 |
} |
| 279 |
function objPatt(elts) { |
| 280 |
return Pattern({ type: "ObjectPattern", properties: elts }); |
| 281 |
} |
| 282 |
|
| 283 |
function assignElem(target, defaultExpr = null, targetIdent = typeof target == 'string' ? ident(target) : target) { |
| 284 |
return defaultExpr ? aExpr('=', targetIdent, defaultExpr) : targetIdent; |
| 285 |
} |
| 286 |
function assignProp(property, target, defaultExpr = null, shorthand = !target, targetProp = target || ident(property)) { |
| 287 |
return Pattern({ |
| 288 |
type: "Property", key: ident(property), shorthand, |
| 289 |
value: defaultExpr ? aExpr('=', targetProp, defaultExpr) : targetProp }); |
| 290 |
} |