Pessoal criei um codigo que verifica o inicio e o fim de um comentario, mas quando eu executo o codigo ele verifica apenas uma vez, como posso fazer para que o codigo fizesse uma verificação constante?
Use a classe java.io.StreamTokenizer (que consegue lidar com comentários e outras coisas parecidas) para efetuar o parse de seu texto.
Esse monte de flags está destruindo sua vida; use uma máquina de estados (se quiser efetuar uma análise léxica você mesmo) em vez desse monte de flags.
ViniGodoy, ja tentei usar o antlr, mas o grande problema e criar o codigo do parser, se tiver alguma referencia ou tutorial que mostra como usar a API, agradeço.
Pessoal criei este codigo, ele verifica a existencia da letra p e colori a letra, mas quando dou um espaço no texto ele para de reconhecer, alguem sabe porque isto acontece?
package shc;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
public class Teste extends JFrame implements DocumentListener {
JTextPane pane;
Document doc;
public Teste(){
pane = new JTextPane();
pane.setPreferredSize(new Dimension(400,500));
pane.setFont(new Font("Courier New",0,13));
doc = pane.getDocument();
doc.addDocumentListener(this);
add(pane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void setStyle(StyledDocument document, Color color, int offset, int length){
Style style = document.addStyle("style",null);
StyleConstants.setForeground(style, color);
StyleConstants.setBold(style, true);
document.setCharacterAttributes(offset, length, style, true);
}
private void LexicalAnalyzer() throws BadLocationException, IOException {
Reader reader = new BufferedReader(new StringReader(doc.getText(doc.getStartPosition().getOffset(), doc.getEndPosition().getOffset())));
StreamTokenizer tokens = new StreamTokenizer(reader);
while(tokens.nextToken() != StreamTokenizer.TT_EOF){
for(int i=0; i < tokens.sval.length(); i++){
char token = tokens.sval.charAt(i);
if(token == 'p'){
setStyle(pane.getStyledDocument(), new Color(127, 0, 65), i, tokens.lineno());
}
else {
setStyle(pane.getStyledDocument(), new Color(0, 0, 155), i, tokens.lineno());
}
}
}
}
private void start(){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
try {
LexicalAnalyzer();
} catch (BadLocationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void changedUpdate(DocumentEvent arg0) {}
public void insertUpdate(DocumentEvent arg0) {
start();
}
public void removeUpdate(DocumentEvent arg0) {
start();
}
public static void main(String[] string){
new Teste();
}
}
Baixe também o editor de textos do André (que está no outro post) e o plugin do antlr no eclipse. O código do editor é relativamente fácil de entender.
Xi, acho que é melhor você ler algo sobre análise léxica (teoria de compiladores).
Depois de pegar o jeito, a máquina de estados é algo fácil, só um pouco trabalhoso; mas é melhor você ler um livro, que eu tentar explicar isso para você.