编写语法高亮显示语法、高亮

2023-09-08 12:22:59 作者:刺骨旳柔情。

我希望能写我自己的语法高亮一个夏天项目,我想工作,但我不知道怎么写我自己的语法高亮。

I was hoping to write my own syntax highlighter for a summer project I am thinking of working on but I am not sure how to write my own syntax highlighter.

我知道有一堆的实现在那里,但我想了解有关正前pressions以及如何语法高亮的作品。

I know that there are bunch of implementations out there but I would like to learn about regular expressions and how syntax highlighting works.

如何语法高亮工作,哪些是开发一款一些很好的参考?是否语法高亮扫描每一个字符,因为它是类型还是它扫描的文档/文本区域作为一个整体后,每个字符键入?

How does syntax highlighting work and what are some good references for developing one? Does the syntax highlighter scan each character as it is typed or does it scan the document/text area as a whole after each character is typed?

任何有识之士将大大AP preciated。

Any insight would be greatly appreciated.

感谢。

PS:我打算写它在ActionScript

PS: I was planning on writing it in ActionScript

推荐答案

语法荧光笔可以在两个非常普遍的方式工作。第一次实现了一个完整的词法和语法分析器的语言(S)被强调,正是标识每个记号的类型(关键字,类名,实例名,变量类型,preprocessor指令...)。这提供了所有需要按照一定标准完全彰显code中的信息(关键字红色,蓝色类名,你有什么)。

Syntax highlighters can work in two very general ways. The first implements a full lexer and parser for the language(s) being highlighted, exactly identifying each token's type (keyword, class name, instance name, variable type, preprocessor directive...). This provides all the information needed to exactly highlight the code according to some specification (keywords in red, class names in blue, what have you).

的第二种方法是这样一个谷歌code prettify 的员工,而不是实现一个词法/每种语言解析器,几个很一般的解析器使用,可以做一个体面的工作,在大多数语法的地方。此荧光笔,例如,将能够解析和突出相当良好任何类似C语言,因为它的词法/解析器可以识别那些种语言的一般组件。

The second way is something like the one Google Code Prettify employs, where instead of implementing one lexer/parser per language, a couple of very general parsers are used that can do a decent job on most syntaxes. This highlighter, for example, will be able to parse and highlight reasonably well any C-like language, because its lexer/parser can identify the general components of those kinds of languages.

这还具有,作为结果,就不必明确地指定语言,作为发动机将自行确定它的哪一个通用解析器能做的最好的工作的优点。当然,缺点是高亮比当一个特定语言的解析器使用较少的完善。

This also has the advantage that, as a result, you don't need to explicitely specify the language, as the engine will determine by itself which of its generic parsers can do the best job. The downside of course is that highlighting is less perfect than when a language-specific parser is used.