• 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-setup.js

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

    This module sets up an application to demonstrate how to call any arbitrary rule name RNM callback function from any other callback function - in this case from a UDT callback. (See the initial discussion for colors-app.js for the motivation and cautionary tales of doing this kind of craziness.)

    module.exports = function moreSetup(udtcallback, title) {
      const nodeUtil = require('node:util');
      const inspectOptions = {
        showHidden: true,
        depth: null,
      };
      try {
        const { apgLib } = require('apg-js');
        const grammar = new (require('./more'))();
        const parser = new apgLib.parser();
        parser.stats = new apgLib.stats();
        parser.trace = new apgLib.trace();
        parser.callbacks.u_more = udtcallback;
        const inputString = 'start more more';
        const inputCharacterCodes = apgLib.utils.stringToChars(inputString);
        const startRule = 0;
    
        /* parse the input string */
        const result = parser.parse(grammar, startRule, inputCharacterCodes);
    
        /* display parser results */
        console.log();
        console.log("the parser's results");
        console.dir(result, inspectOptions);
        if (result.success === false) {
          throw new Error(`input string: '${inputString}' : parse failed`);
        }
        let html = '';
        html += parser.stats.toHtml('hits', title);
        html += parser.trace.toHtml(title);
    
        /* Returns the statistics and trace displays to the application.
            The application will decide how to display it. */
        return html;
      } catch (e) {
        let msg = '\nEXCEPTION THROWN: \n';
        if (e instanceof Error) {
          msg += `${e.name}: ${e.message}`;
        } else if (typeof e === 'string') {
          msg += e;
        } else {
          msg += nodeUtil.inspect(e, inspectOptions);
        }
        process.exitCode = 1;
        console.log(msg);
        throw e;
      }
    };