headerphoto

The result Object

The result object contains the phrase matched by the pattern in the input string along with other information. It has the location in the string where the match was found and parsing information. It also has the location and phrase matched for all matches to all pattern syntax rules.

Syntax

var exp    = new apgExp(pattern[, flags]);
var result = exp.exec(input);

Properties

[0]string/arraythe matched phrase
inputstring/arraya copy of the input string
indexintegerthe index or offset from the beginning of the input string to the matched phrase
lengthintegerthe length of the matched phrase
treeDepthintegerthe actual maximum depth reached in the parse tree by the parser during the match
nodeHitsintegerthe actual number of unit steps required by the parser to make the match
rulesobjectAn object with phrase information for each of the named rules defined in the pattern syntax. The first rule in the pattern defines the entire phrase to be matched. The property, result[0], is just an alias for the first-named phrase.
rules[name]null if no match to rule "name" was found
rules[name]an array of phrase objects, one item for each phrase
rules[name][0]{phrase: string/array, index: integer}

If Unicode mode is true, i.e. flags = "u", phrase is an array of integer character codes, otherwise it is a string.

Methods

  • toText()
    • displays the result object in text format, suitable for output with console.log()
  • toHtml()
    • displays the result object in HTML format, suitable for display on a web page
  • toHtmlPage()
    • displays the result object in HTML format as a complete, stand-alone web page

Example 1

The following set up is used for all three examples. Example 1 is figurative. It illustrates the format of the rules object.

var pattern, str, exp, result;
pattern  = 'word  = 1*(alpha / num)\n';
pattern += 'alpha = %d65-90 / %d97-122\n';
pattern += 'num   = %d48-57\n';
str = "ab12";
exp = new apgExp(pattern);
result = exp.exec(str);
/* result */
itemvalue
[0]ab12
inputab12
index0
length4
tree depth6
node hits26
rules["word"][0]{phrase: "ab12", index: 0}
rules["alpha"][0]{phrase: "a", index: 0}
rules["alpha"][1]{phrase: "b", index: 1}
rules["num"][0]{phrase: "1", index: 2}
rules["num"][1]{phrase: "2", index: 3}

Example 2

Example 2 shows the same result object as example 1, but shows how it is actually displayed with the toText() function.

console.log(result.toText());
/* result */
    result:
       [0]: ab12
     input: ab12
     index: 0
    length: 4
tree depth: 7
 node hits: 26
     rules: word : 0: ab12
          : alpha : 0: a
          : alpha : 1: b
          : num : 2: 1
          : num : 3: 2

Example 3

Example 3 shows the same result object as example 1, but shows how it is actually displayed with the toHtml() function. The toHtmlPage() function would give the same output but wrapped in a complete HTML page header and footer.

$("#this-page").html(result.toHtml());
/* result */
result:
itemvaluephrase
[0]0ab12
input0ab12
index0
length4
tree depth7
node hits26
rulesindexphrase
word0ab12
alpha0a
alpha1b
num21
num32