Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

351
LINES

< > BotCompany Repo | #1014799 // Try to make a JavaX tokenizer for RSTA [dev.]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 6678K of libraries. Click here for Pure Java version (10625L/75K).

!7

sS snippetID;
sS savedText, remoteText;
static EditorFrame e;

import org.fife.ui.rsyntaxtextarea.*;

sclass JavaXTokenizer extends AbstractTokenMaker {
  public TokenMap getWordsToHighlight() {
	ret new TokenMap;
  }

  int currentTokenStart, currentTokenType, startTokenType;
  
  /**
 * Returns a list of tokens representing the given text.
 *
 * @param text The text to break into tokens.
 * @param startTokenType The token with which to start tokenizing.
 * @param startOffset The offset at which the line of tokens begins.
 * @return A linked list of tokens representing <code>text</code>.
 */
  public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
     resetTokenList();

   char[] array = text.array;
   int offset = text.offset;
   int count = text.count;
   int end = offset + count;

   // Token starting offsets are always of the form:
   // 'startOffset + (currentTokenStart-offset)', but since startOffset and
   // offset are constant, tokens' starting positions become:
   // 'newStartOffset+currentTokenStart'.
   int newStartOffset = startOffset - offset;

   currentTokenStart = offset;
   currentTokenType  = startTokenType;

   for (int i=offset; i<end; i++) {

      char c = array[i];

      switch (currentTokenType) {

         case Token.NULL:

            currentTokenStart = i;   // Starting a new token here.

            switch (c) {

               case ' ':
               case '\t':
                  currentTokenType = Token.WHITESPACE;
                  break;

               case '"':
                  currentTokenType = Token.LITERAL_STRING_DOUBLE_QUOTE;
                  break;

               case '#':
                  currentTokenType = Token.COMMENT_EOL;
                  break;

               default:
                  if (RSyntaxUtilities.isDigit(c)) {
                     currentTokenType = Token.LITERAL_NUMBER_DECIMAL_INT;
                     break;
                  }
                  else if (RSyntaxUtilities.isLetter(c) || c=='/' || c=='_') {
                     currentTokenType = Token.IDENTIFIER;
                     break;
                  }
                  
                  // Anything not currently handled - mark as an identifier
                  currentTokenType = Token.IDENTIFIER;
                  break;

            } // End of switch (c).

            break;

         case Token.WHITESPACE:

            switch (c) {

               case ' ':
               case '\t':
                  break;   // Still whitespace.

               case '"':
                  addToken(text, currentTokenStart,i-1, Token.WHITESPACE, newStartOffset+currentTokenStart);
                  currentTokenStart = i;
                  currentTokenType = Token.LITERAL_STRING_DOUBLE_QUOTE;
                  break;

               case '#':
                  addToken(text, currentTokenStart,i-1, Token.WHITESPACE, newStartOffset+currentTokenStart);
                  currentTokenStart = i;
                  currentTokenType = Token.COMMENT_EOL;
                  break;

               default:   // Add the whitespace token and start anew.

                  addToken(text, currentTokenStart,i-1, Token.WHITESPACE, newStartOffset+currentTokenStart);
                  currentTokenStart = i;

                  if (RSyntaxUtilities.isDigit(c)) {
                     currentTokenType = Token.LITERAL_NUMBER_DECIMAL_INT;
                     break;
                  }
                  else if (RSyntaxUtilities.isLetter(c) || c=='/' || c=='_') {
                     currentTokenType = Token.IDENTIFIER;
                     break;
                  }

                  // Anything not currently handled - mark as identifier
                  currentTokenType = Token.IDENTIFIER;

            } // End of switch (c).

            break;

         default: // Should never happen
         case Token.IDENTIFIER:

            switch (c) {

               case ' ':
               case '\t':
                  addToken(text, currentTokenStart,i-1, Token.IDENTIFIER, newStartOffset+currentTokenStart);
                  currentTokenStart = i;
                  currentTokenType = Token.WHITESPACE;
                  break;

               case '"':
                  addToken(text, currentTokenStart,i-1, Token.IDENTIFIER, newStartOffset+currentTokenStart);
                  currentTokenStart = i;
                  currentTokenType = Token.LITERAL_STRING_DOUBLE_QUOTE;
                  break;

               default:
                  if (RSyntaxUtilities.isLetterOrDigit(c) || c=='/' || c=='_') {
                     break;   // Still an identifier of some type.
                  }
                  // Otherwise, we're still an identifier (?).

            } // End of switch (c).

            break;

         case Token.LITERAL_NUMBER_DECIMAL_INT:

            switch (c) {

               case ' ':
               case '\t':
                  addToken(text, currentTokenStart,i-1, Token.LITERAL_NUMBER_DECIMAL_INT, newStartOffset+currentTokenStart);
                  currentTokenStart = i;
                  currentTokenType = Token.WHITESPACE;
                  break;

               case '"':
                  addToken(text, currentTokenStart,i-1, Token.LITERAL_NUMBER_DECIMAL_INT, newStartOffset+currentTokenStart);
                  currentTokenStart = i;
                  currentTokenType = Token.LITERAL_STRING_DOUBLE_QUOTE;
                  break;

               default:

                  if (RSyntaxUtilities.isDigit(c)) {
                     break;   // Still a literal number.
                  }

                  // Otherwise, remember this was a number and start over.
                  addToken(text, currentTokenStart,i-1, Token.LITERAL_NUMBER_DECIMAL_INT, newStartOffset+currentTokenStart);
                  i--;
                  currentTokenType = Token.NULL;

            } // End of switch (c).

            break;

         case Token.COMMENT_EOL:
            i = end - 1;
            addToken(text, currentTokenStart,i, currentTokenType, newStartOffset+currentTokenStart);
            // We need to set token type to null so at the bottom we don't add one more token.
            currentTokenType = Token.NULL;
            break;

         case Token.LITERAL_STRING_DOUBLE_QUOTE:
            if (c=='"') {
               addToken(text, currentTokenStart,i, Token.LITERAL_STRING_DOUBLE_QUOTE, newStartOffset+currentTokenStart);
               currentTokenType = Token.NULL;
            }
            break;

      } // End of switch (currentTokenType).

   } // End of for (int i=offset; i<end; i++).

   switch (currentTokenType) {

      // Remember what token type to begin the next line with.
      case Token.LITERAL_STRING_DOUBLE_QUOTE:
         addToken(text, currentTokenStart,end-1, currentTokenType, newStartOffset+currentTokenStart);
         break;

      // Do nothing if everything was okay.
      case Token.NULL:
         addNullToken();
         break;

      // All other token types don't continue to the next line...
      default:
         addToken(text, currentTokenStart,end-1, currentTokenType, newStartOffset+currentTokenStart);
         addNullToken();

   }

   // Return the first token in our linked list.
   return firstToken;

  }
}


p-substance {
  autoRestart();
  load('savedText);
  
  e = new EditorFrame;
  e.getDocument().setSyntaxStyle(new JavaXTokenizer);
  e._setFont(deriveFont(e._getFont(), 16));
  e.setText(savedText);
  showFrame(e);
  frameIcon(e, #1004655);
  
  addMenu(e, "New", "New text", f newText);

  addMenu(e, "Load", "Load snippet...", r {
    selectSnippetID_v2(vf1(f _loadSnippet));
  });
  
  addMenu(e, "Save", "Save snippet", r { saveSnippetThen(null) });
  
  addMenu(e, "Transpile",
    "Save & Quick Transpile", r { transpile(false) },
    "Save & Medium Transpile", r { transpile(true) });
  addMenu(e, "Activate", "Butter-Reload", f _butterReload);
  
  S snippetID = cast readPersistentVar('snippetID);
  if (snippetID != null) _loadSnippet(snippetID);

  hideConsole();
  
  awtCalcEvery(e, 1000, f update);
  onFrameDeactivated(e, f fullUpdate);
}

svoid update { update(windowActive(e)); }
svoid fullUpdate { update(true); }
svoid update(bool full) {
  S text = e.getText();
  setAndSaveIfChanged(savedText := text);
    
  if (!full) ret;
  
  Pair<Int, S> p = testBracketHygieneExt(text);
  S status = "";
  if (remoteText != null && neq(remoteText, text))
    status = "Changes. ";
  if (p == null)
    status += "Hygienic";
  else {
    status += p.b;
    //setCaretPosition(ta, p.a);
  }
  silentStatus(trim(status));
}

svoid status(S s) {
  silentStatus(infoBoxAndReturn(s));
}

svoid silentStatus(S s) {
  e.setStatus(s);
}

svoid saveSnippetThen(final Runnable next) {
  if (empty(snippetID)) ret;
  fS text = getText(e.textArea);
  status("Saving...");
  thread {
    fS page = editSnippet(snippetID, text);
    remoteText = text; // TODO: check save result
    status("Saved snippet: " + page);
    awtCallF(next);
  }
}

svoid _butterReload() {
  time { butterReload(snippetID); }
  infoBox("Reloaded in " + lastTiming_format100ms());
}

svoid transpile(final bool medium) {
  saveSnippetThen(r { _transpile(medium) });
}

svoid _transpile(final bool medium) {
  status("Transpiling...");
  //jdisable(btnEleuReload);
  
  thread "Transpiling" {
    try {
      final Pair<Bool, S> p = transpileOnServer(snippetID, medium ? "medium" : "quick");
      awt {
        if (p.a)
          status("Transpiled OK!");
        else {
          status("Transpilation error. " + p.b);
          showText("Transpilation Error", p.b);
        }
      }
    } catch print e {
      status("Transpilation problem. " + e);
    }
    //jenable(btnEleuReload);
  }
}

svoid _loadSnippet(S snippetID) {
  e.textArea.setText(remoteText = loadSnippet(snippetID));
  setAndSave('snippetID, snippetID);
  frameTitle(e, snippetID);
}

svoid newText {
  setAndSave(snippetID := null);
  remoteText = null;
  S lastText = e.getText();
  if (nempty(lastText)) logQuotedWithDate("saved-texts.log", lastText);
  e.setText("");
  frameTitle(e, programTitle());
}

svoid cleanMeUp {
  setAndSaveIfChanged(savedText := e.getText());
}

Author comment

Began life as a copy of #1014780

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1014799
Snippet name: Try to make a JavaX tokenizer for RSTA [dev.]
Eternal ID of this version: #1014799/8
Text MD5: eea7eac202bfa483846d944cd328e6b1
Transpilation MD5: c470766a357981dc0b56c271a1aa5eca
Author: stefan
Category: javax / gui
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-02 21:55:17
Source code size: 10363 bytes / 351 lines
Pitched / IR pitched: No / No
Views / Downloads: 415 / 1070
Version history: 7 change(s)
Referenced in: [show references]