• Jump To … +
    api.js attributes.js node-exports.js parser.js rule-attributes.js rule-dependencies.js sabnf-grammar.js scanner-callbacks.js scanner-grammar.js scanner.js semantic-callbacks.js show-rules.js syntax-callbacks.js web-exports.js converter.js node-exports.js transformers.js web-exports.js apg-conv.js help.js apg-exp.js exec.js flags.js parse-replacement.js replace-grammar.js replace.js result.js sabnf-generator.js split.js web-exports.js ast.js circular-buffer.js emitcss.js identifiers.js node-exports.js parser.js stats.js style.js trace.js utilities.js web-exports.js apg.js command-line.js LICENSE.md README.md index.md
  • scanner.js

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

    This module reads the input grammar file and does a preliminary analysis before attempting to parse it into a grammar object. See:
    ./dist/scanner-grammar.bnf
    for the grammar file this parser is based on.

    It has two primary functions.

    • verify the character codes - no non-printing ASCII characters
    • catalog the lines - create an array with a line object for each line. The object carries information about the line number and character length which is used by the parser generator primarily for error reporting.
    module.exports = function exfn(chars, errors, strict, trace) {
      const thisFileName = 'scanner.js: ';
      const apglib = require('../apg-lib/node-exports');
      const grammar = new (require('./scanner-grammar'))();
      const { callbacks } = require('./scanner-callbacks');
    
      /* Scan the grammar for character code errors and catalog the lines. */
      const lines = [];
  • §

    eslint-disable-next-line new-cap

      const parser = new apglib.parser();
  • §

    eslint-disable-next-line new-cap

      parser.ast = new apglib.ast();
      parser.ast.callbacks = callbacks;
      if (trace) {
        if (trace.traceObject !== 'traceObject') {
          throw new TypeError(`${thisFileName}trace argument is not a trace object`);
        }
        parser.trace = trace;
      }
    
      /* parse the input SABNF grammar */
      const test = parser.parse(grammar, 'file', chars);
      if (test.success !== true) {
        errors.push({
          line: 0,
          char: 0,
          msg: 'syntax analysis error analyzing input SABNF grammar',
        });
        return;
      }
      const data = {
        lines,
        lineNo: 0,
        errors,
        strict: !!strict,
      };
    
      /* translate (analyze) the input SABNF grammar */
      parser.ast.translate(data);
  • §

    eslint-disable-next-line consistent-return

      return lines;
    };