Esempio n. 1
0
 static String substituteUnicode(String program) {
   // check for non-ascii chars (these will be/must be in unicode format)
   char p[] = program.toCharArray();
   int unicodeCount = 0;
   for (int i = 0; i < p.length; i++) {
     if (p[i] > 127) unicodeCount++;
   }
   if (unicodeCount == 0) return program;
   // if non-ascii chars are in there, convert to unicode escapes
   // add unicodeCount * 5.. replacing each unicode char
   // with six digit uXXXX sequence (xxxx is in hex)
   // (except for nbsp chars which will be a replaced with a space)
   int index = 0;
   char p2[] = new char[p.length + unicodeCount * 5];
   for (int i = 0; i < p.length; i++) {
     if (p[i] < 128) {
       p2[index++] = p[i];
     } else if (p[i] == 160) { // unicode for non-breaking space
       p2[index++] = ' ';
     } else {
       int c = p[i];
       p2[index++] = '\\';
       p2[index++] = 'u';
       char str[] = Integer.toHexString(c).toCharArray();
       // add leading zeros, so that the length is 4
       // for (int i = 0; i < 4 - str.length; i++) p2[index++] = '0';
       for (int m = 0; m < 4 - str.length; m++) p2[index++] = '0';
       System.arraycopy(str, 0, p2, index, str.length);
       index += str.length;
     }
   }
   return new String(p2, 0, index);
 }
Esempio n. 2
0
  /**
   * preprocesses a pde file and writes out a java file
   *
   * @return the class name of the exported Java
   */
  private String write(final String program, final PrintWriter stream)
      throws SketchException, RecognitionException, TokenStreamException {

    // Match on the uncommented version, otherwise code inside comments used
    // http://code.google.com/p/processing/issues/detail?id=1404
    String uncomment = scrubComments(program);
    PdeRecognizer parser = createParser(program);
    if (PUBLIC_CLASS.matcher(uncomment).find()) {
      try {
        final PrintStream saved = System.err;
        try {
          // throw away stderr for this tentative parse
          System.setErr(new PrintStream(new ByteArrayOutputStream()));
          parser.javaProgram();
        } finally {
          System.setErr(saved);
        }
        setMode(Mode.JAVA);
      } catch (Exception e) {
        // I can't figure out any other way of resetting the parser.
        parser = createParser(program);
        parser.pdeProgram();
      }
    } else if (FUNCTION_DECL.matcher(uncomment).find()) {
      setMode(Mode.ACTIVE);
      parser.activeProgram();
    } else {
      parser.pdeProgram();
    }

    // set up the AST for traversal by PdeEmitter
    //
    ASTFactory factory = new ASTFactory();
    AST parserAST = parser.getAST();
    AST rootNode = factory.create(ROOT_ID, "AST ROOT");
    rootNode.setFirstChild(parserAST);

    makeSimpleMethodsPublic(rootNode);

    // unclear if this actually works, but it's worth a shot
    //
    // ((CommonAST)parserAST).setVerboseStringConversion(
    //  true, parser.getTokenNames());
    // (made to use the static version because of jikes 1.22 warning)
    BaseAST.setVerboseStringConversion(true, parser.getTokenNames());

    final String className;
    if (mode == Mode.JAVA) {
      // if this is an advanced program, the classname is already defined.
      className = getFirstClassName(parserAST);
    } else {
      className = this.name;
    }

    // if 'null' was passed in for the name, but this isn't
    // a 'java' mode class, then there's a problem, so punt.
    //
    if (className == null) return null;

    // debug
    if (false) {
      final StringWriter buf = new StringWriter();
      final PrintWriter bufout = new PrintWriter(buf);
      writeDeclaration(bufout, className);
      new PdeEmitter(this, bufout).print(rootNode);
      writeFooter(bufout, className);
      debugAST(rootNode, true);
      System.err.println(buf.toString());
    }

    writeDeclaration(stream, className);
    new PdeEmitter(this, stream).print(rootNode);
    writeFooter(stream, className);

    // if desired, serialize the parse tree to an XML file.  can
    // be viewed usefully with Mozilla or IE
    if (Preferences.getBoolean("preproc.output_parse_tree")) {
      writeParseTree("parseTree.xml", parserAST);
    }

    return className;
  }