Pegged: Syntax Highlighting

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Mar 13 17:09:31 PDT 2012


On 3/11/12, Philippe Sigaud <philippe.sigaud at gmail.com> wrote:
> Parsing Expression Grammar (PEG) generator in D.

Slightly off-topic and not directly aimed at Philippe, but I'm
curious, how would one use a parser like Pegged for syntax
highlighting? I know my way around painting and using style bits for
coloring text (e.g. I have a D port of Neatpad I play around with),
but I'm not sure how I would combine this with the parser. Maybe
someone more knowledgeable (Rainer Schuetze from VisualD perhaps? :) )
could chime in.

For example, I can set style bits for a certain range of text, e.g.
the style bits for this text:
x( int var);
might be (where S designates Style):
S.Text, S.Blank, S.OpenParen, S.Type, S.Type, S.Type, S.Blank...

Well you get the picture. To set those style bits I do need to run a
parser on the input string. So let's say Peg gives me back this
structure (I'm just guessing what the structure looks like):

ParseTree("Function", ...)
  .children -> ParseTree("BuiltInType", ...)
      .children -> ParseTree("ParamDecl")

This is all good so far, I can extract the needed info for the style
bits, but what I don't have are the offsets into the string at which a
ParseTree starts and ends. So I wouldn't know at which offset to put a
set of style bits.

I can't tell which parts of Pegged are the API and which are internal
functions, but maybe I could use some specific functions instead of
just calling .parse()., and then walk through the string and set each
style bit.

Anyway I think this is an interesting topic worth talking about. :)


More information about the Digitalmars-d-announce mailing list