bits_scan.l 2.01 KB
/* bits_scan.l - Tokenizer for bit description routine */

%p 150
%a 100
%o 1200

digit		[0-9]
hexdigit	[0-9a-fA-F]
bindigit	[0-1]
tick		[']
colon		[:]
comma		[,]
lbrk		[\[]
rbrk		[\]]
lbrace		[\{]
rbrace		[\}]
hex		{tick}[h]{hexdigit}+
bin		{tick}[b]{bindigit}+
number		{digit}+
endofline	[\n]
skip		[ \t]
backslash	[\\]

%%
{number}	{ install_number(yytext); return (tNUMBER);	}
{hex}		{ install_hex(yytext); return (tHEX);	}
{bin}		{ install_bin(yytext); return (tBIN);	}
{colon}		{ return (tCOLON);	}
{comma}		{ return (tCOMMA);	}
{lbrk}		{ return (tLBRACK);	}
{rbrk}		{ return (tRBRACK);	}
{lbrace}	{ return (tLBRACE);	}
{rbrace}	{ return (tRBRACE);	}
quit		{ return (tQUIT);	}
{backslash}	{ int c;  while ( (c = input()) != '\n' &&
		   		c != EOF )
				;  /* eat up input til next line */
		}
{endofline}	{ return (tEOL);	}
{skip}		;
.		{ return (tJUNK);	}
%%

#undef input
#undef unput

static struct {
  int line_no;
  int left;
  char *pbuf;
  char buf[512];
}  cmd_buf;


int yywrap()
{
   return(1);
}


void install_number(char *str)
{
   yylval.ival =  strtol(str, NULL, 10); 
}


void install_hex(char *str)
{
   yylval.ival =  strtol(&str[2], NULL, 16); 
}


void install_bin(char *str)
{
   yylval.ival =  strtol(&str[2], NULL, 2); 
}


void install(char *str)
{
   (void) strncpy(yylval.name, str, TOKENSIZ-1);
   yylval.name[TOKENSIZ-1] = '\0';
}

/*
 *  Parse GlobString once
 */
int input()
{
  static int first_time = 1;

   if(cmd_buf.left < 1 && first_time) {
     first_time = 0;
     cmd_buf.line_no++;
     strcpy(cmd_buf.buf, GlobString);
     cmd_buf.left = strlen(cmd_buf.buf);
 
     if(cmd_buf.left < 0) 
       return 0;

     cmd_buf.pbuf = cmd_buf.buf; 
   } 
   else if(cmd_buf.left < 1 && !first_time)
   {
     /* clean up for next call */
     first_time = 1;
   }

   cmd_buf.left--;
   return(*cmd_buf.pbuf++);
}

int unput(int c)
{
  if (cmd_buf.pbuf > cmd_buf.buf)
  {
    cmd_buf.pbuf--;
    cmd_buf.left++;
    cmd_buf.pbuf[0] = c;
  }
}

void
  yyerror(char *str)
{
  printf("line %d: %s\n", cmd_buf.line_no, str);
}