ApgExpError
If the apgExp constructor encounters and error it will throw an ApgExpError exception object. Derived from JavaScript's Error object, it has special display member functions. If the SABNF pattern syntax has errors, specially formatted data is added to the object.
Properties
- name
- ApgExpError
- message
- a single-line error message
Methods
- toText()
- Displays the error message and possibly other pattern syntax error information in text format suitable for console.log().
- toHtml()
- Displays the error message and possibly other pattern syntax error information in HTML table format suitable for displaying in a web page.
Example 1
The general set up for catching pattern syntax errors and failed pattern matches.
try{ var exp, pattern, flags, result, strintToTest; exp = new apgExp(pattern, flags); result = exp.exec(stringToTest); if(result){ /* do some thing with the result */ }else{ /* deal with failure */ } }catch(e){ if(e.name === "ApgExpError"){ console.log(e.toText()); $("#errors-go-here").html(e.toHtml()); }else{ /* handle other exceptions */ } }
Example 2
A pattern syntax error as text.
var exp, pattern, result; pattern = 'word = alpha *(alpha / num\n'; pattern += 'alpha = %d65-90 / %d97-122\n'; pattern += '1num = %d48-57\n'; try{ exp = new apgExp(pattern); }catch(e){ if(e.name === "ApgExpError"){ console.log(e.toText()); }else{ console.log("other exception: "+e.message); } } / * result */ grammar has syntax errors 0: 0: 15: word = alpha * >> (alpha / num 0: 0: 15: error: Group, (...), opened but not closed. 0: 0: 15: word = alpha * >> (alpha / num 0: 0: 15: error: Unrecognized SABNF element. 2: 55: 0: >> 1num = %d48-57 2: 55: 0: error: Rule names must be alphanum and begin with alphabetic character.
Example 3
Same as example 2, except pattern syntax error as HTML.
$("#this-page").html(e.toHtml()); / * result */
grammar has syntax errors
line no. | line offset | error offset | text |
---|---|---|---|
0 | 0 | 15 | word = alpha *»(alpha / numLF |
↑: Group, (...), opened but not closed. | |||
0 | 0 | 15 | word = alpha *»(alpha / numLF |
↑: Unrecognized SABNF element. | |||
2 | 55 | 0 | »1num = %d48-57LF |
↑: Rule names must be alphanum and begin with alphabetic character. |