home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Arashi 1.1.1 / source code / For your think c folder / Misc / LexStuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-31  |  5.4 KB  |  292 lines  |  [TEXT/MMCC]

  1. /*/
  2.      Project Arashi: LexStuff.c
  3.      Major release: Version 1.1d2, 9/5/95
  4.  
  5.      Last modification: Thursday, August 31, 1995, 1:55
  6.      Created: Saturday, June 25, 1994, 23:50
  7.  
  8.      Copyright © 1994-1995, Juri Munkki
  9. /*/
  10.  
  11. #include "LexStuff.h"
  12.  
  13. short    MatchLongInt(
  14.     StringPtr    theString,
  15.     long        *n)
  16. {
  17.     short    matchCount = 0;
  18.     char    theChar;
  19.     long    theLong = 0;
  20.     Boolean    sign = false;
  21.  
  22.     theChar = *theString++;
  23.  
  24.     if(theChar == '-' || theChar == '+')
  25.     {
  26.         sign = theChar == '-';
  27.  
  28.         matchCount++;
  29.         theChar = *theString++;
  30.     }
  31.  
  32.     while(theChar >= '0' && theChar <= '9')
  33.     {    theLong = theLong * 10 + theChar - '0';
  34.  
  35.         matchCount++;
  36.         theChar = *theString++;
  37.     }
  38.  
  39.     *n = sign ? -theLong : theLong;
  40.     return matchCount;
  41. }
  42.  
  43. /*
  44. **    Match a floating point number.
  45. **    The input is a null-terminated string. The output
  46. **    tells how many characters could belong to a floating
  47. **    point number.
  48. */
  49. short    MatchDouble(
  50.     StringPtr    theString,
  51.     double_t    *theNumber)
  52. {
  53.     enum    {    initialState,
  54.                 integerPartState,
  55.                 decimalPartState,
  56.                 exponentMatchState,
  57.                 endState    };
  58.  
  59.     short    matchCount = 0;
  60.     char    theChar;
  61.     short    state = initialState;
  62.     decimal    numRec;
  63.     
  64.     numRec.sig.length = 0;
  65.     numRec.sgn = 0;
  66.     numRec.exp = 0;
  67.  
  68.     while(state != endState)
  69.     {    theChar = theString[matchCount];
  70.         
  71.         if(theChar == 0)
  72.             state = endState;    //    String ends with null
  73.  
  74.         switch(state)
  75.         {    case initialState:
  76.                 if(    theChar == '+'    ||    theChar == '-'    ||
  77.                     (theChar >= '0' && theChar <= '9'))
  78.                 {        if(theChar == '-' || theChar == '+')
  79.                         {    numRec.sgn = (theChar == '-');
  80.                         }
  81.                         else
  82.                         {    if(theChar != '0')
  83.                                 numRec.sig.text[numRec.sig.length++] = theChar;
  84.                         }
  85.                         matchCount++;
  86.                         state = integerPartState; 
  87.                 }
  88. #define ALLOWSHORTHAND
  89. #ifdef ALLOWSHORTHAND        //    Are numbers like . and .0 and .e1 and .0e1 valid?
  90.                 else if(theChar == '.')
  91.                 {    matchCount++;
  92.                     state = decimalPartState;
  93.                 }
  94. #endif
  95.                 else    state = endState;
  96.                 break;
  97.  
  98.             case integerPartState:
  99.                 if(theChar >= '0' && theChar <= '9')
  100.                 {    matchCount++;
  101.                     if(SIGDIGLEN-1 > numRec.sig.length)
  102.                     {    if(numRec.sig.length != 0 || theChar != '0')
  103.                             numRec.sig.text[numRec.sig.length++] = theChar;
  104.                     }
  105.                     else
  106.                     {    numRec.exp++;
  107.                     }
  108.  
  109.                 }
  110.                 else if(theChar == 'e' || theChar == 'E')
  111.                 {    state = exponentMatchState;
  112.                 }
  113.                 else if(theChar == '.')
  114.                 {    matchCount++;
  115.                     state = decimalPartState;
  116.                 }
  117.                 else    state = endState;
  118.                 break;
  119.  
  120.             case decimalPartState:
  121.                 if(theChar >= '0' && theChar <= '9')
  122.                 {    matchCount++;
  123.                     if(SIGDIGLEN-1 > numRec.sig.length)
  124.                     {
  125.                         if(numRec.sig.length != 0 || theChar != '0')
  126.                         {    numRec.sig.text[numRec.sig.length++] = theChar;
  127.                         }
  128.                         numRec.exp--;
  129.                     }
  130.                 }
  131.                 else if(theChar == 'e' || theChar == 'E')
  132.                 {    state = exponentMatchState;
  133.                 }
  134.                 else    state = endState;
  135.                 break;                
  136.  
  137.             case exponentMatchState:
  138.                 {    long    theExp;
  139.                     short    expLength = MatchLongInt(theString+matchCount+1, &theExp);
  140.                 
  141.                     numRec.exp += theExp;
  142.  
  143.                     if(expLength > 0)
  144.                     {    matchCount += expLength+1;
  145.                     }
  146.                     state = endState;
  147.                 }
  148.                 break;
  149.         }
  150.     }
  151.     
  152. #ifdef powerc
  153.     *theNumber = dec2num(&numRec);
  154. #else
  155. #ifdef __MWERKS__
  156.     {
  157.         extended80    temp;
  158.         
  159.         Dec2Num(&numRec, &temp);
  160.         x80tox96(&temp, (extended96 *)theNumber);
  161.     }
  162.  
  163. #else
  164. #if !__option(mc68881) && __option(native_fp)
  165.     *theNumber = dec2num(&numRec);
  166. #else
  167.     {
  168.         extended    temp;
  169.         
  170.         temp = dec2num(&numRec);
  171.         x80tox96(&temp, theNumber);
  172.     }
  173. #endif
  174. #endif
  175. #endif
  176.     return matchCount;
  177. }
  178.  
  179. short    MatchWhiteSpace(
  180.     StringPtr    theString)
  181. {
  182.     short    matchCount = 0;
  183.     char    theChar;
  184.  
  185.     theChar = theString[matchCount];
  186.     
  187.     while(theChar == ' ' || theChar == 9 /* tab */ || theChar == 13 /* return */)
  188.     {    matchCount++;
  189.         theChar = theString[matchCount];
  190.     }
  191.     
  192.     return matchCount;
  193. }
  194.  
  195. short    MatchLine(
  196.     StringPtr    theString)
  197. {
  198.     short    matchCount = 0;
  199.     char    theChar;
  200.  
  201.     theChar = theString[matchCount];
  202.     
  203.     while(theChar != 13 /* return */ && theChar != 10 /* NewLine */)
  204.     {    matchCount++;
  205.         theChar = theString[matchCount];
  206.     }
  207.     
  208.     do
  209.     {    matchCount++;
  210.         theChar = theString[matchCount];
  211.     } while(theChar == 13 /* return */ || theChar == 10 /* NewLine */);
  212.     
  213.     return matchCount;
  214. }
  215.  
  216. short    UnmatchWhiteSpace(
  217.     StringPtr    theString)
  218. {
  219.     short    matchCount = 0;
  220.     char    theChar;
  221.  
  222.     theString--;
  223.     theChar = theString[matchCount];
  224.     
  225.     while(theChar == ' ' || theChar == 9 /* tab */ || theChar == 13 /* return */)
  226.     {    matchCount--;
  227.         theChar = theString[matchCount];
  228.     }
  229.     
  230.     return matchCount;
  231. }
  232.  
  233. short    MatchWhiteLongInt(
  234.     StringPtr    theString,
  235.     long        *n)
  236. {
  237.     short    whiteMatch;
  238.     short    matchCount;
  239.     
  240.     whiteMatch = MatchWhiteSpace(theString);
  241.     matchCount = MatchLongInt(theString+whiteMatch, n);
  242.     
  243.     return matchCount ? whiteMatch+matchCount : 0;
  244. }
  245.  
  246. short    MatchWhiteDouble(
  247.     StringPtr    theString,
  248.     double_t    *theNumber)
  249. {
  250.     short    whiteMatch;
  251.     short    matchCount;
  252.     
  253.     whiteMatch = MatchWhiteSpace(theString);
  254.     matchCount = MatchDouble(theString+whiteMatch, theNumber);
  255.     
  256.     return matchCount ? whiteMatch+matchCount : 0;
  257. }
  258.  
  259. short    StringToToken(
  260.     Handle        stringList,
  261.     StringPtr    stringStart,
  262.     short        stringLen)
  263. {
  264.     StringPtr    string;
  265.     StringPtr    charp;
  266.     short        tokCount;
  267.     short        ind;
  268.     short        i;
  269.     
  270.     tokCount = *(short *)(*stringList);
  271.     string = ((StringPtr)*stringList)+2;
  272.     
  273.     for(ind = 1;ind <= tokCount; ind++)
  274.     {    if(string[0] == stringLen)
  275.         {    charp = string+1;
  276.         
  277.             for(i=0;i<stringLen;i++)
  278.             {    if(*charp++ != stringStart[i])
  279.                 {    break;
  280.                 }
  281.             }
  282.             
  283.             if(i==stringLen)
  284.             {    break;
  285.             }
  286.         }
  287.         
  288.         string += string[0] + 1;
  289.     }
  290.     
  291.     return ind;
  292. }