Syntax highlighting is the most efficient and professional way.

Question:
I want code for: syntax highlighting (programming languages)
Language: C # or x86 assembly (preferably C #)
Platform: Windows
Qualification: the most effective implementation is possible / most professionally / as large corporations such as Microsoft do.
Rephrased: How to implement syntax highlighting in C # for Windows in the most efficient way currently known?


Development (feel free to skip - no need to answer the question :)):
I do not want to just implement it - I have already seen a few.
I would like to know how Microsoft does this so well on Visual Studio (depending on version).

People keep trying to invent the wheel when it comes to syntax highlighting. I do not understand why.
Is this considered a very difficult problem? I saw implementations that only emphasize what is currently displayed on the screen, I think the way is ... (he used some kind of smart API to find out which lines of the text field are actually showing).
I also saw implementations using RichTextBox, and I think that this is not the way (maybe I am not here) - I think it’s something like a subclass of a subroutine that draws text in a regular text field and changes its brushes, maybe better (maybe I I saw it somewhere - I doubt that I will think about it too)
In addition, I heard that some people implement it with AST in the same way that the compiler will be encoded (part of the lexer, I think?) - I hope this enumeration - I do not consider it effective. (uneducated guess)

If this is really a serious problem, then how does the big case always get it right? I have never heard of the possibility of breaking syntax highlighting in Visual Studio, for example.
But any other tool that implements it does it badly or worse than big guys.
What is the official “this is the best way and any other way is the less effective” way to do this?

I really have no evidence that the Microsoft method is better, but, seeing that they probably know more about the Windows API than anyone else, I would suggest that the implementation method is better (I would like to be wrong - imagine myself being able to say that my syntax highlighting implementation is better than MS!)

Sorry for the scattered development.
Also, I apologize in advance for any faux-pas - this is my first question.

+4
source share
3 answers

I don’t think that “this is the best way and any other way is the less effective” way to do it. In fact, I do not think that efficiency is the main problem. Rather, complexity. A good parser marker is based on a good parser. While you can analyze the code, you can select each part of it in any way. But what happens when the code is poorly formed? Many syntax markers simply highlight keywords and several block structures to solve this problem. By doing this, they can use simple regular expressions instead of having a full syntax-tolerant parser (which is what Visual Studio has).

+1
source

The best way is probably reusing an existing one, such as ScintillaNET .

+1
source

As with any code .... rarely is the "best" way. There are several ways to do things, and each of them has advantages and disadvantages.

However, some form of template interpreter is probably the most common way. According to the GoF book :

The interpreter pattern is widely used in compilers implemented using object-oriented languages, since the Smalltalk Compilers. SPECTalk uses a template to interpret the description of input file formats. QOCA Restriction Toolkit uses it to evaluate constraints.

It also refers to limitations in the applicability section.

  • grammar is simple. For complex grammars, the class hierarchy for grammars becomes large and unmanageable. Tools, such as parser generators, are the best alternative in such cases
  • Efficiency is not a serious problem. Most Effective Translators are usually not implemented by interpreting parsing trees, but first translating them into another form. For example, regular expressions are often transformed into state machines. But even then, the translator may be a template implemented by the translator, so the template is still applicable.

Understanding this, you should now know why it is better to precompile your reusable RegEx before doing many matches with it. If you do not, each time you will have to perform both actions (conversion, interpretation), and not create a state machine once and apply it several times.

In particular, for the type of interpretation you are describing, Microsoft provides the Microsoft.VisualStudio namespace and all its powerful features as part of the Visual Studio SDK . You can also look at System.CodeDOM for generating and compiling dynamic code.

0
source

Source: https://habr.com/ru/post/1303679/


All Articles