implemented java highlighting

Wed, 10 Jul 2013 17:57:03 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Jul 2013 17:57:03 +0200
changeset 17
7ea86024aef0
parent 16
fa0bcd0444eb
child 18
5085b57e3fd6

implemented java highlighting

Makefile file | annotate | diff | comparison | revisions
src/c2html.c file | annotate | diff | comparison | revisions
test/Game.java file | annotate | diff | comparison | revisions
test/jheader.html file | annotate | diff | comparison | revisions
--- a/Makefile	Wed Jul 10 16:31:16 2013 +0200
+++ b/Makefile	Wed Jul 10 17:57:03 2013 +0200
@@ -39,6 +39,10 @@
 test: compile
 	./build/$(BIN) $(ARGS) src/c2html.c > build/body.html
 	cat test/header.html build/body.html test/footer.html > build/code.html
+
+test-java: compile
+	./build/$(BIN) $(ARGS) -j test/Game.java > build/body.html
+	cat test/jheader.html build/body.html test/footer.html > build/code.html
 	
 clean:
 	$(RM) -f -R build
--- a/src/c2html.c	Wed Jul 10 16:31:16 2013 +0200
+++ b/src/c2html.c	Wed Jul 10 17:57:03 2013 +0200
@@ -45,20 +45,30 @@
   "while", NULL
 };
 
-int istype(char *word, size_t len) {
+const char* jkeywords[] = {
+  "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
+  "package", "synchronized", "boolean", "do", "if", "private", "this",
+  "break", "double", "implements", "protected", "throw", "byte", "else",
+  "import", "public", "throws", "case", "enum", "instanceof", "return",
+  "transient", "catch", "extends", "int", "short", "try", "char", "final",
+  "interface", "static", "void", "class", "finally", "long", "strictfp",
+  "volatile", "const", "float", "native", "super", "while", NULL
+};
+
+int isctype(char *word, size_t len) {
   return (word[len-2] == '_' && word[len-1] == 't');
 }
 
-int isdirective(char *word) {
+int iscdirective(char *word) {
   return (word[0] == '#');
 }
 
-int notypes(char *word, size_t len) {
-  return 0;
+int isjtype(char *word, size_t len) {
+  return isupper(word[0]);
 }
 
-int nodirectives(char *word) {
-  return 0;
+int isjdirective(char *word) {
+  return word[0] == '@';
 }
 
 typedef struct {
@@ -278,7 +288,7 @@
       } else {
         if (isstring) {
           dp = writeescapedchar(dest, dp, c);
-        } else if (!isalnum(c) && c != '_' && c != '#' && c != '.') {
+        } else if (!isalnum(c) && c!='_' && c!='#' && c!='.' && c!='@') {
           /* interpret word int_t */
           if (wp > 0 && wp < WORDBUF_SIZE) {
             int closespan = 1;
@@ -336,6 +346,7 @@
       "  c2html [Options] FILE\n\n"
       " Options:\n"
       "  -h                    Prints this help message\n"
+      "  -j                    Highlight Java instead of C source code\n"
       "  -o <output>           Output file (if not specified, stdout is used)\n"
       "  -p                    Disable highlighting (plain text)\n"
       "\n");
@@ -356,18 +367,23 @@
   settings.highlight = 1;
   
   highlighter_t highlighter;
-  highlighter.isdirective = isdirective;
-  highlighter.istype = istype;
+  highlighter.isdirective = iscdirective;
+  highlighter.istype = isctype;
   highlighter.keywords = ckeywords;
   
   char optc;
-  while ((optc = getopt(argc, argv, "ho:p")) != -1) {
+  while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
     switch (optc) {
       case 'o':
         if (!(optarg[0] == '-' && optarg[1] == 0)) {
           settings.outfilename = optarg;
         }
         break;
+      case 'j':
+        highlighter.isdirective = isjdirective;
+        highlighter.istype = isjtype;
+        highlighter.keywords = jkeywords;
+        break;
       case 'p':
         settings.highlight = 0;
         break;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/Game.java	Wed Jul 10 17:57:03 2013 +0200
@@ -0,0 +1,94 @@
+package de.uapcore.threelittlestars;
+
+import java.awt.BorderLayout;
+import java.io.IOException;
+
+import javax.swing.JFrame;
+import javax.swing.JOptionPane;
+
+import de.uapcore.threelittlestars.entities.Player;
+import de.uapcore.threelittlestars.entities.Player.PlayerCharacter;
+import de.uapcore.threelittlestars.managers.AssetManager;
+import de.uapcore.threelittlestars.managers.InputManager;
+import de.uapcore.threelittlestars.managers.WorldManager;
+import de.uapcore.threelittlestars.renderers.GameRenderer;
+
+
+public class Game implements Runnable {
+   
+   public static final int TICK_RATE = 32;
+
+   private InputManager im;
+   private AssetManager am;
+   private WorldManager wm;
+   
+   private MainFrame frame;
+   private MainPanel canvas;
+
+   public Game(MainFrame frame, PlayerCharacter playerCharacter) {
+
+      this.frame = frame;
+      canvas = new MainPanel();
+      
+      am = new AssetManager(canvas);
+      wm = new WorldManager(am);
+      im = new InputManager();
+      
+      canvas.setRenderer(new GameRenderer(wm));
+      canvas.addKeyListener(im);
+      
+      frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
+      frame.add(canvas, BorderLayout.CENTER);
+      frame.pack();
+      canvas.requestFocusInWindow();
+
+      try {
+         startGame(playerCharacter);
+      } catch (IOException e) {
+         JOptionPane.showMessageDialog(frame, "Die Weltdaten sind beschÃĪdigt.",
+               frame.getTitle(), JOptionPane.ERROR_MESSAGE);
+         System.exit(1);
+      }
+   }
+   
+   public void startGame(PlayerCharacter character) throws IOException {
+      Player.setCharacter(character);
+      
+      // TODO: asset / savegame loading
+      wm.loadAsset("testworld"); // just testing here
+      
+      new Thread(this).start();
+   }
+   
+   @Override
+   public void run() {
+      long lastTick = System.currentTimeMillis();
+      do {
+         long currentTick = System.currentTimeMillis();
+         if (currentTick - lastTick >= TICK_RATE) {
+            lastTick += TICK_RATE;
+            
+            // Deliver buffered input events
+            im.deliverEvents(wm);
+            
+            // Call updates
+            wm.update();
+            
+            // Catch other key events
+            if (im.isEscapePressed()) {
+               frame.setVisible(false);
+            }
+            
+            
+            // Repaint canvas
+            canvas.repaint();
+         }
+         Thread.yield();
+      } while (frame.isVisible());
+      
+      // Cleanup stuff
+      frame.dispose();
+      System.exit(0);
+   }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jheader.html	Wed Jul 10 17:57:03 2013 +0200
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>c2html</title>
+    <style type="text/css">
+      span.c2html-lineno {
+        font-style: italic;
+        color: grey;
+      }
+      span.c2html-keyword {
+        color: blue;
+      }
+      span.c2html-macroconst {
+        color: cornflowerblue;
+      }
+      span.c2html-type {
+        color: teal;
+      }
+      span.c2html-directive {
+        color: silver;
+      }
+      span.c2html-string {
+        color: darkorange;
+      }
+      span.c2html-comment {
+        color: grey;
+      }
+      span.c2html-stdinclude, span.c2html-userinclude, a.c2html-userinclude {
+      }
+    </style>
+  </head>
+  <body>
+

mercurial