Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theory of parsing and live syntax highlighting

I am trying to understand how I should implement live syntax highlight when processing a very big string. I'm quite confused. This is what I know:

(Supposing I have the function parsedString parseString(rawString))

  1. Call parseString(entireText) and replace the current string with the returned parsed (and styled, etc.) string on every text change. This seems a bad approach when handling big data.

  2. Someone suggested to analyze the edited range, and replace the current raw edited string with the parsed string parseString(editedRange).

Method (1) is clear enough. What I cannot understand is (2). When typing, for each character added to the string, the notification is fired, and a single character is being parsed (and returned as it is).

For example, if I want red selectors when parsing a .css file, how I can understand when there's a completed selector followed by a { that should be colored? I suppose there is some way to delay the parsing until there is a match. How do you implement this?

I'm not looking for a working application. A good explanation would be useful as well.

Thank you in advance.

like image 662
Donovan Avatar asked May 22 '11 13:05

Donovan


People also ask

What is meant by syntax highlighting?

Syntax highlighting is a feature of text editors that are used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms.

What is parsing explain with an example?

Parse is defined as to break something down into its parts, particularly for study of the individual parts. An example of to parse is to break down a sentence to explain each element to someone. verb.

How do I enable syntax highlighting in Notepad ++?

The Syntax Highlighter is short and easily modified by going to Language > Define your language and selecting choicescript in the User language menu at the top of the new window. This will cover editing the syntax highlighter in Notepad++.


1 Answers

To re-parse incremental changes, you'll need a lower-level API to your parser.

A parser has a state that changes as it processes the input. For example, first the parser might be skipping spaces, now it might be reading a number, later it might be building an abstract syntax tree for an expression. If you could take a snapshot of all that parser state information at milestone points in the input, then you could reparse an incremental change by starting at the last milestone before the change (and possibly stopping earlier if the state is identical at a milestone beyond that change).

For simple syntax highlighting, like many programmers editors do, this is the approach. Syntax highlighting requires little more than tokenization, so there isn't much state to capture. And many programming languages have plenty of opportunities for milestones, e.g., at the beginning of a new line. In those cases, you might not even need to actually save the parser state, as you might know that it's always the same at the beginning of a line.

So you need an API like:

parsedString parseIncrementally(parserState, rawString);
like image 100
Adrian McCarthy Avatar answered Nov 03 '22 00:11

Adrian McCarthy