Syntax Highlighting
   HOME

TheInfoList



OR:

Syntax highlighting is a feature of
text editor A text editor is a type of computer program that edits plain text. Such programs are sometimes known as "notepad" software (e.g. Windows Notepad). Text editors are provided with operating systems and software development packages, and can be us ...
s that are used for programming, scripting, or markup languages, such as
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
. The feature displays text, especially
source code In computing, source code, or simply code, is any collection of code, with or without comments, written using a human-readable programming language, usually as plain text. The source code of a program is specially designed to facilitate the wo ...
, in different colours and fonts according to the category of terms. This feature facilitates writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct. This feature is also employed in many programming related contexts (such as programming manuals), either in the form of colorful books or online websites to make understanding code snippets easier for readers. Highlighting does not affect the meaning of the text itself; it is intended only for human readers. Syntax highlighting is a form of secondary notation, since the highlights are not part of the text meaning, but serve to reinforce it. Some editors also integrate syntax highlighting with other features, such as
spell checking In software, a spell checker (or spelling checker or spell check) is a software feature that checks for misspellings in a text file, text. Spell-checking features are often embedded in software or services, such as a word processor, email client, el ...
or
code folding Code or text folding, or less commonly holophrasting, is a feature of some graphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of text ...
, as aids to editing which are external to the language.


Practical benefits

Syntax highlighting is one strategy to improve the readability and context of the text; especially for code that spans several pages. The reader can easily ignore large sections of comments or code, depending on what they are looking for. Syntax highlighting also helps programmers find errors in their program. For example, most editors highlight
string literal A string literal or anonymous string is a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally " bracketed delimiters", as in x = "foo", where "foo" is a string ...
s in a different color. Consequently, spotting a missing delimiter is much easier because of the contrasting color of the text.
Brace matching Bracket matching, also known as brace matching or parentheses matching, is a syntax highlighting feature of certain text editors and integrated development environments that highlights matching sets of brackets (square brackets, curly brackets, or ...
is another important feature with many popular editors. This makes it simple to see if a brace has been left out or locate the match of the brace the cursor is on by highlighting the pair in a different color. A study published in the conference PPIG evaluated the effects of syntax highlighting on the comprehension of short programs, finding that the presence of syntax highlighting significantly reduces the time taken for a programmer to internalise the semantics of a program. Additionally, data gathered from an eye-tracker during the study suggested that syntax highlighting enables programmers to pay less attention to standard syntactic components such as keywords.


Support in text editors

Some text editors can also export the coloured markup in a format that is suitable for printing or for importing into word-processing and other kinds of text-formatting software; for instance as a HTML, colorized LaTeX,
PostScript PostScript (PS) is a page description language in the electronic publishing and desktop publishing realm. It is a dynamically typed, concatenative programming language. It was created at Adobe Systems by John Warnock, Charles Geschke, Doug Br ...
or RTF version of its syntax highlighting. There are several syntax highlighting
libraries A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
or "engines" that can be used in other applications, but are not complete programs in themselves, for example the Generic Syntax Highlighter (GeSHi) extension for
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
. For editors that support more than one language, the user can usually specify the language of the text, such as C,
LaTeX Latex is an emulsion (stable dispersion) of polymer microparticles in water. Latexes are found in nature, but synthetic latexes are common as well. In nature, latex is found as a milky fluid found in 10% of all flowering plants (angiosperms ...
,
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
, or the text editor can automatically recognize it based on the file extension or by scanning contents of the file. This automatic language detection presents potential problems. For example, a user may want to edit a document containing: * more than one language (for example when editing an
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
file that contains embedded
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
code), * a language that is not recognized (for example when editing source code for an obscure or relatively new programming language), * a language that differs from the file type (for example when editing source code in an extension-less file in an editor that uses file extensions to detect the language). In these cases, it is not clear what language to use, and a document may not be highlighted or be highlighted incorrectly.


Syntax elements

Most editors with syntax highlighting allow different colors and text styles to be given to dozens of different lexical sub-elements of syntax. These include keywords, comments, control-flow statements, variables, and other elements. Programmers often heavily customize their settings in an attempt to show as much useful information as possible without making the code difficult to read. Called ''syntax decoration'', some editors also display certain syntactical elements in more visually pleasing ways, f.e. by replacing a pointer operator like -> in source code by an actual arrow symbol (→), or changing
text decoration Text may refer to: Written word * Text (literary theory), any object that can be read, including: **Religious text, a writing that a religious tradition considers to be sacred **Text, a verse or passage from scripture used in expository preachi ...
clues like /italics/, *boldface*, or _underline_ in source code comments by an actual ''italics'', boldface, or underlined presentation.


Examples

Below is a comparison of a snippet of C code: Below is another snippet of syntax highlighted
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
code: // Create "window_count" Window objects: const auto window_count = int; auto windows = std::array, max_window_count>; for (auto i = int; i < window_count; ++i) In the C++ example, the editor has recognized the
keyword Keyword may refer to: Computing * Keyword (Internet search), a word or phrase typically used by bloggers or online content creator to rank a web page on a particular topic * Index term, a term used as a keyword to documents in an information syst ...
s const, auto, int, and for. The
comment Comment may refer to: * Comment (linguistics) or rheme, that which is said about the topic (theme) of a sentence * Bernard Comment (born 1960), Swiss writer and publisher Computing * Comment (computer programming), explanatory text or informa ...
at the beginning is also highlighted in a specific manner to distinguish it from working code.


History and limitations

The ideas of syntax highlighting overlap significantly with those of syntax-directed editors. One of the first such editors for code was Wilfred Hansen's 1969 code editor, Emily. It provided advanced language-independent
code completion Autocomplete, or word completion, is a feature in which an application predicts the rest of a word a user is typing. In Android and iOS smartphones, this is called predictive text. In graphical user interfaces, users can typically press the tab ...
facilities, and unlike modern editors with syntax highlighting, actually made it impossible to create syntactically incorrect programs. In 1982, Anita H. Klock and Jan B. Chodak filed a patent for the first known syntax highlighting system, which was used in the Intellivision's
Entertainment Computer System The Entertainment Computer System (ECS) was an add-on peripheral for the Intellivision. It was Mattel Electronics' second attempt at creating a peripheral to upgrade the Intellivision into a home computer, and was rushed into production to appeas ...
(ECS) peripheral, released in 1983. It would highlight different elements of BASIC programs and was implemented in an attempt to make it easier for beginners, especially children, to start writing code. Later, the Live Parsing Editor (
LEXX ''Lexx'' (also known as ''LEXX: The Dark Zone Stories'' and ''Tales from a Parallel Universe'') is a science fiction television series created by Lex Gigeroff and brothers Paul Donovan (writer), Paul and Michael Donovan (producer), Michael Dono ...
) written for the VM operating system for the computerization of the ''
Oxford English Dictionary The ''Oxford English Dictionary'' (''OED'') is the first and foundational historical dictionary of the English language, published by Oxford University Press (OUP). It traces the historical development of the English language, providing a com ...
'' in 1985 was one of the first to use color syntax highlighting. Its ''live parsing'' capability allowed user-supplied parsers to be added to the editor, for text, programs, data file, etc. On
microcomputers A microcomputer is a small, relatively inexpensive computer having a central processing unit (CPU) made out of a microprocessor. The computer also includes memory and input/output (I/O) circuitry together mounted on a printed circuit board (PC ...
, ''MacPascal 1.0'' (October 10, 1985) recognized Pascal syntax as it was typed and used font changes (e.g., bold for keywords) to highlight syntax on the monochrome
compact Macintosh A Compact Macintosh (or Compact Mac) is an all-in-one Apple Mac computer with a display integrated in the computer case, beginning with the original Macintosh 128K. Compact Macs include the original Macintosh through to the Color Classic sold ...
and automatically indented code to match its structure. Some text editors and code formatting tools perform syntax highlighting using
pattern matching In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact: "either it will or will not be ...
heuristics A heuristic (; ), or heuristic technique, is any approach to problem solving or self-discovery that employs a practical method that is not guaranteed to be optimal, perfect, or rational, but is nevertheless sufficient for reaching an immediate, ...
(e.g.
Regular expressions A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" o ...
) rather than implementing a
parser Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term ''parsing'' comes from Lat ...
for each possible language. This can result in a text rendering system displaying somewhat inaccurate syntax highlighting and in some cases performing slowly. A solution used by text editors to overcome this problem is not always parsing the whole file but rather just the visible area, sometimes scanning backwards in the text up to a limited number of lines for "syncing". On the other hand, the editor often displays code during its creation, while it is incomplete or incorrect, and the strict parsers (like ones used in compilers) would fail to parse the code most of the time. Some modern, language-specific IDEs (in contrast to text editors) perform full language parsing which results in very accurate understanding of code. An extension of syntax highlighting was called ''"semantic highlighting"'' in 2009 by David Nolden for the open-source C++ IDE
KDevelop KDevelop is a free and open-source integrated development environment (IDE) for Unix-like computer operating systems and Windows. It provides editing, navigation and debugging features for several programming languages, and integration with buil ...
. For example, semantic highlighting may give local variables unique distinct colors to improve the comprehensibility of code. In 2014 the idea of colored local variables was further popularized due to a blog post by Evan Brooks, and after that, the idea was transferred to other popular IDEs like
Visual Studio Visual Studio is an integrated development environment (IDE) from Microsoft. It is used to develop computer programs including web site, websites, web apps, web services and mobile apps. Visual Studio uses Microsoft software development platfor ...
,
Xcode Xcode is Apple's integrated development environment (IDE) for macOS, used to develop software for macOS, iOS, iPadOS, watchOS, and tvOS. It was initially released in late 2003; the latest stable release is version 14.2, released on December 13, ...
, and others. Color in a user interface is less useful if the user has some degree of
color blindness Color blindness or color vision deficiency (CVD) is the decreased ability to color vision, see color or differences in color. It can impair tasks such as selecting ripe fruit, choosing clothing, and reading traffic lights. Color blindness may ...
.


See also

* Programming features in a
Comparison of text editors This article provides basic comparisons for notable text editors. More feature details for text editors are available from the '' Category of text editor features'' and from the individual products' articles. This article may not be up-to-date or n ...
*
Indent style In computer programming, an indentation style is a convention governing the indentation of blocks of code to convey program structure. This article largely addresses the free-form languages, such as C and its descendants, but can be (and often ...
* Secondary notation *
Structure editor A structure editor, also structured editor or projectional editor, is any document editor that is cognizant of the document's underlying structure. Structure editors can be used to edit hierarchical or marked up text, computer programs, diagrams, c ...
*
Parsing Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term ''parsing'' comes from L ...
* Solarized (color scheme)


References

{{DEFAULTSORT:Syntax Highlighting Highlighting User interface techniques Text editor features Articles with example C code