Module:ObjectArg/doc

From Fire Emblem Heroes Wiki
Jump to: navigation, search

This is the documentation page for Module:ObjectArg

Lua metamodule which parses complex object values. Exposes a single function, parse, which accepts a JSON-like string and returns the Lua representation of that string. The syntax of such values is described below. (The mw library already provides functions for generating and parsing real JSON strings.)

The function returns 2 values:

  • If the function successfully parses the input string, the first return value contains the represented object and the second return value is nil.
  • If parsing fails, the first return value is nil and the second return value is an error message string.

Tag[edit source]

A tag is any string not containing the special characters [, ], {, }, =, or ;. Whenever possible, the module converts numeric strings to numbers using the built-in tonumber function (in base 10). ASCII whitespace characters around tags are trimmed.

List[edit source]

A list consists of zero or more values enclosed in square brackets and separated by semicolons. Values may be empty, in which case the resulting Lua table ceases to be a valid Lua sequence.

Hash[edit source]

A hash consists of zero or more tag-value pairs enclosed in curly brackets and separated by semicolons. The tag and value are separated by the equals sign. Values may be empty, in which case the resulting Lua table does not contain an element with that value's corresponding key. Keys cannot be empty. Nested hashes coming from template arguments may avoid prematurely terminating the template, by separating consecutive closing curly brackets with whitespace or a semicolon.

Value[edit source]

A value is either a tag, a list, or a hash. The top-level value is wholly represented by the input string, and cannot be empty.

Examples[edit source]

local parse = require 'Module:ObjectArg'.parse

mw.logObject(parse [[1]]) -- 1
mw.logObject(parse [[a]]) -- "a"
mw.logObject(parse [["1"]]) -- "\"1\""
mw.logObject(parse [[
[a;b;;d;]
]]) -- {"a", "b", [4] = "d"}
mw.logObject(parse [[
{weapon=;assist=Draw Back;base stats=[35;33;35;18;28]}
]]) -- {assist = "Draw Back", ["base stats"] = {35, 33, 35, 18, 28}}
mw.logObject(parse [[
{1=a;2=b;4=d}
]]) -- {"a", "b", [4] = "d"}

Formal grammar[edit source]

The following LL(1) grammar fully specifies the syntax of the strings accepted by this module, except that all ASCII whitespace characters between tokens are trimmed.

V → T
  | [ L ]
  | { H }

V0 → V
   | ε

H → T = V0 H'
  | ε

H' → ; H
   | ε

L → V0 L'

L' → ; L
   | ε

T → /[^=;\{\}\[\]]+/