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.
npm run import -- "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))
}
// 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
const fs = require('fs')
const path = require('path')
const importer = require('../Core')
const getTypes = importer.import('get c types')
fs
(File System) and path
modules, which are built-in Node.js modules for interacting with the file system and working with file paths.importer
module from a file named Core
located in the parent directory.getTypes
function is imported from the importer
module, specifically from a file identified by the string 'get c types'
.async function testAntlr() {
//...
}
testAntlr
function is defined as an asynchronous function.importer.interpret
function and stores the result in the codeCell
variable.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.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.result
is written to a file named tree.json
using fs.writeFileSync
.result
is returned from the function.module.exports = testAntlr
if(typeof $!== 'undefined') {
$.async()
testAntlr()
.then(r => $.sendResult(r))
.catch(e => $.sendError(e))
}
testAntlr
function is exported as a module using module.exports
.$
object is defined. If it is, the following code is executed:
$.async()
function is called, likely to initiate some asynchronous operation.testAntlr
function is called, and its result is handled using promise chaining:
$.sendResult
function.$.sendError
function.