Beispiel #1
0
 private static Object parseValue(CharacterIterator it) {
   switch (it.current()) {
     case '{':
       return parseObject(it);
     case '[':
       return parseArray(it);
     case '"':
       return parseString(it);
     case '-':
     case '0':
     case '1':
     case '2':
     case '3':
     case '4':
     case '5':
     case '6':
     case '7':
     case '8':
     case '9':
       return parseNumber(it);
     case 't':
       parseText(Boolean.TRUE.toString(), it);
       return Boolean.TRUE;
     case 'f':
       parseText(Boolean.FALSE.toString(), it);
       return Boolean.FALSE;
     case 'n':
       parseText(NULL, it);
       return null;
   }
   throw error(
       "Bad JSON starting character '" + it.current() + "'", it); // $NON-NLS-1$ //$NON-NLS-2$;
 }
Beispiel #2
0
  private static List parseArray(CharacterIterator it) {
    it.next();
    parseWhitespace(it);
    if (it.current() == ']') {
      it.next();
      return Collections.EMPTY_LIST;
    }

    List list = new ArrayList();
    while (true) {
      Object value = parseValue(it);
      list.add(value);
      parseWhitespace(it);
      if (it.current() == ',') {
        it.next();
        parseWhitespace(it);
        continue;
      }

      if (it.current() != ']')
        throw error(
            "expected an array close ']' but was '" + it.current() + "'",
            it); //$NON-NLS-1$ //$NON-NLS-2$
      break;
    }
    it.next();
    return list;
  }
Beispiel #3
0
 public int current() {
   int c = iterator.current();
   if (c == CharacterIterator.DONE) {
     return DONE;
   }
   return c;
 }
Beispiel #4
0
  private static Object parse(CharacterIterator it) {
    parseWhitespace(it);
    Object result = parseValue(it);
    parseWhitespace(it);

    if (it.current() != CharacterIterator.DONE) throw error("should be done", it); // $NON-NLS-1$
    return result;
  }
Beispiel #5
0
 public int next() {
   int i = iterator.current();
   iterator.next();
   if (i == CharacterIterator.DONE) {
     return DONE;
   }
   return i;
 }
Beispiel #6
0
 private static void parseText(String string, CharacterIterator it) {
   int length = string.length();
   char c = it.current();
   for (int i = 0; i < length; i++) {
     if (c != string.charAt(i))
       throw error(
           "expected to parse '" + string + "' but character " + (i + 1) + " was '" + c + "'",
           it); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$;
     c = it.next();
   }
 }
Beispiel #7
0
 public static Tile[] fromPuzzleString(String puzzleAsString) {
   Tile[] puzzle = new Tile[puzzleAsString.length()];
   CharacterIterator charIterator = new StringCharacterIterator(puzzleAsString);
   for (int y = 0; y < 9; ++y) {
     for (int x = 0; x < 9; ++x) {
       puzzle[y * 9 + x] = new Tile(x, y, charIterator.current() - '0');
       charIterator.next();
     }
   }
   return puzzle;
 }
 /** Returns the count of next character. */
 private int getCurrentCodePointCount() {
   char c1 = text.current();
   if (Character.isHighSurrogate(c1) && text.getIndex() < text.getEndIndex()) {
     char c2 = text.next();
     text.previous();
     if (Character.isLowSurrogate(c2)) {
       return 2;
     }
   }
   return 1;
 }
 /** Returns current character */
 int getCurrent() {
   char c1 = text.current();
   if (Character.isHighSurrogate(c1) && text.getIndex() < text.getEndIndex()) {
     char c2 = text.next();
     text.previous();
     if (Character.isLowSurrogate(c2)) {
       return Character.toCodePoint(c1, c2);
     }
   }
   return (int) c1;
 }
  @Nullable
  private static String parseClassSignature(
      final CharacterIterator signatureIterator, final List<String> convertedInterfaces)
      throws ClsFormatException {
    final String convertedSuper =
        SignatureParsing.parseTopLevelClassRefSignature(signatureIterator);
    while (signatureIterator.current() != CharacterIterator.DONE) {
      final String ifs = SignatureParsing.parseTopLevelClassRefSignature(signatureIterator);
      if (ifs == null) throw new ClsFormatException();

      convertedInterfaces.add(ifs);
    }
    return convertedSuper;
  }
Beispiel #11
0
 private static Object parseNumber(CharacterIterator it) {
   StringBuffer buffer = new StringBuffer();
   char c = it.current();
   while (Character.isDigit(c) || c == '-' || c == '+' || c == '.' || c == 'e' || c == 'E') {
     buffer.append(c);
     c = it.next();
   }
   try {
     return new BigDecimal(buffer.toString());
   } catch (NumberFormatException e) {
     throw error(
         "expected a number but was '" + buffer.toString() + "'",
         it); //$NON-NLS-1$ //$NON-NLS-2$;
   }
 }
 public Object read(CharacterIterator ci, int start) {
   it = ci;
   switch (start) {
     case FIRST:
       c = it.first();
       break;
     case CURRENT:
       c = it.current();
       break;
     case NEXT:
       c = it.next();
       break;
   }
   return read();
 }
Beispiel #13
0
  private static Map parseObject(CharacterIterator it) {
    it.next();
    parseWhitespace(it);
    if (it.current() == '}') {
      it.next();
      return Collections.EMPTY_MAP;
    }

    Map map = new HashMap();
    while (true) {
      if (it.current() != '"')
        throw error(
            "expected a string start '\"' but was '" + it.current() + "'",
            it); //$NON-NLS-1$ //$NON-NLS-2$
      String key = parseString(it);
      if (map.containsKey(key))
        throw error("' already defined" + "key '" + key, it); // $NON-NLS-1$ //$NON-NLS-2$
      parseWhitespace(it);
      if (it.current() != ':')
        throw error(
            "expected a pair separator ':' but was '" + it.current() + "'",
            it); //$NON-NLS-1$ //$NON-NLS-2$
      it.next();
      parseWhitespace(it);
      Object value = parseValue(it);
      map.put(key, value);
      parseWhitespace(it);
      if (it.current() == ',') {
        it.next();
        parseWhitespace(it);
        continue;
      }

      if (it.current() != '}')
        throw error(
            "expected an object close '}' but was '" + it.current() + "'",
            it); //$NON-NLS-1$ //$NON-NLS-2$
      break;
    }
    it.next();
    return map;
  }
Beispiel #14
0
    /**
     * The <code>setBreakPoint()</code> method inserts a breakpoint into the program. It does so by
     * inserting a special probe that will call back into the <code>commandLoop()</code> before the
     * instruction executes.
     *
     * @param i the iterator over the characters of the command
     * @param on true if the breakpoint should be inserted; false if it should be removed
     * @throws IOException if there is a problem communicating over the socket
     */
    void setBreakPoint(CharacterIterator i, boolean on) throws IOException {
      // TODO: deal with length as well!
      char num = i.current();
      i.next();
      switch (num) {
        case '0':
        case '1':
          if (!StringUtil.peekAndEat(i, ',')) break;
          int addr = StringUtil.readHexValue(i, 4);
          if (!StringUtil.peekAndEat(i, ',')) break;
          int len = StringUtil.readHexValue(i, 4);
          for (int cntr = addr; cntr < addr + len; cntr += 2) setBreakPoint(addr, on);
          sendPacketOK("OK");
          return;
        case '2':
          // TODO: other breakpoint types?
        case '3':
          // TODO: other breakpoint types?
        default:
      }

      sendPacketOK("");
    }
Beispiel #15
0
 private static void parseWhitespace(CharacterIterator it) {
   char c = it.current();
   while (Character.isWhitespace(c)) c = it.next();
 }
  public int next() {
    startPosition = iterator.getIndex();
    char c = iterator.current();
    iterator.next(); // advance
    if (c == CharacterIterator.DONE) {
      kind = EOF;
    } else if (Character.isJavaIdentifierStart(c)) {
      buf.append(c);
      boolean couldBeKeyword = Character.isLowerCase(c);
      while (true) {
        c = iterator.current();
        if (!Character.isJavaIdentifierPart(c)) break;
        buf.append(c);
        if (couldBeKeyword) {
          if (!Character.isLowerCase(c) || buf.length() > MAX_KEYWORD_LENGTH)
            couldBeKeyword = false;
        }
        c = iterator.next();
      }
      kind = NORMAL_TEXT;
      if (couldBeKeyword) {
        if (KEYWORDS.contains(buf.toString())) kind = KEYWORD;
      }
      buf.setLength(0);
    } else if (c == '/') {
      char c2 = iterator.current();
      if (c2 == '/') {
        while (true) {
          c2 = iterator.next();
          if (c2 == '\n' || c2 == '\r' || c2 == CharacterIterator.DONE) break;
        }
        kind = COMMENT;
        return kind;
      } else if (c2 == '*') {
        scanComment:
        while (c2 != CharacterIterator.DONE) {
          c2 = iterator.next();
          if (c2 == '*') {
            do {
              c2 = iterator.next();
              if (c2 == '/') break scanComment;
            } while (c2 == '*');
          }
        }
        kind = JAVADOC;
        return kind;
      }
    } else if (c == '"') {
      kind = QUOTE;
      char c2 = iterator.current();
      while (c2 != '"' && c2 != '\n' && c2 != '\r' && c2 != CharacterIterator.DONE) {
        if (c2 == '\\') {
          c2 = iterator.next();
          if (c2 == '\n' || c2 == '\r') break;
        }
        c2 = iterator.next();
      }
      iterator.next(); // advance past closing char
    } else if (c == '\'') {
      // need to catch '"' so isn't considered to start a String
      kind = QUOTE; // or NORMAL_TEXT ?
      char c2 = iterator.current();
      if (c2 == '\\') c2 = iterator.next(); // advance past the escape char
      if (c2 != '\n' && c2 != '\r' && c2 != CharacterIterator.DONE)
        c2 = iterator.next(); // advance past the content char
      if (c2 != '\n' && c2 != '\r' && c2 != CharacterIterator.DONE)
        iterator.next(); // advance past closing char

    } else kind = NORMAL_TEXT;
    // System.out.println(kind + " " + startPosition + "-" + iterator.getIndex());
    return kind;
  }
Beispiel #17
0
    /**
     * The <code>executeCommand()</code> method executes a single command that has been read from
     * the socket and packaged as a string. If the command should resume the simulation, this method
     * will return <code>true</code>, and <code>false</code> otherwise.
     *
     * @param command the command packaged as a string
     * @return true if the simulation should resume; false otherwise
     * @throws IOException if there is a problem communicating over the socket
     */
    boolean executeCommand(String command) throws IOException {
      CharacterIterator i = new StringCharacterIterator(command);
      if (i.current() == '+') i.next();
      if (!StringUtil.peekAndEat(i, '$')) {
        commandError();
        return false;
      }

      char c = i.current();
      i.next();

      switch (c) {
        case 'c':
          // CONTINUE WITH EXECUTION
          // TODO: implement continue at address
          sendPlus();
          return true;
        case 'D':
          // DISCONNECT
          Terminal.println("GDBServer: disconnected");
          sendPlus();
          simulator.stop();
          return true;
        case 'g':
          // READ REGISTERS
          readAllRegisters();
          return false;
        case 'G':
          // WRITE REGISTERS
          // TODO: implement update of registers
          break;
        case 'H':
          // SET THREAD CONTEXT -- THERE IS ONLY ONE
          sendPacketOK("OK");
          return false;
        case 'i':
          // STEP CYCLE
          isStepping = true;
          break;
        case 'k':
          // KILL
          Terminal.println("GDBServer: killed remotely");
          sendPlus();
          simulator.stop();
          return true;
        case 'm':
          readMemory(i);
          return false;
        case 'M':
          // WRITE MEMORY
          // TODO: implement writes to memory
          break;
        case 'p':
          // READ SELECTED REGISTERS
          readOneRegister(i);
          return false;
        case 'P':
          // WRITE SELECTED REGISTERS
          // TODO: implement writes to selected registers
          break;
        case 'q':
          // QUERY A VARIABLE
          // TODO: implement queries to variables
          break;
        case 's':
          // STEP INSTRUCTION
          isStepping = true;
          sendPlus();
          return true;
        case 'z':
          // REMOVE BREAKPOINT
          setBreakPoint(i, false);
          return false;
        case 'Z':
          // SET BREAKPOINT
          setBreakPoint(i, true);
          return false;
        case '?':
          // GET LAST SIGNAL
          sendPacketOK("S05");
          return false;
      }

      // didn't understand the comand
      sendPacketOK("");
      return false;
    }