简体   繁体   English

PEG.js:如何使用提示?

[英]PEG.js: how to use prompt?

I'm creating a C++ parser with PEG.js, and I need to be able to use cin . 我正在使用PEG.js创建C ++解析器,并且我需要能够使用cin With the after-match JS, when I use prompt() , the (alternate) online version throws an error of 'Parse Error: prompt is not defined'. 使用赛后JS,当我使用prompt() ,(备用) 在线版本会引发“解析错误:未定义提示”错误。 I am trying to use the initializer to create a function to replicate prompt (probably not optimized, I was just trying it as a solution). 我正在尝试使用初始化程序创建一个复制prompt的函数(可能未优化,我只是尝试将其作为解决方案)。 However, when I do this, it still gives me the error. 但是,当我这样做时,它仍然给我错误。 I have tried using window.prompt as well, but again, it does not work. 我也尝试过使用window.prompt ,但是同样,它不起作用。 Here's an example of what I'm doing: 这是我正在做的事的一个例子:

{
    function cin() {
        window.prompt("");
    }

    function changeVar(variable, newValue) {
        if(typeof variable === typeof newValue) {
            variable = newValue;
        } else if(typeof variable === 'undefined') {
            alert("You can't assign a value to a variable if the variable isn't declared yet!");
        } else {
            alert("Bad assignment. In C++, a variable *must* have the same type *all the time*.");
        }
    }
}

stdin =
    whitespace* "std::cin" whitespace* ">>" whitespace* varToBeChanged:[a-zA-Z_]+ ";" whitespace*
        { changeVar(varToBeChanged, cin('')); return varToBeChanged; }

whitespace =
    space:[ \t]
        { return space; }

and then in the parser testing field: 然后在解析器测试字段中:

std::cin >> variable;

Thank you for looking. 谢谢您的光临。 I have tried Googling this and SO-searching this but I haven't found any results. 我已经尝试使用Google搜索,并对此进行了搜索,但未找到任何结果。

Also, here is the main piece of code , for all the (current) extra information anyone needs. 另外,这是主要的代码段 ,其中包含任何人需要的所有(当前)额外信息。 I am having some problems with this as well, but I'll try to figure them out on my own before I post another question. 我对此也有一些问题,但是在发布另一个问题之前,我会尝试自己解决这些问题。

If you are using http://peg.arcanis.fr/ , then the parser code is executed inside of a Web Worker - which has no access to any UI like the window or the DOM. 如果使用的是http://peg.arcanis.fr/ ,则解析器代码在Web Worker内执行-Web Worker无法访问任何UI(如window或DOM)。 The error " undefined variable " means literally that there is no window or prompt declared. 错误“ undefined variable ”字面意思是没有声明windowprompt

If you paste your code into http://pegjs.majda.cz/online , it is executed in the web page environment and works flawlessly. 如果将代码粘贴到http://pegjs.majda.cz/online ,则该代码将在网页环境中执行且可以正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM