使用 Gold Parser 解析项目和包文件 --help 需要“IdList" [英] Parsing Project and Package files using Gold Parser --help needed with 'IdList'

查看:48
本文介绍了使用 Gold Parser 解析项目和包文件 --help 需要“IdList"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Object Pascal Engine(作者:Rob van den Brink) 并且它似乎适用于 Delphi 单元文件(除了一些轻微且易于纠正的错误).

I am dabbling with the Object Pascal Engine (by Rob van den Brink) and it seems (except for a few minor and easily correctable errors) it works for Delphi unit files.

然而,它在解析项目(.dpr)和包(.dpk)文件时出现问题;问题基本上归结为单元和项目中使用"可能具有的内容之间的差异(以及包中包含"子句可能具有的内容).

However, it has problems parsing Project (.dpr) and Package (.dpk) files; and the issue basically boils down to the differences between the stuff that 'uses' can have in units and projects (as well as what 'contains' clause can have in packages).

让我举个简单的例子:

在单元 (.pas) 文件中,'uses' 子句可以是这样的

In a unit (.pas) file, the 'uses' clause can be something like this

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  StdCtrls,
  ExtCtrls,
  ComCtrls;

而在项目 (.dpr) 文件中

whereas, in a Project (.dpr) file

uses
  Forms,
  UnitDemoMain in 'UnitDemoMain.pas' {Form1},
  SomeUnit in '..\SomeUnit.pas',
  SomeOtherUnit;

然而,相同的功能(以包含"的名义)出现如下:

Yet, the same functionality (in the name of 'contains') occurs as:

contains
  OneUnit in 'OneUnit.pas',
  AnotherUnit in '..\AnotherUnit.pas';

我拥有的语法文件的问题(来自上面的链接)是它只处理最简单的情况(即在单元文件中出现使用"的方式),并为其他人抛出错误.

The problem with the grammar file I have (from the above link) is that it only handles the most simple case (i.e. the way 'uses' occurs in unit files), and throws error for others.

我猜这归结为语法文件中IdList"的定义方式,即:

I am guessing it boils down how 'IdList' is defined in the grammar file, which is this:

<IdList> ::= <IdList> ',' <RefId>
| <RefId>

那么,我的问题是:如何更改此定义,以便它可以处理其他替代方案(如 Project 和 Pacckage 文件中所示),即:

My question, then, is: How do I alter this definition, so that it can handle other alternatives (as seen in Project and Pacckage files), i.e.:

UnitDemoMain in 'UnitDemoMain.pas' {Form1},
OneUnit in 'OneUnit.pas';

推荐答案

我自己还没有用过 Gold 包,不过 Yacc 用过不少;语法布局略有不同,但原理是一样的.

I haven't used the Gold package myself yet, but I have used Yacc quite a bit; that has a slightly different grammar layout but the principle is the same.

对于初学者,我会尝试如下修改 Delphi 语法:

For starters I would try modifying the Delphi grammar as follows:

改变

<UsesClause>        ::= USES <IdList> ';'
              | SynError

<UsesClause>        ::= USES <UnitList> ';'
              | SynError

并添加

<UnitList>      ::= <UnitList> ',' <UnitRef>
              | <UnitRef>

<UnitRef>       ::= <RefID>
              | <RefID> IN <StringLiteral>
!                 | <RefID> in <StringLiteral> Comment Start <RefID> Comment End

我用感叹号注释掉的那行最初是为了处理你的例子中的这个结构:

The line which I've commented out using the exclamation mark was initially intended to handle this construct in your example:

  UnitDemoMain in 'UnitDemoMain.pas' {Form1},

然而,Gold's Builder 似乎将左花括号和右花括号字符 { } 视为一种特殊情况,这似乎防止它们被用作包围评论​​以外的任何东西;我一直无法找到将它们用作语法规则的一部分的方法.希望此更改的结果是{Form1}"作为注释被简单地忽略,并且示例构造与之前的变体( IN StringLiteral")相匹配.

However, it seems that Gold's Builder treats the open- and close-curly-brace characters, { }, as a special case which seems to prevent them being used as anything other than to surround comments; I've been unable to find a way of using them as part of a grammar rule. The result of this change should hopefully be that '{Form1}' is simply ignored as a comment, and the example construct matches the previous variant ("<RefID> IN StringLiteral") instead.

Fwiw,Gold 看起来很不错,除了一些问题,包括

Fwiw, Gold looks quite a nice package, except for a few problems including

  • ReadMe 中提到的限制,它只能处理字符 0..127 和

  • the restriction mentioned in the ReadMe that it can only handle characters 0..127 and

它的 Parser Builder (v.5.2) 在使用它附带的 D7 示例语法(在我建议的更改之前)运行时抱怨无效的开始符号和行/状态 82 上的词法错误.也许我我错过了一些东西......

its Parser Builder (v.5.2) complains when running using the D7 sample grammar that comes with it (before my suggested changes) about an invalid start symbol and a lexical error on line/state 82. Maybe I've missed something ...

这篇关于使用 Gold Parser 解析项目和包文件 --help 需要“IdList"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆