为文本编辑器定义语法的最佳方法是什么

为文本编辑器定义语法的最佳方法是什么

本文介绍了为文本编辑器定义语法的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自给自足地为Mac编写一个开源文本编辑器,终于达到了我要添加语法突出显示的地步.在过去的几天里,我一直在反复研究各种解决方案,最后我决定向更广泛的受众开放这个问题.

I'm masochistically writing an open-source text editor for Mac and have finally reached the point at which I want to add syntax highlighting. I've been going back and forth on various solutions for the past few days, and I've finally decided to open the question to a wider audience.

以下是我看到的选项:

  • 基本上通过一系列 regex模式匹配来定义语言(类似于TextMate定义其语言的方式)
  • 使用形式语法定义语言,例如BNF或PEG
  • Define languages basically with a series of regex pattern matching (similar to how TextMate defines its languages)
  • Define languages with a formal grammar like BNF or PEG

使用正则表达式模式匹配似乎不太理想,因为它不能正式代表一种语言,几乎不能代表一种形式语法.但是,一些不太正式的语言将很难适应BNF(例如Markdown -尽管我知道有一个很棒的 PEG实施).

Using regex pattern matching seems less than ideal as it cannot formally represent a language nearly as well as a formal grammar; however, some less formal languages will have a hard time fitting into BNF (i.e. Markdown -- though I know there's a great PEG implementation).

实时语法突出显示的性能折衷是什么?各种语言的灵活性又如何呢?

What are the performance tradeoffs for live syntax highlighting? What about flexibility for a wide range of languages?

如果我走BNF路线,Todd Ditchendorf创建了很棒的 ParseKit 框架,该框架可以很好地工作-盒子.任何人都知道与PEG类似的东西吗?

If I go the BNF route, Todd Ditchendorf created the awesome ParseKit framework which would work nicely out-of-the-box. Anyone know of any anything similar for PEG's?

推荐答案

除非您想争取获得完全免费的(或更糟糕的是,完全依赖上下文的)完整语法的战斗,否则它对每种您想要的语言都是完全正确的处理(或更糟糕的是,对于您要处理的每种语言的方言……有多少种C ++?),出于语法突出显示的目的,您最好放弃完全正确的做法,并接受有时候您会弄错的.在这种情况下,正则表达式似乎是一个非常好的答案.它们也可以非常快,因此不会干扰进行编辑的人.

Unless you want to fight the battle of getting a full-context free (or worse, a full context-sensitive) grammar completely correct for every language you want to process (or worse, for every dialect of the language you want to process... how many kinds of C++ are there?), for the purposes of syntax highlighting you're probably better giving up on complete correctness and accept that sometimes you'll get it wrong. In that case, regexps seem like an extremely good answer. They can also be very fast, so they won't interfere with the person doing the editing.

如果您坚持要进行完整的语法检查/完成(我认为您不是),那么您将需要完整的语法.为真实语言制作编辑器还将花费很长时间.

If you insist on doing full syntax checking/completion (I don't think you are), then you'll need that full grammar. You'll also be a very long time in producing editors for real languages.

有时候最好不要太认真.您可以获得的98%的解决方案要好于从未实现的100%的解决方案.

Sometimes it is better not to be too serious. A 98% solution that you can get is better than a 100% solution that never materializes.

这篇关于为文本编辑器定义语法的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:38