Ejemplo n.º 1
0
 /**
  * Constructs a Parser for the given string.
  *
  * @param text The string to be parsed.
  */
 public Parser(String text) {
   Reader reader = new StringReader(text);
   tokenizer = new StreamTokenizer(reader);
   tokenizer.parseNumbers();
   tokenizer.eolIsSignificant(true);
   tokenizer.slashStarComments(true);
   tokenizer.slashSlashComments(true);
   tokenizer.lowerCaseMode(false);
   tokenizer.ordinaryChars(33, 47);
   tokenizer.ordinaryChars(58, 64);
   tokenizer.ordinaryChars(91, 96);
   tokenizer.ordinaryChars(123, 126);
   tokenizer.quoteChar('\"');
   lineNumber = 1;
 }
 /** @tests java.io.StreamTokenizer#ordinaryChars(int, int) */
 public void test_ordinaryCharsII() throws IOException {
   // SM.
   setTest("azbc iof z 893");
   st.ordinaryChars('a', 'z');
   assertEquals("OrdinaryChars failed.", 'a', st.nextToken());
   assertEquals("OrdinaryChars failed.", 'z', st.nextToken());
 }
 /** @tests java.io.StreamTokenizer#parseNumbers() */
 public void test_parseNumbers() throws IOException {
   // SM
   setTest("9.9 678");
   assertTrue("Base behavior failed.", st.nextToken() == StreamTokenizer.TT_NUMBER);
   st.ordinaryChars('0', '9');
   assertEquals("setOrdinary failed.", '6', st.nextToken());
   st.parseNumbers();
   assertTrue("parseNumbers failed.", st.nextToken() == StreamTokenizer.TT_NUMBER);
 }
Ejemplo n.º 4
0
  public CSVReader(BufferedReader input, char customizedSeparator) {
    this.separator = customizedSeparator;

    parser = new StreamTokenizer(input);
    parser.ordinaryChars(0, 255);
    parser.wordChars(0, 255);
    parser.ordinaryChar('\"');
    parser.ordinaryChar(customizedSeparator);

    // Need to do set EOL significance after setting ordinary and word
    // chars, and need to explicitly set \n and \r as whitespace chars
    // for EOL detection to work
    parser.eolIsSignificant(true);
    parser.whitespaceChars('\n', '\n');
    parser.whitespaceChars('\r', '\r');
    atEOF = false;
  }
Ejemplo n.º 5
0
  StreamTokenizer createScanner(Reader r) {
    StreamTokenizer s = new StreamTokenizer(r);

    // disable number parsing, since it doesn't work in the context of string expansion
    // and we also would have to preserve the number type (int or double)
    s.ordinaryChars('0', '9');
    s.wordChars('0', '9');
    // s.wordChars('"', '"');

    // those are used to expand events
    s.wordChars('[', '[');
    s.wordChars(']', ']');
    s.wordChars('|', '|');
    s.wordChars('-', '-');
    s.wordChars('<', '<');
    s.wordChars('>', '>');

    // those can be part of component names
    s.wordChars('_', '_');
    s.wordChars('#', '#');
    s.wordChars('*', '*');
    s.wordChars('@', '@');
    s.wordChars('$', '$');
    s.wordChars(':', ':');
    s.wordChars('~', '~');

    s.quoteChar('"');

    s.slashSlashComments(true);
    s.slashStarComments(true);

    s.whitespaceChars(',', ',');
    s.whitespaceChars(';', ';');

    return s;
  }
Ejemplo n.º 6
0
  public void run() {
    Socket sock = null;

    try {
      int code = StreamTokenizer.TT_EOL;
      FileReader reader = new FileReader(filename);
      StreamTokenizer tokenizer = new StreamTokenizer(reader);
      tokenizer.ordinaryChars('0', '9');
      tokenizer.wordChars('0', '9');
      tokenizer.slashSlashComments(true);

      System.out.println("Connecting to socket 10576.");
      try {
        sock = new Socket("127.0.0.1", 10576);
        System.out.println("Connection to socket 10576 established.");
      } catch (Exception e) {
        System.out.println(
            "Inputting packets from file must be done while running Tossim with the -ri option");
        System.exit(-1);
      }

      DataOutputStream output = new DataOutputStream(sock.getOutputStream());

      while (true) {
        code = tokenizer.nextToken();
        if (code == tokenizer.TT_EOF) {
          break;
        } else if (code == StreamTokenizer.TT_EOL) {
        } else if (code == StreamTokenizer.TT_WORD) {
          String word = tokenizer.sval;
          long lval = Long.parseLong(word);

          code = tokenizer.nextToken();
          if (code != StreamTokenizer.TT_WORD) {
            break;
          }
          word = tokenizer.sval;
          short sval = Short.parseShort(word);

          byte[] data = new byte[36];
          for (int i = 0; i < 36; i++) {
            code = tokenizer.nextToken();
            if (code != StreamTokenizer.TT_WORD) {
              break;
            }
            String datum = tokenizer.sval;
            try {
              data[i] = (byte) (Integer.parseInt(datum, 16) & 0xff);
            } catch (NumberFormatException e) {
              System.out.println(e);
              System.out.println(datum);
            }
          }

          output.writeLong(lval);
          output.writeShort(sval);
          output.write(data);
        } else if (code == StreamTokenizer.TT_NUMBER) {
        }
      }
    } catch (Exception exception) {
      System.err.println("Exception thrown.");
      exception.printStackTrace();
    } finally {
      try {
        sock.close();
      } catch (Exception e) {
      }
    }
    /// ServerSocket server = new ServerSocket(10576, 1);
    // System.out.println("Waiting on socket 10576.");
    // Socket sock = server.accept();
    // System.out.println("Accepted connection from " + sock);

    // DataOutputStream input = new DataOutputStream(sock.getOutputStream());
  }
Ejemplo n.º 7
0
  /**
   * Return an interned VarInfoAux that represents a given string. Elements are separated by commas,
   * in the form:
   *
   * <p>x = a, "a key" = "a value"
   *
   * <p>Parse allow for quoted elements. White space to the left and right of keys and values do not
   * matter, but inbetween does.
   */
  public static /*@Interned*/ VarInfoAux parse(String inString) throws IOException {
    Reader inStringReader = new StringReader(inString);
    StreamTokenizer tok = new StreamTokenizer(inStringReader);
    tok.resetSyntax();
    tok.wordChars(0, Integer.MAX_VALUE);
    tok.quoteChar('\"');
    tok.whitespaceChars(' ', ' ');
    tok.ordinaryChar('[');
    tok.ordinaryChar(']');
    tok.ordinaryChars(',', ',');
    tok.ordinaryChars('=', '=');
    Map</*@Interned*/ String, /*@Interned*/ String> map = theDefault.map;

    String key = "";
    String value = "";
    boolean seenEqual = false;
    boolean insideVector = false;
    for (int tokInfo = tok.nextToken();
        tokInfo != StreamTokenizer.TT_EOF;
        tokInfo = tok.nextToken()) {
      @SuppressWarnings("interning") // initialization-checking pattern
      boolean mapUnchanged = (map == theDefault.map);
      if (mapUnchanged) {
        // We use default values if none are specified.  We initialize
        // here rather than above to save time when there are no tokens.

        map = new HashMap</*@Interned*/ String, /*@Interned*/ String>(theDefault.map);
      }

      /*@Interned*/ String token;
      if (tok.ttype == StreamTokenizer.TT_WORD || tok.ttype == '\"') {
        assert tok.sval != null
            : "@AssumeAssertion(nullness): representation invariant of StreamTokenizer";
        token = tok.sval.trim().intern();
      } else {
        token = ((char) tok.ttype + "").intern();
      }

      debug.fine("Token info: " + tokInfo + " " + token);

      if (token == "[") { // interned
        if (!seenEqual) throw new IOException("Aux option did not contain an '='");
        if (insideVector) throw new IOException("Vectors cannot be nested in an aux option");
        if (value.length() > 0) throw new IOException("Cannot mix scalar and vector values");

        insideVector = true;
        value = "";
      } else if (token == "]") { // interned
        if (!insideVector) throw new IOException("']' without preceding '['");
        insideVector = false;
      } else if (token == ",") { // interned
        if (!seenEqual) throw new IOException("Aux option did not contain an '='");
        if (insideVector) throw new IOException("',' cannot be used inside a vector");
        map.put(key.intern(), value.intern());
        key = "";
        value = "";
        seenEqual = false;
      } else if (token == "=") { // interned
        if (seenEqual) throw new IOException("Aux option contained more than one '='");
        if (insideVector) throw new IOException("'=' cannot be used inside a vector");
        seenEqual = true;
      } else {
        if (!seenEqual) {
          key = (key + " " + token).trim();
        } else if (insideVector) {
          value = value + " \"" + token.trim() + "\"";
        } else {
          value = (value + " " + token).trim();
        }
      }
    }

    if (seenEqual) {
      map.put(key.intern(), value.intern());
    }

    // Interning
    VarInfoAux result = new VarInfoAux(map).intern();
    assert interningMap != null
        : "@AssumeAssertion(nullness):  application invariant:  postcondition of intern(), which was just called";
    if (debug.isLoggable(Level.FINE)) {
      debug.fine("New parse " + result);
      debug.fine("Intern table size: " + new Integer(interningMap.size()));
    }
    return result;
  }