• Jump To … +
    main.js separate.js single.js web-apg-api.js main.js web-conv-api.js ast.js csv.js dangling-else.js display.js flags.js float.js limits.js main.js multiline-mode.js recursive.js replace.js rules.js split.js testonly.js trace.js udt.js unicode.js web-email.js word-boundaries.js main.js phone-number.js web-main.js web-phone-number.js main.js phone-number.js setup.js translate.js xml.js branch-fail-grammar.js main.js parent-mode-grammar.js setup.js universal-mode-grammar.js colors-app.js colors-callbacks.js colors.js main.js more-app.js more-setup.js more.js ast-callbacks.js bad-input.js basic.js ini-file.js main.js parser-callbacks.js setup.js trace.js anbncn.js and.js c-comment.js compound.js main.js nested.js not.js setup.js boundaries-grammar.js boundaries.js comment-grammar.js comment.js main.js negative-grammar.js negative.js positive-grammar.js positive.js setup.js main.js odata-grammar.js run.js setup.js area-code.js lookaround.js main.js phone-number.js setup.js simple.js all-operators.js default.js fancy-number.js limited-lines.js main.js select-operators.js select-rules.js setup.js main.js minimal.js parent-u.js parent.js phone-number.js setup.js stats.js trace.js universal-u.js universal.js callbacks.js grammar.js main.js parser.js writeHtml.js LICENSE.md README.md index.md
  • more-app.js

  • §
    /* eslint-disable func-names */
    /*  *************************************************************************************
     *   copyright: Copyright (c) 2021 Lowell D. Thomas, all rights reserved
     *     license: BSD-2-Clause (https://opensource.org/licenses/BSD-2-Clause)
     *   ********************************************************************************* */
  • §

    This module defines the UDT callback functions for the demonstration. One UDT will match a phrase directly, the normal UDT function. One UDT will match a phrase by calling one of the rule name operators defined by the SABNF grammar. A strange thing to do, you might think, and you might well be right. But look at the trace of the parse tree and in this case you will see that there is a RNM operator below the UDT operator in the tree. There can be cases where this will turn out to be useful when translating the AST.

    This module calls more-setup.js twice, once for each case described above. The stat and trace displays are collected and shown on a single web page for easy comparison.

    (function moreApp() {
      const { apgLib } = require('apg-js');
      const htmlWrite = require('../writeHtml');
      const setup = require('./more-setup');
      const fileName = 'more-evaluate-rule';
      const id = apgLib.ids;
  • §

    This UDT callback will do phrase matching with a call to evaluateRule().

      const evalRuleCallback = function (result, chars, phraseIndex) {
        let phrase = phraseIndex;
        let length = 0;
        while (phrase + 5 <= chars.length) {
          /* call evaluateRule() here */
          result.evaluateRule(1, phrase, result);
          if (result.state === id.MATCH) {
            length += result.phraseLength;
            phrase += result.phraseLength;
          } else {
            break;
          }
        }
        if (length > 0) {
          result.state = id.MATCH;
          result.phraseLength = length;
        } else {
          result.state = id.NOMATCH;
          result.phraseLength = 0;
        }
      };
  • §

    This UDT callback will simply match a phrase on its own.

      const udtCallback = function (result, chars, phraseIndex) {
        let phrase = phraseIndex;
    
        let length = 0;
        while (phrase + 5 <= chars.length) {
          if (
            chars[phrase] === 32 &&
            chars[phrase + 1] === 109 &&
            chars[phrase + 2] === 111 &&
            chars[phrase + 3] === 114 &&
            chars[phrase + 4] === 101
          ) {
            length += 5;
            phrase += 5;
          } else {
            break;
          }
        }
        if (length > 0) {
          result.state = id.MATCH;
          result.phraseLength = length;
        } else {
          result.state = id.NOMATCH;
          result.phraseLength = 0;
        }
      };
      let html = '';
      /* get the "regular" UDT phrase */
      html += setup(udtCallback, 'more parsed with UDT');
      /* get the phrase with a call to evaluateRule() */
      html += setup(evalRuleCallback, 'more parsed with evaluateRule()');
  • §

    Display it all on the web page.

      html = apgLib.utils.htmlToPage(html, 'more app');
      htmlWrite(html, fileName);
    })();