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
     1.1 --- a/Makefile	Wed Jul 10 16:31:16 2013 +0200
     1.2 +++ b/Makefile	Wed Jul 10 17:57:03 2013 +0200
     1.3 @@ -39,6 +39,10 @@
     1.4  test: compile
     1.5  	./build/$(BIN) $(ARGS) src/c2html.c > build/body.html
     1.6  	cat test/header.html build/body.html test/footer.html > build/code.html
     1.7 +
     1.8 +test-java: compile
     1.9 +	./build/$(BIN) $(ARGS) -j test/Game.java > build/body.html
    1.10 +	cat test/jheader.html build/body.html test/footer.html > build/code.html
    1.11  	
    1.12  clean:
    1.13  	$(RM) -f -R build
     2.1 --- a/src/c2html.c	Wed Jul 10 16:31:16 2013 +0200
     2.2 +++ b/src/c2html.c	Wed Jul 10 17:57:03 2013 +0200
     2.3 @@ -45,20 +45,30 @@
     2.4    "while", NULL
     2.5  };
     2.6  
     2.7 -int istype(char *word, size_t len) {
     2.8 +const char* jkeywords[] = {
     2.9 +  "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
    2.10 +  "package", "synchronized", "boolean", "do", "if", "private", "this",
    2.11 +  "break", "double", "implements", "protected", "throw", "byte", "else",
    2.12 +  "import", "public", "throws", "case", "enum", "instanceof", "return",
    2.13 +  "transient", "catch", "extends", "int", "short", "try", "char", "final",
    2.14 +  "interface", "static", "void", "class", "finally", "long", "strictfp",
    2.15 +  "volatile", "const", "float", "native", "super", "while", NULL
    2.16 +};
    2.17 +
    2.18 +int isctype(char *word, size_t len) {
    2.19    return (word[len-2] == '_' && word[len-1] == 't');
    2.20  }
    2.21  
    2.22 -int isdirective(char *word) {
    2.23 +int iscdirective(char *word) {
    2.24    return (word[0] == '#');
    2.25  }
    2.26  
    2.27 -int notypes(char *word, size_t len) {
    2.28 -  return 0;
    2.29 +int isjtype(char *word, size_t len) {
    2.30 +  return isupper(word[0]);
    2.31  }
    2.32  
    2.33 -int nodirectives(char *word) {
    2.34 -  return 0;
    2.35 +int isjdirective(char *word) {
    2.36 +  return word[0] == '@';
    2.37  }
    2.38  
    2.39  typedef struct {
    2.40 @@ -278,7 +288,7 @@
    2.41        } else {
    2.42          if (isstring) {
    2.43            dp = writeescapedchar(dest, dp, c);
    2.44 -        } else if (!isalnum(c) && c != '_' && c != '#' && c != '.') {
    2.45 +        } else if (!isalnum(c) && c!='_' && c!='#' && c!='.' && c!='@') {
    2.46            /* interpret word int_t */
    2.47            if (wp > 0 && wp < WORDBUF_SIZE) {
    2.48              int closespan = 1;
    2.49 @@ -336,6 +346,7 @@
    2.50        "  c2html [Options] FILE\n\n"
    2.51        " Options:\n"
    2.52        "  -h                    Prints this help message\n"
    2.53 +      "  -j                    Highlight Java instead of C source code\n"
    2.54        "  -o <output>           Output file (if not specified, stdout is used)\n"
    2.55        "  -p                    Disable highlighting (plain text)\n"
    2.56        "\n");
    2.57 @@ -356,18 +367,23 @@
    2.58    settings.highlight = 1;
    2.59    
    2.60    highlighter_t highlighter;
    2.61 -  highlighter.isdirective = isdirective;
    2.62 -  highlighter.istype = istype;
    2.63 +  highlighter.isdirective = iscdirective;
    2.64 +  highlighter.istype = isctype;
    2.65    highlighter.keywords = ckeywords;
    2.66    
    2.67    char optc;
    2.68 -  while ((optc = getopt(argc, argv, "ho:p")) != -1) {
    2.69 +  while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
    2.70      switch (optc) {
    2.71        case 'o':
    2.72          if (!(optarg[0] == '-' && optarg[1] == 0)) {
    2.73            settings.outfilename = optarg;
    2.74          }
    2.75          break;
    2.76 +      case 'j':
    2.77 +        highlighter.isdirective = isjdirective;
    2.78 +        highlighter.istype = isjtype;
    2.79 +        highlighter.keywords = jkeywords;
    2.80 +        break;
    2.81        case 'p':
    2.82          settings.highlight = 0;
    2.83          break;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/Game.java	Wed Jul 10 17:57:03 2013 +0200
     3.3 @@ -0,0 +1,94 @@
     3.4 +package de.uapcore.threelittlestars;
     3.5 +
     3.6 +import java.awt.BorderLayout;
     3.7 +import java.io.IOException;
     3.8 +
     3.9 +import javax.swing.JFrame;
    3.10 +import javax.swing.JOptionPane;
    3.11 +
    3.12 +import de.uapcore.threelittlestars.entities.Player;
    3.13 +import de.uapcore.threelittlestars.entities.Player.PlayerCharacter;
    3.14 +import de.uapcore.threelittlestars.managers.AssetManager;
    3.15 +import de.uapcore.threelittlestars.managers.InputManager;
    3.16 +import de.uapcore.threelittlestars.managers.WorldManager;
    3.17 +import de.uapcore.threelittlestars.renderers.GameRenderer;
    3.18 +
    3.19 +
    3.20 +public class Game implements Runnable {
    3.21 +   
    3.22 +   public static final int TICK_RATE = 32;
    3.23 +
    3.24 +   private InputManager im;
    3.25 +   private AssetManager am;
    3.26 +   private WorldManager wm;
    3.27 +   
    3.28 +   private MainFrame frame;
    3.29 +   private MainPanel canvas;
    3.30 +
    3.31 +   public Game(MainFrame frame, PlayerCharacter playerCharacter) {
    3.32 +
    3.33 +      this.frame = frame;
    3.34 +      canvas = new MainPanel();
    3.35 +      
    3.36 +      am = new AssetManager(canvas);
    3.37 +      wm = new WorldManager(am);
    3.38 +      im = new InputManager();
    3.39 +      
    3.40 +      canvas.setRenderer(new GameRenderer(wm));
    3.41 +      canvas.addKeyListener(im);
    3.42 +      
    3.43 +      frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    3.44 +      frame.add(canvas, BorderLayout.CENTER);
    3.45 +      frame.pack();
    3.46 +      canvas.requestFocusInWindow();
    3.47 +
    3.48 +      try {
    3.49 +         startGame(playerCharacter);
    3.50 +      } catch (IOException e) {
    3.51 +         JOptionPane.showMessageDialog(frame, "Die Weltdaten sind beschÃĪdigt.",
    3.52 +               frame.getTitle(), JOptionPane.ERROR_MESSAGE);
    3.53 +         System.exit(1);
    3.54 +      }
    3.55 +   }
    3.56 +   
    3.57 +   public void startGame(PlayerCharacter character) throws IOException {
    3.58 +      Player.setCharacter(character);
    3.59 +      
    3.60 +      // TODO: asset / savegame loading
    3.61 +      wm.loadAsset("testworld"); // just testing here
    3.62 +      
    3.63 +      new Thread(this).start();
    3.64 +   }
    3.65 +   
    3.66 +   @Override
    3.67 +   public void run() {
    3.68 +      long lastTick = System.currentTimeMillis();
    3.69 +      do {
    3.70 +         long currentTick = System.currentTimeMillis();
    3.71 +         if (currentTick - lastTick >= TICK_RATE) {
    3.72 +            lastTick += TICK_RATE;
    3.73 +            
    3.74 +            // Deliver buffered input events
    3.75 +            im.deliverEvents(wm);
    3.76 +            
    3.77 +            // Call updates
    3.78 +            wm.update();
    3.79 +            
    3.80 +            // Catch other key events
    3.81 +            if (im.isEscapePressed()) {
    3.82 +               frame.setVisible(false);
    3.83 +            }
    3.84 +            
    3.85 +            
    3.86 +            // Repaint canvas
    3.87 +            canvas.repaint();
    3.88 +         }
    3.89 +         Thread.yield();
    3.90 +      } while (frame.isVisible());
    3.91 +      
    3.92 +      // Cleanup stuff
    3.93 +      frame.dispose();
    3.94 +      System.exit(0);
    3.95 +   }
    3.96 +
    3.97 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/jheader.html	Wed Jul 10 17:57:03 2013 +0200
     4.3 @@ -0,0 +1,33 @@
     4.4 +<!DOCTYPE html>
     4.5 +<html>
     4.6 +  <head>
     4.7 +    <title>c2html</title>
     4.8 +    <style type="text/css">
     4.9 +      span.c2html-lineno {
    4.10 +        font-style: italic;
    4.11 +        color: grey;
    4.12 +      }
    4.13 +      span.c2html-keyword {
    4.14 +        color: blue;
    4.15 +      }
    4.16 +      span.c2html-macroconst {
    4.17 +        color: cornflowerblue;
    4.18 +      }
    4.19 +      span.c2html-type {
    4.20 +        color: teal;
    4.21 +      }
    4.22 +      span.c2html-directive {
    4.23 +        color: silver;
    4.24 +      }
    4.25 +      span.c2html-string {
    4.26 +        color: darkorange;
    4.27 +      }
    4.28 +      span.c2html-comment {
    4.29 +        color: grey;
    4.30 +      }
    4.31 +      span.c2html-stdinclude, span.c2html-userinclude, a.c2html-userinclude {
    4.32 +      }
    4.33 +    </style>
    4.34 +  </head>
    4.35 +  <body>
    4.36 +

mercurial