You either parse the language completely, or just look for known patterns with e.g. regexes. There is no single method for all languages, each requires its own set of rules.
Parsing usually produces better quality, but regex rules are easier to produce.
发布评论
评论(3)
据我所知,最常见的方法是使用单行或多行正则表达式,具体取决于语言语法。
对于带有语法文件的 IDE,IDE 在首次加载相关文件时将文件编译为正则表达式。
The most common approach that I know of is the use of single- or multi-line regexes, depending on language syntax.
For IDEs with syntax files, the IDE compiles the file into a regex on first load of relevant file.
通常对源代码进行简单的词法分析,例如使用 regexp-s。
有时与编译器或其他语法解析器交互,以提供更有用的(语义)信息。
语法着色始终取决于语言(即文件扩展名)。
Usually doing simple lexing on the source code, e.h. with regexp-s.
Sometimes interacting with the compiler, or some other syntactic parser, to provide more useful (semantic) information.
The syntactic colorization is always dependent upon the language (i.e. the file extension).
您要么完全解析该语言,要么只是使用正则表达式等查找已知模式。没有适用于所有语言的单一方法,每种语言都需要自己的一套规则。
解析通常会产生更好的质量,但正则表达式规则更容易生成。
You either parse the language completely, or just look for known patterns with e.g. regexes. There is no single method for all languages, each requires its own set of rules.
Parsing usually produces better quality, but regex rules are easier to produce.