antlr | select antlr tree | get antlr tool | Search

This JavaScript code imports necessary modules, defines an asynchronous function testAntlr that interprets and generates type information from a code snippet, and writes the result to a file named tree.json. The function is exported as a module and can be executed asynchronously using the $.async() function, with its result handled by the $.sendResult or $.sendError functions depending on its success or failure.

Run example

npm run import -- "test select jison on some quake 3 C code"

test select jison on some quake 3 C code

const fs = require('fs')
const path = require('path')
//const importer = require('../Core')
const getTypes = importer.import("get c types")
//var antlrToESTree = importer.import("antlr tree to es")

async function testAntlr() {
    let codeCell = importer.interpret('rgb 2 lab')
    var result = getTypes(codeCell.source.join(''))
    console.log(result)
    var result = await (await selectCode)(['//functionDefinition', 
     './declarator//declaratorid/@strdata'], path.join(__dirname, '../mouse.m'), 'cpp')
    //var modified = antlrToESTree(result)
    fs.writeFileSync('tree.json', JSON.stringify(result, null, 2))
    return result
}

module.exports = testAntlr

if(typeof $ !== 'undefined') {
    $.async()
    testAntlr()
        .then(r => $.sendResult(r))
        .catch(e => $.sendError(e))
}

What the code could have been:

// Import required modules
const fs = require('fs');
const path = require('path');
const importer = require('../Core');
const getTypes = importer.import('get c types');
const selectCode = importer.import('select code');

/**
 * Test ANTLR functionality by parsing and printing the types of a given code snippet.
 * @returns {Promise} The parsed types.
 */
async function testAntlr() {
    try {
        // Interpret the code cell
        const codeCell = await importer.interpret('rgb 2 lab');
        
        // Get the source code
        const sourceCode = codeCell.source.join('');

        // Get the types of the source code
        const result = await getTypes(sourceCode);

        // Log the result
        console.log(result);

        // Select code from a file
        const selectedCode = await selectCode(['//functionDefinition', './declarator//declaratorid/@strdata'], 
                                               path.join(__dirname, '../mouse.m'), 'cpp');

        // Write the selected code to a JSON file
        fs.writeFileSync('tree.json', JSON.stringify(selectedCode, null, 2));

        // Return the selected code
        return selectedCode;
    } catch (error) {
        // Log any errors
        console.error(error);
    }
}

// Export the function
module.exports = testAntlr;

// Call the function and handle the result
if (typeof $!== 'undefined') {
    $.async();
    testAntlr()
       .then((result) => $.sendResult(result))
       .catch((error) => $.sendError(error));
}

Breakdown of the Code

Importing Modules

const fs = require('fs')
const path = require('path')
const importer = require('../Core')
const getTypes = importer.import('get c types')
  • The code starts by importing the fs (File System) and path modules, which are built-in Node.js modules for interacting with the file system and working with file paths.
  • It also imports the importer module from a file named Core located in the parent directory.
  • The getTypes function is imported from the importer module, specifically from a file identified by the string 'get c types'.

Test Function

async function testAntlr() {
    //...
}
  • The testAntlr function is defined as an asynchronous function.

Function Body

  • The function interprets a code snippet using the importer.interpret function and stores the result in the codeCell variable.
  • The codeCell.source property is joined into a string and passed to the getTypes function to obtain type information. The result is logged to the console.
  • An asynchronous function named selectCode is called with an array of selectors, a file path (../mouse.m), and a language (cpp). The result is stored in the result variable.
  • The result is written to a file named tree.json using fs.writeFileSync.
  • The result is returned from the function.

Exports and Event Handling

module.exports = testAntlr

if(typeof $!== 'undefined') {
    $.async()
    testAntlr()
       .then(r => $.sendResult(r))
       .catch(e => $.sendError(e))
}
  • The testAntlr function is exported as a module using module.exports.
  • The code checks if the $ object is defined. If it is, the following code is executed:
    • The $.async() function is called, likely to initiate some asynchronous operation.
    • The testAntlr function is called, and its result is handled using promise chaining:
      • If the function resolves successfully, the result is passed to the $.sendResult function.
      • If the function rejects with an error, the error is passed to the $.sendError function.