[BACK]Return to OM2OXM.java CVS log [TXT][DIR] Up to [local] / OpenXM / src / OpenMath

Annotation of OpenXM/src/OpenMath/OM2OXM.java, Revision 1.9

1.1       tam         1: /**
1.9     ! tam         2:  * $OpenXM: OpenXM/src/OpenMath/OM2OXM.java,v 1.8 1999/11/15 23:13:21 tam Exp $
1.1       tam         3:  *
                      4:  * $B$3$N%/%i%9$G$O0J2<$N(B BNF $B$GI=$5$l$k9=J82r@O$r<BAu$7$F$$$k(B
                      5:  * expr -> stag [expr | immediate]* etag
                      6:  * immediate -> INTEGER
                      7:  *            | SYMBOL
                      8:  * stag -> '<' SYMBOL '>'
                      9:  * etag -> '<' '/' SYMBOL '>'
                     10:  * $B=*C<5-9f(B: INTEGER, SYMBOL, '<', '/', '>'
                     11:  * $BHs=*C<5-9f(B: expr, stag, etag, immediate
                     12:  * $B3+;O5-9f(B: expr
                     13:  *
                     14:  * $B0J2<$N4X?t$r;HMQ(B:
                     15:  * public void send(OutputStream os); - buffer $B$K$"$k(B OX message $B$rAw?.$9$k(B.
                     16:  * public int parse(); - $B9=J82r@O$r3+;O$9$k(B.
                     17:  * private int parse_expr();       - parser $B$N(B expr $BItJ,(B
                     18:  * private Attribute parse_stag(); - parser $B$N(B stag $BItJ,(B
                     19:  * private void parse_etag(Attribute tag,int argnum); - parser $B$N(B etag $BItJ,(B
                     20:  * private int parse_immediate();  - parser $B$N(B immediate $BItJ,(B
                     21:  * private void parse_error(String mesg); - parser $B$N(B error $B$rI=<($7$F=*N;$9$k(B
                     22:  * public static void main(String[] args); - main $B4X?t(B.
                     23:  *
                     24:  * $B0J2<$NJQ?t$r;HMQ(B:
                     25:  * Attribute Attribute; - $B;z6g2r@O4o$r8F$S=P$7(B, $B$=$NB0@-CM$rJ]B8$7$F$*$/(B
                     26:  * int token; - $B8=:_FI$_9~$s$G$$$k(B token $B$rJ]B8$7$F$*$/$N$K;HMQ(B.
                     27:  * CellStack stack; - $B%;%^%s%F%#%C%/%9$N%A%'%C%/$K;H$&FbIt%9%?%C%/(B.
                     28:  * ByteArrayOutputStream buffer; - oxasir $B$KAw?.$9$k$^$($K%G!<%?$rN/$a$F$*$/(B
                     29:  */
                     30:
                     31: import java.io.*;
                     32: import java.net.*;
                     33: import java.util.Vector;
                     34: import JP.ac.kobe_u.math.tam.OpenXM.*;
                     35:
                     36: final class OM2OXM implements Runnable{
                     37:   private PushbackInputStream is;
                     38:   private String attribute;
                     39:   private int token = TT_NULL;
                     40:   private boolean lexPushbackFlag = false;
                     41:   private OpenXM asir; // for debug
1.7       tam        42:   private boolean debug = false;
1.1       tam        43:
                     44:   // Token Type for lexical analyzer
                     45:   final static int TT_NULL      = 0;
                     46:   final static int TT_String    = -1;
                     47:   final static int TT_StartTag  = -2;
                     48:   final static int TT_EndTag    = -3;
                     49:   final static int TT_SingleTag = -4;
                     50:
                     51:   public void run(){ // for debug
                     52:     //$B%5!<%PB&$+$iAw?.$5$l$?J8;zNs$r<u?.$7$^$9!#(B
                     53:     try{
                     54:       while(true){
                     55:        CMO tmp;
                     56:
                     57:        switch(asir.receiveOXtag()){
                     58:        case OpenXM.OX_COMMAND:
                     59:           asir.receiveSM();
                     60:           break;
                     61:
                     62:        case OpenXM.OX_DATA:
                     63:           tmp = asir.receiveCMO();
1.2       tam        64:          System.out.println("=> "+ CMO2OM(tmp));
1.1       tam        65:           break;
                     66:        }
                     67:
                     68:       }
                     69:     }catch(IOException e){}
                     70:   }
                     71:
                     72:   public static String CMO2OM(CMO cmo){
1.2       tam        73:     return "<OMOBJ>"+ CMO2OM_sub(cmo) +"</OMOBJ>";
                     74:   }
                     75:
1.9     ! tam        76:   private static String CMO2OM_sub(CMO cmo) throws NumberFormatException{
1.1       tam        77:     String ret = "";
                     78:
                     79:     switch(cmo.getDISCRIMINATOR()){
                     80:     case CMO.CMO_NULL:
                     81:       return "<OMI>0</OMI>";
                     82:
                     83:     case CMO.CMO_INT32:
                     84:       return "<OMI>"+ ((CMO_INT32)cmo).intValue() +"</OMI>";
                     85:
                     86:       // case CMO.CMO_DATUM:
                     87:
                     88:     case CMO.CMO_STRING:
                     89:       return "<OMSTR>"+ ((CMO_STRING)cmo).getString() +"</OMSTR>";
                     90:
                     91:       // case CMO.CMO_LIST:
                     92:
                     93:     case CMO.CMO_MONOMIAL32:
                     94:       ret += "<OMA><OMS name=\"Monom\" cd=\"poly\"/>";
1.2       tam        95:       ret += CMO2OM_sub(((CMO_MONOMIAL32)cmo).getCoefficient());
1.1       tam        96:       for(int i=0;i<((CMO_MONOMIAL32)cmo).getDegree().length;i++){
                     97:        ret += "<OMI>"+ ((CMO_MONOMIAL32)cmo).getDegree()[i] +"</OMI>";
                     98:       }
                     99:       ret += "</OMA>";
                    100:       return ret;
                    101:
                    102:     case CMO.CMO_ZZ:
                    103:       return "<OMI>"+ ((CMO_ZZ)cmo).BigIntValue() +"</OMI>";
                    104:
                    105:     case CMO.CMO_QQ:
                    106:       return "<OMA><OMS name=\"over\" cd=\"basic\"/>"+
1.2       tam       107:        CMO2OM_sub(((CMO_QQ)cmo).getBunshi())+
                    108:        CMO2OM_sub(((CMO_QQ)cmo).getBunbo())+
                    109:        "</OMA>";
1.1       tam       110:
                    111:     case CMO.CMO_ZERO:
                    112:       return "<OMI> 0 </OMI>";
                    113:
                    114:       // case CMO.CMO_DMS:
                    115:
                    116:     case CMO.CMO_DMS_GENERIC:
                    117:       ret += "<OMA><OMS name=\"PolyRing\" cd=\"poly\"/>";
1.4       tam       118:       ret += "<OMI>2</OMI></OMA>";
1.1       tam       119:       return ret;
                    120:
                    121:     case CMO.CMO_DISTRIBUTED_POLYNOMIAL:
                    122:       ret += "<OMA><OMS name=\"DMP\" cd=\"poly\"/>";
1.2       tam       123:       ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getRing());
1.1       tam       124:       ret += "<OMA><OMS name=\"SDMP\" cd=\"poly\"/>";
1.2       tam       125:       for(int i=0;i<((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials().length;
                    126:          i++){
                    127:        ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials()[i]);
1.1       tam       128:       }
                    129:       ret += "</OMA></OMA>";
                    130:       return ret;
                    131:
1.7       tam       132:     case CMO.CMO_BIGFLOAT:
                    133:       ret += "<OMA><OMS name=\"times\" cd=\"basic\"/>";
                    134:       ret += CMO2OM_sub(((CMO_BIGFLOAT)cmo).getSyosubu());
                    135:       ret += "<OMA><OMS name=\"power\" cd=\"basic\"/>";
                    136:       ret += "<OMI>2</OMI>";
                    137:       ret += CMO2OM_sub(((CMO_BIGFLOAT)cmo).getShisubu());
                    138:       ret += "</OMA></OMA>";
                    139:       return ret;
                    140:
1.1       tam       141:     case CMO.CMO_INDETERMINATE:
                    142:       return "<OMV name=\""+ ((CMO_INDETERMINATE)cmo).getString() +"\"/>";
                    143:
                    144:       /*
                    145:        case CMO.CMO_TREE:
1.8       tam       146:        return "<OMA><OMS name=\""+ ((CMO_TREE)cmo).getName() +"\" cdname=\""+
                    147:        ((CMO_TREE)cmo).getCDName() +"\"/>" + "</OMA>";
1.1       tam       148:        */
                    149:
                    150:     default:
1.9     ! tam       151:       //return "<OMSTR>"+ cmo.toCMOexpression() +"</OMSTR>";
1.1       tam       152:     }
1.9     ! tam       153:
        !           154:     throw new NumberFormatException(""+ cmo.toCMOexpression());
1.1       tam       155:   }
                    156:
                    157:   private boolean isSpace(int ch){ // use from lex
                    158:     return (ch==' ' || ch=='\t' || ch=='\n' || ch=='\r');
                    159:   }
                    160:
                    161:   private int skipSpace() throws IOException{ // use from lex
                    162:     int ch;
                    163:
                    164:     do{
                    165:       ch = is.read();
                    166:     }while(isSpace(ch));
                    167:     is.unread(ch);
                    168:
                    169:     return ch;
                    170:   }
                    171:
                    172:   private void pushbackLex(){
                    173:     if(lexPushbackFlag){
                    174:       System.err.println("lex pushback error!");
                    175:     }
                    176:     lexPushbackFlag = true;
                    177:   }
                    178:
                    179:   private int readNextToken() throws IOException{ // lexical analyzer
                    180:     String buf = "";
                    181:     int ch;
                    182:
                    183:     if(lexPushbackFlag){
                    184:       lexPushbackFlag = false;
                    185:       return token;
                    186:     }
                    187:
                    188:     token = TT_NULL;
                    189:     attribute = "";
                    190:     skipSpace();
                    191:     ch = is.read();
                    192:
                    193:     if(ch == '<'){ // for tag
                    194:       ch = skipSpace();
                    195:       if(ch == '/'){
                    196:        is.read();
                    197:        token = TT_EndTag;
                    198:        skipSpace();
                    199:       }else{
                    200:        token = TT_StartTag;
                    201:       }
                    202:       while((ch = is.read()) != '>' && ch != '/' && ch != -1){
                    203:        if(isSpace(ch)){
                    204:          if((ch = skipSpace()) != '>' && ch != '/'){
                    205:            buf += " ";
                    206:          }
                    207:          continue;
                    208:        }
                    209:
                    210:        buf += Character.toUpperCase((char)ch);
                    211:
                    212:        if(ch == '"'){
                    213:          do{
                    214:            ch = is.read();
                    215:            buf += (char)ch;
                    216:          }while(ch != '"' && ch != -1);
                    217:        }
                    218:       }
                    219:       if(ch == '>'){ // for StartTag and EndTag
                    220:        attribute = buf;
                    221:        return token;
                    222:       }else if(ch == '/'){
                    223:        skipSpace();
                    224:        if((ch = is.read()) == '>'){ // for SingleTag
                    225:          attribute = buf;
                    226:          return token = TT_SingleTag;
                    227:        }
                    228:       }
                    229:       return token = TT_NULL;
                    230:     }else if(ch != -1){
                    231:       is.unread(ch);
                    232:       while((ch = is.read()) != '<' && ch != -1){
1.3       tam       233:        debug("debug: "+ch);
1.1       tam       234:        if(isSpace(ch)){
1.5       tam       235:          String spaces = String.valueOf((char)ch);
1.1       tam       236:
                    237:          while(isSpace(ch = is.read())){
                    238:            spaces += (char)ch;
                    239:          }
                    240:          if(ch != '<' && ch != -1){
                    241:            buf += spaces;
                    242:          }
                    243:          is.unread(ch);
                    244:        }else{
                    245:          buf += (char)ch;
                    246:        }
                    247:       }
                    248:       is.unread(ch);
                    249:
                    250:       attribute = buf;
                    251:       return token = TT_String;
                    252:     }
                    253:
                    254:     return token = TT_NULL;
                    255:   }
                    256:
                    257:   private boolean exceptTokenTypeInParse(int type) throws IOException{
                    258:     // use from parse
                    259:     if(readNextToken() != type){
                    260:       parse_error("We expect type :'"+ type
                    261:                  +"', but we got type :'"+ token +"'("+ attribute +").");
                    262:     }
1.3       tam       263:     //debug(":"+token+":"+attribute+":"+type);
1.1       tam       264:     return true;
                    265:   }
                    266:
                    267:   public CMO parse(InputStream stream) throws IOException{
                    268:     // start -> '<OMOBJ>' object '</OMOBJ>'
                    269:     CMO ret;
                    270:
                    271:     is = new PushbackInputStream(stream);
                    272:
                    273:     exceptTokenTypeInParse(TT_StartTag);
                    274:     if(!attribute.equals("OMOBJ")){
                    275:       parse_error("We expect '<OMOBJ>'.");
                    276:     }
                    277:
                    278:     ret = parse_object();
1.3       tam       279:
1.1       tam       280:     exceptTokenTypeInParse(TT_EndTag);
                    281:     if(!attribute.equals("OMOBJ")){
                    282:       parse_error("We expect '</OMOBJ>'.");
                    283:     }
                    284:
                    285:     return ret;
                    286:   }
                    287:
                    288:   private CMO parse_object() throws IOException{
                    289:     // object -> variable
                    290:     //         | '<OMI>' S? integer S? '</OMI>'
1.3       tam       291:     //         | '<OMSTR>' S? utf7 S? '</OMSTR>'
1.1       tam       292:     //         | '<OMA>' S? symbol S? objects S? '</OMA>'
                    293:     CMO ret;
                    294:
                    295:     if(readNextToken() != TT_StartTag && token != TT_SingleTag){
                    296:       parse_error("We expect '<OMI> or '<OMA>' or '<OMV>'.");
                    297:     }
                    298:
                    299:     if(attribute.startsWith("OMV")){
                    300:       pushbackLex();
                    301:       ret = parse_variable();
                    302:     }else if(attribute.equals("OMI")){
                    303:       pushbackLex();
                    304:       ret = parse_OMI();
1.3       tam       305:     }else if(attribute.equals("OMSTR")){
                    306:       pushbackLex();
                    307:       ret = parse_OMSTR();
1.1       tam       308:     }else if(attribute.equals("OMA")){
                    309:       String name,cdname;
                    310:       int argnum = 0;
                    311:
                    312:       name = parse_symbol();
                    313:
                    314:       if(name.equals("DMP")){
                    315:        ret = parse_symb_DMP();
1.6       tam       316:        debug("poly: "+ret);
1.1       tam       317:       }else{
                    318:        ret = new CMO_TREE(name,"Basic",parse_objects());
                    319:       }
                    320:
                    321:       exceptTokenTypeInParse(TT_EndTag);
                    322:       if(!attribute.equals("OMA")){
                    323:        parse_error("We expect '</OMA>'.");
                    324:       }
                    325:     }else{
                    326:       parse_error("???");
                    327:       ret = null;
                    328:     }
                    329:
                    330:     return ret;
                    331:   }
                    332:
                    333:   private CMO_ZZ parse_OMI() throws IOException{
                    334:     CMO_ZZ ret;
                    335:
                    336:     exceptTokenTypeInParse(TT_StartTag);
                    337:     if(!attribute.equals("OMI")){
                    338:       parse_error("We expect '<OMI>'.");
                    339:     }
                    340:
                    341:     ret = (CMO_ZZ)parse_integer();
                    342:
                    343:     exceptTokenTypeInParse(TT_EndTag);
                    344:     if(!attribute.equals("OMI")){
                    345:       parse_error("We expect '</OMI>'.");
                    346:     }
                    347:
                    348:     return ret;
                    349:   }
                    350:
1.3       tam       351:   private CMO_STRING parse_OMSTR() throws IOException{
                    352:     CMO_STRING ret;
                    353:
                    354:     exceptTokenTypeInParse(TT_StartTag);
                    355:     if(!attribute.equals("OMSTR")){
                    356:       parse_error("We expect '<OMSTR>'.");
                    357:     }
                    358:
                    359:     if(readNextToken() == TT_String){
                    360:       //ret = (CMO_STRING)parse_utf7();
                    361:       ret = new CMO_STRING(attribute);
                    362:     }else{
                    363:       ret = new CMO_STRING("");
                    364:       pushbackLex();
                    365:     }
                    366:
                    367:     exceptTokenTypeInParse(TT_EndTag);
                    368:     if(!attribute.equals("OMSTR")){
                    369:       parse_error("We expect '</OMSTR>'.");
                    370:     }
                    371:
                    372:     return ret;
                    373:   }
                    374:
1.1       tam       375:   private CMO parse_symb_DMP() throws IOException{
                    376:     parse_object();
                    377:     return new CMO_DISTRIBUTED_POLYNOMIAL(new CMO_DMS_GENERIC(),
                    378:                                          parse_symb_SDMP());
                    379:   }
                    380:
                    381:   private CMO_MONOMIAL32[] parse_symb_SDMP() throws IOException{
                    382:     Vector mono = new Vector();
                    383:     CMO_MONOMIAL32[] ret;
                    384:
                    385:     exceptTokenTypeInParse(TT_StartTag);
                    386:     if(!attribute.equals("OMA")){
                    387:       parse_error("We expect '<OMA>'.");
                    388:     }
                    389:
                    390:     if(!parse_symbol().equals("SDMP")){
                    391:       parse_error("We expect '<SDMP>'");
                    392:     }
                    393:
                    394:     while(readNextToken() != TT_EndTag){
                    395:       pushbackLex();
                    396:       mono.addElement(parse_symb_Monom());
                    397:     }
                    398:
                    399:     if(!attribute.equals("OMA")){
                    400:       parse_error("We expect '</OMA>'.");
                    401:     }
                    402:
                    403:     ret = new CMO_MONOMIAL32[mono.size()];
                    404:     mono.copyInto((Object[])ret);
                    405:
                    406:     return ret;
                    407:   }
                    408:
                    409:   private CMO_MONOMIAL32 parse_symb_Monom() throws IOException{
                    410:     Vector degree = new Vector();
                    411:     int[] array;
                    412:
                    413:     exceptTokenTypeInParse(TT_StartTag);
                    414:     if(!attribute.equals("OMA")){
                    415:       parse_error("We expect '<OMA>'.");
                    416:     }
                    417:
                    418:     if(!parse_symbol().equals("Monom")){
                    419:       parse_error("We expect '<Monom>'");
                    420:     }
                    421:
                    422:     while(readNextToken() != TT_EndTag){
                    423:       pushbackLex();
                    424:       degree.addElement(parse_OMI());
                    425:     }
                    426:
                    427:     if(!attribute.equals("OMA")){
                    428:       parse_error("We expect '<OMA>'.");
                    429:     }
                    430:
                    431:     array = new int[degree.size()-1];
                    432:     for(int i=0;i<array.length;i++){
                    433:       array[i] = ((CMO_ZZ)degree.elementAt(i+1)).intValue();
                    434:     }
                    435:
1.6       tam       436:     //debug("monom: "+ new CMO_MONOMIAL32(array,(CMO_ZZ)degree.elementAt(0)));
1.1       tam       437:     return new CMO_MONOMIAL32(array,(CMO_ZZ)degree.elementAt(0));
                    438:   }
                    439:
1.8       tam       440:   private CMO_LIST parse_objects() throws IOException{
1.1       tam       441:     // $B2r@O$5$l$?(B object $B$r(B LIST $B$GJV$9(B
                    442:     Vector objects = new Vector();
                    443:     CMO[] array;
                    444:
                    445:     while(readNextToken() != TT_EndTag){
                    446:       pushbackLex();
                    447:       objects.addElement(parse_object());
                    448:     }
                    449:     pushbackLex();
                    450:
                    451:     objects.trimToSize();
                    452:     array = new CMO[objects.size()];
                    453:     objects.copyInto((Object[])array);
                    454:
1.7       tam       455:     debug("debug :"+ new CMO_LIST(array));
1.1       tam       456:     return new CMO_LIST(array);
                    457:   }
                    458:
                    459:   private String parse_symbol() throws IOException{
                    460:     // symbname $B$rJV$9(B
                    461:     String ret = "";
                    462:     StreamTokenizer para;
                    463:
                    464:     exceptTokenTypeInParse(TT_SingleTag);
                    465:
                    466:     para = new StreamTokenizer(new StringReader(attribute));
                    467:     para.resetSyntax();
                    468:     para.wordChars('A','Z');
                    469:     para.wordChars('a','z');
                    470:     para.wordChars('0','9');
                    471:     para.wordChars('\u00A0','\u00FF');
                    472:     para.whitespaceChars('\u0000','\u0020');
                    473:     para.quoteChar('"');
                    474:     para.eolIsSignificant(false);
                    475:     para.lowerCaseMode(true);
                    476:     para.slashStarComments(false);
                    477:     para.slashSlashComments(false);
                    478:     para.ordinaryChar('=');
                    479:
                    480:     if(para.nextToken() != StreamTokenizer.TT_WORD
                    481:        || !para.sval.equalsIgnoreCase("OMS")){
                    482:       parse_error("We expect '<OMS>', but '"+ para.sval +"'.");
                    483:     }
                    484:
                    485:     if(para.nextToken() != StreamTokenizer.TT_WORD){
                    486:       parse_error("We expect 'name' or 'cd'.");
                    487:     }
                    488:     if(para.sval.equals("name")){
                    489:       if(para.nextToken() != '='){
                    490:        parse_error("We expect '='.");
                    491:       }
                    492:       if(para.nextToken() != '"'){
                    493:        parse_error("We expect '\"'.");
                    494:       }
                    495:       ret = parse_symbname(para.sval);
                    496:
                    497:       if(para.nextToken() != StreamTokenizer.TT_WORD
                    498:         || !para.sval.equals("cd")){
                    499:        parse_error("We expect 'cd'.");
                    500:       }
                    501:       if(para.nextToken() != '='){
                    502:        parse_error("We expect '='.");
                    503:       }
                    504:       if(para.nextToken() != '"'){
                    505:        parse_error("We expect '\"'.");
                    506:       }
                    507:       parse_cdname(para.sval);
                    508:
                    509:     }else if(para.sval.equals("cd")){
                    510:       if(para.nextToken() != '='){
                    511:        parse_error("We expect '='.");
                    512:       }
                    513:       if(para.nextToken() != '"'){
                    514:        parse_error("We expect '\"'.");
                    515:       }
                    516:       parse_cdname(para.sval);
                    517:
                    518:       if(para.nextToken() != StreamTokenizer.TT_WORD
                    519:         || !para.sval.equals("name")){
                    520:        parse_error("We expect 'cd'.");
                    521:       }
                    522:       if(para.nextToken() != '='){
                    523:        parse_error("We expect '='.");
                    524:       }
                    525:       if(para.nextToken() != '"'){
                    526:        parse_error("We expect '\"'.");
                    527:       }
                    528:       ret = parse_symbname(para.sval);
                    529:
                    530:     }else{
                    531:       parse_error("We expect 'name' or 'cd'.");
                    532:     }
                    533:
                    534:     if(para.nextToken() != StreamTokenizer.TT_EOF){
                    535:       parse_error("We expect '/>'.");
                    536:     }
                    537:
                    538:     return ret;
                    539:   }
                    540:
                    541:   private CMO parse_integer() throws IOException{
                    542:     //Attribute integer = new Attribute(value);
                    543:     String str;
                    544:     int i=0;
                    545:
                    546:     exceptTokenTypeInParse(TT_String);
                    547:     str = attribute;
                    548:
                    549:     if(str.charAt(i)=='-'){
                    550:       i++;
                    551:     }
                    552:     if(str.length()>=i+2 && str.charAt(i)=='x'){ // for HEX
                    553:       String strhex = str.substring(0,i) + str.substring(i+1);
                    554:       for(i++;i<str.length();i++){
                    555:        if("0123456789ABCDEF".indexOf((int)str.charAt(i)) == -1){
                    556:          parse_error("We expect integer.");
                    557:        }
                    558:       }
                    559:       // 10 $B?J?t$KJQ49(B
                    560:       str = (new java.math.BigInteger(strhex,16)).toString();
                    561:     }else if(str.length()>=i+1){ // for DEC
                    562:       for(;i<str.length();i++){
                    563:        if("0123456789".indexOf((int)str.charAt(i)) == -1){
                    564:          parse_error("We expect integer.");
                    565:        }
                    566:       }
                    567:     }else{
                    568:       parse_error("We expect integer.");
                    569:     }
                    570:
                    571:     return new CMO_ZZ(str);
                    572:   }
                    573:
                    574:   private CMO parse_variable() throws IOException{
                    575:     StreamTokenizer para;
                    576:     CMO ret;
                    577:
                    578:     exceptTokenTypeInParse(TT_SingleTag);
                    579:
                    580:     para = new StreamTokenizer(new StringReader(attribute));
                    581:     para.resetSyntax();
                    582:     para.wordChars('A','Z');
                    583:     para.wordChars('a','z');
                    584:     para.wordChars('0','9');
                    585:     para.wordChars('\u00A0','\u00FF');
                    586:     para.whitespaceChars('\u0000','\u0020');
                    587:     para.quoteChar('"');
                    588:     para.eolIsSignificant(false);
                    589:     para.lowerCaseMode(true);
                    590:     para.slashStarComments(false);
                    591:     para.slashSlashComments(false);
                    592:     para.ordinaryChar('=');
                    593:
                    594:     if(para.nextToken() != StreamTokenizer.TT_WORD
                    595:        || !para.sval.equalsIgnoreCase("OMV")){
                    596:       parse_error("We expect '<OMV>'.");
                    597:     }
                    598:
                    599:     if(para.nextToken() != StreamTokenizer.TT_WORD
                    600:        || !para.sval.equals("name")){
                    601:       parse_error("We expect 'name', but obtain '"+ para.sval +"'.");
                    602:     }
                    603:     if(para.nextToken() != '='){
                    604:       parse_error("We expect '='.");
                    605:     }
                    606:     if(para.nextToken() != '"'){
                    607:       parse_error("We expect '\"'.");
                    608:     }
                    609:     ret = new CMO_INDETERMINATE(parse_varname(para.sval));
                    610:
                    611:     if(para.nextToken() != StreamTokenizer.TT_EOF){
                    612:       parse_error("We expect '/>'.");
                    613:     }
                    614:
                    615:     return ret;
                    616:   }
                    617:
                    618:   private String parse_varname(String str){
                    619:     String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
                    620:       + "0123456789+='(),-./:?!#$%*;@[]^_`{|}";
                    621:
                    622:     for(int i=0;i<str.length();i++){
                    623:       if(words.indexOf((int)str.charAt(i))==-1){
                    624:        parse_error("string \""+ str +"\" is not varname.");
                    625:       }
                    626:     }
                    627:
                    628:     return str;
                    629:   }
                    630:
                    631:   private String parse_cdname(String str){
                    632:     String alphabet = "abcdefghijklmnopqrstuvwxyz";
                    633:     int i=0;
                    634:
                    635:     if(alphabet.indexOf((int)str.charAt(i))==-1){
                    636:       parse_error("cdname \""+ str +"\" must begin small alphabet.");
                    637:     }
                    638:     for(i++;i<str.length();i++){
                    639:       if((alphabet + "0123456789_").indexOf((int)str.charAt(i))==-1){
                    640:        parse_error("string \""+ str +"\" is not cdname.");
                    641:       }
                    642:     }
                    643:
                    644:     return str;
                    645:   }
                    646:
                    647:   private String parse_symbname(String str) throws IOException{
                    648:     String Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                    649:     int i=0;
                    650:
                    651:     if(Alphabet.indexOf((int)str.charAt(i))==-1){
                    652:       parse_error("symbname \""+ str +"\" must begin alphabet.");
                    653:     }
                    654:     for(i++;i<str.length();i++){
                    655:       if((Alphabet + "0123456789_").indexOf((int)str.charAt(i))==-1){
                    656:        parse_error("string \""+ str +"\" is not symbname.");
                    657:       }
                    658:     }
                    659:
                    660:     return str;
                    661:   }
                    662:
1.9     ! tam       663:   private void parse_error(String mesg) throws NumberFormatException{
        !           664:     String ret;
        !           665:
        !           666:     ret = mesg +"\n";
        !           667:     ret += "error occuered near :";
1.1       tam       668:     try{
                    669:       for(int i=0;i<10;i++){
1.9     ! tam       670:        ret += (char)is.read();
1.1       tam       671:       }
                    672:     }catch(IOException e){}
1.9     ! tam       673:     ret += "\n";
        !           674:
        !           675:     throw new NumberFormatException(ret);
1.3       tam       676:   }
                    677:
                    678:   private void debug(String str){
                    679:     if(debug){
                    680:       System.err.println(str);
                    681:     }
1.1       tam       682:   }
                    683:
                    684:   public static void main(String[] argv) throws IOException{
                    685:     OM2OXM P = new OM2OXM();
                    686:     OpenXM asir;
                    687:     String host = "localhost";
                    688:     int CtrlPort = 1200,StreamPort = 1300;
                    689:
                    690:     for(int i=0;i<argv.length;i++){
                    691:       if(argv[i].equals("-h")){
                    692:        System.out.println("");
                    693:        System.exit(0);
                    694:       }else if(argv[i].equals("-host")){
                    695:        host = argv[++i];
                    696:       }else if(argv[i].equals("-data")){
                    697:        StreamPort = Integer.valueOf(argv[++i]).intValue();
                    698:       }else if(argv[i].equals("-control")){
                    699:        CtrlPort = Integer.valueOf(argv[++i]).intValue();
                    700:       }else{
                    701:        System.err.println("unknown option :"+ argv[i]);
                    702:        System.exit(1);
                    703:       }
                    704:     }
                    705:
                    706:     try{
                    707:       asir = new OpenXM(host,CtrlPort,StreamPort);
                    708:       asir.sendSM(new SM(SM.SM_mathcap));
                    709:     }catch(UnknownHostException e){
                    710:       System.err.println("host unknown.");
                    711:       System.err.println(e.getMessage());
                    712:       return;
                    713:     }catch(IOException e){
                    714:       System.err.println("connection failed.");
                    715:       System.err.println("IOException occuer !!");
                    716:       System.err.println(e.getMessage());
                    717:       return;
                    718:     }
                    719:
                    720:     P.asir = asir;
                    721:     new Thread(P).start();
                    722:     System.out.println("start");
                    723:
                    724:     try{
                    725:       //P.value = new Attribute(System.in);
                    726:
                    727:       //$B%5!<%PB&$+$iAw?.$5$l$?J8;zNs$r<u?.$7$^$9!#(B
                    728:       while(true){
1.9     ! tam       729:        try{
        !           730:          CMO obj = P.parse(System.in);
        !           731:          asir.send(obj);
        !           732:          asir.sendSM(new SM(SM.SM_popCMO));
        !           733:        }catch(NumberFormatException e){
        !           734:          System.err.println(e.getMessage());
        !           735:        }
1.1       tam       736:       }
                    737:
                    738:     }catch(IOException e){
                    739:       e.printStackTrace();
1.9     ! tam       740:     }finally{
        !           741:       System.out.println("breaking...");
1.1       tam       742:     }
                    743:   }
                    744: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>