如何使用Infragistics语法解析引擎? [英] How to use infragistics syntax parsing engine?

查看:119
本文介绍了如何使用Infragistics语法解析引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用基础设施语法分析引擎来执行以下操作:

I want to make use of infragistics syntax parsing engine to do something like this:

  1. 用户以EBNF格式定义语言的语法.
  2. 上面定义的语法用于验证文本框中的用户输入是否有效(即匹配或可以匹配上面的语法).

我已经阅读了他们拥有的所有文档,但看不到第一步是什么好. 任何帮助将不胜感激.

I have read all the documentation they have and I cannot see what a good first step would be. Any help will be appreciated.

推荐答案

我正在研究Infragistics的解析引擎,这听起来像是一种完美的使用方式.框架创建的解析器类型完全基于Grammar实例,并且可以从EBNF文件创建此类.我们有一种自定义格式,可以在文件开头或每个非终结符号定义之前按特殊顺序使用这些格式,以分别为语法或非终结符号提供属性,但是也可以识别没有这些特殊属性的常规EBNF. .有关此格式的帮助,可以查看以下链接: http://help.infragistics.com/doc/WPF/2013.2/CLR4.0/?page=IG_SPE_EBNF_Format.html .有关框架的一般帮助,请参见以下链接: http://help.infragistics.com/doc/WPF/2013.2/CLR4.0/?page=IG_SPE.html .

I work on the parsing engine for Infragistics and this sounds like a perfect way to use it. The type of parser created by the framework is based entirely on a Grammar instance, and this class can be created from an EBNF file. We have a custom format that can be used in special sequences at the start of the file or before each non-terminal symbol definition to provide attributes to the grammar or non-terminal symbol, respectively, but normal EBNF without these special attributes is also recognized. For help on this format, you can view this link: http://help.infragistics.com/doc/WPF/2013.2/CLR4.0/?page=IG_SPE_EBNF_Format.html. And for help on the framework in general, see this link: http://help.infragistics.com/doc/WPF/2013.2/CLR4.0/?page=IG_SPE.html.

要创建包含要解析文档的语法定义的语法实例,可以使用静态Grammar.LoadEbnf(…)方法之一从EBNF文件或原始EBNF内容创建语法实例,或者您可以可以通过使用代码添加终端符号和非终端符号来手动构建语法.出于您的目的,听起来最好的方法是使用LoadEbnf方法,然后将其传递给用户指定的EBNF.

To create a Grammar instance containing the grammar definition of the documents to be parsed, you can either use one of the static Grammar.LoadEbnf(…) methods to create a Grammar instance from an EBNF file or raw EBNF content, or you can build the grammar up manually by adding terminal and non-terminal symbols using code. For your purposes, it sounds like the best approach would be to use the LoadEbnf method and just pass it the EBNF specified by the user.

一旦创建了语法实例,就需要为其创建解析器.当语法被包装在LanguageBase派生类中时,这全部在内部进行处理.您可以使用代码生成器写出特定于语法的派生语言类(通过LanguageGenerator.GenerateClass方法),也可以创建新的CustomLanguage实例,并将语法传递给其构造函数.对于您的目的,听起来后一种选择是最好的.这样一来,您就可以在运行时阅读EBNF并创建语言实例,而无需生成代码并重新编译.

Once your Grammar instance is created, you need to create the parser for it. This is all handled internally when the Grammar is wrapped in a LanguageBase derived class. You can either use a code generator to write out a derived language class specific to your grammar (via the LanguageGenerator.GenerateClass method) or you can create a new CustomLanguage instance, passing the Grammar to its constructor. For your purposes, it sounds like the latter option is the best. This allows you to read EBNF and create a language instance at runtime, without having to generate code and re-compile.

一旦有了一个表示语法和解析器的LanguageBase实例,您所需要做的就是在TextDocument的Language属性中指定它.如果您有XamSyntaxEditor,用户可以在其中键入输入,则此编辑器的Document属性将是TextDocument实例,并且您将在此实例上设置Language属性.然后,编辑器将根据语法定义自动显示内容不正确的错误.如果没有编辑器,或者想通过编程控制更多的错误检测方法,则可以创建一个TextDocument,使用InitializeText方法设置其文本,使用Parse()或ParseAsync()方法来解析文本,然后获取使用SyntaxTree属性解析的内容.

Once you have a LanguageBase instance representing your Grammar and parser, all you need to do is specify it on a TextDocument’s Language property. If you have a XamSyntaxEditor in which the user can type input, the Document property of this editor will be a TextDocument instance and you would set the Language property on this instance. Then the editor will automatically show errors for content which is not correct based on the grammar definition. If you do not have an editor, or you would like more programmatic control over detecting errors, you can create a TextDocument, sets its text using the InitializeText method, parse the text using the Parse() or ParseAsync() methods, and then get the parsed content using the SyntaxTree property.

SyntaxTree在与语法定义完全匹配的结构中保留了已解析文本的树状表示形式(用户输入中缺少的任何必需内容都将插入错误节点来表示该内容,而任何意外内容将在树中下一个节点的最前面的忽略内容集合中).在此SyntaxTree中,您可以访问RootNode.ContainsDiagnostics属性以查看整个树中是否存在任何错误(也可以在任何内部节点上使用ContainsDiagnostics来查看该节点上或内部是否存在错误).而且,您可以在节点上使用GetDiagnostics方法来枚举该节点上或该节点内的所有错误.

The SyntaxTree holds a tree-like representation of the parsed text in a structure that matches the grammar definition exactly (any required content which is missing from the user’s input will have error nodes inserted to represent that content, and any unexpected content will be in the leading ignored content collection of the next node in the tree). From this SyntaxTree, you can access the RootNode.ContainsDiagnostics property to see if there are any errors in the entire tree (you can also use ContainsDiagnostics on any interior node to see if there are errors on or within that node). And you can use the GetDiagnostics method on a node to enumerate all errors on or within that node.

我希望我能提供帮助.让我知道是否有任何不清楚的地方,或者您想了解有关任何事情的更多信息.另外,请查看我的博客以获取有关解析的更多信息: http://www.infragistics.com/社区/博客/mike_dour/

I hope I was able to help. Let me know if anything is unclear or you would like more information about anything. Also, check out my blog for more info on parsing: http://www.infragistics.com/community/blogs/mike_dour/

这篇关于如何使用Infragistics语法解析引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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