问题标签 [pegjs]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
312 浏览

javascript - Access Control String (ACS) Parser/Interpreter with PEG.js

Preface

I'm working on creating a Access Control String (or System) (ACS) string Parser/Interpreter with PEG.js. ACS strings are commonly used on Bulletin Board Systems (BBSs) to check access rights to particular areas of the board. For example, see Renegade's ACS documentation.

Example ACS Strings

Below are some simplified strings and their English translations for illustration:

What I'm Trying to Achieve

What I would like to do here is parse and interpret (or "run") the ACS string and ultimately end up with a final boolean.

Grammar So Far

Below is the PEG.js grammer I've some up with so far. Note that the ACS strings themselves are a bit more complex than the examples above (I allow for example GM['abc','def']) but I think up to this point it's fairly self explanatory.

Where I Need Help

The main issue I'm having (if not others I haven't run into yet!) is handling precedence with () and the OR clauses. This may be something simple, but I've worked on this for days and have some up short. Again, what I'm ultimately attempting to achieve here is to feed in an ACS string and output a final boolean result. The various ACS "commands" (e.g. 'GM' in the above example) should make method calls that do the dirty work.

0 投票
2 回答
220 浏览

parsing - 如何使用 PEG.js 对非空行进行分组

我正在尝试使用PEG.js解析类别文件

如何对类别进行分组(一组非空行后跟一个空行)

现在我可以用这段代码逐行解析:

我有:

但我想要(对于每个组):

我也试过这个,但它没有分组,我在一些行的开头得到了 \n 。

0 投票
1 回答
556 浏览

javascript - parsing boolean expression chain with pegjs

I'm trying to parse this string with peg.js:

The relevant extract of the grammar looks like:

The problem is that the boolean chain won't be recognised for more than 2 expression (expr1 or expr2) and I don't know how to parse more "or exprN" parts. I introduced a "bools" rule, but this doesn't work either. Any idea of how I can resolve this and parse arbitrarily long boolean expressions?

0 投票
1 回答
508 浏览

grammar - PEGJS 谓词语法

我需要在谓词的帮助下创建一个语法。对于给定的情况,以下语法失败。

对于给定的输入,它失败了

它应该在结果部分中将“create”作为“name”键的值。

任何帮助都会很棒。

0 投票
1 回答
270 浏览

javascript - Pegjs:不允许保留关键字作为变量名

我正在用 Pegjs 编写我的语言,并且像往常一样,我的语言有一些关键字,例如true, false, if,elsetoday例如。现在,我想声明一个变量,但显然,变量名不能是保留关键字之一。它可以是任何字母,后跟字母数字,语言关键字除外。

我做了以下(可在Pegjs Online中测试):

现在true是非法的,但true1不是。这可以。但是,由于我已经boolean在我的语言的其他地方定义了结构,是否不能重用该定义而不是手动重新定义我的variable定义中不允许的关键字?

你可以想象为什么我的解决方案容易出错。我尝试了几件事,但没有奏效。

谢谢你的帮助!

0 投票
1 回答
1177 浏览

javascript - PEGJS: How to add NOT (!) logical operator to grammar that parses AND (&&) OR (||) logic statements

I'm very new to writing grammars (first time ever to be exact) and I'd like to create a grammar that can return an AST for basic logic statements. So far I have a grammar that can handle AND, OR logic (I simply modified the basic calculator example from the official pegjs site). Here is what the grammar currently does:

The statement

Returns the AST

The statement:

Returns the AST

Here is the grammar that I have so far. It can be pasted directly into the online pegjs parser (http://pegjs.majda.cz/online).

What I'd like to do is add support for the NOT operator (!). So for example I'd like to be able to parse the following statements:

My first question is how do you typically represent the NOT operator in an AST? It seems like the not operator can be applied to an entire left or right branch of the AST but unlike OR and AND will not have both a left and right branch.

The second question is how do you support a NOT operator in a PEGJS grammar?

Thank you very much for your time!

Edit: Fixed the AST representation

0 投票
3 回答
438 浏览

pegjs - 如何使用 PEGjs 进行验证,而不是解析?

我有以下 PEGjs 作品:

true如果我的输入字符串匹配Name,我想以某种方式得到,false否则。我也不关心解析出组件。

但是,如果匹配失败,PEGjs 确实想抛出异常。

我当然可以将它包装在 try/catch 中,但我更愿意避免这种情况。而且我也想避免收集已解析的组件(即,["a", ["b", "c", "d"]]匹配"abcd"时我不需要,我只需要true)。

是否有一些隐藏的 PEGjs 功能可以使这项工作?也许是一个聪明的动作,或者是对组合器的创新使用?

或者也许我应该使用完全不同的工具,而不是解析器生成器?如果是这样,有人知道我应该使用什么吗?

0 投票
1 回答
481 浏览

grammar - Peg.JS 中的左递归错误

我目前正在为科学博览会制作一种编程语言。

这是我的 PEG.js 语法:

我收到以下错误:“检测到规则'语句'的左递归。” 但我无法弄清楚为什么会发生这种情况。

0 投票
2 回答
441 浏览

javascript - 阻止 peg.js 抛出 ParseError

我可以让 PEG.js 返回默认值而不是引发解析错误吗?

基本上我想拥有

在我的语法中,但如果任何规则部分数学,它仍然会引发 Parse 错误。

所以

仍然会在“546aueu”上引发解析错误。试试http://pegjs.org/online

我怎么能告诉解析器返回一些东西而不是抛出错误。

据我所知,它应该尝试匹配第一条规则,如果失败,它应该匹配第二条规则。

感谢任何帮助和建议。

0 投票
1 回答
630 浏览

javascript - 您如何解析 pegjs 中的嵌套注释?

我想知道您如何在 pegjs 中解析评论(例如,la Haskell)。

目标:

例如,以下内容不应解析:

但是你也应该有一个逃生机制:

这种排序看起来像是解析 s-expressions,但使用 s-expressions 很容易:

因为关闭括号只是一个字符,我不能用胡萝卜“不”它。顺便说一句,我想知道如何“不”比 pegjs 中的单个字符长的东西。

谢谢你的帮助。