Syntax-Erkennungs Problem

Beatsleigher

Bekanntes Mitglied
Moin,, moin.

Ich arbeite an einem kleinen Nebenprojekt für Universal Android Toolkit, namens Eddy (Ein Editor für Edify-Scripts).

Ich habe das grundlegende fertig, und auch (so weit wie ich Antworten im Netz finden konnte) die Syntax-Erkennung eingebaut, aber er erkennt nur Anführungszeichen, aber dann ist gleich der restliche Text in rot.

Die anderen zu-erkennenden Teile erkennt er zwar, schmeißt dann aber eine IllegalStateException.

Edit: Ich markiere die IllegalStateExceptions mit einem ←
Java:
    private void jTextPane1KeyTyped(java.awt.event.KeyEvent evt) {                                    
        
        new Thread() {
            @Override
            public void run() {
                String text = jTextPane1.getText();
                String line = null;
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new StringReader(text));
                    while ((line = reader.readLine()) != null) {
                        Matcher stringMatcher = stringPattern.matcher(line);
                        Matcher commentMatcher = commentPattern.matcher(line);
                        Matcher keyWordMatcher = keyWordPattern.matcher(line);
                        Matcher numberMatcher = numberPattern.matcher(line);
                        Matcher operatorMatcher = operatorPattern.matcher(line);
                        Matcher methodMatcher = methodPattern.matcher(line);
                        
                        if (stringMatcher.matches()) {
                            int start = stringMatcher.start(); // ← 
                            int end = stringMatcher.end(); // ←
                            styledDocument.setCharacterAttributes(start, end, stringStyle, true);
                        }
                        stringMatcher.reset();
                        
                        if (commentMatcher.find()) {
                            System.out.println("Match found: " + commentMatcher.group());
                            int start = stringMatcher.toMatchResult().start(); // ←
                            int end = stringMatcher.toMatchResult().end(); // ←
                            styledDocument.setCharacterAttributes(start, end, commentStyle, true);
                        }
                        //commentMatcher.reset();
                        
                        if (keyWordMatcher.matches()) {
                            int start = stringMatcher.start(); // ←
                            int end = stringMatcher.end(); // ←
                            styledDocument.setCharacterAttributes(start, end, keyWordStyle, true);
                        }
                        keyWordMatcher.reset();
                        
                        if (numberMatcher.matches()) {
                            int start = stringMatcher.start(); // ←
                            int end = stringMatcher.end(); // ←
                            styledDocument.setCharacterAttributes(start, end, numberStyle, true);
                        }
                        numberMatcher.reset();
                        
                        if (operatorMatcher.matches()) {
                            int start = stringMatcher.start(); // ←
                            int end = stringMatcher.end(); // ←
                            styledDocument.setCharacterAttributes(start, end, operatorStyle, true);
                        }
                        operatorMatcher.reset();
                        
                        if (methodMatcher.matches()) {
                            int start = stringMatcher.start(); // ←
                            int end = stringMatcher.end(); // ←
                            styledDocument.setCharacterAttributes(start, end, methodStyle, true);
                        }
                        methodMatcher.reset();
                        
                    }
                    reader.close();
                } catch (IOException ex) {
                    System.err.println("An error occurred while highlighting the syntax! " + ex.toString());
                    ex.printStackTrace(System.err);
                }
            }
        }.start();
                
    }

↑ Damit soll die Syntax erkannt werden.

Java:
private final Pattern stringPattern;
    private final Pattern commentPattern;
    private final Pattern keyWordPattern;
    private final Pattern numberPattern;
    private final Pattern operatorPattern;
    private final Pattern methodPattern;
    private final StyledDocument styledDocument;
    
    //<editor-fold defaultstate="collapsed" desc="Strings (Regex)">
    // Colour: Blue
    private static final String keyWords = "(if)|else|then|endif"; 
    
    // Colour: Cyan
    private static final String numbers = "[0-9]"; 
    
    // Colour: Gray
    private static final String operators = "[!=&+]";
    
    // Colour: Dark Gray
    private static final String diskOperations = "format|mount|unmount|is_mounted";
        
    // Colour: Dark Gray
    private static final String imageOperations = "write_raw_image|write_firmware_image";
        
    // Colour: Dark Gray
    private static final String fileOperations = "package_extract_file|package_extract_dir|delete|delete_recursive|symlink|"
            + "set_perm|set_perm_recursive|getprop|file_getprop";
            
    // Colour: Dark Gray
    private static final String patchingOperations = "apply_patch|apply_patch_check|apply_patch_space|run_program";
        
    
    // Colour: Dark Gray
    private static final String control = "assert|abort|ifelse";
        
    // Colour: Dark Gray
    private static final String predicates = "ls_substring|less_than_int|greater_than_int";
            
    // Colour: Dark Gray
    private static final String userFeedback = "show_progress|set_progress|ui_print|sleep";
    //</editor-fold>
    
    private final Style stringStyle;
    private final Style commentStyle;
    private final Style keyWordStyle;
    private final Style numberStyle;
    private final Style operatorStyle;
    private final Style methodStyle;
    
    public Eddy() {
        stringPattern = Pattern.compile("\"(.*)\"");
        commentPattern = Pattern.compile("^(#*)");
        keyWordPattern = Pattern.compile(keyWords);
        numberPattern = Pattern.compile(numbers);
        operatorPattern = Pattern.compile(operators);
        methodPattern = Pattern.compile(diskOperations + "|" + imageOperations + "|" + fileOperations + "|" + patchingOperations + "|" + control + "|" + predicates + "|" + userFeedback);
        styledDocument = (StyledDocument) new DefaultStyledDocument();
        initComponents();
        stringStyle = jTextPane1.addStyle("StringStyle", null);
        commentStyle = jTextPane1.addStyle("CommentStyle", null);
        keyWordStyle = jTextPane1.addStyle("KeyWordStyle", null);
        numberStyle = jTextPane1.addStyle("NumberStyle", null);
        operatorStyle = jTextPane1.addStyle("OperatorStyle", null);
        methodStyle = jTextPane1.addStyle("MethodStyle", null);
        StyleConstants.setForeground(stringStyle, Color.red);
        StyleConstants.setForeground(commentStyle, Color.green);
        StyleConstants.setForeground(keyWordStyle, Color.blue);
        StyleConstants.setForeground(numberStyle, Color.cyan);
        StyleConstants.setForeground(operatorStyle, Color.gray);
        StyleConstants.setForeground(methodStyle, Color.darkGray);
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(this.getClass().getResource("/eu/m4gkbeatz/androidtoolkit/resources/eddy/icon.png")).getImage());
        this.setTitle("Eddy - Edify Code Editor");
    }

↑ Das sind die RegEx-en.

Ich weiß nicht wo ich den Fehler eingebaut habe.

Jede Hilfe mit diesem Problem wird begrüßt.

Danke im Voraus,
Beats
 
Zuletzt bearbeitet:

Zurück
Oben