Jump to content

Syntax highlighting: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
mNo edit summary
Covidely (talk | contribs)
Include more history so we link back to 1969 origins rather than imagining that things began in 1985. Tie into structured editors.
Line 54: Line 54:


==History and limitations==
==History and limitations==

The ideas of syntax highlighting overlap significantly with those of [[Structure_editor|syntax-directed editors]]. One of the first such editor for code was Wilfred Hansen's 1969 code editor Emily.<ref name=hansen>{{cite conference | first = Wilfred J. | last = Hansen | title = User engineering principles for interactive systems | booktitle = Proceedings of the Fall Joint Computer Conference FJCC 39 | pages = 5623–532 | publisher = AFIPS | year = 1971}}</ref> It provided advanced language-independed code completion facilities, and unlike modern editors with syntax highlighting, actually made it impossible to create syntactically incorrect programs.

Other editors followed, for example, on Microcomputers, 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 and automatically indented code to match its structure.<ref name=macpascal>{{cite mailing list | url = http://lists.apple.com/archives/mpw-dev/2011/Oct/msg00000.html | title = A Trio of Historical Recollections | mailinglist = mpw-dev | date = 2011-10-10 | accessdate=11 March, 2012 | last = Allen |first = Dan |authorlink=Bruce Perens }}</ref>


The Live Parsing Editor (LEXX) was written for the [[VM (operating system)|VM]] operating system for the computerization of the [[Oxford English Dictionary]] in 1985 and 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.'' See: [http://www.research.ibm.com/journal/rd/311/ibmrd3101G.pdf LEXX &ndash; A programmable structured editor], Cowlishaw, M. F., ''IBM Journal of Research and Development'', Vol 31, No. 1, 1987, IBM Reprint order number G322-0151
The Live Parsing Editor (LEXX) was written for the [[VM (operating system)|VM]] operating system for the computerization of the [[Oxford English Dictionary]] in 1985 and 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.'' See: [http://www.research.ibm.com/journal/rd/311/ibmrd3101G.pdf LEXX &ndash; A programmable structured editor], Cowlishaw, M. F., ''IBM Journal of Research and Development'', Vol 31, No. 1, 1987, IBM Reprint order number G322-0151

Revision as of 20:50, 11 March 2012

HTML syntax highlighting

Syntax highlighting is a feature of some text editors that display text—especially source code—in different colors and fonts according to the category of terms.[1] This feature eases writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct. Highlighting does not affect the meaning of the text itself; it is intended only for human readers and editors.

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 or code folding, as aids to editing which are external to the language.

Practical considerations

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 one desires.

Syntax highlighting also helps programmers find errors in their program. For example, most editors highlight string literals in a different color. Consequently, spotting a missing delimiter is much easier because of the contrasting color of the text. Brace matching 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.

Some text editors can also export the color markup in a format that is suitable for printing or for importing into word-processing or other kinds of text-formatting software; for instance an HTML, colorized LaTeX, PostScript or RTF version of its syntax highlighting.

Multi-document editors

For editors that support more than one language, the user can usually specify the language of the text, such as C, LaTeX, HTML, 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 file that contains embedded JavaScript 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.

Examples

Below is a snippet of syntax highlighted C code:

/* Hello World */
#include <stdlib.h>
#include <stdio.h>

int main()
{
    printf("Hello World\n");
    return 0;
}

Below is another snippet of syntax highlighted C++ code:

// Create "windowCount" Window objects:
int windowCount = 10;
Window **windows = new Window *[max];
for (int i = 0; i < windowCount; ++i) {
    windows[i] = new Window();
}

In the C++ example, the editor has recognized the keywords int, new, and for. The comment 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 editor for code was Wilfred Hansen's 1969 code editor Emily.[2] It provided advanced language-independed code completion facilities, and unlike modern editors with syntax highlighting, actually made it impossible to create syntactically incorrect programs.

Other editors followed, for example, on Microcomputers, 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 and automatically indented code to match its structure.[3]

The Live Parsing Editor (LEXX) was written for the VM operating system for the computerization of the Oxford English Dictionary in 1985 and 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. See: LEXX – A programmable structured editor, Cowlishaw, M. F., IBM Journal of Research and Development, Vol 31, No. 1, 1987, IBM Reprint order number G322-0151

Since most text editors highlight syntax based on complex pattern matching heuristics rather than actually implementing a parser for each possible language, which could be prohibitively complex, the highlighting is almost never completely accurate. Moreover, depending on the pattern matching algorithms, the highlighting "engine" can become very slow for certain types of language structures. Some editors overcome this problem by 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".

However, modern language-specific IDEs (in contrast to text editors) generally perform actual language parsing so they can be completely accurate.

See the Programming features section of the Comparison of text editors article for a list of some editors that have syntax highlighting.

Syntax highlighting engines

Some popular syntax highlighting engines are:

References

  1. ^ See e.g., The Java Developer's Guide to Eclipse By Jim D'Anjou, Sherry Shavor, Scott Fairbrother, Dan Kehn, John Kellerman, Pat McCarthy Published by Addison-Wesley, 2004 ISBN 0321305027, 9780321305022 1136 pages
  2. ^ Hansen, Wilfred J. (1971). "User engineering principles for interactive systems". Proceedings of the Fall Joint Computer Conference FJCC 39. AFIPS. pp. 5623–532. {{cite conference}}: Unknown parameter |booktitle= ignored (|book-title= suggested) (help)
  3. ^ Allen, Dan (2011-10-10). "A Trio of Historical Recollections". mpw-dev (Mailing list). Retrieved 11 March, 2012. {{cite mailing list}}: Check date values in: |accessdate= (help); Unknown parameter |mailinglist= ignored (|mailing-list= suggested) (help)
  4. ^ "Ultraviolet Introduction". Retrieved 2010-09-10.