如何读取 Javascript 文件

How to read a Javascript file

本文关键字:Javascript 文件 读取 何读取      更新时间:2023-10-16

我有一个文件/path/to/my_js_functions.js,其中包含一个Javascript函数my_js_functions((等函数。

如何在C++中将函数myJsFunction读取为字符串?

我正在寻找一种方法来获取整个函数,并且仅获取该函数,然后将其包含在char *jsFunction中。

function myJsFunction(stringParam) {
return stringParam   // The function returns a stringParam from the parameter
}  

function anotherJsFunction(stringParam) {
return stringParam   // Another function
}  

提前谢谢大家。

使用 fstream,我会逐行读取并检查每行是否包含序列 myJsFunction。如果一行确实包含此序列,则开始聚合到字符串,直到到达下一个函数(在到达下一个关键字"function"或类似内容后停止(。请注意,使用}作为关键字可能不起作用,因为函数可能有多个右大括号。

另一种可能的解决方案可能包括通过注意当换行符后面紧跟非空格时,新函数开始来识别函数的结尾,假设文件中的代码被格式化,其中任何范围较低的内容都被正确选项卡化。

为此,您需要读取javascript代码文件并对其进行解析。使用一些解析器库来做到这一点是非常高度的,比如腰果,esprima-cpp。我以前从未使用过它,所以我不能对此发表评论。

但这里有一些解析器的快速代码。您可以从此构建开始,以使其更加健壮。

主.cpp

#include <fstream>
#include <iostream>
#include <streambuf>
#include <string>
#include <vector>
std::string getFunction(const std::string &fileData, const std::string &name) {
std::string ret;
std::size_t start = 0;
while (true) {
const auto fNameStart = fileData.find(name, start);
if (fNameStart != std::string::npos) {
auto fStart = fileData.find_last_not_of(" ",fNameStart-1);
if(fStart == std::string::npos){
ret = "No Function Defination";
break;
}
else {
fStart = fStart-7;
if(fileData.substr(fStart,8) == "function"){
int openBraceCount = 0, closeBraceCount = 0;
std::size_t fEnd = fNameStart + name.size();
fEnd = fileData.find_first_of("{}", fEnd);
while (fEnd != std::string::npos) {
if (fileData.at(fEnd) == '{') {
openBraceCount++;
} else {
closeBraceCount++;
}
if (openBraceCount == closeBraceCount) {
ret = fileData.substr(fStart, fEnd - fStart+1);
break;
}
fEnd++;
fEnd = fileData.find_first_of("{}", fEnd);
}
if(!ret.empty()){
break;
}
else if(openBraceCount != closeBraceCount){
ret = "Function Parse Error";
break;
}
}
else{
start = fNameStart + name.size();
}
}
} else {
ret = "No Function Defination";
break;
}
}
return ret;
}
int main(int argc, char **argv) {
const std::string jsPath = "module.js";
const std::vector<std::string> vecFuncNames{"funcA", "funcB", "funcC",
"funcD", "funcE"};
std::ifstream fs(jsPath);
if (fs.is_open()) {
std::string fileData((std::istreambuf_iterator<char>(fs)),
std::istreambuf_iterator<char>());
for (auto &name : vecFuncNames) {
std::cout << name << "n" << getFunction(fileData, name) << std::endl;
}
}
return 0;
}

模块.js

function     funcA    (    ){
funcC();  console.log("  Hello");funcB();
}function funcC(){funcB();console.log("Hello");funcA();}
function funcB(a,   b,   c){
funcA();   setTimeout(function(){ alert("Hello"); }, 3000);funcC();
}
funcD();
function funcE(){{{{}}}

你可以简单地这样做

例如/path/code.js 是你的代码存储的路径

你的代码.js

function myJsFunction(stringParam) {
return stringParam   // The function returns a stringParam from the parameter
}  

function anotherJsFunction(stringParam) {
return stringParam   // Another function
}
module.exports = {
myJsFunction,
anotherJsFunction
}

这是您用来读取您编写的函数的文件

索引.js

const code = require('/path/code.js');

console.log(代码(,它将在您的代码.js文件中显示您的整个代码

如果要使其成为字符串,可以使用这样的语法。在下面的代码中添加它到你的索引.js文件,它将使你的代码成为字符串版本。

String(code.myJsFunction)