[plt-scheme] Parentheses and color

From: hendrik at topoi.pooq.com (hendrik at topoi.pooq.com)
Date: Wed Jul 4 10:07:54 EDT 2007

On Wed, Jul 04, 2007 at 08:24:07AM +0200, Jos Koot wrote:
> Hi
> I never count parentheses, but frequently watch the highlighting of 
> subexpressions. DrScheme realy makes it easy. When typing a sequence of 
> closing parentheses I simply check that the start of the highlighting 
> correctly skips back one level of indentation for each closing parenthesis. 
> The same method can be used to quickly spot a missing closing parenthesis. 
> The method works perfect even if the parentheses would not be visible.
> Jos koot

Invisible parentheses.  Interesting.  C++ can also be quite readable 
without curly brackets.  (we,,. almost without).  Here's a fragment 
(it relies on another program that parses arbitrary text based 
*only* on indentation, giving a tree structure of lines) from 
a program that puts the brackets back in:


typedef char Char; // we use strdup, so small charset
typedef unsigned int nat;


struct Line
  int indent;
  struct Line * mother;
  struct Line * sister;
  struct Line * daughters;
  nat length;
  nat lineno;
  Char * contents;
;

Line * readlines();

#include <stdio.h>
#include "readindentx.hh"

bool line_directives = true;
int outlineno = 0;

void nl(int count)
  printf("\n");
  int i = count;
  while(i > 1)
    printf("    ");
    --i;
  if(count > 0) printf("  ");
  ++outlineno;

void print(Line * line, int nest)

  if(line_directives && line->lineno != outlineno)
    if(line->lineno == outlineno + 1)
      printf("\n");
    else
      printf("\n#line %d", line->lineno);

    outlineno = line->lineno;
  if(line->contents && line->length != 0)
    nl(nest);
    printf("%s", line->contents);
    
  if(line->daughters != NULL)
    if(nest >= 0)
      nl(nest);
      if(nest > 0) printf("  ");
      printf("{");
    for(Line * d = line->daughters; d != NULL; d = d->sister)
      print(d, nest + 1);
    if(nest >= 0)
      nl(nest);
      if(nest > 0) printf("  ");
      printf("}");

void print(Line * line)
  print(line, -1);

int main()
  Line * l = readlines();
  print(l);
  nl(0);
  return 0;


-- hendrik



Posted on the users mailing list.