# 语法高亮

**语法高亮**（syntax highlighting）是指在编辑器或渲染器中显示源代码时，针对关键字、字符串、注释等各类文本采用不同的颜色和字体样式，使人一眼便能看清程序的结构。[Visual Studio Code](https://www.edgepedia.org/zh/visual-studio-code) 团队称之为“让文本编辑器变成代码编辑器的那一项功能”。<sup>[1](https://code.visualstudio.com/blogs/2017/02/08/syntax-highlighting-optimizations)</sup> 代码文本更多是为人而写，而非为运行它的机器而写，因此着色是一种可读性手段，而非功能性手段。<sup>[2](https://visualstudiomagazine.com/articles/2014/08/01/semantic-code-highlighting.aspx)</sup>

| 关键事实 | 详情 |
|---|---|
| 目的 | 将关键字、字符串和注释着以不同颜色，使代码的浏览与解析变得容易得多。<sup>[3](https://www.navicat.com/en/company/aboutus/blog/3935-from-sql-beautifiers-to-intelligent-autocomplete-a-history-of-developer-productivity-tools.html)</sup> |
| 最早的研究源头 | 威尔弗雷德·汉森（Wilfred Hansen）于20世纪60年代在斯坦福大学为他的博士论文开始开发 Emily 编辑器；其1971年的摘要描述了一个面向结构化文本的编辑器，提供符合语法的选项，类似现代的代码补全。<sup>[2](https://visualstudiomagazine.com/articles/2014/08/01/semantic-code-highlighting.aspx)</sup> |
| 主流编辑器格式 | TextMate 的 `.tmLanguage` 语法——一种用声明式属性列表描述语言语法的格式——无意中成为了开放标准，其使用范围远远超出了 TextMate 本身。<sup>[4](https://www.thingshavehistory.com/posts/textmate/)</sup> |
| 语法高亮与语义高亮 | 语法高亮按词法规则为文本着色；语义高亮则借助语言服务提供的符号信息对其加以丰富，两者同时适用时，语义词元优先。<sup>[5](https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview/6d015aea2faa5132583928a5a011e99a8962386f)</sup> |
| Web 库规模 | highlight.js 支持193种语言和516种主题，具备自动语言检测，且零依赖。<sup>[6](https://highlightjs.org/)</sup> |
| 研究前沿 | 面向即时场景的神经网络高亮器达到了近乎完美的准确率，在三种主流语言上均优于基于正则表达式的方案（ESEC/FSE 2022）。<sup>[7](https://arxiv.org/abs/2208.02815)</sup> |

## 起源

语法感知的编辑器功能可追溯至20世纪60年代，当时威尔弗雷德·汉森（Wilfred Hansen）为他在斯坦福大学的博士论文开始构建 Emily 代码编辑器。Emily 使用层级文本，在编辑时向程序员提供符合语法的选项，类似于如今所说的代码补全；报告的研究结果是，用户使用 Emily 耗时略长，但犯的错误更少。<sup>[2](https://visualstudiomagazine.com/articles/2014/08/01/semantic-code-highlighting.aspx)</sup> 一位研究该功能的历史学者指出，最早的高亮代码很可能出现在印刷品中——书籍和杂志对读者输入的程式清单使用大写、粗体、斜体和不同字体。<sup>[2](https://visualstudiomagazine.com/articles/2014/08/01/semantic-code-highlighting.aspx)</sup>

**基于 Web 的高亮。** 另一条历史脉络是将高亮作为已发布代码的一项服务。奥列格·帕拉申科（Oleg Parashchenko）于2005年3月注册了 tohtml.com，并于2006年4月上线了“粘贴代码、获得彩色 HTML”的服务；该服务于2014年6月由新的维护者接手，此后不断扩展。<sup>[8](https://tohtml.com/history.php)</sup>

## 语法与编辑器

现代编辑器所继承的语法格式来自 TextMate——这是一款 macOS 编辑器，由艾伦·奥德高（Allan Odgaard）经过约五个月的开发，于2004年10月5日发布1.0版。2005年，[大卫·海涅迈尔·汉森](https://www.edgepedia.org/zh/david-heinemeier-hansson)（David Heinemeier Hansson）发布了一段15分钟的 Rails 录屏，观众不断暂停，只为看编辑器清爽的高亮和代码片段，结果成千上万的人在下载 Rails 之前先下载了 TextMate。<sup>[4](https://www.thingshavehistory.com/posts/textmate/)</sup> `.tmLanguage` 格式存续至今，是 Visual Studio Code 中的主要词元化引擎，其中的词法规则以正则表达式的形式表达，作用于单个文件。<sup>[5](https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview/6d015aea2faa5132583928a5a011e99a8962386f)</sup><sup> • </sup><sup>[9](https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide)</sup>

正则表达式语法并不完美：它们可能误判上下文，因此存在诸如 VS Code 中基于 Tree-sitter 的高亮器之类的扩展，专门用增量解析的完整语法树来取代 TextMate 的匹配方式。<sup>[10](https://github.com/EvgeniyPeshkov/syntax-highlighter)</sup> 出于性能考虑，编辑器采用增量重新词元化。在 VS Code 中，在某一行输入通常只会导致该行被重新词元化，因为词元化器返回的结束状态相同；只有当状态发生变化时，后续行才需要重绘。<sup>[1](https://code.visualstudio.com/blogs/2017/02/08/syntax-highlighting-optimizations)</sup>

## 语法高亮与语义高亮

词法规则只认得词元的形态，不知道某个名称的含义。语义高亮则加入来自语言服务的符号信息，该服务能理解整个项目：于是常量变量在整个文件中都被渲染为常量，而不仅仅是在其声明处，参数、属性和类名同样如此。<sup>[5](https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview/6d015aea2faa5132583928a5a011e99a8962386f)</sup> 在 VS Code 中（自1.43版引入），语义词元与 TextMate 词元合并，两者同时存在时语义词元获胜，而注释、字符串等基本分类仍由语法负责。<sup>[5](https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview/6d015aea2faa5132583928a5a011e99a8962386f)</sup> 语言扩展按语言或文件名注册语义词元提供程序，主题则选择加入以细化着色。<sup>[9](https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide)</sup>

## 生态系统与权衡

高亮引擎如今按使用场景分化。编辑器级引擎偏向准确性：Shiki 以更大的运行时和异步设置为代价，用 TextMate 语法提供类 VS Code 的准确性；而 highlight.js 则以自动语言检测为特色，偏向广泛的覆盖范围。<sup>[11](https://tanstack.com/highlight/latest/docs/comparison)</sup> 面向博客和文档的轻量级库以速度和小体积输出为优先，牺牲了语法；在334个已提交的文档测试样本上，TanStack Highlight 预热后的高亮耗时4.6毫秒，而 Shiki 4.3.1 为182毫秒。<sup>[12](https://github.com/tanstack/highlight)</sup> 发布平台为读者嵌入了同样的机制：WordPress.com 的增强代码区块可为100多种语言应用彩色高亮，并支持行号、文件名显示和复制按钮。<sup>[13](https://wordpress.com/blog/2026/01/19/introducing-the-enhanced-code-block/)</sup>

**主题与无障碍。** 主题在很大程度上可跨编辑器移植，社区合集提供了针对 TextMate、Vim、Emacs、Pygments 等诸多工具的配色转换。<sup>[14](https://stylishthemes.github.io/Syntax-Themes/)</sup> 颜色选择与无障碍性相互作用：VS Code 附带了高对比度主题，并允许用户查找与色觉缺陷兼容的市场扩展。<sup>[15](https://code.visualstudio.com/docs/configure/accessibility/accessibility)</sup>

## 研究方向

当前研究由两个局限驱动。第一，基于正则表达式的高亮器与语言真正的词法分析器存在偏差，维护成本高，且无法表达复杂结构；在代码查看器、代码片段等即时场景中，形式化的语法分析代价高昂，且遇到无效代码便会失败。ESEC/FSE 2022 发表的一项神经方法，利用由语言自身的词法分析器和解析器构建的高亮预言机来训练模型，报告了近乎完美的准确率，在三种主流语言上均优于正则表达式方案。<sup>[7](https://arxiv.org/abs/2208.02815)</sup> 后续工作用卷积网络取代循环模型，以降低训练成本并提升评估性能。<sup>[16](https://arxiv.org/abs/2402.08754)</sup>

第二，一些研究者认为颜色是一种未被充分利用的信息渠道。计算机科学研究员马尔科·帕特里尼亚尼（Marco Patrignani）在 arXiv 的一篇立场论文中指出，颜色和字体的多样化能为写作带来清晰、简洁与正确，语法高亮有望成为传播更清晰、更易理解的研究成果的标准；该论文还回应了基于在 LaTeX 中应用高亮的种种缺陷而提出的批评，并提出了补救措施。<sup>[17](https://arxiv.org/abs/2001.11334)</sup>

## References

1. [Optimizations in Syntax Highlighting, Visual Studio Code blog](https://code.visualstudio.com/blogs/2017/02/08/syntax-highlighting-optimizations)
2. [Semantic Code Highlighting, Visual Studio Magazine (Terrence Dorsey, 2014)](https://visualstudiomagazine.com/articles/2014/08/01/semantic-code-highlighting.aspx)
3. [From SQL Beautifiers to Intelligent Autocomplete, Navicat blog](https://www.navicat.com/en/company/aboutus/blog/3935-from-sql-beautifiers-to-intelligent-autocomplete-a-history-of-developer-productivity-tools.html)
4. [TextMate: the grammar every editor learned to speak, Things Have History](https://www.thingshavehistory.com/posts/textmate/)
5. [Semantic Highlighting Overview, VS Code wiki](https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview/6d015aea2faa5132583928a5a011e99a8962386f)
6. [highlight.js](https://highlightjs.org/)
7. [On-the-Fly Syntax Highlighting using Neural Networks (Palma, Salza, Gall), arXiv:2208.02815](https://arxiv.org/abs/2208.02815)
8. [History of tohtml.com](https://tohtml.com/history.php)
9. [Semantic Highlight Guide, VS Code Extension API](https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide)
10. [syntax-highlighter VS Code extension based on Tree-sitter](https://github.com/EvgeniyPeshkov/syntax-highlighter)
11. [Comparison, TanStack Highlight Docs](https://tanstack.com/highlight/latest/docs/comparison)
12. [TanStack Highlight on GitHub](https://github.com/tanstack/highlight)
13. [Introducing the Enhanced Code Block, WordPress.com blog](https://wordpress.com/blog/2026/01/19/introducing-the-enhanced-code-block/)
14. [Syntax Highlighting themes collection](https://stylishthemes.github.io/Syntax-Themes/)
15. [Accessibility, VS Code documentation](https://code.visualstudio.com/docs/configure/accessibility/accessibility)
16. [On-the-Fly Syntax Highlighting: Generalisation and Speed-ups, arXiv:2402.08754](https://arxiv.org/abs/2402.08754)
17. [Why Should Anyone use Colours? or, Syntax Highlighting Beyond Code Snippets (Patrignani), arXiv:2001.11334](https://arxiv.org/abs/2001.11334)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 25, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 25, 2026*

Topic: 社会与历史 › 语言与语言学 › 语言学 › Formal and computational linguistics › Syntax highlighting and source presentation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/syntax-highlighting

English original: https://www.edgepedia.org/syntax-highlighting
