0

I'm starting to use excellent PEG.JS JavaScript parser generator
to implement Qt's qmake project file parser (*.pro).
It looks bash script, with variable assignments and function calls.
First of all, i need to parse all assignments to some kind of Dictionary, and use it in external code.

I can't undestand how to access internal parser state from the external function. I.e.: i need to parse my pro-file, and save state into client code-accessible variable, e.g. JavaScript Dictionary. But i can't achieve this!

Grammar (right now understood only assignment statement like TEMPLATE = app):

{
    var SystemVariablesDict = {};
}

Start = Statement*
Statement = Whitespace GenericAssignmentStatement Whitespace
GenericAssignmentStatement = TemplateAssignmentStatement

// TEMPLATE = app|lib|subdirs|aux|vcapp|vclib
SystemTemplateVariableName = "TEMPLATE"
SystemTemplateVariableValue = "app" / "lib" / "subdirs" / "aux" / "vcapp" / "vclib"
TemplateAssignmentStatement = lvalue:SystemTemplateVariableName 
AssignmentOperator rvalue:SystemTemplateVariableValue
{
    // [REF 1] !!! Assignment to the global state variable !!!
    SystemVariablesDict[lvalue] = rvalue;
    return undefined;
}

AssignmentOperator = Whitespace "=" Whitespace

Word = w:Character+ { return w.join(""); }
Character = c:[a-zA-Z0-9]
Whitespace = [ \t\r\n]*

(The grammar above was successfully compiled in the qmake-project-parser.js) The parser usage code (frontend.js file):

var pegjs = require("./qmake-project-parser.js");
var parserOutput = pegjs.parse("TEMPLATE = app");
console.log("PEG.js parser output:");
console.log(parserOutput);
console.log("System variables dict:");
console.log(pegjs.SystemVariablesDict);

And the output:

C:\Projects\qmake-project-parser>node frontend.js
PEG.js parser output:
[ [ [], undefined, [] ] ]
System variables dict:
**undefined**

The question is: why undefined was outputed, however the REF 1 rule was successfully applied?
What am i doing wrong?

Thanks!

P.S. I'm new to JavaScript, so be patient to my silly mistakes please :)

4

1 回答 1

1

从我使用 PEG.js 的记忆中,开始规则通常用于返回您想要返回的任何内容作为解析器输出:

Start = Statement* { return SystemVariablesDict }

应该以parserOutput.

于 2017-05-21T17:42:51.680 回答