"Fossies" - the Fresh Open Source Software Archive

Member "asp2php-2008-08-13/src/jscript/gettoken_j.c" (13 Aug 2008, 6358 Bytes) of package /linux/www/old/asp2php-2008-08-13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gettoken_j.c" see the Fossies "Dox" file reference documentation.

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 
    5 #include "../globals.h"
    6 #include "../functions.h"
    7 
    8 /*
    9 JScript Token Types:
   10 
   11 0: EOF
   12 1: number
   13 2: keyword
   14 3: =
   15 4: identifier
   16 5: EOL
   17 6: %>
   18 7: +,-,*,/,%
   19 8: >,<,<=,>=,<>,==,!=,!
   20 9: &,|,^
   21 10: "    "
   22 11: '    '
   23 12: (,)
   24 13: :
   25 14: .
   26 15: comment
   27 16: ,
   28 17: ;
   29 18: &&,||
   30 19: ? 
   31 20: {,}
   32 
   33 */
   34 
   35 int gettoken_j(char *token)
   36 {
   37 int ptr=0,tokentype=2;
   38 int ch,sflag=0,r=0;
   39 int quote=0;
   40 int equals=0;
   41 
   42   if (tokenpush2[0]!=0)
   43   {
   44     strcpy(token,tokenpush2);
   45     tokenpush2[0]=0;
   46     return tokentypepush2;
   47   }
   48 
   49   if (tokenpush[0]!=0)
   50   {
   51     strcpy(token,tokenpush);
   52     tokenpush[0]=0;
   53     return tokentypepush;
   54   }
   55 
   56   token[1]=0;
   57 
   58   while(1)
   59   {
   60     if (pushback!=-1)
   61     { ch=pushback; pushback=-1; }
   62       else
   63     { ch=getc(in); }
   64 
   65     column++;
   66 
   67     if (ch==EOF) break;
   68 
   69     if (equals==1)
   70     {
   71       if (ch=='=')
   72       {
   73         token[ptr++]=ch;
   74         token[ptr++]=0;
   75         return 8;
   76       }
   77         else
   78       {
   79         pushback=ch;
   80         token[ptr++]=0;
   81         return 3;
   82       }
   83     }
   84 
   85     if (quote=='\'' && sflag!=1)
   86     {
   87       if (ch=='\n' || ch=='\r')
   88       { 
   89         token[0]='/';
   90         for (r=ptr; r>0; r--)
   91         { token[r]=token[r-1]; }
   92         token[ptr+1]='\n';
   93         token[ptr+2]=0;
   94         return 15;
   95       }
   96         else
   97       if (ch=='%')
   98       {
   99         ch=getc(in);
  100 
  101         if (ch=='>')
  102         {
  103           push("%>",6);
  104           token[0]='/';
  105           for (r=ptr; r>0; r--)
  106           { token[r]=token[r-1]; }
  107           token[ptr+1]=' ';
  108           token[ptr+2]=0;
  109           return 15;
  110         }
  111           else
  112         {
  113           pushback=ch;
  114         }
  115       }
  116     }
  117 
  118     if (sflag==1)
  119     {
  120       if (ch!=quote)
  121       {
  122     pushback=ch;
  123         token[ptr]=0;
  124     if (ch=='\"') return 10;
  125     return 11;
  126       }
  127 
  128       sflag=0;
  129     }
  130 
  131     if (ch=='\"' || ch=='\'')
  132     {
  133       if (ptr==0)
  134       { quote=ch; }
  135     else
  136       if (quote==ch)
  137       {
  138     if (token[ptr-1]==ch && sflag!=3)
  139         {
  140       if (ptr==1)
  141       {
  142         ch=getc(in);
  143             pushback=ch;
  144         if (ch!=quote)
  145         {
  146           token[ptr]=quote;
  147           token[ptr+1]=0;
  148           if (quote=='\'') return 11; else return 10;
  149         }
  150         sflag=1;
  151       }
  152         else
  153       if (sflag!=3)
  154           { token[ptr-1]='\\'; sflag=3; token[ptr++]=ch; continue; }
  155         }
  156       else
  157         { sflag=1; }
  158       }
  159 /*
  160         else
  161       {
  162 printf("pushback=ch; break;\n");
  163         pushback=ch; break;
  164       }
  165 */
  166     }
  167 
  168     if (sflag==3) sflag=0;
  169 
  170     if (quote!=0)
  171     {
  172       token[ptr++]=ch;
  173       continue;
  174     }
  175 
  176     if ((ch=='>' && ptr==1) && token[ptr-1]=='%')
  177     { token[1]='>'; token[2]=0; return 6; }
  178 
  179     if (sflag==2)
  180     {
  181       if (ch=='=' || (ch=='>' && token[0]=='<'))
  182       { token[1]=ch; token[2]=0; }
  183     else
  184       { pushback=ch; token[1]=0; }
  185       return 8;
  186     }
  187 
  188     if (ch=='>' || ch=='<')
  189     {
  190       if (ptr!=0)
  191       { pushback=ch; token[ptr]=0; break; }
  192     else
  193       { sflag=2; tokentype=8; }
  194     }
  195 
  196     if (ch=='(' || ch==')')
  197     { 
  198       if (ptr!=0)
  199       { pushback=ch; token[ptr]=0; break; }
  200     else
  201       { token[0]=ch; return 12; }
  202     }
  203 
  204     if (ch=='{' || ch=='}')
  205     { 
  206       if (ptr!=0)
  207       { pushback=ch; token[ptr]=0; break; }
  208     else
  209       { token[0]=ch; return 20; }
  210     }
  211 
  212     if (ch=='|' && token[ptr-1]=='|')
  213     {
  214       token[ptr++]='|';
  215       token[ptr]=0;
  216       return 18;
  217     }
  218 
  219     if (ch=='=' && token[ptr-1]=='!')
  220     {
  221       token[ptr++]='=';
  222       token[ptr]=0;
  223       return 8;
  224     }
  225 
  226     if (ch=='&' && token[ptr-1]=='&')
  227     {
  228       token[ptr++]='&';
  229       token[ptr]=0;
  230       return 18;
  231     }
  232 
  233     if (ch=='&')
  234     { tokentype=9; }
  235 
  236     if (ch=='|')
  237     { tokentype=9; }
  238 
  239     if (ch=='%')
  240     { tokentype=7; }
  241 
  242     if (ch=='!')
  243     { tokentype=8; }
  244 
  245     if (ch=='?')
  246     {
  247       if (ptr!=0)
  248       { pushback=ch; token[ptr]=0; break; }
  249     else
  250       { token[0]=ch; return 19; }
  251     }
  252 
  253     if (ch=='=')
  254     {
  255       if (ptr!=0)
  256       { 
  257         pushback=ch;
  258         token[ptr]=0;
  259         break; 
  260       }
  261     else
  262       { 
  263          token[0]=ch; 
  264          equals=1;
  265          /* return 8; */
  266       }
  267     }
  268 
  269 /*
  270     if (ch=='=')
  271     {
  272       if (ptr!=0)
  273       { pushback=ch; token[ptr]=0; break; }
  274     else
  275       { token[0]=ch; return 8; }
  276     }
  277 */
  278 
  279     if (ch=='&')
  280     {
  281       if (ptr!=0)
  282       {
  283     if (ptr==1 && (ch=='h' || ch=='H'))
  284         {
  285       token[0]='0';
  286       token[1]='x';
  287       tokentype=1;
  288         }
  289       else
  290     { pushback=ch; token[ptr]=0; break; }
  291       }
  292     else
  293       { token[0]=ch; return 9; }
  294     }
  295 
  296     if (ch==':')
  297     {
  298       if (ptr!=0)
  299       { pushback=ch; token[ptr]=0; break; }
  300     else
  301       { token[0]=ch; return 13; }
  302     }
  303 
  304     if (ch==';')
  305     {
  306       if (ptr!=0)
  307       { pushback=ch; token[ptr]=0; break; }
  308     else
  309       { token[0]=ch; return 17; }
  310     }
  311 
  312     if (ch==',')
  313     {
  314       if (ptr!=0)
  315       { pushback=ch; token[ptr]=0; break; }
  316     else
  317       { token[0]=ch; return 16; }
  318     }
  319 
  320     if (ch=='.')
  321     {
  322       if (ptr!=0)
  323       { pushback=ch; token[ptr]=0; break; }
  324     else
  325       { token[0]=ch; return 14; }
  326     }
  327 
  328     if ((ch=='+' || ch=='-') || (ch=='*' || ch=='/'))
  329     {
  330       if (ptr==0)
  331       { token[0]=ch; token[1]=0; return 7; }
  332     else
  333       {
  334     pushback=ch;
  335         break;
  336       }
  337     }
  338 
  339 /*
  340     if (ch=='_')
  341     {
  342       if (ptr==0)
  343       {
  344     token[0]=ch;
  345     while ((ch=getc(in))!=EOF)
  346         {
  347       if (ch=='\n' || ch=='\r')
  348           {
  349             line++;
  350         r=getc(in);
  351             if (r=='\r' || r=='\n')
  352             { if (ch==r) pushback=r; }
  353           else
  354         { pushback=r; }
  355         return 3;
  356       }
  357         }
  358         return 0;
  359       }
  360     else
  361       if (tokentype==2)
  362       { token[ptr++]=ch; continue; }
  363     else
  364       { pushback=ch; break; }
  365     }
  366 */
  367 
  368     if (ch=='\r' || ch=='\n')
  369     {
  370       if (ptr==0)
  371       {
  372         token[0]='\n';
  373         column=0;
  374         line++;
  375     r=getc(in);
  376         if (r=='\r' || r=='\n')
  377         { if (ch==r) pushback=r; }
  378       else
  379     { pushback=r; }
  380         return 5;
  381       }
  382     else
  383       { pushback=ch; break; }
  384     }
  385 
  386     if (ch==' ' || ch=='\t')
  387     {
  388       if (ptr==0)
  389       { /* putc(ch,out); */ continue; }
  390     else
  391       {
  392     pushback=ch;
  393         token[ptr]=0;
  394     return tokentype;
  395       }
  396     }
  397 
  398     if (tokentype==1)
  399     {
  400       if (ch<'0' || ch>'9')
  401       { pushback=ch; break; }
  402     }
  403 
  404     if (ch>='0' && ch<='9')
  405     {
  406       if (ptr==0)
  407       { tokentype=1; }
  408     }
  409 
  410     token[ptr++]=(char)ch;
  411   }
  412 
  413   if (ptr==0) return 0;
  414 
  415   token[ptr]=0;
  416   return tokentype;
  417 }
  418