2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
10 require("pretty-print.js");
13 * @param name specifies the name of the label to be defined
15 * The remaining arguments should be strings, specifying the names of
16 * required arguments. Additionally, the last additional argument can
17 * be `null', which indicates that unlimited optional arguments can
18 * follow. If `null' is not specified as the last argument, then it is
19 * assumed that all optional arguments are keyword arguments. The
20 * optional arguments are stored as an array in a member called $.
22 function define_label (name) {
23 var allow_optional = false;
24 var required_args = [];
25 for (let i = 1, ii = arguments.length; i < ii; ++i) {
26 if (arguments[i] === null) {
27 allow_optional = true;
29 throw new Error("null must be the last argument");
31 required_args.push(arguments[i]);
34 function toString () {
35 let optional = this.$;
38 let printed_args = [];
39 let seen_defined_yet = false;
40 for (let i = required_args.length - 1; i >= 0; --i) {
41 let arg = required_args[i];
42 if (seen_defined_yet || this[arg] !== undefined) {
43 printed_args.unshift(arg + " = " + pretty_print_value(this[arg]));
44 seen_defined_yet = true;
47 printed_args.push.apply(null, optional.map(pretty_print_value));
49 if (i.length > 1 && i[0] == "$")
50 printed_args.push(i + " = " + this[i]);
52 if (printed_args.length > 0)
53 printed_args = "(" + printed_args.join(", ") + ")";
56 return this._name + printed_args;
59 result = function () {
60 var o = { _name: name, toString: toString, _id: result, __is_label: true, toSource: toString };
61 let max_req_arg = arguments.length;
62 if (max_req_arg > required_args.length)
63 max_req_arg = required_args.length;
64 for (let i = 0; i < max_req_arg; ++i)
65 o[required_args[i]] = arguments[i];
67 o.$ = Array.prototype.slice.call(arguments, required_args.length);
69 write_keywords(o, arguments, required_args.length);
75 for (let i in required_args)
76 result[i] = undefined;
77 result.toString = toString;
78 result.__is_label = true;
79 conkeror[name] = result;
82 function label_id (value) {