📄 patterns.html
字号:
<HTML><HEAD><TITLE> Customizing </TITLE></HEAD><BODY><A NAME="Highlighting_Patterns"></A><H2> Highlighting Patterns </H2><P><H3>Writing Syntax Highlighting Patterns</H3></P><P>Patterns are the mechanism by which language syntax highlighting isimplemented in NEdit (see <A HREF="syntax.html#Syntax_Highlighting">Syntax Highlighting</A> under the heading of Featuresfor Programming). To create syntax highlighting patterns for a newlanguage, or to modify existing patterns, select "Recognition Patterns" from"Syntax Highlighting" sub-section of the "Default Settings" sub-menu of the"Preferences" menu.</P><P>First, a word of caution. As with regular expression matching in general, itis quite possible to write patterns which are so inefficient that theyessentially lock up the editor as they recursively re-examine the entirecontents of the file thousands of times. With the multiplicity of patterns,the possibility of a lock-up is significantly increased in syntaxhighlighting. When working on highlighting patterns, be sure to save yourwork frequently.</P><P>NEdit's syntax highlighting is unusual in that it works in real-time (as youtype), and yet is completely programmable using standard regular expressionnotation. Other syntax highlighting editors usually fall either into thecategory of fully programmable but unable to keep up in real-time, orreal-time but limited programmability. The additional burden that NEditplaces on pattern writers in order to achieve this speed/flexibility mix, isto force them to state self-imposed limitations on the amount of context thatpatterns may examine when re-parsing after a change. While the "PatternContext Requirements" heading is near the end of this section, it is notoptional, and must be understood before making any any serious effort atpattern writing.</P><P>In its simplest form, a highlight pattern consists of a regular expression tomatch, along with a style representing the font an color for displaying anytext which matches that expression. To bold the word, "highlight", whereverit appears the text, the regular expression simply would be the word"highlight". The style (selected from the menu under the heading of"Highlight Style") determines how the text will be drawn. To bold the text,either select an existing style, such as "Keyword", which bolds text, orcreate a new style and select it under Highlight Style.</P><P>The full range of regular expression capabilities can be applied in such apattern, with the single caveat that the expression must conclusively matchor not match, within the pre-defined context distance (as discussed belowunder Pattern Context Requirements).</P><P>To match longer ranges of text, particularly any constructs which exceed therequested context, you must use a pattern which highlights text between astarting and ending regular expression match. To do so, select "Highlighttext between starting and ending REs" under "Matching", and enter both astarting and ending regular expression. For example, to highlight everythingbetween double quotes, you would enter a double quote character in both thestarting and ending regular expression fields. Patterns with both abeginning and ending expression span all characters between the twoexpressions, including newlines.</P><P>Again, the limitation for automatic parsing to operate properly is that bothexpressions must match within the context distance stated for the patternset.</P><P>With the ability to span large distances, comes the responsibility to recoverwhen things go wrong. Remember that syntax highlighting is called upon toparse incorrect or incomplete syntax as often as correct syntax. To stop apattern short of matching its end expression, you can specify an errorexpression, which stops the pattern from gobbling up more than it should. For example, if the text between double quotes shouldn't contain newlines,the error expression might be "$". As with both starting and endingexpressions, error expressions must also match within the requested contextdistance.</P><P><H4>Coloring Sub-Expressions</H4></P><P>It is also possible to color areas of text within a regular expressionmatch. A pattern of this type associates a style with sub-expressionsreferences of the parent pattern (as used in regular expression substitutionpatterns, see the NEdit Help menu item on <A HREF="regex.html#Regular_Expressions">Regular Expressions</A>). Sub-expressions of both the starting and ending patterns may be colored. Forexample, if the parent pattern has a starting expression "\<", and endexpression "\>", (for highlighting all of the text contained within anglebrackets), a sub-pattern using "&" in both the starting and ending expressionfields could color the brackets differently from the intervening text. Aquick shortcut to typing in pattern names in the Parent Pattern field is touse the middle mouse button to drag them from the Patterns list.</P><P><H4>Hierarchical Patterns</H4></P><P>A hierarchical sub-pattern, is identical to a top level pattern, but isinvoked only between the beginning and ending expression matches of itsparent pattern. Like the sub-expression coloring patterns discussed above,it is associated with a parent pattern using the Parent Pattern field in thepattern specification. Pattern names can be dragged from the pattern listwith the middle mouse button to the Parent Pattern field.</P><P>After the start expression of the parent pattern matches, the syntaxhighlighting parser searches for either the parent's end pattern or amatching sub-pattern. When a sub-pattern matches, control is not returned tothe parent pattern until the entire sub-pattern has been parsed, regardlessof whether the parent's end pattern appears in the text matched by thesub-pattern.</P><P>The most common use for this capability is for coloring sub-structure oflanguage constructs (smaller patterns embedded in larger patterns). Hierarchical patterns can also simplify parsing by having sub-patterns "hide"special syntax from parent patterns, such as special escape sequences orinternal comments.</P><P>There is no depth limit in nesting hierarchical sub-patterns, but beyond thethird level of nesting, automatic re-parsing will sometimes have to re-parsemore than the requested context distance to guarantee a correct parse (whichcan slow down the maximum rate at which the user can type if large sectionsof text are matched only by deeply nested patterns).</P><P>While this is obviously not a complete hierarchical language parser it isstill useful in many text coloring situations. As a pattern writer, yourgoal is not to completely cover the language syntax, but to generatecolorings that are useful to the programmer. Simpler patterns are usuallymore efficient and also more robust when applied to incorrect code.</P><P><H4>Deferred (Pass-2) Parsing</H4></P><P>NEdit does pattern matching for syntax highlighting in two passes. The firstpass is applied to the entire file when syntax highlighting is first turnedon, and to new ranges of text when they are initially read or pasted in. Thesecond pass is applied only as needed when text is exposed (scrolled in toview).</P><P>If you have a particularly complex set of patterns, and parsing is beginningto add a noticeable delay to opening files or operations which change largeregions of text, you can defer some of that parsing from startup time, towhen it is actually needed for viewing the text. Deferred parsing can onlybe used with single expression patterns, or begin/end patterns which matchentirely within the requested context distance. To defer the parsing of apattern to when the text is exposed, click on the Pass-2 pattern type buttonin the highlight patterns dialog.</P><P>Sometimes a pattern can't be deferred, not because of context requirements,but because it must run concurrently with pass-1 (non-deferred) patterns. Ifthey didn't run concurrently, a pass-1 pattern might incorrectly match someof the characters which would normally be hidden inside of a sequence matchedby the deferred pattern. For example, C has character constants enclosed insingle quotes. These typically do not cross line boundaries, meaning theycan be parsed entirely within the context distance of the C pattern set andshould be good candidates for deferred parsing. However, they can't bedeferred because they can contain sequences of characters which can triggerpass-one patterns. Specifically, the sequence, '\"', contains a double quotecharacter, which would be matched by the string pattern and interpreted asintroducing a string.</P><P><H4>Pattern Context Requirements</H4></P><P>The context requirements of a pattern set state how much additional textaround any change must be examined to guarantee that the patterns will matchwhat they are intended to match. Context requirements are a promise by NEditto the pattern writer, that the regular expressions in his/her patterns willbe matched against at least <line context> lines and <character context>characters, around any modified text. Combining line and characterrequirements guarantee that both will be met.</P><P>Automatic re-parsing happens on EVERY KEYSTROKE, so the amount of contextwhich must be examined is very critical to typing efficiency. The morecomplicated your patterns, the more critical the context becomes. To coverall of the keywords in a typical language, without affecting the maximum rateat which users can enter text, you may be limited to just a few lines and/ora few hundred characters of context.</P><P>The default context distance is 1 line, with no minimum characterrequirement. There are several benefits to sticking with this default. Oneis simply that it is easy to understand and to comply with. Regularexpression notation is designed around single line matching. To span linesin a regular expression, you must explicitly mention the newline character"\n", and matches which are restricted to a single line are virtually immuneto lock-ups. Also, if you can code your patterns to work within a singleline of context, without an additional character-range context requirement,the parser can take advantage the fact that patterns don't cross lineboundaries, and nearly double its efficiency over a one-line and 1-charactercontext requirement. (In a single line context, you are allowed to matchnewlines, but only as the first and/or last character.)<P><HR></P><P></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -