buffer-apg-js
Version:
JavaScript APG, an ABNF Parser Generator
118 lines (117 loc) • 4.2 kB
HTML
<html lang="en">
<head>
<title>apg-exp</title>
<meta charset="utf-8">
<link rel="stylesheet" href="./css/apgexp.css" type="text/css" />
<link rel="stylesheet" href="./css/BrightSide.css" type="text/css" />
<script type="text/javascript" src="./import.js"></script>
</head>
<body>
<div id="wrap">
<div id="header"></div>
<div id="content-wrap">
<img src="./images/PointLobosCropped.jpg" width="820" height="120" alt="headerphoto" class="no-border" />
<div id="sidebar"></div>
<div id="main-2col">
<h1>ApgExpError</h1>
<p>
If the <kbd>apgExp</kbd> constructor encounters and error it will throw an <kbd>ApgExpError</kbd> exception object.
Derived from JavaScript's <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a> object, it has special display member functions. If the SABNF pattern syntax has errors,
specially formatted data is added to the object.
</p>
<h3>Properties</h3>
<p>
<ul>
<li>name</li>
<ul>
<li>ApgExpError</li>
</ul>
<li>message</li>
<ul>
<li>a single-line error message</li>
</ul>
</ul>
</p>
<h3>Methods</h3>
<p>
<ul>
<li>toText()</li>
<ul>
<li>Displays the error message and possibly other pattern syntax error information in text format
suitable for console.log().</li>
</ul>
<li>toHtml()</li>
<ul>
<li>Displays the error message and possibly other pattern syntax error information in HTML table format
suitable for displaying in a web page.</li>
</ul>
</ul>
</p>
<h3>Example 1</h3>
<p>The general set up for catching pattern syntax errors and failed pattern matches.</p>
<pre>
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 */
}
}</pre>
<h3>Example 2</h3>
<p>A pattern syntax error as text.</p>
<pre>
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.
</pre>
<h3>Example 3</h3>
<p>Same as example 2, except pattern syntax error as HTML.</p>
<pre>
$("#this-page").html(e.toHtml());
/ * result */
</pre>
<h3>grammar has syntax errors</h3>
<p><table class="apg-last-left-table margin">
<tr><th>line<br>no.</th><th>line<br>offset</th><th>error<br>offset</th><th><br>text</th></tr>
<tr><td>0</td><td>0</td><td>15</td><td>word = alpha *<span class="apg-nomatch">»</span>(alpha / num<span class="apg-ctrl-char">LF</span></td></tr>
<tr><td colspan="3"></td><td>↑: Group, (...), opened but not closed.</td></tr>
<tr><td>0</td><td>0</td><td>15</td><td>word = alpha *<span class="apg-nomatch">»</span>(alpha / num<span class="apg-ctrl-char">LF</span></td></tr>
<tr><td colspan="3"></td><td>↑: Unrecognized SABNF element.</td></tr>
<tr><td>2</td><td>55</td><td>0</td><td><span class="apg-nomatch">»</span>1num = %d48-57<span class="apg-ctrl-char">LF</span></td></tr>
<tr><td colspan="3"></td><td>↑: Rule names must be alphanum and begin with alphabetic character.</td></tr>
</table></p>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>