[PD-cvs] pd/src kernel.c,1.1.2.13,1.1.2.14

Mathieu Bouchard matju at users.sourceforge.net
Thu Dec 28 21:39:03 CET 2006


Update of /cvsroot/pure-data/pd/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6041

Modified Files:
      Tag: desiredata
	kernel.c 
Log Message:
new parser, second draft


Index: kernel.c
===================================================================
RCS file: /cvsroot/pure-data/pd/src/Attic/kernel.c,v
retrieving revision 1.1.2.13
retrieving revision 1.1.2.14
diff -C2 -d -r1.1.2.13 -r1.1.2.14
*** kernel.c	28 Dec 2006 18:30:17 -0000	1.1.2.13
--- kernel.c	28 Dec 2006 20:39:01 -0000	1.1.2.14
***************
*** 1784,1891 ****
  }
  
! /* convert text to binbuf, matju-style */
! void binbuf_text_matju(t_binbuf *x, char *text, size_t size) {
!     char buf[MAXPDSTRING+1], *bufp, *ebuf = buf+MAXPDSTRING;
!     const char *textp = text, *etext = text+size;
!     binbuf_clear(x);
!     x->n = 0;
!     while (1) {
! 	char *token;
! 	while (textp!=etext && isspace(*textp)) textp++;
! 	if (textp == etext) break;
! 	if (*textp==';') {binbuf_addv(x,";"); textp++; continue;}
! 	if (*textp==',') {binbuf_addv(x,","); textp++; continue;}
! 	if (*textp=='+' || *textp=='-' || *textp=='.' || isdigit(*textp)) {
! 	    double v = strtod(textp,&token);
! 	    if (isspace(*token)) {binbuf_addv(x,"f",v); continue;}
! 	    textp=token;
  	}
! 	bufp = buf;
!         for (; textp!=etext && bufp!=ebuf && *textp!=',' && *textp!=';' && !isspace(*textp); bufp++) {
! 		if (*textp=='\\') {
! 			textp++;
! 			if (textp==etext) break;
! 			*bufp++ = *textp++;
! 		}
  	}
! 	//if (c=='$' && textp!=etext && isdigit(textp[0])) dollar = 1;
! 	//if (!slash) bufp++;
! 	//*bufp = 0;
!         /*if (dollar) {
!                 if (buf[0] != '$') dollar = 0;
!                 for (bufp = buf+1; *bufp; bufp++) if (!isdigit(*bufp)) dollar = 0;
!                 if (dollar) binbuf_addv(x,"$",atoi(buf+1));
!                 else        binbuf_addv(x,"&",gensym(buf));
!             } else */       binbuf_addv(x,"s",gensym(buf));
!     }
!     binbuf_capa(x,x->n);
  }
  
! /* convert text to binbuf, miller-style */
! void binbuf_text_miller(t_binbuf *x, char *text, size_t size) {
!     char buf[MAXPDSTRING+1], *bufp, *ebuf = buf+MAXPDSTRING;
!     const char *textp = text, *etext = text+size;
!     binbuf_clear(x);
!     x->n = 0;
!     while (1) {
!         /* skip leading space */
!         while (textp!=etext && isspace(*textp)) textp++;
!         if (textp == etext) break;
!         if      (*textp==';') {binbuf_addv(x,";"); textp++;}
!         else if (*textp==',') {binbuf_addv(x,","); textp++;}
!         else {
!             /* it's an atom other than a comma or semi */
!             int q = 0, slash = 0, lastslash = 0, dollar = 0;
!             bufp = buf;
!             do {
!                 char c = *bufp = *textp++;
!                 lastslash = slash;
!                 slash = c=='\\';
!                 if (q >= 0) {
!                     int digit = isdigit(c), dot=c=='.', minus=c=='-', plusminus=minus||c=='+', expon=c=='e'||c=='E';
!                     if        (q==0) { /* beginning */
!                         if (minus) q = 1; else if (digit) q = 2; else if (dot) q = 3; else q = -1;
!                     } else if (q==1) { /* got minus */
!                         if (digit) q = 2; else if (dot) q = 3; else q = -1;
! 		    } else if (q==2) { /* got digits */
!                         if (dot) q = 4; else if (expon) q = 6; else if (!digit) q = -1;
! 		    } else if (q==3) { /* got '.' without digits */
!                         if (digit) q = 5; else q = -1;
!                     } else if (q==4) { /* got '.' after digits */
!                         if (digit) q = 5; else if (expon) q = 6; else q = -1;
!                     } else if (q==5) { /* got digits after . */
!                         if (expon) q = 6; else if (!digit) q = -1;
!                     } else if (q==6) { /* got 'e' */
!                         if (plusminus) q = 7; else if (digit) q = 8; else q = -1;
!                     } else if (q==7) { /* got plus or minus */
!                         if (digit) q = 8; else q = -1;
!                     } else if (q==8) { /* got digits */
!                         if (!digit) q = -1;
!                     }
!                 }
!                 if (!lastslash && c == '$' && textp!=etext && isdigit(textp[0])) dollar = 1;
!                 if (!slash) bufp++;
!             } while (textp != etext && bufp != ebuf && (slash || !strchr(" \n\r\t,;",*textp)));
!             *bufp = 0;
!             if (q == 2 || q == 4 || q == 5 || q == 8) binbuf_addv(x,"f",atof(buf));
!             /* LATER try to figure out how to mix "$" and "\$" correctly; here, the backslashes were already
! 		stripped so we assume all "$" chars are real dollars.  In fact, we only know at least one was. */
!             else if (dollar) {
!                 if (buf[0] != '$') dollar = 0;
!                 for (bufp = buf+1; *bufp; bufp++) if (!isdigit(*bufp)) dollar = 0;
!                 if (dollar) binbuf_addv(x,"$",atoi(buf+1));
!                 else        binbuf_addv(x,"&",gensym(buf));
!             } else          binbuf_addv(x,"s",gensym(buf));
!         }
!     }
!     binbuf_capa(x,x->n);
  }
  
! void binbuf_text(t_binbuf *x, char *text, size_t size) {
  	if (0) {
! 		binbuf_text_matju(x,text,size);
  	} else {
! 		binbuf_text_miller(x,text,size);
  	}
  }
  
--- 1784,1897 ----
  }
  
! /* called just after a doublequote in version 1 parsing */
! char *binbuf_text_quoted(t_binbuf *x, char *t, char *end) {
! 	char buf[MAXPDSTRING+1], *bufp=buf, *ebuf = buf+MAXPDSTRING;
! 	while (t!=end && bufp!=ebuf) {
! 		char c = *t++;
! 		if (c=='"') break;
! 		if (c!='\\') {*bufp++ = c; continue;}
! 		c = *t++;
! 		if (c=='a') {*bufp++='\a'; continue;}
! 		if (c=='b') {*bufp++='\b'; continue;}
! 		if (c=='f') {*bufp++='\f'; continue;}
! 		if (c=='n') {*bufp++='\n'; continue;}
! 		if (c=='r') {*bufp++='\r'; continue;}
! 		if (c=='v') {*bufp++='\v'; continue;}
! 		if (c=='t') {*bufp++='\t'; continue;}
! 		if (c=='"') {*bufp++='\"'; continue;}
! 		if (c=='\\'){*bufp++='\\'; continue;}
! 		/* if (c=='u') ... */
! 		/* if (c=='x') ... */
! 		/* if (isdigit(c)) ... */
! 		*bufp++ = c; /* ignore syntax error (should it?) */
  	}
! 	*bufp=0;
! 	binbuf_addv(x,"s",gensym(buf));
! 	return t; /* ignore syntax error (should it?) */
! }
! 
! /* find the first atom in text, in any, and add it to this binbuf;
!    returns pointer to end of atom text */
! /* this one is for pd format version 1 */
! /* TODO: double-quotes, braces, test backslashes&dollars */
! char *binbuf_text_matju(t_binbuf *x, char *t, char *end) {
! 	char buf[MAXPDSTRING+1], *bufp=buf, *ebuf = buf+MAXPDSTRING;
! 	int doll=0;
! 	while (t!=end && isspace(*t)) t++;
! 	if (t==end) return t;
! 	if (*t==';') {binbuf_addv(x,";"); return t+1;}
! 	if (*t==',') {binbuf_addv(x,","); return t+1;}
! 	/* if (*t=='"') return binbuf_text_quoted(x,t,end); */
! 	if (*t=='+' || *t=='-' || *t=='.' || isdigit(*t)) {
! 	    char *token;
! 	    double v = strtod(t,&token);
! 	    if (t==end || isspace(*token)) {binbuf_addv(x,"f",v); return token;}
  	}
! 	for (; t!=end && bufp!=ebuf && *t!=',' && *t!=';' && !isspace(*t); bufp++) {
! 		doll |= t[0]=='$' && t+1!=end && isdigit(t[1]);
! 		if (*t=='\\') t++;
! 		if (t!=end) *bufp++ = *t++;
! 	}
! 	*bufp=0;
! 	if (doll) {
! 		if (buf[0]!='$') doll=0;
! 		for (bufp = buf+1; *bufp; bufp++) if (!isdigit(*bufp)) doll=0;
! 		if (doll) binbuf_addv(x,"$",atoi(buf+1));
! 		else      binbuf_addv(x,"&",gensym(buf));
! 	} else            binbuf_addv(x,"s",gensym(buf));
! 	return t;
  }
  
! /* this one is for pd format version 0 */
! char *binbuf_text_miller(t_binbuf *x, char *t, char *end) {
!     char buf[MAXPDSTRING+1], *bufp = buf, *ebuf = buf+MAXPDSTRING;
!     /* it's an atom other than a comma or semi */
!     int q = 0, slash = 0, lastslash = 0, dollar = 0;
!     /* skip leading space */
!     while (t!=end && isspace(*t)) t++;
!     if (t==end) return t;
!     if (*t==';') {binbuf_addv(x,";"); return t+1;}
!     if (*t==',') {binbuf_addv(x,","); return t+1;}
!     do {
! 	char c = *bufp = *t++;
! 	lastslash = slash;
! 	slash = c=='\\';
! 	if (q >= 0) {
! 	  int digit = isdigit(c), dot=c=='.', minus=c=='-', plusminus=minus||c=='+', expon=c=='e'||c=='E';
! 	  if      (q==0) { /* beginning */ if (minus) q=1; else if (digit) q=2; else if (dot) q=3; else q=-1;}
! 	  else if (q==1) { /* got minus */                      if (digit) q=2; else if (dot) q=3; else q=-1;}
! 	  else if (q==2) { /* got digits */               if (dot) q=4; else if (expon) q=6; else if (!digit) q=-1;}
! 	  else if (q==3) { /* got '.' without digits */ if (digit) q=5; else q=-1;}
! 	  else if (q==4) { /* got '.'   after digits */ if (digit) q=5; else if (expon) q=6; else q=-1;}
! 	  else if (q==5) { /* got digits after . */                          if (expon) q=6; else if (!digit) q=-1;}
! 	  else if (q==6) { /* got 'e' */ if (plusminus) q=7; else if (digit) q=8; else q=-1;}
! 	  else if (q==7) { /* got plus or minus */ if (digit) q=8; else q=-1;}
! 	  else if (q==8) { /* got digits */ if (!digit) q=-1;}
! 	}
! 	if (!lastslash && c == '$' && t!=end && isdigit(*t)) dollar = 1;
! 	if (!slash) bufp++;
!     } while (t!=end && bufp!=ebuf && (slash || !strchr(" \n\r\t,;",*t)));
!     *bufp = 0;
!     if (q == 2 || q == 4 || q == 5 || q == 8) {binbuf_addv(x,"f",atof(buf)); return t;}
!     /* LATER try to figure out how to mix "$" and "\$" correctly; here, the backslashes were already
!        stripped so we assume all "$" chars are real dollars.  In fact, we only know at least one was. */
!     if (dollar) {
! 	if (buf[0] != '$') dollar = 0;
! 	for (bufp = buf+1; *bufp; bufp++) if (!isdigit(*bufp)) dollar = 0;
! 	if (dollar) binbuf_addv(x,"$",atoi(buf+1));
! 	else        binbuf_addv(x,"&",gensym(buf));
!     } else          binbuf_addv(x,"s",gensym(buf));
!     return t;
  }
  
! void binbuf_text(t_binbuf *x, char *t, size_t size) {
! 	char *end=t+size;
! 	binbuf_clear(x);
  	if (0) {
! 		while (t!=end) t=binbuf_text_matju(x,t,end);
  	} else {
! 		while (t!=end) t=binbuf_text_miller(x,t,end);
  	}
+ 	binbuf_capa(x,x->n);
  }
  





More information about the Pd-cvs mailing list