如何实现语法高亮?

发布于 2024-08-29 09:01:23 字数 303 浏览 3 评论 0原文

我正在开始一些学习,我想用 C++ 编写自己的文件语法突出显示。

谁能给我关于如何去做这件事的想法?

在我看来,当打开一个文件时:

  1. 需要对其进行解析并确定它是什么类型的源文件。信任扩展可能并不是万无一失的

  2. 一种了解哪些关键字/命令适用于哪种语言的方法

  3. 一种决定每种颜色的方法关键字/命令获取

我想在 OS X 上使用 C++ 或 Objective-C 执行此操作。

任何人都可以指导我如何开始使用这个吗?

I am embarking on some learning and I want to write my own syntax highlighting for files in C++.

Can anyone give me ideas on how to go about doing this?

To me it seems that when a file is opened:

  1. It would need to be parsed and decided what type of source file it is. Trusting the extension might not be fool-proof

  2. A way to know what keywords/commands apply to what language

  3. A way to decide what color each keyword/command gets

I want to do this on OS X, using C++ or Objective-C.

Can anyone provide pointers on how I might get started with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北凤男飞 2024-09-05 09:01:23

语法荧光笔通常不会超出词法分析,这意味着您不必将整个语言解析为语句、声明和表达式等等。您只需要编写一个词法分析器,这对于正则表达式来说相当容易。如果您还没有学习正则表达式,我建议您从学习正则表达式开始。总共需要 30 分钟。

您可能需要考虑使用 Flex(词法分析器生成器;https://github.com/westes/flex ) 作为学习练习。在 Flex 中实现一个基本的语法荧光笔应该很容易,它可以输出突出显示的 HTML 或其他内容。

简而言之,您将为 Flex 提供一组正则表达式以及如何处理匹配文本,生成器将贪婪地匹配您的表达式。您可以使词法分析器在独占状态之间进行转换(例如,进出字符串文字、注释等),如 Flex 常见问题解答。以下是用 Flex 编写的 C 词法分析器的规范示例: http: //www.lysator.liu.se/c/ANSI-C-grammar-l.html

制作可扩展的语法荧光笔将是您旅程的下一部分。虽然我绝不是 XML 的粉丝,但是还是看一下 Kate 语法高亮文件是如何定义的,比如 这个用于 C++ 。您的任务是弄清楚如何定义语法荧光笔,然后编写一个程序,使用这些定义来生成 HTML 或您喜欢的任何内容。

Syntax highlighters typically don't go beyond lexical analysis, which means you don't have to parse the whole language into statements and declarations and expressions and whatnot. You only have to write a lexer, which is fairly easy with regular expressions. I recommend you start by learning regular expressions, if you haven't already. It'll take all of 30 minutes.

You may want to consider toying with Flex ( the lexical analyzer generator; https://github.com/westes/flex ) as a learning exercise. It should be quite easy to implement a basic syntax highlighter in Flex that outputs highlighted HTML or something.

In short, you would give Flex a set of regular expressions and what to do with matching text, and the generator will greedily match against your expressions. You can make your lexer transition among exclusive states (e.g. in and out of string literals, comments, etc.) as shown in the flex FAQ. Here's a canonical example of a lexer for C written in Flex: http://www.lysator.liu.se/c/ANSI-C-grammar-l.html .

Making an extensible syntax highlighter would be the next part of your journey. Although I am by no means a fan of XML, take a look at how Kate syntax highlighting files are defined, such as this one for C++ . Your task would be to figure out how you want to define syntax highlighters, then make a program that uses those definitions to generate HTML or whatever you please.

遇到 2024-09-05 09:01:23

你可能想看看 GeSHI 是如何实现高亮显示等的。此外,它还有一大堆语言包含您想要的所有关键字的包。

You may want to look at how GeSHI implements highlighting, etc. In addition, it has a whole bunch of language packs that contain all the keywords you'll ever want.

或十年 2024-09-05 09:01:23

假设您使用 Cocoa 框架,您可以使用 UTI 来确定文件类型。

有关 API 的概述:

http://developer.apple.com/mac/library/documentation/FileManagement/Conceptual/understand_utis/understand_utis_intro/understand_utis_intro.html#//apple_ref/doc/uid/TP40001319-CH201 -SW1

已知 UTI 列表:

http://developer.apple.com/mac/library/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref /doc/uid/TP40009259-SW1

您可能最感兴趣的两个键是 kUTTypeObjectiveC PlusPlusSource 和 kUTTypeCPlusPlusHeader。

对于突出显示,您可能会发现此页面上的信息很有帮助,因为它讨论了 NSView 和临时属性的语法突出显示:

http://www.cocoadev.com/index.pl?ImplementSyntaxHighlightingUsingTemporaryAttributes

Assuming that you are using Cocoa frameworks you can use UTIs to determine the file type.

For an overview of the api:

http://developer.apple.com/mac/library/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html#//apple_ref/doc/uid/TP40001319-CH201-SW1

For a list of known UTIs:

http://developer.apple.com/mac/library/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1

The two keys are you probably most interested in would be kUTTypeObjectiveC​PlusPlusSource and kUTTypeCPlusPlusHeader.

For the highlighting you might find the information on this page helpful as it discusses syntax highlighting with an NSView and temporary attributes:

http://www.cocoadev.com/index.pl?ImplementSyntaxHighlightingUsingTemporaryAttributes

千里故人稀 2024-09-05 09:01:23

我认为 (1) 是不可能的,因为判断文件是否有效的 C++ 的唯一方法是通过 C++ 解析器运行它并查看它是否解析...但如果您使用它作为标准,则不能'不要对无法编译的代码进行操作,因为它是一个正在进行的工作,而您可能想要这样做。最好只是信任该扩展,因为我认为没有任何其他方法比这更好。

您可以在此处获取 C++ 关键字列表: http://www.cppreference.com/wiki/ keywords/start

颜色由您决定(或者如果您愿意,您可以将它们配置为可配置并将选择权留给用户)

I think (1) isn't possible, since the only way to tell if a file is valid C++ is to run it through a C++ parser and see if it parses... but if you used that as your standard, you couldn't operate on code that doesn't compile because it is a work-in-progress, which you probably want to do. It's probably best just to trust the extension, as I don't think any other method will work better than that.

You can get a list of C++ keywords here: http://www.cppreference.com/wiki/keywords/start

The colors are up to you (or if you want, you can make them configurable and leave the choice to the user)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文